blob: 1e976e3f68035b7b787ed038b7e4577919590617 [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 }
518 Op = Result;
519 Node = Op.Val;
520 }
521
Chris Lattnerdc750592005-01-07 07:47:09 +0000522 switch (getTypeAction(Node->getOperand(1).getValueType())) {
523 case Legal: {
524 SDOperand Val = LegalizeOp(Node->getOperand(1));
525 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
526 Tmp2 != Node->getOperand(2))
527 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
528 break;
529 }
530 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000531 // Truncate the value and store the result.
532 Tmp3 = PromoteOp(Node->getOperand(1));
533 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
534 Node->getOperand(1).getValueType());
535 break;
536
Chris Lattnerdc750592005-01-07 07:47:09 +0000537 case Expand:
538 SDOperand Lo, Hi;
539 ExpandOp(Node->getOperand(1), Lo, Hi);
540
541 if (!TLI.isLittleEndian())
542 std::swap(Lo, Hi);
543
Chris Lattner0d03eb42005-01-19 18:02:17 +0000544 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000545
Chris Lattner0d03eb42005-01-19 18:02:17 +0000546 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000547 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
548 getIntPtrConstant(IncrementSize));
549 assert(isTypeLegal(Tmp2.getValueType()) &&
550 "Pointers must be legal!");
Chris Lattner0d03eb42005-01-19 18:02:17 +0000551 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
552 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
553 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000554 }
555 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000556 case ISD::TRUNCSTORE:
557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
558 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
559
560 switch (getTypeAction(Node->getOperand(1).getValueType())) {
561 case Legal:
562 Tmp2 = LegalizeOp(Node->getOperand(1));
563 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
564 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000565 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000566 cast<MVTSDNode>(Node)->getExtraValueType());
567 break;
568 case Promote:
569 case Expand:
570 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
571 }
572 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000573 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000574 switch (getTypeAction(Node->getOperand(0).getValueType())) {
575 case Expand: assert(0 && "It's impossible to expand bools");
576 case Legal:
577 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
578 break;
579 case Promote:
580 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
581 break;
582 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000583 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000584 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000585
586 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
587 default: assert(0 && "This action is not supported yet!");
588 case TargetLowering::Legal:
589 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
590 Tmp3 != Node->getOperand(2))
591 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
592 Tmp1, Tmp2, Tmp3);
593 break;
594 case TargetLowering::Promote: {
595 MVT::ValueType NVT =
596 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
597 unsigned ExtOp, TruncOp;
598 if (MVT::isInteger(Tmp2.getValueType())) {
599 ExtOp = ISD::ZERO_EXTEND;
600 TruncOp = ISD::TRUNCATE;
601 } else {
602 ExtOp = ISD::FP_EXTEND;
603 TruncOp = ISD::FP_ROUND;
604 }
605 // Promote each of the values to the new type.
606 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
607 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
608 // Perform the larger operation, then round down.
609 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
610 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
611 break;
612 }
613 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000614 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000615 case ISD::SETCC:
616 switch (getTypeAction(Node->getOperand(0).getValueType())) {
617 case Legal:
618 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
619 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
620 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
621 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000622 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000623 break;
624 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000625 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
626 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
627
628 // If this is an FP compare, the operands have already been extended.
629 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
630 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000631 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000632
633 // Otherwise, we have to insert explicit sign or zero extends. Note
634 // that we could insert sign extends for ALL conditions, but zero extend
635 // is cheaper on many machines (an AND instead of two shifts), so prefer
636 // it.
637 switch (cast<SetCCSDNode>(Node)->getCondition()) {
638 default: assert(0 && "Unknown integer comparison!");
639 case ISD::SETEQ:
640 case ISD::SETNE:
641 case ISD::SETUGE:
642 case ISD::SETUGT:
643 case ISD::SETULE:
644 case ISD::SETULT:
645 // ALL of these operations will work if we either sign or zero extend
646 // the operands (including the unsigned comparisons!). Zero extend is
647 // usually a simpler/cheaper operation, so prefer it.
648 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
649 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
650 break;
651 case ISD::SETGE:
652 case ISD::SETGT:
653 case ISD::SETLT:
654 case ISD::SETLE:
655 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
656 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
657 break;
658 }
659
660 }
661 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000662 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000663 break;
664 case Expand:
665 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
666 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
667 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
668 switch (cast<SetCCSDNode>(Node)->getCondition()) {
669 case ISD::SETEQ:
670 case ISD::SETNE:
671 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
672 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
673 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000674 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
675 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000676 DAG.getConstant(0, Tmp1.getValueType()));
677 break;
678 default:
679 // FIXME: This generated code sucks.
680 ISD::CondCode LowCC;
681 switch (cast<SetCCSDNode>(Node)->getCondition()) {
682 default: assert(0 && "Unknown integer setcc!");
683 case ISD::SETLT:
684 case ISD::SETULT: LowCC = ISD::SETULT; break;
685 case ISD::SETGT:
686 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
687 case ISD::SETLE:
688 case ISD::SETULE: LowCC = ISD::SETULE; break;
689 case ISD::SETGE:
690 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
691 }
692
693 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
694 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
695 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
696
697 // NOTE: on targets without efficient SELECT of bools, we can always use
698 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000699 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000700 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000701 Node->getValueType(0), LHSHi, RHSHi);
702 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
703 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
704 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000705 break;
706 }
707 }
708 break;
709
Chris Lattner85d70c62005-01-11 05:57:22 +0000710 case ISD::MEMSET:
711 case ISD::MEMCPY:
712 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000713 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000714 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
715
716 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
717 switch (getTypeAction(Node->getOperand(2).getValueType())) {
718 case Expand: assert(0 && "Cannot expand a byte!");
719 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000720 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000721 break;
722 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000723 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000724 break;
725 }
726 } else {
727 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
728 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000729
730 SDOperand Tmp4;
731 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000732 case Expand: assert(0 && "Cannot expand this yet!");
733 case Legal:
734 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000735 break;
736 case Promote:
737 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000738 break;
739 }
740
741 SDOperand Tmp5;
742 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
743 case Expand: assert(0 && "Cannot expand this yet!");
744 case Legal:
745 Tmp5 = LegalizeOp(Node->getOperand(4));
746 break;
747 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000748 Tmp5 = PromoteOp(Node->getOperand(4));
749 break;
750 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000751
752 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
753 default: assert(0 && "This action not implemented for this operation!");
754 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000755 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
756 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
757 Tmp5 != Node->getOperand(4)) {
758 std::vector<SDOperand> Ops;
759 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
760 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
761 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
762 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000763 break;
764 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000765 // Otherwise, the target does not support this operation. Lower the
766 // operation to an explicit libcall as appropriate.
767 MVT::ValueType IntPtr = TLI.getPointerTy();
768 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
769 std::vector<std::pair<SDOperand, const Type*> > Args;
770
Reid Spencer6dced922005-01-12 14:53:45 +0000771 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000772 if (Node->getOpcode() == ISD::MEMSET) {
773 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
774 // Extend the ubyte argument to be an int value for the call.
775 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
776 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
777 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
778
779 FnName = "memset";
780 } else if (Node->getOpcode() == ISD::MEMCPY ||
781 Node->getOpcode() == ISD::MEMMOVE) {
782 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
783 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
784 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
785 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
786 } else {
787 assert(0 && "Unknown op!");
788 }
789 std::pair<SDOperand,SDOperand> CallResult =
790 TLI.LowerCallTo(Tmp1, Type::VoidTy,
791 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
792 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000793 break;
794 }
795 case TargetLowering::Custom:
796 std::vector<SDOperand> Ops;
797 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
798 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
799 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
800 Result = TLI.LowerOperation(Result);
801 Result = LegalizeOp(Result);
802 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000803 }
804 break;
805 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000806 case ISD::ADD_PARTS:
807 case ISD::SUB_PARTS: {
808 std::vector<SDOperand> Ops;
809 bool Changed = false;
810 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
811 Ops.push_back(LegalizeOp(Node->getOperand(i)));
812 Changed |= Ops.back() != Node->getOperand(i);
813 }
814 if (Changed)
815 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
816 break;
817 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000818 case ISD::ADD:
819 case ISD::SUB:
820 case ISD::MUL:
821 case ISD::UDIV:
822 case ISD::SDIV:
823 case ISD::UREM:
824 case ISD::SREM:
825 case ISD::AND:
826 case ISD::OR:
827 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000828 case ISD::SHL:
829 case ISD::SRL:
830 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000831 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
832 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
833 if (Tmp1 != Node->getOperand(0) ||
834 Tmp2 != Node->getOperand(1))
835 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
836 break;
837 case ISD::ZERO_EXTEND:
838 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000839 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000840 case ISD::FP_EXTEND:
841 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000842 case ISD::FP_TO_SINT:
843 case ISD::FP_TO_UINT:
844 case ISD::SINT_TO_FP:
845 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000846 switch (getTypeAction(Node->getOperand(0).getValueType())) {
847 case Legal:
848 Tmp1 = LegalizeOp(Node->getOperand(0));
849 if (Tmp1 != Node->getOperand(0))
850 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
851 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000852 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +0000853 if (Node->getOpcode() == ISD::SINT_TO_FP ||
854 Node->getOpcode() == ISD::UINT_TO_FP) {
855 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
856 Node->getValueType(0), Node->getOperand(0));
857 Result = LegalizeOp(Result);
858 break;
859 }
Chris Lattnera65a2f02005-01-07 22:37:48 +0000860 // In the expand case, we must be dealing with a truncate, because
861 // otherwise the result would be larger than the source.
862 assert(Node->getOpcode() == ISD::TRUNCATE &&
863 "Shouldn't need to expand other operators here!");
864 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
865
866 // Since the result is legal, we should just be able to truncate the low
867 // part of the source.
868 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
869 break;
870
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000871 case Promote:
872 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000873 case ISD::ZERO_EXTEND:
874 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000875 // NOTE: Any extend would work here...
876 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
877 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000878 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000879 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000880 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000881 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000882 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +0000883 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000884 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
885 Result, Node->getOperand(0).getValueType());
886 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000887 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000888 Result = PromoteOp(Node->getOperand(0));
889 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
890 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000891 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000892 Result = PromoteOp(Node->getOperand(0));
893 if (Result.getValueType() != Op.getValueType())
894 // Dynamically dead while we have only 2 FP types.
895 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
896 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000897 case ISD::FP_ROUND:
898 case ISD::FP_TO_SINT:
899 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000900 Result = PromoteOp(Node->getOperand(0));
901 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
902 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000903 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000904 Result = PromoteOp(Node->getOperand(0));
905 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
906 Result, Node->getOperand(0).getValueType());
907 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
908 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000909 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000910 Result = PromoteOp(Node->getOperand(0));
911 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
912 Result, Node->getOperand(0).getValueType());
913 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
914 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000915 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000916 }
917 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000918 case ISD::FP_ROUND_INREG:
919 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +0000920 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000921 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +0000922 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
923
924 // If this operation is not supported, convert it to a shl/shr or load/store
925 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +0000926 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
927 default: assert(0 && "This action not supported for this op yet!");
928 case TargetLowering::Legal:
929 if (Tmp1 != Node->getOperand(0))
930 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
931 ExtraVT);
932 break;
933 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +0000934 // If this is an integer extend and shifts are supported, do that.
935 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
936 // NOTE: we could fall back on load/store here too for targets without
937 // AND. However, it is doubtful that any exist.
938 // AND out the appropriate bits.
939 SDOperand Mask =
940 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
941 Node->getValueType(0));
942 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
943 Node->getOperand(0), Mask);
944 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
945 // NOTE: we could fall back on load/store here too for targets without
946 // SAR. However, it is doubtful that any exist.
947 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
948 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +0000949 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +0000950 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
951 Node->getOperand(0), ShiftCst);
952 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
953 Result, ShiftCst);
954 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
955 // The only way we can lower this is to turn it into a STORETRUNC,
956 // EXTLOAD pair, targetting a temporary location (a stack slot).
957
958 // NOTE: there is a choice here between constantly creating new stack
959 // slots and always reusing the same one. We currently always create
960 // new ones, as reuse may inhibit scheduling.
961 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
962 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
963 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
964 MachineFunction &MF = DAG.getMachineFunction();
965 int SSFI =
966 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
967 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
968 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
969 Node->getOperand(0), StackSlot, ExtraVT);
970 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
971 Result, StackSlot, ExtraVT);
972 } else {
973 assert(0 && "Unknown op");
974 }
975 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000976 break;
Chris Lattner99222f72005-01-15 07:15:18 +0000977 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000978 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000979 }
Chris Lattner99222f72005-01-15 07:15:18 +0000980 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000981
Chris Lattnerea4ca942005-01-07 22:28:47 +0000982 if (!Op.Val->hasOneUse())
983 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +0000984
985 return Result;
986}
987
Chris Lattner4d978642005-01-15 22:16:26 +0000988/// PromoteOp - Given an operation that produces a value in an invalid type,
989/// promote it to compute the value into a larger type. The produced value will
990/// have the correct bits for the low portion of the register, but no guarantee
991/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000992SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
993 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000994 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000995 assert(getTypeAction(VT) == Promote &&
996 "Caller should expand or legalize operands that are not promotable!");
997 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
998 "Cannot promote to smaller type!");
999
1000 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1001 if (I != PromotedNodes.end()) return I->second;
1002
1003 SDOperand Tmp1, Tmp2, Tmp3;
1004
1005 SDOperand Result;
1006 SDNode *Node = Op.Val;
1007
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001008 // Promotion needs an optimization step to clean up after it, and is not
1009 // careful to avoid operations the target does not support. Make sure that
1010 // all generated operations are legalized in the next iteration.
1011 NeedsAnotherIteration = true;
1012
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001013 switch (Node->getOpcode()) {
1014 default:
1015 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1016 assert(0 && "Do not know how to promote this operator!");
1017 abort();
1018 case ISD::Constant:
1019 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1020 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1021 break;
1022 case ISD::ConstantFP:
1023 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1024 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1025 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001026 case ISD::CopyFromReg:
1027 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1028 Node->getOperand(0));
1029 // Remember that we legalized the chain.
1030 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1031 break;
1032
Chris Lattner2cb338d2005-01-18 02:59:52 +00001033 case ISD::SETCC:
1034 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1035 "SetCC type is not legal??");
1036 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1037 TLI.getSetCCResultTy(), Node->getOperand(0),
1038 Node->getOperand(1));
1039 Result = LegalizeOp(Result);
1040 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001041
1042 case ISD::TRUNCATE:
1043 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1044 case Legal:
1045 Result = LegalizeOp(Node->getOperand(0));
1046 assert(Result.getValueType() >= NVT &&
1047 "This truncation doesn't make sense!");
1048 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1049 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1050 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001051 case Promote:
1052 // The truncation is not required, because we don't guarantee anything
1053 // about high bits anyway.
1054 Result = PromoteOp(Node->getOperand(0));
1055 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001056 case Expand:
1057 assert(0 && "Cannot handle expand yet");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001058 }
1059 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001060 case ISD::SIGN_EXTEND:
1061 case ISD::ZERO_EXTEND:
1062 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1063 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1064 case Legal:
1065 // Input is legal? Just do extend all the way to the larger type.
1066 Result = LegalizeOp(Node->getOperand(0));
1067 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1068 break;
1069 case Promote:
1070 // Promote the reg if it's smaller.
1071 Result = PromoteOp(Node->getOperand(0));
1072 // The high bits are not guaranteed to be anything. Insert an extend.
1073 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001074 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1075 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001076 else
Chris Lattner05596912005-02-04 18:39:19 +00001077 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result,
1078 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001079 break;
1080 }
1081 break;
1082
1083 case ISD::FP_EXTEND:
1084 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1085 case ISD::FP_ROUND:
1086 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1087 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1088 case Promote: assert(0 && "Unreachable with 2 FP types!");
1089 case Legal:
1090 // Input is legal? Do an FP_ROUND_INREG.
1091 Result = LegalizeOp(Node->getOperand(0));
1092 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1093 break;
1094 }
1095 break;
1096
1097 case ISD::SINT_TO_FP:
1098 case ISD::UINT_TO_FP:
1099 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1100 case Legal:
1101 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001102 // No extra round required here.
1103 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001104 break;
1105
1106 case Promote:
1107 Result = PromoteOp(Node->getOperand(0));
1108 if (Node->getOpcode() == ISD::SINT_TO_FP)
1109 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1110 Result, Node->getOperand(0).getValueType());
1111 else
1112 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1113 Result, Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001114 // No extra round required here.
1115 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001116 break;
1117 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001118 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1119 Node->getOperand(0));
1120 Result = LegalizeOp(Result);
1121
1122 // Round if we cannot tolerate excess precision.
1123 if (NoExcessFPPrecision)
1124 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1125 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001126 }
Chris Lattner4d978642005-01-15 22:16:26 +00001127 break;
1128
1129 case ISD::FP_TO_SINT:
1130 case ISD::FP_TO_UINT:
1131 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1132 case Legal:
1133 Tmp1 = LegalizeOp(Node->getOperand(0));
1134 break;
1135 case Promote:
1136 // The input result is prerounded, so we don't have to do anything
1137 // special.
1138 Tmp1 = PromoteOp(Node->getOperand(0));
1139 break;
1140 case Expand:
1141 assert(0 && "not implemented");
1142 }
1143 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1144 break;
1145
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001146 case ISD::AND:
1147 case ISD::OR:
1148 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001149 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001150 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001151 case ISD::MUL:
1152 // The input may have strange things in the top bits of the registers, but
1153 // these operations don't care. They may have wierd bits going out, but
1154 // that too is okay if they are integer operations.
1155 Tmp1 = PromoteOp(Node->getOperand(0));
1156 Tmp2 = PromoteOp(Node->getOperand(1));
1157 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1158 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1159
1160 // However, if this is a floating point operation, they will give excess
1161 // precision that we may not be able to tolerate. If we DO allow excess
1162 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001163 // FIXME: Why would we need to round FP ops more than integer ones?
1164 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001165 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1166 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1167 break;
1168
Chris Lattner4d978642005-01-15 22:16:26 +00001169 case ISD::SDIV:
1170 case ISD::SREM:
1171 // These operators require that their input be sign extended.
1172 Tmp1 = PromoteOp(Node->getOperand(0));
1173 Tmp2 = PromoteOp(Node->getOperand(1));
1174 if (MVT::isInteger(NVT)) {
1175 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001176 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001177 }
1178 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1179
1180 // Perform FP_ROUND: this is probably overly pessimistic.
1181 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1182 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1183 break;
1184
1185 case ISD::UDIV:
1186 case ISD::UREM:
1187 // These operators require that their input be zero extended.
1188 Tmp1 = PromoteOp(Node->getOperand(0));
1189 Tmp2 = PromoteOp(Node->getOperand(1));
1190 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1191 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001192 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001193 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1194 break;
1195
1196 case ISD::SHL:
1197 Tmp1 = PromoteOp(Node->getOperand(0));
1198 Tmp2 = LegalizeOp(Node->getOperand(1));
1199 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1200 break;
1201 case ISD::SRA:
1202 // The input value must be properly sign extended.
1203 Tmp1 = PromoteOp(Node->getOperand(0));
1204 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1205 Tmp2 = LegalizeOp(Node->getOperand(1));
1206 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1207 break;
1208 case ISD::SRL:
1209 // The input value must be properly zero extended.
1210 Tmp1 = PromoteOp(Node->getOperand(0));
1211 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1212 Tmp2 = LegalizeOp(Node->getOperand(1));
1213 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1214 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001215 case ISD::LOAD:
1216 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1217 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1218 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1219
1220 // Remember that we legalized the chain.
1221 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1222 break;
1223 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001224 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1225 case Expand: assert(0 && "It's impossible to expand bools");
1226 case Legal:
1227 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1228 break;
1229 case Promote:
1230 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1231 break;
1232 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001233 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1234 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1235 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1236 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001237 case ISD::CALL: {
1238 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1239 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1240
Chris Lattner3d95c142005-01-19 20:24:35 +00001241 std::vector<SDOperand> Ops;
1242 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1243 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1244
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001245 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1246 "Can only promote single result calls");
1247 std::vector<MVT::ValueType> RetTyVTs;
1248 RetTyVTs.reserve(2);
1249 RetTyVTs.push_back(NVT);
1250 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001251 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001252 Result = SDOperand(NC, 0);
1253
1254 // Insert the new chain mapping.
1255 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1256 break;
1257 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001258 }
1259
1260 assert(Result.Val && "Didn't set a result!");
1261 AddPromotedOperand(Op, Result);
1262 return Result;
1263}
Chris Lattnerdc750592005-01-07 07:47:09 +00001264
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001265/// ExpandAddSub - Find a clever way to expand this add operation into
1266/// subcomponents.
1267void SelectionDAGLegalize::ExpandAddSub(bool isAdd, SDOperand LHS,SDOperand RHS,
1268 SDOperand &Lo, SDOperand &Hi) {
1269 // Expand the subcomponents.
1270 SDOperand LHSL, LHSH, RHSL, RHSH;
1271 ExpandOp(LHS, LHSL, LHSH);
1272 ExpandOp(RHS, RHSL, RHSH);
1273
1274 // Convert this add to the appropriate ADDC pair. The low part has no carry
1275 // in.
1276 unsigned Opc = isAdd ? ISD::ADD_PARTS : ISD::SUB_PARTS;
1277 std::vector<SDOperand> Ops;
1278 Ops.push_back(LHSL);
1279 Ops.push_back(LHSH);
1280 Ops.push_back(RHSL);
1281 Ops.push_back(RHSH);
1282 Lo = DAG.getNode(Opc, LHSL.getValueType(), Ops);
1283 Hi = Lo.getValue(1);
1284}
1285
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001286/// ExpandShift - Try to find a clever way to expand this shift operation out to
1287/// smaller elements. If we can't find a way that is more efficient than a
1288/// libcall on this target, return false. Otherwise, return true with the
1289/// low-parts expanded into Lo and Hi.
1290bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1291 SDOperand &Lo, SDOperand &Hi) {
1292 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1293 "This is not a shift!");
1294 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
1295
1296 // If we have an efficient select operation (or if the selects will all fold
1297 // away), lower to some complex code, otherwise just emit the libcall.
1298 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1299 !isa<ConstantSDNode>(Amt))
1300 return false;
1301
1302 SDOperand InL, InH;
1303 ExpandOp(Op, InL, InH);
1304 SDOperand ShAmt = LegalizeOp(Amt);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001305 MVT::ValueType ShTy = ShAmt.getValueType();
1306
1307 unsigned NVTBits = MVT::getSizeInBits(NVT);
1308 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1309 DAG.getConstant(NVTBits, ShTy), ShAmt);
1310
Chris Lattner4d25c042005-01-20 20:29:23 +00001311 // Compare the unmasked shift amount against 32.
1312 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1313 DAG.getConstant(NVTBits, ShTy));
1314
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001315 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1316 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1317 DAG.getConstant(NVTBits-1, ShTy));
1318 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1319 DAG.getConstant(NVTBits-1, ShTy));
1320 }
1321
1322 if (Opc == ISD::SHL) {
1323 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1324 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1325 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001326 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001327
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001328 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1329 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1330 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001331 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1332 DAG.getSetCC(ISD::SETEQ,
1333 TLI.getSetCCResultTy(), NAmt,
1334 DAG.getConstant(32, ShTy)),
1335 DAG.getConstant(0, NVT),
1336 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001337 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001338 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001339 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001340 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001341
1342 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001343 if (Opc == ISD::SRA)
1344 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1345 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001346 else
1347 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001348 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001349 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001350 }
1351 return true;
1352}
Chris Lattneraac464e2005-01-21 06:05:23 +00001353
Chris Lattner4add7e32005-01-23 04:42:50 +00001354/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1355/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1356/// Found.
1357static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1358 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1359
1360 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1361 // than the Found node. Just remember this node and return.
1362 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1363 Found = Node;
1364 return;
1365 }
1366
1367 // Otherwise, scan the operands of Node to see if any of them is a call.
1368 assert(Node->getNumOperands() != 0 &&
1369 "All leaves should have depth equal to the entry node!");
1370 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1371 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1372
1373 // Tail recurse for the last iteration.
1374 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1375 Found);
1376}
1377
1378
1379/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1380/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1381/// than Found.
1382static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1383 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1384
1385 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1386 // than the Found node. Just remember this node and return.
1387 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1388 Found = Node;
1389 return;
1390 }
1391
1392 // Otherwise, scan the operands of Node to see if any of them is a call.
1393 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1394 if (UI == E) return;
1395 for (--E; UI != E; ++UI)
1396 FindEarliestAdjCallStackUp(*UI, Found);
1397
1398 // Tail recurse for the last iteration.
1399 FindEarliestAdjCallStackUp(*UI, Found);
1400}
1401
1402/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1403/// find the ADJCALLSTACKUP node that terminates the call sequence.
1404static SDNode *FindAdjCallStackUp(SDNode *Node) {
1405 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1406 return Node;
1407 assert(!Node->use_empty() && "Could not find ADJCALLSTACKUP!");
1408
1409 if (Node->hasOneUse()) // Simple case, only has one user to check.
1410 return FindAdjCallStackUp(*Node->use_begin());
1411
1412 SDOperand TheChain(Node, Node->getNumValues()-1);
1413 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1414
1415 for (SDNode::use_iterator UI = Node->use_begin(),
1416 E = Node->use_end(); ; ++UI) {
1417 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1418
1419 // Make sure to only follow users of our token chain.
1420 SDNode *User = *UI;
1421 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1422 if (User->getOperand(i) == TheChain)
1423 return FindAdjCallStackUp(User);
1424 }
1425 assert(0 && "Unreachable");
1426 abort();
1427}
1428
1429/// FindInputOutputChains - If we are replacing an operation with a call we need
1430/// to find the call that occurs before and the call that occurs after it to
1431/// properly serialize the calls in the block.
1432static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1433 SDOperand Entry) {
1434 SDNode *LatestAdjCallStackDown = Entry.Val;
1435 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1436 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
1437
1438 SDNode *LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1439
1440
1441 SDNode *EarliestAdjCallStackUp = 0;
1442 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1443
1444 if (EarliestAdjCallStackUp) {
1445 //std::cerr << "Found node: ";
1446 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1447 }
1448
1449 return SDOperand(LatestAdjCallStackUp, 0);
1450}
1451
1452
1453
Chris Lattneraac464e2005-01-21 06:05:23 +00001454// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1455// does not fit into a register, return the lo part and set the hi part to the
1456// by-reg argument. If it does fit into a single register, return the result
1457// and leave the Hi part unset.
1458SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1459 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001460 SDNode *OutChain;
1461 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1462 DAG.getEntryNode());
1463 // TODO. Link in chains.
1464
Chris Lattneraac464e2005-01-21 06:05:23 +00001465 TargetLowering::ArgListTy Args;
1466 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1467 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1468 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1469 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1470 }
1471 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1472
1473 // We don't care about token chains for libcalls. We just use the entry
1474 // node as our input and ignore the output chain. This allows us to place
1475 // calls wherever we need them to satisfy data dependences.
1476 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner4add7e32005-01-23 04:42:50 +00001477 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, Callee,
Chris Lattneraac464e2005-01-21 06:05:23 +00001478 Args, DAG).first;
1479 switch (getTypeAction(Result.getValueType())) {
1480 default: assert(0 && "Unknown thing");
1481 case Legal:
1482 return Result;
1483 case Promote:
1484 assert(0 && "Cannot promote this yet!");
1485 case Expand:
1486 SDOperand Lo;
1487 ExpandOp(Result, Lo, Hi);
1488 return Lo;
1489 }
1490}
1491
Chris Lattner4add7e32005-01-23 04:42:50 +00001492
Chris Lattneraac464e2005-01-21 06:05:23 +00001493/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1494/// destination type is legal.
1495SDOperand SelectionDAGLegalize::
1496ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1497 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1498 assert(getTypeAction(Source.getValueType()) == Expand &&
1499 "This is not an expansion!");
1500 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1501
Chris Lattner4add7e32005-01-23 04:42:50 +00001502 SDNode *OutChain;
1503 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1504 DAG.getEntryNode());
1505
Chris Lattner0dfd7d32005-01-23 23:19:44 +00001506 const char *FnName = 0;
Chris Lattneraac464e2005-01-21 06:05:23 +00001507 if (isSigned) {
1508 if (DestTy == MVT::f32)
1509 FnName = "__floatdisf";
1510 else {
1511 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1512 FnName = "__floatdidf";
1513 }
1514 } else {
1515 // If this is unsigned, and not supported, first perform the conversion to
1516 // signed, then adjust the result if the sign bit is set.
1517 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1518
1519 assert(0 && "Unsigned casts not supported yet!");
1520 }
1521 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1522
1523 TargetLowering::ArgListTy Args;
1524 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1525 Args.push_back(std::make_pair(Source, ArgTy));
1526
1527 // We don't care about token chains for libcalls. We just use the entry
1528 // node as our input and ignore the output chain. This allows us to place
1529 // calls wherever we need them to satisfy data dependences.
1530 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner4add7e32005-01-23 04:42:50 +00001531 return TLI.LowerCallTo(InChain, RetTy, Callee, Args, DAG).first;
1532
Chris Lattneraac464e2005-01-21 06:05:23 +00001533}
1534
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001535
1536
Chris Lattnerdc750592005-01-07 07:47:09 +00001537/// ExpandOp - Expand the specified SDOperand into its two component pieces
1538/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1539/// LegalizeNodes map is filled in for any results that are not expanded, the
1540/// ExpandedNodes map is filled in for any results that are expanded, and the
1541/// Lo/Hi values are returned.
1542void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1543 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001544 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001545 SDNode *Node = Op.Val;
1546 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1547 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1548 assert(MVT::isInteger(NVT) && NVT < VT &&
1549 "Cannot expand to FP value or to larger int value!");
1550
1551 // If there is more than one use of this, see if we already expanded it.
1552 // There is no use remembering values that only have a single use, as the map
1553 // entries will never be reused.
1554 if (!Node->hasOneUse()) {
1555 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1556 = ExpandedNodes.find(Op);
1557 if (I != ExpandedNodes.end()) {
1558 Lo = I->second.first;
1559 Hi = I->second.second;
1560 return;
1561 }
1562 }
1563
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001564 // Expanding to multiple registers needs to perform an optimization step, and
1565 // is not careful to avoid operations the target does not support. Make sure
1566 // that all generated operations are legalized in the next iteration.
1567 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001568
Chris Lattnerdc750592005-01-07 07:47:09 +00001569 switch (Node->getOpcode()) {
1570 default:
1571 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1572 assert(0 && "Do not know how to expand this operator!");
1573 abort();
1574 case ISD::Constant: {
1575 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1576 Lo = DAG.getConstant(Cst, NVT);
1577 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1578 break;
1579 }
1580
1581 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001582 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001583 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001584 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1585 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1586
1587 // Remember that we legalized the chain.
1588 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1589
Chris Lattnerdc750592005-01-07 07:47:09 +00001590 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1591 break;
1592 }
1593
1594 case ISD::LOAD: {
1595 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1596 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1597 Lo = DAG.getLoad(NVT, Ch, Ptr);
1598
1599 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001600 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001601 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1602 getIntPtrConstant(IncrementSize));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001603 Hi = DAG.getLoad(NVT, Ch, Ptr);
1604
1605 // Build a factor node to remember that this load is independent of the
1606 // other one.
1607 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1608 Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001609
1610 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00001611 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00001612 if (!TLI.isLittleEndian())
1613 std::swap(Lo, Hi);
1614 break;
1615 }
1616 case ISD::CALL: {
1617 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1618 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1619
Chris Lattner3d95c142005-01-19 20:24:35 +00001620 bool Changed = false;
1621 std::vector<SDOperand> Ops;
1622 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1623 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1624 Changed |= Ops.back() != Node->getOperand(i);
1625 }
1626
Chris Lattnerdc750592005-01-07 07:47:09 +00001627 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1628 "Can only expand a call once so far, not i64 -> i16!");
1629
1630 std::vector<MVT::ValueType> RetTyVTs;
1631 RetTyVTs.reserve(3);
1632 RetTyVTs.push_back(NVT);
1633 RetTyVTs.push_back(NVT);
1634 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001635 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00001636 Lo = SDOperand(NC, 0);
1637 Hi = SDOperand(NC, 1);
1638
1639 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001640 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001641 break;
1642 }
1643 case ISD::AND:
1644 case ISD::OR:
1645 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1646 SDOperand LL, LH, RL, RH;
1647 ExpandOp(Node->getOperand(0), LL, LH);
1648 ExpandOp(Node->getOperand(1), RL, RH);
1649 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1650 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1651 break;
1652 }
1653 case ISD::SELECT: {
1654 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001655
1656 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1657 case Expand: assert(0 && "It's impossible to expand bools");
1658 case Legal:
1659 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1660 break;
1661 case Promote:
1662 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1663 break;
1664 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001665 ExpandOp(Node->getOperand(1), LL, LH);
1666 ExpandOp(Node->getOperand(2), RL, RH);
1667 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1668 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1669 break;
1670 }
1671 case ISD::SIGN_EXTEND: {
1672 // The low part is just a sign extension of the input (which degenerates to
1673 // a copy).
1674 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1675
1676 // The high part is obtained by SRA'ing all but one of the bits of the lo
1677 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001678 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00001679 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
1680 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00001681 break;
1682 }
1683 case ISD::ZERO_EXTEND:
1684 // The low part is just a zero extension of the input (which degenerates to
1685 // a copy).
1686 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1687
1688 // The high part is just a zero.
1689 Hi = DAG.getConstant(0, NVT);
1690 break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001691
1692 // These operators cannot be expanded directly, emit them as calls to
1693 // library functions.
1694 case ISD::FP_TO_SINT:
1695 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001696 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001697 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001698 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001699 break;
1700 case ISD::FP_TO_UINT:
1701 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001702 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001703 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001704 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001705 break;
1706
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001707 case ISD::SHL:
1708 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001709 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001710 break;
1711 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001712 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001713 break;
1714
1715 case ISD::SRA:
1716 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001717 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001718 break;
1719 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001720 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001721 break;
1722 case ISD::SRL:
1723 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001724 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001725 break;
1726 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001727 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001728 break;
1729
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001730 case ISD::ADD:
1731 ExpandAddSub(true, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1732 break;
1733 case ISD::SUB:
1734 ExpandAddSub(false, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1735 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001736 case ISD::MUL: Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
1737 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
1738 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
1739 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
1740 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001741 }
1742
1743 // Remember in a map if the values will be reused later.
1744 if (!Node->hasOneUse()) {
1745 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1746 std::make_pair(Lo, Hi))).second;
1747 assert(isNew && "Value already expanded?!?");
1748 }
1749}
1750
1751
1752// SelectionDAG::Legalize - This is the entry point for the file.
1753//
Chris Lattner4add7e32005-01-23 04:42:50 +00001754void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00001755 /// run - This is the main entry point to this class.
1756 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00001757 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00001758}
1759