blob: 8c26c5821be919e96f7c66103c78467acc98c895 [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))
Chris Lattner8005e912005-05-12 00:17:04 +0000326 Node->setAdjCallChain(Tmp1);
327 // Note that we do not create new ADJCALLSTACK DOWN/UP nodes here. These
328 // nodes are treated specially and are mutated in place. This makes the dag
329 // legalization process more efficient and also makes libcall insertion
330 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000331 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000332 case ISD::DYNAMIC_STACKALLOC:
333 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
334 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
335 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
336 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
337 Tmp3 != Node->getOperand(2))
338 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
339 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000340 else
341 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000342
343 // Since this op produces two values, make sure to remember that we
344 // legalized both of them.
345 AddLegalizedOperand(SDOperand(Node, 0), Result);
346 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
347 return Result.getValue(Op.ResNo);
348
Chris Lattner3d95c142005-01-19 20:24:35 +0000349 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000350 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
351 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000352
353 bool Changed = false;
354 std::vector<SDOperand> Ops;
355 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
356 Ops.push_back(LegalizeOp(Node->getOperand(i)));
357 Changed |= Ops.back() != Node->getOperand(i);
358 }
359
360 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000361 std::vector<MVT::ValueType> RetTyVTs;
362 RetTyVTs.reserve(Node->getNumValues());
363 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000364 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattner3d95c142005-01-19 20:24:35 +0000365 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000366 } else {
367 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000368 }
Chris Lattner9242c502005-01-09 19:43:23 +0000369 // Since calls produce multiple values, make sure to remember that we
370 // legalized all of them.
371 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
372 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
373 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000374 }
Chris Lattner68a12142005-01-07 22:12:08 +0000375 case ISD::BR:
376 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
377 if (Tmp1 != Node->getOperand(0))
378 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
379 break;
380
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000381 case ISD::BRCOND:
382 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000383
384 switch (getTypeAction(Node->getOperand(1).getValueType())) {
385 case Expand: assert(0 && "It's impossible to expand bools");
386 case Legal:
387 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
388 break;
389 case Promote:
390 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
391 break;
392 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000393 // Basic block destination (Op#2) is always legal.
394 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
395 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
396 Node->getOperand(2));
397 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000398 case ISD::BRCONDTWOWAY:
399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
400 switch (getTypeAction(Node->getOperand(1).getValueType())) {
401 case Expand: assert(0 && "It's impossible to expand bools");
402 case Legal:
403 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
404 break;
405 case Promote:
406 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
407 break;
408 }
409 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
410 // pair.
411 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
412 case TargetLowering::Promote:
413 default: assert(0 && "This action is not supported yet!");
414 case TargetLowering::Legal:
415 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
416 std::vector<SDOperand> Ops;
417 Ops.push_back(Tmp1);
418 Ops.push_back(Tmp2);
419 Ops.push_back(Node->getOperand(2));
420 Ops.push_back(Node->getOperand(3));
421 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
422 }
423 break;
424 case TargetLowering::Expand:
425 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
426 Node->getOperand(2));
427 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
428 break;
429 }
430 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000431
Chris Lattnerdc750592005-01-07 07:47:09 +0000432 case ISD::LOAD:
433 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
434 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000435
Chris Lattnerdc750592005-01-07 07:47:09 +0000436 if (Tmp1 != Node->getOperand(0) ||
437 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000438 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
439 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000440 else
441 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000442
Chris Lattnerea4ca942005-01-07 22:28:47 +0000443 // Since loads produce two values, make sure to remember that we legalized
444 // both of them.
445 AddLegalizedOperand(SDOperand(Node, 0), Result);
446 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
447 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000448
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000449 case ISD::EXTLOAD:
450 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000451 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000452 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
453 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000454
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000455 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
456 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000457 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000458 case TargetLowering::Promote:
459 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
460 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000461 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000462 // Since loads produce two values, make sure to remember that we legalized
463 // both of them.
464 AddLegalizedOperand(SDOperand(Node, 0), Result);
465 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
466 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000467
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000468 case TargetLowering::Legal:
469 if (Tmp1 != Node->getOperand(0) ||
470 Tmp2 != Node->getOperand(1))
471 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000472 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000473 else
474 Result = SDOperand(Node, 0);
475
476 // Since loads produce two values, make sure to remember that we legalized
477 // both of them.
478 AddLegalizedOperand(SDOperand(Node, 0), Result);
479 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
480 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000481 case TargetLowering::Expand:
482 assert(Node->getOpcode() != ISD::EXTLOAD &&
483 "EXTLOAD should always be supported!");
484 // Turn the unsupported load into an EXTLOAD followed by an explicit
485 // zero/sign extend inreg.
486 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000487 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000488 SDOperand ValRes;
489 if (Node->getOpcode() == ISD::SEXTLOAD)
490 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
491 Result, SrcVT);
492 else
493 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000494 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
495 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
496 if (Op.ResNo)
497 return Result.getValue(1);
498 return ValRes;
499 }
500 assert(0 && "Unreachable");
501 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000502 case ISD::EXTRACT_ELEMENT:
503 // Get both the low and high parts.
504 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
505 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
506 Result = Tmp2; // 1 -> Hi
507 else
508 Result = Tmp1; // 0 -> Lo
509 break;
510
511 case ISD::CopyToReg:
512 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000513
Chris Lattnerdc750592005-01-07 07:47:09 +0000514 switch (getTypeAction(Node->getOperand(1).getValueType())) {
515 case Legal:
516 // Legalize the incoming value (must be legal).
517 Tmp2 = LegalizeOp(Node->getOperand(1));
518 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000519 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000520 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000521 case Promote:
522 Tmp2 = PromoteOp(Node->getOperand(1));
523 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
524 break;
525 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000526 SDOperand Lo, Hi;
Misha Brukman835702a2005-04-21 22:36:52 +0000527 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000528 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000529 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
530 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
531 // Note that the copytoreg nodes are independent of each other.
532 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000533 assert(isTypeLegal(Result.getValueType()) &&
534 "Cannot expand multiple times yet (i64 -> i16)");
535 break;
536 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000537 break;
538
539 case ISD::RET:
540 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
541 switch (Node->getNumOperands()) {
542 case 2: // ret val
543 switch (getTypeAction(Node->getOperand(1).getValueType())) {
544 case Legal:
545 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000546 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000547 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
548 break;
549 case Expand: {
550 SDOperand Lo, Hi;
551 ExpandOp(Node->getOperand(1), Lo, Hi);
552 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000553 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000554 }
555 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000556 Tmp2 = PromoteOp(Node->getOperand(1));
557 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
558 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000559 }
560 break;
561 case 1: // ret void
562 if (Tmp1 != Node->getOperand(0))
563 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
564 break;
565 default: { // ret <values>
566 std::vector<SDOperand> NewValues;
567 NewValues.push_back(Tmp1);
568 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
569 switch (getTypeAction(Node->getOperand(i).getValueType())) {
570 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000571 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000572 break;
573 case Expand: {
574 SDOperand Lo, Hi;
575 ExpandOp(Node->getOperand(i), Lo, Hi);
576 NewValues.push_back(Lo);
577 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000578 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000579 }
580 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000581 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000582 }
583 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
584 break;
585 }
586 }
587 break;
588 case ISD::STORE:
589 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
590 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
591
Chris Lattnere69daaf2005-01-08 06:25:56 +0000592 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000593 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000594 if (CFP->getValueType(0) == MVT::f32) {
595 union {
596 unsigned I;
597 float F;
598 } V;
599 V.F = CFP->getValue();
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000600 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000601 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000602 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000603 } else {
604 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
605 union {
606 uint64_t I;
607 double F;
608 } V;
609 V.F = CFP->getValue();
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000610 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner5385db52005-05-09 20:23:03 +0000611 DAG.getConstant(V.I, MVT::i64), Tmp2,
612 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000613 }
Chris Lattnera4743132005-02-22 07:23:39 +0000614 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000615 }
616
Chris Lattnerdc750592005-01-07 07:47:09 +0000617 switch (getTypeAction(Node->getOperand(1).getValueType())) {
618 case Legal: {
619 SDOperand Val = LegalizeOp(Node->getOperand(1));
620 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
621 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +0000622 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
623 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000624 break;
625 }
626 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000627 // Truncate the value and store the result.
628 Tmp3 = PromoteOp(Node->getOperand(1));
629 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000630 Node->getOperand(3),
631 Node->getOperand(1).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000632 break;
633
Chris Lattnerdc750592005-01-07 07:47:09 +0000634 case Expand:
635 SDOperand Lo, Hi;
636 ExpandOp(Node->getOperand(1), Lo, Hi);
637
638 if (!TLI.isLittleEndian())
639 std::swap(Lo, Hi);
640
Chris Lattner55e9cde2005-05-11 04:51:16 +0000641 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
642 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000643 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000644 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
645 getIntPtrConstant(IncrementSize));
646 assert(isTypeLegal(Tmp2.getValueType()) &&
647 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000648 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +0000649 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
650 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000651 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
652 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000653 }
654 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000655 case ISD::PCMARKER:
656 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000657 if (Tmp1 != Node->getOperand(0))
658 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000659 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000660 case ISD::TRUNCSTORE:
661 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
662 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
663
664 switch (getTypeAction(Node->getOperand(1).getValueType())) {
665 case Legal:
666 Tmp2 = LegalizeOp(Node->getOperand(1));
667 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
668 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000669 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000670 Node->getOperand(3),
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000671 cast<MVTSDNode>(Node)->getExtraValueType());
672 break;
673 case Promote:
674 case Expand:
675 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
676 }
677 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000678 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000679 switch (getTypeAction(Node->getOperand(0).getValueType())) {
680 case Expand: assert(0 && "It's impossible to expand bools");
681 case Legal:
682 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
683 break;
684 case Promote:
685 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
686 break;
687 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000688 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000689 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000690
691 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
692 default: assert(0 && "This action is not supported yet!");
693 case TargetLowering::Legal:
694 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
695 Tmp3 != Node->getOperand(2))
696 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
697 Tmp1, Tmp2, Tmp3);
698 break;
699 case TargetLowering::Promote: {
700 MVT::ValueType NVT =
701 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
702 unsigned ExtOp, TruncOp;
703 if (MVT::isInteger(Tmp2.getValueType())) {
704 ExtOp = ISD::ZERO_EXTEND;
705 TruncOp = ISD::TRUNCATE;
706 } else {
707 ExtOp = ISD::FP_EXTEND;
708 TruncOp = ISD::FP_ROUND;
709 }
710 // Promote each of the values to the new type.
711 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
712 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
713 // Perform the larger operation, then round down.
714 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
715 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
716 break;
717 }
718 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000719 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000720 case ISD::SETCC:
721 switch (getTypeAction(Node->getOperand(0).getValueType())) {
722 case Legal:
723 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
724 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
725 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
726 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000727 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000728 break;
729 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000730 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
731 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
732
733 // If this is an FP compare, the operands have already been extended.
734 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
735 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000736 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000737
738 // Otherwise, we have to insert explicit sign or zero extends. Note
739 // that we could insert sign extends for ALL conditions, but zero extend
740 // is cheaper on many machines (an AND instead of two shifts), so prefer
741 // it.
742 switch (cast<SetCCSDNode>(Node)->getCondition()) {
743 default: assert(0 && "Unknown integer comparison!");
744 case ISD::SETEQ:
745 case ISD::SETNE:
746 case ISD::SETUGE:
747 case ISD::SETUGT:
748 case ISD::SETULE:
749 case ISD::SETULT:
750 // ALL of these operations will work if we either sign or zero extend
751 // the operands (including the unsigned comparisons!). Zero extend is
752 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +0000753 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
754 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000755 break;
756 case ISD::SETGE:
757 case ISD::SETGT:
758 case ISD::SETLT:
759 case ISD::SETLE:
760 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
761 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
762 break;
763 }
764
765 }
766 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000767 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000768 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000769 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000770 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
771 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
772 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
773 switch (cast<SetCCSDNode>(Node)->getCondition()) {
774 case ISD::SETEQ:
775 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +0000776 if (RHSLo == RHSHi)
777 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
778 if (RHSCST->isAllOnesValue()) {
779 // Comparison to -1.
780 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukman835702a2005-04-21 22:36:52 +0000781 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner71ff44e2005-04-12 01:46:05 +0000782 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukman835702a2005-04-21 22:36:52 +0000783 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +0000784 }
785
Chris Lattnerdc750592005-01-07 07:47:09 +0000786 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
787 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
788 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +0000789 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000790 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000791 DAG.getConstant(0, Tmp1.getValueType()));
792 break;
793 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +0000794 // If this is a comparison of the sign bit, just look at the top part.
795 // X > -1, x < 0
796 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukman835702a2005-04-21 22:36:52 +0000797 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +0000798 CST->getValue() == 0) || // X < 0
799 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
800 (CST->isAllOnesValue()))) // X > -1
801 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
802 Node->getValueType(0), LHSHi, RHSHi);
803
Chris Lattnerdc750592005-01-07 07:47:09 +0000804 // FIXME: This generated code sucks.
805 ISD::CondCode LowCC;
806 switch (cast<SetCCSDNode>(Node)->getCondition()) {
807 default: assert(0 && "Unknown integer setcc!");
808 case ISD::SETLT:
809 case ISD::SETULT: LowCC = ISD::SETULT; break;
810 case ISD::SETGT:
811 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
812 case ISD::SETLE:
813 case ISD::SETULE: LowCC = ISD::SETULE; break;
814 case ISD::SETGE:
815 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
816 }
Misha Brukman835702a2005-04-21 22:36:52 +0000817
Chris Lattnerdc750592005-01-07 07:47:09 +0000818 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
819 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
820 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
821
822 // NOTE: on targets without efficient SELECT of bools, we can always use
823 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000824 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000825 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000826 Node->getValueType(0), LHSHi, RHSHi);
827 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
828 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
829 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000830 break;
831 }
832 }
833 break;
834
Chris Lattner85d70c62005-01-11 05:57:22 +0000835 case ISD::MEMSET:
836 case ISD::MEMCPY:
837 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000838 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000839 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
840
841 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
842 switch (getTypeAction(Node->getOperand(2).getValueType())) {
843 case Expand: assert(0 && "Cannot expand a byte!");
844 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000845 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000846 break;
847 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000848 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000849 break;
850 }
851 } else {
Misha Brukman835702a2005-04-21 22:36:52 +0000852 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000853 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000854
855 SDOperand Tmp4;
856 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000857 case Expand: assert(0 && "Cannot expand this yet!");
858 case Legal:
859 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000860 break;
861 case Promote:
862 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000863 break;
864 }
865
866 SDOperand Tmp5;
867 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
868 case Expand: assert(0 && "Cannot expand this yet!");
869 case Legal:
870 Tmp5 = LegalizeOp(Node->getOperand(4));
871 break;
872 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000873 Tmp5 = PromoteOp(Node->getOperand(4));
874 break;
875 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000876
877 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
878 default: assert(0 && "This action not implemented for this operation!");
879 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000880 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
881 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
882 Tmp5 != Node->getOperand(4)) {
883 std::vector<SDOperand> Ops;
884 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
885 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
886 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
887 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000888 break;
889 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000890 // Otherwise, the target does not support this operation. Lower the
891 // operation to an explicit libcall as appropriate.
892 MVT::ValueType IntPtr = TLI.getPointerTy();
893 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
894 std::vector<std::pair<SDOperand, const Type*> > Args;
895
Reid Spencer6dced922005-01-12 14:53:45 +0000896 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000897 if (Node->getOpcode() == ISD::MEMSET) {
898 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
899 // Extend the ubyte argument to be an int value for the call.
900 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
901 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
902 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
903
904 FnName = "memset";
905 } else if (Node->getOpcode() == ISD::MEMCPY ||
906 Node->getOpcode() == ISD::MEMMOVE) {
907 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
908 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
909 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
910 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
911 } else {
912 assert(0 && "Unknown op!");
913 }
Chris Lattner06bbeb62005-05-11 19:02:11 +0000914 // FIXME: THESE SHOULD USE ExpandLibCall ??!?
Chris Lattner85d70c62005-01-11 05:57:22 +0000915 std::pair<SDOperand,SDOperand> CallResult =
Nate Begemanf6565252005-03-26 01:29:23 +0000916 TLI.LowerCallTo(Tmp1, Type::VoidTy, false,
Chris Lattner85d70c62005-01-11 05:57:22 +0000917 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
918 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000919 break;
920 }
921 case TargetLowering::Custom:
922 std::vector<SDOperand> Ops;
923 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
924 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
925 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
926 Result = TLI.LowerOperation(Result);
927 Result = LegalizeOp(Result);
928 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000929 }
930 break;
931 }
Chris Lattner5385db52005-05-09 20:23:03 +0000932
933 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +0000934 Tmp1 = LegalizeOp(Node->getOperand(0));
935 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000936
Chris Lattner5385db52005-05-09 20:23:03 +0000937 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000938 Result = DAG.getNode(ISD::READPORT, Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner5385db52005-05-09 20:23:03 +0000939 else
940 Result = SDOperand(Node, 0);
941 // Since these produce two values, make sure to remember that we legalized
942 // both of them.
943 AddLegalizedOperand(SDOperand(Node, 0), Result);
944 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
945 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +0000946 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +0000947 Tmp1 = LegalizeOp(Node->getOperand(0));
948 Tmp2 = LegalizeOp(Node->getOperand(1));
949 Tmp3 = LegalizeOp(Node->getOperand(2));
950 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
951 Tmp3 != Node->getOperand(2))
952 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
953 break;
954
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000955 case ISD::READIO:
956 Tmp1 = LegalizeOp(Node->getOperand(0));
957 Tmp2 = LegalizeOp(Node->getOperand(1));
958
959 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
960 case TargetLowering::Custom:
961 default: assert(0 && "This action not implemented for this operation!");
962 case TargetLowering::Legal:
963 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
964 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
965 Tmp1, Tmp2);
966 else
967 Result = SDOperand(Node, 0);
968 break;
969 case TargetLowering::Expand:
970 // Replace this with a load from memory.
971 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
972 Node->getOperand(1), DAG.getSrcValue(NULL));
973 Result = LegalizeOp(Result);
974 break;
975 }
976
977 // Since these produce two values, make sure to remember that we legalized
978 // both of them.
979 AddLegalizedOperand(SDOperand(Node, 0), Result);
980 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
981 return Result.getValue(Op.ResNo);
982
983 case ISD::WRITEIO:
984 Tmp1 = LegalizeOp(Node->getOperand(0));
985 Tmp2 = LegalizeOp(Node->getOperand(1));
986 Tmp3 = LegalizeOp(Node->getOperand(2));
987
988 switch (TLI.getOperationAction(Node->getOpcode(),
989 Node->getOperand(1).getValueType())) {
990 case TargetLowering::Custom:
991 default: assert(0 && "This action not implemented for this operation!");
992 case TargetLowering::Legal:
993 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
994 Tmp3 != Node->getOperand(2))
995 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
996 break;
997 case TargetLowering::Expand:
998 // Replace this with a store to memory.
999 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1000 Node->getOperand(1), Node->getOperand(2),
1001 DAG.getSrcValue(NULL));
1002 Result = LegalizeOp(Result);
1003 break;
1004 }
1005 break;
1006
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001007 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001008 case ISD::SUB_PARTS:
1009 case ISD::SHL_PARTS:
1010 case ISD::SRA_PARTS:
1011 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001012 std::vector<SDOperand> Ops;
1013 bool Changed = false;
1014 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1015 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1016 Changed |= Ops.back() != Node->getOperand(i);
1017 }
1018 if (Changed)
1019 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001020
1021 // Since these produce multiple values, make sure to remember that we
1022 // legalized all of them.
1023 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1024 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1025 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001026 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001027
1028 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001029 case ISD::ADD:
1030 case ISD::SUB:
1031 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001032 case ISD::MULHS:
1033 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001034 case ISD::UDIV:
1035 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001036 case ISD::AND:
1037 case ISD::OR:
1038 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001039 case ISD::SHL:
1040 case ISD::SRL:
1041 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001042 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1043 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1044 if (Tmp1 != Node->getOperand(0) ||
1045 Tmp2 != Node->getOperand(1))
1046 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1047 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001048
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001049 case ISD::UREM:
1050 case ISD::SREM:
1051 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1052 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1053 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1054 case TargetLowering::Legal:
1055 if (Tmp1 != Node->getOperand(0) ||
1056 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001057 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001058 Tmp2);
1059 break;
1060 case TargetLowering::Promote:
1061 case TargetLowering::Custom:
1062 assert(0 && "Cannot promote/custom handle this yet!");
1063 case TargetLowering::Expand: {
1064 MVT::ValueType VT = Node->getValueType(0);
1065 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1066 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1067 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1068 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1069 }
1070 break;
1071 }
1072 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001073
Andrew Lenharth5e177822005-05-03 17:19:30 +00001074 case ISD::CTPOP:
1075 case ISD::CTTZ:
1076 case ISD::CTLZ:
1077 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1078 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1079 case TargetLowering::Legal:
1080 if (Tmp1 != Node->getOperand(0))
1081 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1082 break;
1083 case TargetLowering::Promote: {
1084 MVT::ValueType OVT = Tmp1.getValueType();
1085 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001086
1087 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001088 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1089 // Perform the larger operation, then subtract if needed.
1090 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1091 switch(Node->getOpcode())
1092 {
1093 case ISD::CTPOP:
1094 Result = Tmp1;
1095 break;
1096 case ISD::CTTZ:
1097 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1098 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1099 DAG.getConstant(getSizeInBits(NVT), NVT));
1100 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1101 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1102 break;
1103 case ISD::CTLZ:
1104 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1105 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1106 DAG.getConstant(getSizeInBits(NVT) -
1107 getSizeInBits(OVT), NVT));
1108 break;
1109 }
1110 break;
1111 }
1112 case TargetLowering::Custom:
1113 assert(0 && "Cannot custom handle this yet!");
1114 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001115 switch(Node->getOpcode())
1116 {
1117 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001118 static const uint64_t mask[6] = {
1119 0x5555555555555555ULL, 0x3333333333333333ULL,
1120 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1121 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1122 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001123 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001124 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1125 unsigned len = getSizeInBits(VT);
1126 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001127 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001128 Tmp2 = DAG.getConstant(mask[i], VT);
1129 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001130 Tmp1 = DAG.getNode(ISD::ADD, VT,
1131 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1132 DAG.getNode(ISD::AND, VT,
1133 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1134 Tmp2));
1135 }
1136 Result = Tmp1;
1137 break;
1138 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001139 case ISD::CTLZ: {
1140 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001141 x = x | (x >> 1);
1142 x = x | (x >> 2);
1143 ...
1144 x = x | (x >>16);
1145 x = x | (x >>32); // for 64-bit input
1146 return popcount(~x);
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001147
Chris Lattner56add052005-05-11 18:35:21 +00001148 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1149 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001150 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1151 unsigned len = getSizeInBits(VT);
1152 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1153 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
1154 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
1155 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1156 }
1157 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001158 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001159 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001160 }
1161 case ISD::CTTZ: {
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001162 // for now, we use: { return popcount(~x & (x - 1)); }
1163 // unless the target has ctlz but not ctpop, in which case we use:
1164 // { return 32 - nlz(~x & (x-1)); }
1165 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001166 MVT::ValueType VT = Tmp1.getValueType();
1167 Tmp2 = DAG.getConstant(~0ULL, VT);
1168 Tmp3 = DAG.getNode(ISD::AND, VT,
1169 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1170 DAG.getNode(ISD::SUB, VT, Tmp1,
1171 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001172 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1173 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1174 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
1175 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
1176 DAG.getConstant(getSizeInBits(VT), VT),
1177 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1178 } else {
1179 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1180 }
Chris Lattner72473242005-05-11 05:27:09 +00001181 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001182 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001183 default:
1184 assert(0 && "Cannot expand this yet!");
1185 break;
1186 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001187 break;
1188 }
1189 break;
1190
Chris Lattner13fe99c2005-04-02 05:00:07 +00001191 // Unary operators
1192 case ISD::FABS:
1193 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001194 case ISD::FSQRT:
1195 case ISD::FSIN:
1196 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001197 Tmp1 = LegalizeOp(Node->getOperand(0));
1198 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1199 case TargetLowering::Legal:
1200 if (Tmp1 != Node->getOperand(0))
1201 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1202 break;
1203 case TargetLowering::Promote:
1204 case TargetLowering::Custom:
1205 assert(0 && "Cannot promote/custom handle this yet!");
1206 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001207 switch(Node->getOpcode()) {
1208 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001209 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1210 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1211 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1212 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001213 break;
1214 }
1215 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001216 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1217 MVT::ValueType VT = Node->getValueType(0);
1218 Tmp2 = DAG.getConstantFP(0.0, VT);
1219 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1220 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1221 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1222 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001223 break;
1224 }
1225 case ISD::FSQRT:
1226 case ISD::FSIN:
1227 case ISD::FCOS: {
1228 MVT::ValueType VT = Node->getValueType(0);
1229 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1230 const char *FnName = 0;
1231 switch(Node->getOpcode()) {
1232 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1233 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1234 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1235 default: assert(0 && "Unreachable!");
1236 }
1237 std::vector<std::pair<SDOperand, const Type*> > Args;
1238 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner06bbeb62005-05-11 19:02:11 +00001239 // FIXME: should use ExpandLibCall!
Chris Lattner80026402005-04-30 04:43:14 +00001240 std::pair<SDOperand,SDOperand> CallResult =
1241 TLI.LowerCallTo(DAG.getEntryNode(), T, false,
1242 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1243 Result = LegalizeOp(CallResult.first);
1244 break;
1245 }
1246 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001247 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001248 }
1249 break;
1250 }
1251 break;
1252
1253 // Conversion operators. The source and destination have different types.
Chris Lattnerdc750592005-01-07 07:47:09 +00001254 case ISD::ZERO_EXTEND:
1255 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +00001256 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001257 case ISD::FP_EXTEND:
1258 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001259 case ISD::FP_TO_SINT:
1260 case ISD::FP_TO_UINT:
1261 case ISD::SINT_TO_FP:
1262 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +00001263 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1264 case Legal:
1265 Tmp1 = LegalizeOp(Node->getOperand(0));
1266 if (Tmp1 != Node->getOperand(0))
1267 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1268 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001269 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001270 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1271 Node->getOpcode() == ISD::UINT_TO_FP) {
1272 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1273 Node->getValueType(0), Node->getOperand(0));
1274 Result = LegalizeOp(Result);
1275 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001276 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1277 // In the expand case, we must be dealing with a truncate, because
1278 // otherwise the result would be larger than the source.
1279 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +00001280
Chris Lattner13fe99c2005-04-02 05:00:07 +00001281 // Since the result is legal, we should just be able to truncate the low
1282 // part of the source.
1283 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1284 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001285 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001286 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001287
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001288 case Promote:
1289 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001290 case ISD::ZERO_EXTEND:
1291 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001292 // NOTE: Any extend would work here...
1293 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001294 Result = DAG.getZeroExtendInReg(Result,
1295 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001296 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001297 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001298 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001299 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001300 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001301 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1302 Result, Node->getOperand(0).getValueType());
1303 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001304 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001305 Result = PromoteOp(Node->getOperand(0));
1306 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1307 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001308 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001309 Result = PromoteOp(Node->getOperand(0));
1310 if (Result.getValueType() != Op.getValueType())
1311 // Dynamically dead while we have only 2 FP types.
1312 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1313 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001314 case ISD::FP_ROUND:
1315 case ISD::FP_TO_SINT:
1316 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001317 Result = PromoteOp(Node->getOperand(0));
1318 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1319 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001320 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001321 Result = PromoteOp(Node->getOperand(0));
1322 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1323 Result, Node->getOperand(0).getValueType());
1324 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1325 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001326 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001327 Result = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001328 Result = DAG.getZeroExtendInReg(Result,
1329 Node->getOperand(0).getValueType());
Chris Lattner3ba56b32005-01-16 05:06:12 +00001330 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1331 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001332 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001333 }
1334 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001335 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001336 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001337 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +00001338 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1339
1340 // If this operation is not supported, convert it to a shl/shr or load/store
1341 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001342 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1343 default: assert(0 && "This action not supported for this op yet!");
1344 case TargetLowering::Legal:
1345 if (Tmp1 != Node->getOperand(0))
1346 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1347 ExtraVT);
1348 break;
1349 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001350 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001351 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001352 // NOTE: we could fall back on load/store here too for targets without
1353 // SAR. However, it is doubtful that any exist.
1354 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1355 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001356 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001357 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1358 Node->getOperand(0), ShiftCst);
1359 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1360 Result, ShiftCst);
1361 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1362 // The only way we can lower this is to turn it into a STORETRUNC,
1363 // EXTLOAD pair, targetting a temporary location (a stack slot).
1364
1365 // NOTE: there is a choice here between constantly creating new stack
1366 // slots and always reusing the same one. We currently always create
1367 // new ones, as reuse may inhibit scheduling.
1368 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1369 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1370 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1371 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001372 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001373 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1374 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1375 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001376 Node->getOperand(0), StackSlot,
1377 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001378 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001379 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001380 } else {
1381 assert(0 && "Unknown op");
1382 }
1383 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001384 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001385 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001386 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001387 }
Chris Lattner99222f72005-01-15 07:15:18 +00001388 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001389
Chris Lattnerea4ca942005-01-07 22:28:47 +00001390 if (!Op.Val->hasOneUse())
1391 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001392
1393 return Result;
1394}
1395
Chris Lattner4d978642005-01-15 22:16:26 +00001396/// PromoteOp - Given an operation that produces a value in an invalid type,
1397/// promote it to compute the value into a larger type. The produced value will
1398/// have the correct bits for the low portion of the register, but no guarantee
1399/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001400SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1401 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001402 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001403 assert(getTypeAction(VT) == Promote &&
1404 "Caller should expand or legalize operands that are not promotable!");
1405 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1406 "Cannot promote to smaller type!");
1407
1408 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1409 if (I != PromotedNodes.end()) return I->second;
1410
1411 SDOperand Tmp1, Tmp2, Tmp3;
1412
1413 SDOperand Result;
1414 SDNode *Node = Op.Val;
1415
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001416 // Promotion needs an optimization step to clean up after it, and is not
1417 // careful to avoid operations the target does not support. Make sure that
1418 // all generated operations are legalized in the next iteration.
1419 NeedsAnotherIteration = true;
1420
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001421 switch (Node->getOpcode()) {
1422 default:
1423 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1424 assert(0 && "Do not know how to promote this operator!");
1425 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001426 case ISD::UNDEF:
1427 Result = DAG.getNode(ISD::UNDEF, NVT);
1428 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001429 case ISD::Constant:
1430 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1431 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1432 break;
1433 case ISD::ConstantFP:
1434 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1435 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1436 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001437 case ISD::CopyFromReg:
1438 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1439 Node->getOperand(0));
1440 // Remember that we legalized the chain.
1441 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1442 break;
1443
Chris Lattner2cb338d2005-01-18 02:59:52 +00001444 case ISD::SETCC:
1445 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1446 "SetCC type is not legal??");
1447 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1448 TLI.getSetCCResultTy(), Node->getOperand(0),
1449 Node->getOperand(1));
1450 Result = LegalizeOp(Result);
1451 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001452
1453 case ISD::TRUNCATE:
1454 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1455 case Legal:
1456 Result = LegalizeOp(Node->getOperand(0));
1457 assert(Result.getValueType() >= NVT &&
1458 "This truncation doesn't make sense!");
1459 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1460 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1461 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001462 case Promote:
1463 // The truncation is not required, because we don't guarantee anything
1464 // about high bits anyway.
1465 Result = PromoteOp(Node->getOperand(0));
1466 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001467 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001468 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1469 // Truncate the low part of the expanded value to the result type
Misha Brukman835702a2005-04-21 22:36:52 +00001470 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001471 }
1472 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001473 case ISD::SIGN_EXTEND:
1474 case ISD::ZERO_EXTEND:
1475 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1476 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1477 case Legal:
1478 // Input is legal? Just do extend all the way to the larger type.
1479 Result = LegalizeOp(Node->getOperand(0));
1480 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1481 break;
1482 case Promote:
1483 // Promote the reg if it's smaller.
1484 Result = PromoteOp(Node->getOperand(0));
1485 // The high bits are not guaranteed to be anything. Insert an extend.
1486 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001487 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1488 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001489 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001490 Result = DAG.getZeroExtendInReg(Result,
1491 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001492 break;
1493 }
1494 break;
1495
1496 case ISD::FP_EXTEND:
1497 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1498 case ISD::FP_ROUND:
1499 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1500 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1501 case Promote: assert(0 && "Unreachable with 2 FP types!");
1502 case Legal:
1503 // Input is legal? Do an FP_ROUND_INREG.
1504 Result = LegalizeOp(Node->getOperand(0));
1505 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1506 break;
1507 }
1508 break;
1509
1510 case ISD::SINT_TO_FP:
1511 case ISD::UINT_TO_FP:
1512 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1513 case Legal:
1514 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001515 // No extra round required here.
1516 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001517 break;
1518
1519 case Promote:
1520 Result = PromoteOp(Node->getOperand(0));
1521 if (Node->getOpcode() == ISD::SINT_TO_FP)
1522 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1523 Result, Node->getOperand(0).getValueType());
1524 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001525 Result = DAG.getZeroExtendInReg(Result,
1526 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001527 // No extra round required here.
1528 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001529 break;
1530 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001531 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1532 Node->getOperand(0));
1533 Result = LegalizeOp(Result);
1534
1535 // Round if we cannot tolerate excess precision.
1536 if (NoExcessFPPrecision)
1537 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1538 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001539 }
Chris Lattner4d978642005-01-15 22:16:26 +00001540 break;
1541
1542 case ISD::FP_TO_SINT:
1543 case ISD::FP_TO_UINT:
1544 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1545 case Legal:
1546 Tmp1 = LegalizeOp(Node->getOperand(0));
1547 break;
1548 case Promote:
1549 // The input result is prerounded, so we don't have to do anything
1550 // special.
1551 Tmp1 = PromoteOp(Node->getOperand(0));
1552 break;
1553 case Expand:
1554 assert(0 && "not implemented");
1555 }
1556 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1557 break;
1558
Chris Lattner13fe99c2005-04-02 05:00:07 +00001559 case ISD::FABS:
1560 case ISD::FNEG:
1561 Tmp1 = PromoteOp(Node->getOperand(0));
1562 assert(Tmp1.getValueType() == NVT);
1563 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1564 // NOTE: we do not have to do any extra rounding here for
1565 // NoExcessFPPrecision, because we know the input will have the appropriate
1566 // precision, and these operations don't modify precision at all.
1567 break;
1568
Chris Lattner9d6fa982005-04-28 21:44:33 +00001569 case ISD::FSQRT:
1570 case ISD::FSIN:
1571 case ISD::FCOS:
1572 Tmp1 = PromoteOp(Node->getOperand(0));
1573 assert(Tmp1.getValueType() == NVT);
1574 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1575 if(NoExcessFPPrecision)
1576 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1577 break;
1578
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001579 case ISD::AND:
1580 case ISD::OR:
1581 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001582 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001583 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001584 case ISD::MUL:
1585 // The input may have strange things in the top bits of the registers, but
1586 // these operations don't care. They may have wierd bits going out, but
1587 // that too is okay if they are integer operations.
1588 Tmp1 = PromoteOp(Node->getOperand(0));
1589 Tmp2 = PromoteOp(Node->getOperand(1));
1590 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1591 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1592
1593 // However, if this is a floating point operation, they will give excess
1594 // precision that we may not be able to tolerate. If we DO allow excess
1595 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001596 // FIXME: Why would we need to round FP ops more than integer ones?
1597 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001598 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1599 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1600 break;
1601
Chris Lattner4d978642005-01-15 22:16:26 +00001602 case ISD::SDIV:
1603 case ISD::SREM:
1604 // These operators require that their input be sign extended.
1605 Tmp1 = PromoteOp(Node->getOperand(0));
1606 Tmp2 = PromoteOp(Node->getOperand(1));
1607 if (MVT::isInteger(NVT)) {
1608 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001609 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001610 }
1611 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1612
1613 // Perform FP_ROUND: this is probably overly pessimistic.
1614 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1615 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1616 break;
1617
1618 case ISD::UDIV:
1619 case ISD::UREM:
1620 // These operators require that their input be zero extended.
1621 Tmp1 = PromoteOp(Node->getOperand(0));
1622 Tmp2 = PromoteOp(Node->getOperand(1));
1623 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00001624 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1625 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001626 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1627 break;
1628
1629 case ISD::SHL:
1630 Tmp1 = PromoteOp(Node->getOperand(0));
1631 Tmp2 = LegalizeOp(Node->getOperand(1));
1632 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1633 break;
1634 case ISD::SRA:
1635 // The input value must be properly sign extended.
1636 Tmp1 = PromoteOp(Node->getOperand(0));
1637 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1638 Tmp2 = LegalizeOp(Node->getOperand(1));
1639 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1640 break;
1641 case ISD::SRL:
1642 // The input value must be properly zero extended.
1643 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001644 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001645 Tmp2 = LegalizeOp(Node->getOperand(1));
1646 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1647 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001648 case ISD::LOAD:
1649 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1650 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001651 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001652 if (MVT::isInteger(NVT))
Chris Lattner5385db52005-05-09 20:23:03 +00001653 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1654 VT);
Chris Lattner391a3512005-04-10 17:40:35 +00001655 else
Chris Lattner5385db52005-05-09 20:23:03 +00001656 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1657 VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001658
1659 // Remember that we legalized the chain.
1660 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1661 break;
1662 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001663 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1664 case Expand: assert(0 && "It's impossible to expand bools");
1665 case Legal:
1666 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1667 break;
1668 case Promote:
1669 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1670 break;
1671 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001672 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1673 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1674 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1675 break;
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001676 case ISD::CALL: {
1677 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1678 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1679
Chris Lattner3d95c142005-01-19 20:24:35 +00001680 std::vector<SDOperand> Ops;
1681 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1682 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1683
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001684 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1685 "Can only promote single result calls");
1686 std::vector<MVT::ValueType> RetTyVTs;
1687 RetTyVTs.reserve(2);
1688 RetTyVTs.push_back(NVT);
1689 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00001690 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001691 Result = SDOperand(NC, 0);
1692
1693 // Insert the new chain mapping.
1694 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1695 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001696 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00001697 case ISD::CTPOP:
1698 case ISD::CTTZ:
1699 case ISD::CTLZ:
1700 Tmp1 = Node->getOperand(0);
1701 //Zero extend the argument
1702 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1703 // Perform the larger operation, then subtract if needed.
1704 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1705 switch(Node->getOpcode())
1706 {
1707 case ISD::CTPOP:
1708 Result = Tmp1;
1709 break;
1710 case ISD::CTTZ:
1711 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1712 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1713 DAG.getConstant(getSizeInBits(NVT), NVT));
1714 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1715 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1716 break;
1717 case ISD::CTLZ:
1718 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1719 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1720 DAG.getConstant(getSizeInBits(NVT) -
1721 getSizeInBits(VT), NVT));
1722 break;
1723 }
1724 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001725 }
1726
1727 assert(Result.Val && "Didn't set a result!");
1728 AddPromotedOperand(Op, Result);
1729 return Result;
1730}
Chris Lattnerdc750592005-01-07 07:47:09 +00001731
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001732/// ExpandAddSub - Find a clever way to expand this add operation into
1733/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001734void SelectionDAGLegalize::
1735ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1736 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001737 // Expand the subcomponents.
1738 SDOperand LHSL, LHSH, RHSL, RHSH;
1739 ExpandOp(LHS, LHSL, LHSH);
1740 ExpandOp(RHS, RHSL, RHSH);
1741
Chris Lattner8ffd0042005-04-11 20:29:59 +00001742 // FIXME: this should be moved to the dag combiner someday.
1743 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1744 if (LHSL.getValueType() == MVT::i32) {
1745 SDOperand LowEl;
1746 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1747 if (C->getValue() == 0)
1748 LowEl = RHSL;
1749 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1750 if (C->getValue() == 0)
1751 LowEl = LHSL;
1752 if (LowEl.Val) {
1753 // Turn this into an add/sub of the high part only.
1754 SDOperand HiEl =
1755 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1756 LowEl.getValueType(), LHSH, RHSH);
1757 Lo = LowEl;
1758 Hi = HiEl;
1759 return;
1760 }
1761 }
1762
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001763 std::vector<SDOperand> Ops;
1764 Ops.push_back(LHSL);
1765 Ops.push_back(LHSH);
1766 Ops.push_back(RHSL);
1767 Ops.push_back(RHSH);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001768 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001769 Hi = Lo.getValue(1);
1770}
1771
Chris Lattner4157c412005-04-02 04:00:59 +00001772void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1773 SDOperand Op, SDOperand Amt,
1774 SDOperand &Lo, SDOperand &Hi) {
1775 // Expand the subcomponents.
1776 SDOperand LHSL, LHSH;
1777 ExpandOp(Op, LHSL, LHSH);
1778
1779 std::vector<SDOperand> Ops;
1780 Ops.push_back(LHSL);
1781 Ops.push_back(LHSH);
1782 Ops.push_back(Amt);
1783 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1784 Hi = Lo.getValue(1);
1785}
1786
1787
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001788/// ExpandShift - Try to find a clever way to expand this shift operation out to
1789/// smaller elements. If we can't find a way that is more efficient than a
1790/// libcall on this target, return false. Otherwise, return true with the
1791/// low-parts expanded into Lo and Hi.
1792bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1793 SDOperand &Lo, SDOperand &Hi) {
1794 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1795 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00001796
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001797 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00001798 SDOperand ShAmt = LegalizeOp(Amt);
1799 MVT::ValueType ShTy = ShAmt.getValueType();
1800 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1801 unsigned NVTBits = MVT::getSizeInBits(NVT);
1802
1803 // Handle the case when Amt is an immediate. Other cases are currently broken
1804 // and are disabled.
1805 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1806 unsigned Cst = CN->getValue();
1807 // Expand the incoming operand to be shifted, so that we have its parts
1808 SDOperand InL, InH;
1809 ExpandOp(Op, InL, InH);
1810 switch(Opc) {
1811 case ISD::SHL:
1812 if (Cst > VTBits) {
1813 Lo = DAG.getConstant(0, NVT);
1814 Hi = DAG.getConstant(0, NVT);
1815 } else if (Cst > NVTBits) {
1816 Lo = DAG.getConstant(0, NVT);
1817 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001818 } else if (Cst == NVTBits) {
1819 Lo = DAG.getConstant(0, NVT);
1820 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00001821 } else {
1822 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1823 Hi = DAG.getNode(ISD::OR, NVT,
1824 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1825 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1826 }
1827 return true;
1828 case ISD::SRL:
1829 if (Cst > VTBits) {
1830 Lo = DAG.getConstant(0, NVT);
1831 Hi = DAG.getConstant(0, NVT);
1832 } else if (Cst > NVTBits) {
1833 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1834 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00001835 } else if (Cst == NVTBits) {
1836 Lo = InH;
1837 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00001838 } else {
1839 Lo = DAG.getNode(ISD::OR, NVT,
1840 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1841 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1842 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1843 }
1844 return true;
1845 case ISD::SRA:
1846 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001847 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001848 DAG.getConstant(NVTBits-1, ShTy));
1849 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001850 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001851 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00001852 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001853 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001854 } else if (Cst == NVTBits) {
1855 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00001856 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00001857 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00001858 } else {
1859 Lo = DAG.getNode(ISD::OR, NVT,
1860 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1861 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1862 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1863 }
1864 return true;
1865 }
1866 }
1867 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1868 // so disable it for now. Currently targets are handling this via SHL_PARTS
1869 // and friends.
1870 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001871
1872 // If we have an efficient select operation (or if the selects will all fold
1873 // away), lower to some complex code, otherwise just emit the libcall.
1874 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1875 !isa<ConstantSDNode>(Amt))
1876 return false;
1877
1878 SDOperand InL, InH;
1879 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001880 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1881 DAG.getConstant(NVTBits, ShTy), ShAmt);
1882
Chris Lattner4d25c042005-01-20 20:29:23 +00001883 // Compare the unmasked shift amount against 32.
1884 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1885 DAG.getConstant(NVTBits, ShTy));
1886
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001887 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1888 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1889 DAG.getConstant(NVTBits-1, ShTy));
1890 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1891 DAG.getConstant(NVTBits-1, ShTy));
1892 }
1893
1894 if (Opc == ISD::SHL) {
1895 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1896 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1897 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001898 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00001899
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001900 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1901 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1902 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001903 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1904 DAG.getSetCC(ISD::SETEQ,
1905 TLI.getSetCCResultTy(), NAmt,
1906 DAG.getConstant(32, ShTy)),
1907 DAG.getConstant(0, NVT),
1908 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001909 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001910 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001911 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001912 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001913
1914 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001915 if (Opc == ISD::SRA)
1916 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1917 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001918 else
1919 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001920 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001921 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001922 }
1923 return true;
1924}
Chris Lattneraac464e2005-01-21 06:05:23 +00001925
Chris Lattner4add7e32005-01-23 04:42:50 +00001926/// FindLatestAdjCallStackDown - Scan up the dag to find the latest (highest
1927/// NodeDepth) node that is an AdjCallStackDown operation and occurs later than
1928/// Found.
1929static void FindLatestAdjCallStackDown(SDNode *Node, SDNode *&Found) {
1930 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1931
1932 // If we found an ADJCALLSTACKDOWN, we already know this node occurs later
1933 // than the Found node. Just remember this node and return.
1934 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) {
1935 Found = Node;
1936 return;
1937 }
1938
1939 // Otherwise, scan the operands of Node to see if any of them is a call.
1940 assert(Node->getNumOperands() != 0 &&
1941 "All leaves should have depth equal to the entry node!");
1942 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
1943 FindLatestAdjCallStackDown(Node->getOperand(i).Val, Found);
1944
1945 // Tail recurse for the last iteration.
1946 FindLatestAdjCallStackDown(Node->getOperand(Node->getNumOperands()-1).Val,
1947 Found);
1948}
1949
1950
1951/// FindEarliestAdjCallStackUp - Scan down the dag to find the earliest (lowest
1952/// NodeDepth) node that is an AdjCallStackUp operation and occurs more recent
1953/// than Found.
1954static void FindEarliestAdjCallStackUp(SDNode *Node, SDNode *&Found) {
1955 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1956
1957 // If we found an ADJCALLSTACKUP, we already know this node occurs earlier
1958 // than the Found node. Just remember this node and return.
1959 if (Node->getOpcode() == ISD::ADJCALLSTACKUP) {
1960 Found = Node;
1961 return;
1962 }
1963
1964 // Otherwise, scan the operands of Node to see if any of them is a call.
1965 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1966 if (UI == E) return;
1967 for (--E; UI != E; ++UI)
1968 FindEarliestAdjCallStackUp(*UI, Found);
1969
1970 // Tail recurse for the last iteration.
1971 FindEarliestAdjCallStackUp(*UI, Found);
1972}
1973
1974/// FindAdjCallStackUp - Given a chained node that is part of a call sequence,
1975/// find the ADJCALLSTACKUP node that terminates the call sequence.
1976static SDNode *FindAdjCallStackUp(SDNode *Node) {
1977 if (Node->getOpcode() == ISD::ADJCALLSTACKUP)
1978 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00001979 if (Node->use_empty())
1980 return 0; // No adjcallstackup
Chris Lattner4add7e32005-01-23 04:42:50 +00001981
1982 if (Node->hasOneUse()) // Simple case, only has one user to check.
1983 return FindAdjCallStackUp(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00001984
Chris Lattner4add7e32005-01-23 04:42:50 +00001985 SDOperand TheChain(Node, Node->getNumValues()-1);
1986 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00001987
1988 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner4add7e32005-01-23 04:42:50 +00001989 E = Node->use_end(); ; ++UI) {
1990 assert(UI != E && "Didn't find a user of the tokchain, no ADJCALLSTACKUP!");
Misha Brukman835702a2005-04-21 22:36:52 +00001991
Chris Lattner4add7e32005-01-23 04:42:50 +00001992 // Make sure to only follow users of our token chain.
1993 SDNode *User = *UI;
1994 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
1995 if (User->getOperand(i) == TheChain)
1996 return FindAdjCallStackUp(User);
1997 }
1998 assert(0 && "Unreachable");
1999 abort();
2000}
2001
Chris Lattner06bbeb62005-05-11 19:02:11 +00002002/// FindAdjCallStackDown - Given a chained node that is part of a call sequence,
2003/// find the ADJCALLSTACKDOWN node that initiates the call sequence.
2004static SDNode *FindAdjCallStackDown(SDNode *Node) {
2005 assert(Node && "Didn't find adjcallstackdown for a call??");
2006 if (Node->getOpcode() == ISD::ADJCALLSTACKDOWN) return Node;
2007
2008 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2009 "Node doesn't have a token chain argument!");
2010 return FindAdjCallStackDown(Node->getOperand(0).Val);
2011}
2012
2013
Chris Lattner4add7e32005-01-23 04:42:50 +00002014/// FindInputOutputChains - If we are replacing an operation with a call we need
2015/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002016/// properly serialize the calls in the block. The returned operand is the
2017/// input chain value for the new call (e.g. the entry node or the previous
2018/// call), and OutChain is set to be the chain node to update to point to the
2019/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002020static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2021 SDOperand Entry) {
2022 SDNode *LatestAdjCallStackDown = Entry.Val;
Nate Begemanadd0c632005-04-11 03:01:51 +00002023 SDNode *LatestAdjCallStackUp = 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002024 FindLatestAdjCallStackDown(OpNode, LatestAdjCallStackDown);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002025 //std::cerr<<"Found node: "; LatestAdjCallStackDown->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002026
Nate Begemanadd0c632005-04-11 03:01:51 +00002027 // It is possible that no ISD::ADJCALLSTACKDOWN was found because there is no
2028 // previous call in the function. LatestCallStackDown may in that case be
2029 // the entry node itself. Do not attempt to find a matching ADJCALLSTACKUP
2030 // unless LatestCallStackDown is an ADJCALLSTACKDOWN.
2031 if (LatestAdjCallStackDown->getOpcode() == ISD::ADJCALLSTACKDOWN)
2032 LatestAdjCallStackUp = FindAdjCallStackUp(LatestAdjCallStackDown);
2033 else
2034 LatestAdjCallStackUp = Entry.Val;
2035 assert(LatestAdjCallStackUp && "NULL return from FindAdjCallStackUp");
Misha Brukman835702a2005-04-21 22:36:52 +00002036
Chris Lattner06bbeb62005-05-11 19:02:11 +00002037 // Finally, find the first call that this must come before, first we find the
2038 // adjcallstackup that ends the call.
2039 OutChain = 0;
2040 FindEarliestAdjCallStackUp(OpNode, OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002041
Chris Lattner06bbeb62005-05-11 19:02:11 +00002042 // If we found one, translate from the adj up to the adjdown.
2043 if (OutChain)
2044 OutChain = FindAdjCallStackDown(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002045
2046 return SDOperand(LatestAdjCallStackUp, 0);
2047}
2048
Chris Lattner06bbeb62005-05-11 19:02:11 +00002049/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
2050static void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain,
2051 SelectionDAG &DAG) {
2052 // Nothing to splice it into?
2053 if (OutChain == 0) return;
2054
2055 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2056 //OutChain->dump();
2057
2058 // Form a token factor node merging the old inval and the new inval.
2059 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2060 OutChain->getOperand(0));
2061 // Change the node to refer to the new token.
2062 OutChain->setAdjCallChain(InToken);
2063}
Chris Lattner4add7e32005-01-23 04:42:50 +00002064
2065
Chris Lattneraac464e2005-01-21 06:05:23 +00002066// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2067// does not fit into a register, return the lo part and set the hi part to the
2068// by-reg argument. If it does fit into a single register, return the result
2069// and leave the Hi part unset.
2070SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2071 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002072 SDNode *OutChain;
2073 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2074 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002075 if (InChain.Val == 0)
2076 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002077
Chris Lattneraac464e2005-01-21 06:05:23 +00002078 TargetLowering::ArgListTy Args;
2079 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2080 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2081 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2082 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2083 }
2084 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002085
Chris Lattner06bbeb62005-05-11 19:02:11 +00002086 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002087 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002088 std::pair<SDOperand,SDOperand> CallInfo =
2089 TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG);
2090 SpliceCallInto(CallInfo.second, OutChain, DAG);
2091
2092 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002093 default: assert(0 && "Unknown thing");
2094 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002095 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002096 case Promote:
2097 assert(0 && "Cannot promote this yet!");
2098 case Expand:
2099 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002100 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002101 return Lo;
2102 }
2103}
2104
Chris Lattner4add7e32005-01-23 04:42:50 +00002105
Chris Lattneraac464e2005-01-21 06:05:23 +00002106/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2107/// destination type is legal.
2108SDOperand SelectionDAGLegalize::
2109ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2110 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2111 assert(getTypeAction(Source.getValueType()) == Expand &&
2112 "This is not an expansion!");
2113 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2114
Chris Lattner06bbeb62005-05-11 19:02:11 +00002115 if (!isSigned) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002116 // If this is unsigned, and not supported, first perform the conversion to
2117 // signed, then adjust the result if the sign bit is set.
Chris Lattner0efd77e2005-04-13 03:42:14 +00002118 SDOperand SignedConv = ExpandIntToFP(true, DestTy, Source);
Chris Lattneraac464e2005-01-21 06:05:23 +00002119
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002120 assert(Source.getValueType() == MVT::i64 &&
2121 "This only works for 64-bit -> FP");
2122 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2123 // incoming integer is set. To handle this, we dynamically test to see if
2124 // it is set, and, if so, add a fudge factor.
2125 SDOperand Lo, Hi;
2126 ExpandOp(Source, Lo, Hi);
2127
2128 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2129 DAG.getConstant(0, Hi.getValueType()));
2130 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2131 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2132 SignSet, Four, Zero);
2133 // FIXME: This is almost certainly broken for big-endian systems. Should
2134 // this just put the fudge factor in the low bits of the uint64 constant or?
2135 static Constant *FudgeFactor =
2136 ConstantUInt::get(Type::ULongTy, 0x5f800000ULL << 32);
2137
2138 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2139 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2140 TLI.getPointerTy());
2141 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2142 SDOperand FudgeInReg;
2143 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002144 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2145 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002146 else {
2147 assert(DestTy == MVT::f64 && "Unexpected conversion");
2148 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002149 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002150 }
2151 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002152 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002153
2154 SDNode *OutChain = 0;
2155 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2156 DAG.getEntryNode());
2157 const char *FnName = 0;
2158 if (DestTy == MVT::f32)
2159 FnName = "__floatdisf";
2160 else {
2161 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2162 FnName = "__floatdidf";
2163 }
2164
Chris Lattneraac464e2005-01-21 06:05:23 +00002165 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2166
2167 TargetLowering::ArgListTy Args;
2168 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
2169 Args.push_back(std::make_pair(Source, ArgTy));
2170
2171 // We don't care about token chains for libcalls. We just use the entry
2172 // node as our input and ignore the output chain. This allows us to place
2173 // calls wherever we need them to satisfy data dependences.
2174 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002175
2176 std::pair<SDOperand,SDOperand> CallResult =
2177 TLI.LowerCallTo(InChain, RetTy, false, Callee, Args, DAG);
2178
2179 SpliceCallInto(CallResult.second, OutChain, DAG);
2180 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002181}
Misha Brukman835702a2005-04-21 22:36:52 +00002182
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002183
2184
Chris Lattnerdc750592005-01-07 07:47:09 +00002185/// ExpandOp - Expand the specified SDOperand into its two component pieces
2186/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2187/// LegalizeNodes map is filled in for any results that are not expanded, the
2188/// ExpandedNodes map is filled in for any results that are expanded, and the
2189/// Lo/Hi values are returned.
2190void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2191 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002192 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002193 SDNode *Node = Op.Val;
2194 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2195 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2196 assert(MVT::isInteger(NVT) && NVT < VT &&
2197 "Cannot expand to FP value or to larger int value!");
2198
2199 // If there is more than one use of this, see if we already expanded it.
2200 // There is no use remembering values that only have a single use, as the map
2201 // entries will never be reused.
2202 if (!Node->hasOneUse()) {
2203 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2204 = ExpandedNodes.find(Op);
2205 if (I != ExpandedNodes.end()) {
2206 Lo = I->second.first;
2207 Hi = I->second.second;
2208 return;
2209 }
2210 }
2211
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002212 // Expanding to multiple registers needs to perform an optimization step, and
2213 // is not careful to avoid operations the target does not support. Make sure
2214 // that all generated operations are legalized in the next iteration.
2215 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002216
Chris Lattnerdc750592005-01-07 07:47:09 +00002217 switch (Node->getOpcode()) {
2218 default:
2219 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2220 assert(0 && "Do not know how to expand this operator!");
2221 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002222 case ISD::UNDEF:
2223 Lo = DAG.getNode(ISD::UNDEF, NVT);
2224 Hi = DAG.getNode(ISD::UNDEF, NVT);
2225 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002226 case ISD::Constant: {
2227 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2228 Lo = DAG.getConstant(Cst, NVT);
2229 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2230 break;
2231 }
2232
2233 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00002234 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00002235 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00002236 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2237 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002238
Chris Lattner3b8e7192005-01-14 22:38:01 +00002239 // Remember that we legalized the chain.
2240 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2241
Chris Lattnerdc750592005-01-07 07:47:09 +00002242 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2243 break;
2244 }
2245
Chris Lattner32e08b72005-03-28 22:03:13 +00002246 case ISD::BUILD_PAIR:
2247 // Legalize both operands. FIXME: in the future we should handle the case
2248 // where the two elements are not legal.
2249 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2250 Lo = LegalizeOp(Node->getOperand(0));
2251 Hi = LegalizeOp(Node->getOperand(1));
2252 break;
2253
Chris Lattner55e9cde2005-05-11 04:51:16 +00002254 case ISD::CTPOP:
2255 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002256 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2257 DAG.getNode(ISD::CTPOP, NVT, Lo),
2258 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002259 Hi = DAG.getConstant(0, NVT);
2260 break;
2261
2262 case ISD::CTTZ:
2263 case ISD::CTLZ:
2264 assert(0 && "ct intrinsics cannot be expanded!");
2265
Chris Lattnerdc750592005-01-07 07:47:09 +00002266 case ISD::LOAD: {
2267 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2268 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002269 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002270
2271 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002272 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002273 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2274 getIntPtrConstant(IncrementSize));
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002275 //Is this safe? declaring that the two parts of the split load
2276 //are from the same instruction?
2277 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002278
2279 // Build a factor node to remember that this load is independent of the
2280 // other one.
2281 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2282 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002283
Chris Lattnerdc750592005-01-07 07:47:09 +00002284 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002285 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002286 if (!TLI.isLittleEndian())
2287 std::swap(Lo, Hi);
2288 break;
2289 }
2290 case ISD::CALL: {
2291 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2292 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2293
Chris Lattner3d95c142005-01-19 20:24:35 +00002294 bool Changed = false;
2295 std::vector<SDOperand> Ops;
2296 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2297 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2298 Changed |= Ops.back() != Node->getOperand(i);
2299 }
2300
Chris Lattnerdc750592005-01-07 07:47:09 +00002301 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2302 "Can only expand a call once so far, not i64 -> i16!");
2303
2304 std::vector<MVT::ValueType> RetTyVTs;
2305 RetTyVTs.reserve(3);
2306 RetTyVTs.push_back(NVT);
2307 RetTyVTs.push_back(NVT);
2308 RetTyVTs.push_back(MVT::Other);
Chris Lattner3d95c142005-01-19 20:24:35 +00002309 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops);
Chris Lattnerdc750592005-01-07 07:47:09 +00002310 Lo = SDOperand(NC, 0);
2311 Hi = SDOperand(NC, 1);
2312
2313 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002314 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002315 break;
2316 }
2317 case ISD::AND:
2318 case ISD::OR:
2319 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2320 SDOperand LL, LH, RL, RH;
2321 ExpandOp(Node->getOperand(0), LL, LH);
2322 ExpandOp(Node->getOperand(1), RL, RH);
2323 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2324 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2325 break;
2326 }
2327 case ISD::SELECT: {
2328 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002329
2330 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2331 case Expand: assert(0 && "It's impossible to expand bools");
2332 case Legal:
2333 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2334 break;
2335 case Promote:
2336 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2337 break;
2338 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002339 ExpandOp(Node->getOperand(1), LL, LH);
2340 ExpandOp(Node->getOperand(2), RL, RH);
2341 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2342 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2343 break;
2344 }
2345 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002346 SDOperand In;
2347 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2348 case Expand: assert(0 && "expand-expand not implemented yet!");
2349 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2350 case Promote:
2351 In = PromoteOp(Node->getOperand(0));
2352 // Emit the appropriate sign_extend_inreg to get the value we want.
2353 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2354 Node->getOperand(0).getValueType());
2355 break;
2356 }
2357
Chris Lattnerdc750592005-01-07 07:47:09 +00002358 // The low part is just a sign extension of the input (which degenerates to
2359 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002360 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002361
Chris Lattnerdc750592005-01-07 07:47:09 +00002362 // The high part is obtained by SRA'ing all but one of the bits of the lo
2363 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002364 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002365 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2366 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002367 break;
2368 }
Chris Lattner47844892005-04-03 23:41:52 +00002369 case ISD::ZERO_EXTEND: {
2370 SDOperand In;
2371 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2372 case Expand: assert(0 && "expand-expand not implemented yet!");
2373 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2374 case Promote:
2375 In = PromoteOp(Node->getOperand(0));
2376 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00002377 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00002378 break;
2379 }
2380
Chris Lattnerdc750592005-01-07 07:47:09 +00002381 // The low part is just a zero extension of the input (which degenerates to
2382 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002383 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002384
Chris Lattnerdc750592005-01-07 07:47:09 +00002385 // The high part is just a zero.
2386 Hi = DAG.getConstant(0, NVT);
2387 break;
Chris Lattner47844892005-04-03 23:41:52 +00002388 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002389 // These operators cannot be expanded directly, emit them as calls to
2390 // library functions.
2391 case ISD::FP_TO_SINT:
2392 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002393 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002394 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002395 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002396 break;
2397 case ISD::FP_TO_UINT:
2398 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002399 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002400 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002401 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002402 break;
2403
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002404 case ISD::SHL:
2405 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002406 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002407 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002408
2409 // If this target supports SHL_PARTS, use it.
2410 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002411 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2412 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002413 break;
2414 }
2415
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002416 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002417 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002418 break;
2419
2420 case ISD::SRA:
2421 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002422 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002423 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002424
2425 // If this target supports SRA_PARTS, use it.
2426 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002427 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2428 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002429 break;
2430 }
2431
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002432 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002433 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002434 break;
2435 case ISD::SRL:
2436 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002437 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002438 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002439
2440 // If this target supports SRL_PARTS, use it.
2441 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002442 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2443 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002444 break;
2445 }
2446
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002447 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002448 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002449 break;
2450
Misha Brukman835702a2005-04-21 22:36:52 +00002451 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002452 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2453 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002454 break;
2455 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002456 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2457 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002458 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002459 case ISD::MUL: {
2460 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2461 SDOperand LL, LH, RL, RH;
2462 ExpandOp(Node->getOperand(0), LL, LH);
2463 ExpandOp(Node->getOperand(1), RL, RH);
2464 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2465 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2466 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2467 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2468 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2469 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2470 } else {
2471 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2472 }
2473 break;
2474 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002475 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2476 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2477 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2478 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002479 }
2480
2481 // Remember in a map if the values will be reused later.
2482 if (!Node->hasOneUse()) {
2483 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2484 std::make_pair(Lo, Hi))).second;
2485 assert(isNew && "Value already expanded?!?");
2486 }
2487}
2488
2489
2490// SelectionDAG::Legalize - This is the entry point for the file.
2491//
Chris Lattner4add7e32005-01-23 04:42:50 +00002492void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002493 /// run - This is the main entry point to this class.
2494 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002495 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002496}
2497