blob: 7d283ffd3da9fde033b571a840592faa8166c355 [file] [log] [blame]
Chris Lattner3e928bb2005-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 Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Constants.h"
22#include <iostream>
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27/// hacks on it until the target machine can handle it. This involves
28/// eliminating value sizes the machine cannot handle (promoting small sizes to
29/// large sizes or splitting up large values into small values) as well as
30/// eliminating operations the machine cannot handle.
31///
32/// This code also does a small amount of optimization and recognition of idioms
33/// as part of its processing. For example, if a target does not support a
34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35/// will attempt merge setcc and brc instructions into brcc's.
36///
37namespace {
38class SelectionDAGLegalize {
39 TargetLowering &TLI;
40 SelectionDAG &DAG;
41
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
44 enum LegalizeAction {
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
48 };
49
Chris Lattner3e928bb2005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
54
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
60
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
65
Chris Lattner03c85462005-01-15 05:21:40 +000066 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
74 /// more than once.
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
76
Chris Lattner8afc48e2005-01-07 22:28:47 +000077 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
80 }
Chris Lattner03c85462005-01-15 05:21:40 +000081 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
84 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000085
Chris Lattner3e928bb2005-01-07 07:47:09 +000086public:
87
88 SelectionDAGLegalize(TargetLowering &TLI, SelectionDAG &DAG);
89
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
95 void Run() {
96 do {
97 NeedsAnotherIteration = false;
98 LegalizeDAG();
99 } while (NeedsAnotherIteration);
100 }
101
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
107 }
108
109 /// isTypeLegal - Return true if this type is legal on this target.
110 ///
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
113 }
114
115private:
116 void LegalizeDAG();
117
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121
Chris Lattner77e77a62005-01-21 06:05:23 +0000122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
123 SDOperand &Hi);
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
125 SDOperand Source);
Chris Lattnere34b3962005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattner84f67882005-01-20 18:52:28 +0000128 void ExpandAddSub(bool isAdd, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000130
Chris Lattner3e928bb2005-01-07 07:47:09 +0000131 SDOperand getIntPtrConstant(uint64_t Val) {
132 return DAG.getConstant(Val, TLI.getPointerTy());
133 }
134};
135}
136
137
138SelectionDAGLegalize::SelectionDAGLegalize(TargetLowering &tli,
139 SelectionDAG &dag)
Chris Lattner71c42a02005-01-16 01:11:45 +0000140 : TLI(tli), DAG(dag), ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000141 assert(MVT::LAST_VALUETYPE <= 16 &&
142 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000143}
144
Chris Lattner3e928bb2005-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 Lattner71c42a02005-01-16 01:11:45 +0000152 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000153
154 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000155 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000156}
157
158SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000159 assert(getTypeAction(Op.getValueType()) == Legal &&
160 "Caller should expand or promote operands that are not legal!");
161
Chris Lattner3e928bb2005-01-07 07:47:09 +0000162 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000163 // register on this target, make sure to expand or promote them.
164 if (Op.Val->getNumValues() > 1) {
Chris Lattner3e928bb2005-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 Lattner03c85462005-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 Lattner3e928bb2005-01-07 07:47:09 +0000180 }
181 }
182
Chris Lattnere1bd8222005-01-11 05:57:22 +0000183 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
184 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000185
Chris Lattnerfa404e82005-01-09 19:03:49 +0000186 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000187
188 SDOperand Result = Op;
189 SDNode *Node = Op.Val;
Chris Lattner3e928bb2005-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 Lattner03c0cf82005-01-07 21:45:56 +0000199 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000200 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000201 assert(getTypeAction(Node->getValueType(0)) == Legal &&
202 "This must be legal!");
203 break;
Chris Lattner69a52152005-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);
209 break;
Chris Lattner18c2f132005-01-13 20:50:02 +0000210 case ISD::ImplicitDef:
211 Tmp1 = LegalizeOp(Node->getOperand(0));
212 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000213 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000214 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000215 case ISD::Constant:
216 // We know we don't need to expand constants here, constants only have one
217 // value and we check that it is fine above.
218
219 // FIXME: Maybe we should handle things like targets that don't support full
220 // 32-bit immediates?
221 break;
222 case ISD::ConstantFP: {
223 // Spill FP immediates to the constant pool if the target cannot directly
224 // codegen them. Targets often have some immediate values that can be
225 // efficiently generated into an FP register without a load. We explicitly
226 // leave these constants as ConstantFP nodes for the target to deal with.
227
228 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
229
230 // Check to see if this FP immediate is already legal.
231 bool isLegal = false;
232 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
233 E = TLI.legal_fpimm_end(); I != E; ++I)
234 if (CFP->isExactlyValue(*I)) {
235 isLegal = true;
236 break;
237 }
238
239 if (!isLegal) {
240 // Otherwise we need to spill the constant to memory.
241 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
242
243 bool Extend = false;
244
245 // If a FP immediate is precise when represented as a float, we put it
246 // into the constant pool as a float, even if it's is statically typed
247 // as a double.
248 MVT::ValueType VT = CFP->getValueType(0);
249 bool isDouble = VT == MVT::f64;
250 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
251 Type::FloatTy, CFP->getValue());
252 if (isDouble && CFP->isExactlyValue((float)CFP->getValue())) {
253 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
254 VT = MVT::f32;
255 Extend = true;
256 }
257
258 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
259 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000260 if (Extend) {
261 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
262 MVT::f32);
263 } else {
264 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx);
265 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000266 }
267 break;
268 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000269 case ISD::TokenFactor: {
270 std::vector<SDOperand> Ops;
271 bool Changed = false;
272 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000273 SDOperand Op = Node->getOperand(i);
274 // Fold single-use TokenFactor nodes into this token factor as we go.
275 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
276 Changed = true;
277 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
278 Ops.push_back(LegalizeOp(Op.getOperand(j)));
279 } else {
280 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
281 Changed |= Ops[i] != Op;
282 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000283 }
284 if (Changed)
285 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
286 break;
287 }
288
Chris Lattner3e928bb2005-01-07 07:47:09 +0000289 case ISD::ADJCALLSTACKDOWN:
290 case ISD::ADJCALLSTACKUP:
291 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
292 // There is no need to legalize the size argument (Operand #1)
293 if (Tmp1 != Node->getOperand(0))
294 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
295 Node->getOperand(1));
296 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000297 case ISD::DYNAMIC_STACKALLOC:
298 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
299 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
300 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
301 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
302 Tmp3 != Node->getOperand(2))
303 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
304 Tmp1, Tmp2, Tmp3);
Chris Lattner513e52e2005-01-09 19:07:54 +0000305 else
306 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000307
308 // Since this op produces two values, make sure to remember that we
309 // legalized both of them.
310 AddLegalizedOperand(SDOperand(Node, 0), Result);
311 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
312 return Result.getValue(Op.ResNo);
313
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000314 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000315 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
316 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000317
318 bool Changed = false;
319 std::vector<SDOperand> Ops;
320 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
321 Ops.push_back(LegalizeOp(Node->getOperand(i)));
322 Changed |= Ops.back() != Node->getOperand(i);
323 }
324
325 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000326 std::vector<MVT::ValueType> RetTyVTs;
327 RetTyVTs.reserve(Node->getNumValues());
328 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000329 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000330 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000331 } else {
332 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000333 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000334 // Since calls produce multiple values, make sure to remember that we
335 // legalized all of them.
336 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
337 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
338 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000339 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000340 case ISD::BR:
341 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
342 if (Tmp1 != Node->getOperand(0))
343 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
344 break;
345
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000346 case ISD::BRCOND:
347 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000348
349 switch (getTypeAction(Node->getOperand(1).getValueType())) {
350 case Expand: assert(0 && "It's impossible to expand bools");
351 case Legal:
352 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
353 break;
354 case Promote:
355 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
356 break;
357 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000358 // Basic block destination (Op#2) is always legal.
359 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
360 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
361 Node->getOperand(2));
362 break;
363
Chris Lattner3e928bb2005-01-07 07:47:09 +0000364 case ISD::LOAD:
365 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
366 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
367 if (Tmp1 != Node->getOperand(0) ||
368 Tmp2 != Node->getOperand(1))
369 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner8afc48e2005-01-07 22:28:47 +0000370 else
371 Result = SDOperand(Node, 0);
372
373 // Since loads produce two values, make sure to remember that we legalized
374 // both of them.
375 AddLegalizedOperand(SDOperand(Node, 0), Result);
376 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
377 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000378
Chris Lattner0f69b292005-01-15 06:18:18 +0000379 case ISD::EXTLOAD:
380 case ISD::SEXTLOAD:
381 case ISD::ZEXTLOAD:
382 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
384 if (Tmp1 != Node->getOperand(0) ||
385 Tmp2 != Node->getOperand(1))
386 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1, Tmp2,
387 cast<MVTSDNode>(Node)->getExtraValueType());
388 else
389 Result = SDOperand(Node, 0);
390
391 // Since loads produce two values, make sure to remember that we legalized
392 // both of them.
393 AddLegalizedOperand(SDOperand(Node, 0), Result);
394 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
395 return Result.getValue(Op.ResNo);
396
Chris Lattner3e928bb2005-01-07 07:47:09 +0000397 case ISD::EXTRACT_ELEMENT:
398 // Get both the low and high parts.
399 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
400 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
401 Result = Tmp2; // 1 -> Hi
402 else
403 Result = Tmp1; // 0 -> Lo
404 break;
405
406 case ISD::CopyToReg:
407 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
408
409 switch (getTypeAction(Node->getOperand(1).getValueType())) {
410 case Legal:
411 // Legalize the incoming value (must be legal).
412 Tmp2 = LegalizeOp(Node->getOperand(1));
413 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000414 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000415 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000416 case Promote:
417 Tmp2 = PromoteOp(Node->getOperand(1));
418 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
419 break;
420 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000421 SDOperand Lo, Hi;
422 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000423 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000424 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
425 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
426 // Note that the copytoreg nodes are independent of each other.
427 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000428 assert(isTypeLegal(Result.getValueType()) &&
429 "Cannot expand multiple times yet (i64 -> i16)");
430 break;
431 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000432 break;
433
434 case ISD::RET:
435 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
436 switch (Node->getNumOperands()) {
437 case 2: // ret val
438 switch (getTypeAction(Node->getOperand(1).getValueType())) {
439 case Legal:
440 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000441 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000442 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
443 break;
444 case Expand: {
445 SDOperand Lo, Hi;
446 ExpandOp(Node->getOperand(1), Lo, Hi);
447 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
448 break;
449 }
450 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000451 Tmp2 = PromoteOp(Node->getOperand(1));
452 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
453 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000454 }
455 break;
456 case 1: // ret void
457 if (Tmp1 != Node->getOperand(0))
458 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
459 break;
460 default: { // ret <values>
461 std::vector<SDOperand> NewValues;
462 NewValues.push_back(Tmp1);
463 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
464 switch (getTypeAction(Node->getOperand(i).getValueType())) {
465 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000466 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000467 break;
468 case Expand: {
469 SDOperand Lo, Hi;
470 ExpandOp(Node->getOperand(i), Lo, Hi);
471 NewValues.push_back(Lo);
472 NewValues.push_back(Hi);
473 break;
474 }
475 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000476 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000477 }
478 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
479 break;
480 }
481 }
482 break;
483 case ISD::STORE:
484 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
485 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
486
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000487 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000488 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000489 if (CFP->getValueType(0) == MVT::f32) {
490 union {
491 unsigned I;
492 float F;
493 } V;
494 V.F = CFP->getValue();
495 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
496 DAG.getConstant(V.I, MVT::i32), Tmp2);
497 } else {
498 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
499 union {
500 uint64_t I;
501 double F;
502 } V;
503 V.F = CFP->getValue();
504 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
505 DAG.getConstant(V.I, MVT::i64), Tmp2);
506 }
507 Op = Result;
508 Node = Op.Val;
509 }
510
Chris Lattner3e928bb2005-01-07 07:47:09 +0000511 switch (getTypeAction(Node->getOperand(1).getValueType())) {
512 case Legal: {
513 SDOperand Val = LegalizeOp(Node->getOperand(1));
514 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
515 Tmp2 != Node->getOperand(2))
516 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2);
517 break;
518 }
519 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000520 // Truncate the value and store the result.
521 Tmp3 = PromoteOp(Node->getOperand(1));
522 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
523 Node->getOperand(1).getValueType());
524 break;
525
Chris Lattner3e928bb2005-01-07 07:47:09 +0000526 case Expand:
527 SDOperand Lo, Hi;
528 ExpandOp(Node->getOperand(1), Lo, Hi);
529
530 if (!TLI.isLittleEndian())
531 std::swap(Lo, Hi);
532
Chris Lattnerec39a452005-01-19 18:02:17 +0000533 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000534
Chris Lattnerec39a452005-01-19 18:02:17 +0000535 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000536 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
537 getIntPtrConstant(IncrementSize));
538 assert(isTypeLegal(Tmp2.getValueType()) &&
539 "Pointers must be legal!");
Chris Lattnerec39a452005-01-19 18:02:17 +0000540 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2);
541 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
542 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000543 }
544 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000545 case ISD::TRUNCSTORE:
546 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
547 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
548
549 switch (getTypeAction(Node->getOperand(1).getValueType())) {
550 case Legal:
551 Tmp2 = LegalizeOp(Node->getOperand(1));
552 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
553 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000554 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner0f69b292005-01-15 06:18:18 +0000555 cast<MVTSDNode>(Node)->getExtraValueType());
556 break;
557 case Promote:
558 case Expand:
559 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
560 }
561 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000562 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000563 switch (getTypeAction(Node->getOperand(0).getValueType())) {
564 case Expand: assert(0 && "It's impossible to expand bools");
565 case Legal:
566 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
567 break;
568 case Promote:
569 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
570 break;
571 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000572 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000573 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000574
575 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
576 default: assert(0 && "This action is not supported yet!");
577 case TargetLowering::Legal:
578 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
579 Tmp3 != Node->getOperand(2))
580 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
581 Tmp1, Tmp2, Tmp3);
582 break;
583 case TargetLowering::Promote: {
584 MVT::ValueType NVT =
585 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
586 unsigned ExtOp, TruncOp;
587 if (MVT::isInteger(Tmp2.getValueType())) {
588 ExtOp = ISD::ZERO_EXTEND;
589 TruncOp = ISD::TRUNCATE;
590 } else {
591 ExtOp = ISD::FP_EXTEND;
592 TruncOp = ISD::FP_ROUND;
593 }
594 // Promote each of the values to the new type.
595 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
596 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
597 // Perform the larger operation, then round down.
598 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
599 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
600 break;
601 }
602 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000603 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000604 case ISD::SETCC:
605 switch (getTypeAction(Node->getOperand(0).getValueType())) {
606 case Legal:
607 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
608 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
609 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
610 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000611 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000612 break;
613 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000614 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
615 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
616
617 // If this is an FP compare, the operands have already been extended.
618 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
619 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000620 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000621
622 // Otherwise, we have to insert explicit sign or zero extends. Note
623 // that we could insert sign extends for ALL conditions, but zero extend
624 // is cheaper on many machines (an AND instead of two shifts), so prefer
625 // it.
626 switch (cast<SetCCSDNode>(Node)->getCondition()) {
627 default: assert(0 && "Unknown integer comparison!");
628 case ISD::SETEQ:
629 case ISD::SETNE:
630 case ISD::SETUGE:
631 case ISD::SETUGT:
632 case ISD::SETULE:
633 case ISD::SETULT:
634 // ALL of these operations will work if we either sign or zero extend
635 // the operands (including the unsigned comparisons!). Zero extend is
636 // usually a simpler/cheaper operation, so prefer it.
637 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
638 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
639 break;
640 case ISD::SETGE:
641 case ISD::SETGT:
642 case ISD::SETLT:
643 case ISD::SETLE:
644 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
645 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
646 break;
647 }
648
649 }
650 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000651 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000652 break;
653 case Expand:
654 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
655 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
656 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
657 switch (cast<SetCCSDNode>(Node)->getCondition()) {
658 case ISD::SETEQ:
659 case ISD::SETNE:
660 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
661 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
662 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000663 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
664 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000665 DAG.getConstant(0, Tmp1.getValueType()));
666 break;
667 default:
668 // FIXME: This generated code sucks.
669 ISD::CondCode LowCC;
670 switch (cast<SetCCSDNode>(Node)->getCondition()) {
671 default: assert(0 && "Unknown integer setcc!");
672 case ISD::SETLT:
673 case ISD::SETULT: LowCC = ISD::SETULT; break;
674 case ISD::SETGT:
675 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
676 case ISD::SETLE:
677 case ISD::SETULE: LowCC = ISD::SETULE; break;
678 case ISD::SETGE:
679 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
680 }
681
682 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
683 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
684 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
685
686 // NOTE: on targets without efficient SELECT of bools, we can always use
687 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000688 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000689 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000690 Node->getValueType(0), LHSHi, RHSHi);
691 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
692 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
693 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000694 break;
695 }
696 }
697 break;
698
Chris Lattnere1bd8222005-01-11 05:57:22 +0000699 case ISD::MEMSET:
700 case ISD::MEMCPY:
701 case ISD::MEMMOVE: {
702 Tmp1 = LegalizeOp(Node->getOperand(0));
703 Tmp2 = LegalizeOp(Node->getOperand(1));
704 Tmp3 = LegalizeOp(Node->getOperand(2));
705 SDOperand Tmp4 = LegalizeOp(Node->getOperand(3));
706 SDOperand Tmp5 = LegalizeOp(Node->getOperand(4));
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000707
708 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
709 default: assert(0 && "This action not implemented for this operation!");
710 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +0000711 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
712 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
713 Tmp5 != Node->getOperand(4)) {
714 std::vector<SDOperand> Ops;
715 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
716 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
717 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
718 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000719 break;
720 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +0000721 // Otherwise, the target does not support this operation. Lower the
722 // operation to an explicit libcall as appropriate.
723 MVT::ValueType IntPtr = TLI.getPointerTy();
724 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
725 std::vector<std::pair<SDOperand, const Type*> > Args;
726
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000727 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000728 if (Node->getOpcode() == ISD::MEMSET) {
729 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
730 // Extend the ubyte argument to be an int value for the call.
731 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
732 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
733 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
734
735 FnName = "memset";
736 } else if (Node->getOpcode() == ISD::MEMCPY ||
737 Node->getOpcode() == ISD::MEMMOVE) {
738 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
739 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
740 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
741 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
742 } else {
743 assert(0 && "Unknown op!");
744 }
745 std::pair<SDOperand,SDOperand> CallResult =
746 TLI.LowerCallTo(Tmp1, Type::VoidTy,
747 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
748 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000749 break;
750 }
751 case TargetLowering::Custom:
752 std::vector<SDOperand> Ops;
753 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
754 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
755 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
756 Result = TLI.LowerOperation(Result);
757 Result = LegalizeOp(Result);
758 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000759 }
760 break;
761 }
Chris Lattner84f67882005-01-20 18:52:28 +0000762 case ISD::ADD_PARTS:
763 case ISD::SUB_PARTS: {
764 std::vector<SDOperand> Ops;
765 bool Changed = false;
766 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
767 Ops.push_back(LegalizeOp(Node->getOperand(i)));
768 Changed |= Ops.back() != Node->getOperand(i);
769 }
770 if (Changed)
771 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
772 break;
773 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000774 case ISD::ADD:
775 case ISD::SUB:
776 case ISD::MUL:
777 case ISD::UDIV:
778 case ISD::SDIV:
779 case ISD::UREM:
780 case ISD::SREM:
781 case ISD::AND:
782 case ISD::OR:
783 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000784 case ISD::SHL:
785 case ISD::SRL:
786 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000787 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
788 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
789 if (Tmp1 != Node->getOperand(0) ||
790 Tmp2 != Node->getOperand(1))
791 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
792 break;
793 case ISD::ZERO_EXTEND:
794 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +0000795 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000796 case ISD::FP_EXTEND:
797 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +0000798 case ISD::FP_TO_SINT:
799 case ISD::FP_TO_UINT:
800 case ISD::SINT_TO_FP:
801 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000802 switch (getTypeAction(Node->getOperand(0).getValueType())) {
803 case Legal:
804 Tmp1 = LegalizeOp(Node->getOperand(0));
805 if (Tmp1 != Node->getOperand(0))
806 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
807 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +0000808 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +0000809 if (Node->getOpcode() == ISD::SINT_TO_FP ||
810 Node->getOpcode() == ISD::UINT_TO_FP) {
811 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
812 Node->getValueType(0), Node->getOperand(0));
813 Result = LegalizeOp(Result);
814 break;
815 }
Chris Lattnerb00a6422005-01-07 22:37:48 +0000816 // In the expand case, we must be dealing with a truncate, because
817 // otherwise the result would be larger than the source.
818 assert(Node->getOpcode() == ISD::TRUNCATE &&
819 "Shouldn't need to expand other operators here!");
820 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
821
822 // Since the result is legal, we should just be able to truncate the low
823 // part of the source.
824 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
825 break;
826
Chris Lattner03c85462005-01-15 05:21:40 +0000827 case Promote:
828 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +0000829 case ISD::ZERO_EXTEND:
830 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +0000831 // NOTE: Any extend would work here...
832 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
833 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Op.getValueType(),
Chris Lattner1713e732005-01-16 00:38:00 +0000834 Result, Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +0000835 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000836 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +0000837 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +0000838 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +0000839 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +0000840 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
841 Result, Node->getOperand(0).getValueType());
842 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000843 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +0000844 Result = PromoteOp(Node->getOperand(0));
845 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
846 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000847 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +0000848 Result = PromoteOp(Node->getOperand(0));
849 if (Result.getValueType() != Op.getValueType())
850 // Dynamically dead while we have only 2 FP types.
851 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
852 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000853 case ISD::FP_ROUND:
854 case ISD::FP_TO_SINT:
855 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +0000856 Result = PromoteOp(Node->getOperand(0));
857 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
858 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000859 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +0000860 Result = PromoteOp(Node->getOperand(0));
861 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
862 Result, Node->getOperand(0).getValueType());
863 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
864 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000865 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +0000866 Result = PromoteOp(Node->getOperand(0));
867 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
868 Result, Node->getOperand(0).getValueType());
869 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
870 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000871 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000872 }
873 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000874 case ISD::FP_ROUND_INREG:
875 case ISD::SIGN_EXTEND_INREG:
Chris Lattner45b8caf2005-01-15 07:15:18 +0000876 case ISD::ZERO_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000877 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +0000878 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
879
880 // If this operation is not supported, convert it to a shl/shr or load/store
881 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000882 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
883 default: assert(0 && "This action not supported for this op yet!");
884 case TargetLowering::Legal:
885 if (Tmp1 != Node->getOperand(0))
886 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
887 ExtraVT);
888 break;
889 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +0000890 // If this is an integer extend and shifts are supported, do that.
891 if (Node->getOpcode() == ISD::ZERO_EXTEND_INREG) {
892 // NOTE: we could fall back on load/store here too for targets without
893 // AND. However, it is doubtful that any exist.
894 // AND out the appropriate bits.
895 SDOperand Mask =
896 DAG.getConstant((1ULL << MVT::getSizeInBits(ExtraVT))-1,
897 Node->getValueType(0));
898 Result = DAG.getNode(ISD::AND, Node->getValueType(0),
899 Node->getOperand(0), Mask);
900 } else if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
901 // NOTE: we could fall back on load/store here too for targets without
902 // SAR. However, it is doubtful that any exist.
903 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
904 MVT::getSizeInBits(ExtraVT);
905 SDOperand ShiftCst = DAG.getConstant(BitsDiff, MVT::i8);
906 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
907 Node->getOperand(0), ShiftCst);
908 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
909 Result, ShiftCst);
910 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
911 // The only way we can lower this is to turn it into a STORETRUNC,
912 // EXTLOAD pair, targetting a temporary location (a stack slot).
913
914 // NOTE: there is a choice here between constantly creating new stack
915 // slots and always reusing the same one. We currently always create
916 // new ones, as reuse may inhibit scheduling.
917 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
918 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
919 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
920 MachineFunction &MF = DAG.getMachineFunction();
921 int SSFI =
922 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
923 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
924 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
925 Node->getOperand(0), StackSlot, ExtraVT);
926 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
927 Result, StackSlot, ExtraVT);
928 } else {
929 assert(0 && "Unknown op");
930 }
931 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000932 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +0000933 }
Chris Lattner0f69b292005-01-15 06:18:18 +0000934 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000935 }
Chris Lattner45b8caf2005-01-15 07:15:18 +0000936 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000937
Chris Lattner8afc48e2005-01-07 22:28:47 +0000938 if (!Op.Val->hasOneUse())
939 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000940
941 return Result;
942}
943
Chris Lattner8b6fa222005-01-15 22:16:26 +0000944/// PromoteOp - Given an operation that produces a value in an invalid type,
945/// promote it to compute the value into a larger type. The produced value will
946/// have the correct bits for the low portion of the register, but no guarantee
947/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +0000948SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
949 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000950 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +0000951 assert(getTypeAction(VT) == Promote &&
952 "Caller should expand or legalize operands that are not promotable!");
953 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
954 "Cannot promote to smaller type!");
955
956 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
957 if (I != PromotedNodes.end()) return I->second;
958
959 SDOperand Tmp1, Tmp2, Tmp3;
960
961 SDOperand Result;
962 SDNode *Node = Op.Val;
963
Chris Lattner0f69b292005-01-15 06:18:18 +0000964 // Promotion needs an optimization step to clean up after it, and is not
965 // careful to avoid operations the target does not support. Make sure that
966 // all generated operations are legalized in the next iteration.
967 NeedsAnotherIteration = true;
968
Chris Lattner03c85462005-01-15 05:21:40 +0000969 switch (Node->getOpcode()) {
970 default:
971 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
972 assert(0 && "Do not know how to promote this operator!");
973 abort();
974 case ISD::Constant:
975 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
976 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
977 break;
978 case ISD::ConstantFP:
979 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
980 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
981 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000982 case ISD::CopyFromReg:
983 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
984 Node->getOperand(0));
985 // Remember that we legalized the chain.
986 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
987 break;
988
Chris Lattner82fbfb62005-01-18 02:59:52 +0000989 case ISD::SETCC:
990 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
991 "SetCC type is not legal??");
992 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
993 TLI.getSetCCResultTy(), Node->getOperand(0),
994 Node->getOperand(1));
995 Result = LegalizeOp(Result);
996 break;
Chris Lattner03c85462005-01-15 05:21:40 +0000997
998 case ISD::TRUNCATE:
999 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1000 case Legal:
1001 Result = LegalizeOp(Node->getOperand(0));
1002 assert(Result.getValueType() >= NVT &&
1003 "This truncation doesn't make sense!");
1004 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1005 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1006 break;
1007 case Expand:
1008 assert(0 && "Cannot handle expand yet");
1009 case Promote:
1010 assert(0 && "Cannot handle promote-promote yet");
1011 }
1012 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001013 case ISD::SIGN_EXTEND:
1014 case ISD::ZERO_EXTEND:
1015 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1016 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1017 case Legal:
1018 // Input is legal? Just do extend all the way to the larger type.
1019 Result = LegalizeOp(Node->getOperand(0));
1020 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1021 break;
1022 case Promote:
1023 // Promote the reg if it's smaller.
1024 Result = PromoteOp(Node->getOperand(0));
1025 // The high bits are not guaranteed to be anything. Insert an extend.
1026 if (Node->getOpcode() == ISD::SIGN_EXTEND)
1027 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result, VT);
1028 else
1029 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Result, VT);
1030 break;
1031 }
1032 break;
1033
1034 case ISD::FP_EXTEND:
1035 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1036 case ISD::FP_ROUND:
1037 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1038 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1039 case Promote: assert(0 && "Unreachable with 2 FP types!");
1040 case Legal:
1041 // Input is legal? Do an FP_ROUND_INREG.
1042 Result = LegalizeOp(Node->getOperand(0));
1043 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1044 break;
1045 }
1046 break;
1047
1048 case ISD::SINT_TO_FP:
1049 case ISD::UINT_TO_FP:
1050 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1051 case Legal:
1052 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001053 // No extra round required here.
1054 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001055 break;
1056
1057 case Promote:
1058 Result = PromoteOp(Node->getOperand(0));
1059 if (Node->getOpcode() == ISD::SINT_TO_FP)
1060 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1061 Result, Node->getOperand(0).getValueType());
1062 else
1063 Result = DAG.getNode(ISD::ZERO_EXTEND_INREG, Result.getValueType(),
1064 Result, Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001065 // No extra round required here.
1066 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001067 break;
1068 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001069 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1070 Node->getOperand(0));
1071 Result = LegalizeOp(Result);
1072
1073 // Round if we cannot tolerate excess precision.
1074 if (NoExcessFPPrecision)
1075 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1076 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001077 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001078 break;
1079
1080 case ISD::FP_TO_SINT:
1081 case ISD::FP_TO_UINT:
1082 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1083 case Legal:
1084 Tmp1 = LegalizeOp(Node->getOperand(0));
1085 break;
1086 case Promote:
1087 // The input result is prerounded, so we don't have to do anything
1088 // special.
1089 Tmp1 = PromoteOp(Node->getOperand(0));
1090 break;
1091 case Expand:
1092 assert(0 && "not implemented");
1093 }
1094 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1095 break;
1096
Chris Lattner03c85462005-01-15 05:21:40 +00001097 case ISD::AND:
1098 case ISD::OR:
1099 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001100 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001101 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001102 case ISD::MUL:
1103 // The input may have strange things in the top bits of the registers, but
1104 // these operations don't care. They may have wierd bits going out, but
1105 // that too is okay if they are integer operations.
1106 Tmp1 = PromoteOp(Node->getOperand(0));
1107 Tmp2 = PromoteOp(Node->getOperand(1));
1108 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1109 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1110
1111 // However, if this is a floating point operation, they will give excess
1112 // precision that we may not be able to tolerate. If we DO allow excess
1113 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001114 // FIXME: Why would we need to round FP ops more than integer ones?
1115 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001116 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1117 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1118 break;
1119
Chris Lattner8b6fa222005-01-15 22:16:26 +00001120 case ISD::SDIV:
1121 case ISD::SREM:
1122 // These operators require that their input be sign extended.
1123 Tmp1 = PromoteOp(Node->getOperand(0));
1124 Tmp2 = PromoteOp(Node->getOperand(1));
1125 if (MVT::isInteger(NVT)) {
1126 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001127 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001128 }
1129 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1130
1131 // Perform FP_ROUND: this is probably overly pessimistic.
1132 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1133 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1134 break;
1135
1136 case ISD::UDIV:
1137 case ISD::UREM:
1138 // These operators require that their input be zero extended.
1139 Tmp1 = PromoteOp(Node->getOperand(0));
1140 Tmp2 = PromoteOp(Node->getOperand(1));
1141 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
1142 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001143 Tmp2 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001144 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1145 break;
1146
1147 case ISD::SHL:
1148 Tmp1 = PromoteOp(Node->getOperand(0));
1149 Tmp2 = LegalizeOp(Node->getOperand(1));
1150 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1151 break;
1152 case ISD::SRA:
1153 // The input value must be properly sign extended.
1154 Tmp1 = PromoteOp(Node->getOperand(0));
1155 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1156 Tmp2 = LegalizeOp(Node->getOperand(1));
1157 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1158 break;
1159 case ISD::SRL:
1160 // The input value must be properly zero extended.
1161 Tmp1 = PromoteOp(Node->getOperand(0));
1162 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND_INREG, NVT, Tmp1, VT);
1163 Tmp2 = LegalizeOp(Node->getOperand(1));
1164 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1165 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001166 case ISD::LOAD:
1167 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1168 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1169 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, VT);
1170
1171 // Remember that we legalized the chain.
1172 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1173 break;
1174 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001175 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1176 case Expand: assert(0 && "It's impossible to expand bools");
1177 case Legal:
1178 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1179 break;
1180 case Promote:
1181 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1182 break;
1183 }
Chris Lattner03c85462005-01-15 05:21:40 +00001184 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1185 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1186 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1187 break;
Chris Lattner8ac532c2005-01-16 19:46:48 +00001188 case ISD::CALL: {
1189 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1190 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1191
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001192 std::vector<SDOperand> Ops;
1193 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1194 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1195
Chris Lattner8ac532c2005-01-16 19:46:48 +00001196 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1197 "Can only promote single result calls");
1198 std::vector<MVT::ValueType> RetTyVTs;
1199 RetTyVTs.reserve(2);
1200 RetTyVTs.push_back(NVT);
1201 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001202 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001203 Result = SDOperand(NC, 0);
1204
1205 // Insert the new chain mapping.
1206 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1207 break;
1208 }
Chris Lattner03c85462005-01-15 05:21:40 +00001209 }
1210
1211 assert(Result.Val && "Didn't set a result!");
1212 AddPromotedOperand(Op, Result);
1213 return Result;
1214}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001215
Chris Lattner84f67882005-01-20 18:52:28 +00001216/// ExpandAddSub - Find a clever way to expand this add operation into
1217/// subcomponents.
1218void SelectionDAGLegalize::ExpandAddSub(bool isAdd, SDOperand LHS,SDOperand RHS,
1219 SDOperand &Lo, SDOperand &Hi) {
1220 // Expand the subcomponents.
1221 SDOperand LHSL, LHSH, RHSL, RHSH;
1222 ExpandOp(LHS, LHSL, LHSH);
1223 ExpandOp(RHS, RHSL, RHSH);
1224
1225 // Convert this add to the appropriate ADDC pair. The low part has no carry
1226 // in.
1227 unsigned Opc = isAdd ? ISD::ADD_PARTS : ISD::SUB_PARTS;
1228 std::vector<SDOperand> Ops;
1229 Ops.push_back(LHSL);
1230 Ops.push_back(LHSH);
1231 Ops.push_back(RHSL);
1232 Ops.push_back(RHSH);
1233 Lo = DAG.getNode(Opc, LHSL.getValueType(), Ops);
1234 Hi = Lo.getValue(1);
1235}
1236
Chris Lattnere34b3962005-01-19 04:19:40 +00001237/// ExpandShift - Try to find a clever way to expand this shift operation out to
1238/// smaller elements. If we can't find a way that is more efficient than a
1239/// libcall on this target, return false. Otherwise, return true with the
1240/// low-parts expanded into Lo and Hi.
1241bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1242 SDOperand &Lo, SDOperand &Hi) {
1243 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1244 "This is not a shift!");
1245 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
1246
1247 // If we have an efficient select operation (or if the selects will all fold
1248 // away), lower to some complex code, otherwise just emit the libcall.
1249 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1250 !isa<ConstantSDNode>(Amt))
1251 return false;
1252
1253 SDOperand InL, InH;
1254 ExpandOp(Op, InL, InH);
1255 SDOperand ShAmt = LegalizeOp(Amt);
Chris Lattnere34b3962005-01-19 04:19:40 +00001256 MVT::ValueType ShTy = ShAmt.getValueType();
1257
1258 unsigned NVTBits = MVT::getSizeInBits(NVT);
1259 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1260 DAG.getConstant(NVTBits, ShTy), ShAmt);
1261
Chris Lattnere5544f82005-01-20 20:29:23 +00001262 // Compare the unmasked shift amount against 32.
1263 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1264 DAG.getConstant(NVTBits, ShTy));
1265
Chris Lattnere34b3962005-01-19 04:19:40 +00001266 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1267 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1268 DAG.getConstant(NVTBits-1, ShTy));
1269 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1270 DAG.getConstant(NVTBits-1, ShTy));
1271 }
1272
1273 if (Opc == ISD::SHL) {
1274 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1275 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1276 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001277 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001278
Chris Lattnere34b3962005-01-19 04:19:40 +00001279 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1280 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1281 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001282 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1283 DAG.getSetCC(ISD::SETEQ,
1284 TLI.getSetCCResultTy(), NAmt,
1285 DAG.getConstant(32, ShTy)),
1286 DAG.getConstant(0, NVT),
1287 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001288 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001289 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001290 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001291 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001292
1293 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001294 if (Opc == ISD::SRA)
1295 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1296 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001297 else
1298 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001299 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001300 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001301 }
1302 return true;
1303}
Chris Lattner77e77a62005-01-21 06:05:23 +00001304
1305
1306// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1307// does not fit into a register, return the lo part and set the hi part to the
1308// by-reg argument. If it does fit into a single register, return the result
1309// and leave the Hi part unset.
1310SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1311 SDOperand &Hi) {
1312 TargetLowering::ArgListTy Args;
1313 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1314 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1315 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1316 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1317 }
1318 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
1319
1320 // We don't care about token chains for libcalls. We just use the entry
1321 // node as our input and ignore the output chain. This allows us to place
1322 // calls wherever we need them to satisfy data dependences.
1323 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
1324 SDOperand Result = TLI.LowerCallTo(DAG.getEntryNode(), RetTy, Callee,
1325 Args, DAG).first;
1326 switch (getTypeAction(Result.getValueType())) {
1327 default: assert(0 && "Unknown thing");
1328 case Legal:
1329 return Result;
1330 case Promote:
1331 assert(0 && "Cannot promote this yet!");
1332 case Expand:
1333 SDOperand Lo;
1334 ExpandOp(Result, Lo, Hi);
1335 return Lo;
1336 }
1337}
1338
1339/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1340/// destination type is legal.
1341SDOperand SelectionDAGLegalize::
1342ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1343 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1344 assert(getTypeAction(Source.getValueType()) == Expand &&
1345 "This is not an expansion!");
1346 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1347
1348 const char *FnName;
1349 if (isSigned) {
1350 if (DestTy == MVT::f32)
1351 FnName = "__floatdisf";
1352 else {
1353 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1354 FnName = "__floatdidf";
1355 }
1356 } else {
1357 // If this is unsigned, and not supported, first perform the conversion to
1358 // signed, then adjust the result if the sign bit is set.
1359 SDOperand SignedConv = ExpandIntToFP(false, DestTy, Source);
1360
1361 assert(0 && "Unsigned casts not supported yet!");
1362 }
1363 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1364
1365 TargetLowering::ArgListTy Args;
1366 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1367 Args.push_back(std::make_pair(Source, ArgTy));
1368
1369 // We don't care about token chains for libcalls. We just use the entry
1370 // node as our input and ignore the output chain. This allows us to place
1371 // calls wherever we need them to satisfy data dependences.
1372 const Type *RetTy = MVT::getTypeForValueType(DestTy);
1373 return TLI.LowerCallTo(DAG.getEntryNode(), RetTy, Callee,
1374 Args, DAG).first;
1375}
1376
Chris Lattnere34b3962005-01-19 04:19:40 +00001377
1378
Chris Lattner3e928bb2005-01-07 07:47:09 +00001379/// ExpandOp - Expand the specified SDOperand into its two component pieces
1380/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1381/// LegalizeNodes map is filled in for any results that are not expanded, the
1382/// ExpandedNodes map is filled in for any results that are expanded, and the
1383/// Lo/Hi values are returned.
1384void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1385 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001386 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001387 SDNode *Node = Op.Val;
1388 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1389 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1390 assert(MVT::isInteger(NVT) && NVT < VT &&
1391 "Cannot expand to FP value or to larger int value!");
1392
1393 // If there is more than one use of this, see if we already expanded it.
1394 // There is no use remembering values that only have a single use, as the map
1395 // entries will never be reused.
1396 if (!Node->hasOneUse()) {
1397 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1398 = ExpandedNodes.find(Op);
1399 if (I != ExpandedNodes.end()) {
1400 Lo = I->second.first;
1401 Hi = I->second.second;
1402 return;
1403 }
1404 }
1405
Chris Lattner4e6c7462005-01-08 19:27:05 +00001406 // Expanding to multiple registers needs to perform an optimization step, and
1407 // is not careful to avoid operations the target does not support. Make sure
1408 // that all generated operations are legalized in the next iteration.
1409 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001410
Chris Lattner3e928bb2005-01-07 07:47:09 +00001411 switch (Node->getOpcode()) {
1412 default:
1413 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1414 assert(0 && "Do not know how to expand this operator!");
1415 abort();
1416 case ISD::Constant: {
1417 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
1418 Lo = DAG.getConstant(Cst, NVT);
1419 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
1420 break;
1421 }
1422
1423 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00001424 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00001425 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00001426 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
1427 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
1428
1429 // Remember that we legalized the chain.
1430 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
1431
Chris Lattner3e928bb2005-01-07 07:47:09 +00001432 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
1433 break;
1434 }
1435
1436 case ISD::LOAD: {
1437 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1438 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
1439 Lo = DAG.getLoad(NVT, Ch, Ptr);
1440
1441 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00001442 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001443 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
1444 getIntPtrConstant(IncrementSize));
Chris Lattnerec39a452005-01-19 18:02:17 +00001445 Hi = DAG.getLoad(NVT, Ch, Ptr);
1446
1447 // Build a factor node to remember that this load is independent of the
1448 // other one.
1449 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
1450 Hi.getValue(1));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001451
1452 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00001453 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001454 if (!TLI.isLittleEndian())
1455 std::swap(Lo, Hi);
1456 break;
1457 }
1458 case ISD::CALL: {
1459 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1460 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1461
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001462 bool Changed = false;
1463 std::vector<SDOperand> Ops;
1464 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
1465 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1466 Changed |= Ops.back() != Node->getOperand(i);
1467 }
1468
Chris Lattner3e928bb2005-01-07 07:47:09 +00001469 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1470 "Can only expand a call once so far, not i64 -> i16!");
1471
1472 std::vector<MVT::ValueType> RetTyVTs;
1473 RetTyVTs.reserve(3);
1474 RetTyVTs.push_back(NVT);
1475 RetTyVTs.push_back(NVT);
1476 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001477 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001478 Lo = SDOperand(NC, 0);
1479 Hi = SDOperand(NC, 1);
1480
1481 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00001482 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001483 break;
1484 }
1485 case ISD::AND:
1486 case ISD::OR:
1487 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
1488 SDOperand LL, LH, RL, RH;
1489 ExpandOp(Node->getOperand(0), LL, LH);
1490 ExpandOp(Node->getOperand(1), RL, RH);
1491 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
1492 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
1493 break;
1494 }
1495 case ISD::SELECT: {
1496 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00001497
1498 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1499 case Expand: assert(0 && "It's impossible to expand bools");
1500 case Legal:
1501 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1502 break;
1503 case Promote:
1504 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
1505 break;
1506 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001507 ExpandOp(Node->getOperand(1), LL, LH);
1508 ExpandOp(Node->getOperand(2), RL, RH);
1509 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
1510 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
1511 break;
1512 }
1513 case ISD::SIGN_EXTEND: {
1514 // The low part is just a sign extension of the input (which degenerates to
1515 // a copy).
1516 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1517
1518 // The high part is obtained by SRA'ing all but one of the bits of the lo
1519 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00001520 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
1521 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1, MVT::i8));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001522 break;
1523 }
1524 case ISD::ZERO_EXTEND:
1525 // The low part is just a zero extension of the input (which degenerates to
1526 // a copy).
1527 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, LegalizeOp(Node->getOperand(0)));
1528
1529 // The high part is just a zero.
1530 Hi = DAG.getConstant(0, NVT);
1531 break;
Chris Lattner4e6c7462005-01-08 19:27:05 +00001532
1533 // These operators cannot be expanded directly, emit them as calls to
1534 // library functions.
1535 case ISD::FP_TO_SINT:
1536 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00001537 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00001538 else
Chris Lattner77e77a62005-01-21 06:05:23 +00001539 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00001540 break;
1541 case ISD::FP_TO_UINT:
1542 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00001543 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00001544 else
Chris Lattner77e77a62005-01-21 06:05:23 +00001545 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00001546 break;
1547
Chris Lattnere34b3962005-01-19 04:19:40 +00001548 case ISD::SHL:
1549 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00001550 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00001551 break;
1552 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00001553 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00001554 break;
1555
1556 case ISD::SRA:
1557 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00001558 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00001559 break;
1560 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00001561 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00001562 break;
1563 case ISD::SRL:
1564 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00001565 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00001566 break;
1567 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00001568 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00001569 break;
1570
Chris Lattner84f67882005-01-20 18:52:28 +00001571 case ISD::ADD:
1572 ExpandAddSub(true, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1573 break;
1574 case ISD::SUB:
1575 ExpandAddSub(false, Node->getOperand(0), Node->getOperand(1), Lo, Hi);
1576 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001577 case ISD::MUL: Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
1578 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
1579 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
1580 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
1581 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001582 }
1583
1584 // Remember in a map if the values will be reused later.
1585 if (!Node->hasOneUse()) {
1586 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
1587 std::make_pair(Lo, Hi))).second;
1588 assert(isNew && "Value already expanded?!?");
1589 }
1590}
1591
1592
1593// SelectionDAG::Legalize - This is the entry point for the file.
1594//
1595void SelectionDAG::Legalize(TargetLowering &TLI) {
1596 /// run - This is the main entry point to this class.
1597 ///
1598 SelectionDAGLegalize(TLI, *this).Run();
1599}
1600