blob: 14b4c5a4b2394d7fb523e4cdd215048e6b9ff7d9 [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 Lattnera5bf1032005-05-12 04:49:08 +0000133 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
134
Chris Lattnerdc750592005-01-07 07:47:09 +0000135 SDOperand getIntPtrConstant(uint64_t Val) {
136 return DAG.getConstant(Val, TLI.getPointerTy());
137 }
138};
139}
140
141
Chris Lattner4add7e32005-01-23 04:42:50 +0000142SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
143 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
144 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000145 assert(MVT::LAST_VALUETYPE <= 16 &&
146 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000147}
148
Chris Lattnerdc750592005-01-07 07:47:09 +0000149void SelectionDAGLegalize::LegalizeDAG() {
150 SDOperand OldRoot = DAG.getRoot();
151 SDOperand NewRoot = LegalizeOp(OldRoot);
152 DAG.setRoot(NewRoot);
153
154 ExpandedNodes.clear();
155 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000156 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000157
158 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000159 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000160}
161
162SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000163 assert(getTypeAction(Op.getValueType()) == Legal &&
164 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000165 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000166
Chris Lattnerdc750592005-01-07 07:47:09 +0000167 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000168 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000169 if (Node->getNumValues() > 1) {
170 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
171 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000172 case Legal: break; // Nothing to do.
173 case Expand: {
174 SDOperand T1, T2;
175 ExpandOp(Op.getValue(i), T1, T2);
176 assert(LegalizedNodes.count(Op) &&
177 "Expansion didn't add legal operands!");
178 return LegalizedNodes[Op];
179 }
180 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000181 PromoteOp(Op.getValue(i));
182 assert(LegalizedNodes.count(Op) &&
183 "Expansion didn't add legal operands!");
184 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000185 }
186 }
187
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000188 // Note that LegalizeOp may be reentered even from single-use nodes, which
189 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000190 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
191 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000192
Chris Lattnerec26b482005-01-09 19:03:49 +0000193 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000194
195 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000196
197 switch (Node->getOpcode()) {
198 default:
199 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
200 assert(0 && "Do not know how to legalize this operator!");
201 abort();
202 case ISD::EntryToken:
203 case ISD::FrameIndex:
204 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000205 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000206 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000207 assert(getTypeAction(Node->getValueType(0)) == Legal &&
208 "This must be legal!");
209 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000210 case ISD::CopyFromReg:
211 Tmp1 = LegalizeOp(Node->getOperand(0));
212 if (Tmp1 != Node->getOperand(0))
213 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
214 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000215 else
216 Result = Op.getValue(0);
217
218 // Since CopyFromReg produces two values, make sure to remember that we
219 // legalized both of them.
220 AddLegalizedOperand(Op.getValue(0), Result);
221 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
222 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000223 case ISD::ImplicitDef:
224 Tmp1 = LegalizeOp(Node->getOperand(0));
225 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000226 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000227 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000228 case ISD::UNDEF: {
229 MVT::ValueType VT = Op.getValueType();
230 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000231 default: assert(0 && "This action is not supported yet!");
232 case TargetLowering::Expand:
233 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000234 if (MVT::isInteger(VT))
235 Result = DAG.getConstant(0, VT);
236 else if (MVT::isFloatingPoint(VT))
237 Result = DAG.getConstantFP(0, VT);
238 else
239 assert(0 && "Unknown value type!");
240 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000241 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000242 break;
243 }
244 break;
245 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000246 case ISD::Constant:
247 // We know we don't need to expand constants here, constants only have one
248 // value and we check that it is fine above.
249
250 // FIXME: Maybe we should handle things like targets that don't support full
251 // 32-bit immediates?
252 break;
253 case ISD::ConstantFP: {
254 // Spill FP immediates to the constant pool if the target cannot directly
255 // codegen them. Targets often have some immediate values that can be
256 // efficiently generated into an FP register without a load. We explicitly
257 // leave these constants as ConstantFP nodes for the target to deal with.
258
259 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
260
261 // Check to see if this FP immediate is already legal.
262 bool isLegal = false;
263 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
264 E = TLI.legal_fpimm_end(); I != E; ++I)
265 if (CFP->isExactlyValue(*I)) {
266 isLegal = true;
267 break;
268 }
269
270 if (!isLegal) {
271 // Otherwise we need to spill the constant to memory.
272 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
273
274 bool Extend = false;
275
276 // If a FP immediate is precise when represented as a float, we put it
277 // into the constant pool as a float, even if it's is statically typed
278 // as a double.
279 MVT::ValueType VT = CFP->getValueType(0);
280 bool isDouble = VT == MVT::f64;
281 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
282 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000283 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
284 // Only do this if the target has a native EXTLOAD instruction from
285 // f32.
286 TLI.getOperationAction(ISD::EXTLOAD,
287 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000288 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
289 VT = MVT::f32;
290 Extend = true;
291 }
Misha Brukman835702a2005-04-21 22:36:52 +0000292
Chris Lattnerdc750592005-01-07 07:47:09 +0000293 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
294 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000295 if (Extend) {
296 Result = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(), CPIdx,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000297 DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000298 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000299 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
300 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000301 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000302 }
303 break;
304 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000305 case ISD::TokenFactor: {
306 std::vector<SDOperand> Ops;
307 bool Changed = false;
308 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000309 SDOperand Op = Node->getOperand(i);
310 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000311 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000312 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
313 Changed = true;
314 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
315 Ops.push_back(LegalizeOp(Op.getOperand(j)));
316 } else {
317 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
318 Changed |= Ops[i] != Op;
319 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000320 }
321 if (Changed)
322 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
323 break;
324 }
325
Chris Lattner2dce7032005-05-12 23:24:06 +0000326 case ISD::CALLSEQ_START:
327 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000328 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000329 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000330 Tmp2 = Node->getOperand(0);
331 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000332 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000333
334 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
335 // this will cause the maps used to memoize results to get confused.
336 // Create and add a dummy use, just to increase its use count. This will
337 // be removed at the end of legalize when dead nodes are removed.
338 if (Tmp2.Val->hasOneUse())
339 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
340 DAG.getConstant(0, MVT::i32));
341 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000342 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000343 // nodes are treated specially and are mutated in place. This makes the dag
344 // legalization process more efficient and also makes libcall insertion
345 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000346 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000347 case ISD::DYNAMIC_STACKALLOC:
348 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
349 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
350 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
351 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
352 Tmp3 != Node->getOperand(2))
353 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, Node->getValueType(0),
354 Tmp1, Tmp2, Tmp3);
Chris Lattner02f5ce22005-01-09 19:07:54 +0000355 else
356 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000357
358 // Since this op produces two values, make sure to remember that we
359 // legalized both of them.
360 AddLegalizedOperand(SDOperand(Node, 0), Result);
361 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
362 return Result.getValue(Op.ResNo);
363
Chris Lattnerd0feb642005-05-13 18:43:43 +0000364 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000365 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000366 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
367 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000368
369 bool Changed = false;
370 std::vector<SDOperand> Ops;
371 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
372 Ops.push_back(LegalizeOp(Node->getOperand(i)));
373 Changed |= Ops.back() != Node->getOperand(i);
374 }
375
376 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000377 std::vector<MVT::ValueType> RetTyVTs;
378 RetTyVTs.reserve(Node->getNumValues());
379 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000380 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000381 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
382 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000383 } else {
384 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000385 }
Chris Lattner9242c502005-01-09 19:43:23 +0000386 // Since calls produce multiple values, make sure to remember that we
387 // legalized all of them.
388 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
389 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
390 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000391 }
Chris Lattner68a12142005-01-07 22:12:08 +0000392 case ISD::BR:
393 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
394 if (Tmp1 != Node->getOperand(0))
395 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
396 break;
397
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000398 case ISD::BRCOND:
399 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000400
401 switch (getTypeAction(Node->getOperand(1).getValueType())) {
402 case Expand: assert(0 && "It's impossible to expand bools");
403 case Legal:
404 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
405 break;
406 case Promote:
407 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
408 break;
409 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000410 // Basic block destination (Op#2) is always legal.
411 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
412 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
413 Node->getOperand(2));
414 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000415 case ISD::BRCONDTWOWAY:
416 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
417 switch (getTypeAction(Node->getOperand(1).getValueType())) {
418 case Expand: assert(0 && "It's impossible to expand bools");
419 case Legal:
420 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
421 break;
422 case Promote:
423 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
424 break;
425 }
426 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
427 // pair.
428 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
429 case TargetLowering::Promote:
430 default: assert(0 && "This action is not supported yet!");
431 case TargetLowering::Legal:
432 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
433 std::vector<SDOperand> Ops;
434 Ops.push_back(Tmp1);
435 Ops.push_back(Tmp2);
436 Ops.push_back(Node->getOperand(2));
437 Ops.push_back(Node->getOperand(3));
438 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
439 }
440 break;
441 case TargetLowering::Expand:
442 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
443 Node->getOperand(2));
444 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
445 break;
446 }
447 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000448
Chris Lattnerdc750592005-01-07 07:47:09 +0000449 case ISD::LOAD:
450 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
451 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000452
Chris Lattnerdc750592005-01-07 07:47:09 +0000453 if (Tmp1 != Node->getOperand(0) ||
454 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000455 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
456 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000457 else
458 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000459
Chris Lattnerea4ca942005-01-07 22:28:47 +0000460 // Since loads produce two values, make sure to remember that we legalized
461 // both of them.
462 AddLegalizedOperand(SDOperand(Node, 0), Result);
463 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
464 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000465
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000466 case ISD::EXTLOAD:
467 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000468 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000469 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
470 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000471
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000472 MVT::ValueType SrcVT = cast<MVTSDNode>(Node)->getExtraValueType();
473 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000474 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000475 case TargetLowering::Promote:
476 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
477 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000478 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000479 // Since loads produce two values, make sure to remember that we legalized
480 // both of them.
481 AddLegalizedOperand(SDOperand(Node, 0), Result);
482 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
483 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000484
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000485 case TargetLowering::Legal:
486 if (Tmp1 != Node->getOperand(0) ||
487 Tmp2 != Node->getOperand(1))
488 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000489 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000490 else
491 Result = SDOperand(Node, 0);
492
493 // Since loads produce two values, make sure to remember that we legalized
494 // both of them.
495 AddLegalizedOperand(SDOperand(Node, 0), Result);
496 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
497 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000498 case TargetLowering::Expand:
499 assert(Node->getOpcode() != ISD::EXTLOAD &&
500 "EXTLOAD should always be supported!");
501 // Turn the unsupported load into an EXTLOAD followed by an explicit
502 // zero/sign extend inreg.
503 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000504 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000505 SDOperand ValRes;
506 if (Node->getOpcode() == ISD::SEXTLOAD)
507 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
508 Result, SrcVT);
509 else
510 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000511 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
512 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
513 if (Op.ResNo)
514 return Result.getValue(1);
515 return ValRes;
516 }
517 assert(0 && "Unreachable");
518 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000519 case ISD::EXTRACT_ELEMENT:
520 // Get both the low and high parts.
521 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
522 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
523 Result = Tmp2; // 1 -> Hi
524 else
525 Result = Tmp1; // 0 -> Lo
526 break;
527
528 case ISD::CopyToReg:
529 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000530
Chris Lattnerdc750592005-01-07 07:47:09 +0000531 switch (getTypeAction(Node->getOperand(1).getValueType())) {
532 case Legal:
533 // Legalize the incoming value (must be legal).
534 Tmp2 = LegalizeOp(Node->getOperand(1));
535 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000536 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000537 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000538 case Promote:
539 Tmp2 = PromoteOp(Node->getOperand(1));
540 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
541 break;
542 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000543 SDOperand Lo, Hi;
Misha Brukman835702a2005-04-21 22:36:52 +0000544 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000545 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000546 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
547 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
548 // Note that the copytoreg nodes are independent of each other.
549 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000550 assert(isTypeLegal(Result.getValueType()) &&
551 "Cannot expand multiple times yet (i64 -> i16)");
552 break;
553 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000554 break;
555
556 case ISD::RET:
557 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
558 switch (Node->getNumOperands()) {
559 case 2: // ret val
560 switch (getTypeAction(Node->getOperand(1).getValueType())) {
561 case Legal:
562 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000563 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000564 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
565 break;
566 case Expand: {
567 SDOperand Lo, Hi;
568 ExpandOp(Node->getOperand(1), Lo, Hi);
569 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000570 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000571 }
572 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000573 Tmp2 = PromoteOp(Node->getOperand(1));
574 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
575 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000576 }
577 break;
578 case 1: // ret void
579 if (Tmp1 != Node->getOperand(0))
580 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
581 break;
582 default: { // ret <values>
583 std::vector<SDOperand> NewValues;
584 NewValues.push_back(Tmp1);
585 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
586 switch (getTypeAction(Node->getOperand(i).getValueType())) {
587 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000588 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000589 break;
590 case Expand: {
591 SDOperand Lo, Hi;
592 ExpandOp(Node->getOperand(i), Lo, Hi);
593 NewValues.push_back(Lo);
594 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000595 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000596 }
597 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000598 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000599 }
600 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
601 break;
602 }
603 }
604 break;
605 case ISD::STORE:
606 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
607 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
608
Chris Lattnere69daaf2005-01-08 06:25:56 +0000609 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000610 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000611 if (CFP->getValueType(0) == MVT::f32) {
612 union {
613 unsigned I;
614 float F;
615 } V;
616 V.F = CFP->getValue();
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000617 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000618 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000619 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000620 } else {
621 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
622 union {
623 uint64_t I;
624 double F;
625 } V;
626 V.F = CFP->getValue();
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000627 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner5385db52005-05-09 20:23:03 +0000628 DAG.getConstant(V.I, MVT::i64), Tmp2,
629 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000630 }
Chris Lattnera4743132005-02-22 07:23:39 +0000631 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000632 }
633
Chris Lattnerdc750592005-01-07 07:47:09 +0000634 switch (getTypeAction(Node->getOperand(1).getValueType())) {
635 case Legal: {
636 SDOperand Val = LegalizeOp(Node->getOperand(1));
637 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
638 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +0000639 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
640 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000641 break;
642 }
643 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000644 // Truncate the value and store the result.
645 Tmp3 = PromoteOp(Node->getOperand(1));
646 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000647 Node->getOperand(3),
648 Node->getOperand(1).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000649 break;
650
Chris Lattnerdc750592005-01-07 07:47:09 +0000651 case Expand:
652 SDOperand Lo, Hi;
653 ExpandOp(Node->getOperand(1), Lo, Hi);
654
655 if (!TLI.isLittleEndian())
656 std::swap(Lo, Hi);
657
Chris Lattner55e9cde2005-05-11 04:51:16 +0000658 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
659 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000660 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000661 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
662 getIntPtrConstant(IncrementSize));
663 assert(isTypeLegal(Tmp2.getValueType()) &&
664 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000665 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +0000666 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
667 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000668 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
669 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000670 }
671 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000672 case ISD::PCMARKER:
673 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000674 if (Tmp1 != Node->getOperand(0))
675 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000676 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000677 case ISD::TRUNCSTORE:
678 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
679 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
680
681 switch (getTypeAction(Node->getOperand(1).getValueType())) {
682 case Legal:
683 Tmp2 = LegalizeOp(Node->getOperand(1));
684 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
685 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000686 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000687 Node->getOperand(3),
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000688 cast<MVTSDNode>(Node)->getExtraValueType());
689 break;
690 case Promote:
691 case Expand:
692 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
693 }
694 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000695 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000696 switch (getTypeAction(Node->getOperand(0).getValueType())) {
697 case Expand: assert(0 && "It's impossible to expand bools");
698 case Legal:
699 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
700 break;
701 case Promote:
702 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
703 break;
704 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000705 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000706 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000707
708 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
709 default: assert(0 && "This action is not supported yet!");
710 case TargetLowering::Legal:
711 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
712 Tmp3 != Node->getOperand(2))
713 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
714 Tmp1, Tmp2, Tmp3);
715 break;
716 case TargetLowering::Promote: {
717 MVT::ValueType NVT =
718 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
719 unsigned ExtOp, TruncOp;
720 if (MVT::isInteger(Tmp2.getValueType())) {
721 ExtOp = ISD::ZERO_EXTEND;
722 TruncOp = ISD::TRUNCATE;
723 } else {
724 ExtOp = ISD::FP_EXTEND;
725 TruncOp = ISD::FP_ROUND;
726 }
727 // Promote each of the values to the new type.
728 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
729 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
730 // Perform the larger operation, then round down.
731 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
732 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
733 break;
734 }
735 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000736 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000737 case ISD::SETCC:
738 switch (getTypeAction(Node->getOperand(0).getValueType())) {
739 case Legal:
740 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
741 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
742 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
743 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000744 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000745 break;
746 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000747 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
748 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
749
750 // If this is an FP compare, the operands have already been extended.
751 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
752 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000753 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000754
755 // Otherwise, we have to insert explicit sign or zero extends. Note
756 // that we could insert sign extends for ALL conditions, but zero extend
757 // is cheaper on many machines (an AND instead of two shifts), so prefer
758 // it.
759 switch (cast<SetCCSDNode>(Node)->getCondition()) {
760 default: assert(0 && "Unknown integer comparison!");
761 case ISD::SETEQ:
762 case ISD::SETNE:
763 case ISD::SETUGE:
764 case ISD::SETUGT:
765 case ISD::SETULE:
766 case ISD::SETULT:
767 // ALL of these operations will work if we either sign or zero extend
768 // the operands (including the unsigned comparisons!). Zero extend is
769 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +0000770 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
771 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000772 break;
773 case ISD::SETGE:
774 case ISD::SETGT:
775 case ISD::SETLT:
776 case ISD::SETLE:
777 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
778 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
779 break;
780 }
781
782 }
783 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000784 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000785 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000786 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000787 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
788 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
789 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
790 switch (cast<SetCCSDNode>(Node)->getCondition()) {
791 case ISD::SETEQ:
792 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +0000793 if (RHSLo == RHSHi)
794 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
795 if (RHSCST->isAllOnesValue()) {
796 // Comparison to -1.
797 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukman835702a2005-04-21 22:36:52 +0000798 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner71ff44e2005-04-12 01:46:05 +0000799 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukman835702a2005-04-21 22:36:52 +0000800 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +0000801 }
802
Chris Lattnerdc750592005-01-07 07:47:09 +0000803 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
804 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
805 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +0000806 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000807 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000808 DAG.getConstant(0, Tmp1.getValueType()));
809 break;
810 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +0000811 // If this is a comparison of the sign bit, just look at the top part.
812 // X > -1, x < 0
813 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukman835702a2005-04-21 22:36:52 +0000814 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +0000815 CST->getValue() == 0) || // X < 0
816 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
817 (CST->isAllOnesValue()))) // X > -1
818 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
819 Node->getValueType(0), LHSHi, RHSHi);
820
Chris Lattnerdc750592005-01-07 07:47:09 +0000821 // FIXME: This generated code sucks.
822 ISD::CondCode LowCC;
823 switch (cast<SetCCSDNode>(Node)->getCondition()) {
824 default: assert(0 && "Unknown integer setcc!");
825 case ISD::SETLT:
826 case ISD::SETULT: LowCC = ISD::SETULT; break;
827 case ISD::SETGT:
828 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
829 case ISD::SETLE:
830 case ISD::SETULE: LowCC = ISD::SETULE; break;
831 case ISD::SETGE:
832 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
833 }
Misha Brukman835702a2005-04-21 22:36:52 +0000834
Chris Lattnerdc750592005-01-07 07:47:09 +0000835 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
836 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
837 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
838
839 // NOTE: on targets without efficient SELECT of bools, we can always use
840 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000841 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000842 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000843 Node->getValueType(0), LHSHi, RHSHi);
844 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
845 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
846 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000847 break;
848 }
849 }
850 break;
851
Chris Lattner85d70c62005-01-11 05:57:22 +0000852 case ISD::MEMSET:
853 case ISD::MEMCPY:
854 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +0000855 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000856 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
857
858 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
859 switch (getTypeAction(Node->getOperand(2).getValueType())) {
860 case Expand: assert(0 && "Cannot expand a byte!");
861 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000862 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000863 break;
864 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +0000865 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000866 break;
867 }
868 } else {
Misha Brukman835702a2005-04-21 22:36:52 +0000869 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000870 }
Chris Lattner5aa75e42005-02-02 03:44:41 +0000871
872 SDOperand Tmp4;
873 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000874 case Expand: assert(0 && "Cannot expand this yet!");
875 case Legal:
876 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000877 break;
878 case Promote:
879 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +0000880 break;
881 }
882
883 SDOperand Tmp5;
884 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
885 case Expand: assert(0 && "Cannot expand this yet!");
886 case Legal:
887 Tmp5 = LegalizeOp(Node->getOperand(4));
888 break;
889 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +0000890 Tmp5 = PromoteOp(Node->getOperand(4));
891 break;
892 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000893
894 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
895 default: assert(0 && "This action not implemented for this operation!");
896 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +0000897 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
898 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
899 Tmp5 != Node->getOperand(4)) {
900 std::vector<SDOperand> Ops;
901 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
902 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
903 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
904 }
Chris Lattner3c0dd462005-01-16 07:29:19 +0000905 break;
906 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +0000907 // Otherwise, the target does not support this operation. Lower the
908 // operation to an explicit libcall as appropriate.
909 MVT::ValueType IntPtr = TLI.getPointerTy();
910 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
911 std::vector<std::pair<SDOperand, const Type*> > Args;
912
Reid Spencer6dced922005-01-12 14:53:45 +0000913 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +0000914 if (Node->getOpcode() == ISD::MEMSET) {
915 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
916 // Extend the ubyte argument to be an int value for the call.
917 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
918 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
919 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
920
921 FnName = "memset";
922 } else if (Node->getOpcode() == ISD::MEMCPY ||
923 Node->getOpcode() == ISD::MEMMOVE) {
924 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
925 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
926 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
927 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
928 } else {
929 assert(0 && "Unknown op!");
930 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000931
Chris Lattner85d70c62005-01-11 05:57:22 +0000932 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner111778e2005-05-12 19:56:57 +0000933 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, 0,
Chris Lattner85d70c62005-01-11 05:57:22 +0000934 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
935 Result = LegalizeOp(CallResult.second);
Chris Lattner3c0dd462005-01-16 07:29:19 +0000936 break;
937 }
938 case TargetLowering::Custom:
939 std::vector<SDOperand> Ops;
940 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
941 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
942 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
943 Result = TLI.LowerOperation(Result);
944 Result = LegalizeOp(Result);
945 break;
Chris Lattner85d70c62005-01-11 05:57:22 +0000946 }
947 break;
948 }
Chris Lattner5385db52005-05-09 20:23:03 +0000949
950 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +0000951 Tmp1 = LegalizeOp(Node->getOperand(0));
952 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000953
Chris Lattner5385db52005-05-09 20:23:03 +0000954 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000955 Result = DAG.getNode(ISD::READPORT, Node->getValueType(0), Tmp1, Tmp2);
Chris Lattner5385db52005-05-09 20:23:03 +0000956 else
957 Result = SDOperand(Node, 0);
958 // Since these produce two values, make sure to remember that we legalized
959 // both of them.
960 AddLegalizedOperand(SDOperand(Node, 0), Result);
961 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
962 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +0000963 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +0000964 Tmp1 = LegalizeOp(Node->getOperand(0));
965 Tmp2 = LegalizeOp(Node->getOperand(1));
966 Tmp3 = LegalizeOp(Node->getOperand(2));
967 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
968 Tmp3 != Node->getOperand(2))
969 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
970 break;
971
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000972 case ISD::READIO:
973 Tmp1 = LegalizeOp(Node->getOperand(0));
974 Tmp2 = LegalizeOp(Node->getOperand(1));
975
976 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
977 case TargetLowering::Custom:
978 default: assert(0 && "This action not implemented for this operation!");
979 case TargetLowering::Legal:
980 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
981 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0),
982 Tmp1, Tmp2);
983 else
984 Result = SDOperand(Node, 0);
985 break;
986 case TargetLowering::Expand:
987 // Replace this with a load from memory.
988 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
989 Node->getOperand(1), DAG.getSrcValue(NULL));
990 Result = LegalizeOp(Result);
991 break;
992 }
993
994 // Since these produce two values, make sure to remember that we legalized
995 // both of them.
996 AddLegalizedOperand(SDOperand(Node, 0), Result);
997 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
998 return Result.getValue(Op.ResNo);
999
1000 case ISD::WRITEIO:
1001 Tmp1 = LegalizeOp(Node->getOperand(0));
1002 Tmp2 = LegalizeOp(Node->getOperand(1));
1003 Tmp3 = LegalizeOp(Node->getOperand(2));
1004
1005 switch (TLI.getOperationAction(Node->getOpcode(),
1006 Node->getOperand(1).getValueType())) {
1007 case TargetLowering::Custom:
1008 default: assert(0 && "This action not implemented for this operation!");
1009 case TargetLowering::Legal:
1010 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1011 Tmp3 != Node->getOperand(2))
1012 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1013 break;
1014 case TargetLowering::Expand:
1015 // Replace this with a store to memory.
1016 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1017 Node->getOperand(1), Node->getOperand(2),
1018 DAG.getSrcValue(NULL));
1019 Result = LegalizeOp(Result);
1020 break;
1021 }
1022 break;
1023
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001024 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001025 case ISD::SUB_PARTS:
1026 case ISD::SHL_PARTS:
1027 case ISD::SRA_PARTS:
1028 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001029 std::vector<SDOperand> Ops;
1030 bool Changed = false;
1031 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1032 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1033 Changed |= Ops.back() != Node->getOperand(i);
1034 }
1035 if (Changed)
1036 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
Chris Lattner13fe99c2005-04-02 05:00:07 +00001037
1038 // Since these produce multiple values, make sure to remember that we
1039 // legalized all of them.
1040 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1041 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1042 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001043 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001044
1045 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001046 case ISD::ADD:
1047 case ISD::SUB:
1048 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001049 case ISD::MULHS:
1050 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001051 case ISD::UDIV:
1052 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001053 case ISD::AND:
1054 case ISD::OR:
1055 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001056 case ISD::SHL:
1057 case ISD::SRL:
1058 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001059 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1060 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1061 if (Tmp1 != Node->getOperand(0) ||
1062 Tmp2 != Node->getOperand(1))
1063 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1064 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001065
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001066 case ISD::UREM:
1067 case ISD::SREM:
1068 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1069 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1070 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1071 case TargetLowering::Legal:
1072 if (Tmp1 != Node->getOperand(0) ||
1073 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001074 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001075 Tmp2);
1076 break;
1077 case TargetLowering::Promote:
1078 case TargetLowering::Custom:
1079 assert(0 && "Cannot promote/custom handle this yet!");
1080 case TargetLowering::Expand: {
1081 MVT::ValueType VT = Node->getValueType(0);
1082 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1083 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1084 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1085 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1086 }
1087 break;
1088 }
1089 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001090
Andrew Lenharth5e177822005-05-03 17:19:30 +00001091 case ISD::CTPOP:
1092 case ISD::CTTZ:
1093 case ISD::CTLZ:
1094 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1095 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1096 case TargetLowering::Legal:
1097 if (Tmp1 != Node->getOperand(0))
1098 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1099 break;
1100 case TargetLowering::Promote: {
1101 MVT::ValueType OVT = Tmp1.getValueType();
1102 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001103
1104 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001105 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1106 // Perform the larger operation, then subtract if needed.
1107 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1108 switch(Node->getOpcode())
1109 {
1110 case ISD::CTPOP:
1111 Result = Tmp1;
1112 break;
1113 case ISD::CTTZ:
1114 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnercf5f6b02005-05-12 19:05:01 +00001115 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001116 DAG.getConstant(getSizeInBits(NVT), NVT));
1117 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1118 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1119 break;
1120 case ISD::CTLZ:
1121 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1122 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1123 DAG.getConstant(getSizeInBits(NVT) -
1124 getSizeInBits(OVT), NVT));
1125 break;
1126 }
1127 break;
1128 }
1129 case TargetLowering::Custom:
1130 assert(0 && "Cannot custom handle this yet!");
1131 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001132 switch(Node->getOpcode())
1133 {
1134 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001135 static const uint64_t mask[6] = {
1136 0x5555555555555555ULL, 0x3333333333333333ULL,
1137 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1138 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1139 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001140 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001141 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1142 unsigned len = getSizeInBits(VT);
1143 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001144 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001145 Tmp2 = DAG.getConstant(mask[i], VT);
1146 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001147 Tmp1 = DAG.getNode(ISD::ADD, VT,
1148 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1149 DAG.getNode(ISD::AND, VT,
1150 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1151 Tmp2));
1152 }
1153 Result = Tmp1;
1154 break;
1155 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001156 case ISD::CTLZ: {
1157 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001158 x = x | (x >> 1);
1159 x = x | (x >> 2);
1160 ...
1161 x = x | (x >>16);
1162 x = x | (x >>32); // for 64-bit input
1163 return popcount(~x);
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001164
Chris Lattner56add052005-05-11 18:35:21 +00001165 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1166 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001167 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1168 unsigned len = getSizeInBits(VT);
1169 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1170 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
1171 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
1172 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1173 }
1174 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001175 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001176 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001177 }
1178 case ISD::CTTZ: {
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001179 // for now, we use: { return popcount(~x & (x - 1)); }
1180 // unless the target has ctlz but not ctpop, in which case we use:
1181 // { return 32 - nlz(~x & (x-1)); }
1182 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001183 MVT::ValueType VT = Tmp1.getValueType();
1184 Tmp2 = DAG.getConstant(~0ULL, VT);
1185 Tmp3 = DAG.getNode(ISD::AND, VT,
1186 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1187 DAG.getNode(ISD::SUB, VT, Tmp1,
1188 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001189 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1190 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1191 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
1192 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
1193 DAG.getConstant(getSizeInBits(VT), VT),
1194 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1195 } else {
1196 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1197 }
Chris Lattner72473242005-05-11 05:27:09 +00001198 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001199 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001200 default:
1201 assert(0 && "Cannot expand this yet!");
1202 break;
1203 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001204 break;
1205 }
1206 break;
1207
Chris Lattner13fe99c2005-04-02 05:00:07 +00001208 // Unary operators
1209 case ISD::FABS:
1210 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001211 case ISD::FSQRT:
1212 case ISD::FSIN:
1213 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001214 Tmp1 = LegalizeOp(Node->getOperand(0));
1215 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1216 case TargetLowering::Legal:
1217 if (Tmp1 != Node->getOperand(0))
1218 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1219 break;
1220 case TargetLowering::Promote:
1221 case TargetLowering::Custom:
1222 assert(0 && "Cannot promote/custom handle this yet!");
1223 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001224 switch(Node->getOpcode()) {
1225 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001226 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1227 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1228 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1229 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001230 break;
1231 }
1232 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001233 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1234 MVT::ValueType VT = Node->getValueType(0);
1235 Tmp2 = DAG.getConstantFP(0.0, VT);
1236 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1237 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1238 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1239 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001240 break;
1241 }
1242 case ISD::FSQRT:
1243 case ISD::FSIN:
1244 case ISD::FCOS: {
1245 MVT::ValueType VT = Node->getValueType(0);
1246 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1247 const char *FnName = 0;
1248 switch(Node->getOpcode()) {
1249 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1250 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1251 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1252 default: assert(0 && "Unreachable!");
1253 }
1254 std::vector<std::pair<SDOperand, const Type*> > Args;
1255 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner06bbeb62005-05-11 19:02:11 +00001256 // FIXME: should use ExpandLibCall!
Chris Lattner80026402005-04-30 04:43:14 +00001257 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner111778e2005-05-12 19:56:57 +00001258 TLI.LowerCallTo(DAG.getEntryNode(), T, false, 0,
Chris Lattner80026402005-04-30 04:43:14 +00001259 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1260 Result = LegalizeOp(CallResult.first);
1261 break;
1262 }
1263 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001264 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001265 }
1266 break;
1267 }
1268 break;
1269
1270 // Conversion operators. The source and destination have different types.
Chris Lattnerdc750592005-01-07 07:47:09 +00001271 case ISD::ZERO_EXTEND:
1272 case ISD::SIGN_EXTEND:
Chris Lattner19a83992005-01-07 21:56:57 +00001273 case ISD::TRUNCATE:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001274 case ISD::FP_EXTEND:
1275 case ISD::FP_ROUND:
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001276 case ISD::FP_TO_SINT:
1277 case ISD::FP_TO_UINT:
1278 case ISD::SINT_TO_FP:
1279 case ISD::UINT_TO_FP:
Chris Lattnerdc750592005-01-07 07:47:09 +00001280 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1281 case Legal:
1282 Tmp1 = LegalizeOp(Node->getOperand(0));
1283 if (Tmp1 != Node->getOperand(0))
1284 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1285 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001286 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001287 if (Node->getOpcode() == ISD::SINT_TO_FP ||
1288 Node->getOpcode() == ISD::UINT_TO_FP) {
1289 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1290 Node->getValueType(0), Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001291 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001292 } else if (Node->getOpcode() == ISD::TRUNCATE) {
1293 // In the expand case, we must be dealing with a truncate, because
1294 // otherwise the result would be larger than the source.
1295 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +00001296
Chris Lattner13fe99c2005-04-02 05:00:07 +00001297 // Since the result is legal, we should just be able to truncate the low
1298 // part of the source.
1299 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1300 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00001301 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001302 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001303
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001304 case Promote:
1305 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001306 case ISD::ZERO_EXTEND:
1307 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001308 // NOTE: Any extend would work here...
1309 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001310 Result = DAG.getZeroExtendInReg(Result,
1311 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001312 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001313 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001314 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001315 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001316 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001317 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1318 Result, Node->getOperand(0).getValueType());
1319 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001320 case ISD::TRUNCATE:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001321 Result = PromoteOp(Node->getOperand(0));
1322 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1323 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001324 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001325 Result = PromoteOp(Node->getOperand(0));
1326 if (Result.getValueType() != Op.getValueType())
1327 // Dynamically dead while we have only 2 FP types.
1328 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1329 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001330 case ISD::FP_ROUND:
1331 case ISD::FP_TO_SINT:
1332 case ISD::FP_TO_UINT:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001333 Result = PromoteOp(Node->getOperand(0));
1334 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1335 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001336 case ISD::SINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001337 Result = PromoteOp(Node->getOperand(0));
1338 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1339 Result, Node->getOperand(0).getValueType());
1340 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1341 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001342 case ISD::UINT_TO_FP:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001343 Result = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001344 Result = DAG.getZeroExtendInReg(Result,
1345 Node->getOperand(0).getValueType());
Chris Lattner3ba56b32005-01-16 05:06:12 +00001346 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
1347 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001348 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001349 }
1350 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001351 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001352 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001353 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner99222f72005-01-15 07:15:18 +00001354 MVT::ValueType ExtraVT = cast<MVTSDNode>(Node)->getExtraValueType();
1355
1356 // If this operation is not supported, convert it to a shl/shr or load/store
1357 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001358 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1359 default: assert(0 && "This action not supported for this op yet!");
1360 case TargetLowering::Legal:
1361 if (Tmp1 != Node->getOperand(0))
1362 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
1363 ExtraVT);
1364 break;
1365 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001366 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001367 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001368 // NOTE: we could fall back on load/store here too for targets without
1369 // SAR. However, it is doubtful that any exist.
1370 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1371 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001372 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001373 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1374 Node->getOperand(0), ShiftCst);
1375 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1376 Result, ShiftCst);
1377 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1378 // The only way we can lower this is to turn it into a STORETRUNC,
1379 // EXTLOAD pair, targetting a temporary location (a stack slot).
1380
1381 // NOTE: there is a choice here between constantly creating new stack
1382 // slots and always reusing the same one. We currently always create
1383 // new ones, as reuse may inhibit scheduling.
1384 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1385 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1386 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1387 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001388 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001389 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1390 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1391 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001392 Node->getOperand(0), StackSlot,
1393 DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001394 Result = DAG.getNode(ISD::EXTLOAD, Node->getValueType(0),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001395 Result, StackSlot, DAG.getSrcValue(NULL), ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001396 } else {
1397 assert(0 && "Unknown op");
1398 }
1399 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001400 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001401 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001402 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001403 }
Chris Lattner99222f72005-01-15 07:15:18 +00001404 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001405
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001406 // Note that LegalizeOp may be reentered even from single-use nodes, which
1407 // means that we always must cache transformed nodes.
1408 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001409 return Result;
1410}
1411
Chris Lattner4d978642005-01-15 22:16:26 +00001412/// PromoteOp - Given an operation that produces a value in an invalid type,
1413/// promote it to compute the value into a larger type. The produced value will
1414/// have the correct bits for the low portion of the register, but no guarantee
1415/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001416SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1417 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001418 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001419 assert(getTypeAction(VT) == Promote &&
1420 "Caller should expand or legalize operands that are not promotable!");
1421 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1422 "Cannot promote to smaller type!");
1423
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001424 SDOperand Tmp1, Tmp2, Tmp3;
1425
1426 SDOperand Result;
1427 SDNode *Node = Op.Val;
1428
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001429 if (!Node->hasOneUse()) {
1430 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1431 if (I != PromotedNodes.end()) return I->second;
1432 } else {
1433 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1434 }
1435
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001436 // Promotion needs an optimization step to clean up after it, and is not
1437 // careful to avoid operations the target does not support. Make sure that
1438 // all generated operations are legalized in the next iteration.
1439 NeedsAnotherIteration = true;
1440
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001441 switch (Node->getOpcode()) {
1442 default:
1443 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1444 assert(0 && "Do not know how to promote this operator!");
1445 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001446 case ISD::UNDEF:
1447 Result = DAG.getNode(ISD::UNDEF, NVT);
1448 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001449 case ISD::Constant:
1450 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1451 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1452 break;
1453 case ISD::ConstantFP:
1454 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1455 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1456 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001457 case ISD::CopyFromReg:
1458 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1459 Node->getOperand(0));
1460 // Remember that we legalized the chain.
1461 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1462 break;
1463
Chris Lattner2cb338d2005-01-18 02:59:52 +00001464 case ISD::SETCC:
1465 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1466 "SetCC type is not legal??");
1467 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1468 TLI.getSetCCResultTy(), Node->getOperand(0),
1469 Node->getOperand(1));
1470 Result = LegalizeOp(Result);
1471 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001472
1473 case ISD::TRUNCATE:
1474 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1475 case Legal:
1476 Result = LegalizeOp(Node->getOperand(0));
1477 assert(Result.getValueType() >= NVT &&
1478 "This truncation doesn't make sense!");
1479 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1480 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1481 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001482 case Promote:
1483 // The truncation is not required, because we don't guarantee anything
1484 // about high bits anyway.
1485 Result = PromoteOp(Node->getOperand(0));
1486 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001487 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001488 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1489 // Truncate the low part of the expanded value to the result type
Misha Brukman835702a2005-04-21 22:36:52 +00001490 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001491 }
1492 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001493 case ISD::SIGN_EXTEND:
1494 case ISD::ZERO_EXTEND:
1495 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1496 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1497 case Legal:
1498 // Input is legal? Just do extend all the way to the larger type.
1499 Result = LegalizeOp(Node->getOperand(0));
1500 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1501 break;
1502 case Promote:
1503 // Promote the reg if it's smaller.
1504 Result = PromoteOp(Node->getOperand(0));
1505 // The high bits are not guaranteed to be anything. Insert an extend.
1506 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001507 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
1508 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001509 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001510 Result = DAG.getZeroExtendInReg(Result,
1511 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001512 break;
1513 }
1514 break;
1515
1516 case ISD::FP_EXTEND:
1517 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1518 case ISD::FP_ROUND:
1519 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1520 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1521 case Promote: assert(0 && "Unreachable with 2 FP types!");
1522 case Legal:
1523 // Input is legal? Do an FP_ROUND_INREG.
1524 Result = LegalizeOp(Node->getOperand(0));
1525 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1526 break;
1527 }
1528 break;
1529
1530 case ISD::SINT_TO_FP:
1531 case ISD::UINT_TO_FP:
1532 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1533 case Legal:
1534 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001535 // No extra round required here.
1536 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001537 break;
1538
1539 case Promote:
1540 Result = PromoteOp(Node->getOperand(0));
1541 if (Node->getOpcode() == ISD::SINT_TO_FP)
1542 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1543 Result, Node->getOperand(0).getValueType());
1544 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001545 Result = DAG.getZeroExtendInReg(Result,
1546 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001547 // No extra round required here.
1548 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001549 break;
1550 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001551 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1552 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001553 // Round if we cannot tolerate excess precision.
1554 if (NoExcessFPPrecision)
1555 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1556 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001557 }
Chris Lattner4d978642005-01-15 22:16:26 +00001558 break;
1559
1560 case ISD::FP_TO_SINT:
1561 case ISD::FP_TO_UINT:
1562 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1563 case Legal:
1564 Tmp1 = LegalizeOp(Node->getOperand(0));
1565 break;
1566 case Promote:
1567 // The input result is prerounded, so we don't have to do anything
1568 // special.
1569 Tmp1 = PromoteOp(Node->getOperand(0));
1570 break;
1571 case Expand:
1572 assert(0 && "not implemented");
1573 }
1574 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1575 break;
1576
Chris Lattner13fe99c2005-04-02 05:00:07 +00001577 case ISD::FABS:
1578 case ISD::FNEG:
1579 Tmp1 = PromoteOp(Node->getOperand(0));
1580 assert(Tmp1.getValueType() == NVT);
1581 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1582 // NOTE: we do not have to do any extra rounding here for
1583 // NoExcessFPPrecision, because we know the input will have the appropriate
1584 // precision, and these operations don't modify precision at all.
1585 break;
1586
Chris Lattner9d6fa982005-04-28 21:44:33 +00001587 case ISD::FSQRT:
1588 case ISD::FSIN:
1589 case ISD::FCOS:
1590 Tmp1 = PromoteOp(Node->getOperand(0));
1591 assert(Tmp1.getValueType() == NVT);
1592 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1593 if(NoExcessFPPrecision)
1594 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1595 break;
1596
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001597 case ISD::AND:
1598 case ISD::OR:
1599 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001600 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001601 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001602 case ISD::MUL:
1603 // The input may have strange things in the top bits of the registers, but
1604 // these operations don't care. They may have wierd bits going out, but
1605 // that too is okay if they are integer operations.
1606 Tmp1 = PromoteOp(Node->getOperand(0));
1607 Tmp2 = PromoteOp(Node->getOperand(1));
1608 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1609 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1610
1611 // However, if this is a floating point operation, they will give excess
1612 // precision that we may not be able to tolerate. If we DO allow excess
1613 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001614 // FIXME: Why would we need to round FP ops more than integer ones?
1615 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001616 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1617 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1618 break;
1619
Chris Lattner4d978642005-01-15 22:16:26 +00001620 case ISD::SDIV:
1621 case ISD::SREM:
1622 // These operators require that their input be sign extended.
1623 Tmp1 = PromoteOp(Node->getOperand(0));
1624 Tmp2 = PromoteOp(Node->getOperand(1));
1625 if (MVT::isInteger(NVT)) {
1626 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
Chris Lattner207a9622005-01-16 00:17:42 +00001627 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001628 }
1629 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1630
1631 // Perform FP_ROUND: this is probably overly pessimistic.
1632 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
1633 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result, VT);
1634 break;
1635
1636 case ISD::UDIV:
1637 case ISD::UREM:
1638 // These operators require that their input be zero extended.
1639 Tmp1 = PromoteOp(Node->getOperand(0));
1640 Tmp2 = PromoteOp(Node->getOperand(1));
1641 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00001642 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1643 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001644 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1645 break;
1646
1647 case ISD::SHL:
1648 Tmp1 = PromoteOp(Node->getOperand(0));
1649 Tmp2 = LegalizeOp(Node->getOperand(1));
1650 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1651 break;
1652 case ISD::SRA:
1653 // The input value must be properly sign extended.
1654 Tmp1 = PromoteOp(Node->getOperand(0));
1655 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1, VT);
1656 Tmp2 = LegalizeOp(Node->getOperand(1));
1657 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1658 break;
1659 case ISD::SRL:
1660 // The input value must be properly zero extended.
1661 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001662 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001663 Tmp2 = LegalizeOp(Node->getOperand(1));
1664 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1665 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001666 case ISD::LOAD:
1667 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1668 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001669 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001670 if (MVT::isInteger(NVT))
Chris Lattner5385db52005-05-09 20:23:03 +00001671 Result = DAG.getNode(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1672 VT);
Chris Lattner391a3512005-04-10 17:40:35 +00001673 else
Chris Lattner5385db52005-05-09 20:23:03 +00001674 Result = DAG.getNode(ISD::EXTLOAD, NVT, Tmp1, Tmp2, Node->getOperand(2),
1675 VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001676
1677 // Remember that we legalized the chain.
1678 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1679 break;
1680 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001681 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1682 case Expand: assert(0 && "It's impossible to expand bools");
1683 case Legal:
1684 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1685 break;
1686 case Promote:
1687 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1688 break;
1689 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001690 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1691 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1692 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1693 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00001694 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001695 case ISD::CALL: {
1696 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1697 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1698
Chris Lattner3d95c142005-01-19 20:24:35 +00001699 std::vector<SDOperand> Ops;
1700 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1701 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1702
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001703 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1704 "Can only promote single result calls");
1705 std::vector<MVT::ValueType> RetTyVTs;
1706 RetTyVTs.reserve(2);
1707 RetTyVTs.push_back(NVT);
1708 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00001709 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
1710 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001711 Result = SDOperand(NC, 0);
1712
1713 // Insert the new chain mapping.
1714 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1715 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001716 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00001717 case ISD::CTPOP:
1718 case ISD::CTTZ:
1719 case ISD::CTLZ:
1720 Tmp1 = Node->getOperand(0);
1721 //Zero extend the argument
1722 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1723 // Perform the larger operation, then subtract if needed.
1724 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1725 switch(Node->getOpcode())
1726 {
1727 case ISD::CTPOP:
1728 Result = Tmp1;
1729 break;
1730 case ISD::CTTZ:
1731 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
1732 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
1733 DAG.getConstant(getSizeInBits(NVT), NVT));
1734 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
1735 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1736 break;
1737 case ISD::CTLZ:
1738 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
1739 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1740 DAG.getConstant(getSizeInBits(NVT) -
1741 getSizeInBits(VT), NVT));
1742 break;
1743 }
1744 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001745 }
1746
1747 assert(Result.Val && "Didn't set a result!");
1748 AddPromotedOperand(Op, Result);
1749 return Result;
1750}
Chris Lattnerdc750592005-01-07 07:47:09 +00001751
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001752/// ExpandAddSub - Find a clever way to expand this add operation into
1753/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001754void SelectionDAGLegalize::
1755ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1756 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001757 // Expand the subcomponents.
1758 SDOperand LHSL, LHSH, RHSL, RHSH;
1759 ExpandOp(LHS, LHSL, LHSH);
1760 ExpandOp(RHS, RHSL, RHSH);
1761
Chris Lattner8ffd0042005-04-11 20:29:59 +00001762 // FIXME: this should be moved to the dag combiner someday.
1763 if (NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS)
1764 if (LHSL.getValueType() == MVT::i32) {
1765 SDOperand LowEl;
1766 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1767 if (C->getValue() == 0)
1768 LowEl = RHSL;
1769 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1770 if (C->getValue() == 0)
1771 LowEl = LHSL;
1772 if (LowEl.Val) {
1773 // Turn this into an add/sub of the high part only.
1774 SDOperand HiEl =
1775 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
1776 LowEl.getValueType(), LHSH, RHSH);
1777 Lo = LowEl;
1778 Hi = HiEl;
1779 return;
1780 }
1781 }
1782
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001783 std::vector<SDOperand> Ops;
1784 Ops.push_back(LHSL);
1785 Ops.push_back(LHSH);
1786 Ops.push_back(RHSL);
1787 Ops.push_back(RHSH);
Chris Lattner2e5872c2005-04-02 03:38:53 +00001788 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001789 Hi = Lo.getValue(1);
1790}
1791
Chris Lattner4157c412005-04-02 04:00:59 +00001792void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
1793 SDOperand Op, SDOperand Amt,
1794 SDOperand &Lo, SDOperand &Hi) {
1795 // Expand the subcomponents.
1796 SDOperand LHSL, LHSH;
1797 ExpandOp(Op, LHSL, LHSH);
1798
1799 std::vector<SDOperand> Ops;
1800 Ops.push_back(LHSL);
1801 Ops.push_back(LHSH);
1802 Ops.push_back(Amt);
1803 Lo = DAG.getNode(NodeOp, LHSL.getValueType(), Ops);
1804 Hi = Lo.getValue(1);
1805}
1806
1807
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001808/// ExpandShift - Try to find a clever way to expand this shift operation out to
1809/// smaller elements. If we can't find a way that is more efficient than a
1810/// libcall on this target, return false. Otherwise, return true with the
1811/// low-parts expanded into Lo and Hi.
1812bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
1813 SDOperand &Lo, SDOperand &Hi) {
1814 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
1815 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00001816
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001817 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00001818 SDOperand ShAmt = LegalizeOp(Amt);
1819 MVT::ValueType ShTy = ShAmt.getValueType();
1820 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
1821 unsigned NVTBits = MVT::getSizeInBits(NVT);
1822
1823 // Handle the case when Amt is an immediate. Other cases are currently broken
1824 // and are disabled.
1825 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
1826 unsigned Cst = CN->getValue();
1827 // Expand the incoming operand to be shifted, so that we have its parts
1828 SDOperand InL, InH;
1829 ExpandOp(Op, InL, InH);
1830 switch(Opc) {
1831 case ISD::SHL:
1832 if (Cst > VTBits) {
1833 Lo = DAG.getConstant(0, NVT);
1834 Hi = DAG.getConstant(0, NVT);
1835 } else if (Cst > NVTBits) {
1836 Lo = DAG.getConstant(0, NVT);
1837 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001838 } else if (Cst == NVTBits) {
1839 Lo = DAG.getConstant(0, NVT);
1840 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00001841 } else {
1842 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
1843 Hi = DAG.getNode(ISD::OR, NVT,
1844 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
1845 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
1846 }
1847 return true;
1848 case ISD::SRL:
1849 if (Cst > VTBits) {
1850 Lo = DAG.getConstant(0, NVT);
1851 Hi = DAG.getConstant(0, NVT);
1852 } else if (Cst > NVTBits) {
1853 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
1854 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00001855 } else if (Cst == NVTBits) {
1856 Lo = InH;
1857 Hi = DAG.getConstant(0, NVT);
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::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
1863 }
1864 return true;
1865 case ISD::SRA:
1866 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001867 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001868 DAG.getConstant(NVTBits-1, ShTy));
1869 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00001870 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001871 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00001872 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00001873 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00001874 } else if (Cst == NVTBits) {
1875 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00001876 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00001877 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00001878 } else {
1879 Lo = DAG.getNode(ISD::OR, NVT,
1880 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
1881 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
1882 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
1883 }
1884 return true;
1885 }
1886 }
1887 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
1888 // so disable it for now. Currently targets are handling this via SHL_PARTS
1889 // and friends.
1890 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001891
1892 // If we have an efficient select operation (or if the selects will all fold
1893 // away), lower to some complex code, otherwise just emit the libcall.
1894 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
1895 !isa<ConstantSDNode>(Amt))
1896 return false;
1897
1898 SDOperand InL, InH;
1899 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001900 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
1901 DAG.getConstant(NVTBits, ShTy), ShAmt);
1902
Chris Lattner4d25c042005-01-20 20:29:23 +00001903 // Compare the unmasked shift amount against 32.
1904 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
1905 DAG.getConstant(NVTBits, ShTy));
1906
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001907 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
1908 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
1909 DAG.getConstant(NVTBits-1, ShTy));
1910 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
1911 DAG.getConstant(NVTBits-1, ShTy));
1912 }
1913
1914 if (Opc == ISD::SHL) {
1915 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
1916 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
1917 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001918 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00001919
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001920 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
1921 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
1922 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00001923 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
1924 DAG.getSetCC(ISD::SETEQ,
1925 TLI.getSetCCResultTy(), NAmt,
1926 DAG.getConstant(32, ShTy)),
1927 DAG.getConstant(0, NVT),
1928 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001929 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00001930 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001931 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00001932 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001933
1934 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00001935 if (Opc == ISD::SRA)
1936 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
1937 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001938 else
1939 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001940 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00001941 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00001942 }
1943 return true;
1944}
Chris Lattneraac464e2005-01-21 06:05:23 +00001945
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001946/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
1947/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00001948/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001949static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001950 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
1951
Chris Lattner2dce7032005-05-12 23:24:06 +00001952 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00001953 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00001954 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001955 Found = Node;
1956 return;
1957 }
1958
1959 // Otherwise, scan the operands of Node to see if any of them is a call.
1960 assert(Node->getNumOperands() != 0 &&
1961 "All leaves should have depth equal to the entry node!");
1962 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001963 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00001964
1965 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001966 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00001967 Found);
1968}
1969
1970
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001971/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
1972/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00001973/// than Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001974static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001975 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
1976
Chris Lattner2dce7032005-05-12 23:24:06 +00001977 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00001978 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00001979 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00001980 Found = Node;
1981 return;
1982 }
1983
1984 // Otherwise, scan the operands of Node to see if any of them is a call.
1985 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
1986 if (UI == E) return;
1987 for (--E; UI != E; ++UI)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001988 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00001989
1990 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001991 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00001992}
1993
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001994/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00001995/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00001996static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00001997 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00001998 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00001999 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002000 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002001
2002 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002003 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002004
Chris Lattner4add7e32005-01-23 04:42:50 +00002005 SDOperand TheChain(Node, Node->getNumValues()-1);
2006 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002007
2008 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner4add7e32005-01-23 04:42:50 +00002009 E = Node->use_end(); ; ++UI) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002010 assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!");
Misha Brukman835702a2005-04-21 22:36:52 +00002011
Chris Lattner4add7e32005-01-23 04:42:50 +00002012 // Make sure to only follow users of our token chain.
2013 SDNode *User = *UI;
2014 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2015 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002016 if (SDNode *Result = FindCallSeqEnd(User))
2017 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002018 }
2019 assert(0 && "Unreachable");
2020 abort();
2021}
2022
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002023/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002024/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002025static SDNode *FindCallSeqStart(SDNode *Node) {
2026 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002027 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002028
2029 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2030 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002031 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002032}
2033
2034
Chris Lattner4add7e32005-01-23 04:42:50 +00002035/// FindInputOutputChains - If we are replacing an operation with a call we need
2036/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002037/// properly serialize the calls in the block. The returned operand is the
2038/// input chain value for the new call (e.g. the entry node or the previous
2039/// call), and OutChain is set to be the chain node to update to point to the
2040/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002041static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2042 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002043 SDNode *LatestCallSeqStart = Entry.Val;
2044 SDNode *LatestCallSeqEnd = 0;
2045 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2046 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002047
Chris Lattner2dce7032005-05-12 23:24:06 +00002048 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002049 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002050 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2051 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002052 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2053 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002054 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002055 LatestCallSeqEnd = Entry.Val;
2056 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002057
Chris Lattner06bbeb62005-05-11 19:02:11 +00002058 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002059 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002060 OutChain = 0;
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002061 FindEarliestCallSeqEnd(OpNode, OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002062
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002063 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002064 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002065 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002066
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002067 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002068}
2069
Chris Lattner06bbeb62005-05-11 19:02:11 +00002070/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002071void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2072 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002073 // Nothing to splice it into?
2074 if (OutChain == 0) return;
2075
2076 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2077 //OutChain->dump();
2078
2079 // Form a token factor node merging the old inval and the new inval.
2080 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2081 OutChain->getOperand(0));
2082 // Change the node to refer to the new token.
2083 OutChain->setAdjCallChain(InToken);
2084}
Chris Lattner4add7e32005-01-23 04:42:50 +00002085
2086
Chris Lattneraac464e2005-01-21 06:05:23 +00002087// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2088// does not fit into a register, return the lo part and set the hi part to the
2089// by-reg argument. If it does fit into a single register, return the result
2090// and leave the Hi part unset.
2091SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2092 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002093 SDNode *OutChain;
2094 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2095 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002096 if (InChain.Val == 0)
2097 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002098
Chris Lattneraac464e2005-01-21 06:05:23 +00002099 TargetLowering::ArgListTy Args;
2100 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2101 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2102 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2103 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2104 }
2105 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002106
Chris Lattner06bbeb62005-05-11 19:02:11 +00002107 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002108 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002109 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner111778e2005-05-12 19:56:57 +00002110 TLI.LowerCallTo(InChain, RetTy, false, 0, Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002111 SpliceCallInto(CallInfo.second, OutChain);
2112
2113 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002114
2115 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002116 default: assert(0 && "Unknown thing");
2117 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002118 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002119 case Promote:
2120 assert(0 && "Cannot promote this yet!");
2121 case Expand:
2122 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002123 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002124 return Lo;
2125 }
2126}
2127
Chris Lattner4add7e32005-01-23 04:42:50 +00002128
Chris Lattneraac464e2005-01-21 06:05:23 +00002129/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2130/// destination type is legal.
2131SDOperand SelectionDAGLegalize::
2132ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2133 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2134 assert(getTypeAction(Source.getValueType()) == Expand &&
2135 "This is not an expansion!");
2136 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2137
Chris Lattner06bbeb62005-05-11 19:02:11 +00002138 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002139 assert(Source.getValueType() == MVT::i64 &&
2140 "This only works for 64-bit -> FP");
2141 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2142 // incoming integer is set. To handle this, we dynamically test to see if
2143 // it is set, and, if so, add a fudge factor.
2144 SDOperand Lo, Hi;
2145 ExpandOp(Source, Lo, Hi);
2146
Chris Lattner2a4f7312005-05-13 04:45:13 +00002147 // If this is unsigned, and not supported, first perform the conversion to
2148 // signed, then adjust the result if the sign bit is set.
2149 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2150 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2151
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002152 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2153 DAG.getConstant(0, Hi.getValueType()));
2154 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2155 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2156 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002157 uint64_t FF = 0x5f800000ULL;
2158 if (TLI.isLittleEndian()) FF <<= 32;
2159 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002160
2161 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2162 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2163 TLI.getPointerTy());
2164 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2165 SDOperand FudgeInReg;
2166 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002167 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2168 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002169 else {
2170 assert(DestTy == MVT::f64 && "Unexpected conversion");
2171 FudgeInReg = DAG.getNode(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002172 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002173 }
2174 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002175 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002176
Chris Lattner153587e2005-05-12 07:00:44 +00002177 // Expand the source, then glue it back together for the call. We must expand
2178 // the source in case it is shared (this pass of legalize must traverse it).
2179 SDOperand SrcLo, SrcHi;
2180 ExpandOp(Source, SrcLo, SrcHi);
2181 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2182
Chris Lattner06bbeb62005-05-11 19:02:11 +00002183 SDNode *OutChain = 0;
2184 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2185 DAG.getEntryNode());
2186 const char *FnName = 0;
2187 if (DestTy == MVT::f32)
2188 FnName = "__floatdisf";
2189 else {
2190 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2191 FnName = "__floatdidf";
2192 }
2193
Chris Lattneraac464e2005-01-21 06:05:23 +00002194 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2195
2196 TargetLowering::ArgListTy Args;
2197 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002198
Chris Lattneraac464e2005-01-21 06:05:23 +00002199 Args.push_back(std::make_pair(Source, ArgTy));
2200
2201 // We don't care about token chains for libcalls. We just use the entry
2202 // node as our input and ignore the output chain. This allows us to place
2203 // calls wherever we need them to satisfy data dependences.
2204 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002205
2206 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner111778e2005-05-12 19:56:57 +00002207 TLI.LowerCallTo(InChain, RetTy, false, 0, Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002208
Chris Lattnera5bf1032005-05-12 04:49:08 +00002209 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002210 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002211}
Misha Brukman835702a2005-04-21 22:36:52 +00002212
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002213
2214
Chris Lattnerdc750592005-01-07 07:47:09 +00002215/// ExpandOp - Expand the specified SDOperand into its two component pieces
2216/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2217/// LegalizeNodes map is filled in for any results that are not expanded, the
2218/// ExpandedNodes map is filled in for any results that are expanded, and the
2219/// Lo/Hi values are returned.
2220void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2221 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002222 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002223 SDNode *Node = Op.Val;
2224 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2225 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2226 assert(MVT::isInteger(NVT) && NVT < VT &&
2227 "Cannot expand to FP value or to larger int value!");
2228
2229 // If there is more than one use of this, see if we already expanded it.
2230 // There is no use remembering values that only have a single use, as the map
2231 // entries will never be reused.
2232 if (!Node->hasOneUse()) {
2233 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2234 = ExpandedNodes.find(Op);
2235 if (I != ExpandedNodes.end()) {
2236 Lo = I->second.first;
2237 Hi = I->second.second;
2238 return;
2239 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002240 } else {
2241 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002242 }
2243
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002244 // Expanding to multiple registers needs to perform an optimization step, and
2245 // is not careful to avoid operations the target does not support. Make sure
2246 // that all generated operations are legalized in the next iteration.
2247 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002248
Chris Lattnerdc750592005-01-07 07:47:09 +00002249 switch (Node->getOpcode()) {
2250 default:
2251 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2252 assert(0 && "Do not know how to expand this operator!");
2253 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002254 case ISD::UNDEF:
2255 Lo = DAG.getNode(ISD::UNDEF, NVT);
2256 Hi = DAG.getNode(ISD::UNDEF, NVT);
2257 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002258 case ISD::Constant: {
2259 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2260 Lo = DAG.getConstant(Cst, NVT);
2261 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2262 break;
2263 }
2264
2265 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00002266 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00002267 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00002268 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2269 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002270
Chris Lattner3b8e7192005-01-14 22:38:01 +00002271 // Remember that we legalized the chain.
2272 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2273
Chris Lattnerdc750592005-01-07 07:47:09 +00002274 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2275 break;
2276 }
2277
Chris Lattner32e08b72005-03-28 22:03:13 +00002278 case ISD::BUILD_PAIR:
2279 // Legalize both operands. FIXME: in the future we should handle the case
2280 // where the two elements are not legal.
2281 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2282 Lo = LegalizeOp(Node->getOperand(0));
2283 Hi = LegalizeOp(Node->getOperand(1));
2284 break;
2285
Chris Lattner55e9cde2005-05-11 04:51:16 +00002286 case ISD::CTPOP:
2287 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002288 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2289 DAG.getNode(ISD::CTPOP, NVT, Lo),
2290 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002291 Hi = DAG.getConstant(0, NVT);
2292 break;
2293
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002294 case ISD::CTLZ: {
2295 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002296 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002297 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2298 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2299 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2300 HLZ, BitsC);
2301 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2302 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2303
2304 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2305 Hi = DAG.getConstant(0, NVT);
2306 break;
2307 }
2308
2309 case ISD::CTTZ: {
2310 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002311 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002312 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2313 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2314 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2315 LTZ, BitsC);
2316 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2317 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2318
2319 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2320 Hi = DAG.getConstant(0, NVT);
2321 break;
2322 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002323
Chris Lattnerdc750592005-01-07 07:47:09 +00002324 case ISD::LOAD: {
2325 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2326 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002327 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002328
2329 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002330 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002331 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2332 getIntPtrConstant(IncrementSize));
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002333 //Is this safe? declaring that the two parts of the split load
2334 //are from the same instruction?
2335 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002336
2337 // Build a factor node to remember that this load is independent of the
2338 // other one.
2339 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2340 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002341
Chris Lattnerdc750592005-01-07 07:47:09 +00002342 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002343 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002344 if (!TLI.isLittleEndian())
2345 std::swap(Lo, Hi);
2346 break;
2347 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002348 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002349 case ISD::CALL: {
2350 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2351 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2352
Chris Lattner3d95c142005-01-19 20:24:35 +00002353 bool Changed = false;
2354 std::vector<SDOperand> Ops;
2355 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2356 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2357 Changed |= Ops.back() != Node->getOperand(i);
2358 }
2359
Chris Lattnerdc750592005-01-07 07:47:09 +00002360 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2361 "Can only expand a call once so far, not i64 -> i16!");
2362
2363 std::vector<MVT::ValueType> RetTyVTs;
2364 RetTyVTs.reserve(3);
2365 RetTyVTs.push_back(NVT);
2366 RetTyVTs.push_back(NVT);
2367 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002368 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2369 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002370 Lo = SDOperand(NC, 0);
2371 Hi = SDOperand(NC, 1);
2372
2373 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002374 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002375 break;
2376 }
2377 case ISD::AND:
2378 case ISD::OR:
2379 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2380 SDOperand LL, LH, RL, RH;
2381 ExpandOp(Node->getOperand(0), LL, LH);
2382 ExpandOp(Node->getOperand(1), RL, RH);
2383 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2384 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2385 break;
2386 }
2387 case ISD::SELECT: {
2388 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002389
2390 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2391 case Expand: assert(0 && "It's impossible to expand bools");
2392 case Legal:
2393 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2394 break;
2395 case Promote:
2396 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2397 break;
2398 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002399 ExpandOp(Node->getOperand(1), LL, LH);
2400 ExpandOp(Node->getOperand(2), RL, RH);
2401 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2402 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2403 break;
2404 }
2405 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002406 SDOperand In;
2407 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2408 case Expand: assert(0 && "expand-expand not implemented yet!");
2409 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2410 case Promote:
2411 In = PromoteOp(Node->getOperand(0));
2412 // Emit the appropriate sign_extend_inreg to get the value we want.
2413 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
2414 Node->getOperand(0).getValueType());
2415 break;
2416 }
2417
Chris Lattnerdc750592005-01-07 07:47:09 +00002418 // The low part is just a sign extension of the input (which degenerates to
2419 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002420 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002421
Chris Lattnerdc750592005-01-07 07:47:09 +00002422 // The high part is obtained by SRA'ing all but one of the bits of the lo
2423 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002424 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002425 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2426 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002427 break;
2428 }
Chris Lattner47844892005-04-03 23:41:52 +00002429 case ISD::ZERO_EXTEND: {
2430 SDOperand In;
2431 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2432 case Expand: assert(0 && "expand-expand not implemented yet!");
2433 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2434 case Promote:
2435 In = PromoteOp(Node->getOperand(0));
2436 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00002437 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00002438 break;
2439 }
2440
Chris Lattnerdc750592005-01-07 07:47:09 +00002441 // The low part is just a zero extension of the input (which degenerates to
2442 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002443 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002444
Chris Lattnerdc750592005-01-07 07:47:09 +00002445 // The high part is just a zero.
2446 Hi = DAG.getConstant(0, NVT);
2447 break;
Chris Lattner47844892005-04-03 23:41:52 +00002448 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002449 // These operators cannot be expanded directly, emit them as calls to
2450 // library functions.
2451 case ISD::FP_TO_SINT:
2452 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002453 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002454 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002455 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002456 break;
2457 case ISD::FP_TO_UINT:
2458 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002459 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002460 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002461 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002462 break;
2463
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002464 case ISD::SHL:
2465 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002466 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002467 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002468
2469 // If this target supports SHL_PARTS, use it.
2470 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002471 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2472 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002473 break;
2474 }
2475
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002476 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002477 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002478 break;
2479
2480 case ISD::SRA:
2481 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002482 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002483 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002484
2485 // If this target supports SRA_PARTS, use it.
2486 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002487 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2488 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002489 break;
2490 }
2491
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002492 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002493 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002494 break;
2495 case ISD::SRL:
2496 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002497 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002498 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002499
2500 // If this target supports SRL_PARTS, use it.
2501 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002502 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2503 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002504 break;
2505 }
2506
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002507 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002508 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002509 break;
2510
Misha Brukman835702a2005-04-21 22:36:52 +00002511 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002512 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2513 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002514 break;
2515 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002516 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2517 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002518 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002519 case ISD::MUL: {
2520 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2521 SDOperand LL, LH, RL, RH;
2522 ExpandOp(Node->getOperand(0), LL, LH);
2523 ExpandOp(Node->getOperand(1), RL, RH);
2524 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2525 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2526 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2527 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2528 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2529 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2530 } else {
2531 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2532 }
2533 break;
2534 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002535 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2536 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2537 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2538 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002539 }
2540
2541 // Remember in a map if the values will be reused later.
2542 if (!Node->hasOneUse()) {
2543 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2544 std::make_pair(Lo, Hi))).second;
2545 assert(isNew && "Value already expanded?!?");
2546 }
2547}
2548
2549
2550// SelectionDAG::Legalize - This is the entry point for the file.
2551//
Chris Lattner4add7e32005-01-23 04:42:50 +00002552void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002553 /// run - This is the main entry point to this class.
2554 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002555 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002556}
2557