blob: b48c1b6aaba23230c40a242795b741de9cedcc63 [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());
259 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
260 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
261 VT = MVT::f32;
262 Extend = true;
263 }
264
265 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
266 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000267 if (Extend) {
268 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
269 MVT::f32);
270 } else {
271 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
272 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000273 }
274 break;
275 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000276 case ISD::TokenFactor: {
277 std::vector<SDOperand> Ops;
278 bool Changed = false;
279 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000280 SDOperand Op = Node->getOperand(i);
281 // Fold single-use TokenFactor nodes into this token factor as we go.
282 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
283 Changed = true;
284 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
285 Ops.push_back(LegalizeOp(Op.getOperand(j)));
286 } else {
287 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
288 Changed |= Ops[i] != Op;
289 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000290 }
291 if (Changed)
292 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
293 break;
294 }
295
Chris Lattnerdc750592005-01-07 07:47:09 +0000296 case ISD::ADJCALLSTACKDOWN:
297 case ISD::ADJCALLSTACKUP:
298 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
299 // There is no need to legalize the size argument (Operand #1)
300 if (Tmp1 != Node->getOperand(0))
301 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
302 Node->getOperand(1));
303 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000304 case ISD::DYNAMIC_STACKALLOC:
305 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
306 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
307 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
308 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
309 Tmp3 != Node->getOperand(2))
310 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
311 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000312 else
313 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000314
315 // Since this op produces two values, make sure to remember that we
316 // legalized both of them.
317 AddLegalizedOperand(SDOperand(Node, 0), Result);
318 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
319 return Result.getValue(Op.ResNo);
320
Chris Lattner3d95c142005-01-19 20:24:35 +0000321 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
323 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000324
325 bool Changed = false;
326 std::vector<SDOperand> Ops;
327 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
328 Ops.push_back(LegalizeOp(Node->getOperand(i)));
329 Changed |= Ops.back() != Node->getOperand(i);
330 }
331
332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000333 std::vector<MVT::ValueType> RetTyVTs;
334 RetTyVTs.reserve(Node->getNumValues());
335 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000336 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d95c142005-01-19 20:24:35 +0000337 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000338 } else {
339 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000340 }
Chris Lattner9242c502005-01-09 19:43:23 +0000341 // Since calls produce multiple values, make sure to remember that we
342 // legalized all of them.
343 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
344 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
345 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000346 }
Chris Lattner68a12142005-01-07 22:12:08 +0000347 case ISD::BR:
348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
349 if (Tmp1 != Node->getOperand(0))
350 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
351 break;
352
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000353 case ISD::BRCOND:
354 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000355
356 switch (getTypeAction(Node->getOperand(1).getValueType())) {
357 case Expand: assert(0 && "It's impossible to expand bools");
358 case Legal:
359 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
360 break;
361 case Promote:
362 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
363 break;
364 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000365 // Basic block destination (Op#2) is always legal.
366 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
367 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
368 Node->getOperand(2));
369 break;
370
Chris Lattnerdc750592005-01-07 07:47:09 +0000371 case ISD::LOAD:
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
374 if (Tmp1 != Node->getOperand(0) ||
375 Tmp2 != Node->getOperand(1))
376 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerea4ca942005-01-07 22:28:47 +0000377 else
378 Result = SDOperand(Node, 0);
379
380 // Since loads produce two values, make sure to remember that we legalized
381 // both of them.
382 AddLegalizedOperand(SDOperand(Node, 0), Result);
383 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
384 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000385
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000386 case ISD::EXTLOAD:
387 case ISD::SEXTLOAD:
388 case ISD::ZEXTLOAD:
389 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
390 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
391 if (Tmp1 != Node->getOperand(0) ||
392 Tmp2 != Node->getOperand(1))
393 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
394 cast<MVTSDNode>(Node)->getExtraValueType());
395 else
396 Result = SDOperand(Node, 0);
397
398 // Since loads produce two values, make sure to remember that we legalized
399 // both of them.
400 AddLegalizedOperand(SDOperand(Node, 0), Result);
401 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
402 return Result.getValue(Op.ResNo);
403
Chris Lattnerdc750592005-01-07 07:47:09 +0000404 case ISD::EXTRACT_ELEMENT:
405 // Get both the low and high parts.
406 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
407 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
408 Result = Tmp2; // 1 -> Hi
409 else
410 Result = Tmp1; // 0 -> Lo
411 break;
412
413 case ISD::CopyToReg:
414 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
415
416 switch (getTypeAction(Node->getOperand(1).getValueType())) {
417 case Legal:
418 // Legalize the incoming value (must be legal).
419 Tmp2 = LegalizeOp(Node->getOperand(1));
420 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000421 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000422 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000423 case Promote:
424 Tmp2 = PromoteOp(Node->getOperand(1));
425 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
426 break;
427 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000428 SDOperand Lo, Hi;
429 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000430 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000431 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
432 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
433 // Note that the copytoreg nodes are independent of each other.
434 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000435 assert(isTypeLegal(Result.getValueType()) &&
436 "Cannot expand multiple times yet (i64 -> i16)");
437 break;
438 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000439 break;
440
441 case ISD::RET:
442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
443 switch (Node->getNumOperands()) {
444 case 2: // ret val
445 switch (getTypeAction(Node->getOperand(1).getValueType())) {
446 case Legal:
447 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000448 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000449 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
450 break;
451 case Expand: {
452 SDOperand Lo, Hi;
453 ExpandOp(Node->getOperand(1), Lo, Hi);
454 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
455 break;
456 }
457 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000458 Tmp2 = PromoteOp(Node->getOperand(1));
459 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
460 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000461 }
462 break;
463 case 1: // ret void
464 if (Tmp1 != Node->getOperand(0))
465 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
466 break;
467 default: { // ret <values>
468 std::vector<SDOperand> NewValues;
469 NewValues.push_back(Tmp1);
470 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
471 switch (getTypeAction(Node->getOperand(i).getValueType())) {
472 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000473 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000474 break;
475 case Expand: {
476 SDOperand Lo, Hi;
477 ExpandOp(Node->getOperand(i), Lo, Hi);
478 NewValues.push_back(Lo);
479 NewValues.push_back(Hi);
480 break;
481 }
482 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000483 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000484 }
485 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
486 break;
487 }
488 }
489 break;
490 case ISD::STORE:
491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
492 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
493
Chris Lattnere69daaf2005-01-08 06:25:56 +0000494 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000495 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000496 if (CFP->getValueType(0) == MVT::f32) {
497 union {
498 unsigned I;
499 float F;
500 } V;
501 V.F = CFP->getValue();
502 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
503 DAG.getConstant(V.I, MVT::i32), Tmp2);
504 } else {
505 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
506 union {
507 uint64_t I;
508 double F;
509 } V;
510 V.F = CFP->getValue();
511 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
512 DAG.getConstant(V.I, MVT::i64), Tmp2);
513 }
514 Op = Result;
515 Node = Op.Val;
516 }
517
Chris Lattnerdc750592005-01-07 07:47:09 +0000518 switch (getTypeAction(Node->getOperand(1).getValueType())) {
519 case Legal: {
520 SDOperand Val = LegalizeOp(Node->getOperand(1));
521 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
522 Tmp2 != Node->getOperand(2))
523 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
524 break;
525 }
526 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000527 // Truncate the value and store the result.
528 Tmp3 = PromoteOp(Node->getOperand(1));
529 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
530 Node->getOperand(1).getValueType());
531 break;
532
Chris Lattnerdc750592005-01-07 07:47:09 +0000533 case Expand:
534 SDOperand Lo, Hi;
535 ExpandOp(Node->getOperand(1), Lo, Hi);
536
537 if (!TLI.isLittleEndian())
538 std::swap(Lo, Hi);
539
Chris Lattner0d03eb42005-01-19 18:02:17 +0000540 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000541
Chris Lattner0d03eb42005-01-19 18:02:17 +0000542 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000543 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
544 getIntPtrConstant(IncrementSize));
545 assert(isTypeLegal(Tmp2.getValueType()) &&
546 "Pointers must be legal!");
Chris Lattner0d03eb42005-01-19 18:02:17 +0000547 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
548 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
549 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000550 }
551 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000552 case ISD::TRUNCSTORE:
553 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
554 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
555
556 switch (getTypeAction(Node->getOperand(1).getValueType())) {
557 case Legal:
558 Tmp2 = LegalizeOp(Node->getOperand(1));
559 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
560 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000561 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000562 cast<MVTSDNode>(Node)->getExtraValueType());
563 break;
564 case Promote:
565 case Expand:
566 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
567 }
568 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000569 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000570 switch (getTypeAction(Node->getOperand(0).getValueType())) {
571 case Expand: assert(0 && "It's impossible to expand bools");
572 case Legal:
573 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
574 break;
575 case Promote:
576 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
577 break;
578 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000579 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000580 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000581
582 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
583 default: assert(0 && "This action is not supported yet!");
584 case TargetLowering::Legal:
585 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
586 Tmp3 != Node->getOperand(2))
587 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
588 Tmp1, Tmp2, Tmp3);
589 break;
590 case TargetLowering::Promote: {
591 MVT::ValueType NVT =
592 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
593 unsigned ExtOp, TruncOp;
594 if (MVT::isInteger(Tmp2.getValueType())) {
595 ExtOp = ISD::ZERO_EXTEND;
596 TruncOp = ISD::TRUNCATE;
597 } else {
598 ExtOp = ISD::FP_EXTEND;
599 TruncOp = ISD::FP_ROUND;
600 }
601 // Promote each of the values to the new type.
602 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
603 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
604 // Perform the larger operation, then round down.
605 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
606 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
607 break;
608 }
609 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000610 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000611 case ISD::SETCC:
612 switch (getTypeAction(Node->getOperand(0).getValueType())) {
613 case Legal:
614 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
615 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
616 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
617 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000618 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000619 break;
620 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000621 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
622 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
623
624 // If this is an FP compare, the operands have already been extended.
625 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
626 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000627 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000628
629 // Otherwise, we have to insert explicit sign or zero extends. Note
630 // that we could insert sign extends for ALL conditions, but zero extend
631 // is cheaper on many machines (an AND instead of two shifts), so prefer
632 // it.
633 switch (cast<SetCCSDNode>(Node)->getCondition()) {
634 default: assert(0 && "Unknown integer comparison!");
635 case ISD::SETEQ:
636 case ISD::SETNE:
637 case ISD::SETUGE:
638 case ISD::SETUGT:
639 case ISD::SETULE:
640 case ISD::SETULT:
641 // ALL of these operations will work if we either sign or zero extend
642 // the operands (including the unsigned comparisons!). Zero extend is
643 // usually a simpler/cheaper operation, so prefer it.
644 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
645 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
646 break;
647 case ISD::SETGE:
648 case ISD::SETGT:
649 case ISD::SETLT:
650 case ISD::SETLE:
651 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
652 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
653 break;
654 }
655
656 }
657 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000658 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000659 break;
660 case Expand:
661 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
662 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
663 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
664 switch (cast<SetCCSDNode>(Node)->getCondition()) {
665 case ISD::SETEQ:
666 case ISD::SETNE:
667 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
668 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
669 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000670 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
671 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000672 DAG.getConstant(0, Tmp1.getValueType()));
673 break;
674 default:
675 // FIXME: This generated code sucks.
676 ISD::CondCode LowCC;
677 switch (cast<SetCCSDNode>(Node)->getCondition()) {
678 default: assert(0 && "Unknown integer setcc!");
679 case ISD::SETLT:
680 case ISD::SETULT: LowCC = ISD::SETULT; break;
681 case ISD::SETGT:
682 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
683 case ISD::SETLE:
684 case ISD::SETULE: LowCC = ISD::SETULE; break;
685 case ISD::SETGE:
686 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
687 }
688
689 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
690 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
691 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
692
693 // NOTE: on targets without efficient SELECT of bools, we can always use
694 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000695 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000696 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000697 Node->getValueType(0), LHSHi, RHSHi);
698 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
699 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
700 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000701 break;
702 }
703 }
704 break;
705
Chris Lattner85d70c62005-01-11 05:57:22 +0000706 case ISD::MEMSET:
707 case ISD::MEMCPY:
708 case ISD::MEMMOVE: {
709 Tmp1 = LegalizeOp(Node->getOperand(0));
710 Tmp2 = LegalizeOp(Node->getOperand(1));
711 Tmp3 = LegalizeOp(Node->getOperand(2));
712 SDOperand Tmp4 = LegalizeOp(Node->getOperand(3));
713 SDOperand Tmp5 = LegalizeOp(Node->getOperand(4));
Chris Lattner3c0dd462005-01-16 07:29:19 +0000714
715 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
716 default: assert(0 && "This action not implemented for this operation!");
717 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000718 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
719 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
720 Tmp5 != Node->getOperand(4)) {
721 std::vector<SDOperand> Ops;
722 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
723 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
724 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
725 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000726 break;
727 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000728 // Otherwise, the target does not support this operation. Lower the
729 // operation to an explicit libcall as appropriate.
730 MVT::ValueType IntPtr = TLI.getPointerTy();
731 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
732 std::vector<std::pair<SDOperand, const Type*> > Args;
733
Reid Spencer6dced922005-01-12 14:53:45 +0000734 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000735 if (Node->getOpcode() == ISD::MEMSET) {
736 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
737 // Extend the ubyte argument to be an int value for the call.
738 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
739 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
740 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
741
742 FnName = "memset";
743 } else if (Node->getOpcode() == ISD::MEMCPY ||
744 Node->getOpcode() == ISD::MEMMOVE) {
745 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
746 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
747 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
748 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
749 } else {
750 assert(0 && "Unknown op!");
751 }
752 std::pair<SDOperand,SDOperand> CallResult =
753 TLI.LowerCallTo(Tmp1, Type::VoidTy,
754 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
755 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000756 break;
757 }
758 case TargetLowering::Custom:
759 std::vector<SDOperand> Ops;
760 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
761 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
762 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
763 Result = TLI.LowerOperation(Result);
764 Result = LegalizeOp(Result);
765 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000766 }
767 break;
768 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000769 case ISD::ADD_PARTS:
770 case ISD::SUB_PARTS: {
771 std::vector<SDOperand> Ops;
772 bool Changed = false;
773 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
774 Ops.push_back(LegalizeOp(Node->getOperand(i)));
775 Changed |= Ops.back() != Node->getOperand(i);
776 }
777 if (Changed)
778 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
779 break;
780 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000781 case ISD::ADD:
782 case ISD::SUB:
783 case ISD::MUL:
784 case ISD::UDIV:
785 case ISD::SDIV:
786 case ISD::UREM:
787 case ISD::SREM:
788 case ISD::AND:
789 case ISD::OR:
790 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000791 case ISD::SHL:
792 case ISD::SRL:
793 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000794 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
795 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
796 if (Tmp1 != Node->getOperand(0) ||
797 Tmp2 != Node->getOperand(1))
798 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
799 break;
800 case ISD::ZERO_EXTEND:
801 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000802 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000803 case ISD::FP_EXTEND:
804 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000805 case ISD::FP_TO_SINT:
806 case ISD::FP_TO_UINT:
807 case ISD::SINT_TO_FP:
808 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000809 switch (getTypeAction(Node->getOperand(0).getValueType())) {
810 case Legal:
811 Tmp1 = LegalizeOp(Node->getOperand(0));
812 if (Tmp1 != Node->getOperand(0))
813 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
814 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000815 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +0000816 if (Node->getOpcode() == ISD::SINT_TO_FP ||
817 Node->getOpcode() == ISD::UINT_TO_FP) {
818 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
819 Node->getValueType(0), Node->getOperand(0));
820 Result = LegalizeOp(Result);
821 break;
822 }
Chris Lattnera65a2f02005-01-07 22:37:48 +0000823 // In the expand case, we must be dealing with a truncate, because
824 // otherwise the result would be larger than the source.
825 assert(Node->getOpcode() == ISD::TRUNCATE &&
826 "Shouldn't need to expand other operators here!");
827 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
828
829 // Since the result is legal, we should just be able to truncate the low
830 // part of the source.
831 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
832 break;
833
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000834 case Promote:
835 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000836 case ISD::ZERO_EXTEND:
837 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000838 // NOTE: Any extend would work here...
839 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
840 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000841 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000842 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000843 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000844 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000845 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +0000846 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000847 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
848 Result, Node->getOperand(0).getValueType());
849 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000850 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000851 Result = PromoteOp(Node->getOperand(0));
852 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
853 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000854 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000855 Result = PromoteOp(Node->getOperand(0));
856 if (Result.getValueType() != Op.getValueType())
857 // Dynamically dead while we have only 2 FP types.
858 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
859 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000860 case ISD::FP_ROUND:
861 case ISD::FP_TO_SINT:
862 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000863 Result = PromoteOp(Node->getOperand(0));
864 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
865 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000866 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000867 Result = PromoteOp(Node->getOperand(0));
868 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
869 Result, Node->getOperand(0).getValueType());
870 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
871 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000872 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000873 Result = PromoteOp(Node->getOperand(0));
874 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
875 Result, Node->getOperand(0).getValueType());
876 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
877 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000878 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000879 }
880 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000881 case ISD::FP_ROUND_INREG:
882 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +0000883 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000884 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +0000885 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
886
887 // If this operation is not supported, convert it to a shl/shr or load/store
888 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +0000889 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
890 default: assert(0 && "This action not supported for this op yet!");
891 case TargetLowering::Legal:
892 if (Tmp1 != Node->getOperand(0))
893 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
894 ExtraVT);
895 break;
896 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +0000897 // If this is an integer extend and shifts are supported, do that.
898 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
899 // NOTE: we could fall back on load/store here too for targets without
900 // AND. However, it is doubtful that any exist.
901 // AND out the appropriate bits.
902 SDOperand Mask =
903 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
904 Node->getValueType(0));
905 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
906 Node->getOperand(0), Mask);
907 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
908 // NOTE: we could fall back on load/store here too for targets without
909 // SAR. However, it is doubtful that any exist.
910 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
911 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +0000912 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +0000913 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
914 Node->getOperand(0), ShiftCst);
915 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
916 Result, ShiftCst);
917 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
918 // The only way we can lower this is to turn it into a STORETRUNC,
919 // EXTLOAD pair, targetting a temporary location (a stack slot).
920
921 // NOTE: there is a choice here between constantly creating new stack
922 // slots and always reusing the same one. We currently always create
923 // new ones, as reuse may inhibit scheduling.
924 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
925 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
926 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
927 MachineFunction &MF = DAG.getMachineFunction();
928 int SSFI =
929 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
930 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
931 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
932 Node->getOperand(0), StackSlot, ExtraVT);
933 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
934 Result, StackSlot, ExtraVT);
935 } else {
936 assert(0 && "Unknown op");
937 }
938 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000939 break;
Chris Lattner99222f72005-01-15 07:15:18 +0000940 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000941 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000942 }
Chris Lattner99222f72005-01-15 07:15:18 +0000943 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000944
Chris Lattnerea4ca942005-01-07 22:28:47 +0000945 if (!Op.Val->hasOneUse())
946 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +0000947
948 return Result;
949}
950
Chris Lattner4d978642005-01-15 22:16:26 +0000951/// PromoteOp - Given an operation that produces a value in an invalid type,
952/// promote it to compute the value into a larger type. The produced value will
953/// have the correct bits for the low portion of the register, but no guarantee
954/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000955SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
956 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000957 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000958 assert(getTypeAction(VT) == Promote &&
959 "Caller should expand or legalize operands that are not promotable!");
960 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
961 "Cannot promote to smaller type!");
962
963 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
964 if (I != PromotedNodes.end()) return I->second;
965
966 SDOperand Tmp1, Tmp2, Tmp3;
967
968 SDOperand Result;
969 SDNode *Node = Op.Val;
970
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000971 // Promotion needs an optimization step to clean up after it, and is not
972 // careful to avoid operations the target does not support. Make sure that
973 // all generated operations are legalized in the next iteration.
974 NeedsAnotherIteration = true;
975
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000976 switch (Node->getOpcode()) {
977 default:
978 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
979 assert(0 && "Do not know how to promote this operator!");
980 abort();
981 case ISD::Constant:
982 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
983 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
984 break;
985 case ISD::ConstantFP:
986 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
987 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
988 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000989 case ISD::CopyFromReg:
990 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
991 Node->getOperand(0));
992 // Remember that we legalized the chain.
993 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
994 break;
995
Chris Lattner2cb338d2005-01-18 02:59:52 +0000996 case ISD::SETCC:
997 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
998 "SetCC type is not legal??");
999 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1000 TLI.getSetCCResultTy(), Node->getOperand(0),
1001 Node->getOperand(1));
1002 Result = LegalizeOp(Result);
1003 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001004
1005 case ISD::TRUNCATE:
1006 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1007 case Legal:
1008 Result = LegalizeOp(Node->getOperand(0));
1009 assert(Result.getValueType() >= NVT &&
1010 "This truncation doesn't make sense!");
1011 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1012 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1013 break;
1014 case Expand:
1015 assert(0 && "Cannot handle expand yet");
1016 case Promote:
1017 assert(0 && "Cannot handle promote-promote yet");
1018 }
1019 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001020 case ISD::SIGN_EXTEND:
1021 case ISD::ZERO_EXTEND:
1022 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1023 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1024 case Legal:
1025 // Input is legal? Just do extend all the way to the larger type.
1026 Result = LegalizeOp(Node->getOperand(0));
1027 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1028 break;
1029 case Promote:
1030 // Promote the reg if it's smaller.
1031 Result = PromoteOp(Node->getOperand(0));
1032 // The high bits are not guaranteed to be anything. Insert an extend.
1033 if (Node->getOpcode() == ISD::SIGN_EXTEND)
1034 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, VT);
1035 else
1036 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, VT);
1037 break;
1038 }
1039 break;
1040
1041 case ISD::FP_EXTEND:
1042 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1043 case ISD::FP_ROUND:
1044 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1045 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1046 case Promote: assert(0 && "Unreachable with 2 FP types!");
1047 case Legal:
1048 // Input is legal? Do an FP_ROUND_INREG.
1049 Result = LegalizeOp(Node->getOperand(0));
1050 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1051 break;
1052 }
1053 break;
1054
1055 case ISD::SINT_TO_FP:
1056 case ISD::UINT_TO_FP:
1057 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1058 case Legal:
1059 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001060 // No extra round required here.
1061 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001062 break;
1063
1064 case Promote:
1065 Result = PromoteOp(Node->getOperand(0));
1066 if (Node->getOpcode() == ISD::SINT_TO_FP)
1067 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1068 Result, Node->getOperand(0).getValueType());
1069 else
1070 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1071 Result, Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001072 // No extra round required here.
1073 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001074 break;
1075 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001076 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1077 Node->getOperand(0));
1078 Result = LegalizeOp(Result);
1079
1080 // Round if we cannot tolerate excess precision.
1081 if (NoExcessFPPrecision)
1082 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1083 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001084 }
Chris Lattner4d978642005-01-15 22:16:26 +00001085 break;
1086
1087 case ISD::FP_TO_SINT:
1088 case ISD::FP_TO_UINT:
1089 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1090 case Legal:
1091 Tmp1 = LegalizeOp(Node->getOperand(0));
1092 break;
1093 case Promote:
1094 // The input result is prerounded, so we don't have to do anything
1095 // special.
1096 Tmp1 = PromoteOp(Node->getOperand(0));
1097 break;
1098 case Expand:
1099 assert(0 && "not implemented");
1100 }
1101 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1102 break;
1103
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001104 case ISD::AND:
1105 case ISD::OR:
1106 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001107 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001108 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001109 case ISD::MUL:
1110 // The input may have strange things in the top bits of the registers, but
1111 // these operations don't care. They may have wierd bits going out, but
1112 // that too is okay if they are integer operations.
1113 Tmp1 = PromoteOp(Node->getOperand(0));
1114 Tmp2 = PromoteOp(Node->getOperand(1));
1115 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1116 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1117
1118 // However, if this is a floating point operation, they will give excess
1119 // precision that we may not be able to tolerate. If we DO allow excess
1120 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001121 // FIXME: Why would we need to round FP ops more than integer ones?
1122 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001123 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1124 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1125 break;
1126
Chris Lattner4d978642005-01-15 22:16:26 +00001127 case ISD::SDIV:
1128 case ISD::SREM:
1129 // These operators require that their input be sign extended.
1130 Tmp1 = PromoteOp(Node->getOperand(0));
1131 Tmp2 = PromoteOp(Node->getOperand(1));
1132 if (MVT::isInteger(NVT)) {
1133 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001134 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001135 }
1136 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1137
1138 // Perform FP_ROUND: this is probably overly pessimistic.
1139 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1140 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1141 break;
1142
1143 case ISD::UDIV:
1144 case ISD::UREM:
1145 // These operators require that their input be zero extended.
1146 Tmp1 = PromoteOp(Node->getOperand(0));
1147 Tmp2 = PromoteOp(Node->getOperand(1));
1148 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1149 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001150 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001151 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1152 break;
1153
1154 case ISD::SHL:
1155 Tmp1 = PromoteOp(Node->getOperand(0));
1156 Tmp2 = LegalizeOp(Node->getOperand(1));
1157 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1158 break;
1159 case ISD::SRA:
1160 // The input value must be properly sign extended.
1161 Tmp1 = PromoteOp(Node->getOperand(0));
1162 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1163 Tmp2 = LegalizeOp(Node->getOperand(1));
1164 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1165 break;
1166 case ISD::SRL:
1167 // The input value must be properly zero extended.
1168 Tmp1 = PromoteOp(Node->getOperand(0));
1169 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1170 Tmp2 = LegalizeOp(Node->getOperand(1));
1171 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1172 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001173 case ISD::LOAD:
1174 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1175 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1176 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1177
1178 // Remember that we legalized the chain.
1179 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1180 break;
1181 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001182 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1183 case Expand: assert(0 && "It's impossible to expand bools");
1184 case Legal:
1185 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1186 break;
1187 case Promote:
1188 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1189 break;
1190 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001191 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1192 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1193 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1194 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001195 case ISD::CALL: {
1196 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1197 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1198
Chris Lattner3d95c142005-01-19 20:24:35 +00001199 std::vector<SDOperand> Ops;
1200 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1201 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1202
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001203 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1204 "Can only promote single result calls");
1205 std::vector<MVT::ValueType> RetTyVTs;
1206 RetTyVTs.reserve(2);
1207 RetTyVTs.push_back(NVT);
1208 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001209 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001210 Result = SDOperand(NC, 0);
1211
1212 // Insert the new chain mapping.
1213 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1214 break;
1215 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001216 }
1217
1218 assert(Result.Val && "Didn't set a result!");
1219 AddPromotedOperand(Op, Result);
1220 return Result;
1221}
Chris Lattnerdc750592005-01-07 07:47:09 +00001222
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001223/// ExpandAddSub - Find a clever way to expand this add operation into
1224/// subcomponents.
1225void SelectionDAGLegalize::ExpandAddSub(bool isAdd, SDOperand LHS,SDOperand RHS,
1226 SDOperand &Lo, SDOperand &Hi) {
1227 // Expand the subcomponents.
1228 SDOperand LHSL, LHSH, RHSL, RHSH;
1229 ExpandOp(LHS, LHSL, LHSH);
1230 ExpandOp(RHS, RHSL, RHSH);
1231
1232 // Convert this add to the appropriate ADDC pair. The low part has no carry
1233 // in.
1234 unsigned Opc = isAdd ? ISD::ADD_PARTS : ISD::SUB_PARTS;
1235 std::vector<SDOperand> Ops;
1236 Ops.push_back(LHSL);
1237 Ops.push_back(LHSH);
1238 Ops.push_back(RHSL);
1239 Ops.push_back(RHSH);
1240 Lo = DAG.getNode(Opc, LHSL.getValueType(), Ops);
1241 Hi = Lo.getValue(1);
1242}
1243
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001244/// ExpandShift - Try to find a clever way to expand this shift operation out to
1245/// smaller elements. If we can't find a way that is more efficient than a
1246/// libcall on this target, return false. Otherwise, return true with the
1247/// low-parts expanded into Lo and Hi.
1248bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1249 SDOperand &Lo, SDOperand &Hi) {
1250 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1251 "This is not a shift!");
1252 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
1253
1254 // If we have an efficient select operation (or if the selects will all fold
1255 // away), lower to some complex code, otherwise just emit the libcall.
1256 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1257 !isa<ConstantSDNode>(Amt))
1258 return false;
1259
1260 SDOperand InL, InH;
1261 ExpandOp(Op, InL, InH);
1262 SDOperand ShAmt = LegalizeOp(Amt);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001263 MVT::ValueType ShTy = ShAmt.getValueType();
1264
1265 unsigned NVTBits = MVT::getSizeInBits(NVT);
1266 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1267 DAG.getConstant(NVTBits, ShTy), ShAmt);
1268
Chris Lattner4d25c042005-01-20 20:29:23 +00001269 // Compare the unmasked shift amount against 32.
1270 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1271 DAG.getConstant(NVTBits, ShTy));
1272
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001273 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1274 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1275 DAG.getConstant(NVTBits-1, ShTy));
1276 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1277 DAG.getConstant(NVTBits-1, ShTy));
1278 }
1279
1280 if (Opc == ISD::SHL) {
1281 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1282 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1283 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001284 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001285
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001286 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1287 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1288 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001289 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1290 DAG.getSetCC(ISD::SETEQ,
1291 TLI.getSetCCResultTy(), NAmt,
1292 DAG.getConstant(32, ShTy)),
1293 DAG.getConstant(0, NVT),
1294 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001295 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001296 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001297 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001298 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001299
1300 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001301 if (Opc == ISD::SRA)
1302 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1303 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001304 else
1305 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001306 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001307 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001308 }
1309 return true;
1310}
Chris Lattneraac464e2005-01-21 06:05:23 +00001311
Chris Lattner4add7e32005-01-23 04:42:50 +00001312/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1313/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1314/// Found.
1315static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1316 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1317
1318 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1319 // than the Found node. Just remember this node and return.
1320 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1321 Found = Node;
1322 return;
1323 }
1324
1325 // Otherwise, scan the operands of Node to see if any of them is a call.
1326 assert(Node->getNumOperands() != 0 &&
1327 "All leaves should have depth equal to the entry node!");
1328 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1329 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1330
1331 // Tail recurse for the last iteration.
1332 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1333 Found);
1334}
1335
1336
1337/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1338/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1339/// than Found.
1340static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1341 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1342
1343 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1344 // than the Found node. Just remember this node and return.
1345 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1346 Found = Node;
1347 return;
1348 }
1349
1350 // Otherwise, scan the operands of Node to see if any of them is a call.
1351 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1352 if (UI == E) return;
1353 for (--E; UI != E; ++UI)
1354 FindEarliestAdjCallStackUp(*UI, Found);
1355
1356 // Tail recurse for the last iteration.
1357 FindEarliestAdjCallStackUp(*UI, Found);
1358}
1359
1360/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1361/// find the ADJCALLSTACKUP node that terminates the call sequence.
1362static SDNode *FindAdjCallStackUp(SDNode *Node) {
1363 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1364 return Node;
1365 assert(!Node->use_empty() && "Could not find ADJCALLSTACKUP!");
1366
1367 if (Node->hasOneUse()) // Simple case, only has one user to check.
1368 return FindAdjCallStackUp(*Node->use_begin());
1369
1370 SDOperand TheChain(Node, Node->getNumValues()-1);
1371 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1372
1373 for (SDNode::use_iterator UI = Node->use_begin(),
1374 E = Node->use_end(); ; ++UI) {
1375 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1376
1377 // Make sure to only follow users of our token chain.
1378 SDNode *User = *UI;
1379 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1380 if (User->getOperand(i) == TheChain)
1381 return FindAdjCallStackUp(User);
1382 }
1383 assert(0 && "Unreachable");
1384 abort();
1385}
1386
1387/// FindInputOutputChains - If we are replacing an operation with a call we need
1388/// to find the call that occurs before and the call that occurs after it to
1389/// properly serialize the calls in the block.
1390static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1391 SDOperand Entry) {
1392 SDNode *LatestAdjCallStackDown = Entry.Val;
1393 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1394 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
1395
1396 SDNode *LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1397
1398
1399 SDNode *EarliestAdjCallStackUp = 0;
1400 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1401
1402 if (EarliestAdjCallStackUp) {
1403 //std::cerr << "Found node: ";
1404 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1405 }
1406
1407 return SDOperand(LatestAdjCallStackUp, 0);
1408}
1409
1410
1411
Chris Lattneraac464e2005-01-21 06:05:23 +00001412// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1413// does not fit into a register, return the lo part and set the hi part to the
1414// by-reg argument. If it does fit into a single register, return the result
1415// and leave the Hi part unset.
1416SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1417 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001418 SDNode *OutChain;
1419 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1420 DAG.getEntryNode());
1421 // TODO. Link in chains.
1422
Chris Lattneraac464e2005-01-21 06:05:23 +00001423 TargetLowering::ArgListTy Args;
1424 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1425 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1426 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1427 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1428 }
1429 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1430
1431 // We don't care about token chains for libcalls. We just use the entry
1432 // node as our input and ignore the output chain. This allows us to place
1433 // calls wherever we need them to satisfy data dependences.
1434 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner4add7e32005-01-23 04:42:50 +00001435 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, Callee,
Chris Lattneraac464e2005-01-21 06:05:23 +00001436 Args, DAG).first;
1437 switch (getTypeAction(Result.getValueType())) {
1438 default: assert(0 && "Unknown thing");
1439 case Legal:
1440 return Result;
1441 case Promote:
1442 assert(0 && "Cannot promote this yet!");
1443 case Expand:
1444 SDOperand Lo;
1445 ExpandOp(Result, Lo, Hi);
1446 return Lo;
1447 }
1448}
1449
Chris Lattner4add7e32005-01-23 04:42:50 +00001450
Chris Lattneraac464e2005-01-21 06:05:23 +00001451/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1452/// destination type is legal.
1453SDOperand SelectionDAGLegalize::
1454ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1455 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1456 assert(getTypeAction(Source.getValueType()) == Expand &&
1457 "This is not an expansion!");
1458 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1459
Chris Lattner4add7e32005-01-23 04:42:50 +00001460 SDNode *OutChain;
1461 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1462 DAG.getEntryNode());
1463
Chris Lattner0dfd7d32005-01-23 23:19:44 +00001464 const char *FnName = 0;
Chris Lattneraac464e2005-01-21 06:05:23 +00001465 if (isSigned) {
1466 if (DestTy == MVT::f32)
1467 FnName = "__floatdisf";
1468 else {
1469 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1470 FnName = "__floatdidf";
1471 }
1472 } else {
1473 // If this is unsigned, and not supported, first perform the conversion to
1474 // signed, then adjust the result if the sign bit is set.
1475 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1476
1477 assert(0 && "Unsigned casts not supported yet!");
1478 }
1479 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1480
1481 TargetLowering::ArgListTy Args;
1482 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1483 Args.push_back(std::make_pair(Source, ArgTy));
1484
1485 // We don't care about token chains for libcalls. We just use the entry
1486 // node as our input and ignore the output chain. This allows us to place
1487 // calls wherever we need them to satisfy data dependences.
1488 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner4add7e32005-01-23 04:42:50 +00001489 return TLI.LowerCallTo(InChain, RetTy, Callee, Args, DAG).first;
1490
Chris Lattneraac464e2005-01-21 06:05:23 +00001491}
1492
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001493
1494
Chris Lattnerdc750592005-01-07 07:47:09 +00001495/// ExpandOp - Expand the specified SDOperand into its two component pieces
1496/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1497/// LegalizeNodes map is filled in for any results that are not expanded, the
1498/// ExpandedNodes map is filled in for any results that are expanded, and the
1499/// Lo/Hi values are returned.
1500void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1501 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001502 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001503 SDNode *Node = Op.Val;
1504 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1505 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1506 assert(MVT::isInteger(NVT) && NVT < VT &&
1507 "Cannot expand to FP value or to larger int value!");
1508
1509 // If there is more than one use of this, see if we already expanded it.
1510 // There is no use remembering values that only have a single use, as the map
1511 // entries will never be reused.
1512 if (!Node->hasOneUse()) {
1513 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1514 = ExpandedNodes.find(Op);
1515 if (I != ExpandedNodes.end()) {
1516 Lo = I->second.first;
1517 Hi = I->second.second;
1518 return;
1519 }
1520 }
1521
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001522 // Expanding to multiple registers needs to perform an optimization step, and
1523 // is not careful to avoid operations the target does not support. Make sure
1524 // that all generated operations are legalized in the next iteration.
1525 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001526
Chris Lattnerdc750592005-01-07 07:47:09 +00001527 switch (Node->getOpcode()) {
1528 default:
1529 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1530 assert(0 && "Do not know how to expand this operator!");
1531 abort();
1532 case ISD::Constant: {
1533 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1534 Lo = DAG.getConstant(Cst, NVT);
1535 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1536 break;
1537 }
1538
1539 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001540 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001541 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001542 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1543 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1544
1545 // Remember that we legalized the chain.
1546 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1547
Chris Lattnerdc750592005-01-07 07:47:09 +00001548 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1549 break;
1550 }
1551
1552 case ISD::LOAD: {
1553 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1554 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1555 Lo = DAG.getLoad(NVT, Ch, Ptr);
1556
1557 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001558 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001559 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1560 getIntPtrConstant(IncrementSize));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001561 Hi = DAG.getLoad(NVT, Ch, Ptr);
1562
1563 // Build a factor node to remember that this load is independent of the
1564 // other one.
1565 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1566 Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001567
1568 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00001569 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00001570 if (!TLI.isLittleEndian())
1571 std::swap(Lo, Hi);
1572 break;
1573 }
1574 case ISD::CALL: {
1575 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1576 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1577
Chris Lattner3d95c142005-01-19 20:24:35 +00001578 bool Changed = false;
1579 std::vector<SDOperand> Ops;
1580 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1581 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1582 Changed |= Ops.back() != Node->getOperand(i);
1583 }
1584
Chris Lattnerdc750592005-01-07 07:47:09 +00001585 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1586 "Can only expand a call once so far, not i64 -> i16!");
1587
1588 std::vector<MVT::ValueType> RetTyVTs;
1589 RetTyVTs.reserve(3);
1590 RetTyVTs.push_back(NVT);
1591 RetTyVTs.push_back(NVT);
1592 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001593 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00001594 Lo = SDOperand(NC, 0);
1595 Hi = SDOperand(NC, 1);
1596
1597 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001598 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001599 break;
1600 }
1601 case ISD::AND:
1602 case ISD::OR:
1603 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1604 SDOperand LL, LH, RL, RH;
1605 ExpandOp(Node->getOperand(0), LL, LH);
1606 ExpandOp(Node->getOperand(1), RL, RH);
1607 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1608 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1609 break;
1610 }
1611 case ISD::SELECT: {
1612 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001613
1614 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1615 case Expand: assert(0 && "It's impossible to expand bools");
1616 case Legal:
1617 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1618 break;
1619 case Promote:
1620 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1621 break;
1622 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001623 ExpandOp(Node->getOperand(1), LL, LH);
1624 ExpandOp(Node->getOperand(2), RL, RH);
1625 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1626 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1627 break;
1628 }
1629 case ISD::SIGN_EXTEND: {
1630 // The low part is just a sign extension of the input (which degenerates to
1631 // a copy).
1632 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1633
1634 // The high part is obtained by SRA'ing all but one of the bits of the lo
1635 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001636 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00001637 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
1638 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00001639 break;
1640 }
1641 case ISD::ZERO_EXTEND:
1642 // The low part is just a zero extension of the input (which degenerates to
1643 // a copy).
1644 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1645
1646 // The high part is just a zero.
1647 Hi = DAG.getConstant(0, NVT);
1648 break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001649
1650 // These operators cannot be expanded directly, emit them as calls to
1651 // library functions.
1652 case ISD::FP_TO_SINT:
1653 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001654 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001655 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001656 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001657 break;
1658 case ISD::FP_TO_UINT:
1659 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001660 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001661 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001662 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001663 break;
1664
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001665 case ISD::SHL:
1666 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001667 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001668 break;
1669 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001670 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001671 break;
1672
1673 case ISD::SRA:
1674 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001675 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001676 break;
1677 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001678 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001679 break;
1680 case ISD::SRL:
1681 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001682 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001683 break;
1684 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001685 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001686 break;
1687
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001688 case ISD::ADD:
1689 ExpandAddSub(true, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1690 break;
1691 case ISD::SUB:
1692 ExpandAddSub(false, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1693 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001694 case ISD::MUL: Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
1695 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
1696 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
1697 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
1698 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001699 }
1700
1701 // Remember in a map if the values will be reused later.
1702 if (!Node->hasOneUse()) {
1703 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1704 std::make_pair(Lo, Hi))).second;
1705 assert(isNew && "Value already expanded?!?");
1706 }
1707}
1708
1709
1710// SelectionDAG::Legalize - This is the entry point for the file.
1711//
Chris Lattner4add7e32005-01-23 04:42:50 +00001712void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00001713 /// run - This is the main entry point to this class.
1714 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00001715 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00001716}
1717