blob: c6fc076ee2804d3fdb3a59fdf8c130d4c6245c2a [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 Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
24using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
28/// hacks on it until the target machine can handle it. This involves
29/// eliminating value sizes the machine cannot handle (promoting small sizes to
30/// large sizes or splitting up large values into small values) as well as
31/// eliminating operations the machine cannot handle.
32///
33/// This code also does a small amount of optimization and recognition of idioms
34/// as part of its processing. For example, if a target does not support a
35/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
36/// will attempt merge setcc and brc instructions into brcc's.
37///
38namespace {
39class SelectionDAGLegalize {
40 TargetLowering &TLI;
41 SelectionDAG &DAG;
42
43 /// LegalizeAction - This enum indicates what action we should take for each
44 /// value type the can occur in the program.
45 enum LegalizeAction {
46 Legal, // The target natively supports this value type.
47 Promote, // This should be promoted to the next larger type.
48 Expand, // This integer type should be broken into smaller pieces.
49 };
50
Chris Lattnerdc750592005-01-07 07:47:09 +000051 /// ValueTypeActions - This is a bitvector that contains two bits for each
52 /// value type, where the two bits correspond to the LegalizeAction enum.
53 /// This can be queried with "getTypeAction(VT)".
54 unsigned ValueTypeActions;
55
56 /// NeedsAnotherIteration - This is set when we expand a large integer
57 /// operation into smaller integer operations, but the smaller operations are
58 /// not set. This occurs only rarely in practice, for targets that don't have
59 /// 32-bit or larger integer registers.
60 bool NeedsAnotherIteration;
61
62 /// LegalizedNodes - For nodes that are of legal width, and that have more
63 /// than one use, this map indicates what regularized operand to use. This
64 /// allows us to avoid legalizing the same thing more than once.
65 std::map<SDOperand, SDOperand> LegalizedNodes;
66
Chris Lattner1f2c9d82005-01-15 05:21:40 +000067 /// PromotedNodes - For nodes that are below legal width, and that have more
68 /// than one use, this map indicates what promoted value to use. This allows
69 /// us to avoid promoting the same thing more than once.
70 std::map<SDOperand, SDOperand> PromotedNodes;
71
Chris Lattnerdc750592005-01-07 07:47:09 +000072 /// ExpandedNodes - For nodes that need to be expanded, and which have more
73 /// than one use, this map indicates which which operands are the expanded
74 /// version of the input. This allows us to avoid expanding the same node
75 /// more than once.
76 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
77
Chris Lattnerea4ca942005-01-07 22:28:47 +000078 void AddLegalizedOperand(SDOperand From, SDOperand To) {
79 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
80 assert(isNew && "Got into the map somehow?");
81 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000082 void AddPromotedOperand(SDOperand From, SDOperand To) {
83 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
84 assert(isNew && "Got into the map somehow?");
85 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000086
Chris Lattnerdc750592005-01-07 07:47:09 +000087public:
88
Chris Lattner4add7e32005-01-23 04:42:50 +000089 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000090
91 /// Run - While there is still lowering to do, perform a pass over the DAG.
92 /// Most regularization can be done in a single pass, but targets that require
93 /// large values to be split into registers multiple times (e.g. i64 -> 4x
94 /// i16) require iteration for these values (the first iteration will demote
95 /// to i32, the second will demote to i16).
96 void Run() {
97 do {
98 NeedsAnotherIteration = false;
99 LegalizeDAG();
100 } while (NeedsAnotherIteration);
101 }
102
103 /// getTypeAction - Return how we should legalize values of this type, either
104 /// it is already legal or we need to expand it into multiple registers of
105 /// smaller integer type, or we need to promote it to a larger type.
106 LegalizeAction getTypeAction(MVT::ValueType VT) const {
107 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
108 }
109
110 /// isTypeLegal - Return true if this type is legal on this target.
111 ///
112 bool isTypeLegal(MVT::ValueType VT) const {
113 return getTypeAction(VT) == Legal;
114 }
115
116private:
117 void LegalizeDAG();
118
119 SDOperand LegalizeOp(SDOperand O);
120 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000121 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000122
Chris Lattneraac464e2005-01-21 06:05:23 +0000123 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
124 SDOperand &Hi);
125 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
126 SDOperand Source);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000127 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
128 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000129 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
130 SDOperand &Lo, SDOperand &Hi);
131 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000132 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000133
Chris Lattnera5bf1032005-05-12 04:49:08 +0000134 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
135
Chris Lattnerdc750592005-01-07 07:47:09 +0000136 SDOperand getIntPtrConstant(uint64_t Val) {
137 return DAG.getConstant(Val, TLI.getPointerTy());
138 }
139};
140}
141
142
Chris Lattner4add7e32005-01-23 04:42:50 +0000143SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
144 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
145 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 assert(MVT::LAST_VALUETYPE <= 16 &&
147 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000148}
149
Chris Lattnerdc750592005-01-07 07:47:09 +0000150void SelectionDAGLegalize::LegalizeDAG() {
151 SDOperand OldRoot = DAG.getRoot();
152 SDOperand NewRoot = LegalizeOp(OldRoot);
153 DAG.setRoot(NewRoot);
154
155 ExpandedNodes.clear();
156 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000157 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000158
159 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000160 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000161}
162
163SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000164 assert(getTypeAction(Op.getValueType()) == Legal &&
165 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000166 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000167
Chris Lattnerdc750592005-01-07 07:47:09 +0000168 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000169 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000170 if (Node->getNumValues() > 1) {
171 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
172 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000173 case Legal: break; // Nothing to do.
174 case Expand: {
175 SDOperand T1, T2;
176 ExpandOp(Op.getValue(i), T1, T2);
177 assert(LegalizedNodes.count(Op) &&
178 "Expansion didn't add legal operands!");
179 return LegalizedNodes[Op];
180 }
181 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000182 PromoteOp(Op.getValue(i));
183 assert(LegalizedNodes.count(Op) &&
184 "Expansion didn't add legal operands!");
185 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000186 }
187 }
188
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000189 // Note that LegalizeOp may be reentered even from single-use nodes, which
190 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000191 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
192 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000193
Chris Lattnerec26b482005-01-09 19:03:49 +0000194 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000195
196 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000197
198 switch (Node->getOpcode()) {
199 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000200 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
201 // If this is a target node, legalize it by legalizing the operands then
202 // passing it through.
203 std::vector<SDOperand> Ops;
204 bool Changed = false;
205 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
206 Ops.push_back(LegalizeOp(Node->getOperand(i)));
207 Changed = Changed || Node->getOperand(i) != Ops.back();
208 }
209 if (Changed)
210 if (Node->getNumValues() == 1)
211 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
212 else {
213 std::vector<MVT::ValueType> VTs(Node->value_begin(),
214 Node->value_end());
215 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
216 }
217
218 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
219 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
220 return Result.getValue(Op.ResNo);
221 }
222 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000223 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
224 assert(0 && "Do not know how to legalize this operator!");
225 abort();
226 case ISD::EntryToken:
227 case ISD::FrameIndex:
228 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000229 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000230 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000231 assert(getTypeAction(Node->getValueType(0)) == Legal &&
232 "This must be legal!");
233 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000234 case ISD::CopyFromReg:
235 Tmp1 = LegalizeOp(Node->getOperand(0));
236 if (Tmp1 != Node->getOperand(0))
237 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
238 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000239 else
240 Result = Op.getValue(0);
241
242 // Since CopyFromReg produces two values, make sure to remember that we
243 // legalized both of them.
244 AddLegalizedOperand(Op.getValue(0), Result);
245 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
246 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000247 case ISD::ImplicitDef:
248 Tmp1 = LegalizeOp(Node->getOperand(0));
249 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000250 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000251 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000252 case ISD::UNDEF: {
253 MVT::ValueType VT = Op.getValueType();
254 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000255 default: assert(0 && "This action is not supported yet!");
256 case TargetLowering::Expand:
257 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000258 if (MVT::isInteger(VT))
259 Result = DAG.getConstant(0, VT);
260 else if (MVT::isFloatingPoint(VT))
261 Result = DAG.getConstantFP(0, VT);
262 else
263 assert(0 && "Unknown value type!");
264 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000265 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000266 break;
267 }
268 break;
269 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000270 case ISD::Constant:
271 // We know we don't need to expand constants here, constants only have one
272 // value and we check that it is fine above.
273
274 // FIXME: Maybe we should handle things like targets that don't support full
275 // 32-bit immediates?
276 break;
277 case ISD::ConstantFP: {
278 // Spill FP immediates to the constant pool if the target cannot directly
279 // codegen them. Targets often have some immediate values that can be
280 // efficiently generated into an FP register without a load. We explicitly
281 // leave these constants as ConstantFP nodes for the target to deal with.
282
283 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
284
285 // Check to see if this FP immediate is already legal.
286 bool isLegal = false;
287 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
288 E = TLI.legal_fpimm_end(); I != E; ++I)
289 if (CFP->isExactlyValue(*I)) {
290 isLegal = true;
291 break;
292 }
293
294 if (!isLegal) {
295 // Otherwise we need to spill the constant to memory.
296 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
297
298 bool Extend = false;
299
300 // If a FP immediate is precise when represented as a float, we put it
301 // into the constant pool as a float, even if it's is statically typed
302 // as a double.
303 MVT::ValueType VT = CFP->getValueType(0);
304 bool isDouble = VT == MVT::f64;
305 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
306 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000307 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
308 // Only do this if the target has a native EXTLOAD instruction from
309 // f32.
310 TLI.getOperationAction(ISD::EXTLOAD,
311 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000312 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
313 VT = MVT::f32;
314 Extend = true;
315 }
Misha Brukman835702a2005-04-21 22:36:52 +0000316
Chris Lattnerdc750592005-01-07 07:47:09 +0000317 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
318 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000319 if (Extend) {
320 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000321 DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000322 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000323 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
324 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000325 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000326 }
327 break;
328 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000329 case ISD::TokenFactor: {
330 std::vector<SDOperand> Ops;
331 bool Changed = false;
332 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000333 SDOperand Op = Node->getOperand(i);
334 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000335 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000336 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
337 Changed = true;
338 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
339 Ops.push_back(LegalizeOp(Op.getOperand(j)));
340 } else {
341 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
342 Changed |= Ops[i] != Op;
343 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000344 }
345 if (Changed)
346 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
347 break;
348 }
349
Chris Lattner2dce7032005-05-12 23:24:06 +0000350 case ISD::CALLSEQ_START:
351 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000352 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000353 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000354 Tmp2 = Node->getOperand(0);
355 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000356 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000357
358 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
359 // this will cause the maps used to memoize results to get confused.
360 // Create and add a dummy use, just to increase its use count. This will
361 // be removed at the end of legalize when dead nodes are removed.
362 if (Tmp2.Val->hasOneUse())
363 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
364 DAG.getConstant(0, MVT::i32));
365 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000366 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000367 // nodes are treated specially and are mutated in place. This makes the dag
368 // legalization process more efficient and also makes libcall insertion
369 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000370 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000371 case ISD::DYNAMIC_STACKALLOC:
372 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
373 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
374 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
375 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000376 Tmp3 != Node->getOperand(2)) {
377 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
378 std::vector<SDOperand> Ops;
379 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
380 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
381 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000382 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000383
384 // Since this op produces two values, make sure to remember that we
385 // legalized both of them.
386 AddLegalizedOperand(SDOperand(Node, 0), Result);
387 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
388 return Result.getValue(Op.ResNo);
389
Chris Lattnerd0feb642005-05-13 18:43:43 +0000390 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000391 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000392 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
393 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000394
395 bool Changed = false;
396 std::vector<SDOperand> Ops;
397 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
398 Ops.push_back(LegalizeOp(Node->getOperand(i)));
399 Changed |= Ops.back() != Node->getOperand(i);
400 }
401
402 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000403 std::vector<MVT::ValueType> RetTyVTs;
404 RetTyVTs.reserve(Node->getNumValues());
405 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000406 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000407 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
408 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000409 } else {
410 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000411 }
Chris Lattner9242c502005-01-09 19:43:23 +0000412 // Since calls produce multiple values, make sure to remember that we
413 // legalized all of them.
414 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
415 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
416 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000417 }
Chris Lattner68a12142005-01-07 22:12:08 +0000418 case ISD::BR:
419 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
420 if (Tmp1 != Node->getOperand(0))
421 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
422 break;
423
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000424 case ISD::BRCOND:
425 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000426
427 switch (getTypeAction(Node->getOperand(1).getValueType())) {
428 case Expand: assert(0 && "It's impossible to expand bools");
429 case Legal:
430 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
431 break;
432 case Promote:
433 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
434 break;
435 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000436 // Basic block destination (Op#2) is always legal.
437 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
438 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
439 Node->getOperand(2));
440 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000441 case ISD::BRCONDTWOWAY:
442 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
443 switch (getTypeAction(Node->getOperand(1).getValueType())) {
444 case Expand: assert(0 && "It's impossible to expand bools");
445 case Legal:
446 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
447 break;
448 case Promote:
449 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
450 break;
451 }
452 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
453 // pair.
454 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
455 case TargetLowering::Promote:
456 default: assert(0 && "This action is not supported yet!");
457 case TargetLowering::Legal:
458 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
459 std::vector<SDOperand> Ops;
460 Ops.push_back(Tmp1);
461 Ops.push_back(Tmp2);
462 Ops.push_back(Node->getOperand(2));
463 Ops.push_back(Node->getOperand(3));
464 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
465 }
466 break;
467 case TargetLowering::Expand:
468 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
469 Node->getOperand(2));
470 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
471 break;
472 }
473 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000474
Chris Lattnerdc750592005-01-07 07:47:09 +0000475 case ISD::LOAD:
476 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
477 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000478
Chris Lattnerdc750592005-01-07 07:47:09 +0000479 if (Tmp1 != Node->getOperand(0) ||
480 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000481 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
482 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000483 else
484 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000485
Chris Lattnerea4ca942005-01-07 22:28:47 +0000486 // Since loads produce two values, make sure to remember that we legalized
487 // both of them.
488 AddLegalizedOperand(SDOperand(Node, 0), Result);
489 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
490 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000491
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000492 case ISD::EXTLOAD:
493 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000494 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000495 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
496 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000497
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000498 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
499 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000500 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000501 case TargetLowering::Promote:
502 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
503 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000504 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000505 // Since loads produce two values, make sure to remember that we legalized
506 // both of them.
507 AddLegalizedOperand(SDOperand(Node, 0), Result);
508 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
509 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000510
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000511 case TargetLowering::Legal:
512 if (Tmp1 != Node->getOperand(0) ||
513 Tmp2 != Node->getOperand(1))
514 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000515 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000516 else
517 Result = SDOperand(Node, 0);
518
519 // Since loads produce two values, make sure to remember that we legalized
520 // both of them.
521 AddLegalizedOperand(SDOperand(Node, 0), Result);
522 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
523 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000524 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000525 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
526 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
527 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000528 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000529 if (Op.ResNo)
530 return Load.getValue(1);
531 return Result;
532 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000533 assert(Node->getOpcode() != ISD::EXTLOAD &&
534 "EXTLOAD should always be supported!");
535 // Turn the unsupported load into an EXTLOAD followed by an explicit
536 // zero/sign extend inreg.
537 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000538 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000539 SDOperand ValRes;
540 if (Node->getOpcode() == ISD::SEXTLOAD)
541 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000542 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000543 else
544 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000545 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
546 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
547 if (Op.ResNo)
548 return Result.getValue(1);
549 return ValRes;
550 }
551 assert(0 && "Unreachable");
552 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000553 case ISD::EXTRACT_ELEMENT:
554 // Get both the low and high parts.
555 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
556 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
557 Result = Tmp2; // 1 -> Hi
558 else
559 Result = Tmp1; // 0 -> Lo
560 break;
561
562 case ISD::CopyToReg:
563 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000564
Chris Lattnerdc750592005-01-07 07:47:09 +0000565 switch (getTypeAction(Node->getOperand(1).getValueType())) {
566 case Legal:
567 // Legalize the incoming value (must be legal).
568 Tmp2 = LegalizeOp(Node->getOperand(1));
569 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000570 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000571 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000572 case Promote:
573 Tmp2 = PromoteOp(Node->getOperand(1));
574 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
575 break;
576 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000577 SDOperand Lo, Hi;
Misha Brukman835702a2005-04-21 22:36:52 +0000578 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000579 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000580 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
581 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
582 // Note that the copytoreg nodes are independent of each other.
583 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000584 assert(isTypeLegal(Result.getValueType()) &&
585 "Cannot expand multiple times yet (i64 -> i16)");
586 break;
587 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000588 break;
589
590 case ISD::RET:
591 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
592 switch (Node->getNumOperands()) {
593 case 2: // ret val
594 switch (getTypeAction(Node->getOperand(1).getValueType())) {
595 case Legal:
596 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000597 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000598 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
599 break;
600 case Expand: {
601 SDOperand Lo, Hi;
602 ExpandOp(Node->getOperand(1), Lo, Hi);
603 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000604 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000605 }
606 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000607 Tmp2 = PromoteOp(Node->getOperand(1));
608 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
609 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000610 }
611 break;
612 case 1: // ret void
613 if (Tmp1 != Node->getOperand(0))
614 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
615 break;
616 default: { // ret <values>
617 std::vector<SDOperand> NewValues;
618 NewValues.push_back(Tmp1);
619 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
620 switch (getTypeAction(Node->getOperand(i).getValueType())) {
621 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000622 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000623 break;
624 case Expand: {
625 SDOperand Lo, Hi;
626 ExpandOp(Node->getOperand(i), Lo, Hi);
627 NewValues.push_back(Lo);
628 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000629 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000630 }
631 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000632 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000633 }
634 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
635 break;
636 }
637 }
638 break;
639 case ISD::STORE:
640 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
641 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
642
Chris Lattnere69daaf2005-01-08 06:25:56 +0000643 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000644 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000645 if (CFP->getValueType(0) == MVT::f32) {
646 union {
647 unsigned I;
648 float F;
649 } V;
650 V.F = CFP->getValue();
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000651 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000652 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000653 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000654 } else {
655 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
656 union {
657 uint64_t I;
658 double F;
659 } V;
660 V.F = CFP->getValue();
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000661 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner5385db52005-05-09 20:23:03 +0000662 DAG.getConstant(V.I, MVT::i64), Tmp2,
663 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000664 }
Chris Lattnera4743132005-02-22 07:23:39 +0000665 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000666 }
667
Chris Lattnerdc750592005-01-07 07:47:09 +0000668 switch (getTypeAction(Node->getOperand(1).getValueType())) {
669 case Legal: {
670 SDOperand Val = LegalizeOp(Node->getOperand(1));
671 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
672 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +0000673 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
674 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000675 break;
676 }
677 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000678 // Truncate the value and store the result.
679 Tmp3 = PromoteOp(Node->getOperand(1));
680 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000681 Node->getOperand(3),
682 Node->getOperand(1).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000683 break;
684
Chris Lattnerdc750592005-01-07 07:47:09 +0000685 case Expand:
686 SDOperand Lo, Hi;
687 ExpandOp(Node->getOperand(1), Lo, Hi);
688
689 if (!TLI.isLittleEndian())
690 std::swap(Lo, Hi);
691
Chris Lattner55e9cde2005-05-11 04:51:16 +0000692 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
693 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000694 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000695 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
696 getIntPtrConstant(IncrementSize));
697 assert(isTypeLegal(Tmp2.getValueType()) &&
698 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000699 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +0000700 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
701 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000702 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
703 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000704 }
705 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000706 case ISD::PCMARKER:
707 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000708 if (Tmp1 != Node->getOperand(0))
709 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000710 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000711 case ISD::TRUNCSTORE:
712 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
713 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
714
715 switch (getTypeAction(Node->getOperand(1).getValueType())) {
716 case Legal:
717 Tmp2 = LegalizeOp(Node->getOperand(1));
718 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
719 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000720 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000721 Node->getOperand(3),
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000722 cast<MVTSDNode>(Node)->getExtraValueType());
723 break;
724 case Promote:
725 case Expand:
726 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
727 }
728 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000729 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000730 switch (getTypeAction(Node->getOperand(0).getValueType())) {
731 case Expand: assert(0 && "It's impossible to expand bools");
732 case Legal:
733 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
734 break;
735 case Promote:
736 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
737 break;
738 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000739 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000740 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000741
742 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
743 default: assert(0 && "This action is not supported yet!");
744 case TargetLowering::Legal:
745 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
746 Tmp3 != Node->getOperand(2))
747 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
748 Tmp1, Tmp2, Tmp3);
749 break;
750 case TargetLowering::Promote: {
751 MVT::ValueType NVT =
752 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
753 unsigned ExtOp, TruncOp;
754 if (MVT::isInteger(Tmp2.getValueType())) {
755 ExtOp = ISD::ZERO_EXTEND;
756 TruncOp = ISD::TRUNCATE;
757 } else {
758 ExtOp = ISD::FP_EXTEND;
759 TruncOp = ISD::FP_ROUND;
760 }
761 // Promote each of the values to the new type.
762 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
763 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
764 // Perform the larger operation, then round down.
765 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
766 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
767 break;
768 }
769 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000770 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000771 case ISD::SETCC:
772 switch (getTypeAction(Node->getOperand(0).getValueType())) {
773 case Legal:
774 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
775 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
776 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
777 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000778 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000779 break;
780 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000781 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
782 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
783
784 // If this is an FP compare, the operands have already been extended.
785 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
786 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000787 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000788
789 // Otherwise, we have to insert explicit sign or zero extends. Note
790 // that we could insert sign extends for ALL conditions, but zero extend
791 // is cheaper on many machines (an AND instead of two shifts), so prefer
792 // it.
793 switch (cast<SetCCSDNode>(Node)->getCondition()) {
794 default: assert(0 && "Unknown integer comparison!");
795 case ISD::SETEQ:
796 case ISD::SETNE:
797 case ISD::SETUGE:
798 case ISD::SETUGT:
799 case ISD::SETULE:
800 case ISD::SETULT:
801 // ALL of these operations will work if we either sign or zero extend
802 // the operands (including the unsigned comparisons!). Zero extend is
803 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +0000804 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
805 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000806 break;
807 case ISD::SETGE:
808 case ISD::SETGT:
809 case ISD::SETLT:
810 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +0000811 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
812 DAG.getValueType(VT));
813 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
814 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +0000815 break;
816 }
817
818 }
819 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000820 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000821 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000822 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000823 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
824 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
825 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
826 switch (cast<SetCCSDNode>(Node)->getCondition()) {
827 case ISD::SETEQ:
828 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +0000829 if (RHSLo == RHSHi)
830 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
831 if (RHSCST->isAllOnesValue()) {
832 // Comparison to -1.
833 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukman835702a2005-04-21 22:36:52 +0000834 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner71ff44e2005-04-12 01:46:05 +0000835 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukman835702a2005-04-21 22:36:52 +0000836 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +0000837 }
838
Chris Lattnerdc750592005-01-07 07:47:09 +0000839 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
840 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
841 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +0000842 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000843 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000844 DAG.getConstant(0, Tmp1.getValueType()));
845 break;
846 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +0000847 // If this is a comparison of the sign bit, just look at the top part.
848 // X > -1, x < 0
849 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukman835702a2005-04-21 22:36:52 +0000850 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +0000851 CST->getValue() == 0) || // X < 0
852 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
853 (CST->isAllOnesValue()))) // X > -1
854 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
855 Node->getValueType(0), LHSHi, RHSHi);
856
Chris Lattnerdc750592005-01-07 07:47:09 +0000857 // FIXME: This generated code sucks.
858 ISD::CondCode LowCC;
859 switch (cast<SetCCSDNode>(Node)->getCondition()) {
860 default: assert(0 && "Unknown integer setcc!");
861 case ISD::SETLT:
862 case ISD::SETULT: LowCC = ISD::SETULT; break;
863 case ISD::SETGT:
864 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
865 case ISD::SETLE:
866 case ISD::SETULE: LowCC = ISD::SETULE; break;
867 case ISD::SETGE:
868 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
869 }
Misha Brukman835702a2005-04-21 22:36:52 +0000870
Chris Lattnerdc750592005-01-07 07:47:09 +0000871 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
872 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
873 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
874
875 // NOTE: on targets without efficient SELECT of bools, we can always use
876 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000877 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000878 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000879 Node->getValueType(0), LHSHi, RHSHi);
880 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
881 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
882 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000883 break;
884 }
885 }
886 break;
887
Chris Lattner85d70c62005-01-11 05:57:22 +0000888 case ISD::MEMSET:
889 case ISD::MEMCPY:
890 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000891 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000892 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
893
894 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
895 switch (getTypeAction(Node->getOperand(2).getValueType())) {
896 case Expand: assert(0 && "Cannot expand a byte!");
897 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000898 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000899 break;
900 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000901 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000902 break;
903 }
904 } else {
Misha Brukman835702a2005-04-21 22:36:52 +0000905 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000906 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000907
908 SDOperand Tmp4;
909 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000910 case Expand: assert(0 && "Cannot expand this yet!");
911 case Legal:
912 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000913 break;
914 case Promote:
915 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000916 break;
917 }
918
919 SDOperand Tmp5;
920 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
921 case Expand: assert(0 && "Cannot expand this yet!");
922 case Legal:
923 Tmp5 = LegalizeOp(Node->getOperand(4));
924 break;
925 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000926 Tmp5 = PromoteOp(Node->getOperand(4));
927 break;
928 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000929
930 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
931 default: assert(0 && "This action not implemented for this operation!");
932 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000933 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
934 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
935 Tmp5 != Node->getOperand(4)) {
936 std::vector<SDOperand> Ops;
937 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
938 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
939 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
940 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000941 break;
942 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000943 // Otherwise, the target does not support this operation. Lower the
944 // operation to an explicit libcall as appropriate.
945 MVT::ValueType IntPtr = TLI.getPointerTy();
946 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
947 std::vector<std::pair<SDOperand, const Type*> > Args;
948
Reid Spencer6dced922005-01-12 14:53:45 +0000949 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000950 if (Node->getOpcode() == ISD::MEMSET) {
951 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
952 // Extend the ubyte argument to be an int value for the call.
953 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
954 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
955 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
956
957 FnName = "memset";
958 } else if (Node->getOpcode() == ISD::MEMCPY ||
959 Node->getOpcode() == ISD::MEMMOVE) {
960 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
961 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
962 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
963 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
964 } else {
965 assert(0 && "Unknown op!");
966 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000967
Chris Lattner85d70c62005-01-11 05:57:22 +0000968 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +0000969 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +0000970 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
971 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000972 break;
973 }
974 case TargetLowering::Custom:
975 std::vector<SDOperand> Ops;
976 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
977 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
978 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner29dcc712005-05-14 05:50:48 +0000979 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000980 Result = LegalizeOp(Result);
981 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000982 }
983 break;
984 }
Chris Lattner5385db52005-05-09 20:23:03 +0000985
986 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +0000987 Tmp1 = LegalizeOp(Node->getOperand(0));
988 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000989
Chris Lattner86535992005-05-14 07:45:46 +0000990 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
991 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
992 std::vector<SDOperand> Ops;
993 Ops.push_back(Tmp1);
994 Ops.push_back(Tmp2);
995 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
996 } else
Chris Lattner5385db52005-05-09 20:23:03 +0000997 Result = SDOperand(Node, 0);
998 // Since these produce two values, make sure to remember that we legalized
999 // both of them.
1000 AddLegalizedOperand(SDOperand(Node, 0), Result);
1001 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1002 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001003 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001004 Tmp1 = LegalizeOp(Node->getOperand(0));
1005 Tmp2 = LegalizeOp(Node->getOperand(1));
1006 Tmp3 = LegalizeOp(Node->getOperand(2));
1007 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1008 Tmp3 != Node->getOperand(2))
1009 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1010 break;
1011
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001012 case ISD::READIO:
1013 Tmp1 = LegalizeOp(Node->getOperand(0));
1014 Tmp2 = LegalizeOp(Node->getOperand(1));
1015
1016 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1017 case TargetLowering::Custom:
1018 default: assert(0 && "This action not implemented for this operation!");
1019 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001020 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1021 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1022 std::vector<SDOperand> Ops;
1023 Ops.push_back(Tmp1);
1024 Ops.push_back(Tmp2);
1025 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1026 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001027 Result = SDOperand(Node, 0);
1028 break;
1029 case TargetLowering::Expand:
1030 // Replace this with a load from memory.
1031 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1032 Node->getOperand(1), DAG.getSrcValue(NULL));
1033 Result = LegalizeOp(Result);
1034 break;
1035 }
1036
1037 // Since these produce two values, make sure to remember that we legalized
1038 // both of them.
1039 AddLegalizedOperand(SDOperand(Node, 0), Result);
1040 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1041 return Result.getValue(Op.ResNo);
1042
1043 case ISD::WRITEIO:
1044 Tmp1 = LegalizeOp(Node->getOperand(0));
1045 Tmp2 = LegalizeOp(Node->getOperand(1));
1046 Tmp3 = LegalizeOp(Node->getOperand(2));
1047
1048 switch (TLI.getOperationAction(Node->getOpcode(),
1049 Node->getOperand(1).getValueType())) {
1050 case TargetLowering::Custom:
1051 default: assert(0 && "This action not implemented for this operation!");
1052 case TargetLowering::Legal:
1053 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1054 Tmp3 != Node->getOperand(2))
1055 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1056 break;
1057 case TargetLowering::Expand:
1058 // Replace this with a store to memory.
1059 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1060 Node->getOperand(1), Node->getOperand(2),
1061 DAG.getSrcValue(NULL));
1062 Result = LegalizeOp(Result);
1063 break;
1064 }
1065 break;
1066
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001067 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001068 case ISD::SUB_PARTS:
1069 case ISD::SHL_PARTS:
1070 case ISD::SRA_PARTS:
1071 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001072 std::vector<SDOperand> Ops;
1073 bool Changed = false;
1074 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1075 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1076 Changed |= Ops.back() != Node->getOperand(i);
1077 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001078 if (Changed) {
1079 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1080 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1081 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001082
1083 // Since these produce multiple values, make sure to remember that we
1084 // legalized all of them.
1085 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1086 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1087 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001088 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001089
1090 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001091 case ISD::ADD:
1092 case ISD::SUB:
1093 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001094 case ISD::MULHS:
1095 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001096 case ISD::UDIV:
1097 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001098 case ISD::AND:
1099 case ISD::OR:
1100 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001101 case ISD::SHL:
1102 case ISD::SRL:
1103 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001104 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001105 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1106 case Expand: assert(0 && "Not possible");
1107 case Legal:
1108 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1109 break;
1110 case Promote:
1111 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1112 break;
1113 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001114 if (Tmp1 != Node->getOperand(0) ||
1115 Tmp2 != Node->getOperand(1))
1116 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1117 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001118
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001119 case ISD::UREM:
1120 case ISD::SREM:
1121 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1122 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1123 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1124 case TargetLowering::Legal:
1125 if (Tmp1 != Node->getOperand(0) ||
1126 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001127 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001128 Tmp2);
1129 break;
1130 case TargetLowering::Promote:
1131 case TargetLowering::Custom:
1132 assert(0 && "Cannot promote/custom handle this yet!");
1133 case TargetLowering::Expand: {
1134 MVT::ValueType VT = Node->getValueType(0);
1135 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1136 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1137 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1138 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1139 }
1140 break;
1141 }
1142 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001143
Andrew Lenharth5e177822005-05-03 17:19:30 +00001144 case ISD::CTPOP:
1145 case ISD::CTTZ:
1146 case ISD::CTLZ:
1147 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1148 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1149 case TargetLowering::Legal:
1150 if (Tmp1 != Node->getOperand(0))
1151 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1152 break;
1153 case TargetLowering::Promote: {
1154 MVT::ValueType OVT = Tmp1.getValueType();
1155 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001156
1157 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001158 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1159 // Perform the larger operation, then subtract if needed.
1160 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1161 switch(Node->getOpcode())
1162 {
1163 case ISD::CTPOP:
1164 Result = Tmp1;
1165 break;
1166 case ISD::CTTZ:
1167 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnercf5f6b02005-05-12 19:05:01 +00001168 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001169 DAG.getConstant(getSizeInBits(NVT), NVT));
1170 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1171 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1172 break;
1173 case ISD::CTLZ:
1174 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1175 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1176 DAG.getConstant(getSizeInBits(NVT) -
1177 getSizeInBits(OVT), NVT));
1178 break;
1179 }
1180 break;
1181 }
1182 case TargetLowering::Custom:
1183 assert(0 && "Cannot custom handle this yet!");
1184 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001185 switch(Node->getOpcode())
1186 {
1187 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001188 static const uint64_t mask[6] = {
1189 0x5555555555555555ULL, 0x3333333333333333ULL,
1190 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1191 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1192 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001193 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001194 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1195 unsigned len = getSizeInBits(VT);
1196 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001197 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001198 Tmp2 = DAG.getConstant(mask[i], VT);
1199 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001200 Tmp1 = DAG.getNode(ISD::ADD, VT,
1201 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1202 DAG.getNode(ISD::AND, VT,
1203 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1204 Tmp2));
1205 }
1206 Result = Tmp1;
1207 break;
1208 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001209 case ISD::CTLZ: {
1210 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001211 x = x | (x >> 1);
1212 x = x | (x >> 2);
1213 ...
1214 x = x | (x >>16);
1215 x = x | (x >>32); // for 64-bit input
1216 return popcount(~x);
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001217
Chris Lattner56add052005-05-11 18:35:21 +00001218 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1219 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001220 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1221 unsigned len = getSizeInBits(VT);
1222 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1223 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
1224 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
1225 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1226 }
1227 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001228 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001229 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001230 }
1231 case ISD::CTTZ: {
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001232 // for now, we use: { return popcount(~x & (x - 1)); }
1233 // unless the target has ctlz but not ctpop, in which case we use:
1234 // { return 32 - nlz(~x & (x-1)); }
1235 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001236 MVT::ValueType VT = Tmp1.getValueType();
1237 Tmp2 = DAG.getConstant(~0ULL, VT);
1238 Tmp3 = DAG.getNode(ISD::AND, VT,
1239 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1240 DAG.getNode(ISD::SUB, VT, Tmp1,
1241 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001242 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1243 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1244 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
1245 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
1246 DAG.getConstant(getSizeInBits(VT), VT),
1247 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1248 } else {
1249 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1250 }
Chris Lattner72473242005-05-11 05:27:09 +00001251 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001252 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001253 default:
1254 assert(0 && "Cannot expand this yet!");
1255 break;
1256 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001257 break;
1258 }
1259 break;
1260
Chris Lattner13fe99c2005-04-02 05:00:07 +00001261 // Unary operators
1262 case ISD::FABS:
1263 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001264 case ISD::FSQRT:
1265 case ISD::FSIN:
1266 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001267 Tmp1 = LegalizeOp(Node->getOperand(0));
1268 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1269 case TargetLowering::Legal:
1270 if (Tmp1 != Node->getOperand(0))
1271 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1272 break;
1273 case TargetLowering::Promote:
1274 case TargetLowering::Custom:
1275 assert(0 && "Cannot promote/custom handle this yet!");
1276 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001277 switch(Node->getOpcode()) {
1278 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001279 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1280 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1281 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1282 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001283 break;
1284 }
1285 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001286 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1287 MVT::ValueType VT = Node->getValueType(0);
1288 Tmp2 = DAG.getConstantFP(0.0, VT);
1289 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1290 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1291 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1292 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001293 break;
1294 }
1295 case ISD::FSQRT:
1296 case ISD::FSIN:
1297 case ISD::FCOS: {
1298 MVT::ValueType VT = Node->getValueType(0);
1299 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1300 const char *FnName = 0;
1301 switch(Node->getOpcode()) {
1302 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1303 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1304 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1305 default: assert(0 && "Unreachable!");
1306 }
1307 std::vector<std::pair<SDOperand, const Type*> > Args;
1308 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner06bbeb62005-05-11 19:02:11 +00001309 // FIXME: should use ExpandLibCall!
Chris Lattner80026402005-04-30 04:43:14 +00001310 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001311 TLI.LowerCallTo(DAG.getEntryNode(), T, false, CallingConv::C, true,
Chris Lattner80026402005-04-30 04:43:14 +00001312 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1313 Result = LegalizeOp(CallResult.first);
1314 break;
1315 }
1316 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001317 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001318 }
1319 break;
1320 }
1321 break;
1322
1323 // Conversion operators. The source and destination have different types.
Chris Lattnerdc750592005-01-07 07:47:09 +00001324 case ISD::ZERO_EXTEND:
1325 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +00001326 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001327 case ISD::FP_EXTEND:
1328 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001329 case ISD::FP_TO_SINT:
1330 case ISD::FP_TO_UINT:
1331 case ISD::SINT_TO_FP:
1332 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +00001333 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1334 case Legal:
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001335 //still made need to expand if the op is illegal, but the types are legal
1336 if (Node->getOpcode() == ISD::UINT_TO_FP &&
1337 TLI.getOperationAction(Node->getOpcode(),
1338 Node->getOperand(0).getValueType())
1339 == TargetLowering::Expand) {
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001340 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001341 Tmp1 = DAG.getNode(ISD::SINT_TO_FP, Node->getValueType(0),
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001342 Op0);
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001343
1344 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(),
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001345 Op0,
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001346 DAG.getConstant(0,
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001347 Op0.getValueType()));
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001348 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
1349 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
1350 SignSet, Four, Zero);
1351 uint64_t FF = 0x5f800000ULL;
1352 if (TLI.isLittleEndian()) FF <<= 32;
1353 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
1354
1355 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
1356 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
1357 TLI.getPointerTy());
1358 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
1359 SDOperand FudgeInReg;
1360 if (Node->getValueType(0) == MVT::f32)
1361 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
1362 DAG.getSrcValue(NULL));
1363 else {
1364 assert(Node->getValueType(0) == MVT::f64 && "Unexpected conversion");
Andrew Lenharthbe3a74c2005-07-02 20:58:53 +00001365 FudgeInReg =
1366 LegalizeOp(DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
1367 CPIdx, DAG.getSrcValue(NULL), MVT::f32));
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001368 }
1369 Result = DAG.getNode(ISD::ADD, Node->getValueType(0), Tmp1, FudgeInReg);
1370 break;
1371 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001372 Tmp1 = LegalizeOp(Node->getOperand(0));
1373 if (Tmp1 != Node->getOperand(0))
1374 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1375 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001376 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001377 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1378 Node->getOpcode() == ISD::UINT_TO_FP) {
1379 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1380 Node->getValueType(0), Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001381 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001382 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1383 // In the expand case, we must be dealing with a truncate, because
1384 // otherwise the result would be larger than the source.
1385 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +00001386
Chris Lattner13fe99c2005-04-02 05:00:07 +00001387 // Since the result is legal, we should just be able to truncate the low
1388 // part of the source.
1389 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1390 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001391 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001392 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001393
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001394 case Promote:
1395 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001396 case ISD::ZERO_EXTEND:
1397 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001398 // NOTE: Any extend would work here...
1399 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001400 Result = DAG.getZeroExtendInReg(Result,
1401 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001402 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001403 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001404 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001405 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001406 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001407 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001408 Result,
1409 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001410 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001411 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001412 Result = PromoteOp(Node->getOperand(0));
1413 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1414 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001415 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001416 Result = PromoteOp(Node->getOperand(0));
1417 if (Result.getValueType() != Op.getValueType())
1418 // Dynamically dead while we have only 2 FP types.
1419 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1420 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001421 case ISD::FP_ROUND:
1422 case ISD::FP_TO_SINT:
1423 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001424 Result = PromoteOp(Node->getOperand(0));
1425 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1426 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001427 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001428 Result = PromoteOp(Node->getOperand(0));
1429 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001430 Result,
1431 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner3ba56b32005-01-16 05:06:12 +00001432 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1433 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001434 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001435 Result = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001436 Result = DAG.getZeroExtendInReg(Result,
1437 Node->getOperand(0).getValueType());
Chris Lattner3ba56b32005-01-16 05:06:12 +00001438 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1439 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001440 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001441 }
1442 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001443 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001444 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001445 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001446 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001447
1448 // If this operation is not supported, convert it to a shl/shr or load/store
1449 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001450 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1451 default: assert(0 && "This action not supported for this op yet!");
1452 case TargetLowering::Legal:
1453 if (Tmp1 != Node->getOperand(0))
1454 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1455 ExtraVT);
1456 break;
1457 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001458 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001459 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001460 // NOTE: we could fall back on load/store here too for targets without
1461 // SAR. However, it is doubtful that any exist.
1462 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1463 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001464 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001465 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1466 Node->getOperand(0), ShiftCst);
1467 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1468 Result, ShiftCst);
1469 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1470 // The only way we can lower this is to turn it into a STORETRUNC,
1471 // EXTLOAD pair, targetting a temporary location (a stack slot).
1472
1473 // NOTE: there is a choice here between constantly creating new stack
1474 // slots and always reusing the same one. We currently always create
1475 // new ones, as reuse may inhibit scheduling.
1476 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1477 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1478 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1479 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001480 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001481 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1482 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1483 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001484 Node->getOperand(0), StackSlot,
1485 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001486 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001487 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001488 } else {
1489 assert(0 && "Unknown op");
1490 }
1491 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001492 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001493 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001494 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001495 }
Chris Lattner99222f72005-01-15 07:15:18 +00001496 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001497
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001498 // Note that LegalizeOp may be reentered even from single-use nodes, which
1499 // means that we always must cache transformed nodes.
1500 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001501 return Result;
1502}
1503
Chris Lattner4d978642005-01-15 22:16:26 +00001504/// PromoteOp - Given an operation that produces a value in an invalid type,
1505/// promote it to compute the value into a larger type. The produced value will
1506/// have the correct bits for the low portion of the register, but no guarantee
1507/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001508SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1509 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001510 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001511 assert(getTypeAction(VT) == Promote &&
1512 "Caller should expand or legalize operands that are not promotable!");
1513 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1514 "Cannot promote to smaller type!");
1515
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001516 SDOperand Tmp1, Tmp2, Tmp3;
1517
1518 SDOperand Result;
1519 SDNode *Node = Op.Val;
1520
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001521 if (!Node->hasOneUse()) {
1522 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1523 if (I != PromotedNodes.end()) return I->second;
1524 } else {
1525 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1526 }
1527
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001528 // Promotion needs an optimization step to clean up after it, and is not
1529 // careful to avoid operations the target does not support. Make sure that
1530 // all generated operations are legalized in the next iteration.
1531 NeedsAnotherIteration = true;
1532
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001533 switch (Node->getOpcode()) {
1534 default:
1535 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1536 assert(0 && "Do not know how to promote this operator!");
1537 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001538 case ISD::UNDEF:
1539 Result = DAG.getNode(ISD::UNDEF, NVT);
1540 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001541 case ISD::Constant:
1542 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1543 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1544 break;
1545 case ISD::ConstantFP:
1546 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1547 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1548 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001549 case ISD::CopyFromReg:
1550 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1551 Node->getOperand(0));
1552 // Remember that we legalized the chain.
1553 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1554 break;
1555
Chris Lattner2cb338d2005-01-18 02:59:52 +00001556 case ISD::SETCC:
1557 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1558 "SetCC type is not legal??");
1559 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1560 TLI.getSetCCResultTy(), Node->getOperand(0),
1561 Node->getOperand(1));
1562 Result = LegalizeOp(Result);
1563 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001564
1565 case ISD::TRUNCATE:
1566 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1567 case Legal:
1568 Result = LegalizeOp(Node->getOperand(0));
1569 assert(Result.getValueType() >= NVT &&
1570 "This truncation doesn't make sense!");
1571 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1572 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1573 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001574 case Promote:
1575 // The truncation is not required, because we don't guarantee anything
1576 // about high bits anyway.
1577 Result = PromoteOp(Node->getOperand(0));
1578 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001579 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001580 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1581 // Truncate the low part of the expanded value to the result type
Misha Brukman835702a2005-04-21 22:36:52 +00001582 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001583 }
1584 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001585 case ISD::SIGN_EXTEND:
1586 case ISD::ZERO_EXTEND:
1587 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1588 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1589 case Legal:
1590 // Input is legal? Just do extend all the way to the larger type.
1591 Result = LegalizeOp(Node->getOperand(0));
1592 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1593 break;
1594 case Promote:
1595 // Promote the reg if it's smaller.
1596 Result = PromoteOp(Node->getOperand(0));
1597 // The high bits are not guaranteed to be anything. Insert an extend.
1598 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001599 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00001600 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00001601 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001602 Result = DAG.getZeroExtendInReg(Result,
1603 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001604 break;
1605 }
1606 break;
1607
1608 case ISD::FP_EXTEND:
1609 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1610 case ISD::FP_ROUND:
1611 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1612 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1613 case Promote: assert(0 && "Unreachable with 2 FP types!");
1614 case Legal:
1615 // Input is legal? Do an FP_ROUND_INREG.
1616 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001617 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1618 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001619 break;
1620 }
1621 break;
1622
1623 case ISD::SINT_TO_FP:
1624 case ISD::UINT_TO_FP:
1625 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1626 case Legal:
1627 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001628 // No extra round required here.
1629 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001630 break;
1631
1632 case Promote:
1633 Result = PromoteOp(Node->getOperand(0));
1634 if (Node->getOpcode() == ISD::SINT_TO_FP)
1635 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001636 Result,
1637 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00001638 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001639 Result = DAG.getZeroExtendInReg(Result,
1640 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001641 // No extra round required here.
1642 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001643 break;
1644 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001645 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1646 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001647 // Round if we cannot tolerate excess precision.
1648 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001649 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1650 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00001651 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001652 }
Chris Lattner4d978642005-01-15 22:16:26 +00001653 break;
1654
1655 case ISD::FP_TO_SINT:
1656 case ISD::FP_TO_UINT:
1657 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1658 case Legal:
1659 Tmp1 = LegalizeOp(Node->getOperand(0));
1660 break;
1661 case Promote:
1662 // The input result is prerounded, so we don't have to do anything
1663 // special.
1664 Tmp1 = PromoteOp(Node->getOperand(0));
1665 break;
1666 case Expand:
1667 assert(0 && "not implemented");
1668 }
1669 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1670 break;
1671
Chris Lattner13fe99c2005-04-02 05:00:07 +00001672 case ISD::FABS:
1673 case ISD::FNEG:
1674 Tmp1 = PromoteOp(Node->getOperand(0));
1675 assert(Tmp1.getValueType() == NVT);
1676 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1677 // NOTE: we do not have to do any extra rounding here for
1678 // NoExcessFPPrecision, because we know the input will have the appropriate
1679 // precision, and these operations don't modify precision at all.
1680 break;
1681
Chris Lattner9d6fa982005-04-28 21:44:33 +00001682 case ISD::FSQRT:
1683 case ISD::FSIN:
1684 case ISD::FCOS:
1685 Tmp1 = PromoteOp(Node->getOperand(0));
1686 assert(Tmp1.getValueType() == NVT);
1687 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1688 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001689 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1690 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00001691 break;
1692
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001693 case ISD::AND:
1694 case ISD::OR:
1695 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001696 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001697 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001698 case ISD::MUL:
1699 // The input may have strange things in the top bits of the registers, but
1700 // these operations don't care. They may have wierd bits going out, but
1701 // that too is okay if they are integer operations.
1702 Tmp1 = PromoteOp(Node->getOperand(0));
1703 Tmp2 = PromoteOp(Node->getOperand(1));
1704 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1705 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1706
1707 // However, if this is a floating point operation, they will give excess
1708 // precision that we may not be able to tolerate. If we DO allow excess
1709 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001710 // FIXME: Why would we need to round FP ops more than integer ones?
1711 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001712 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001713 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1714 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001715 break;
1716
Chris Lattner4d978642005-01-15 22:16:26 +00001717 case ISD::SDIV:
1718 case ISD::SREM:
1719 // These operators require that their input be sign extended.
1720 Tmp1 = PromoteOp(Node->getOperand(0));
1721 Tmp2 = PromoteOp(Node->getOperand(1));
1722 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00001723 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1724 DAG.getValueType(VT));
1725 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1726 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001727 }
1728 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1729
1730 // Perform FP_ROUND: this is probably overly pessimistic.
1731 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001732 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1733 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001734 break;
1735
1736 case ISD::UDIV:
1737 case ISD::UREM:
1738 // These operators require that their input be zero extended.
1739 Tmp1 = PromoteOp(Node->getOperand(0));
1740 Tmp2 = PromoteOp(Node->getOperand(1));
1741 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00001742 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1743 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001744 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1745 break;
1746
1747 case ISD::SHL:
1748 Tmp1 = PromoteOp(Node->getOperand(0));
1749 Tmp2 = LegalizeOp(Node->getOperand(1));
1750 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1751 break;
1752 case ISD::SRA:
1753 // The input value must be properly sign extended.
1754 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001755 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1756 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001757 Tmp2 = LegalizeOp(Node->getOperand(1));
1758 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1759 break;
1760 case ISD::SRL:
1761 // The input value must be properly zero extended.
1762 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001763 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001764 Tmp2 = LegalizeOp(Node->getOperand(1));
1765 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1766 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001767 case ISD::LOAD:
1768 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1769 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001770 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001771 if (MVT::isInteger(NVT))
Chris Lattner5385db52005-05-09 20:23:03 +00001772 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1773 VT);
Chris Lattner391a3512005-04-10 17:40:35 +00001774 else
Chris Lattner5385db52005-05-09 20:23:03 +00001775 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1776 VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001777
1778 // Remember that we legalized the chain.
1779 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1780 break;
1781 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001782 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1783 case Expand: assert(0 && "It's impossible to expand bools");
1784 case Legal:
1785 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1786 break;
1787 case Promote:
1788 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1789 break;
1790 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001791 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1792 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1793 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1794 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00001795 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001796 case ISD::CALL: {
1797 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1798 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1799
Chris Lattner3d95c142005-01-19 20:24:35 +00001800 std::vector<SDOperand> Ops;
1801 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1802 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1803
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001804 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1805 "Can only promote single result calls");
1806 std::vector<MVT::ValueType> RetTyVTs;
1807 RetTyVTs.reserve(2);
1808 RetTyVTs.push_back(NVT);
1809 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00001810 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
1811 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001812 Result = SDOperand(NC, 0);
1813
1814 // Insert the new chain mapping.
1815 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1816 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001817 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00001818 case ISD::CTPOP:
1819 case ISD::CTTZ:
1820 case ISD::CTLZ:
1821 Tmp1 = Node->getOperand(0);
1822 //Zero extend the argument
1823 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1824 // Perform the larger operation, then subtract if needed.
1825 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1826 switch(Node->getOpcode())
1827 {
1828 case ISD::CTPOP:
1829 Result = Tmp1;
1830 break;
1831 case ISD::CTTZ:
1832 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1833 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1834 DAG.getConstant(getSizeInBits(NVT), NVT));
1835 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1836 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1837 break;
1838 case ISD::CTLZ:
1839 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1840 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1841 DAG.getConstant(getSizeInBits(NVT) -
1842 getSizeInBits(VT), NVT));
1843 break;
1844 }
1845 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001846 }
1847
1848 assert(Result.Val && "Didn't set a result!");
1849 AddPromotedOperand(Op, Result);
1850 return Result;
1851}
Chris Lattnerdc750592005-01-07 07:47:09 +00001852
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001853/// ExpandAddSub - Find a clever way to expand this add operation into
1854/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001855void SelectionDAGLegalize::
1856ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1857 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001858 // Expand the subcomponents.
1859 SDOperand LHSL, LHSH, RHSL, RHSH;
1860 ExpandOp(LHS, LHSL, LHSH);
1861 ExpandOp(RHS, RHSL, RHSH);
1862
Chris Lattner8ffd0042005-04-11 20:29:59 +00001863 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00001864 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
1865 if (LHSL.getValueType() == MVT::i32) {
1866 SDOperand LowEl;
1867 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1868 if (C->getValue() == 0)
1869 LowEl = RHSL;
1870 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1871 if (C->getValue() == 0)
1872 LowEl = LHSL;
1873 if (LowEl.Val) {
1874 // Turn this into an add/sub of the high part only.
1875 SDOperand HiEl =
1876 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1877 LowEl.getValueType(), LHSH, RHSH);
1878 Lo = LowEl;
1879 Hi = HiEl;
1880 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00001881 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001882 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00001883
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001884 std::vector<SDOperand> Ops;
1885 Ops.push_back(LHSL);
1886 Ops.push_back(LHSH);
1887 Ops.push_back(RHSL);
1888 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00001889
1890 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
1891 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001892 Hi = Lo.getValue(1);
1893}
1894
Chris Lattner4157c412005-04-02 04:00:59 +00001895void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1896 SDOperand Op, SDOperand Amt,
1897 SDOperand &Lo, SDOperand &Hi) {
1898 // Expand the subcomponents.
1899 SDOperand LHSL, LHSH;
1900 ExpandOp(Op, LHSL, LHSH);
1901
1902 std::vector<SDOperand> Ops;
1903 Ops.push_back(LHSL);
1904 Ops.push_back(LHSH);
1905 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00001906 std::vector<MVT::ValueType> VTs;
1907 VTs.push_back(LHSL.getValueType());
1908 VTs.push_back(LHSH.getValueType());
1909 VTs.push_back(Amt.getValueType());
1910 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00001911 Hi = Lo.getValue(1);
1912}
1913
1914
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001915/// ExpandShift - Try to find a clever way to expand this shift operation out to
1916/// smaller elements. If we can't find a way that is more efficient than a
1917/// libcall on this target, return false. Otherwise, return true with the
1918/// low-parts expanded into Lo and Hi.
1919bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1920 SDOperand &Lo, SDOperand &Hi) {
1921 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1922 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00001923
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001924 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00001925 SDOperand ShAmt = LegalizeOp(Amt);
1926 MVT::ValueType ShTy = ShAmt.getValueType();
1927 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1928 unsigned NVTBits = MVT::getSizeInBits(NVT);
1929
1930 // Handle the case when Amt is an immediate. Other cases are currently broken
1931 // and are disabled.
1932 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1933 unsigned Cst = CN->getValue();
1934 // Expand the incoming operand to be shifted, so that we have its parts
1935 SDOperand InL, InH;
1936 ExpandOp(Op, InL, InH);
1937 switch(Opc) {
1938 case ISD::SHL:
1939 if (Cst > VTBits) {
1940 Lo = DAG.getConstant(0, NVT);
1941 Hi = DAG.getConstant(0, NVT);
1942 } else if (Cst > NVTBits) {
1943 Lo = DAG.getConstant(0, NVT);
1944 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001945 } else if (Cst == NVTBits) {
1946 Lo = DAG.getConstant(0, NVT);
1947 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00001948 } else {
1949 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1950 Hi = DAG.getNode(ISD::OR, NVT,
1951 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1952 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1953 }
1954 return true;
1955 case ISD::SRL:
1956 if (Cst > VTBits) {
1957 Lo = DAG.getConstant(0, NVT);
1958 Hi = DAG.getConstant(0, NVT);
1959 } else if (Cst > NVTBits) {
1960 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1961 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00001962 } else if (Cst == NVTBits) {
1963 Lo = InH;
1964 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00001965 } else {
1966 Lo = DAG.getNode(ISD::OR, NVT,
1967 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1968 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1969 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1970 }
1971 return true;
1972 case ISD::SRA:
1973 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001974 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001975 DAG.getConstant(NVTBits-1, ShTy));
1976 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001977 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001978 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00001979 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001980 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001981 } else if (Cst == NVTBits) {
1982 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00001983 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00001984 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00001985 } else {
1986 Lo = DAG.getNode(ISD::OR, NVT,
1987 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1988 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1989 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1990 }
1991 return true;
1992 }
1993 }
1994 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1995 // so disable it for now. Currently targets are handling this via SHL_PARTS
1996 // and friends.
1997 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001998
1999 // If we have an efficient select operation (or if the selects will all fold
2000 // away), lower to some complex code, otherwise just emit the libcall.
2001 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2002 !isa<ConstantSDNode>(Amt))
2003 return false;
2004
2005 SDOperand InL, InH;
2006 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002007 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2008 DAG.getConstant(NVTBits, ShTy), ShAmt);
2009
Chris Lattner4d25c042005-01-20 20:29:23 +00002010 // Compare the unmasked shift amount against 32.
2011 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
2012 DAG.getConstant(NVTBits, ShTy));
2013
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002014 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2015 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2016 DAG.getConstant(NVTBits-1, ShTy));
2017 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2018 DAG.getConstant(NVTBits-1, ShTy));
2019 }
2020
2021 if (Opc == ISD::SHL) {
2022 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2023 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2024 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002025 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002026
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002027 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2028 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2029 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002030 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
2031 DAG.getSetCC(ISD::SETEQ,
2032 TLI.getSetCCResultTy(), NAmt,
2033 DAG.getConstant(32, ShTy)),
2034 DAG.getConstant(0, NVT),
2035 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002036 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002037 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002038 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002039 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002040
2041 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002042 if (Opc == ISD::SRA)
2043 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2044 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002045 else
2046 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002047 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002048 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002049 }
2050 return true;
2051}
Chris Lattneraac464e2005-01-21 06:05:23 +00002052
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002053/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2054/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002055/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002056static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002057 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
2058
Chris Lattner2dce7032005-05-12 23:24:06 +00002059 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002060 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002061 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002062 Found = Node;
2063 return;
2064 }
2065
2066 // Otherwise, scan the operands of Node to see if any of them is a call.
2067 assert(Node->getNumOperands() != 0 &&
2068 "All leaves should have depth equal to the entry node!");
2069 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002070 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002071
2072 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002073 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002074 Found);
2075}
2076
2077
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002078/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2079/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002080/// than Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002081static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002082 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
2083
Chris Lattner2dce7032005-05-12 23:24:06 +00002084 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002085 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002086 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002087 Found = Node;
2088 return;
2089 }
2090
2091 // Otherwise, scan the operands of Node to see if any of them is a call.
2092 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2093 if (UI == E) return;
2094 for (--E; UI != E; ++UI)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002095 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002096
2097 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002098 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002099}
2100
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002101/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002102/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002103static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002104 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002105 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002106 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002107 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002108
2109 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002110 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002111
Chris Lattner4add7e32005-01-23 04:42:50 +00002112 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002113 if (TheChain.getValueType() != MVT::Other)
2114 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002115 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002116
2117 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner4add7e32005-01-23 04:42:50 +00002118 E = Node->use_end(); ; ++UI) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002119 assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!");
Misha Brukman835702a2005-04-21 22:36:52 +00002120
Chris Lattner4add7e32005-01-23 04:42:50 +00002121 // Make sure to only follow users of our token chain.
2122 SDNode *User = *UI;
2123 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2124 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002125 if (SDNode *Result = FindCallSeqEnd(User))
2126 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002127 }
2128 assert(0 && "Unreachable");
2129 abort();
2130}
2131
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002132/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002133/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002134static SDNode *FindCallSeqStart(SDNode *Node) {
2135 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002136 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002137
2138 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2139 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002140 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002141}
2142
2143
Chris Lattner4add7e32005-01-23 04:42:50 +00002144/// FindInputOutputChains - If we are replacing an operation with a call we need
2145/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002146/// properly serialize the calls in the block. The returned operand is the
2147/// input chain value for the new call (e.g. the entry node or the previous
2148/// call), and OutChain is set to be the chain node to update to point to the
2149/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002150static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2151 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002152 SDNode *LatestCallSeqStart = Entry.Val;
2153 SDNode *LatestCallSeqEnd = 0;
2154 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2155 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002156
Chris Lattner2dce7032005-05-12 23:24:06 +00002157 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002158 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002159 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2160 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002161 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2162 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002163 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002164 LatestCallSeqEnd = Entry.Val;
2165 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002166
Chris Lattner06bbeb62005-05-11 19:02:11 +00002167 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002168 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002169 OutChain = 0;
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002170 FindEarliestCallSeqEnd(OpNode, OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002171
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002172 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002173 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002174 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002175
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002176 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002177}
2178
Chris Lattner06bbeb62005-05-11 19:02:11 +00002179/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002180void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2181 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002182 // Nothing to splice it into?
2183 if (OutChain == 0) return;
2184
2185 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2186 //OutChain->dump();
2187
2188 // Form a token factor node merging the old inval and the new inval.
2189 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2190 OutChain->getOperand(0));
2191 // Change the node to refer to the new token.
2192 OutChain->setAdjCallChain(InToken);
2193}
Chris Lattner4add7e32005-01-23 04:42:50 +00002194
2195
Chris Lattneraac464e2005-01-21 06:05:23 +00002196// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2197// does not fit into a register, return the lo part and set the hi part to the
2198// by-reg argument. If it does fit into a single register, return the result
2199// and leave the Hi part unset.
2200SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2201 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002202 SDNode *OutChain;
2203 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2204 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002205 if (InChain.Val == 0)
2206 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002207
Chris Lattneraac464e2005-01-21 06:05:23 +00002208 TargetLowering::ArgListTy Args;
2209 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2210 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2211 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2212 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2213 }
2214 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002215
Chris Lattner06bbeb62005-05-11 19:02:11 +00002216 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002217 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002218 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002219 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2220 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002221 SpliceCallInto(CallInfo.second, OutChain);
2222
2223 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002224
2225 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002226 default: assert(0 && "Unknown thing");
2227 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002228 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002229 case Promote:
2230 assert(0 && "Cannot promote this yet!");
2231 case Expand:
2232 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002233 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002234 return Lo;
2235 }
2236}
2237
Chris Lattner4add7e32005-01-23 04:42:50 +00002238
Chris Lattneraac464e2005-01-21 06:05:23 +00002239/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2240/// destination type is legal.
2241SDOperand SelectionDAGLegalize::
2242ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2243 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2244 assert(getTypeAction(Source.getValueType()) == Expand &&
2245 "This is not an expansion!");
2246 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2247
Chris Lattner06bbeb62005-05-11 19:02:11 +00002248 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002249 assert(Source.getValueType() == MVT::i64 &&
2250 "This only works for 64-bit -> FP");
2251 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2252 // incoming integer is set. To handle this, we dynamically test to see if
2253 // it is set, and, if so, add a fudge factor.
2254 SDOperand Lo, Hi;
2255 ExpandOp(Source, Lo, Hi);
2256
Chris Lattner2a4f7312005-05-13 04:45:13 +00002257 // If this is unsigned, and not supported, first perform the conversion to
2258 // signed, then adjust the result if the sign bit is set.
2259 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2260 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2261
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002262 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2263 DAG.getConstant(0, Hi.getValueType()));
2264 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2265 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2266 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002267 uint64_t FF = 0x5f800000ULL;
2268 if (TLI.isLittleEndian()) FF <<= 32;
2269 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002270
2271 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2272 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2273 TLI.getPointerTy());
2274 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2275 SDOperand FudgeInReg;
2276 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002277 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2278 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002279 else {
2280 assert(DestTy == MVT::f64 && "Unexpected conversion");
2281 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002282 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002283 }
2284 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002285 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002286
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002287 // Check to see if the target has a custom way to lower this. If so, use it.
2288 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2289 default: assert(0 && "This action not implemented for this operation!");
2290 case TargetLowering::Legal:
2291 case TargetLowering::Expand:
2292 break; // This case is handled below.
2293 case TargetLowering::Custom:
2294 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner29dcc712005-05-14 05:50:48 +00002295 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002296 }
2297
Chris Lattner153587e2005-05-12 07:00:44 +00002298 // Expand the source, then glue it back together for the call. We must expand
2299 // the source in case it is shared (this pass of legalize must traverse it).
2300 SDOperand SrcLo, SrcHi;
2301 ExpandOp(Source, SrcLo, SrcHi);
2302 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2303
Chris Lattner06bbeb62005-05-11 19:02:11 +00002304 SDNode *OutChain = 0;
2305 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2306 DAG.getEntryNode());
2307 const char *FnName = 0;
2308 if (DestTy == MVT::f32)
2309 FnName = "__floatdisf";
2310 else {
2311 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2312 FnName = "__floatdidf";
2313 }
2314
Chris Lattneraac464e2005-01-21 06:05:23 +00002315 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2316
2317 TargetLowering::ArgListTy Args;
2318 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002319
Chris Lattneraac464e2005-01-21 06:05:23 +00002320 Args.push_back(std::make_pair(Source, ArgTy));
2321
2322 // We don't care about token chains for libcalls. We just use the entry
2323 // node as our input and ignore the output chain. This allows us to place
2324 // calls wherever we need them to satisfy data dependences.
2325 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002326
2327 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002328 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2329 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002330
Chris Lattnera5bf1032005-05-12 04:49:08 +00002331 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002332 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002333}
Misha Brukman835702a2005-04-21 22:36:52 +00002334
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002335
2336
Chris Lattnerdc750592005-01-07 07:47:09 +00002337/// ExpandOp - Expand the specified SDOperand into its two component pieces
2338/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2339/// LegalizeNodes map is filled in for any results that are not expanded, the
2340/// ExpandedNodes map is filled in for any results that are expanded, and the
2341/// Lo/Hi values are returned.
2342void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2343 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002344 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002345 SDNode *Node = Op.Val;
2346 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2347 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2348 assert(MVT::isInteger(NVT) && NVT < VT &&
2349 "Cannot expand to FP value or to larger int value!");
2350
2351 // If there is more than one use of this, see if we already expanded it.
2352 // There is no use remembering values that only have a single use, as the map
2353 // entries will never be reused.
2354 if (!Node->hasOneUse()) {
2355 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2356 = ExpandedNodes.find(Op);
2357 if (I != ExpandedNodes.end()) {
2358 Lo = I->second.first;
2359 Hi = I->second.second;
2360 return;
2361 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002362 } else {
2363 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002364 }
2365
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002366 // Expanding to multiple registers needs to perform an optimization step, and
2367 // is not careful to avoid operations the target does not support. Make sure
2368 // that all generated operations are legalized in the next iteration.
2369 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002370
Chris Lattnerdc750592005-01-07 07:47:09 +00002371 switch (Node->getOpcode()) {
2372 default:
2373 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2374 assert(0 && "Do not know how to expand this operator!");
2375 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002376 case ISD::UNDEF:
2377 Lo = DAG.getNode(ISD::UNDEF, NVT);
2378 Hi = DAG.getNode(ISD::UNDEF, NVT);
2379 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002380 case ISD::Constant: {
2381 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2382 Lo = DAG.getConstant(Cst, NVT);
2383 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2384 break;
2385 }
2386
2387 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00002388 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00002389 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00002390 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2391 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002392
Chris Lattner3b8e7192005-01-14 22:38:01 +00002393 // Remember that we legalized the chain.
2394 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2395
Chris Lattnerdc750592005-01-07 07:47:09 +00002396 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2397 break;
2398 }
2399
Chris Lattner32e08b72005-03-28 22:03:13 +00002400 case ISD::BUILD_PAIR:
2401 // Legalize both operands. FIXME: in the future we should handle the case
2402 // where the two elements are not legal.
2403 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2404 Lo = LegalizeOp(Node->getOperand(0));
2405 Hi = LegalizeOp(Node->getOperand(1));
2406 break;
2407
Chris Lattner55e9cde2005-05-11 04:51:16 +00002408 case ISD::CTPOP:
2409 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002410 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2411 DAG.getNode(ISD::CTPOP, NVT, Lo),
2412 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002413 Hi = DAG.getConstant(0, NVT);
2414 break;
2415
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002416 case ISD::CTLZ: {
2417 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002418 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002419 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2420 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2421 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2422 HLZ, BitsC);
2423 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2424 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2425
2426 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2427 Hi = DAG.getConstant(0, NVT);
2428 break;
2429 }
2430
2431 case ISD::CTTZ: {
2432 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002433 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002434 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2435 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2436 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2437 LTZ, BitsC);
2438 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2439 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2440
2441 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2442 Hi = DAG.getConstant(0, NVT);
2443 break;
2444 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002445
Chris Lattnerdc750592005-01-07 07:47:09 +00002446 case ISD::LOAD: {
2447 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2448 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002449 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002450
2451 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002452 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002453 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2454 getIntPtrConstant(IncrementSize));
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002455 //Is this safe? declaring that the two parts of the split load
2456 //are from the same instruction?
2457 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002458
2459 // Build a factor node to remember that this load is independent of the
2460 // other one.
2461 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2462 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002463
Chris Lattnerdc750592005-01-07 07:47:09 +00002464 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002465 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002466 if (!TLI.isLittleEndian())
2467 std::swap(Lo, Hi);
2468 break;
2469 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002470 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002471 case ISD::CALL: {
2472 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2473 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2474
Chris Lattner3d95c142005-01-19 20:24:35 +00002475 bool Changed = false;
2476 std::vector<SDOperand> Ops;
2477 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2478 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2479 Changed |= Ops.back() != Node->getOperand(i);
2480 }
2481
Chris Lattnerdc750592005-01-07 07:47:09 +00002482 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2483 "Can only expand a call once so far, not i64 -> i16!");
2484
2485 std::vector<MVT::ValueType> RetTyVTs;
2486 RetTyVTs.reserve(3);
2487 RetTyVTs.push_back(NVT);
2488 RetTyVTs.push_back(NVT);
2489 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002490 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2491 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002492 Lo = SDOperand(NC, 0);
2493 Hi = SDOperand(NC, 1);
2494
2495 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002496 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002497 break;
2498 }
2499 case ISD::AND:
2500 case ISD::OR:
2501 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2502 SDOperand LL, LH, RL, RH;
2503 ExpandOp(Node->getOperand(0), LL, LH);
2504 ExpandOp(Node->getOperand(1), RL, RH);
2505 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2506 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2507 break;
2508 }
2509 case ISD::SELECT: {
2510 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002511
2512 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2513 case Expand: assert(0 && "It's impossible to expand bools");
2514 case Legal:
2515 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2516 break;
2517 case Promote:
2518 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2519 break;
2520 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002521 ExpandOp(Node->getOperand(1), LL, LH);
2522 ExpandOp(Node->getOperand(2), RL, RH);
2523 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2524 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2525 break;
2526 }
2527 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002528 SDOperand In;
2529 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2530 case Expand: assert(0 && "expand-expand not implemented yet!");
2531 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2532 case Promote:
2533 In = PromoteOp(Node->getOperand(0));
2534 // Emit the appropriate sign_extend_inreg to get the value we want.
2535 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002536 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00002537 break;
2538 }
2539
Chris Lattnerdc750592005-01-07 07:47:09 +00002540 // The low part is just a sign extension of the input (which degenerates to
2541 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002542 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002543
Chris Lattnerdc750592005-01-07 07:47:09 +00002544 // The high part is obtained by SRA'ing all but one of the bits of the lo
2545 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002546 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002547 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2548 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002549 break;
2550 }
Chris Lattner47844892005-04-03 23:41:52 +00002551 case ISD::ZERO_EXTEND: {
2552 SDOperand In;
2553 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2554 case Expand: assert(0 && "expand-expand not implemented yet!");
2555 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2556 case Promote:
2557 In = PromoteOp(Node->getOperand(0));
2558 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00002559 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00002560 break;
2561 }
2562
Chris Lattnerdc750592005-01-07 07:47:09 +00002563 // The low part is just a zero extension of the input (which degenerates to
2564 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002565 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002566
Chris Lattnerdc750592005-01-07 07:47:09 +00002567 // The high part is just a zero.
2568 Hi = DAG.getConstant(0, NVT);
2569 break;
Chris Lattner47844892005-04-03 23:41:52 +00002570 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002571 // These operators cannot be expanded directly, emit them as calls to
2572 // library functions.
2573 case ISD::FP_TO_SINT:
2574 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002575 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002576 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002577 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002578 break;
2579 case ISD::FP_TO_UINT:
2580 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002581 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002582 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002583 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002584 break;
2585
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002586 case ISD::SHL:
2587 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002588 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002589 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002590
2591 // If this target supports SHL_PARTS, use it.
2592 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002593 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2594 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002595 break;
2596 }
2597
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002598 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002599 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002600 break;
2601
2602 case ISD::SRA:
2603 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002604 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002605 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002606
2607 // If this target supports SRA_PARTS, use it.
2608 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002609 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2610 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002611 break;
2612 }
2613
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002614 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002615 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002616 break;
2617 case ISD::SRL:
2618 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002619 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002620 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002621
2622 // If this target supports SRL_PARTS, use it.
2623 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002624 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2625 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002626 break;
2627 }
2628
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002629 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002630 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002631 break;
2632
Misha Brukman835702a2005-04-21 22:36:52 +00002633 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002634 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2635 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002636 break;
2637 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002638 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2639 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002640 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002641 case ISD::MUL: {
2642 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2643 SDOperand LL, LH, RL, RH;
2644 ExpandOp(Node->getOperand(0), LL, LH);
2645 ExpandOp(Node->getOperand(1), RL, RH);
2646 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2647 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2648 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2649 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2650 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2651 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2652 } else {
2653 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2654 }
2655 break;
2656 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002657 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2658 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2659 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2660 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002661 }
2662
2663 // Remember in a map if the values will be reused later.
2664 if (!Node->hasOneUse()) {
2665 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2666 std::make_pair(Lo, Hi))).second;
2667 assert(isNew && "Value already expanded?!?");
2668 }
2669}
2670
2671
2672// SelectionDAG::Legalize - This is the entry point for the file.
2673//
Chris Lattner4add7e32005-01-23 04:42:50 +00002674void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002675 /// run - This is the main entry point to this class.
2676 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002677 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002678}
2679