blob: adb88bf9b1874fb6fb3221cab59a7de6937671a6 [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// 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.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
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 Lattner4157c412005-04-02 04:00:59 +0000128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000131 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000132
Chris Lattnerdc750592005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
139
Chris Lattner4add7e32005-01-23 04:42:50 +0000140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000145}
146
Chris Lattnerdc750592005-01-07 07:47:09 +0000147void SelectionDAGLegalize::LegalizeDAG() {
148 SDOperand OldRoot = DAG.getRoot();
149 SDOperand NewRoot = LegalizeOp(OldRoot);
150 DAG.setRoot(NewRoot);
151
152 ExpandedNodes.clear();
153 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000154 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000155
156 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000157 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000158}
159
160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
163
Chris Lattnerdc750592005-01-07 07:47:09 +0000164 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
168 switch (getTypeAction(Op.Val->getValueType(i))) {
169 case Legal: break; // Nothing to do.
170 case Expand: {
171 SDOperand T1, T2;
172 ExpandOp(Op.getValue(i), T1, T2);
173 assert(LegalizedNodes.count(Op) &&
174 "Expansion didn't add legal operands!");
175 return LegalizedNodes[Op];
176 }
177 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000178 PromoteOp(Op.getValue(i));
179 assert(LegalizedNodes.count(Op) &&
180 "Expansion didn't add legal operands!");
181 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000182 }
183 }
184
Chris Lattner85d70c62005-01-11 05:57:22 +0000185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000187
Chris Lattnerec26b482005-01-09 19:03:49 +0000188 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000189
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
Chris Lattnerdc750592005-01-07 07:47:09 +0000192
193 switch (Node->getOpcode()) {
194 default:
195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
196 assert(0 && "Do not know how to legalize this operator!");
197 abort();
198 case ISD::EntryToken:
199 case ISD::FrameIndex:
200 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000201 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000202 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
205 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000206 case ISD::CopyFromReg:
207 Tmp1 = LegalizeOp(Node->getOperand(0));
208 if (Tmp1 != Node->getOperand(0))
209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
210 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000211 else
212 Result = Op.getValue(0);
213
214 // Since CopyFromReg produces two values, make sure to remember that we
215 // legalized both of them.
216 AddLegalizedOperand(Op.getValue(0), Result);
217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
218 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000223 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000224 case ISD::UNDEF: {
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000230 if (MVT::isInteger(VT))
231 Result = DAG.getConstant(0, VT);
232 else if (MVT::isFloatingPoint(VT))
233 Result = DAG.getConstantFP(0, VT);
234 else
235 assert(0 && "Unknown value type!");
236 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000237 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000238 break;
239 }
240 break;
241 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000242 case ISD::Constant:
243 // We know we don't need to expand constants here, constants only have one
244 // value and we check that it is fine above.
245
246 // FIXME: Maybe we should handle things like targets that don't support full
247 // 32-bit immediates?
248 break;
249 case ISD::ConstantFP: {
250 // Spill FP immediates to the constant pool if the target cannot directly
251 // codegen them. Targets often have some immediate values that can be
252 // efficiently generated into an FP register without a load. We explicitly
253 // leave these constants as ConstantFP nodes for the target to deal with.
254
255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
256
257 // Check to see if this FP immediate is already legal.
258 bool isLegal = false;
259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
260 E = TLI.legal_fpimm_end(); I != E; ++I)
261 if (CFP->isExactlyValue(*I)) {
262 isLegal = true;
263 break;
264 }
265
266 if (!isLegal) {
267 // Otherwise we need to spill the constant to memory.
268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
269
270 bool Extend = false;
271
272 // If a FP immediate is precise when represented as a float, we put it
273 // into the constant pool as a float, even if it's is statically typed
274 // as a double.
275 MVT::ValueType VT = CFP->getValueType(0);
276 bool isDouble = VT == MVT::f64;
277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
278 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
280 // Only do this if the target has a native EXTLOAD instruction from
281 // f32.
282 TLI.getOperationAction(ISD::EXTLOAD,
283 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
285 VT = MVT::f32;
286 Extend = true;
287 }
Misha Brukman835702a2005-04-21 22:36:52 +0000288
Chris Lattnerdc750592005-01-07 07:47:09 +0000289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
290 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000291 if (Extend) {
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000293 DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000294 } else {
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000296 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000297 }
298 break;
299 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000300 case ISD::TokenFactor: {
301 std::vector<SDOperand> Ops;
302 bool Changed = false;
303 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000304 SDOperand Op = Node->getOperand(i);
305 // Fold single-use TokenFactor nodes into this token factor as we go.
306 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
307 Changed = true;
308 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
309 Ops.push_back(LegalizeOp(Op.getOperand(j)));
310 } else {
311 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
312 Changed |= Ops[i] != Op;
313 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000314 }
315 if (Changed)
316 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
317 break;
318 }
319
Chris Lattnerdc750592005-01-07 07:47:09 +0000320 case ISD::ADJCALLSTACKDOWN:
321 case ISD::ADJCALLSTACKUP:
322 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
323 // There is no need to legalize the size argument (Operand #1)
324 if (Tmp1 != Node->getOperand(0))
325 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
326 Node->getOperand(1));
327 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000328 case ISD::DYNAMIC_STACKALLOC:
329 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
330 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
331 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
332 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
333 Tmp3 != Node->getOperand(2))
334 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
335 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000336 else
337 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000338
339 // Since this op produces two values, make sure to remember that we
340 // legalized both of them.
341 AddLegalizedOperand(SDOperand(Node, 0), Result);
342 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
343 return Result.getValue(Op.ResNo);
344
Chris Lattner3d95c142005-01-19 20:24:35 +0000345 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000346 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
347 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000348
349 bool Changed = false;
350 std::vector<SDOperand> Ops;
351 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
352 Ops.push_back(LegalizeOp(Node->getOperand(i)));
353 Changed |= Ops.back() != Node->getOperand(i);
354 }
355
356 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000357 std::vector<MVT::ValueType> RetTyVTs;
358 RetTyVTs.reserve(Node->getNumValues());
359 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000360 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d95c142005-01-19 20:24:35 +0000361 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000362 } else {
363 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000364 }
Chris Lattner9242c502005-01-09 19:43:23 +0000365 // Since calls produce multiple values, make sure to remember that we
366 // legalized all of them.
367 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
368 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
369 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000370 }
Chris Lattner68a12142005-01-07 22:12:08 +0000371 case ISD::BR:
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 if (Tmp1 != Node->getOperand(0))
374 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
375 break;
376
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000377 case ISD::BRCOND:
378 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000379
380 switch (getTypeAction(Node->getOperand(1).getValueType())) {
381 case Expand: assert(0 && "It's impossible to expand bools");
382 case Legal:
383 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
384 break;
385 case Promote:
386 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
387 break;
388 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000389 // Basic block destination (Op#2) is always legal.
390 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
391 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
392 Node->getOperand(2));
393 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000394 case ISD::BRCONDTWOWAY:
395 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
396 switch (getTypeAction(Node->getOperand(1).getValueType())) {
397 case Expand: assert(0 && "It's impossible to expand bools");
398 case Legal:
399 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
400 break;
401 case Promote:
402 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
403 break;
404 }
405 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
406 // pair.
407 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
408 case TargetLowering::Promote:
409 default: assert(0 && "This action is not supported yet!");
410 case TargetLowering::Legal:
411 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
412 std::vector<SDOperand> Ops;
413 Ops.push_back(Tmp1);
414 Ops.push_back(Tmp2);
415 Ops.push_back(Node->getOperand(2));
416 Ops.push_back(Node->getOperand(3));
417 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
418 }
419 break;
420 case TargetLowering::Expand:
421 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
422 Node->getOperand(2));
423 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
424 break;
425 }
426 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000427
Chris Lattnerdc750592005-01-07 07:47:09 +0000428 case ISD::LOAD:
429 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000431
Chris Lattnerdc750592005-01-07 07:47:09 +0000432 if (Tmp1 != Node->getOperand(0) ||
433 Tmp2 != Node->getOperand(1))
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000434 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2, Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000435 else
436 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000437
Chris Lattnerea4ca942005-01-07 22:28:47 +0000438 // Since loads produce two values, make sure to remember that we legalized
439 // both of them.
440 AddLegalizedOperand(SDOperand(Node, 0), Result);
441 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
442 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000443
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000444 case ISD::EXTLOAD:
445 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000446 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000447 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
448 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000449
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000450 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
451 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000452 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000453 case TargetLowering::Promote:
454 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
455 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000456 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000457 // Since loads produce two values, make sure to remember that we legalized
458 // both of them.
459 AddLegalizedOperand(SDOperand(Node, 0), Result);
460 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
461 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000462
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000463 case TargetLowering::Legal:
464 if (Tmp1 != Node->getOperand(0) ||
465 Tmp2 != Node->getOperand(1))
466 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000467 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000468 else
469 Result = SDOperand(Node, 0);
470
471 // Since loads produce two values, make sure to remember that we legalized
472 // both of them.
473 AddLegalizedOperand(SDOperand(Node, 0), Result);
474 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
475 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000476 case TargetLowering::Expand:
477 assert(Node->getOpcode() != ISD::EXTLOAD &&
478 "EXTLOAD should always be supported!");
479 // Turn the unsupported load into an EXTLOAD followed by an explicit
480 // zero/sign extend inreg.
481 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000482 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000483 SDOperand ValRes;
484 if (Node->getOpcode() == ISD::SEXTLOAD)
485 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
486 Result, SrcVT);
487 else
488 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000489 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
490 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
491 if (Op.ResNo)
492 return Result.getValue(1);
493 return ValRes;
494 }
495 assert(0 && "Unreachable");
496 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000497 case ISD::EXTRACT_ELEMENT:
498 // Get both the low and high parts.
499 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
500 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
501 Result = Tmp2; // 1 -> Hi
502 else
503 Result = Tmp1; // 0 -> Lo
504 break;
505
506 case ISD::CopyToReg:
507 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000508
Chris Lattnerdc750592005-01-07 07:47:09 +0000509 switch (getTypeAction(Node->getOperand(1).getValueType())) {
510 case Legal:
511 // Legalize the incoming value (must be legal).
512 Tmp2 = LegalizeOp(Node->getOperand(1));
513 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000514 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000515 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000516 case Promote:
517 Tmp2 = PromoteOp(Node->getOperand(1));
518 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
519 break;
520 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000521 SDOperand Lo, Hi;
Misha Brukman835702a2005-04-21 22:36:52 +0000522 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000523 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000524 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
525 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
526 // Note that the copytoreg nodes are independent of each other.
527 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000528 assert(isTypeLegal(Result.getValueType()) &&
529 "Cannot expand multiple times yet (i64 -> i16)");
530 break;
531 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000532 break;
533
534 case ISD::RET:
535 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
536 switch (Node->getNumOperands()) {
537 case 2: // ret val
538 switch (getTypeAction(Node->getOperand(1).getValueType())) {
539 case Legal:
540 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000541 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000542 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
543 break;
544 case Expand: {
545 SDOperand Lo, Hi;
546 ExpandOp(Node->getOperand(1), Lo, Hi);
547 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000548 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000549 }
550 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000551 Tmp2 = PromoteOp(Node->getOperand(1));
552 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
553 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000554 }
555 break;
556 case 1: // ret void
557 if (Tmp1 != Node->getOperand(0))
558 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
559 break;
560 default: { // ret <values>
561 std::vector<SDOperand> NewValues;
562 NewValues.push_back(Tmp1);
563 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
564 switch (getTypeAction(Node->getOperand(i).getValueType())) {
565 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000566 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000567 break;
568 case Expand: {
569 SDOperand Lo, Hi;
570 ExpandOp(Node->getOperand(i), Lo, Hi);
571 NewValues.push_back(Lo);
572 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000573 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000574 }
575 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000576 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000577 }
578 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
579 break;
580 }
581 }
582 break;
583 case ISD::STORE:
584 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
585 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
586
Chris Lattnere69daaf2005-01-08 06:25:56 +0000587 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000588 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000589 if (CFP->getValueType(0) == MVT::f32) {
590 union {
591 unsigned I;
592 float F;
593 } V;
594 V.F = CFP->getValue();
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000595 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
596 DAG.getConstant(V.I, MVT::i32), Tmp2, Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000597 } else {
598 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
599 union {
600 uint64_t I;
601 double F;
602 } V;
603 V.F = CFP->getValue();
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000604 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
605 DAG.getConstant(V.I, MVT::i64), Tmp2, Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000606 }
Chris Lattnera4743132005-02-22 07:23:39 +0000607 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000608 }
609
Chris Lattnerdc750592005-01-07 07:47:09 +0000610 switch (getTypeAction(Node->getOperand(1).getValueType())) {
611 case Legal: {
612 SDOperand Val = LegalizeOp(Node->getOperand(1));
613 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
614 Tmp2 != Node->getOperand(2))
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000615 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2, Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000616 break;
617 }
618 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000619 // Truncate the value and store the result.
620 Tmp3 = PromoteOp(Node->getOperand(1));
621 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000622 Node->getOperand(3),
623 Node->getOperand(1).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000624 break;
625
Chris Lattnerdc750592005-01-07 07:47:09 +0000626 case Expand:
627 SDOperand Lo, Hi;
628 ExpandOp(Node->getOperand(1), Lo, Hi);
629
630 if (!TLI.isLittleEndian())
631 std::swap(Lo, Hi);
632
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000633 Lo = DAG.getNode(ISD::STORE, MVT::Other,Tmp1, Lo, Tmp2,Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000634
Chris Lattner0d03eb42005-01-19 18:02:17 +0000635 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000636 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
637 getIntPtrConstant(IncrementSize));
638 assert(isTypeLegal(Tmp2.getValueType()) &&
639 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000640 //Again, claiming both parts of the store came form the same Instr
641 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2, Node->getOperand(3));
642
Chris Lattner0d03eb42005-01-19 18:02:17 +0000643 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
644 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000645 }
646 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000647 case ISD::PCMARKER:
648 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000649 if (Tmp1 != Node->getOperand(0))
650 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000651 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000652 case ISD::TRUNCSTORE:
653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
654 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
655
656 switch (getTypeAction(Node->getOperand(1).getValueType())) {
657 case Legal:
658 Tmp2 = LegalizeOp(Node->getOperand(1));
659 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
660 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000661 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000662 Node->getOperand(3),
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000663 cast<MVTSDNode>(Node)->getExtraValueType());
664 break;
665 case Promote:
666 case Expand:
667 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
668 }
669 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000670 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000671 switch (getTypeAction(Node->getOperand(0).getValueType())) {
672 case Expand: assert(0 && "It's impossible to expand bools");
673 case Legal:
674 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
675 break;
676 case Promote:
677 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
678 break;
679 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000680 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000681 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000682
683 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
684 default: assert(0 && "This action is not supported yet!");
685 case TargetLowering::Legal:
686 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
687 Tmp3 != Node->getOperand(2))
688 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
689 Tmp1, Tmp2, Tmp3);
690 break;
691 case TargetLowering::Promote: {
692 MVT::ValueType NVT =
693 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
694 unsigned ExtOp, TruncOp;
695 if (MVT::isInteger(Tmp2.getValueType())) {
696 ExtOp = ISD::ZERO_EXTEND;
697 TruncOp = ISD::TRUNCATE;
698 } else {
699 ExtOp = ISD::FP_EXTEND;
700 TruncOp = ISD::FP_ROUND;
701 }
702 // Promote each of the values to the new type.
703 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
704 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
705 // Perform the larger operation, then round down.
706 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
707 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
708 break;
709 }
710 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000711 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000712 case ISD::SETCC:
713 switch (getTypeAction(Node->getOperand(0).getValueType())) {
714 case Legal:
715 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
716 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
717 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
718 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000719 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000720 break;
721 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000722 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
723 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
724
725 // If this is an FP compare, the operands have already been extended.
726 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
727 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000728 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000729
730 // Otherwise, we have to insert explicit sign or zero extends. Note
731 // that we could insert sign extends for ALL conditions, but zero extend
732 // is cheaper on many machines (an AND instead of two shifts), so prefer
733 // it.
734 switch (cast<SetCCSDNode>(Node)->getCondition()) {
735 default: assert(0 && "Unknown integer comparison!");
736 case ISD::SETEQ:
737 case ISD::SETNE:
738 case ISD::SETUGE:
739 case ISD::SETUGT:
740 case ISD::SETULE:
741 case ISD::SETULT:
742 // ALL of these operations will work if we either sign or zero extend
743 // the operands (including the unsigned comparisons!). Zero extend is
744 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +0000745 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
746 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000747 break;
748 case ISD::SETGE:
749 case ISD::SETGT:
750 case ISD::SETLT:
751 case ISD::SETLE:
752 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
753 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
754 break;
755 }
756
757 }
758 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000759 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000760 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000761 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000762 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
763 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
764 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
765 switch (cast<SetCCSDNode>(Node)->getCondition()) {
766 case ISD::SETEQ:
767 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +0000768 if (RHSLo == RHSHi)
769 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
770 if (RHSCST->isAllOnesValue()) {
771 // Comparison to -1.
772 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukman835702a2005-04-21 22:36:52 +0000773 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner71ff44e2005-04-12 01:46:05 +0000774 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukman835702a2005-04-21 22:36:52 +0000775 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +0000776 }
777
Chris Lattnerdc750592005-01-07 07:47:09 +0000778 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
779 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
780 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +0000781 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000782 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000783 DAG.getConstant(0, Tmp1.getValueType()));
784 break;
785 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +0000786 // If this is a comparison of the sign bit, just look at the top part.
787 // X > -1, x < 0
788 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukman835702a2005-04-21 22:36:52 +0000789 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +0000790 CST->getValue() == 0) || // X < 0
791 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
792 (CST->isAllOnesValue()))) // X > -1
793 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
794 Node->getValueType(0), LHSHi, RHSHi);
795
Chris Lattnerdc750592005-01-07 07:47:09 +0000796 // FIXME: This generated code sucks.
797 ISD::CondCode LowCC;
798 switch (cast<SetCCSDNode>(Node)->getCondition()) {
799 default: assert(0 && "Unknown integer setcc!");
800 case ISD::SETLT:
801 case ISD::SETULT: LowCC = ISD::SETULT; break;
802 case ISD::SETGT:
803 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
804 case ISD::SETLE:
805 case ISD::SETULE: LowCC = ISD::SETULE; break;
806 case ISD::SETGE:
807 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
808 }
Misha Brukman835702a2005-04-21 22:36:52 +0000809
Chris Lattnerdc750592005-01-07 07:47:09 +0000810 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
811 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
812 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
813
814 // NOTE: on targets without efficient SELECT of bools, we can always use
815 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000816 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000817 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000818 Node->getValueType(0), LHSHi, RHSHi);
819 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
820 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
821 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000822 break;
823 }
824 }
825 break;
826
Chris Lattner85d70c62005-01-11 05:57:22 +0000827 case ISD::MEMSET:
828 case ISD::MEMCPY:
829 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000830 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000831 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
832
833 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
834 switch (getTypeAction(Node->getOperand(2).getValueType())) {
835 case Expand: assert(0 && "Cannot expand a byte!");
836 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000837 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000838 break;
839 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000840 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000841 break;
842 }
843 } else {
Misha Brukman835702a2005-04-21 22:36:52 +0000844 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000845 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000846
847 SDOperand Tmp4;
848 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000849 case Expand: assert(0 && "Cannot expand this yet!");
850 case Legal:
851 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000852 break;
853 case Promote:
854 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000855 break;
856 }
857
858 SDOperand Tmp5;
859 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
860 case Expand: assert(0 && "Cannot expand this yet!");
861 case Legal:
862 Tmp5 = LegalizeOp(Node->getOperand(4));
863 break;
864 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000865 Tmp5 = PromoteOp(Node->getOperand(4));
866 break;
867 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000868
869 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
870 default: assert(0 && "This action not implemented for this operation!");
871 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000872 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
873 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
874 Tmp5 != Node->getOperand(4)) {
875 std::vector<SDOperand> Ops;
876 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
877 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
878 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
879 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000880 break;
881 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000882 // Otherwise, the target does not support this operation. Lower the
883 // operation to an explicit libcall as appropriate.
884 MVT::ValueType IntPtr = TLI.getPointerTy();
885 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
886 std::vector<std::pair<SDOperand, const Type*> > Args;
887
Reid Spencer6dced922005-01-12 14:53:45 +0000888 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000889 if (Node->getOpcode() == ISD::MEMSET) {
890 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
891 // Extend the ubyte argument to be an int value for the call.
892 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
893 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
894 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
895
896 FnName = "memset";
897 } else if (Node->getOpcode() == ISD::MEMCPY ||
898 Node->getOpcode() == ISD::MEMMOVE) {
899 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
900 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
901 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
902 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
903 } else {
904 assert(0 && "Unknown op!");
905 }
906 std::pair<SDOperand,SDOperand> CallResult =
Nate Begemanf6565252005-03-26 01:29:23 +0000907 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattner85d70c62005-01-11 05:57:22 +0000908 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
909 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000910 break;
911 }
912 case TargetLowering::Custom:
913 std::vector<SDOperand> Ops;
914 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
915 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
916 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
917 Result = TLI.LowerOperation(Result);
918 Result = LegalizeOp(Result);
919 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000920 }
921 break;
922 }
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000923 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +0000924 case ISD::SUB_PARTS:
925 case ISD::SHL_PARTS:
926 case ISD::SRA_PARTS:
927 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000928 std::vector<SDOperand> Ops;
929 bool Changed = false;
930 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
931 Ops.push_back(LegalizeOp(Node->getOperand(i)));
932 Changed |= Ops.back() != Node->getOperand(i);
933 }
934 if (Changed)
935 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +0000936
937 // Since these produce multiple values, make sure to remember that we
938 // legalized all of them.
939 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
940 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
941 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +0000942 }
Chris Lattner13fe99c2005-04-02 05:00:07 +0000943
944 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +0000945 case ISD::ADD:
946 case ISD::SUB:
947 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +0000948 case ISD::MULHS:
949 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +0000950 case ISD::UDIV:
951 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +0000952 case ISD::AND:
953 case ISD::OR:
954 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000955 case ISD::SHL:
956 case ISD::SRL:
957 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +0000958 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
959 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
960 if (Tmp1 != Node->getOperand(0) ||
961 Tmp2 != Node->getOperand(1))
962 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
963 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000964
Nate Begeman20b7d2a2005-04-06 00:23:54 +0000965 case ISD::UREM:
966 case ISD::SREM:
967 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
968 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
969 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
970 case TargetLowering::Legal:
971 if (Tmp1 != Node->getOperand(0) ||
972 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +0000973 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +0000974 Tmp2);
975 break;
976 case TargetLowering::Promote:
977 case TargetLowering::Custom:
978 assert(0 && "Cannot promote/custom handle this yet!");
979 case TargetLowering::Expand: {
980 MVT::ValueType VT = Node->getValueType(0);
981 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
982 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
983 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
984 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
985 }
986 break;
987 }
988 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +0000989
Andrew Lenharth5e177822005-05-03 17:19:30 +0000990 case ISD::CTPOP:
991 case ISD::CTTZ:
992 case ISD::CTLZ:
993 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
994 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
995 case TargetLowering::Legal:
996 if (Tmp1 != Node->getOperand(0))
997 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
998 break;
999 case TargetLowering::Promote: {
1000 MVT::ValueType OVT = Tmp1.getValueType();
1001 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
1002 //Zero extend the argument
1003 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1004 // Perform the larger operation, then subtract if needed.
1005 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1006 switch(Node->getOpcode())
1007 {
1008 case ISD::CTPOP:
1009 Result = Tmp1;
1010 break;
1011 case ISD::CTTZ:
1012 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1013 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1014 DAG.getConstant(getSizeInBits(NVT), NVT));
1015 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1016 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1017 break;
1018 case ISD::CTLZ:
1019 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1020 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1021 DAG.getConstant(getSizeInBits(NVT) -
1022 getSizeInBits(OVT), NVT));
1023 break;
1024 }
1025 break;
1026 }
1027 case TargetLowering::Custom:
1028 assert(0 && "Cannot custom handle this yet!");
1029 case TargetLowering::Expand:
1030 assert(0 && "Cannot expand this yet!");
1031 break;
1032 }
1033 break;
1034
Chris Lattner13fe99c2005-04-02 05:00:07 +00001035 // Unary operators
1036 case ISD::FABS:
1037 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001038 case ISD::FSQRT:
1039 case ISD::FSIN:
1040 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001041 Tmp1 = LegalizeOp(Node->getOperand(0));
1042 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1043 case TargetLowering::Legal:
1044 if (Tmp1 != Node->getOperand(0))
1045 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1046 break;
1047 case TargetLowering::Promote:
1048 case TargetLowering::Custom:
1049 assert(0 && "Cannot promote/custom handle this yet!");
1050 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001051 switch(Node->getOpcode()) {
1052 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001053 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1054 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1055 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1056 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001057 break;
1058 }
1059 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001060 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1061 MVT::ValueType VT = Node->getValueType(0);
1062 Tmp2 = DAG.getConstantFP(0.0, VT);
1063 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1064 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1065 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1066 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001067 break;
1068 }
1069 case ISD::FSQRT:
1070 case ISD::FSIN:
1071 case ISD::FCOS: {
1072 MVT::ValueType VT = Node->getValueType(0);
1073 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1074 const char *FnName = 0;
1075 switch(Node->getOpcode()) {
1076 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1077 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1078 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1079 default: assert(0 && "Unreachable!");
1080 }
1081 std::vector<std::pair<SDOperand, const Type*> > Args;
1082 Args.push_back(std::make_pair(Tmp1, T));
1083 std::pair<SDOperand,SDOperand> CallResult =
1084 TLI.LowerCallTo(DAG.getEntryNode(), T, false,
1085 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1086 Result = LegalizeOp(CallResult.first);
1087 break;
1088 }
1089 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001090 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001091 }
1092 break;
1093 }
1094 break;
1095
1096 // Conversion operators. The source and destination have different types.
Chris Lattnerdc750592005-01-07 07:47:09 +00001097 case ISD::ZERO_EXTEND:
1098 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +00001099 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001100 case ISD::FP_EXTEND:
1101 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001102 case ISD::FP_TO_SINT:
1103 case ISD::FP_TO_UINT:
1104 case ISD::SINT_TO_FP:
1105 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +00001106 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1107 case Legal:
1108 Tmp1 = LegalizeOp(Node->getOperand(0));
1109 if (Tmp1 != Node->getOperand(0))
1110 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1111 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001112 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001113 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1114 Node->getOpcode() == ISD::UINT_TO_FP) {
1115 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1116 Node->getValueType(0), Node->getOperand(0));
1117 Result = LegalizeOp(Result);
1118 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001119 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1120 // In the expand case, we must be dealing with a truncate, because
1121 // otherwise the result would be larger than the source.
1122 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +00001123
Chris Lattner13fe99c2005-04-02 05:00:07 +00001124 // Since the result is legal, we should just be able to truncate the low
1125 // part of the source.
1126 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1127 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001128 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001129 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001130
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001131 case Promote:
1132 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001133 case ISD::ZERO_EXTEND:
1134 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001135 // NOTE: Any extend would work here...
1136 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001137 Result = DAG.getZeroExtendInReg(Result,
1138 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001139 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001140 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001141 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001142 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001143 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001144 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1145 Result, Node->getOperand(0).getValueType());
1146 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001147 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001148 Result = PromoteOp(Node->getOperand(0));
1149 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1150 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001151 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001152 Result = PromoteOp(Node->getOperand(0));
1153 if (Result.getValueType() != Op.getValueType())
1154 // Dynamically dead while we have only 2 FP types.
1155 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1156 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001157 case ISD::FP_ROUND:
1158 case ISD::FP_TO_SINT:
1159 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001160 Result = PromoteOp(Node->getOperand(0));
1161 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1162 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001163 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001164 Result = PromoteOp(Node->getOperand(0));
1165 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1166 Result, Node->getOperand(0).getValueType());
1167 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1168 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001169 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001170 Result = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001171 Result = DAG.getZeroExtendInReg(Result,
1172 Node->getOperand(0).getValueType());
Chris Lattner3ba56b32005-01-16 05:06:12 +00001173 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1174 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001175 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001176 }
1177 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001178 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001179 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001180 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +00001181 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1182
1183 // If this operation is not supported, convert it to a shl/shr or load/store
1184 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001185 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1186 default: assert(0 && "This action not supported for this op yet!");
1187 case TargetLowering::Legal:
1188 if (Tmp1 != Node->getOperand(0))
1189 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1190 ExtraVT);
1191 break;
1192 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001193 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001194 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001195 // NOTE: we could fall back on load/store here too for targets without
1196 // SAR. However, it is doubtful that any exist.
1197 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1198 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001199 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001200 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1201 Node->getOperand(0), ShiftCst);
1202 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1203 Result, ShiftCst);
1204 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1205 // The only way we can lower this is to turn it into a STORETRUNC,
1206 // EXTLOAD pair, targetting a temporary location (a stack slot).
1207
1208 // NOTE: there is a choice here between constantly creating new stack
1209 // slots and always reusing the same one. We currently always create
1210 // new ones, as reuse may inhibit scheduling.
1211 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1212 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1213 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1214 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001215 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001216 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1217 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1218 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001219 Node->getOperand(0), StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001220 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001221 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001222 } else {
1223 assert(0 && "Unknown op");
1224 }
1225 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001226 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001227 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001228 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001229 }
Chris Lattner99222f72005-01-15 07:15:18 +00001230 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001231
Chris Lattnerea4ca942005-01-07 22:28:47 +00001232 if (!Op.Val->hasOneUse())
1233 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001234
1235 return Result;
1236}
1237
Chris Lattner4d978642005-01-15 22:16:26 +00001238/// PromoteOp - Given an operation that produces a value in an invalid type,
1239/// promote it to compute the value into a larger type. The produced value will
1240/// have the correct bits for the low portion of the register, but no guarantee
1241/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001242SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1243 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001244 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001245 assert(getTypeAction(VT) == Promote &&
1246 "Caller should expand or legalize operands that are not promotable!");
1247 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1248 "Cannot promote to smaller type!");
1249
1250 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1251 if (I != PromotedNodes.end()) return I->second;
1252
1253 SDOperand Tmp1, Tmp2, Tmp3;
1254
1255 SDOperand Result;
1256 SDNode *Node = Op.Val;
1257
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001258 // Promotion needs an optimization step to clean up after it, and is not
1259 // careful to avoid operations the target does not support. Make sure that
1260 // all generated operations are legalized in the next iteration.
1261 NeedsAnotherIteration = true;
1262
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001263 switch (Node->getOpcode()) {
1264 default:
1265 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1266 assert(0 && "Do not know how to promote this operator!");
1267 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001268 case ISD::UNDEF:
1269 Result = DAG.getNode(ISD::UNDEF, NVT);
1270 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001271 case ISD::Constant:
1272 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1273 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1274 break;
1275 case ISD::ConstantFP:
1276 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1277 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1278 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001279 case ISD::CopyFromReg:
1280 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1281 Node->getOperand(0));
1282 // Remember that we legalized the chain.
1283 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1284 break;
1285
Chris Lattner2cb338d2005-01-18 02:59:52 +00001286 case ISD::SETCC:
1287 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1288 "SetCC type is not legal??");
1289 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1290 TLI.getSetCCResultTy(), Node->getOperand(0),
1291 Node->getOperand(1));
1292 Result = LegalizeOp(Result);
1293 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001294
1295 case ISD::TRUNCATE:
1296 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1297 case Legal:
1298 Result = LegalizeOp(Node->getOperand(0));
1299 assert(Result.getValueType() >= NVT &&
1300 "This truncation doesn't make sense!");
1301 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1302 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1303 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001304 case Promote:
1305 // The truncation is not required, because we don't guarantee anything
1306 // about high bits anyway.
1307 Result = PromoteOp(Node->getOperand(0));
1308 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001309 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001310 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1311 // Truncate the low part of the expanded value to the result type
Misha Brukman835702a2005-04-21 22:36:52 +00001312 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001313 }
1314 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001315 case ISD::SIGN_EXTEND:
1316 case ISD::ZERO_EXTEND:
1317 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1318 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1319 case Legal:
1320 // Input is legal? Just do extend all the way to the larger type.
1321 Result = LegalizeOp(Node->getOperand(0));
1322 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1323 break;
1324 case Promote:
1325 // Promote the reg if it's smaller.
1326 Result = PromoteOp(Node->getOperand(0));
1327 // The high bits are not guaranteed to be anything. Insert an extend.
1328 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001329 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1330 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001331 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001332 Result = DAG.getZeroExtendInReg(Result,
1333 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001334 break;
1335 }
1336 break;
1337
1338 case ISD::FP_EXTEND:
1339 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1340 case ISD::FP_ROUND:
1341 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1342 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1343 case Promote: assert(0 && "Unreachable with 2 FP types!");
1344 case Legal:
1345 // Input is legal? Do an FP_ROUND_INREG.
1346 Result = LegalizeOp(Node->getOperand(0));
1347 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1348 break;
1349 }
1350 break;
1351
1352 case ISD::SINT_TO_FP:
1353 case ISD::UINT_TO_FP:
1354 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1355 case Legal:
1356 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001357 // No extra round required here.
1358 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001359 break;
1360
1361 case Promote:
1362 Result = PromoteOp(Node->getOperand(0));
1363 if (Node->getOpcode() == ISD::SINT_TO_FP)
1364 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1365 Result, Node->getOperand(0).getValueType());
1366 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001367 Result = DAG.getZeroExtendInReg(Result,
1368 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001369 // No extra round required here.
1370 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001371 break;
1372 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001373 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1374 Node->getOperand(0));
1375 Result = LegalizeOp(Result);
1376
1377 // Round if we cannot tolerate excess precision.
1378 if (NoExcessFPPrecision)
1379 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1380 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001381 }
Chris Lattner4d978642005-01-15 22:16:26 +00001382 break;
1383
1384 case ISD::FP_TO_SINT:
1385 case ISD::FP_TO_UINT:
1386 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1387 case Legal:
1388 Tmp1 = LegalizeOp(Node->getOperand(0));
1389 break;
1390 case Promote:
1391 // The input result is prerounded, so we don't have to do anything
1392 // special.
1393 Tmp1 = PromoteOp(Node->getOperand(0));
1394 break;
1395 case Expand:
1396 assert(0 && "not implemented");
1397 }
1398 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1399 break;
1400
Chris Lattner13fe99c2005-04-02 05:00:07 +00001401 case ISD::FABS:
1402 case ISD::FNEG:
1403 Tmp1 = PromoteOp(Node->getOperand(0));
1404 assert(Tmp1.getValueType() == NVT);
1405 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1406 // NOTE: we do not have to do any extra rounding here for
1407 // NoExcessFPPrecision, because we know the input will have the appropriate
1408 // precision, and these operations don't modify precision at all.
1409 break;
1410
Chris Lattner9d6fa982005-04-28 21:44:33 +00001411 case ISD::FSQRT:
1412 case ISD::FSIN:
1413 case ISD::FCOS:
1414 Tmp1 = PromoteOp(Node->getOperand(0));
1415 assert(Tmp1.getValueType() == NVT);
1416 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1417 if(NoExcessFPPrecision)
1418 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1419 break;
1420
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001421 case ISD::AND:
1422 case ISD::OR:
1423 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001424 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001425 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001426 case ISD::MUL:
1427 // The input may have strange things in the top bits of the registers, but
1428 // these operations don't care. They may have wierd bits going out, but
1429 // that too is okay if they are integer operations.
1430 Tmp1 = PromoteOp(Node->getOperand(0));
1431 Tmp2 = PromoteOp(Node->getOperand(1));
1432 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1433 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1434
1435 // However, if this is a floating point operation, they will give excess
1436 // precision that we may not be able to tolerate. If we DO allow excess
1437 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001438 // FIXME: Why would we need to round FP ops more than integer ones?
1439 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001440 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1441 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1442 break;
1443
Chris Lattner4d978642005-01-15 22:16:26 +00001444 case ISD::SDIV:
1445 case ISD::SREM:
1446 // These operators require that their input be sign extended.
1447 Tmp1 = PromoteOp(Node->getOperand(0));
1448 Tmp2 = PromoteOp(Node->getOperand(1));
1449 if (MVT::isInteger(NVT)) {
1450 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001451 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001452 }
1453 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1454
1455 // Perform FP_ROUND: this is probably overly pessimistic.
1456 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1457 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1458 break;
1459
1460 case ISD::UDIV:
1461 case ISD::UREM:
1462 // These operators require that their input be zero extended.
1463 Tmp1 = PromoteOp(Node->getOperand(0));
1464 Tmp2 = PromoteOp(Node->getOperand(1));
1465 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00001466 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1467 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001468 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1469 break;
1470
1471 case ISD::SHL:
1472 Tmp1 = PromoteOp(Node->getOperand(0));
1473 Tmp2 = LegalizeOp(Node->getOperand(1));
1474 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1475 break;
1476 case ISD::SRA:
1477 // The input value must be properly sign extended.
1478 Tmp1 = PromoteOp(Node->getOperand(0));
1479 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1480 Tmp2 = LegalizeOp(Node->getOperand(1));
1481 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1482 break;
1483 case ISD::SRL:
1484 // The input value must be properly zero extended.
1485 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001486 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001487 Tmp2 = LegalizeOp(Node->getOperand(1));
1488 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1489 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001490 case ISD::LOAD:
1491 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1492 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001493 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001494 if (MVT::isInteger(NVT))
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001495 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00001496 else
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001497 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001498
1499 // Remember that we legalized the chain.
1500 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1501 break;
1502 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001503 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1504 case Expand: assert(0 && "It's impossible to expand bools");
1505 case Legal:
1506 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1507 break;
1508 case Promote:
1509 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1510 break;
1511 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001512 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1513 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1514 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1515 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001516 case ISD::CALL: {
1517 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1518 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1519
Chris Lattner3d95c142005-01-19 20:24:35 +00001520 std::vector<SDOperand> Ops;
1521 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1522 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1523
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001524 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1525 "Can only promote single result calls");
1526 std::vector<MVT::ValueType> RetTyVTs;
1527 RetTyVTs.reserve(2);
1528 RetTyVTs.push_back(NVT);
1529 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001530 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001531 Result = SDOperand(NC, 0);
1532
1533 // Insert the new chain mapping.
1534 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1535 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001536 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001537 }
1538
1539 assert(Result.Val && "Didn't set a result!");
1540 AddPromotedOperand(Op, Result);
1541 return Result;
1542}
Chris Lattnerdc750592005-01-07 07:47:09 +00001543
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001544/// ExpandAddSub - Find a clever way to expand this add operation into
1545/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001546void SelectionDAGLegalize::
1547ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1548 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001549 // Expand the subcomponents.
1550 SDOperand LHSL, LHSH, RHSL, RHSH;
1551 ExpandOp(LHS, LHSL, LHSH);
1552 ExpandOp(RHS, RHSL, RHSH);
1553
Chris Lattner8ffd0042005-04-11 20:29:59 +00001554 // FIXME: this should be moved to the dag combiner someday.
1555 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1556 if (LHSL.getValueType() == MVT::i32) {
1557 SDOperand LowEl;
1558 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1559 if (C->getValue() == 0)
1560 LowEl = RHSL;
1561 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1562 if (C->getValue() == 0)
1563 LowEl = LHSL;
1564 if (LowEl.Val) {
1565 // Turn this into an add/sub of the high part only.
1566 SDOperand HiEl =
1567 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1568 LowEl.getValueType(), LHSH, RHSH);
1569 Lo = LowEl;
1570 Hi = HiEl;
1571 return;
1572 }
1573 }
1574
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001575 std::vector<SDOperand> Ops;
1576 Ops.push_back(LHSL);
1577 Ops.push_back(LHSH);
1578 Ops.push_back(RHSL);
1579 Ops.push_back(RHSH);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001580 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001581 Hi = Lo.getValue(1);
1582}
1583
Chris Lattner4157c412005-04-02 04:00:59 +00001584void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1585 SDOperand Op, SDOperand Amt,
1586 SDOperand &Lo, SDOperand &Hi) {
1587 // Expand the subcomponents.
1588 SDOperand LHSL, LHSH;
1589 ExpandOp(Op, LHSL, LHSH);
1590
1591 std::vector<SDOperand> Ops;
1592 Ops.push_back(LHSL);
1593 Ops.push_back(LHSH);
1594 Ops.push_back(Amt);
1595 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1596 Hi = Lo.getValue(1);
1597}
1598
1599
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001600/// ExpandShift - Try to find a clever way to expand this shift operation out to
1601/// smaller elements. If we can't find a way that is more efficient than a
1602/// libcall on this target, return false. Otherwise, return true with the
1603/// low-parts expanded into Lo and Hi.
1604bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1605 SDOperand &Lo, SDOperand &Hi) {
1606 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1607 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00001608
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001609 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00001610 SDOperand ShAmt = LegalizeOp(Amt);
1611 MVT::ValueType ShTy = ShAmt.getValueType();
1612 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1613 unsigned NVTBits = MVT::getSizeInBits(NVT);
1614
1615 // Handle the case when Amt is an immediate. Other cases are currently broken
1616 // and are disabled.
1617 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1618 unsigned Cst = CN->getValue();
1619 // Expand the incoming operand to be shifted, so that we have its parts
1620 SDOperand InL, InH;
1621 ExpandOp(Op, InL, InH);
1622 switch(Opc) {
1623 case ISD::SHL:
1624 if (Cst > VTBits) {
1625 Lo = DAG.getConstant(0, NVT);
1626 Hi = DAG.getConstant(0, NVT);
1627 } else if (Cst > NVTBits) {
1628 Lo = DAG.getConstant(0, NVT);
1629 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001630 } else if (Cst == NVTBits) {
1631 Lo = DAG.getConstant(0, NVT);
1632 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00001633 } else {
1634 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1635 Hi = DAG.getNode(ISD::OR, NVT,
1636 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1637 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1638 }
1639 return true;
1640 case ISD::SRL:
1641 if (Cst > VTBits) {
1642 Lo = DAG.getConstant(0, NVT);
1643 Hi = DAG.getConstant(0, NVT);
1644 } else if (Cst > NVTBits) {
1645 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1646 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00001647 } else if (Cst == NVTBits) {
1648 Lo = InH;
1649 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00001650 } else {
1651 Lo = DAG.getNode(ISD::OR, NVT,
1652 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1653 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1654 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1655 }
1656 return true;
1657 case ISD::SRA:
1658 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001659 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001660 DAG.getConstant(NVTBits-1, ShTy));
1661 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001662 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001663 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00001664 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001665 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001666 } else if (Cst == NVTBits) {
1667 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00001668 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00001669 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00001670 } else {
1671 Lo = DAG.getNode(ISD::OR, NVT,
1672 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1673 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1674 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1675 }
1676 return true;
1677 }
1678 }
1679 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1680 // so disable it for now. Currently targets are handling this via SHL_PARTS
1681 // and friends.
1682 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001683
1684 // If we have an efficient select operation (or if the selects will all fold
1685 // away), lower to some complex code, otherwise just emit the libcall.
1686 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1687 !isa<ConstantSDNode>(Amt))
1688 return false;
1689
1690 SDOperand InL, InH;
1691 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001692 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1693 DAG.getConstant(NVTBits, ShTy), ShAmt);
1694
Chris Lattner4d25c042005-01-20 20:29:23 +00001695 // Compare the unmasked shift amount against 32.
1696 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1697 DAG.getConstant(NVTBits, ShTy));
1698
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001699 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1700 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1701 DAG.getConstant(NVTBits-1, ShTy));
1702 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1703 DAG.getConstant(NVTBits-1, ShTy));
1704 }
1705
1706 if (Opc == ISD::SHL) {
1707 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1708 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1709 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001710 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00001711
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001712 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1713 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1714 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001715 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1716 DAG.getSetCC(ISD::SETEQ,
1717 TLI.getSetCCResultTy(), NAmt,
1718 DAG.getConstant(32, ShTy)),
1719 DAG.getConstant(0, NVT),
1720 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001721 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001722 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001723 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001724 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001725
1726 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001727 if (Opc == ISD::SRA)
1728 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1729 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001730 else
1731 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001732 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001733 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001734 }
1735 return true;
1736}
Chris Lattneraac464e2005-01-21 06:05:23 +00001737
Chris Lattner4add7e32005-01-23 04:42:50 +00001738/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1739/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1740/// Found.
1741static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1742 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1743
1744 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1745 // than the Found node. Just remember this node and return.
1746 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1747 Found = Node;
1748 return;
1749 }
1750
1751 // Otherwise, scan the operands of Node to see if any of them is a call.
1752 assert(Node->getNumOperands() != 0 &&
1753 "All leaves should have depth equal to the entry node!");
1754 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1755 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1756
1757 // Tail recurse for the last iteration.
1758 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1759 Found);
1760}
1761
1762
1763/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1764/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1765/// than Found.
1766static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1767 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1768
1769 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1770 // than the Found node. Just remember this node and return.
1771 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1772 Found = Node;
1773 return;
1774 }
1775
1776 // Otherwise, scan the operands of Node to see if any of them is a call.
1777 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1778 if (UI == E) return;
1779 for (--E; UI != E; ++UI)
1780 FindEarliestAdjCallStackUp(*UI, Found);
1781
1782 // Tail recurse for the last iteration.
1783 FindEarliestAdjCallStackUp(*UI, Found);
1784}
1785
1786/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1787/// find the ADJCALLSTACKUP node that terminates the call sequence.
1788static SDNode *FindAdjCallStackUp(SDNode *Node) {
1789 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1790 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00001791 if (Node->use_empty())
1792 return 0; // No adjcallstackup
Chris Lattner4add7e32005-01-23 04:42:50 +00001793
1794 if (Node->hasOneUse()) // Simple case, only has one user to check.
1795 return FindAdjCallStackUp(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00001796
Chris Lattner4add7e32005-01-23 04:42:50 +00001797 SDOperand TheChain(Node, Node->getNumValues()-1);
1798 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00001799
1800 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner4add7e32005-01-23 04:42:50 +00001801 E = Node->use_end(); ; ++UI) {
1802 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
Misha Brukman835702a2005-04-21 22:36:52 +00001803
Chris Lattner4add7e32005-01-23 04:42:50 +00001804 // Make sure to only follow users of our token chain.
1805 SDNode *User = *UI;
1806 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1807 if (User->getOperand(i) == TheChain)
1808 return FindAdjCallStackUp(User);
1809 }
1810 assert(0 && "Unreachable");
1811 abort();
1812}
1813
1814/// FindInputOutputChains - If we are replacing an operation with a call we need
1815/// to find the call that occurs before and the call that occurs after it to
1816/// properly serialize the calls in the block.
1817static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
1818 SDOperand Entry) {
1819 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanadd0c632005-04-11 03:01:51 +00001820 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00001821 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
1822 //std::cerr << "Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00001823
Nate Begemanadd0c632005-04-11 03:01:51 +00001824 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
1825 // previous call in the function. LatestCallStackDown may in that case be
1826 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
1827 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
1828 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
1829 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
1830 else
1831 LatestAdjCallStackUp = Entry.Val;
1832 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
Misha Brukman835702a2005-04-21 22:36:52 +00001833
Chris Lattner4add7e32005-01-23 04:42:50 +00001834 SDNode *EarliestAdjCallStackUp = 0;
1835 FindEarliestAdjCallStackUp(OpNode, EarliestAdjCallStackUp);
1836
1837 if (EarliestAdjCallStackUp) {
Misha Brukman835702a2005-04-21 22:36:52 +00001838 //std::cerr << "Found node: ";
Chris Lattner4add7e32005-01-23 04:42:50 +00001839 //EarliestAdjCallStackUp->dump(); std::cerr <<"\n";
1840 }
1841
1842 return SDOperand(LatestAdjCallStackUp, 0);
1843}
1844
1845
1846
Chris Lattneraac464e2005-01-21 06:05:23 +00001847// ExpandLibCall - Expand a node into a call to a libcall. If the result value
1848// does not fit into a register, return the lo part and set the hi part to the
1849// by-reg argument. If it does fit into a single register, return the result
1850// and leave the Hi part unset.
1851SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
1852 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001853 SDNode *OutChain;
1854 SDOperand InChain = FindInputOutputChains(Node, OutChain,
1855 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00001856 if (InChain.Val == 0)
1857 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00001858
Chris Lattneraac464e2005-01-21 06:05:23 +00001859 TargetLowering::ArgListTy Args;
1860 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1861 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
1862 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
1863 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
1864 }
1865 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00001866
Chris Lattneraac464e2005-01-21 06:05:23 +00001867 // We don't care about token chains for libcalls. We just use the entry
1868 // node as our input and ignore the output chain. This allows us to place
1869 // calls wherever we need them to satisfy data dependences.
1870 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Nate Begemanf6565252005-03-26 01:29:23 +00001871 SDOperand Result = TLI.LowerCallTo(InChain, RetTy, false, Callee,
Chris Lattneraac464e2005-01-21 06:05:23 +00001872 Args, DAG).first;
1873 switch (getTypeAction(Result.getValueType())) {
1874 default: assert(0 && "Unknown thing");
1875 case Legal:
1876 return Result;
1877 case Promote:
1878 assert(0 && "Cannot promote this yet!");
1879 case Expand:
1880 SDOperand Lo;
1881 ExpandOp(Result, Lo, Hi);
1882 return Lo;
1883 }
1884}
1885
Chris Lattner4add7e32005-01-23 04:42:50 +00001886
Chris Lattneraac464e2005-01-21 06:05:23 +00001887/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
1888/// destination type is legal.
1889SDOperand SelectionDAGLegalize::
1890ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
1891 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
1892 assert(getTypeAction(Source.getValueType()) == Expand &&
1893 "This is not an expansion!");
1894 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
1895
Chris Lattner4add7e32005-01-23 04:42:50 +00001896 SDNode *OutChain;
1897 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
1898 DAG.getEntryNode());
1899
Chris Lattner0dfd7d32005-01-23 23:19:44 +00001900 const char *FnName = 0;
Chris Lattneraac464e2005-01-21 06:05:23 +00001901 if (isSigned) {
1902 if (DestTy == MVT::f32)
1903 FnName = "__floatdisf";
1904 else {
1905 assert(DestTy == MVT::f64 && "Unknown fp value type!");
1906 FnName = "__floatdidf";
1907 }
1908 } else {
1909 // If this is unsigned, and not supported, first perform the conversion to
1910 // signed, then adjust the result if the sign bit is set.
Chris Lattner0efd77e2005-04-13 03:42:14 +00001911 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattneraac464e2005-01-21 06:05:23 +00001912
Chris Lattnere69ad5f2005-04-13 05:09:42 +00001913 assert(Source.getValueType() == MVT::i64 &&
1914 "This only works for 64-bit -> FP");
1915 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
1916 // incoming integer is set. To handle this, we dynamically test to see if
1917 // it is set, and, if so, add a fudge factor.
1918 SDOperand Lo, Hi;
1919 ExpandOp(Source, Lo, Hi);
1920
1921 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
1922 DAG.getConstant(0, Hi.getValueType()));
1923 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
1924 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
1925 SignSet, Four, Zero);
1926 // FIXME: This is almost certainly broken for big-endian systems. Should
1927 // this just put the fudge factor in the low bits of the uint64 constant or?
1928 static Constant *FudgeFactor =
1929 ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32);
1930
1931 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
1932 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
1933 TLI.getPointerTy());
1934 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
1935 SDOperand FudgeInReg;
1936 if (DestTy == MVT::f32)
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001937 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00001938 else {
1939 assert(DestTy == MVT::f64 && "Unexpected conversion");
1940 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001941 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00001942 }
1943 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00001944 }
1945 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
1946
1947 TargetLowering::ArgListTy Args;
1948 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
1949 Args.push_back(std::make_pair(Source, ArgTy));
1950
1951 // We don't care about token chains for libcalls. We just use the entry
1952 // node as our input and ignore the output chain. This allows us to place
1953 // calls wherever we need them to satisfy data dependences.
1954 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Nate Begemanf6565252005-03-26 01:29:23 +00001955 return TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG).first;
Chris Lattneraac464e2005-01-21 06:05:23 +00001956}
Misha Brukman835702a2005-04-21 22:36:52 +00001957
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001958
1959
Chris Lattnerdc750592005-01-07 07:47:09 +00001960/// ExpandOp - Expand the specified SDOperand into its two component pieces
1961/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
1962/// LegalizeNodes map is filled in for any results that are not expanded, the
1963/// ExpandedNodes map is filled in for any results that are expanded, and the
1964/// Lo/Hi values are returned.
1965void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
1966 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001967 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00001968 SDNode *Node = Op.Val;
1969 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
1970 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
1971 assert(MVT::isInteger(NVT) && NVT < VT &&
1972 "Cannot expand to FP value or to larger int value!");
1973
1974 // If there is more than one use of this, see if we already expanded it.
1975 // There is no use remembering values that only have a single use, as the map
1976 // entries will never be reused.
1977 if (!Node->hasOneUse()) {
1978 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
1979 = ExpandedNodes.find(Op);
1980 if (I != ExpandedNodes.end()) {
1981 Lo = I->second.first;
1982 Hi = I->second.second;
1983 return;
1984 }
1985 }
1986
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001987 // Expanding to multiple registers needs to perform an optimization step, and
1988 // is not careful to avoid operations the target does not support. Make sure
1989 // that all generated operations are legalized in the next iteration.
1990 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00001991
Chris Lattnerdc750592005-01-07 07:47:09 +00001992 switch (Node->getOpcode()) {
1993 default:
1994 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1995 assert(0 && "Do not know how to expand this operator!");
1996 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001997 case ISD::UNDEF:
1998 Lo = DAG.getNode(ISD::UNDEF, NVT);
1999 Hi = DAG.getNode(ISD::UNDEF, NVT);
2000 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002001 case ISD::Constant: {
2002 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2003 Lo = DAG.getConstant(Cst, NVT);
2004 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2005 break;
2006 }
2007
2008 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00002009 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00002010 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00002011 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2012 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002013
Chris Lattner3b8e7192005-01-14 22:38:01 +00002014 // Remember that we legalized the chain.
2015 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2016
Chris Lattnerdc750592005-01-07 07:47:09 +00002017 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2018 break;
2019 }
2020
Chris Lattner32e08b72005-03-28 22:03:13 +00002021 case ISD::BUILD_PAIR:
2022 // Legalize both operands. FIXME: in the future we should handle the case
2023 // where the two elements are not legal.
2024 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2025 Lo = LegalizeOp(Node->getOperand(0));
2026 Hi = LegalizeOp(Node->getOperand(1));
2027 break;
2028
Chris Lattnerdc750592005-01-07 07:47:09 +00002029 case ISD::LOAD: {
2030 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2031 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002032 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002033
2034 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002035 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002036 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2037 getIntPtrConstant(IncrementSize));
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002038 //Is this safe? declaring that the two parts of the split load
2039 //are from the same instruction?
2040 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002041
2042 // Build a factor node to remember that this load is independent of the
2043 // other one.
2044 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2045 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002046
Chris Lattnerdc750592005-01-07 07:47:09 +00002047 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002048 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002049 if (!TLI.isLittleEndian())
2050 std::swap(Lo, Hi);
2051 break;
2052 }
2053 case ISD::CALL: {
2054 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2055 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2056
Chris Lattner3d95c142005-01-19 20:24:35 +00002057 bool Changed = false;
2058 std::vector<SDOperand> Ops;
2059 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2060 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2061 Changed |= Ops.back() != Node->getOperand(i);
2062 }
2063
Chris Lattnerdc750592005-01-07 07:47:09 +00002064 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2065 "Can only expand a call once so far, not i64 -> i16!");
2066
2067 std::vector<MVT::ValueType> RetTyVTs;
2068 RetTyVTs.reserve(3);
2069 RetTyVTs.push_back(NVT);
2070 RetTyVTs.push_back(NVT);
2071 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00002072 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00002073 Lo = SDOperand(NC, 0);
2074 Hi = SDOperand(NC, 1);
2075
2076 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002077 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002078 break;
2079 }
2080 case ISD::AND:
2081 case ISD::OR:
2082 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2083 SDOperand LL, LH, RL, RH;
2084 ExpandOp(Node->getOperand(0), LL, LH);
2085 ExpandOp(Node->getOperand(1), RL, RH);
2086 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2087 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2088 break;
2089 }
2090 case ISD::SELECT: {
2091 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002092
2093 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2094 case Expand: assert(0 && "It's impossible to expand bools");
2095 case Legal:
2096 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2097 break;
2098 case Promote:
2099 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2100 break;
2101 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002102 ExpandOp(Node->getOperand(1), LL, LH);
2103 ExpandOp(Node->getOperand(2), RL, RH);
2104 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2105 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2106 break;
2107 }
2108 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002109 SDOperand In;
2110 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2111 case Expand: assert(0 && "expand-expand not implemented yet!");
2112 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2113 case Promote:
2114 In = PromoteOp(Node->getOperand(0));
2115 // Emit the appropriate sign_extend_inreg to get the value we want.
2116 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2117 Node->getOperand(0).getValueType());
2118 break;
2119 }
2120
Chris Lattnerdc750592005-01-07 07:47:09 +00002121 // The low part is just a sign extension of the input (which degenerates to
2122 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002123 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002124
Chris Lattnerdc750592005-01-07 07:47:09 +00002125 // The high part is obtained by SRA'ing all but one of the bits of the lo
2126 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002127 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002128 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2129 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002130 break;
2131 }
Chris Lattner47844892005-04-03 23:41:52 +00002132 case ISD::ZERO_EXTEND: {
2133 SDOperand In;
2134 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2135 case Expand: assert(0 && "expand-expand not implemented yet!");
2136 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2137 case Promote:
2138 In = PromoteOp(Node->getOperand(0));
2139 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00002140 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00002141 break;
2142 }
2143
Chris Lattnerdc750592005-01-07 07:47:09 +00002144 // The low part is just a zero extension of the input (which degenerates to
2145 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002146 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002147
Chris Lattnerdc750592005-01-07 07:47:09 +00002148 // The high part is just a zero.
2149 Hi = DAG.getConstant(0, NVT);
2150 break;
Chris Lattner47844892005-04-03 23:41:52 +00002151 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002152 // These operators cannot be expanded directly, emit them as calls to
2153 // library functions.
2154 case ISD::FP_TO_SINT:
2155 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002156 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002157 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002158 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002159 break;
2160 case ISD::FP_TO_UINT:
2161 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002162 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002163 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002164 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002165 break;
2166
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002167 case ISD::SHL:
2168 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002169 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002170 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002171
2172 // If this target supports SHL_PARTS, use it.
2173 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002174 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2175 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002176 break;
2177 }
2178
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002179 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002180 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002181 break;
2182
2183 case ISD::SRA:
2184 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002185 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002186 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002187
2188 // If this target supports SRA_PARTS, use it.
2189 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002190 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2191 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002192 break;
2193 }
2194
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002195 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002196 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002197 break;
2198 case ISD::SRL:
2199 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002200 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002201 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002202
2203 // If this target supports SRL_PARTS, use it.
2204 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002205 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2206 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002207 break;
2208 }
2209
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002210 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002211 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002212 break;
2213
Misha Brukman835702a2005-04-21 22:36:52 +00002214 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002215 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2216 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002217 break;
2218 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002219 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2220 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002221 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002222 case ISD::MUL: {
2223 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2224 SDOperand LL, LH, RL, RH;
2225 ExpandOp(Node->getOperand(0), LL, LH);
2226 ExpandOp(Node->getOperand(1), RL, RH);
2227 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2228 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2229 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2230 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2231 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2232 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2233 } else {
2234 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2235 }
2236 break;
2237 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002238 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2239 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2240 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2241 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002242 }
2243
2244 // Remember in a map if the values will be reused later.
2245 if (!Node->hasOneUse()) {
2246 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2247 std::make_pair(Lo, Hi))).second;
2248 assert(isNew && "Value already expanded?!?");
2249 }
2250}
2251
2252
2253// SelectionDAG::Legalize - This is the entry point for the file.
2254//
Chris Lattner4add7e32005-01-23 04:42:50 +00002255void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002256 /// run - This is the main entry point to this class.
2257 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002258 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002259}
2260