blob: 08e8d776fd431778611230e05fcac7ca6382821d [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000021#include "llvm/Constants.h"
22#include <iostream>
23using namespace llvm;
24
25//===----------------------------------------------------------------------===//
26/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
27/// hacks on it until the target machine can handle it. This involves
28/// eliminating value sizes the machine cannot handle (promoting small sizes to
29/// large sizes or splitting up large values into small values) as well as
30/// eliminating operations the machine cannot handle.
31///
32/// This code also does a small amount of optimization and recognition of idioms
33/// as part of its processing. For example, if a target does not support a
34/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
35/// will attempt merge setcc and brc instructions into brcc's.
36///
37namespace {
38class SelectionDAGLegalize {
39 TargetLowering &TLI;
40 SelectionDAG &DAG;
41
42 /// LegalizeAction - This enum indicates what action we should take for each
43 /// value type the can occur in the program.
44 enum LegalizeAction {
45 Legal, // The target natively supports this value type.
46 Promote, // This should be promoted to the next larger type.
47 Expand, // This integer type should be broken into smaller pieces.
48 };
49
Chris Lattnerdc750592005-01-07 07:47:09 +000050 /// ValueTypeActions - This is a bitvector that contains two bits for each
51 /// value type, where the two bits correspond to the LegalizeAction enum.
52 /// This can be queried with "getTypeAction(VT)".
53 unsigned ValueTypeActions;
54
55 /// NeedsAnotherIteration - This is set when we expand a large integer
56 /// operation into smaller integer operations, but the smaller operations are
57 /// not set. This occurs only rarely in practice, for targets that don't have
58 /// 32-bit or larger integer registers.
59 bool NeedsAnotherIteration;
60
61 /// LegalizedNodes - For nodes that are of legal width, and that have more
62 /// than one use, this map indicates what regularized operand to use. This
63 /// allows us to avoid legalizing the same thing more than once.
64 std::map<SDOperand, SDOperand> LegalizedNodes;
65
Chris Lattner1f2c9d82005-01-15 05:21:40 +000066 /// PromotedNodes - For nodes that are below legal width, and that have more
67 /// than one use, this map indicates what promoted value to use. This allows
68 /// us to avoid promoting the same thing more than once.
69 std::map<SDOperand, SDOperand> PromotedNodes;
70
Chris Lattnerdc750592005-01-07 07:47:09 +000071 /// ExpandedNodes - For nodes that need to be expanded, and which have more
72 /// than one use, this map indicates which which operands are the expanded
73 /// version of the input. This allows us to avoid expanding the same node
74 /// more than once.
75 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
76
Chris Lattnerea4ca942005-01-07 22:28:47 +000077 void AddLegalizedOperand(SDOperand From, SDOperand To) {
78 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
79 assert(isNew && "Got into the map somehow?");
80 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000081 void AddPromotedOperand(SDOperand From, SDOperand To) {
82 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
83 assert(isNew && "Got into the map somehow?");
84 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000085
Chris Lattnerdc750592005-01-07 07:47:09 +000086public:
87
Chris Lattner4add7e32005-01-23 04:42:50 +000088 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000089
90 /// Run - While there is still lowering to do, perform a pass over the DAG.
91 /// Most regularization can be done in a single pass, but targets that require
92 /// large values to be split into registers multiple times (e.g. i64 -> 4x
93 /// i16) require iteration for these values (the first iteration will demote
94 /// to i32, the second will demote to i16).
95 void Run() {
96 do {
97 NeedsAnotherIteration = false;
98 LegalizeDAG();
99 } while (NeedsAnotherIteration);
100 }
101
102 /// getTypeAction - Return how we should legalize values of this type, either
103 /// it is already legal or we need to expand it into multiple registers of
104 /// smaller integer type, or we need to promote it to a larger type.
105 LegalizeAction getTypeAction(MVT::ValueType VT) const {
106 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
107 }
108
109 /// isTypeLegal - Return true if this type is legal on this target.
110 ///
111 bool isTypeLegal(MVT::ValueType VT) const {
112 return getTypeAction(VT) == Legal;
113 }
114
115private:
116 void LegalizeDAG();
117
118 SDOperand LegalizeOp(SDOperand O);
119 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000120 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000121
Chris Lattneraac464e2005-01-21 06:05:23 +0000122 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
123 SDOperand &Hi);
124 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
125 SDOperand Source);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000126 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
127 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000128 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
129 SDOperand &Lo, SDOperand &Hi);
130 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000131 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000132
Chris Lattnerdc750592005-01-07 07:47:09 +0000133 SDOperand getIntPtrConstant(uint64_t Val) {
134 return DAG.getConstant(Val, TLI.getPointerTy());
135 }
136};
137}
138
139
Chris Lattner4add7e32005-01-23 04:42:50 +0000140SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
141 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
142 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000143 assert(MVT::LAST_VALUETYPE <= 16 &&
144 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000145}
146
Chris Lattnerdc750592005-01-07 07:47:09 +0000147void SelectionDAGLegalize::LegalizeDAG() {
148 SDOperand OldRoot = DAG.getRoot();
149 SDOperand NewRoot = LegalizeOp(OldRoot);
150 DAG.setRoot(NewRoot);
151
152 ExpandedNodes.clear();
153 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000154 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000155
156 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000157 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000158}
159
160SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000161 assert(getTypeAction(Op.getValueType()) == Legal &&
162 "Caller should expand or promote operands that are not legal!");
163
Chris Lattnerdc750592005-01-07 07:47:09 +0000164 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000165 // register on this target, make sure to expand or promote them.
166 if (Op.Val->getNumValues() > 1) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000167 for (unsigned i = 0, e = Op.Val->getNumValues(); i != e; ++i)
168 switch (getTypeAction(Op.Val->getValueType(i))) {
169 case Legal: break; // Nothing to do.
170 case Expand: {
171 SDOperand T1, T2;
172 ExpandOp(Op.getValue(i), T1, T2);
173 assert(LegalizedNodes.count(Op) &&
174 "Expansion didn't add legal operands!");
175 return LegalizedNodes[Op];
176 }
177 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000178 PromoteOp(Op.getValue(i));
179 assert(LegalizedNodes.count(Op) &&
180 "Expansion didn't add legal operands!");
181 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000182 }
183 }
184
Chris Lattner85d70c62005-01-11 05:57:22 +0000185 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
186 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000187
Chris Lattnerec26b482005-01-09 19:03:49 +0000188 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000189
190 SDOperand Result = Op;
191 SDNode *Node = Op.Val;
Chris Lattnerdc750592005-01-07 07:47:09 +0000192
193 switch (Node->getOpcode()) {
194 default:
195 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
196 assert(0 && "Do not know how to legalize this operator!");
197 abort();
198 case ISD::EntryToken:
199 case ISD::FrameIndex:
200 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000201 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000202 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000203 assert(getTypeAction(Node->getValueType(0)) == Legal &&
204 "This must be legal!");
205 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000206 case ISD::CopyFromReg:
207 Tmp1 = LegalizeOp(Node->getOperand(0));
208 if (Tmp1 != Node->getOperand(0))
209 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
210 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000211 else
212 Result = Op.getValue(0);
213
214 // Since CopyFromReg produces two values, make sure to remember that we
215 // legalized both of them.
216 AddLegalizedOperand(Op.getValue(0), Result);
217 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
218 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000219 case ISD::ImplicitDef:
220 Tmp1 = LegalizeOp(Node->getOperand(0));
221 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000222 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000223 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000224 case ISD::UNDEF: {
225 MVT::ValueType VT = Op.getValueType();
226 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000227 default: assert(0 && "This action is not supported yet!");
228 case TargetLowering::Expand:
229 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000230 if (MVT::isInteger(VT))
231 Result = DAG.getConstant(0, VT);
232 else if (MVT::isFloatingPoint(VT))
233 Result = DAG.getConstantFP(0, VT);
234 else
235 assert(0 && "Unknown value type!");
236 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000237 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000238 break;
239 }
240 break;
241 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000242 case ISD::Constant:
243 // We know we don't need to expand constants here, constants only have one
244 // value and we check that it is fine above.
245
246 // FIXME: Maybe we should handle things like targets that don't support full
247 // 32-bit immediates?
248 break;
249 case ISD::ConstantFP: {
250 // Spill FP immediates to the constant pool if the target cannot directly
251 // codegen them. Targets often have some immediate values that can be
252 // efficiently generated into an FP register without a load. We explicitly
253 // leave these constants as ConstantFP nodes for the target to deal with.
254
255 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
256
257 // Check to see if this FP immediate is already legal.
258 bool isLegal = false;
259 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
260 E = TLI.legal_fpimm_end(); I != E; ++I)
261 if (CFP->isExactlyValue(*I)) {
262 isLegal = true;
263 break;
264 }
265
266 if (!isLegal) {
267 // Otherwise we need to spill the constant to memory.
268 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
269
270 bool Extend = false;
271
272 // If a FP immediate is precise when represented as a float, we put it
273 // into the constant pool as a float, even if it's is statically typed
274 // as a double.
275 MVT::ValueType VT = CFP->getValueType(0);
276 bool isDouble = VT == MVT::f64;
277 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
278 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000279 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
280 // Only do this if the target has a native EXTLOAD instruction from
281 // f32.
282 TLI.getOperationAction(ISD::EXTLOAD,
283 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000284 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
285 VT = MVT::f32;
286 Extend = true;
287 }
Misha Brukman835702a2005-04-21 22:36:52 +0000288
Chris Lattnerdc750592005-01-07 07:47:09 +0000289 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
290 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000291 if (Extend) {
292 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000293 DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000294 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000295 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
296 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000297 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000298 }
299 break;
300 }
Chris Lattner05b4e372005-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 Lattner55562fa2005-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 Lattner05b4e372005-01-13 17:59:25 +0000315 }
316 if (Changed)
317 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
318 break;
319 }
320
Chris Lattnerdc750592005-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 Lattnerec26b482005-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 Lattner02f5ce22005-01-09 19:07:54 +0000337 else
338 Result = Op.getValue(0);
Chris Lattnerec26b482005-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 Lattner3d95c142005-01-19 20:24:35 +0000346 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000347 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
348 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-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 Lattnerdc750592005-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 Lattnerf025d672005-01-07 21:34:13 +0000361 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d95c142005-01-19 20:24:35 +0000362 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000363 } else {
364 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000365 }
Chris Lattner9242c502005-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 Lattner3d95c142005-01-19 20:24:35 +0000371 }
Chris Lattner68a12142005-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 Lattnerec3fe7c2005-01-07 08:19:42 +0000378 case ISD::BRCOND:
379 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-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 Lattnerec3fe7c2005-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 Lattnerfd986782005-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 Lattnerec3fe7c2005-01-07 08:19:42 +0000428
Chris Lattnerdc750592005-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 Lenharth4a73c2c2005-04-27 20:10:01 +0000432
Chris Lattnerdc750592005-01-07 07:47:09 +0000433 if (Tmp1 != Node->getOperand(0) ||
434 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000435 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
436 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000437 else
438 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000439
Chris Lattnerea4ca942005-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 Lattnerdc750592005-01-07 07:47:09 +0000445
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000446 case ISD::EXTLOAD:
447 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000448 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000449 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
450 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000451
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000452 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
453 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000454 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-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 Lenharth4a73c2c2005-04-27 20:10:01 +0000458 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-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 Brukman835702a2005-04-21 22:36:52 +0000464
Chris Lattnera3b7ef02005-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 Lenharth4a73c2c2005-04-27 20:10:01 +0000469 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-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 Lattnera3b7ef02005-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 Lenharth4a73c2c2005-04-27 20:10:01 +0000484 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-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 Lattnera3b7ef02005-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 Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +0000510
Chris Lattnerdc750592005-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 Lattnere727af02005-01-13 20:50:02 +0000516 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000517 break;
Chris Lattner9f2c4a52005-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 Lattnerdc750592005-01-07 07:47:09 +0000523 SDOperand Lo, Hi;
Misha Brukman835702a2005-04-21 22:36:52 +0000524 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000525 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-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 Lattnerdc750592005-01-07 07:47:09 +0000530 assert(isTypeLegal(Result.getValueType()) &&
531 "Cannot expand multiple times yet (i64 -> i16)");
532 break;
533 }
Chris Lattnerdc750592005-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 Lattnerea4ca942005-01-07 22:28:47 +0000543 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +0000550 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000551 }
552 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000553 Tmp2 = PromoteOp(Node->getOperand(1));
554 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
555 break;
Chris Lattnerdc750592005-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 Lattner7e6eeba2005-01-08 19:27:05 +0000568 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +0000575 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000576 }
577 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000578 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-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 Lattnere69daaf2005-01-08 06:25:56 +0000589 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000590 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-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 Lenharth4a73c2c2005-04-27 20:10:01 +0000597 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000598 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000599 Node->getOperand(3));
Chris Lattnere69daaf2005-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 Lenharth4a73c2c2005-04-27 20:10:01 +0000607 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner5385db52005-05-09 20:23:03 +0000608 DAG.getConstant(V.I, MVT::i64), Tmp2,
609 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000610 }
Chris Lattnera4743132005-02-22 07:23:39 +0000611 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000612 }
613
Chris Lattnerdc750592005-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 Lattner5385db52005-05-09 20:23:03 +0000619 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
620 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000621 break;
622 }
623 case Promote:
Chris Lattner1f2c9d82005-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 Lenharth4a73c2c2005-04-27 20:10:01 +0000627 Node->getOperand(3),
628 Node->getOperand(1).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000629 break;
630
Chris Lattnerdc750592005-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 Lattner55e9cde2005-05-11 04:51:16 +0000638 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
639 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000640 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-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 Lenharth4a73c2c2005-04-27 20:10:01 +0000645 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +0000646 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
647 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000648 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
649 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000650 }
651 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000652 case ISD::PCMARKER:
653 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000654 if (Tmp1 != Node->getOperand(0))
655 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000656 break;
Chris Lattnerc6c9a5b2005-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 Lattner99222f72005-01-15 07:15:18 +0000666 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000667 Node->getOperand(3),
Chris Lattnerc6c9a5b2005-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 Lattner39c67442005-01-14 22:08:15 +0000675 case ISD::SELECT:
Chris Lattnerd65c3f32005-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 Lattnerdc750592005-01-07 07:47:09 +0000685 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000686 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-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 Lattnerdc750592005-01-07 07:47:09 +0000716 break;
Chris Lattnerdc750592005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000724 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000725 break;
726 case Promote:
Chris Lattner4d978642005-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 Lattner87a769c2005-01-16 01:11:45 +0000733 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-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 Lattner0e852af2005-04-13 02:38:47 +0000750 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
751 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000764 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000765 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000766 case Expand:
Chris Lattnerdc750592005-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 Lattner71ff44e2005-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 Brukman835702a2005-04-21 22:36:52 +0000778 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner71ff44e2005-04-12 01:46:05 +0000779 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukman835702a2005-04-21 22:36:52 +0000780 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +0000781 }
782
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +0000786 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000787 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000788 DAG.getConstant(0, Tmp1.getValueType()));
789 break;
790 default:
Chris Lattneraedcabe2005-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 Brukman835702a2005-04-21 22:36:52 +0000794 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattneraedcabe2005-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 Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +0000814
Chris Lattnerdc750592005-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 Lattnerb07e2d22005-01-18 02:52:03 +0000821 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000822 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-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 Lattnerdc750592005-01-07 07:47:09 +0000827 break;
828 }
829 }
830 break;
831
Chris Lattner85d70c62005-01-11 05:57:22 +0000832 case ISD::MEMSET:
833 case ISD::MEMCPY:
834 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000835 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-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 Lattner4487b2e2005-02-01 18:38:28 +0000842 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000843 break;
844 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000845 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000846 break;
847 }
848 } else {
Misha Brukman835702a2005-04-21 22:36:52 +0000849 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000850 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000851
852 SDOperand Tmp4;
853 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000854 case Expand: assert(0 && "Cannot expand this yet!");
855 case Legal:
856 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000857 break;
858 case Promote:
859 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-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 Lattnera4cfafe2005-01-28 22:29:18 +0000870 Tmp5 = PromoteOp(Node->getOperand(4));
871 break;
872 }
Chris Lattner3c0dd462005-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 Lattner85d70c62005-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 Lattner3c0dd462005-01-16 07:29:19 +0000885 break;
886 case TargetLowering::Expand: {
Chris Lattner85d70c62005-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 Spencer6dced922005-01-12 14:53:45 +0000893 const char *FnName = 0;
Chris Lattner85d70c62005-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 Lattner06bbeb62005-05-11 19:02:11 +0000911 // FIXME: THESE SHOULD USE ExpandLibCall ??!?
Chris Lattner85d70c62005-01-11 05:57:22 +0000912 std::pair<SDOperand,SDOperand> CallResult =
Nate Begemanf6565252005-03-26 01:29:23 +0000913 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattner85d70c62005-01-11 05:57:22 +0000914 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
915 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-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 Lattner85d70c62005-01-11 05:57:22 +0000926 }
927 break;
928 }
Chris Lattner5385db52005-05-09 20:23:03 +0000929
930 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +0000931 Tmp1 = LegalizeOp(Node->getOperand(0));
932 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000933
Chris Lattner5385db52005-05-09 20:23:03 +0000934 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000935 Result = DAG.getNode(ISD::READPORT, Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner5385db52005-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 Lattner5385db52005-05-09 20:23:03 +0000943 case ISD::WRITEPORT:
Chris Lattner5385db52005-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 Lattnerba45e6c2005-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 Lattnerb3f83b282005-01-20 18:52:28 +00001004 case ISD::ADD_PARTS:
Chris Lattner4157c412005-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 Lattnerb3f83b282005-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 Lattner13fe99c2005-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 Lattnerb3f83b282005-01-20 18:52:28 +00001023 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001024
1025 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001026 case ISD::ADD:
1027 case ISD::SUB:
1028 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001029 case ISD::MULHS:
1030 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001031 case ISD::UDIV:
1032 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001033 case ISD::AND:
1034 case ISD::OR:
1035 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001036 case ISD::SHL:
1037 case ISD::SRL:
1038 case ISD::SRA:
Chris Lattnerdc750592005-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 Brukman835702a2005-04-21 22:36:52 +00001045
Nate Begeman20b7d2a2005-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 Brukman835702a2005-04-21 22:36:52 +00001054 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-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 Lattner13fe99c2005-04-02 05:00:07 +00001070
Andrew Lenharth5e177822005-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 Lattner55e9cde2005-05-11 04:51:16 +00001083
1084 // Zero extend the argument.
Andrew Lenharth5e177822005-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 Lenharth2dbbb3a2005-05-05 15:55:21 +00001112 switch(Node->getOpcode())
1113 {
1114 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001115 static const uint64_t mask[6] = {
1116 0x5555555555555555ULL, 0x3333333333333333ULL,
1117 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1118 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1119 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001120 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-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 Lenharth2dbbb3a2005-05-05 15:55:21 +00001124 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001125 Tmp2 = DAG.getConstant(mask[i], VT);
1126 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharth2dbbb3a2005-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 Madinaa1ebbac2005-05-11 08:45:08 +00001136 case ISD::CTLZ: {
1137 /* for now, we do this:
Chris Lattner56add052005-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 Madinaa1ebbac2005-05-11 08:45:08 +00001144
Chris Lattner56add052005-05-11 18:35:21 +00001145 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1146 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-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 Lattner56add052005-05-11 18:35:21 +00001155 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001156 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001157 }
1158 case ISD::CTTZ: {
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001159 // for now, we use: { return popcount(~x & (x - 1)); }
1160 // unless the target has ctlz but not ctpop, in which case we use:
1161 // { return 32 - nlz(~x & (x-1)); }
1162 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001163 MVT::ValueType VT = Tmp1.getValueType();
1164 Tmp2 = DAG.getConstant(~0ULL, VT);
1165 Tmp3 = DAG.getNode(ISD::AND, VT,
1166 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1167 DAG.getNode(ISD::SUB, VT, Tmp1,
1168 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001169 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1170 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1171 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
1172 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
1173 DAG.getConstant(getSizeInBits(VT), VT),
1174 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1175 } else {
1176 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1177 }
Chris Lattner72473242005-05-11 05:27:09 +00001178 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001179 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001180 default:
1181 assert(0 && "Cannot expand this yet!");
1182 break;
1183 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001184 break;
1185 }
1186 break;
1187
Chris Lattner13fe99c2005-04-02 05:00:07 +00001188 // Unary operators
1189 case ISD::FABS:
1190 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001191 case ISD::FSQRT:
1192 case ISD::FSIN:
1193 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001194 Tmp1 = LegalizeOp(Node->getOperand(0));
1195 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1196 case TargetLowering::Legal:
1197 if (Tmp1 != Node->getOperand(0))
1198 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1199 break;
1200 case TargetLowering::Promote:
1201 case TargetLowering::Custom:
1202 assert(0 && "Cannot promote/custom handle this yet!");
1203 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001204 switch(Node->getOpcode()) {
1205 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001206 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1207 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1208 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1209 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001210 break;
1211 }
1212 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001213 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1214 MVT::ValueType VT = Node->getValueType(0);
1215 Tmp2 = DAG.getConstantFP(0.0, VT);
1216 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1217 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1218 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1219 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001220 break;
1221 }
1222 case ISD::FSQRT:
1223 case ISD::FSIN:
1224 case ISD::FCOS: {
1225 MVT::ValueType VT = Node->getValueType(0);
1226 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1227 const char *FnName = 0;
1228 switch(Node->getOpcode()) {
1229 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1230 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1231 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1232 default: assert(0 && "Unreachable!");
1233 }
1234 std::vector<std::pair<SDOperand, const Type*> > Args;
1235 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner06bbeb62005-05-11 19:02:11 +00001236 // FIXME: should use ExpandLibCall!
Chris Lattner80026402005-04-30 04:43:14 +00001237 std::pair<SDOperand,SDOperand> CallResult =
1238 TLI.LowerCallTo(DAG.getEntryNode(), T, false,
1239 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1240 Result = LegalizeOp(CallResult.first);
1241 break;
1242 }
1243 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001244 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001245 }
1246 break;
1247 }
1248 break;
1249
1250 // Conversion operators. The source and destination have different types.
Chris Lattnerdc750592005-01-07 07:47:09 +00001251 case ISD::ZERO_EXTEND:
1252 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +00001253 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001254 case ISD::FP_EXTEND:
1255 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001256 case ISD::FP_TO_SINT:
1257 case ISD::FP_TO_UINT:
1258 case ISD::SINT_TO_FP:
1259 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +00001260 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1261 case Legal:
1262 Tmp1 = LegalizeOp(Node->getOperand(0));
1263 if (Tmp1 != Node->getOperand(0))
1264 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1265 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001266 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001267 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1268 Node->getOpcode() == ISD::UINT_TO_FP) {
1269 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1270 Node->getValueType(0), Node->getOperand(0));
1271 Result = LegalizeOp(Result);
1272 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001273 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1274 // In the expand case, we must be dealing with a truncate, because
1275 // otherwise the result would be larger than the source.
1276 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +00001277
Chris Lattner13fe99c2005-04-02 05:00:07 +00001278 // Since the result is legal, we should just be able to truncate the low
1279 // part of the source.
1280 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1281 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001282 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001283 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001284
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001285 case Promote:
1286 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001287 case ISD::ZERO_EXTEND:
1288 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001289 // NOTE: Any extend would work here...
1290 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001291 Result = DAG.getZeroExtendInReg(Result,
1292 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001293 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001294 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001295 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001296 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001297 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001298 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1299 Result, Node->getOperand(0).getValueType());
1300 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001301 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001302 Result = PromoteOp(Node->getOperand(0));
1303 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1304 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001305 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001306 Result = PromoteOp(Node->getOperand(0));
1307 if (Result.getValueType() != Op.getValueType())
1308 // Dynamically dead while we have only 2 FP types.
1309 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1310 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001311 case ISD::FP_ROUND:
1312 case ISD::FP_TO_SINT:
1313 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001314 Result = PromoteOp(Node->getOperand(0));
1315 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1316 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001317 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001318 Result = PromoteOp(Node->getOperand(0));
1319 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1320 Result, Node->getOperand(0).getValueType());
1321 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1322 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001323 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001324 Result = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001325 Result = DAG.getZeroExtendInReg(Result,
1326 Node->getOperand(0).getValueType());
Chris Lattner3ba56b32005-01-16 05:06:12 +00001327 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1328 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001329 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001330 }
1331 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001332 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001333 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001334 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +00001335 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1336
1337 // If this operation is not supported, convert it to a shl/shr or load/store
1338 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001339 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1340 default: assert(0 && "This action not supported for this op yet!");
1341 case TargetLowering::Legal:
1342 if (Tmp1 != Node->getOperand(0))
1343 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1344 ExtraVT);
1345 break;
1346 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001347 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001348 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001349 // NOTE: we could fall back on load/store here too for targets without
1350 // SAR. However, it is doubtful that any exist.
1351 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1352 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001353 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001354 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1355 Node->getOperand(0), ShiftCst);
1356 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1357 Result, ShiftCst);
1358 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1359 // The only way we can lower this is to turn it into a STORETRUNC,
1360 // EXTLOAD pair, targetting a temporary location (a stack slot).
1361
1362 // NOTE: there is a choice here between constantly creating new stack
1363 // slots and always reusing the same one. We currently always create
1364 // new ones, as reuse may inhibit scheduling.
1365 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1366 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1367 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1368 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001369 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001370 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1371 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1372 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001373 Node->getOperand(0), StackSlot,
1374 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001375 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001376 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001377 } else {
1378 assert(0 && "Unknown op");
1379 }
1380 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001381 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001382 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001383 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001384 }
Chris Lattner99222f72005-01-15 07:15:18 +00001385 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001386
Chris Lattnerea4ca942005-01-07 22:28:47 +00001387 if (!Op.Val->hasOneUse())
1388 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001389
1390 return Result;
1391}
1392
Chris Lattner4d978642005-01-15 22:16:26 +00001393/// PromoteOp - Given an operation that produces a value in an invalid type,
1394/// promote it to compute the value into a larger type. The produced value will
1395/// have the correct bits for the low portion of the register, but no guarantee
1396/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001397SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1398 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001399 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001400 assert(getTypeAction(VT) == Promote &&
1401 "Caller should expand or legalize operands that are not promotable!");
1402 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1403 "Cannot promote to smaller type!");
1404
1405 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1406 if (I != PromotedNodes.end()) return I->second;
1407
1408 SDOperand Tmp1, Tmp2, Tmp3;
1409
1410 SDOperand Result;
1411 SDNode *Node = Op.Val;
1412
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001413 // Promotion needs an optimization step to clean up after it, and is not
1414 // careful to avoid operations the target does not support. Make sure that
1415 // all generated operations are legalized in the next iteration.
1416 NeedsAnotherIteration = true;
1417
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001418 switch (Node->getOpcode()) {
1419 default:
1420 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1421 assert(0 && "Do not know how to promote this operator!");
1422 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001423 case ISD::UNDEF:
1424 Result = DAG.getNode(ISD::UNDEF, NVT);
1425 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001426 case ISD::Constant:
1427 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1428 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1429 break;
1430 case ISD::ConstantFP:
1431 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1432 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1433 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001434 case ISD::CopyFromReg:
1435 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1436 Node->getOperand(0));
1437 // Remember that we legalized the chain.
1438 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1439 break;
1440
Chris Lattner2cb338d2005-01-18 02:59:52 +00001441 case ISD::SETCC:
1442 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1443 "SetCC type is not legal??");
1444 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1445 TLI.getSetCCResultTy(), Node->getOperand(0),
1446 Node->getOperand(1));
1447 Result = LegalizeOp(Result);
1448 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001449
1450 case ISD::TRUNCATE:
1451 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1452 case Legal:
1453 Result = LegalizeOp(Node->getOperand(0));
1454 assert(Result.getValueType() >= NVT &&
1455 "This truncation doesn't make sense!");
1456 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1457 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1458 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001459 case Promote:
1460 // The truncation is not required, because we don't guarantee anything
1461 // about high bits anyway.
1462 Result = PromoteOp(Node->getOperand(0));
1463 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001464 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001465 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1466 // Truncate the low part of the expanded value to the result type
Misha Brukman835702a2005-04-21 22:36:52 +00001467 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001468 }
1469 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001470 case ISD::SIGN_EXTEND:
1471 case ISD::ZERO_EXTEND:
1472 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1473 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1474 case Legal:
1475 // Input is legal? Just do extend all the way to the larger type.
1476 Result = LegalizeOp(Node->getOperand(0));
1477 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1478 break;
1479 case Promote:
1480 // Promote the reg if it's smaller.
1481 Result = PromoteOp(Node->getOperand(0));
1482 // The high bits are not guaranteed to be anything. Insert an extend.
1483 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001484 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1485 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001486 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001487 Result = DAG.getZeroExtendInReg(Result,
1488 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001489 break;
1490 }
1491 break;
1492
1493 case ISD::FP_EXTEND:
1494 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1495 case ISD::FP_ROUND:
1496 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1497 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1498 case Promote: assert(0 && "Unreachable with 2 FP types!");
1499 case Legal:
1500 // Input is legal? Do an FP_ROUND_INREG.
1501 Result = LegalizeOp(Node->getOperand(0));
1502 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1503 break;
1504 }
1505 break;
1506
1507 case ISD::SINT_TO_FP:
1508 case ISD::UINT_TO_FP:
1509 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1510 case Legal:
1511 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001512 // No extra round required here.
1513 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001514 break;
1515
1516 case Promote:
1517 Result = PromoteOp(Node->getOperand(0));
1518 if (Node->getOpcode() == ISD::SINT_TO_FP)
1519 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1520 Result, Node->getOperand(0).getValueType());
1521 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001522 Result = DAG.getZeroExtendInReg(Result,
1523 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001524 // No extra round required here.
1525 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001526 break;
1527 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001528 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1529 Node->getOperand(0));
1530 Result = LegalizeOp(Result);
1531
1532 // Round if we cannot tolerate excess precision.
1533 if (NoExcessFPPrecision)
1534 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1535 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001536 }
Chris Lattner4d978642005-01-15 22:16:26 +00001537 break;
1538
1539 case ISD::FP_TO_SINT:
1540 case ISD::FP_TO_UINT:
1541 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1542 case Legal:
1543 Tmp1 = LegalizeOp(Node->getOperand(0));
1544 break;
1545 case Promote:
1546 // The input result is prerounded, so we don't have to do anything
1547 // special.
1548 Tmp1 = PromoteOp(Node->getOperand(0));
1549 break;
1550 case Expand:
1551 assert(0 && "not implemented");
1552 }
1553 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1554 break;
1555
Chris Lattner13fe99c2005-04-02 05:00:07 +00001556 case ISD::FABS:
1557 case ISD::FNEG:
1558 Tmp1 = PromoteOp(Node->getOperand(0));
1559 assert(Tmp1.getValueType() == NVT);
1560 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1561 // NOTE: we do not have to do any extra rounding here for
1562 // NoExcessFPPrecision, because we know the input will have the appropriate
1563 // precision, and these operations don't modify precision at all.
1564 break;
1565
Chris Lattner9d6fa982005-04-28 21:44:33 +00001566 case ISD::FSQRT:
1567 case ISD::FSIN:
1568 case ISD::FCOS:
1569 Tmp1 = PromoteOp(Node->getOperand(0));
1570 assert(Tmp1.getValueType() == NVT);
1571 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1572 if(NoExcessFPPrecision)
1573 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1574 break;
1575
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001576 case ISD::AND:
1577 case ISD::OR:
1578 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001579 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001580 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001581 case ISD::MUL:
1582 // The input may have strange things in the top bits of the registers, but
1583 // these operations don't care. They may have wierd bits going out, but
1584 // that too is okay if they are integer operations.
1585 Tmp1 = PromoteOp(Node->getOperand(0));
1586 Tmp2 = PromoteOp(Node->getOperand(1));
1587 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1588 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1589
1590 // However, if this is a floating point operation, they will give excess
1591 // precision that we may not be able to tolerate. If we DO allow excess
1592 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001593 // FIXME: Why would we need to round FP ops more than integer ones?
1594 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001595 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1596 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1597 break;
1598
Chris Lattner4d978642005-01-15 22:16:26 +00001599 case ISD::SDIV:
1600 case ISD::SREM:
1601 // These operators require that their input be sign extended.
1602 Tmp1 = PromoteOp(Node->getOperand(0));
1603 Tmp2 = PromoteOp(Node->getOperand(1));
1604 if (MVT::isInteger(NVT)) {
1605 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001606 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001607 }
1608 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1609
1610 // Perform FP_ROUND: this is probably overly pessimistic.
1611 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1612 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1613 break;
1614
1615 case ISD::UDIV:
1616 case ISD::UREM:
1617 // These operators require that their input be zero extended.
1618 Tmp1 = PromoteOp(Node->getOperand(0));
1619 Tmp2 = PromoteOp(Node->getOperand(1));
1620 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00001621 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1622 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001623 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1624 break;
1625
1626 case ISD::SHL:
1627 Tmp1 = PromoteOp(Node->getOperand(0));
1628 Tmp2 = LegalizeOp(Node->getOperand(1));
1629 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1630 break;
1631 case ISD::SRA:
1632 // The input value must be properly sign extended.
1633 Tmp1 = PromoteOp(Node->getOperand(0));
1634 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1635 Tmp2 = LegalizeOp(Node->getOperand(1));
1636 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1637 break;
1638 case ISD::SRL:
1639 // The input value must be properly zero extended.
1640 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001641 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001642 Tmp2 = LegalizeOp(Node->getOperand(1));
1643 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1644 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001645 case ISD::LOAD:
1646 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1647 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001648 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001649 if (MVT::isInteger(NVT))
Chris Lattner5385db52005-05-09 20:23:03 +00001650 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1651 VT);
Chris Lattner391a3512005-04-10 17:40:35 +00001652 else
Chris Lattner5385db52005-05-09 20:23:03 +00001653 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1654 VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001655
1656 // Remember that we legalized the chain.
1657 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1658 break;
1659 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001660 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1661 case Expand: assert(0 && "It's impossible to expand bools");
1662 case Legal:
1663 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1664 break;
1665 case Promote:
1666 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1667 break;
1668 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001669 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1670 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1671 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1672 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001673 case ISD::CALL: {
1674 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1675 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1676
Chris Lattner3d95c142005-01-19 20:24:35 +00001677 std::vector<SDOperand> Ops;
1678 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1679 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1680
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001681 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1682 "Can only promote single result calls");
1683 std::vector<MVT::ValueType> RetTyVTs;
1684 RetTyVTs.reserve(2);
1685 RetTyVTs.push_back(NVT);
1686 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001687 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001688 Result = SDOperand(NC, 0);
1689
1690 // Insert the new chain mapping.
1691 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1692 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001693 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00001694 case ISD::CTPOP:
1695 case ISD::CTTZ:
1696 case ISD::CTLZ:
1697 Tmp1 = Node->getOperand(0);
1698 //Zero extend the argument
1699 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1700 // Perform the larger operation, then subtract if needed.
1701 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1702 switch(Node->getOpcode())
1703 {
1704 case ISD::CTPOP:
1705 Result = Tmp1;
1706 break;
1707 case ISD::CTTZ:
1708 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1709 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1710 DAG.getConstant(getSizeInBits(NVT), NVT));
1711 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1712 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1713 break;
1714 case ISD::CTLZ:
1715 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1716 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1717 DAG.getConstant(getSizeInBits(NVT) -
1718 getSizeInBits(VT), NVT));
1719 break;
1720 }
1721 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001722 }
1723
1724 assert(Result.Val && "Didn't set a result!");
1725 AddPromotedOperand(Op, Result);
1726 return Result;
1727}
Chris Lattnerdc750592005-01-07 07:47:09 +00001728
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001729/// ExpandAddSub - Find a clever way to expand this add operation into
1730/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001731void SelectionDAGLegalize::
1732ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1733 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001734 // Expand the subcomponents.
1735 SDOperand LHSL, LHSH, RHSL, RHSH;
1736 ExpandOp(LHS, LHSL, LHSH);
1737 ExpandOp(RHS, RHSL, RHSH);
1738
Chris Lattner8ffd0042005-04-11 20:29:59 +00001739 // FIXME: this should be moved to the dag combiner someday.
1740 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1741 if (LHSL.getValueType() == MVT::i32) {
1742 SDOperand LowEl;
1743 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1744 if (C->getValue() == 0)
1745 LowEl = RHSL;
1746 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1747 if (C->getValue() == 0)
1748 LowEl = LHSL;
1749 if (LowEl.Val) {
1750 // Turn this into an add/sub of the high part only.
1751 SDOperand HiEl =
1752 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1753 LowEl.getValueType(), LHSH, RHSH);
1754 Lo = LowEl;
1755 Hi = HiEl;
1756 return;
1757 }
1758 }
1759
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001760 std::vector<SDOperand> Ops;
1761 Ops.push_back(LHSL);
1762 Ops.push_back(LHSH);
1763 Ops.push_back(RHSL);
1764 Ops.push_back(RHSH);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001765 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001766 Hi = Lo.getValue(1);
1767}
1768
Chris Lattner4157c412005-04-02 04:00:59 +00001769void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1770 SDOperand Op, SDOperand Amt,
1771 SDOperand &Lo, SDOperand &Hi) {
1772 // Expand the subcomponents.
1773 SDOperand LHSL, LHSH;
1774 ExpandOp(Op, LHSL, LHSH);
1775
1776 std::vector<SDOperand> Ops;
1777 Ops.push_back(LHSL);
1778 Ops.push_back(LHSH);
1779 Ops.push_back(Amt);
1780 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1781 Hi = Lo.getValue(1);
1782}
1783
1784
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001785/// ExpandShift - Try to find a clever way to expand this shift operation out to
1786/// smaller elements. If we can't find a way that is more efficient than a
1787/// libcall on this target, return false. Otherwise, return true with the
1788/// low-parts expanded into Lo and Hi.
1789bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1790 SDOperand &Lo, SDOperand &Hi) {
1791 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1792 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00001793
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001794 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00001795 SDOperand ShAmt = LegalizeOp(Amt);
1796 MVT::ValueType ShTy = ShAmt.getValueType();
1797 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1798 unsigned NVTBits = MVT::getSizeInBits(NVT);
1799
1800 // Handle the case when Amt is an immediate. Other cases are currently broken
1801 // and are disabled.
1802 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1803 unsigned Cst = CN->getValue();
1804 // Expand the incoming operand to be shifted, so that we have its parts
1805 SDOperand InL, InH;
1806 ExpandOp(Op, InL, InH);
1807 switch(Opc) {
1808 case ISD::SHL:
1809 if (Cst > VTBits) {
1810 Lo = DAG.getConstant(0, NVT);
1811 Hi = DAG.getConstant(0, NVT);
1812 } else if (Cst > NVTBits) {
1813 Lo = DAG.getConstant(0, NVT);
1814 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001815 } else if (Cst == NVTBits) {
1816 Lo = DAG.getConstant(0, NVT);
1817 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00001818 } else {
1819 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1820 Hi = DAG.getNode(ISD::OR, NVT,
1821 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1822 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1823 }
1824 return true;
1825 case ISD::SRL:
1826 if (Cst > VTBits) {
1827 Lo = DAG.getConstant(0, NVT);
1828 Hi = DAG.getConstant(0, NVT);
1829 } else if (Cst > NVTBits) {
1830 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1831 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00001832 } else if (Cst == NVTBits) {
1833 Lo = InH;
1834 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00001835 } else {
1836 Lo = DAG.getNode(ISD::OR, NVT,
1837 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1838 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1839 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1840 }
1841 return true;
1842 case ISD::SRA:
1843 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001844 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001845 DAG.getConstant(NVTBits-1, ShTy));
1846 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001847 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001848 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00001849 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001850 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001851 } else if (Cst == NVTBits) {
1852 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00001853 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00001854 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00001855 } else {
1856 Lo = DAG.getNode(ISD::OR, NVT,
1857 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1858 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1859 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1860 }
1861 return true;
1862 }
1863 }
1864 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1865 // so disable it for now. Currently targets are handling this via SHL_PARTS
1866 // and friends.
1867 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001868
1869 // If we have an efficient select operation (or if the selects will all fold
1870 // away), lower to some complex code, otherwise just emit the libcall.
1871 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1872 !isa<ConstantSDNode>(Amt))
1873 return false;
1874
1875 SDOperand InL, InH;
1876 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001877 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1878 DAG.getConstant(NVTBits, ShTy), ShAmt);
1879
Chris Lattner4d25c042005-01-20 20:29:23 +00001880 // Compare the unmasked shift amount against 32.
1881 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1882 DAG.getConstant(NVTBits, ShTy));
1883
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001884 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1885 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1886 DAG.getConstant(NVTBits-1, ShTy));
1887 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1888 DAG.getConstant(NVTBits-1, ShTy));
1889 }
1890
1891 if (Opc == ISD::SHL) {
1892 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1893 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1894 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001895 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00001896
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001897 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1898 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1899 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001900 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1901 DAG.getSetCC(ISD::SETEQ,
1902 TLI.getSetCCResultTy(), NAmt,
1903 DAG.getConstant(32, ShTy)),
1904 DAG.getConstant(0, NVT),
1905 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001906 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001907 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001908 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001909 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001910
1911 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001912 if (Opc == ISD::SRA)
1913 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1914 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001915 else
1916 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001917 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001918 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001919 }
1920 return true;
1921}
Chris Lattneraac464e2005-01-21 06:05:23 +00001922
Chris Lattner4add7e32005-01-23 04:42:50 +00001923/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1924/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1925/// Found.
1926static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1927 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1928
1929 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1930 // than the Found node. Just remember this node and return.
1931 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1932 Found = Node;
1933 return;
1934 }
1935
1936 // Otherwise, scan the operands of Node to see if any of them is a call.
1937 assert(Node->getNumOperands() != 0 &&
1938 "All leaves should have depth equal to the entry node!");
1939 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1940 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1941
1942 // Tail recurse for the last iteration.
1943 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1944 Found);
1945}
1946
1947
1948/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1949/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1950/// than Found.
1951static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1952 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1953
1954 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1955 // than the Found node. Just remember this node and return.
1956 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1957 Found = Node;
1958 return;
1959 }
1960
1961 // Otherwise, scan the operands of Node to see if any of them is a call.
1962 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1963 if (UI == E) return;
1964 for (--E; UI != E; ++UI)
1965 FindEarliestAdjCallStackUp(*UI, Found);
1966
1967 // Tail recurse for the last iteration.
1968 FindEarliestAdjCallStackUp(*UI, Found);
1969}
1970
1971/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1972/// find the ADJCALLSTACKUP node that terminates the call sequence.
1973static SDNode *FindAdjCallStackUp(SDNode *Node) {
1974 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1975 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00001976 if (Node->use_empty())
1977 return 0; // No adjcallstackup
Chris Lattner4add7e32005-01-23 04:42:50 +00001978
1979 if (Node->hasOneUse()) // Simple case, only has one user to check.
1980 return FindAdjCallStackUp(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00001981
Chris Lattner4add7e32005-01-23 04:42:50 +00001982 SDOperand TheChain(Node, Node->getNumValues()-1);
1983 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00001984
1985 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner4add7e32005-01-23 04:42:50 +00001986 E = Node->use_end(); ; ++UI) {
1987 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
Misha Brukman835702a2005-04-21 22:36:52 +00001988
Chris Lattner4add7e32005-01-23 04:42:50 +00001989 // Make sure to only follow users of our token chain.
1990 SDNode *User = *UI;
1991 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1992 if (User->getOperand(i) == TheChain)
1993 return FindAdjCallStackUp(User);
1994 }
1995 assert(0 && "Unreachable");
1996 abort();
1997}
1998
Chris Lattner06bbeb62005-05-11 19:02:11 +00001999/// FindAdjCallStackDown - Given a chained node that is part of a call sequence,
2000/// find the ADJCALLSTACKDOWN node that initiates the call sequence.
2001static SDNode *FindAdjCallStackDown(SDNode *Node) {
2002 assert(Node && "Didn't find adjcallstackdown for a call??");
2003 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) return Node;
2004
2005 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2006 "Node doesn't have a token chain argument!");
2007 return FindAdjCallStackDown(Node->getOperand(0).Val);
2008}
2009
2010
Chris Lattner4add7e32005-01-23 04:42:50 +00002011/// FindInputOutputChains - If we are replacing an operation with a call we need
2012/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002013/// properly serialize the calls in the block. The returned operand is the
2014/// input chain value for the new call (e.g. the entry node or the previous
2015/// call), and OutChain is set to be the chain node to update to point to the
2016/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002017static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2018 SDOperand Entry) {
2019 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanadd0c632005-04-11 03:01:51 +00002020 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002021 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002022 //std::cerr<<"Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002023
Nate Begemanadd0c632005-04-11 03:01:51 +00002024 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
2025 // previous call in the function. LatestCallStackDown may in that case be
2026 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
2027 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
2028 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
2029 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
2030 else
2031 LatestAdjCallStackUp = Entry.Val;
2032 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
Misha Brukman835702a2005-04-21 22:36:52 +00002033
Chris Lattner06bbeb62005-05-11 19:02:11 +00002034 // Finally, find the first call that this must come before, first we find the
2035 // adjcallstackup that ends the call.
2036 OutChain = 0;
2037 FindEarliestAdjCallStackUp(OpNode, OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002038
Chris Lattner06bbeb62005-05-11 19:02:11 +00002039 // If we found one, translate from the adj up to the adjdown.
2040 if (OutChain)
2041 OutChain = FindAdjCallStackDown(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002042
2043 return SDOperand(LatestAdjCallStackUp, 0);
2044}
2045
Chris Lattner06bbeb62005-05-11 19:02:11 +00002046/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
2047static void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain,
2048 SelectionDAG &DAG) {
2049 // Nothing to splice it into?
2050 if (OutChain == 0) return;
2051
2052 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2053 //OutChain->dump();
2054
2055 // Form a token factor node merging the old inval and the new inval.
2056 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2057 OutChain->getOperand(0));
2058 // Change the node to refer to the new token.
2059 OutChain->setAdjCallChain(InToken);
2060}
Chris Lattner4add7e32005-01-23 04:42:50 +00002061
2062
Chris Lattneraac464e2005-01-21 06:05:23 +00002063// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2064// does not fit into a register, return the lo part and set the hi part to the
2065// by-reg argument. If it does fit into a single register, return the result
2066// and leave the Hi part unset.
2067SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2068 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002069 SDNode *OutChain;
2070 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2071 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002072 if (InChain.Val == 0)
2073 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002074
Chris Lattneraac464e2005-01-21 06:05:23 +00002075 TargetLowering::ArgListTy Args;
2076 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2077 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2078 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2079 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2080 }
2081 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002082
Chris Lattner06bbeb62005-05-11 19:02:11 +00002083 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002084 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002085 std::pair<SDOperand,SDOperand> CallInfo =
2086 TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG);
2087 SpliceCallInto(CallInfo.second, OutChain, DAG);
2088
2089 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002090 default: assert(0 && "Unknown thing");
2091 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002092 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002093 case Promote:
2094 assert(0 && "Cannot promote this yet!");
2095 case Expand:
2096 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002097 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002098 return Lo;
2099 }
2100}
2101
Chris Lattner4add7e32005-01-23 04:42:50 +00002102
Chris Lattneraac464e2005-01-21 06:05:23 +00002103/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2104/// destination type is legal.
2105SDOperand SelectionDAGLegalize::
2106ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2107 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2108 assert(getTypeAction(Source.getValueType()) == Expand &&
2109 "This is not an expansion!");
2110 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2111
Chris Lattner06bbeb62005-05-11 19:02:11 +00002112 if (!isSigned) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002113 // If this is unsigned, and not supported, first perform the conversion to
2114 // signed, then adjust the result if the sign bit is set.
Chris Lattner0efd77e2005-04-13 03:42:14 +00002115 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattneraac464e2005-01-21 06:05:23 +00002116
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002117 assert(Source.getValueType() == MVT::i64 &&
2118 "This only works for 64-bit -> FP");
2119 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2120 // incoming integer is set. To handle this, we dynamically test to see if
2121 // it is set, and, if so, add a fudge factor.
2122 SDOperand Lo, Hi;
2123 ExpandOp(Source, Lo, Hi);
2124
2125 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2126 DAG.getConstant(0, Hi.getValueType()));
2127 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2128 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2129 SignSet, Four, Zero);
2130 // FIXME: This is almost certainly broken for big-endian systems. Should
2131 // this just put the fudge factor in the low bits of the uint64 constant or?
2132 static Constant *FudgeFactor =
2133 ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32);
2134
2135 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2136 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2137 TLI.getPointerTy());
2138 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2139 SDOperand FudgeInReg;
2140 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002141 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2142 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002143 else {
2144 assert(DestTy == MVT::f64 && "Unexpected conversion");
2145 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002146 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002147 }
2148 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002149 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002150
2151 SDNode *OutChain = 0;
2152 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2153 DAG.getEntryNode());
2154 const char *FnName = 0;
2155 if (DestTy == MVT::f32)
2156 FnName = "__floatdisf";
2157 else {
2158 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2159 FnName = "__floatdidf";
2160 }
2161
Chris Lattneraac464e2005-01-21 06:05:23 +00002162 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2163
2164 TargetLowering::ArgListTy Args;
2165 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
2166 Args.push_back(std::make_pair(Source, ArgTy));
2167
2168 // We don't care about token chains for libcalls. We just use the entry
2169 // node as our input and ignore the output chain. This allows us to place
2170 // calls wherever we need them to satisfy data dependences.
2171 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002172
2173 std::pair<SDOperand,SDOperand> CallResult =
2174 TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG);
2175
2176 SpliceCallInto(CallResult.second, OutChain, DAG);
2177 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002178}
Misha Brukman835702a2005-04-21 22:36:52 +00002179
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002180
2181
Chris Lattnerdc750592005-01-07 07:47:09 +00002182/// ExpandOp - Expand the specified SDOperand into its two component pieces
2183/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2184/// LegalizeNodes map is filled in for any results that are not expanded, the
2185/// ExpandedNodes map is filled in for any results that are expanded, and the
2186/// Lo/Hi values are returned.
2187void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2188 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002189 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002190 SDNode *Node = Op.Val;
2191 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2192 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2193 assert(MVT::isInteger(NVT) && NVT < VT &&
2194 "Cannot expand to FP value or to larger int value!");
2195
2196 // If there is more than one use of this, see if we already expanded it.
2197 // There is no use remembering values that only have a single use, as the map
2198 // entries will never be reused.
2199 if (!Node->hasOneUse()) {
2200 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2201 = ExpandedNodes.find(Op);
2202 if (I != ExpandedNodes.end()) {
2203 Lo = I->second.first;
2204 Hi = I->second.second;
2205 return;
2206 }
2207 }
2208
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002209 // Expanding to multiple registers needs to perform an optimization step, and
2210 // is not careful to avoid operations the target does not support. Make sure
2211 // that all generated operations are legalized in the next iteration.
2212 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002213
Chris Lattnerdc750592005-01-07 07:47:09 +00002214 switch (Node->getOpcode()) {
2215 default:
2216 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2217 assert(0 && "Do not know how to expand this operator!");
2218 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002219 case ISD::UNDEF:
2220 Lo = DAG.getNode(ISD::UNDEF, NVT);
2221 Hi = DAG.getNode(ISD::UNDEF, NVT);
2222 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002223 case ISD::Constant: {
2224 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2225 Lo = DAG.getConstant(Cst, NVT);
2226 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2227 break;
2228 }
2229
2230 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00002231 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00002232 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00002233 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2234 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002235
Chris Lattner3b8e7192005-01-14 22:38:01 +00002236 // Remember that we legalized the chain.
2237 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2238
Chris Lattnerdc750592005-01-07 07:47:09 +00002239 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2240 break;
2241 }
2242
Chris Lattner32e08b72005-03-28 22:03:13 +00002243 case ISD::BUILD_PAIR:
2244 // Legalize both operands. FIXME: in the future we should handle the case
2245 // where the two elements are not legal.
2246 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2247 Lo = LegalizeOp(Node->getOperand(0));
2248 Hi = LegalizeOp(Node->getOperand(1));
2249 break;
2250
Chris Lattner55e9cde2005-05-11 04:51:16 +00002251 case ISD::CTPOP:
2252 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002253 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2254 DAG.getNode(ISD::CTPOP, NVT, Lo),
2255 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002256 Hi = DAG.getConstant(0, NVT);
2257 break;
2258
2259 case ISD::CTTZ:
2260 case ISD::CTLZ:
2261 assert(0 && "ct intrinsics cannot be expanded!");
2262
Chris Lattnerdc750592005-01-07 07:47:09 +00002263 case ISD::LOAD: {
2264 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2265 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002266 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002267
2268 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002269 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002270 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2271 getIntPtrConstant(IncrementSize));
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002272 //Is this safe? declaring that the two parts of the split load
2273 //are from the same instruction?
2274 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002275
2276 // Build a factor node to remember that this load is independent of the
2277 // other one.
2278 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2279 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002280
Chris Lattnerdc750592005-01-07 07:47:09 +00002281 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002282 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002283 if (!TLI.isLittleEndian())
2284 std::swap(Lo, Hi);
2285 break;
2286 }
2287 case ISD::CALL: {
2288 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2289 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2290
Chris Lattner3d95c142005-01-19 20:24:35 +00002291 bool Changed = false;
2292 std::vector<SDOperand> Ops;
2293 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2294 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2295 Changed |= Ops.back() != Node->getOperand(i);
2296 }
2297
Chris Lattnerdc750592005-01-07 07:47:09 +00002298 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2299 "Can only expand a call once so far, not i64 -> i16!");
2300
2301 std::vector<MVT::ValueType> RetTyVTs;
2302 RetTyVTs.reserve(3);
2303 RetTyVTs.push_back(NVT);
2304 RetTyVTs.push_back(NVT);
2305 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00002306 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00002307 Lo = SDOperand(NC, 0);
2308 Hi = SDOperand(NC, 1);
2309
2310 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002311 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002312 break;
2313 }
2314 case ISD::AND:
2315 case ISD::OR:
2316 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2317 SDOperand LL, LH, RL, RH;
2318 ExpandOp(Node->getOperand(0), LL, LH);
2319 ExpandOp(Node->getOperand(1), RL, RH);
2320 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2321 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2322 break;
2323 }
2324 case ISD::SELECT: {
2325 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002326
2327 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2328 case Expand: assert(0 && "It's impossible to expand bools");
2329 case Legal:
2330 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2331 break;
2332 case Promote:
2333 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2334 break;
2335 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002336 ExpandOp(Node->getOperand(1), LL, LH);
2337 ExpandOp(Node->getOperand(2), RL, RH);
2338 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2339 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2340 break;
2341 }
2342 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002343 SDOperand In;
2344 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2345 case Expand: assert(0 && "expand-expand not implemented yet!");
2346 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2347 case Promote:
2348 In = PromoteOp(Node->getOperand(0));
2349 // Emit the appropriate sign_extend_inreg to get the value we want.
2350 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2351 Node->getOperand(0).getValueType());
2352 break;
2353 }
2354
Chris Lattnerdc750592005-01-07 07:47:09 +00002355 // The low part is just a sign extension of the input (which degenerates to
2356 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002357 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002358
Chris Lattnerdc750592005-01-07 07:47:09 +00002359 // The high part is obtained by SRA'ing all but one of the bits of the lo
2360 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002361 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002362 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2363 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002364 break;
2365 }
Chris Lattner47844892005-04-03 23:41:52 +00002366 case ISD::ZERO_EXTEND: {
2367 SDOperand In;
2368 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2369 case Expand: assert(0 && "expand-expand not implemented yet!");
2370 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2371 case Promote:
2372 In = PromoteOp(Node->getOperand(0));
2373 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00002374 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00002375 break;
2376 }
2377
Chris Lattnerdc750592005-01-07 07:47:09 +00002378 // The low part is just a zero extension of the input (which degenerates to
2379 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002380 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002381
Chris Lattnerdc750592005-01-07 07:47:09 +00002382 // The high part is just a zero.
2383 Hi = DAG.getConstant(0, NVT);
2384 break;
Chris Lattner47844892005-04-03 23:41:52 +00002385 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002386 // These operators cannot be expanded directly, emit them as calls to
2387 // library functions.
2388 case ISD::FP_TO_SINT:
2389 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002390 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002391 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002392 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002393 break;
2394 case ISD::FP_TO_UINT:
2395 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002396 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002397 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002398 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002399 break;
2400
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002401 case ISD::SHL:
2402 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002403 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002404 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002405
2406 // If this target supports SHL_PARTS, use it.
2407 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002408 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2409 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002410 break;
2411 }
2412
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002413 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002414 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002415 break;
2416
2417 case ISD::SRA:
2418 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002419 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002420 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002421
2422 // If this target supports SRA_PARTS, use it.
2423 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002424 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2425 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002426 break;
2427 }
2428
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002429 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002430 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002431 break;
2432 case ISD::SRL:
2433 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002434 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002435 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002436
2437 // If this target supports SRL_PARTS, use it.
2438 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002439 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2440 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002441 break;
2442 }
2443
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002444 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002445 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002446 break;
2447
Misha Brukman835702a2005-04-21 22:36:52 +00002448 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002449 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2450 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002451 break;
2452 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002453 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2454 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002455 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002456 case ISD::MUL: {
2457 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2458 SDOperand LL, LH, RL, RH;
2459 ExpandOp(Node->getOperand(0), LL, LH);
2460 ExpandOp(Node->getOperand(1), RL, RH);
2461 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2462 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2463 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2464 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2465 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2466 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2467 } else {
2468 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2469 }
2470 break;
2471 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002472 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2473 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2474 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2475 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002476 }
2477
2478 // Remember in a map if the values will be reused later.
2479 if (!Node->hasOneUse()) {
2480 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2481 std::make_pair(Lo, Hi))).second;
2482 assert(isNew && "Value already expanded?!?");
2483 }
2484}
2485
2486
2487// SelectionDAG::Legalize - This is the entry point for the file.
2488//
Chris Lattner4add7e32005-01-23 04:42:50 +00002489void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002490 /// run - This is the main entry point to this class.
2491 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002492 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002493}
2494