blob: a511202a534b6e47d438ca6f6aa4c139f4edd8ac [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-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 Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-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 Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000021#include "llvm/Constants.h"
22#include <iostream>
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27/// hacks on it until the target machine can handle it. This involves
28/// eliminating value sizes the machine cannot handle (promoting small sizes to
29/// large sizes or splitting up large values into small values) as well as
30/// eliminating operations the machine cannot handle.
31///
32/// This code also does a small amount of optimization and recognition of idioms
33/// as part of its processing. For example, if a target does not support a
34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35/// will attempt merge setcc and brc instructions into brcc's.
36///
37namespace {
38class SelectionDAGLegalize {
39 TargetLowering &TLI;
40 SelectionDAG &DAG;
41
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
44 enum LegalizeAction {
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
48 };
49
Chris Lattner3e928bb2005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
54
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
60
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
65
Chris Lattner03c85462005-01-15 05:21:40 +000066 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
70
Chris Lattner3e928bb2005-01-07 07:47:09 +000071 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
74 /// more than once.
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
76
Chris Lattner8afc48e2005-01-07 22:28:47 +000077 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
80 }
Chris Lattner03c85462005-01-15 05:21:40 +000081 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
84 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000085
Chris Lattner3e928bb2005-01-07 07:47:09 +000086public:
87
Chris Lattner9c32d3b2005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000089
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
95 void Run() {
96 do {
97 NeedsAnotherIteration = false;
98 LegalizeDAG();
99 } while (NeedsAnotherIteration);
100 }
101
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
107 }
108
109 /// isTypeLegal - Return true if this type is legal on this target.
110 ///
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
113 }
114
115private:
116 void LegalizeDAG();
117
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000121
Chris Lattner77e77a62005-01-21 06:05:23 +0000122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
123 SDOperand &Hi);
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
125 SDOperand Source);
Chris Lattnere34b3962005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000131 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000132
Chris Lattner3e928bb2005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
139
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000145}
146
Chris Lattner3e928bb2005-01-07 07:47:09 +0000147void SelectionDAGLegalize::LegalizeDAG() {
148 SDOperand OldRoot = DAG.getRoot();
149 SDOperand NewRoot = LegalizeOp(OldRoot);
150 DAG.setRoot(NewRoot);
151
152 ExpandedNodes.clear();
153 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000154 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000155
156 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000157 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000158}
159
160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
163
Chris Lattner3e928bb2005-01-07 07:47:09 +0000164 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
168 switch (getTypeAction(Op.Val->getValueType(i))) {
169 case Legal: break; // Nothing to do.
170 case Expand: {
171 SDOperand T1, T2;
172 ExpandOp(Op.getValue(i), T1, T2);
173 assert(LegalizedNodes.count(Op) &&
174 "Expansion didn't add legal operands!");
175 return LegalizedNodes[Op];
176 }
177 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000178 PromoteOp(Op.getValue(i));
179 assert(LegalizedNodes.count(Op) &&
180 "Expansion didn't add legal operands!");
181 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000182 }
183 }
184
Chris Lattnere1bd8222005-01-11 05:57:22 +0000185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000187
Chris Lattnerfa404e82005-01-09 19:03:49 +0000188 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000189
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000192
193 switch (Node->getOpcode()) {
194 default:
195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
196 assert(0 && "Do not know how to legalize this operator!");
197 abort();
198 case ISD::EntryToken:
199 case ISD::FrameIndex:
200 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000201 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000202 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
205 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000206 case ISD::CopyFromReg:
207 Tmp1 = LegalizeOp(Node->getOperand(0));
208 if (Tmp1 != Node->getOperand(0))
209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
210 Node->getValueType(0), Tmp1);
Chris Lattner13c184d2005-01-28 06:27:38 +0000211 else
212 Result = Op.getValue(0);
213
214 // Since CopyFromReg produces two values, make sure to remember that we
215 // legalized both of them.
216 AddLegalizedOperand(Op.getValue(0), Result);
217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
218 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
Chris Lattner2ee743f2005-01-14 22:08:15 +0000222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattner18c2f132005-01-13 20:50:02 +0000223 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000224 case ISD::UNDEF: {
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000230 if (MVT::isInteger(VT))
231 Result = DAG.getConstant(0, VT);
232 else if (MVT::isFloatingPoint(VT))
233 Result = DAG.getConstantFP(0, VT);
234 else
235 assert(0 && "Unknown value type!");
236 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000237 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000238 break;
239 }
240 break;
241 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000242 case ISD::Constant:
243 // We know we don't need to expand constants here, constants only have one
244 // value and we check that it is fine above.
245
246 // FIXME: Maybe we should handle things like targets that don't support full
247 // 32-bit immediates?
248 break;
249 case ISD::ConstantFP: {
250 // Spill FP immediates to the constant pool if the target cannot directly
251 // codegen them. Targets often have some immediate values that can be
252 // efficiently generated into an FP register without a load. We explicitly
253 // leave these constants as ConstantFP nodes for the target to deal with.
254
255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
256
257 // Check to see if this FP immediate is already legal.
258 bool isLegal = false;
259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
260 E = TLI.legal_fpimm_end(); I != E; ++I)
261 if (CFP->isExactlyValue(*I)) {
262 isLegal = true;
263 break;
264 }
265
266 if (!isLegal) {
267 // Otherwise we need to spill the constant to memory.
268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
269
270 bool Extend = false;
271
272 // If a FP immediate is precise when represented as a float, we put it
273 // into the constant pool as a float, even if it's is statically typed
274 // as a double.
275 MVT::ValueType VT = CFP->getValueType(0);
276 bool isDouble = VT == MVT::f64;
277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
278 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
280 // Only do this if the target has a native EXTLOAD instruction from
281 // f32.
282 TLI.getOperationAction(ISD::EXTLOAD,
283 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
285 VT = MVT::f32;
286 Extend = true;
287 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000288
Chris Lattner3e928bb2005-01-07 07:47:09 +0000289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
290 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000291 if (Extend) {
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000293 DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000294 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
296 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000297 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000298 }
299 break;
300 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000301 case ISD::TokenFactor: {
302 std::vector<SDOperand> Ops;
303 bool Changed = false;
304 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000305 SDOperand Op = Node->getOperand(i);
306 // Fold single-use TokenFactor nodes into this token factor as we go.
307 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
308 Changed = true;
309 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
310 Ops.push_back(LegalizeOp(Op.getOperand(j)));
311 } else {
312 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
313 Changed |= Ops[i] != Op;
314 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000315 }
316 if (Changed)
317 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
318 break;
319 }
320
Chris Lattner3e928bb2005-01-07 07:47:09 +0000321 case ISD::ADJCALLSTACKDOWN:
322 case ISD::ADJCALLSTACKUP:
323 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
324 // There is no need to legalize the size argument (Operand #1)
325 if (Tmp1 != Node->getOperand(0))
326 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
327 Node->getOperand(1));
328 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000329 case ISD::DYNAMIC_STACKALLOC:
330 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
331 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
332 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
333 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
334 Tmp3 != Node->getOperand(2))
335 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
336 Tmp1, Tmp2, Tmp3);
Chris Lattner513e52e2005-01-09 19:07:54 +0000337 else
338 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000339
340 // Since this op produces two values, make sure to remember that we
341 // legalized both of them.
342 AddLegalizedOperand(SDOperand(Node, 0), Result);
343 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
344 return Result.getValue(Op.ResNo);
345
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000346 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000347 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
348 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000349
350 bool Changed = false;
351 std::vector<SDOperand> Ops;
352 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
353 Ops.push_back(LegalizeOp(Node->getOperand(i)));
354 Changed |= Ops.back() != Node->getOperand(i);
355 }
356
357 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000358 std::vector<MVT::ValueType> RetTyVTs;
359 RetTyVTs.reserve(Node->getNumValues());
360 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000361 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000362 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000363 } else {
364 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000365 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000366 // Since calls produce multiple values, make sure to remember that we
367 // legalized all of them.
368 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
369 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
370 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000371 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000372 case ISD::BR:
373 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
374 if (Tmp1 != Node->getOperand(0))
375 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
376 break;
377
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000378 case ISD::BRCOND:
379 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner47e92232005-01-18 19:27:06 +0000380
381 switch (getTypeAction(Node->getOperand(1).getValueType())) {
382 case Expand: assert(0 && "It's impossible to expand bools");
383 case Legal:
384 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
385 break;
386 case Promote:
387 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
388 break;
389 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000390 // Basic block destination (Op#2) is always legal.
391 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
392 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
393 Node->getOperand(2));
394 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000395 case ISD::BRCONDTWOWAY:
396 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
397 switch (getTypeAction(Node->getOperand(1).getValueType())) {
398 case Expand: assert(0 && "It's impossible to expand bools");
399 case Legal:
400 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
401 break;
402 case Promote:
403 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
404 break;
405 }
406 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
407 // pair.
408 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
409 case TargetLowering::Promote:
410 default: assert(0 && "This action is not supported yet!");
411 case TargetLowering::Legal:
412 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
413 std::vector<SDOperand> Ops;
414 Ops.push_back(Tmp1);
415 Ops.push_back(Tmp2);
416 Ops.push_back(Node->getOperand(2));
417 Ops.push_back(Node->getOperand(3));
418 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
419 }
420 break;
421 case TargetLowering::Expand:
422 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
423 Node->getOperand(2));
424 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
425 break;
426 }
427 break;
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000428
Chris Lattner3e928bb2005-01-07 07:47:09 +0000429 case ISD::LOAD:
430 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
431 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000432
Chris Lattner3e928bb2005-01-07 07:47:09 +0000433 if (Tmp1 != Node->getOperand(0) ||
434 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000435 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
436 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000437 else
438 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000439
Chris Lattner8afc48e2005-01-07 22:28:47 +0000440 // Since loads produce two values, make sure to remember that we legalized
441 // both of them.
442 AddLegalizedOperand(SDOperand(Node, 0), Result);
443 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
444 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000445
Chris Lattner0f69b292005-01-15 06:18:18 +0000446 case ISD::EXTLOAD:
447 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000448 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000449 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
450 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000451
Chris Lattner01ff7212005-04-10 22:54:25 +0000452 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
453 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000454 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000455 case TargetLowering::Promote:
456 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
457 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000458 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000459 // Since loads produce two values, make sure to remember that we legalized
460 // both of them.
461 AddLegalizedOperand(SDOperand(Node, 0), Result);
462 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
463 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000464
Chris Lattner01ff7212005-04-10 22:54:25 +0000465 case TargetLowering::Legal:
466 if (Tmp1 != Node->getOperand(0) ||
467 Tmp2 != Node->getOperand(1))
468 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000469 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000470 else
471 Result = SDOperand(Node, 0);
472
473 // Since loads produce two values, make sure to remember that we legalized
474 // both of them.
475 AddLegalizedOperand(SDOperand(Node, 0), Result);
476 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
477 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000478 case TargetLowering::Expand:
479 assert(Node->getOpcode() != ISD::EXTLOAD &&
480 "EXTLOAD should always be supported!");
481 // Turn the unsupported load into an EXTLOAD followed by an explicit
482 // zero/sign extend inreg.
483 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000484 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000485 SDOperand ValRes;
486 if (Node->getOpcode() == ISD::SEXTLOAD)
487 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
488 Result, SrcVT);
489 else
490 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000491 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
492 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
493 if (Op.ResNo)
494 return Result.getValue(1);
495 return ValRes;
496 }
497 assert(0 && "Unreachable");
498 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000499 case ISD::EXTRACT_ELEMENT:
500 // Get both the low and high parts.
501 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
502 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
503 Result = Tmp2; // 1 -> Hi
504 else
505 Result = Tmp1; // 0 -> Lo
506 break;
507
508 case ISD::CopyToReg:
509 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000510
Chris Lattner3e928bb2005-01-07 07:47:09 +0000511 switch (getTypeAction(Node->getOperand(1).getValueType())) {
512 case Legal:
513 // Legalize the incoming value (must be legal).
514 Tmp2 = LegalizeOp(Node->getOperand(1));
515 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner18c2f132005-01-13 20:50:02 +0000516 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattner3e928bb2005-01-07 07:47:09 +0000517 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +0000518 case Promote:
519 Tmp2 = PromoteOp(Node->getOperand(1));
520 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
521 break;
522 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000523 SDOperand Lo, Hi;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000524 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattner18c2f132005-01-13 20:50:02 +0000525 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerec39a452005-01-19 18:02:17 +0000526 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
527 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
528 // Note that the copytoreg nodes are independent of each other.
529 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000530 assert(isTypeLegal(Result.getValueType()) &&
531 "Cannot expand multiple times yet (i64 -> i16)");
532 break;
533 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000534 break;
535
536 case ISD::RET:
537 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
538 switch (Node->getNumOperands()) {
539 case 2: // ret val
540 switch (getTypeAction(Node->getOperand(1).getValueType())) {
541 case Legal:
542 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000543 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000544 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
545 break;
546 case Expand: {
547 SDOperand Lo, Hi;
548 ExpandOp(Node->getOperand(1), Lo, Hi);
549 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000550 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000551 }
552 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000553 Tmp2 = PromoteOp(Node->getOperand(1));
554 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
555 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000556 }
557 break;
558 case 1: // ret void
559 if (Tmp1 != Node->getOperand(0))
560 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
561 break;
562 default: { // ret <values>
563 std::vector<SDOperand> NewValues;
564 NewValues.push_back(Tmp1);
565 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
566 switch (getTypeAction(Node->getOperand(i).getValueType())) {
567 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000568 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000569 break;
570 case Expand: {
571 SDOperand Lo, Hi;
572 ExpandOp(Node->getOperand(i), Lo, Hi);
573 NewValues.push_back(Lo);
574 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000575 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000576 }
577 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000578 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000579 }
580 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
581 break;
582 }
583 }
584 break;
585 case ISD::STORE:
586 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
587 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
588
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000589 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000590 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000591 if (CFP->getValueType(0) == MVT::f32) {
592 union {
593 unsigned I;
594 float F;
595 } V;
596 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000597 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000598 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000599 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000600 } else {
601 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
602 union {
603 uint64_t I;
604 double F;
605 } V;
606 V.F = CFP->getValue();
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000607 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000608 DAG.getConstant(V.I, MVT::i64), Tmp2,
609 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000610 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000611 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000612 }
613
Chris Lattner3e928bb2005-01-07 07:47:09 +0000614 switch (getTypeAction(Node->getOperand(1).getValueType())) {
615 case Legal: {
616 SDOperand Val = LegalizeOp(Node->getOperand(1));
617 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
618 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000619 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
620 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000621 break;
622 }
623 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000624 // Truncate the value and store the result.
625 Tmp3 = PromoteOp(Node->getOperand(1));
626 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000627 Node->getOperand(3),
628 Node->getOperand(1).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +0000629 break;
630
Chris Lattner3e928bb2005-01-07 07:47:09 +0000631 case Expand:
632 SDOperand Lo, Hi;
633 ExpandOp(Node->getOperand(1), Lo, Hi);
634
635 if (!TLI.isLittleEndian())
636 std::swap(Lo, Hi);
637
Chris Lattneredb1add2005-05-11 04:51:16 +0000638 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
639 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000640 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000641 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
642 getIntPtrConstant(IncrementSize));
643 assert(isTypeLegal(Tmp2.getValueType()) &&
644 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000645 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +0000646 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
647 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000648 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
649 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000650 }
651 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000652 case ISD::PCMARKER:
653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000654 if (Tmp1 != Node->getOperand(0))
655 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000656 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000657 case ISD::TRUNCSTORE:
658 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
659 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
660
661 switch (getTypeAction(Node->getOperand(1).getValueType())) {
662 case Legal:
663 Tmp2 = LegalizeOp(Node->getOperand(1));
664 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
665 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000666 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000667 Node->getOperand(3),
Chris Lattner0f69b292005-01-15 06:18:18 +0000668 cast<MVTSDNode>(Node)->getExtraValueType());
669 break;
670 case Promote:
671 case Expand:
672 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
673 }
674 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000675 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000676 switch (getTypeAction(Node->getOperand(0).getValueType())) {
677 case Expand: assert(0 && "It's impossible to expand bools");
678 case Legal:
679 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
680 break;
681 case Promote:
682 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
683 break;
684 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000685 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +0000686 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000687
688 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
689 default: assert(0 && "This action is not supported yet!");
690 case TargetLowering::Legal:
691 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
692 Tmp3 != Node->getOperand(2))
693 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
694 Tmp1, Tmp2, Tmp3);
695 break;
696 case TargetLowering::Promote: {
697 MVT::ValueType NVT =
698 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
699 unsigned ExtOp, TruncOp;
700 if (MVT::isInteger(Tmp2.getValueType())) {
701 ExtOp = ISD::ZERO_EXTEND;
702 TruncOp = ISD::TRUNCATE;
703 } else {
704 ExtOp = ISD::FP_EXTEND;
705 TruncOp = ISD::FP_ROUND;
706 }
707 // Promote each of the values to the new type.
708 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
709 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
710 // Perform the larger operation, then round down.
711 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
712 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
713 break;
714 }
715 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000716 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000717 case ISD::SETCC:
718 switch (getTypeAction(Node->getOperand(0).getValueType())) {
719 case Legal:
720 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
721 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
722 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
723 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000724 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000725 break;
726 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000727 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
728 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
729
730 // If this is an FP compare, the operands have already been extended.
731 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
732 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +0000733 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000734
735 // Otherwise, we have to insert explicit sign or zero extends. Note
736 // that we could insert sign extends for ALL conditions, but zero extend
737 // is cheaper on many machines (an AND instead of two shifts), so prefer
738 // it.
739 switch (cast<SetCCSDNode>(Node)->getCondition()) {
740 default: assert(0 && "Unknown integer comparison!");
741 case ISD::SETEQ:
742 case ISD::SETNE:
743 case ISD::SETUGE:
744 case ISD::SETUGT:
745 case ISD::SETULE:
746 case ISD::SETULT:
747 // ALL of these operations will work if we either sign or zero extend
748 // the operands (including the unsigned comparisons!). Zero extend is
749 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +0000750 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
751 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +0000752 break;
753 case ISD::SETGE:
754 case ISD::SETGT:
755 case ISD::SETLT:
756 case ISD::SETLE:
757 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
758 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
759 break;
760 }
761
762 }
763 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000764 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000765 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000766 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000767 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
768 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
769 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
770 switch (cast<SetCCSDNode>(Node)->getCondition()) {
771 case ISD::SETEQ:
772 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +0000773 if (RHSLo == RHSHi)
774 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
775 if (RHSCST->isAllOnesValue()) {
776 // Comparison to -1.
777 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000778 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner08b698e2005-04-12 01:46:05 +0000779 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000780 break;
Chris Lattner08b698e2005-04-12 01:46:05 +0000781 }
782
Chris Lattner3e928bb2005-01-07 07:47:09 +0000783 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
784 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
785 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000786 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000787 Node->getValueType(0), Tmp1,
Chris Lattner3e928bb2005-01-07 07:47:09 +0000788 DAG.getConstant(0, Tmp1.getValueType()));
789 break;
790 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +0000791 // If this is a comparison of the sign bit, just look at the top part.
792 // X > -1, x < 0
793 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukmanedf128a2005-04-21 22:36:52 +0000794 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +0000795 CST->getValue() == 0) || // X < 0
796 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
797 (CST->isAllOnesValue()))) // X > -1
798 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
799 Node->getValueType(0), LHSHi, RHSHi);
800
Chris Lattner3e928bb2005-01-07 07:47:09 +0000801 // FIXME: This generated code sucks.
802 ISD::CondCode LowCC;
803 switch (cast<SetCCSDNode>(Node)->getCondition()) {
804 default: assert(0 && "Unknown integer setcc!");
805 case ISD::SETLT:
806 case ISD::SETULT: LowCC = ISD::SETULT; break;
807 case ISD::SETGT:
808 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
809 case ISD::SETLE:
810 case ISD::SETULE: LowCC = ISD::SETULE; break;
811 case ISD::SETGE:
812 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
813 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000814
Chris Lattner3e928bb2005-01-07 07:47:09 +0000815 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
816 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
817 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
818
819 // NOTE: on targets without efficient SELECT of bools, we can always use
820 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000821 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000822 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerf30b73b2005-01-18 02:52:03 +0000823 Node->getValueType(0), LHSHi, RHSHi);
824 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
825 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
826 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000827 break;
828 }
829 }
830 break;
831
Chris Lattnere1bd8222005-01-11 05:57:22 +0000832 case ISD::MEMSET:
833 case ISD::MEMCPY:
834 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000835 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +0000836 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
837
838 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
839 switch (getTypeAction(Node->getOperand(2).getValueType())) {
840 case Expand: assert(0 && "Cannot expand a byte!");
841 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000842 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000843 break;
844 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +0000845 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +0000846 break;
847 }
848 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +0000849 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +0000850 }
Chris Lattner272455b2005-02-02 03:44:41 +0000851
852 SDOperand Tmp4;
853 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnere5605212005-01-28 22:29:18 +0000854 case Expand: assert(0 && "Cannot expand this yet!");
855 case Legal:
856 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +0000857 break;
858 case Promote:
859 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +0000860 break;
861 }
862
863 SDOperand Tmp5;
864 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
865 case Expand: assert(0 && "Cannot expand this yet!");
866 case Legal:
867 Tmp5 = LegalizeOp(Node->getOperand(4));
868 break;
869 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +0000870 Tmp5 = PromoteOp(Node->getOperand(4));
871 break;
872 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000873
874 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
875 default: assert(0 && "This action not implemented for this operation!");
876 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +0000877 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
878 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
879 Tmp5 != Node->getOperand(4)) {
880 std::vector<SDOperand> Ops;
881 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
882 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
883 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
884 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000885 break;
886 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +0000887 // Otherwise, the target does not support this operation. Lower the
888 // operation to an explicit libcall as appropriate.
889 MVT::ValueType IntPtr = TLI.getPointerTy();
890 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
891 std::vector<std::pair<SDOperand, const Type*> > Args;
892
Reid Spencer3bfbf4e2005-01-12 14:53:45 +0000893 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000894 if (Node->getOpcode() == ISD::MEMSET) {
895 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
896 // Extend the ubyte argument to be an int value for the call.
897 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
898 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
899 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
900
901 FnName = "memset";
902 } else if (Node->getOpcode() == ISD::MEMCPY ||
903 Node->getOpcode() == ISD::MEMMOVE) {
904 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
905 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
906 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
907 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
908 } else {
909 assert(0 && "Unknown op!");
910 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +0000911 // FIXME: THESE SHOULD USE ExpandLibCall ??!?
Chris Lattnere1bd8222005-01-11 05:57:22 +0000912 std::pair<SDOperand,SDOperand> CallResult =
Nate Begeman8e21e712005-03-26 01:29:23 +0000913 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +0000914 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
915 Result = LegalizeOp(CallResult.second);
Chris Lattner55ba8fb2005-01-16 07:29:19 +0000916 break;
917 }
918 case TargetLowering::Custom:
919 std::vector<SDOperand> Ops;
920 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
921 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
922 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
923 Result = TLI.LowerOperation(Result);
924 Result = LegalizeOp(Result);
925 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +0000926 }
927 break;
928 }
Chris Lattner52d08bd2005-05-09 20:23:03 +0000929
930 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000931 Tmp1 = LegalizeOp(Node->getOperand(0));
932 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000933
Chris Lattner52d08bd2005-05-09 20:23:03 +0000934 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000935 Result = DAG.getNode(ISD::READPORT, Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000936 else
937 Result = SDOperand(Node, 0);
938 // Since these produce two values, make sure to remember that we legalized
939 // both of them.
940 AddLegalizedOperand(SDOperand(Node, 0), Result);
941 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
942 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +0000943 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +0000944 Tmp1 = LegalizeOp(Node->getOperand(0));
945 Tmp2 = LegalizeOp(Node->getOperand(1));
946 Tmp3 = LegalizeOp(Node->getOperand(2));
947 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
948 Tmp3 != Node->getOperand(2))
949 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
950 break;
951
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000952 case ISD::READIO:
953 Tmp1 = LegalizeOp(Node->getOperand(0));
954 Tmp2 = LegalizeOp(Node->getOperand(1));
955
956 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
957 case TargetLowering::Custom:
958 default: assert(0 && "This action not implemented for this operation!");
959 case TargetLowering::Legal:
960 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
961 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
962 Tmp1, Tmp2);
963 else
964 Result = SDOperand(Node, 0);
965 break;
966 case TargetLowering::Expand:
967 // Replace this with a load from memory.
968 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
969 Node->getOperand(1), DAG.getSrcValue(NULL));
970 Result = LegalizeOp(Result);
971 break;
972 }
973
974 // Since these produce two values, make sure to remember that we legalized
975 // both of them.
976 AddLegalizedOperand(SDOperand(Node, 0), Result);
977 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
978 return Result.getValue(Op.ResNo);
979
980 case ISD::WRITEIO:
981 Tmp1 = LegalizeOp(Node->getOperand(0));
982 Tmp2 = LegalizeOp(Node->getOperand(1));
983 Tmp3 = LegalizeOp(Node->getOperand(2));
984
985 switch (TLI.getOperationAction(Node->getOpcode(),
986 Node->getOperand(1).getValueType())) {
987 case TargetLowering::Custom:
988 default: assert(0 && "This action not implemented for this operation!");
989 case TargetLowering::Legal:
990 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
991 Tmp3 != Node->getOperand(2))
992 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
993 break;
994 case TargetLowering::Expand:
995 // Replace this with a store to memory.
996 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
997 Node->getOperand(1), Node->getOperand(2),
998 DAG.getSrcValue(NULL));
999 Result = LegalizeOp(Result);
1000 break;
1001 }
1002 break;
1003
Chris Lattner84f67882005-01-20 18:52:28 +00001004 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001005 case ISD::SUB_PARTS:
1006 case ISD::SHL_PARTS:
1007 case ISD::SRA_PARTS:
1008 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001009 std::vector<SDOperand> Ops;
1010 bool Changed = false;
1011 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1012 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1013 Changed |= Ops.back() != Node->getOperand(i);
1014 }
1015 if (Changed)
1016 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner2c8086f2005-04-02 05:00:07 +00001017
1018 // Since these produce multiple values, make sure to remember that we
1019 // legalized all of them.
1020 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1021 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1022 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001023 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001024
1025 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001026 case ISD::ADD:
1027 case ISD::SUB:
1028 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001029 case ISD::MULHS:
1030 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001031 case ISD::UDIV:
1032 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001033 case ISD::AND:
1034 case ISD::OR:
1035 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001036 case ISD::SHL:
1037 case ISD::SRL:
1038 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001039 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1040 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1041 if (Tmp1 != Node->getOperand(0) ||
1042 Tmp2 != Node->getOperand(1))
1043 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1044 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001045
Nate Begemanc105e192005-04-06 00:23:54 +00001046 case ISD::UREM:
1047 case ISD::SREM:
1048 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1049 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1050 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1051 case TargetLowering::Legal:
1052 if (Tmp1 != Node->getOperand(0) ||
1053 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001054 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001055 Tmp2);
1056 break;
1057 case TargetLowering::Promote:
1058 case TargetLowering::Custom:
1059 assert(0 && "Cannot promote/custom handle this yet!");
1060 case TargetLowering::Expand: {
1061 MVT::ValueType VT = Node->getValueType(0);
1062 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1063 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1064 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1065 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1066 }
1067 break;
1068 }
1069 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001070
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001071 case ISD::CTPOP:
1072 case ISD::CTTZ:
1073 case ISD::CTLZ:
1074 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1075 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1076 case TargetLowering::Legal:
1077 if (Tmp1 != Node->getOperand(0))
1078 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1079 break;
1080 case TargetLowering::Promote: {
1081 MVT::ValueType OVT = Tmp1.getValueType();
1082 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001083
1084 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001085 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1086 // Perform the larger operation, then subtract if needed.
1087 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1088 switch(Node->getOpcode())
1089 {
1090 case ISD::CTPOP:
1091 Result = Tmp1;
1092 break;
1093 case ISD::CTTZ:
1094 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1095 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1096 DAG.getConstant(getSizeInBits(NVT), NVT));
1097 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1098 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1099 break;
1100 case ISD::CTLZ:
1101 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1102 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1103 DAG.getConstant(getSizeInBits(NVT) -
1104 getSizeInBits(OVT), NVT));
1105 break;
1106 }
1107 break;
1108 }
1109 case TargetLowering::Custom:
1110 assert(0 && "Cannot custom handle this yet!");
1111 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001112 switch(Node->getOpcode())
1113 {
1114 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001115 static const uint64_t mask[6] = {
1116 0x5555555555555555ULL, 0x3333333333333333ULL,
1117 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1118 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1119 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001120 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001121 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1122 unsigned len = getSizeInBits(VT);
1123 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001124 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001125 Tmp2 = DAG.getConstant(mask[i], VT);
1126 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001127 Tmp1 = DAG.getNode(ISD::ADD, VT,
1128 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1129 DAG.getNode(ISD::AND, VT,
1130 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1131 Tmp2));
1132 }
1133 Result = Tmp1;
1134 break;
1135 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001136 case ISD::CTLZ: {
1137 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001138 x = x | (x >> 1);
1139 x = x | (x >> 2);
1140 ...
1141 x = x | (x >>16);
1142 x = x | (x >>32); // for 64-bit input
1143 return popcount(~x);
Duraid Madina57ff7e52005-05-11 08:45:08 +00001144
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001145 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1146 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001147 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1148 unsigned len = getSizeInBits(VT);
1149 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1150 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
1151 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
1152 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1153 }
1154 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001155 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001156 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001157 }
1158 case ISD::CTTZ: {
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001159 // for now, we use: { return popcount(~x & (x - 1)); }
1160 // but see also http://www.hackersdelight.org/HDcode/ntz.cc
1161 MVT::ValueType VT = Tmp1.getValueType();
1162 Tmp2 = DAG.getConstant(~0ULL, VT);
1163 Tmp3 = DAG.getNode(ISD::AND, VT,
1164 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1165 DAG.getNode(ISD::SUB, VT, Tmp1,
1166 DAG.getConstant(1, VT)));
1167 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001168 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001169 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001170 default:
1171 assert(0 && "Cannot expand this yet!");
1172 break;
1173 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001174 break;
1175 }
1176 break;
1177
Chris Lattner2c8086f2005-04-02 05:00:07 +00001178 // Unary operators
1179 case ISD::FABS:
1180 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001181 case ISD::FSQRT:
1182 case ISD::FSIN:
1183 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001184 Tmp1 = LegalizeOp(Node->getOperand(0));
1185 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1186 case TargetLowering::Legal:
1187 if (Tmp1 != Node->getOperand(0))
1188 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1189 break;
1190 case TargetLowering::Promote:
1191 case TargetLowering::Custom:
1192 assert(0 && "Cannot promote/custom handle this yet!");
1193 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001194 switch(Node->getOpcode()) {
1195 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001196 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1197 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1198 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1199 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001200 break;
1201 }
1202 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001203 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1204 MVT::ValueType VT = Node->getValueType(0);
1205 Tmp2 = DAG.getConstantFP(0.0, VT);
1206 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1207 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1208 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1209 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001210 break;
1211 }
1212 case ISD::FSQRT:
1213 case ISD::FSIN:
1214 case ISD::FCOS: {
1215 MVT::ValueType VT = Node->getValueType(0);
1216 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1217 const char *FnName = 0;
1218 switch(Node->getOpcode()) {
1219 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1220 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1221 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1222 default: assert(0 && "Unreachable!");
1223 }
1224 std::vector<std::pair<SDOperand, const Type*> > Args;
1225 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00001226 // FIXME: should use ExpandLibCall!
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001227 std::pair<SDOperand,SDOperand> CallResult =
1228 TLI.LowerCallTo(DAG.getEntryNode(), T, false,
1229 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1230 Result = LegalizeOp(CallResult.first);
1231 break;
1232 }
1233 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001234 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001235 }
1236 break;
1237 }
1238 break;
1239
1240 // Conversion operators. The source and destination have different types.
Chris Lattner3e928bb2005-01-07 07:47:09 +00001241 case ISD::ZERO_EXTEND:
1242 case ISD::SIGN_EXTEND:
Chris Lattner7cc47772005-01-07 21:56:57 +00001243 case ISD::TRUNCATE:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001244 case ISD::FP_EXTEND:
1245 case ISD::FP_ROUND:
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001246 case ISD::FP_TO_SINT:
1247 case ISD::FP_TO_UINT:
1248 case ISD::SINT_TO_FP:
1249 case ISD::UINT_TO_FP:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001250 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1251 case Legal:
1252 Tmp1 = LegalizeOp(Node->getOperand(0));
1253 if (Tmp1 != Node->getOperand(0))
1254 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1255 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001256 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001257 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1258 Node->getOpcode() == ISD::UINT_TO_FP) {
1259 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1260 Node->getValueType(0), Node->getOperand(0));
1261 Result = LegalizeOp(Result);
1262 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001263 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1264 // In the expand case, we must be dealing with a truncate, because
1265 // otherwise the result would be larger than the source.
1266 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001267
Chris Lattner2c8086f2005-04-02 05:00:07 +00001268 // Since the result is legal, we should just be able to truncate the low
1269 // part of the source.
1270 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1271 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00001272 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001273 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001274
Chris Lattner03c85462005-01-15 05:21:40 +00001275 case Promote:
1276 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001277 case ISD::ZERO_EXTEND:
1278 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001279 // NOTE: Any extend would work here...
1280 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001281 Result = DAG.getZeroExtendInReg(Result,
1282 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001283 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001284 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001285 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001286 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001287 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001288 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1289 Result, Node->getOperand(0).getValueType());
1290 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001291 case ISD::TRUNCATE:
Chris Lattner1713e732005-01-16 00:38:00 +00001292 Result = PromoteOp(Node->getOperand(0));
1293 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1294 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001295 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001296 Result = PromoteOp(Node->getOperand(0));
1297 if (Result.getValueType() != Op.getValueType())
1298 // Dynamically dead while we have only 2 FP types.
1299 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1300 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001301 case ISD::FP_ROUND:
1302 case ISD::FP_TO_SINT:
1303 case ISD::FP_TO_UINT:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001304 Result = PromoteOp(Node->getOperand(0));
1305 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1306 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001307 case ISD::SINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001308 Result = PromoteOp(Node->getOperand(0));
1309 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1310 Result, Node->getOperand(0).getValueType());
1311 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1312 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001313 case ISD::UINT_TO_FP:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001314 Result = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001315 Result = DAG.getZeroExtendInReg(Result,
1316 Node->getOperand(0).getValueType());
Chris Lattnerf8161d82005-01-16 05:06:12 +00001317 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1318 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001319 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001320 }
1321 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001322 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001323 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001324 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner45b8caf2005-01-15 07:15:18 +00001325 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1326
1327 // If this operation is not supported, convert it to a shl/shr or load/store
1328 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001329 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1330 default: assert(0 && "This action not supported for this op yet!");
1331 case TargetLowering::Legal:
1332 if (Tmp1 != Node->getOperand(0))
1333 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1334 ExtraVT);
1335 break;
1336 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001337 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001338 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001339 // NOTE: we could fall back on load/store here too for targets without
1340 // SAR. However, it is doubtful that any exist.
1341 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1342 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001343 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001344 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1345 Node->getOperand(0), ShiftCst);
1346 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1347 Result, ShiftCst);
1348 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1349 // The only way we can lower this is to turn it into a STORETRUNC,
1350 // EXTLOAD pair, targetting a temporary location (a stack slot).
1351
1352 // NOTE: there is a choice here between constantly creating new stack
1353 // slots and always reusing the same one. We currently always create
1354 // new ones, as reuse may inhibit scheduling.
1355 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1356 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1357 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1358 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001359 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001360 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1361 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1362 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001363 Node->getOperand(0), StackSlot,
1364 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001365 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001366 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001367 } else {
1368 assert(0 && "Unknown op");
1369 }
1370 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001371 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001372 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001373 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001374 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001375 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001376
Chris Lattner8afc48e2005-01-07 22:28:47 +00001377 if (!Op.Val->hasOneUse())
1378 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001379
1380 return Result;
1381}
1382
Chris Lattner8b6fa222005-01-15 22:16:26 +00001383/// PromoteOp - Given an operation that produces a value in an invalid type,
1384/// promote it to compute the value into a larger type. The produced value will
1385/// have the correct bits for the low portion of the register, but no guarantee
1386/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001387SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1388 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001389 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001390 assert(getTypeAction(VT) == Promote &&
1391 "Caller should expand or legalize operands that are not promotable!");
1392 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1393 "Cannot promote to smaller type!");
1394
1395 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1396 if (I != PromotedNodes.end()) return I->second;
1397
1398 SDOperand Tmp1, Tmp2, Tmp3;
1399
1400 SDOperand Result;
1401 SDNode *Node = Op.Val;
1402
Chris Lattner0f69b292005-01-15 06:18:18 +00001403 // Promotion needs an optimization step to clean up after it, and is not
1404 // careful to avoid operations the target does not support. Make sure that
1405 // all generated operations are legalized in the next iteration.
1406 NeedsAnotherIteration = true;
1407
Chris Lattner03c85462005-01-15 05:21:40 +00001408 switch (Node->getOpcode()) {
1409 default:
1410 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1411 assert(0 && "Do not know how to promote this operator!");
1412 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001413 case ISD::UNDEF:
1414 Result = DAG.getNode(ISD::UNDEF, NVT);
1415 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001416 case ISD::Constant:
1417 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1418 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1419 break;
1420 case ISD::ConstantFP:
1421 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1422 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1423 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001424 case ISD::CopyFromReg:
1425 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1426 Node->getOperand(0));
1427 // Remember that we legalized the chain.
1428 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1429 break;
1430
Chris Lattner82fbfb62005-01-18 02:59:52 +00001431 case ISD::SETCC:
1432 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1433 "SetCC type is not legal??");
1434 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1435 TLI.getSetCCResultTy(), Node->getOperand(0),
1436 Node->getOperand(1));
1437 Result = LegalizeOp(Result);
1438 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001439
1440 case ISD::TRUNCATE:
1441 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1442 case Legal:
1443 Result = LegalizeOp(Node->getOperand(0));
1444 assert(Result.getValueType() >= NVT &&
1445 "This truncation doesn't make sense!");
1446 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1447 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1448 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001449 case Promote:
1450 // The truncation is not required, because we don't guarantee anything
1451 // about high bits anyway.
1452 Result = PromoteOp(Node->getOperand(0));
1453 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001454 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001455 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1456 // Truncate the low part of the expanded value to the result type
Misha Brukmanedf128a2005-04-21 22:36:52 +00001457 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001458 }
1459 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001460 case ISD::SIGN_EXTEND:
1461 case ISD::ZERO_EXTEND:
1462 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1463 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1464 case Legal:
1465 // Input is legal? Just do extend all the way to the larger type.
1466 Result = LegalizeOp(Node->getOperand(0));
1467 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1468 break;
1469 case Promote:
1470 // Promote the reg if it's smaller.
1471 Result = PromoteOp(Node->getOperand(0));
1472 // The high bits are not guaranteed to be anything. Insert an extend.
1473 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001474 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1475 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001476 else
Chris Lattner23993562005-04-13 02:38:47 +00001477 Result = DAG.getZeroExtendInReg(Result,
1478 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001479 break;
1480 }
1481 break;
1482
1483 case ISD::FP_EXTEND:
1484 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1485 case ISD::FP_ROUND:
1486 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1487 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1488 case Promote: assert(0 && "Unreachable with 2 FP types!");
1489 case Legal:
1490 // Input is legal? Do an FP_ROUND_INREG.
1491 Result = LegalizeOp(Node->getOperand(0));
1492 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1493 break;
1494 }
1495 break;
1496
1497 case ISD::SINT_TO_FP:
1498 case ISD::UINT_TO_FP:
1499 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1500 case Legal:
1501 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001502 // No extra round required here.
1503 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001504 break;
1505
1506 case Promote:
1507 Result = PromoteOp(Node->getOperand(0));
1508 if (Node->getOpcode() == ISD::SINT_TO_FP)
1509 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1510 Result, Node->getOperand(0).getValueType());
1511 else
Chris Lattner23993562005-04-13 02:38:47 +00001512 Result = DAG.getZeroExtendInReg(Result,
1513 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00001514 // No extra round required here.
1515 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001516 break;
1517 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00001518 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1519 Node->getOperand(0));
1520 Result = LegalizeOp(Result);
1521
1522 // Round if we cannot tolerate excess precision.
1523 if (NoExcessFPPrecision)
1524 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1525 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001526 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001527 break;
1528
1529 case ISD::FP_TO_SINT:
1530 case ISD::FP_TO_UINT:
1531 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1532 case Legal:
1533 Tmp1 = LegalizeOp(Node->getOperand(0));
1534 break;
1535 case Promote:
1536 // The input result is prerounded, so we don't have to do anything
1537 // special.
1538 Tmp1 = PromoteOp(Node->getOperand(0));
1539 break;
1540 case Expand:
1541 assert(0 && "not implemented");
1542 }
1543 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1544 break;
1545
Chris Lattner2c8086f2005-04-02 05:00:07 +00001546 case ISD::FABS:
1547 case ISD::FNEG:
1548 Tmp1 = PromoteOp(Node->getOperand(0));
1549 assert(Tmp1.getValueType() == NVT);
1550 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1551 // NOTE: we do not have to do any extra rounding here for
1552 // NoExcessFPPrecision, because we know the input will have the appropriate
1553 // precision, and these operations don't modify precision at all.
1554 break;
1555
Chris Lattnerda6ba872005-04-28 21:44:33 +00001556 case ISD::FSQRT:
1557 case ISD::FSIN:
1558 case ISD::FCOS:
1559 Tmp1 = PromoteOp(Node->getOperand(0));
1560 assert(Tmp1.getValueType() == NVT);
1561 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1562 if(NoExcessFPPrecision)
1563 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1564 break;
1565
Chris Lattner03c85462005-01-15 05:21:40 +00001566 case ISD::AND:
1567 case ISD::OR:
1568 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00001569 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001570 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00001571 case ISD::MUL:
1572 // The input may have strange things in the top bits of the registers, but
1573 // these operations don't care. They may have wierd bits going out, but
1574 // that too is okay if they are integer operations.
1575 Tmp1 = PromoteOp(Node->getOperand(0));
1576 Tmp2 = PromoteOp(Node->getOperand(1));
1577 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1578 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1579
1580 // However, if this is a floating point operation, they will give excess
1581 // precision that we may not be able to tolerate. If we DO allow excess
1582 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00001583 // FIXME: Why would we need to round FP ops more than integer ones?
1584 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00001585 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1586 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1587 break;
1588
Chris Lattner8b6fa222005-01-15 22:16:26 +00001589 case ISD::SDIV:
1590 case ISD::SREM:
1591 // These operators require that their input be sign extended.
1592 Tmp1 = PromoteOp(Node->getOperand(0));
1593 Tmp2 = PromoteOp(Node->getOperand(1));
1594 if (MVT::isInteger(NVT)) {
1595 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattnerff3e50c2005-01-16 00:17:42 +00001596 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001597 }
1598 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1599
1600 // Perform FP_ROUND: this is probably overly pessimistic.
1601 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1602 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1603 break;
1604
1605 case ISD::UDIV:
1606 case ISD::UREM:
1607 // These operators require that their input be zero extended.
1608 Tmp1 = PromoteOp(Node->getOperand(0));
1609 Tmp2 = PromoteOp(Node->getOperand(1));
1610 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00001611 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1612 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001613 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1614 break;
1615
1616 case ISD::SHL:
1617 Tmp1 = PromoteOp(Node->getOperand(0));
1618 Tmp2 = LegalizeOp(Node->getOperand(1));
1619 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1620 break;
1621 case ISD::SRA:
1622 // The input value must be properly sign extended.
1623 Tmp1 = PromoteOp(Node->getOperand(0));
1624 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1625 Tmp2 = LegalizeOp(Node->getOperand(1));
1626 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1627 break;
1628 case ISD::SRL:
1629 // The input value must be properly zero extended.
1630 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00001631 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001632 Tmp2 = LegalizeOp(Node->getOperand(1));
1633 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1634 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001635 case ISD::LOAD:
1636 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1637 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00001638 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00001639 if (MVT::isInteger(NVT))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001640 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1641 VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00001642 else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001643 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1644 VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001645
1646 // Remember that we legalized the chain.
1647 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1648 break;
1649 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001650 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1651 case Expand: assert(0 && "It's impossible to expand bools");
1652 case Legal:
1653 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1654 break;
1655 case Promote:
1656 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1657 break;
1658 }
Chris Lattner03c85462005-01-15 05:21:40 +00001659 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1660 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1661 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1662 break;
Chris Lattner8ac532c2005-01-16 19:46:48 +00001663 case ISD::CALL: {
1664 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1665 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1666
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001667 std::vector<SDOperand> Ops;
1668 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1669 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1670
Chris Lattner8ac532c2005-01-16 19:46:48 +00001671 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1672 "Can only promote single result calls");
1673 std::vector<MVT::ValueType> RetTyVTs;
1674 RetTyVTs.reserve(2);
1675 RetTyVTs.push_back(NVT);
1676 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00001677 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner8ac532c2005-01-16 19:46:48 +00001678 Result = SDOperand(NC, 0);
1679
1680 // Insert the new chain mapping.
1681 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1682 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001683 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00001684 case ISD::CTPOP:
1685 case ISD::CTTZ:
1686 case ISD::CTLZ:
1687 Tmp1 = Node->getOperand(0);
1688 //Zero extend the argument
1689 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1690 // Perform the larger operation, then subtract if needed.
1691 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1692 switch(Node->getOpcode())
1693 {
1694 case ISD::CTPOP:
1695 Result = Tmp1;
1696 break;
1697 case ISD::CTTZ:
1698 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1699 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1700 DAG.getConstant(getSizeInBits(NVT), NVT));
1701 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1702 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1703 break;
1704 case ISD::CTLZ:
1705 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1706 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1707 DAG.getConstant(getSizeInBits(NVT) -
1708 getSizeInBits(VT), NVT));
1709 break;
1710 }
1711 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001712 }
1713
1714 assert(Result.Val && "Didn't set a result!");
1715 AddPromotedOperand(Op, Result);
1716 return Result;
1717}
Chris Lattner3e928bb2005-01-07 07:47:09 +00001718
Chris Lattner84f67882005-01-20 18:52:28 +00001719/// ExpandAddSub - Find a clever way to expand this add operation into
1720/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00001721void SelectionDAGLegalize::
1722ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1723 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00001724 // Expand the subcomponents.
1725 SDOperand LHSL, LHSH, RHSL, RHSH;
1726 ExpandOp(LHS, LHSL, LHSH);
1727 ExpandOp(RHS, RHSL, RHSH);
1728
Chris Lattnerbd0781e2005-04-11 20:29:59 +00001729 // FIXME: this should be moved to the dag combiner someday.
1730 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1731 if (LHSL.getValueType() == MVT::i32) {
1732 SDOperand LowEl;
1733 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1734 if (C->getValue() == 0)
1735 LowEl = RHSL;
1736 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1737 if (C->getValue() == 0)
1738 LowEl = LHSL;
1739 if (LowEl.Val) {
1740 // Turn this into an add/sub of the high part only.
1741 SDOperand HiEl =
1742 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1743 LowEl.getValueType(), LHSH, RHSH);
1744 Lo = LowEl;
1745 Hi = HiEl;
1746 return;
1747 }
1748 }
1749
Chris Lattner84f67882005-01-20 18:52:28 +00001750 std::vector<SDOperand> Ops;
1751 Ops.push_back(LHSL);
1752 Ops.push_back(LHSH);
1753 Ops.push_back(RHSL);
1754 Ops.push_back(RHSH);
Chris Lattner47599822005-04-02 03:38:53 +00001755 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00001756 Hi = Lo.getValue(1);
1757}
1758
Chris Lattner5b359c62005-04-02 04:00:59 +00001759void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1760 SDOperand Op, SDOperand Amt,
1761 SDOperand &Lo, SDOperand &Hi) {
1762 // Expand the subcomponents.
1763 SDOperand LHSL, LHSH;
1764 ExpandOp(Op, LHSL, LHSH);
1765
1766 std::vector<SDOperand> Ops;
1767 Ops.push_back(LHSL);
1768 Ops.push_back(LHSH);
1769 Ops.push_back(Amt);
1770 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1771 Hi = Lo.getValue(1);
1772}
1773
1774
Chris Lattnere34b3962005-01-19 04:19:40 +00001775/// ExpandShift - Try to find a clever way to expand this shift operation out to
1776/// smaller elements. If we can't find a way that is more efficient than a
1777/// libcall on this target, return false. Otherwise, return true with the
1778/// low-parts expanded into Lo and Hi.
1779bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1780 SDOperand &Lo, SDOperand &Hi) {
1781 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1782 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001783
Chris Lattnere34b3962005-01-19 04:19:40 +00001784 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001785 SDOperand ShAmt = LegalizeOp(Amt);
1786 MVT::ValueType ShTy = ShAmt.getValueType();
1787 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1788 unsigned NVTBits = MVT::getSizeInBits(NVT);
1789
1790 // Handle the case when Amt is an immediate. Other cases are currently broken
1791 // and are disabled.
1792 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1793 unsigned Cst = CN->getValue();
1794 // Expand the incoming operand to be shifted, so that we have its parts
1795 SDOperand InL, InH;
1796 ExpandOp(Op, InL, InH);
1797 switch(Opc) {
1798 case ISD::SHL:
1799 if (Cst > VTBits) {
1800 Lo = DAG.getConstant(0, NVT);
1801 Hi = DAG.getConstant(0, NVT);
1802 } else if (Cst > NVTBits) {
1803 Lo = DAG.getConstant(0, NVT);
1804 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001805 } else if (Cst == NVTBits) {
1806 Lo = DAG.getConstant(0, NVT);
1807 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001808 } else {
1809 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1810 Hi = DAG.getNode(ISD::OR, NVT,
1811 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1812 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1813 }
1814 return true;
1815 case ISD::SRL:
1816 if (Cst > VTBits) {
1817 Lo = DAG.getConstant(0, NVT);
1818 Hi = DAG.getConstant(0, NVT);
1819 } else if (Cst > NVTBits) {
1820 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1821 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00001822 } else if (Cst == NVTBits) {
1823 Lo = InH;
1824 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001825 } else {
1826 Lo = DAG.getNode(ISD::OR, NVT,
1827 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1828 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1829 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1830 }
1831 return true;
1832 case ISD::SRA:
1833 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001834 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001835 DAG.getConstant(NVTBits-1, ShTy));
1836 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001837 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001838 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001839 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001840 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00001841 } else if (Cst == NVTBits) {
1842 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001843 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00001844 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00001845 } else {
1846 Lo = DAG.getNode(ISD::OR, NVT,
1847 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1848 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1849 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1850 }
1851 return true;
1852 }
1853 }
1854 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1855 // so disable it for now. Currently targets are handling this via SHL_PARTS
1856 // and friends.
1857 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00001858
1859 // If we have an efficient select operation (or if the selects will all fold
1860 // away), lower to some complex code, otherwise just emit the libcall.
1861 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1862 !isa<ConstantSDNode>(Amt))
1863 return false;
1864
1865 SDOperand InL, InH;
1866 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00001867 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1868 DAG.getConstant(NVTBits, ShTy), ShAmt);
1869
Chris Lattnere5544f82005-01-20 20:29:23 +00001870 // Compare the unmasked shift amount against 32.
1871 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1872 DAG.getConstant(NVTBits, ShTy));
1873
Chris Lattnere34b3962005-01-19 04:19:40 +00001874 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1875 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1876 DAG.getConstant(NVTBits-1, ShTy));
1877 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1878 DAG.getConstant(NVTBits-1, ShTy));
1879 }
1880
1881 if (Opc == ISD::SHL) {
1882 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1883 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1884 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001885 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00001886
Chris Lattnere34b3962005-01-19 04:19:40 +00001887 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1888 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1889 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00001890 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1891 DAG.getSetCC(ISD::SETEQ,
1892 TLI.getSetCCResultTy(), NAmt,
1893 DAG.getConstant(32, ShTy)),
1894 DAG.getConstant(0, NVT),
1895 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00001896 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00001897 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00001898 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00001899 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00001900
1901 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00001902 if (Opc == ISD::SRA)
1903 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1904 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00001905 else
1906 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00001907 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00001908 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00001909 }
1910 return true;
1911}
Chris Lattner77e77a62005-01-21 06:05:23 +00001912
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001913/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1914/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1915/// Found.
1916static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1917 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1918
1919 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1920 // than the Found node. Just remember this node and return.
1921 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1922 Found = Node;
1923 return;
1924 }
1925
1926 // Otherwise, scan the operands of Node to see if any of them is a call.
1927 assert(Node->getNumOperands() != 0 &&
1928 "All leaves should have depth equal to the entry node!");
1929 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1930 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1931
1932 // Tail recurse for the last iteration.
1933 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1934 Found);
1935}
1936
1937
1938/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1939/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1940/// than Found.
1941static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1942 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1943
1944 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1945 // than the Found node. Just remember this node and return.
1946 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1947 Found = Node;
1948 return;
1949 }
1950
1951 // Otherwise, scan the operands of Node to see if any of them is a call.
1952 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1953 if (UI == E) return;
1954 for (--E; UI != E; ++UI)
1955 FindEarliestAdjCallStackUp(*UI, Found);
1956
1957 // Tail recurse for the last iteration.
1958 FindEarliestAdjCallStackUp(*UI, Found);
1959}
1960
1961/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1962/// find the ADJCALLSTACKUP node that terminates the call sequence.
1963static SDNode *FindAdjCallStackUp(SDNode *Node) {
1964 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1965 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00001966 if (Node->use_empty())
1967 return 0; // No adjcallstackup
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001968
1969 if (Node->hasOneUse()) // Simple case, only has one user to check.
1970 return FindAdjCallStackUp(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00001971
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001972 SDOperand TheChain(Node, Node->getNumValues()-1);
1973 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001974
1975 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001976 E = Node->use_end(); ; ++UI) {
1977 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00001978
Chris Lattner9c32d3b2005-01-23 04:42:50 +00001979 // Make sure to only follow users of our token chain.
1980 SDNode *User = *UI;
1981 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1982 if (User->getOperand(i) == TheChain)
1983 return FindAdjCallStackUp(User);
1984 }
1985 assert(0 && "Unreachable");
1986 abort();
1987}
1988
Chris Lattner0d67f0c2005-05-11 19:02:11 +00001989/// FindAdjCallStackDown - Given a chained node that is part of a call sequence,
1990/// find the ADJCALLSTACKDOWN node that initiates the call sequence.
1991static SDNode *FindAdjCallStackDown(SDNode *Node) {
1992 assert(Node && "Didn't find adjcallstackdown for a call??");
1993 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) return Node;
1994
1995 assert(Node->getOperand(0).getValueType() == MVT::Other &&
1996 "Node doesn't have a token chain argument!");
1997 return FindAdjCallStackDown(Node->getOperand(0).Val);
1998}
1999
2000
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002001/// FindInputOutputChains - If we are replacing an operation with a call we need
2002/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002003/// properly serialize the calls in the block. The returned operand is the
2004/// input chain value for the new call (e.g. the entry node or the previous
2005/// call), and OutChain is set to be the chain node to update to point to the
2006/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002007static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2008 SDOperand Entry) {
2009 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanc7c16572005-04-11 03:01:51 +00002010 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002011 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002012 //std::cerr<<"Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002013
Nate Begemanc7c16572005-04-11 03:01:51 +00002014 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
2015 // previous call in the function. LatestCallStackDown may in that case be
2016 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
2017 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
2018 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
2019 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
2020 else
2021 LatestAdjCallStackUp = Entry.Val;
2022 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002023
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002024 // Finally, find the first call that this must come before, first we find the
2025 // adjcallstackup that ends the call.
2026 OutChain = 0;
2027 FindEarliestAdjCallStackUp(OpNode, OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002028
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002029 // If we found one, translate from the adj up to the adjdown.
2030 if (OutChain)
2031 OutChain = FindAdjCallStackDown(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002032
2033 return SDOperand(LatestAdjCallStackUp, 0);
2034}
2035
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002036/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
2037static void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain,
2038 SelectionDAG &DAG) {
2039 // Nothing to splice it into?
2040 if (OutChain == 0) return;
2041
2042 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2043 //OutChain->dump();
2044
2045 // Form a token factor node merging the old inval and the new inval.
2046 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2047 OutChain->getOperand(0));
2048 // Change the node to refer to the new token.
2049 OutChain->setAdjCallChain(InToken);
2050}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002051
2052
Chris Lattner77e77a62005-01-21 06:05:23 +00002053// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2054// does not fit into a register, return the lo part and set the hi part to the
2055// by-reg argument. If it does fit into a single register, return the result
2056// and leave the Hi part unset.
2057SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2058 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002059 SDNode *OutChain;
2060 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2061 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002062 if (InChain.Val == 0)
2063 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002064
Chris Lattner77e77a62005-01-21 06:05:23 +00002065 TargetLowering::ArgListTy Args;
2066 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2067 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2068 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2069 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2070 }
2071 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002072
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002073 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002074 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002075 std::pair<SDOperand,SDOperand> CallInfo =
2076 TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG);
2077 SpliceCallInto(CallInfo.second, OutChain, DAG);
2078
2079 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002080 default: assert(0 && "Unknown thing");
2081 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002082 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002083 case Promote:
2084 assert(0 && "Cannot promote this yet!");
2085 case Expand:
2086 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002087 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002088 return Lo;
2089 }
2090}
2091
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002092
Chris Lattner77e77a62005-01-21 06:05:23 +00002093/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2094/// destination type is legal.
2095SDOperand SelectionDAGLegalize::
2096ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2097 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2098 assert(getTypeAction(Source.getValueType()) == Expand &&
2099 "This is not an expansion!");
2100 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2101
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002102 if (!isSigned) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002103 // If this is unsigned, and not supported, first perform the conversion to
2104 // signed, then adjust the result if the sign bit is set.
Chris Lattnerffe284c2005-04-13 03:42:14 +00002105 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattner77e77a62005-01-21 06:05:23 +00002106
Chris Lattnere9c35e72005-04-13 05:09:42 +00002107 assert(Source.getValueType() == MVT::i64 &&
2108 "This only works for 64-bit -> FP");
2109 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2110 // incoming integer is set. To handle this, we dynamically test to see if
2111 // it is set, and, if so, add a fudge factor.
2112 SDOperand Lo, Hi;
2113 ExpandOp(Source, Lo, Hi);
2114
2115 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2116 DAG.getConstant(0, Hi.getValueType()));
2117 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2118 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2119 SignSet, Four, Zero);
2120 // FIXME: This is almost certainly broken for big-endian systems. Should
2121 // this just put the fudge factor in the low bits of the uint64 constant or?
2122 static Constant *FudgeFactor =
2123 ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32);
2124
2125 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2126 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2127 TLI.getPointerTy());
2128 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2129 SDOperand FudgeInReg;
2130 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002131 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2132 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002133 else {
2134 assert(DestTy == MVT::f64 && "Unexpected conversion");
2135 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002136 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002137 }
2138 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002139 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002140
2141 SDNode *OutChain = 0;
2142 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2143 DAG.getEntryNode());
2144 const char *FnName = 0;
2145 if (DestTy == MVT::f32)
2146 FnName = "__floatdisf";
2147 else {
2148 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2149 FnName = "__floatdidf";
2150 }
2151
Chris Lattner77e77a62005-01-21 06:05:23 +00002152 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2153
2154 TargetLowering::ArgListTy Args;
2155 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
2156 Args.push_back(std::make_pair(Source, ArgTy));
2157
2158 // We don't care about token chains for libcalls. We just use the entry
2159 // node as our input and ignore the output chain. This allows us to place
2160 // calls wherever we need them to satisfy data dependences.
2161 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002162
2163 std::pair<SDOperand,SDOperand> CallResult =
2164 TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG);
2165
2166 SpliceCallInto(CallResult.second, OutChain, DAG);
2167 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002168}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002169
Chris Lattnere34b3962005-01-19 04:19:40 +00002170
2171
Chris Lattner3e928bb2005-01-07 07:47:09 +00002172/// ExpandOp - Expand the specified SDOperand into its two component pieces
2173/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2174/// LegalizeNodes map is filled in for any results that are not expanded, the
2175/// ExpandedNodes map is filled in for any results that are expanded, and the
2176/// Lo/Hi values are returned.
2177void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2178 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002179 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002180 SDNode *Node = Op.Val;
2181 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2182 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2183 assert(MVT::isInteger(NVT) && NVT < VT &&
2184 "Cannot expand to FP value or to larger int value!");
2185
2186 // If there is more than one use of this, see if we already expanded it.
2187 // There is no use remembering values that only have a single use, as the map
2188 // entries will never be reused.
2189 if (!Node->hasOneUse()) {
2190 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2191 = ExpandedNodes.find(Op);
2192 if (I != ExpandedNodes.end()) {
2193 Lo = I->second.first;
2194 Hi = I->second.second;
2195 return;
2196 }
2197 }
2198
Chris Lattner4e6c7462005-01-08 19:27:05 +00002199 // Expanding to multiple registers needs to perform an optimization step, and
2200 // is not careful to avoid operations the target does not support. Make sure
2201 // that all generated operations are legalized in the next iteration.
2202 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002203
Chris Lattner3e928bb2005-01-07 07:47:09 +00002204 switch (Node->getOpcode()) {
2205 default:
2206 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2207 assert(0 && "Do not know how to expand this operator!");
2208 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002209 case ISD::UNDEF:
2210 Lo = DAG.getNode(ISD::UNDEF, NVT);
2211 Hi = DAG.getNode(ISD::UNDEF, NVT);
2212 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002213 case ISD::Constant: {
2214 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2215 Lo = DAG.getConstant(Cst, NVT);
2216 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2217 break;
2218 }
2219
2220 case ISD::CopyFromReg: {
Chris Lattner18c2f132005-01-13 20:50:02 +00002221 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002222 // Aggregate register values are always in consequtive pairs.
Chris Lattner69a52152005-01-14 22:38:01 +00002223 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2224 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002225
Chris Lattner69a52152005-01-14 22:38:01 +00002226 // Remember that we legalized the chain.
2227 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2228
Chris Lattner3e928bb2005-01-07 07:47:09 +00002229 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2230 break;
2231 }
2232
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002233 case ISD::BUILD_PAIR:
2234 // Legalize both operands. FIXME: in the future we should handle the case
2235 // where the two elements are not legal.
2236 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2237 Lo = LegalizeOp(Node->getOperand(0));
2238 Hi = LegalizeOp(Node->getOperand(1));
2239 break;
2240
Chris Lattneredb1add2005-05-11 04:51:16 +00002241 case ISD::CTPOP:
2242 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002243 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2244 DAG.getNode(ISD::CTPOP, NVT, Lo),
2245 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002246 Hi = DAG.getConstant(0, NVT);
2247 break;
2248
2249 case ISD::CTTZ:
2250 case ISD::CTLZ:
2251 assert(0 && "ct intrinsics cannot be expanded!");
2252
Chris Lattner3e928bb2005-01-07 07:47:09 +00002253 case ISD::LOAD: {
2254 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2255 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002256 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002257
2258 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002259 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002260 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2261 getIntPtrConstant(IncrementSize));
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002262 //Is this safe? declaring that the two parts of the split load
2263 //are from the same instruction?
2264 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002265
2266 // Build a factor node to remember that this load is independent of the
2267 // other one.
2268 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2269 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002270
Chris Lattner3e928bb2005-01-07 07:47:09 +00002271 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002272 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002273 if (!TLI.isLittleEndian())
2274 std::swap(Lo, Hi);
2275 break;
2276 }
2277 case ISD::CALL: {
2278 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2279 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2280
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002281 bool Changed = false;
2282 std::vector<SDOperand> Ops;
2283 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2284 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2285 Changed |= Ops.back() != Node->getOperand(i);
2286 }
2287
Chris Lattner3e928bb2005-01-07 07:47:09 +00002288 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2289 "Can only expand a call once so far, not i64 -> i16!");
2290
2291 std::vector<MVT::ValueType> RetTyVTs;
2292 RetTyVTs.reserve(3);
2293 RetTyVTs.push_back(NVT);
2294 RetTyVTs.push_back(NVT);
2295 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002296 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002297 Lo = SDOperand(NC, 0);
2298 Hi = SDOperand(NC, 1);
2299
2300 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002301 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002302 break;
2303 }
2304 case ISD::AND:
2305 case ISD::OR:
2306 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2307 SDOperand LL, LH, RL, RH;
2308 ExpandOp(Node->getOperand(0), LL, LH);
2309 ExpandOp(Node->getOperand(1), RL, RH);
2310 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2311 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2312 break;
2313 }
2314 case ISD::SELECT: {
2315 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002316
2317 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2318 case Expand: assert(0 && "It's impossible to expand bools");
2319 case Legal:
2320 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2321 break;
2322 case Promote:
2323 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2324 break;
2325 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002326 ExpandOp(Node->getOperand(1), LL, LH);
2327 ExpandOp(Node->getOperand(2), RL, RH);
2328 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2329 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2330 break;
2331 }
2332 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002333 SDOperand In;
2334 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2335 case Expand: assert(0 && "expand-expand not implemented yet!");
2336 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2337 case Promote:
2338 In = PromoteOp(Node->getOperand(0));
2339 // Emit the appropriate sign_extend_inreg to get the value we want.
2340 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2341 Node->getOperand(0).getValueType());
2342 break;
2343 }
2344
Chris Lattner3e928bb2005-01-07 07:47:09 +00002345 // The low part is just a sign extension of the input (which degenerates to
2346 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002347 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002348
Chris Lattner3e928bb2005-01-07 07:47:09 +00002349 // The high part is obtained by SRA'ing all but one of the bits of the lo
2350 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002351 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002352 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2353 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002354 break;
2355 }
Chris Lattner06098e02005-04-03 23:41:52 +00002356 case ISD::ZERO_EXTEND: {
2357 SDOperand In;
2358 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2359 case Expand: assert(0 && "expand-expand not implemented yet!");
2360 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2361 case Promote:
2362 In = PromoteOp(Node->getOperand(0));
2363 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002364 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002365 break;
2366 }
2367
Chris Lattner3e928bb2005-01-07 07:47:09 +00002368 // The low part is just a zero extension of the input (which degenerates to
2369 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002370 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002371
Chris Lattner3e928bb2005-01-07 07:47:09 +00002372 // The high part is just a zero.
2373 Hi = DAG.getConstant(0, NVT);
2374 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002375 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002376 // These operators cannot be expanded directly, emit them as calls to
2377 // library functions.
2378 case ISD::FP_TO_SINT:
2379 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002380 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002381 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002382 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002383 break;
2384 case ISD::FP_TO_UINT:
2385 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002386 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002387 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002388 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002389 break;
2390
Chris Lattnere34b3962005-01-19 04:19:40 +00002391 case ISD::SHL:
2392 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002393 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002394 break;
Chris Lattner47599822005-04-02 03:38:53 +00002395
2396 // If this target supports SHL_PARTS, use it.
2397 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002398 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2399 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002400 break;
2401 }
2402
Chris Lattnere34b3962005-01-19 04:19:40 +00002403 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002404 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002405 break;
2406
2407 case ISD::SRA:
2408 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002409 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002410 break;
Chris Lattner47599822005-04-02 03:38:53 +00002411
2412 // If this target supports SRA_PARTS, use it.
2413 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002414 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2415 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002416 break;
2417 }
2418
Chris Lattnere34b3962005-01-19 04:19:40 +00002419 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002420 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002421 break;
2422 case ISD::SRL:
2423 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002424 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002425 break;
Chris Lattner47599822005-04-02 03:38:53 +00002426
2427 // If this target supports SRL_PARTS, use it.
2428 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00002429 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2430 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00002431 break;
2432 }
2433
Chris Lattnere34b3962005-01-19 04:19:40 +00002434 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00002435 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00002436 break;
2437
Misha Brukmanedf128a2005-04-21 22:36:52 +00002438 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00002439 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2440 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002441 break;
2442 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00002443 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2444 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00002445 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00002446 case ISD::MUL: {
2447 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2448 SDOperand LL, LH, RL, RH;
2449 ExpandOp(Node->getOperand(0), LL, LH);
2450 ExpandOp(Node->getOperand(1), RL, RH);
2451 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2452 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2453 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2454 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2455 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2456 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2457 } else {
2458 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2459 }
2460 break;
2461 }
Chris Lattner77e77a62005-01-21 06:05:23 +00002462 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2463 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2464 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2465 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002466 }
2467
2468 // Remember in a map if the values will be reused later.
2469 if (!Node->hasOneUse()) {
2470 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2471 std::make_pair(Lo, Hi))).second;
2472 assert(isNew && "Value already expanded?!?");
2473 }
2474}
2475
2476
2477// SelectionDAG::Legalize - This is the entry point for the file.
2478//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002479void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00002480 /// run - This is the main entry point to this class.
2481 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002482 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00002483}
2484