blob: 32cf5970726dc688d476dd2772158912db913017 [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: {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000709 Tmp1 = LegalizeOp(Node->getOperand(0)); // Function
710 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
711
712 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
713 switch (getTypeAction(Node->getOperand(2).getValueType())) {
714 case Expand: assert(0 && "Cannot expand a byte!");
715 case Legal:
716 Tmp3 = LegalizeOp(Node->getOperand(1));
717 break;
718 case Promote:
719 Tmp3 = PromoteOp(Node->getOperand(1));
720 break;
721 }
722 } else {
723 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
724 }
725 SDOperand Tmp4, Tmp5;
726
727 switch (getTypeAction(Node->getOperand(3).getValueType())) { // uint
728 case Expand: assert(0 && "Cannot expand this yet!");
729 case Legal:
730 Tmp4 = LegalizeOp(Node->getOperand(3));
731 Tmp5 = LegalizeOp(Node->getOperand(4));
732 break;
733 case Promote:
734 Tmp4 = PromoteOp(Node->getOperand(3));
735 Tmp5 = PromoteOp(Node->getOperand(4));
736 break;
737 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000738
739 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
740 default: assert(0 && "This action not implemented for this operation!");
741 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000742 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
743 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
744 Tmp5 != Node->getOperand(4)) {
745 std::vector<SDOperand> Ops;
746 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
747 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
748 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
749 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000750 break;
751 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000752 // Otherwise, the target does not support this operation. Lower the
753 // operation to an explicit libcall as appropriate.
754 MVT::ValueType IntPtr = TLI.getPointerTy();
755 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
756 std::vector<std::pair<SDOperand, const Type*> > Args;
757
Reid Spencer6dced922005-01-12 14:53:45 +0000758 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000759 if (Node->getOpcode() == ISD::MEMSET) {
760 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
761 // Extend the ubyte argument to be an int value for the call.
762 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
763 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
764 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
765
766 FnName = "memset";
767 } else if (Node->getOpcode() == ISD::MEMCPY ||
768 Node->getOpcode() == ISD::MEMMOVE) {
769 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
770 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
771 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
772 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
773 } else {
774 assert(0 && "Unknown op!");
775 }
776 std::pair<SDOperand,SDOperand> CallResult =
777 TLI.LowerCallTo(Tmp1, Type::VoidTy,
778 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
779 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000780 break;
781 }
782 case TargetLowering::Custom:
783 std::vector<SDOperand> Ops;
784 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
785 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
786 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
787 Result = TLI.LowerOperation(Result);
788 Result = LegalizeOp(Result);
789 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000790 }
791 break;
792 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000793 case ISD::ADD_PARTS:
794 case ISD::SUB_PARTS: {
795 std::vector<SDOperand> Ops;
796 bool Changed = false;
797 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
798 Ops.push_back(LegalizeOp(Node->getOperand(i)));
799 Changed |= Ops.back() != Node->getOperand(i);
800 }
801 if (Changed)
802 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
803 break;
804 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000805 case ISD::ADD:
806 case ISD::SUB:
807 case ISD::MUL:
808 case ISD::UDIV:
809 case ISD::SDIV:
810 case ISD::UREM:
811 case ISD::SREM:
812 case ISD::AND:
813 case ISD::OR:
814 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000815 case ISD::SHL:
816 case ISD::SRL:
817 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000818 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
819 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
820 if (Tmp1 != Node->getOperand(0) ||
821 Tmp2 != Node->getOperand(1))
822 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
823 break;
824 case ISD::ZERO_EXTEND:
825 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +0000826 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000827 case ISD::FP_EXTEND:
828 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +0000829 case ISD::FP_TO_SINT:
830 case ISD::FP_TO_UINT:
831 case ISD::SINT_TO_FP:
832 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +0000833 switch (getTypeAction(Node->getOperand(0).getValueType())) {
834 case Legal:
835 Tmp1 = LegalizeOp(Node->getOperand(0));
836 if (Tmp1 != Node->getOperand(0))
837 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
838 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +0000839 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +0000840 if (Node->getOpcode() == ISD::SINT_TO_FP ||
841 Node->getOpcode() == ISD::UINT_TO_FP) {
842 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
843 Node->getValueType(0), Node->getOperand(0));
844 Result = LegalizeOp(Result);
845 break;
846 }
Chris Lattnera65a2f02005-01-07 22:37:48 +0000847 // In the expand case, we must be dealing with a truncate, because
848 // otherwise the result would be larger than the source.
849 assert(Node->getOpcode() == ISD::TRUNCATE &&
850 "Shouldn't need to expand other operators here!");
851 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
852
853 // Since the result is legal, we should just be able to truncate the low
854 // part of the source.
855 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
856 break;
857
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000858 case Promote:
859 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000860 case ISD::ZERO_EXTEND:
861 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000862 // NOTE: Any extend would work here...
863 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
864 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000865 Result, Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000866 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000867 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000868 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000869 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +0000870 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000871 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
872 Result, Node->getOperand(0).getValueType());
873 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000874 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000875 Result = PromoteOp(Node->getOperand(0));
876 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
877 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000878 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +0000879 Result = PromoteOp(Node->getOperand(0));
880 if (Result.getValueType() != Op.getValueType())
881 // Dynamically dead while we have only 2 FP types.
882 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
883 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000884 case ISD::FP_ROUND:
885 case ISD::FP_TO_SINT:
886 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000887 Result = PromoteOp(Node->getOperand(0));
888 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
889 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000890 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000891 Result = PromoteOp(Node->getOperand(0));
892 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
893 Result, Node->getOperand(0).getValueType());
894 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
895 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000896 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +0000897 Result = PromoteOp(Node->getOperand(0));
898 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
899 Result, Node->getOperand(0).getValueType());
900 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
901 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000902 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000903 }
904 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000905 case ISD::FP_ROUND_INREG:
906 case ISD::SIGN_EXTEND_INREG:
Chris Lattner99222f72005-01-15 07:15:18 +0000907 case ISD::ZERO_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000908 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +0000909 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
910
911 // If this operation is not supported, convert it to a shl/shr or load/store
912 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +0000913 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
914 default: assert(0 && "This action not supported for this op yet!");
915 case TargetLowering::Legal:
916 if (Tmp1 != Node->getOperand(0))
917 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
918 ExtraVT);
919 break;
920 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +0000921 // If this is an integer extend and shifts are supported, do that.
922 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
923 // NOTE: we could fall back on load/store here too for targets without
924 // AND. However, it is doubtful that any exist.
925 // AND out the appropriate bits.
926 SDOperand Mask =
927 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
928 Node->getValueType(0));
929 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
930 Node->getOperand(0), Mask);
931 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
932 // NOTE: we could fall back on load/store here too for targets without
933 // SAR. However, it is doubtful that any exist.
934 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
935 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +0000936 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +0000937 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
938 Node->getOperand(0), ShiftCst);
939 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
940 Result, ShiftCst);
941 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
942 // The only way we can lower this is to turn it into a STORETRUNC,
943 // EXTLOAD pair, targetting a temporary location (a stack slot).
944
945 // NOTE: there is a choice here between constantly creating new stack
946 // slots and always reusing the same one. We currently always create
947 // new ones, as reuse may inhibit scheduling.
948 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
949 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
950 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
951 MachineFunction &MF = DAG.getMachineFunction();
952 int SSFI =
953 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
954 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
955 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
956 Node->getOperand(0), StackSlot, ExtraVT);
957 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
958 Result, StackSlot, ExtraVT);
959 } else {
960 assert(0 && "Unknown op");
961 }
962 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000963 break;
Chris Lattner99222f72005-01-15 07:15:18 +0000964 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000965 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000966 }
Chris Lattner99222f72005-01-15 07:15:18 +0000967 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000968
Chris Lattnerea4ca942005-01-07 22:28:47 +0000969 if (!Op.Val->hasOneUse())
970 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +0000971
972 return Result;
973}
974
Chris Lattner4d978642005-01-15 22:16:26 +0000975/// PromoteOp - Given an operation that produces a value in an invalid type,
976/// promote it to compute the value into a larger type. The produced value will
977/// have the correct bits for the low portion of the register, but no guarantee
978/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000979SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
980 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000981 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000982 assert(getTypeAction(VT) == Promote &&
983 "Caller should expand or legalize operands that are not promotable!");
984 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
985 "Cannot promote to smaller type!");
986
987 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
988 if (I != PromotedNodes.end()) return I->second;
989
990 SDOperand Tmp1, Tmp2, Tmp3;
991
992 SDOperand Result;
993 SDNode *Node = Op.Val;
994
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000995 // Promotion needs an optimization step to clean up after it, and is not
996 // careful to avoid operations the target does not support. Make sure that
997 // all generated operations are legalized in the next iteration.
998 NeedsAnotherIteration = true;
999
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001000 switch (Node->getOpcode()) {
1001 default:
1002 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1003 assert(0 && "Do not know how to promote this operator!");
1004 abort();
1005 case ISD::Constant:
1006 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1007 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1008 break;
1009 case ISD::ConstantFP:
1010 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1011 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1012 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001013 case ISD::CopyFromReg:
1014 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1015 Node->getOperand(0));
1016 // Remember that we legalized the chain.
1017 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1018 break;
1019
Chris Lattner2cb338d2005-01-18 02:59:52 +00001020 case ISD::SETCC:
1021 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1022 "SetCC type is not legal??");
1023 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1024 TLI.getSetCCResultTy(), Node->getOperand(0),
1025 Node->getOperand(1));
1026 Result = LegalizeOp(Result);
1027 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001028
1029 case ISD::TRUNCATE:
1030 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1031 case Legal:
1032 Result = LegalizeOp(Node->getOperand(0));
1033 assert(Result.getValueType() >= NVT &&
1034 "This truncation doesn't make sense!");
1035 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1036 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1037 break;
1038 case Expand:
1039 assert(0 && "Cannot handle expand yet");
1040 case Promote:
1041 assert(0 && "Cannot handle promote-promote yet");
1042 }
1043 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001044 case ISD::SIGN_EXTEND:
1045 case ISD::ZERO_EXTEND:
1046 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1047 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1048 case Legal:
1049 // Input is legal? Just do extend all the way to the larger type.
1050 Result = LegalizeOp(Node->getOperand(0));
1051 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1052 break;
1053 case Promote:
1054 // Promote the reg if it's smaller.
1055 Result = PromoteOp(Node->getOperand(0));
1056 // The high bits are not guaranteed to be anything. Insert an extend.
1057 if (Node->getOpcode() == ISD::SIGN_EXTEND)
1058 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, VT);
1059 else
1060 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, VT);
1061 break;
1062 }
1063 break;
1064
1065 case ISD::FP_EXTEND:
1066 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1067 case ISD::FP_ROUND:
1068 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1069 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1070 case Promote: assert(0 && "Unreachable with 2 FP types!");
1071 case Legal:
1072 // Input is legal? Do an FP_ROUND_INREG.
1073 Result = LegalizeOp(Node->getOperand(0));
1074 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1075 break;
1076 }
1077 break;
1078
1079 case ISD::SINT_TO_FP:
1080 case ISD::UINT_TO_FP:
1081 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1082 case Legal:
1083 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001084 // No extra round required here.
1085 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001086 break;
1087
1088 case Promote:
1089 Result = PromoteOp(Node->getOperand(0));
1090 if (Node->getOpcode() == ISD::SINT_TO_FP)
1091 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1092 Result, Node->getOperand(0).getValueType());
1093 else
1094 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1095 Result, Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001096 // No extra round required here.
1097 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001098 break;
1099 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001100 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1101 Node->getOperand(0));
1102 Result = LegalizeOp(Result);
1103
1104 // Round if we cannot tolerate excess precision.
1105 if (NoExcessFPPrecision)
1106 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1107 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001108 }
Chris Lattner4d978642005-01-15 22:16:26 +00001109 break;
1110
1111 case ISD::FP_TO_SINT:
1112 case ISD::FP_TO_UINT:
1113 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1114 case Legal:
1115 Tmp1 = LegalizeOp(Node->getOperand(0));
1116 break;
1117 case Promote:
1118 // The input result is prerounded, so we don't have to do anything
1119 // special.
1120 Tmp1 = PromoteOp(Node->getOperand(0));
1121 break;
1122 case Expand:
1123 assert(0 && "not implemented");
1124 }
1125 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1126 break;
1127
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001128 case ISD::AND:
1129 case ISD::OR:
1130 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001131 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001132 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001133 case ISD::MUL:
1134 // The input may have strange things in the top bits of the registers, but
1135 // these operations don't care. They may have wierd bits going out, but
1136 // that too is okay if they are integer operations.
1137 Tmp1 = PromoteOp(Node->getOperand(0));
1138 Tmp2 = PromoteOp(Node->getOperand(1));
1139 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1140 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1141
1142 // However, if this is a floating point operation, they will give excess
1143 // precision that we may not be able to tolerate. If we DO allow excess
1144 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001145 // FIXME: Why would we need to round FP ops more than integer ones?
1146 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001147 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1148 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1149 break;
1150
Chris Lattner4d978642005-01-15 22:16:26 +00001151 case ISD::SDIV:
1152 case ISD::SREM:
1153 // These operators require that their input be sign extended.
1154 Tmp1 = PromoteOp(Node->getOperand(0));
1155 Tmp2 = PromoteOp(Node->getOperand(1));
1156 if (MVT::isInteger(NVT)) {
1157 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001158 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001159 }
1160 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1161
1162 // Perform FP_ROUND: this is probably overly pessimistic.
1163 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1164 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1165 break;
1166
1167 case ISD::UDIV:
1168 case ISD::UREM:
1169 // These operators require that their input be zero extended.
1170 Tmp1 = PromoteOp(Node->getOperand(0));
1171 Tmp2 = PromoteOp(Node->getOperand(1));
1172 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1173 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001174 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001175 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1176 break;
1177
1178 case ISD::SHL:
1179 Tmp1 = PromoteOp(Node->getOperand(0));
1180 Tmp2 = LegalizeOp(Node->getOperand(1));
1181 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1182 break;
1183 case ISD::SRA:
1184 // The input value must be properly sign extended.
1185 Tmp1 = PromoteOp(Node->getOperand(0));
1186 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1187 Tmp2 = LegalizeOp(Node->getOperand(1));
1188 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1189 break;
1190 case ISD::SRL:
1191 // The input value must be properly zero extended.
1192 Tmp1 = PromoteOp(Node->getOperand(0));
1193 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1194 Tmp2 = LegalizeOp(Node->getOperand(1));
1195 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1196 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001197 case ISD::LOAD:
1198 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1199 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1200 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1201
1202 // Remember that we legalized the chain.
1203 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1204 break;
1205 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001206 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1207 case Expand: assert(0 && "It's impossible to expand bools");
1208 case Legal:
1209 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1210 break;
1211 case Promote:
1212 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1213 break;
1214 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001215 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1216 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1217 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1218 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001219 case ISD::CALL: {
1220 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1221 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1222
Chris Lattner3d95c142005-01-19 20:24:35 +00001223 std::vector<SDOperand> Ops;
1224 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1225 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1226
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001227 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1228 "Can only promote single result calls");
1229 std::vector<MVT::ValueType> RetTyVTs;
1230 RetTyVTs.reserve(2);
1231 RetTyVTs.push_back(NVT);
1232 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001233 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001234 Result = SDOperand(NC, 0);
1235
1236 // Insert the new chain mapping.
1237 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1238 break;
1239 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001240 }
1241
1242 assert(Result.Val && "Didn't set a result!");
1243 AddPromotedOperand(Op, Result);
1244 return Result;
1245}
Chris Lattnerdc750592005-01-07 07:47:09 +00001246
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001247/// ExpandAddSub - Find a clever way to expand this add operation into
1248/// subcomponents.
1249void SelectionDAGLegalize::ExpandAddSub(bool isAdd, SDOperand LHS,SDOperand RHS,
1250 SDOperand &Lo, SDOperand &Hi) {
1251 // Expand the subcomponents.
1252 SDOperand LHSL, LHSH, RHSL, RHSH;
1253 ExpandOp(LHS, LHSL, LHSH);
1254 ExpandOp(RHS, RHSL, RHSH);
1255
1256 // Convert this add to the appropriate ADDC pair. The low part has no carry
1257 // in.
1258 unsigned Opc = isAdd ? ISD::ADD_PARTS : ISD::SUB_PARTS;
1259 std::vector<SDOperand> Ops;
1260 Ops.push_back(LHSL);
1261 Ops.push_back(LHSH);
1262 Ops.push_back(RHSL);
1263 Ops.push_back(RHSH);
1264 Lo = DAG.getNode(Opc, LHSL.getValueType(), Ops);
1265 Hi = Lo.getValue(1);
1266}
1267
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001268/// ExpandShift - Try to find a clever way to expand this shift operation out to
1269/// smaller elements. If we can't find a way that is more efficient than a
1270/// libcall on this target, return false. Otherwise, return true with the
1271/// low-parts expanded into Lo and Hi.
1272bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1273 SDOperand &Lo, SDOperand &Hi) {
1274 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1275 "This is not a shift!");
1276 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
1277
1278 // If we have an efficient select operation (or if the selects will all fold
1279 // away), lower to some complex code, otherwise just emit the libcall.
1280 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1281 !isa<ConstantSDNode>(Amt))
1282 return false;
1283
1284 SDOperand InL, InH;
1285 ExpandOp(Op, InL, InH);
1286 SDOperand ShAmt = LegalizeOp(Amt);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001287 MVT::ValueType ShTy = ShAmt.getValueType();
1288
1289 unsigned NVTBits = MVT::getSizeInBits(NVT);
1290 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1291 DAG.getConstant(NVTBits, ShTy), ShAmt);
1292
Chris Lattner4d25c042005-01-20 20:29:23 +00001293 // Compare the unmasked shift amount against 32.
1294 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1295 DAG.getConstant(NVTBits, ShTy));
1296
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001297 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1298 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1299 DAG.getConstant(NVTBits-1, ShTy));
1300 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1301 DAG.getConstant(NVTBits-1, ShTy));
1302 }
1303
1304 if (Opc == ISD::SHL) {
1305 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1306 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1307 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001308 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001309
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001310 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1311 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1312 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001313 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1314 DAG.getSetCC(ISD::SETEQ,
1315 TLI.getSetCCResultTy(), NAmt,
1316 DAG.getConstant(32, ShTy)),
1317 DAG.getConstant(0, NVT),
1318 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001319 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001320 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001321 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001322 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001323
1324 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001325 if (Opc == ISD::SRA)
1326 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1327 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001328 else
1329 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001330 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001331 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001332 }
1333 return true;
1334}
Chris Lattneraac464e2005-01-21 06:05:23 +00001335
Chris Lattner4add7e32005-01-23 04:42:50 +00001336/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1337/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1338/// Found.
1339static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1340 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1341
1342 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1343 // than the Found node. Just remember this node and return.
1344 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1345 Found = Node;
1346 return;
1347 }
1348
1349 // Otherwise, scan the operands of Node to see if any of them is a call.
1350 assert(Node->getNumOperands() != 0 &&
1351 "All leaves should have depth equal to the entry node!");
1352 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1353 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1354
1355 // Tail recurse for the last iteration.
1356 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1357 Found);
1358}
1359
1360
1361/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1362/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1363/// than Found.
1364static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1365 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1366
1367 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1368 // than the Found node. Just remember this node and return.
1369 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1370 Found = Node;
1371 return;
1372 }
1373
1374 // Otherwise, scan the operands of Node to see if any of them is a call.
1375 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1376 if (UI == E) return;
1377 for (--E; UI != E; ++UI)
1378 FindEarliestAdjCallStackUp(*UI, Found);
1379
1380 // Tail recurse for the last iteration.
1381 FindEarliestAdjCallStackUp(*UI, Found);
1382}
1383
1384/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1385/// find the ADJCALLSTACKUP node that terminates the call sequence.
1386static SDNode *FindAdjCallStackUp(SDNode *Node) {
1387 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1388 return Node;
1389 assert(!Node->use_empty() && "Could not find ADJCALLSTACKUP!");
1390
1391 if (Node->hasOneUse()) // Simple case, only has one user to check.
1392 return FindAdjCallStackUp(*Node->use_begin());
1393
1394 SDOperand TheChain(Node, Node->getNumValues()-1);
1395 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
1396
1397 for (SDNode::use_iterator UI = Node->use_begin(),
1398 E = Node->use_end(); ; ++UI) {
1399 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
1400
1401 // Make sure to only follow users of our token chain.
1402 SDNode *User = *UI;
1403 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1404 if (User->getOperand(i) == TheChain)
1405 return FindAdjCallStackUp(User);
1406 }
1407 assert(0 && "Unreachable");
1408 abort();
1409}
1410
1411/// FindInputOutputChains - If we are replacing an operation with a call we need
1412/// to find the call that occurs before and the call that occurs after it to
1413/// properly serialize the calls in the block.
1414static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1415 SDOperand Entry) {
1416 SDNode *LatestAdjCallStackDown = Entry.Val;
1417 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1418 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
1419
1420 SDNode *LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1421
1422
1423 SDNode *EarliestAdjCallStackUp = 0;
1424 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1425
1426 if (EarliestAdjCallStackUp) {
1427 //std::cerr << "Found node: ";
1428 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1429 }
1430
1431 return SDOperand(LatestAdjCallStackUp, 0);
1432}
1433
1434
1435
Chris Lattneraac464e2005-01-21 06:05:23 +00001436// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1437// does not fit into a register, return the lo part and set the hi part to the
1438// by-reg argument. If it does fit into a single register, return the result
1439// and leave the Hi part unset.
1440SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1441 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001442 SDNode *OutChain;
1443 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1444 DAG.getEntryNode());
1445 // TODO. Link in chains.
1446
Chris Lattneraac464e2005-01-21 06:05:23 +00001447 TargetLowering::ArgListTy Args;
1448 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1449 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1450 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1451 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1452 }
1453 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1454
1455 // We don't care about token chains for libcalls. We just use the entry
1456 // node as our input and ignore the output chain. This allows us to place
1457 // calls wherever we need them to satisfy data dependences.
1458 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner4add7e32005-01-23 04:42:50 +00001459 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, Callee,
Chris Lattneraac464e2005-01-21 06:05:23 +00001460 Args, DAG).first;
1461 switch (getTypeAction(Result.getValueType())) {
1462 default: assert(0 && "Unknown thing");
1463 case Legal:
1464 return Result;
1465 case Promote:
1466 assert(0 && "Cannot promote this yet!");
1467 case Expand:
1468 SDOperand Lo;
1469 ExpandOp(Result, Lo, Hi);
1470 return Lo;
1471 }
1472}
1473
Chris Lattner4add7e32005-01-23 04:42:50 +00001474
Chris Lattneraac464e2005-01-21 06:05:23 +00001475/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1476/// destination type is legal.
1477SDOperand SelectionDAGLegalize::
1478ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1479 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1480 assert(getTypeAction(Source.getValueType()) == Expand &&
1481 "This is not an expansion!");
1482 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1483
Chris Lattner4add7e32005-01-23 04:42:50 +00001484 SDNode *OutChain;
1485 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1486 DAG.getEntryNode());
1487
Chris Lattner0dfd7d32005-01-23 23:19:44 +00001488 const char *FnName = 0;
Chris Lattneraac464e2005-01-21 06:05:23 +00001489 if (isSigned) {
1490 if (DestTy == MVT::f32)
1491 FnName = "__floatdisf";
1492 else {
1493 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1494 FnName = "__floatdidf";
1495 }
1496 } else {
1497 // If this is unsigned, and not supported, first perform the conversion to
1498 // signed, then adjust the result if the sign bit is set.
1499 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1500
1501 assert(0 && "Unsigned casts not supported yet!");
1502 }
1503 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1504
1505 TargetLowering::ArgListTy Args;
1506 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1507 Args.push_back(std::make_pair(Source, ArgTy));
1508
1509 // We don't care about token chains for libcalls. We just use the entry
1510 // node as our input and ignore the output chain. This allows us to place
1511 // calls wherever we need them to satisfy data dependences.
1512 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner4add7e32005-01-23 04:42:50 +00001513 return TLI.LowerCallTo(InChain, RetTy, Callee, Args, DAG).first;
1514
Chris Lattneraac464e2005-01-21 06:05:23 +00001515}
1516
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001517
1518
Chris Lattnerdc750592005-01-07 07:47:09 +00001519/// ExpandOp - Expand the specified SDOperand into its two component pieces
1520/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1521/// LegalizeNodes map is filled in for any results that are not expanded, the
1522/// ExpandedNodes map is filled in for any results that are expanded, and the
1523/// Lo/Hi values are returned.
1524void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1525 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001526 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001527 SDNode *Node = Op.Val;
1528 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1529 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1530 assert(MVT::isInteger(NVT) && NVT < VT &&
1531 "Cannot expand to FP value or to larger int value!");
1532
1533 // If there is more than one use of this, see if we already expanded it.
1534 // There is no use remembering values that only have a single use, as the map
1535 // entries will never be reused.
1536 if (!Node->hasOneUse()) {
1537 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1538 = ExpandedNodes.find(Op);
1539 if (I != ExpandedNodes.end()) {
1540 Lo = I->second.first;
1541 Hi = I->second.second;
1542 return;
1543 }
1544 }
1545
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001546 // Expanding to multiple registers needs to perform an optimization step, and
1547 // is not careful to avoid operations the target does not support. Make sure
1548 // that all generated operations are legalized in the next iteration.
1549 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001550
Chris Lattnerdc750592005-01-07 07:47:09 +00001551 switch (Node->getOpcode()) {
1552 default:
1553 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1554 assert(0 && "Do not know how to expand this operator!");
1555 abort();
1556 case ISD::Constant: {
1557 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1558 Lo = DAG.getConstant(Cst, NVT);
1559 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1560 break;
1561 }
1562
1563 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00001564 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00001565 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00001566 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1567 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1568
1569 // Remember that we legalized the chain.
1570 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1571
Chris Lattnerdc750592005-01-07 07:47:09 +00001572 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1573 break;
1574 }
1575
1576 case ISD::LOAD: {
1577 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1578 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1579 Lo = DAG.getLoad(NVT, Ch, Ptr);
1580
1581 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00001582 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001583 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1584 getIntPtrConstant(IncrementSize));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001585 Hi = DAG.getLoad(NVT, Ch, Ptr);
1586
1587 // Build a factor node to remember that this load is independent of the
1588 // other one.
1589 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1590 Hi.getValue(1));
Chris Lattnerdc750592005-01-07 07:47:09 +00001591
1592 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00001593 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00001594 if (!TLI.isLittleEndian())
1595 std::swap(Lo, Hi);
1596 break;
1597 }
1598 case ISD::CALL: {
1599 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1600 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1601
Chris Lattner3d95c142005-01-19 20:24:35 +00001602 bool Changed = false;
1603 std::vector<SDOperand> Ops;
1604 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1605 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1606 Changed |= Ops.back() != Node->getOperand(i);
1607 }
1608
Chris Lattnerdc750592005-01-07 07:47:09 +00001609 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1610 "Can only expand a call once so far, not i64 -> i16!");
1611
1612 std::vector<MVT::ValueType> RetTyVTs;
1613 RetTyVTs.reserve(3);
1614 RetTyVTs.push_back(NVT);
1615 RetTyVTs.push_back(NVT);
1616 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001617 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00001618 Lo = SDOperand(NC, 0);
1619 Hi = SDOperand(NC, 1);
1620
1621 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00001622 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00001623 break;
1624 }
1625 case ISD::AND:
1626 case ISD::OR:
1627 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1628 SDOperand LL, LH, RL, RH;
1629 ExpandOp(Node->getOperand(0), LL, LH);
1630 ExpandOp(Node->getOperand(1), RL, RH);
1631 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1632 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1633 break;
1634 }
1635 case ISD::SELECT: {
1636 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001637
1638 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1639 case Expand: assert(0 && "It's impossible to expand bools");
1640 case Legal:
1641 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1642 break;
1643 case Promote:
1644 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1645 break;
1646 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001647 ExpandOp(Node->getOperand(1), LL, LH);
1648 ExpandOp(Node->getOperand(2), RL, RH);
1649 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1650 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1651 break;
1652 }
1653 case ISD::SIGN_EXTEND: {
1654 // The low part is just a sign extension of the input (which degenerates to
1655 // a copy).
1656 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1657
1658 // The high part is obtained by SRA'ing all but one of the bits of the lo
1659 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00001660 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00001661 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
1662 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00001663 break;
1664 }
1665 case ISD::ZERO_EXTEND:
1666 // The low part is just a zero extension of the input (which degenerates to
1667 // a copy).
1668 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1669
1670 // The high part is just a zero.
1671 Hi = DAG.getConstant(0, NVT);
1672 break;
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001673
1674 // These operators cannot be expanded directly, emit them as calls to
1675 // library functions.
1676 case ISD::FP_TO_SINT:
1677 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001678 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001679 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001680 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001681 break;
1682 case ISD::FP_TO_UINT:
1683 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00001684 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001685 else
Chris Lattneraac464e2005-01-21 06:05:23 +00001686 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001687 break;
1688
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001689 case ISD::SHL:
1690 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001691 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001692 break;
1693 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001694 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001695 break;
1696
1697 case ISD::SRA:
1698 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001699 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001700 break;
1701 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001702 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001703 break;
1704 case ISD::SRL:
1705 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00001706 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001707 break;
1708 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00001709 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001710 break;
1711
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001712 case ISD::ADD:
1713 ExpandAddSub(true, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1714 break;
1715 case ISD::SUB:
1716 ExpandAddSub(false, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1717 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001718 case ISD::MUL: Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
1719 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
1720 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
1721 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
1722 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001723 }
1724
1725 // Remember in a map if the values will be reused later.
1726 if (!Node->hasOneUse()) {
1727 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1728 std::make_pair(Lo, Hi))).second;
1729 assert(isNew && "Value already expanded?!?");
1730 }
1731}
1732
1733
1734// SelectionDAG::Legalize - This is the entry point for the file.
1735//
Chris Lattner4add7e32005-01-23 04:42:50 +00001736void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00001737 /// run - This is the main entry point to this class.
1738 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00001739 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00001740}
1741