blob: 7bb4ce2565cffe888180e1a3185194e5aac1373e [file] [log] [blame]
Chris Lattner3e928bb2005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukmanedf128a2005-04-21 22:36:52 +00002//
Chris Lattner3e928bb2005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukmanedf128a2005-04-21 22:36:52 +00007//
Chris Lattner3e928bb2005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattnere1bd8222005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattner0f69b292005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattneradf6a962005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattner3e928bb2005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner82299e72005-08-05 18:10:27 +000024#include <set>
Chris Lattner3e928bb2005-01-07 07:47:09 +000025using namespace llvm;
26
27//===----------------------------------------------------------------------===//
28/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
29/// hacks on it until the target machine can handle it. This involves
30/// eliminating value sizes the machine cannot handle (promoting small sizes to
31/// large sizes or splitting up large values into small values) as well as
32/// eliminating operations the machine cannot handle.
33///
34/// This code also does a small amount of optimization and recognition of idioms
35/// as part of its processing. For example, if a target does not support a
36/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
37/// will attempt merge setcc and brc instructions into brcc's.
38///
39namespace {
40class SelectionDAGLegalize {
41 TargetLowering &TLI;
42 SelectionDAG &DAG;
43
44 /// LegalizeAction - This enum indicates what action we should take for each
45 /// value type the can occur in the program.
46 enum LegalizeAction {
47 Legal, // The target natively supports this value type.
48 Promote, // This should be promoted to the next larger type.
49 Expand, // This integer type should be broken into smaller pieces.
50 };
51
Chris Lattner3e928bb2005-01-07 07:47:09 +000052 /// ValueTypeActions - This is a bitvector that contains two bits for each
53 /// value type, where the two bits correspond to the LegalizeAction enum.
54 /// This can be queried with "getTypeAction(VT)".
55 unsigned ValueTypeActions;
56
57 /// NeedsAnotherIteration - This is set when we expand a large integer
58 /// operation into smaller integer operations, but the smaller operations are
59 /// not set. This occurs only rarely in practice, for targets that don't have
60 /// 32-bit or larger integer registers.
61 bool NeedsAnotherIteration;
62
63 /// LegalizedNodes - For nodes that are of legal width, and that have more
64 /// than one use, this map indicates what regularized operand to use. This
65 /// allows us to avoid legalizing the same thing more than once.
66 std::map<SDOperand, SDOperand> LegalizedNodes;
67
Chris Lattner03c85462005-01-15 05:21:40 +000068 /// PromotedNodes - For nodes that are below legal width, and that have more
69 /// than one use, this map indicates what promoted value to use. This allows
70 /// us to avoid promoting the same thing more than once.
71 std::map<SDOperand, SDOperand> PromotedNodes;
72
Chris Lattner3e928bb2005-01-07 07:47:09 +000073 /// ExpandedNodes - For nodes that need to be expanded, and which have more
74 /// than one use, this map indicates which which operands are the expanded
75 /// version of the input. This allows us to avoid expanding the same node
76 /// more than once.
77 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
78
Chris Lattner8afc48e2005-01-07 22:28:47 +000079 void AddLegalizedOperand(SDOperand From, SDOperand To) {
80 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
81 assert(isNew && "Got into the map somehow?");
82 }
Chris Lattner03c85462005-01-15 05:21:40 +000083 void AddPromotedOperand(SDOperand From, SDOperand To) {
84 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
85 assert(isNew && "Got into the map somehow?");
86 }
Chris Lattner8afc48e2005-01-07 22:28:47 +000087
Chris Lattner3e928bb2005-01-07 07:47:09 +000088public:
89
Chris Lattner9c32d3b2005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattner3e928bb2005-01-07 07:47:09 +000091
92 /// Run - While there is still lowering to do, perform a pass over the DAG.
93 /// Most regularization can be done in a single pass, but targets that require
94 /// large values to be split into registers multiple times (e.g. i64 -> 4x
95 /// i16) require iteration for these values (the first iteration will demote
96 /// to i32, the second will demote to i16).
97 void Run() {
98 do {
99 NeedsAnotherIteration = false;
100 LegalizeDAG();
101 } while (NeedsAnotherIteration);
102 }
103
104 /// getTypeAction - Return how we should legalize values of this type, either
105 /// it is already legal or we need to expand it into multiple registers of
106 /// smaller integer type, or we need to promote it to a larger type.
107 LegalizeAction getTypeAction(MVT::ValueType VT) const {
108 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
109 }
110
111 /// isTypeLegal - Return true if this type is legal on this target.
112 ///
113 bool isTypeLegal(MVT::ValueType VT) const {
114 return getTypeAction(VT) == Legal;
115 }
116
117private:
118 void LegalizeDAG();
119
120 SDOperand LegalizeOp(SDOperand O);
121 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner03c85462005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000123
Chris Lattner77e77a62005-01-21 06:05:23 +0000124 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
125 SDOperand &Hi);
126 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
127 SDOperand Source);
Chris Lattnercad063f2005-07-16 00:19:57 +0000128
129 SDOperand ExpandLegalUINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000130 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
131 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000132 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000134
Chris Lattnere34b3962005-01-19 04:19:40 +0000135 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
136 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000137 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
139 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000140 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000141
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000142 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
143
Chris Lattner3e928bb2005-01-07 07:47:09 +0000144 SDOperand getIntPtrConstant(uint64_t Val) {
145 return DAG.getConstant(Val, TLI.getPointerTy());
146 }
147};
148}
149
150
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000151SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
152 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
153 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000154 assert(MVT::LAST_VALUETYPE <= 16 &&
155 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000156}
157
Jeff Cohen00b168892005-07-27 06:12:32 +0000158/// ExpandLegalUINT_TO_FP - This function is responsible for legalizing a
Chris Lattnercad063f2005-07-16 00:19:57 +0000159/// UINT_TO_FP operation of the specified operand when the target requests that
160/// we expand it. At this point, we know that the result and operand types are
161/// legal for the target.
162SDOperand SelectionDAGLegalize::ExpandLegalUINT_TO_FP(SDOperand Op0,
163 MVT::ValueType DestVT) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000164 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000165
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000166 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
167 DAG.getConstant(0, Op0.getValueType()),
168 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000169 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
170 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
171 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000172
Chris Lattnerf4d32722005-07-18 04:31:14 +0000173 // If the sign bit of the integer is set, the large number will be treated as
174 // a negative number. To counteract this, the dynamic code adds an offset
175 // depending on the data type.
176 uint64_t FF;
177 switch (Op0.getValueType()) {
178 default: assert(0 && "Unsupported integer type!");
179 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
180 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
181 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
182 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
183 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000184 if (TLI.isLittleEndian()) FF <<= 32;
185 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000186
Chris Lattnercad063f2005-07-16 00:19:57 +0000187 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
188 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
189 TLI.getPointerTy());
190 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
191 SDOperand FudgeInReg;
192 if (DestVT == MVT::f32)
193 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
194 DAG.getSrcValue(NULL));
195 else {
196 assert(DestVT == MVT::f64 && "Unexpected conversion");
197 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
198 DAG.getEntryNode(), CPIdx,
199 DAG.getSrcValue(NULL), MVT::f32));
200 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000201
Chris Lattnercad063f2005-07-16 00:19:57 +0000202 NeedsAnotherIteration = true;
203 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
204}
205
Chris Lattner149c58c2005-08-16 18:17:10 +0000206/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000207/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000208/// we promote it. At this point, we know that the result and operand types are
209/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
210/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000211SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
212 MVT::ValueType DestVT,
213 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000214 // First step, figure out the appropriate *INT_TO_FP operation to use.
215 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000216
Chris Lattnercad063f2005-07-16 00:19:57 +0000217 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000218
Chris Lattnercad063f2005-07-16 00:19:57 +0000219 // Scan for the appropriate larger type to use.
220 while (1) {
221 NewInTy = (MVT::ValueType)(NewInTy+1);
222 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000223
Chris Lattnercad063f2005-07-16 00:19:57 +0000224 // If the target supports SINT_TO_FP of this type, use it.
225 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
226 default: break;
227 case TargetLowering::Legal:
228 if (!TLI.hasNativeSupportFor(NewInTy))
229 break; // Can't use this datatype.
230 // FALL THROUGH.
231 case TargetLowering::Custom:
232 OpToUse = ISD::SINT_TO_FP;
233 break;
234 }
235 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000236 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000237
Chris Lattnercad063f2005-07-16 00:19:57 +0000238 // If the target supports UINT_TO_FP of this type, use it.
239 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
240 default: break;
241 case TargetLowering::Legal:
242 if (!TLI.hasNativeSupportFor(NewInTy))
243 break; // Can't use this datatype.
244 // FALL THROUGH.
245 case TargetLowering::Custom:
246 OpToUse = ISD::UINT_TO_FP;
247 break;
248 }
249 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000250
Chris Lattnercad063f2005-07-16 00:19:57 +0000251 // Otherwise, try a larger type.
252 }
253
254 // Make sure to legalize any nodes we create here in the next pass.
255 NeedsAnotherIteration = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000256
Chris Lattnercad063f2005-07-16 00:19:57 +0000257 // Okay, we found the operation and type to use. Zero extend our input to the
258 // desired type then run the operation on it.
259 return DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000260 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
261 NewInTy, LegalOp));
Chris Lattnercad063f2005-07-16 00:19:57 +0000262}
263
Chris Lattner1618beb2005-07-29 00:11:56 +0000264/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
265/// FP_TO_*INT operation of the specified operand when the target requests that
266/// we promote it. At this point, we know that the result and operand types are
267/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
268/// operation that returns a larger result.
269SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
270 MVT::ValueType DestVT,
271 bool isSigned) {
272 // First step, figure out the appropriate FP_TO*INT operation to use.
273 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000274
Chris Lattner1618beb2005-07-29 00:11:56 +0000275 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000276
Chris Lattner1618beb2005-07-29 00:11:56 +0000277 // Scan for the appropriate larger type to use.
278 while (1) {
279 NewOutTy = (MVT::ValueType)(NewOutTy+1);
280 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000281
Chris Lattner1618beb2005-07-29 00:11:56 +0000282 // If the target supports FP_TO_SINT returning this type, use it.
283 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
284 default: break;
285 case TargetLowering::Legal:
286 if (!TLI.hasNativeSupportFor(NewOutTy))
287 break; // Can't use this datatype.
288 // FALL THROUGH.
289 case TargetLowering::Custom:
290 OpToUse = ISD::FP_TO_SINT;
291 break;
292 }
293 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000294
Chris Lattner1618beb2005-07-29 00:11:56 +0000295 // If the target supports FP_TO_UINT of this type, use it.
296 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
297 default: break;
298 case TargetLowering::Legal:
299 if (!TLI.hasNativeSupportFor(NewOutTy))
300 break; // Can't use this datatype.
301 // FALL THROUGH.
302 case TargetLowering::Custom:
303 OpToUse = ISD::FP_TO_UINT;
304 break;
305 }
306 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000307
Chris Lattner1618beb2005-07-29 00:11:56 +0000308 // Otherwise, try a larger type.
309 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000310
Chris Lattner1618beb2005-07-29 00:11:56 +0000311 // Make sure to legalize any nodes we create here in the next pass.
312 NeedsAnotherIteration = true;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000313
Chris Lattner1618beb2005-07-29 00:11:56 +0000314 // Okay, we found the operation and type to use. Truncate the result of the
315 // extended FP_TO_*INT operation to the desired size.
316 return DAG.getNode(ISD::TRUNCATE, DestVT,
317 DAG.getNode(OpToUse, NewOutTy, LegalOp));
318}
319
320
Chris Lattner3e928bb2005-01-07 07:47:09 +0000321void SelectionDAGLegalize::LegalizeDAG() {
322 SDOperand OldRoot = DAG.getRoot();
323 SDOperand NewRoot = LegalizeOp(OldRoot);
324 DAG.setRoot(NewRoot);
325
326 ExpandedNodes.clear();
327 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000328 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000329
330 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000331 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000332}
333
334SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnere3304a32005-01-08 20:35:13 +0000335 assert(getTypeAction(Op.getValueType()) == Legal &&
336 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000337 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000338
Chris Lattner3e928bb2005-01-07 07:47:09 +0000339 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000340 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000341 if (Node->getNumValues() > 1) {
342 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
343 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000344 case Legal: break; // Nothing to do.
345 case Expand: {
346 SDOperand T1, T2;
347 ExpandOp(Op.getValue(i), T1, T2);
348 assert(LegalizedNodes.count(Op) &&
349 "Expansion didn't add legal operands!");
350 return LegalizedNodes[Op];
351 }
352 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000353 PromoteOp(Op.getValue(i));
354 assert(LegalizedNodes.count(Op) &&
355 "Expansion didn't add legal operands!");
356 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000357 }
358 }
359
Chris Lattner45982da2005-05-12 16:53:42 +0000360 // Note that LegalizeOp may be reentered even from single-use nodes, which
361 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000362 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
363 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000364
Nate Begeman9373a812005-08-10 20:51:12 +0000365 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000366
367 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000368
369 switch (Node->getOpcode()) {
370 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000371 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
372 // If this is a target node, legalize it by legalizing the operands then
373 // passing it through.
374 std::vector<SDOperand> Ops;
375 bool Changed = false;
376 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
377 Ops.push_back(LegalizeOp(Node->getOperand(i)));
378 Changed = Changed || Node->getOperand(i) != Ops.back();
379 }
380 if (Changed)
381 if (Node->getNumValues() == 1)
382 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
383 else {
384 std::vector<MVT::ValueType> VTs(Node->value_begin(),
385 Node->value_end());
386 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
387 }
388
389 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
390 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
391 return Result.getValue(Op.ResNo);
392 }
393 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000394 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
395 assert(0 && "Do not know how to legalize this operator!");
396 abort();
397 case ISD::EntryToken:
398 case ISD::FrameIndex:
399 case ISD::GlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000400 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000401 case ISD::ConstantPool: // Nothing to do.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000402 assert(getTypeAction(Node->getValueType(0)) == Legal &&
403 "This must be legal!");
404 break;
Chris Lattner69a52152005-01-14 22:38:01 +0000405 case ISD::CopyFromReg:
406 Tmp1 = LegalizeOp(Node->getOperand(0));
407 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000408 Result = DAG.getCopyFromReg(Tmp1,
409 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
410 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000411 else
412 Result = Op.getValue(0);
413
414 // Since CopyFromReg produces two values, make sure to remember that we
415 // legalized both of them.
416 AddLegalizedOperand(Op.getValue(0), Result);
417 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
418 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000419 case ISD::ImplicitDef:
420 Tmp1 = LegalizeOp(Node->getOperand(0));
421 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000422 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
423 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000424 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000425 case ISD::UNDEF: {
426 MVT::ValueType VT = Op.getValueType();
427 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000428 default: assert(0 && "This action is not supported yet!");
429 case TargetLowering::Expand:
430 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000431 if (MVT::isInteger(VT))
432 Result = DAG.getConstant(0, VT);
433 else if (MVT::isFloatingPoint(VT))
434 Result = DAG.getConstantFP(0, VT);
435 else
436 assert(0 && "Unknown value type!");
437 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000438 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000439 break;
440 }
441 break;
442 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000443 case ISD::Constant:
444 // We know we don't need to expand constants here, constants only have one
445 // value and we check that it is fine above.
446
447 // FIXME: Maybe we should handle things like targets that don't support full
448 // 32-bit immediates?
449 break;
450 case ISD::ConstantFP: {
451 // Spill FP immediates to the constant pool if the target cannot directly
452 // codegen them. Targets often have some immediate values that can be
453 // efficiently generated into an FP register without a load. We explicitly
454 // leave these constants as ConstantFP nodes for the target to deal with.
455
456 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
457
458 // Check to see if this FP immediate is already legal.
459 bool isLegal = false;
460 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
461 E = TLI.legal_fpimm_end(); I != E; ++I)
462 if (CFP->isExactlyValue(*I)) {
463 isLegal = true;
464 break;
465 }
466
467 if (!isLegal) {
468 // Otherwise we need to spill the constant to memory.
469 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
470
471 bool Extend = false;
472
473 // If a FP immediate is precise when represented as a float, we put it
474 // into the constant pool as a float, even if it's is statically typed
475 // as a double.
476 MVT::ValueType VT = CFP->getValueType(0);
477 bool isDouble = VT == MVT::f64;
478 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
479 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000480 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
481 // Only do this if the target has a native EXTLOAD instruction from
482 // f32.
483 TLI.getOperationAction(ISD::EXTLOAD,
484 MVT::f32) == TargetLowering::Legal) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000485 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
486 VT = MVT::f32;
487 Extend = true;
488 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000489
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
491 TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000492 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000493 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
494 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000495 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000496 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
497 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000498 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000499 }
500 break;
501 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000502 case ISD::TokenFactor: {
503 std::vector<SDOperand> Ops;
504 bool Changed = false;
505 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000506 SDOperand Op = Node->getOperand(i);
507 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnere131e5b2005-05-12 06:04:14 +0000508 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner1e81b9e2005-01-19 19:10:54 +0000509 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
510 Changed = true;
511 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
512 Ops.push_back(LegalizeOp(Op.getOperand(j)));
513 } else {
514 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
515 Changed |= Ops[i] != Op;
516 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000517 }
518 if (Changed)
519 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
520 break;
521 }
522
Chris Lattner16cd04d2005-05-12 23:24:06 +0000523 case ISD::CALLSEQ_START:
524 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000525 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000526 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000527 Tmp2 = Node->getOperand(0);
528 if (Tmp1 != Tmp2) {
Chris Lattner88de6e72005-05-12 00:17:04 +0000529 Node->setAdjCallChain(Tmp1);
Chris Lattner45982da2005-05-12 16:53:42 +0000530
531 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
532 // this will cause the maps used to memoize results to get confused.
533 // Create and add a dummy use, just to increase its use count. This will
534 // be removed at the end of legalize when dead nodes are removed.
535 if (Tmp2.Val->hasOneUse())
536 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
537 DAG.getConstant(0, MVT::i32));
538 }
Chris Lattner16cd04d2005-05-12 23:24:06 +0000539 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000540 // nodes are treated specially and are mutated in place. This makes the dag
541 // legalization process more efficient and also makes libcall insertion
542 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000543 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000544 case ISD::DYNAMIC_STACKALLOC:
545 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
546 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
547 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
548 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000549 Tmp3 != Node->getOperand(2)) {
550 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
551 std::vector<SDOperand> Ops;
552 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
553 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
554 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000555 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000556
557 // Since this op produces two values, make sure to remember that we
558 // legalized both of them.
559 AddLegalizedOperand(SDOperand(Node, 0), Result);
560 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
561 return Result.getValue(Op.ResNo);
562
Chris Lattnerd71c0412005-05-13 18:43:43 +0000563 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000564 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000565 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
566 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000567
568 bool Changed = false;
569 std::vector<SDOperand> Ops;
570 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
571 Ops.push_back(LegalizeOp(Node->getOperand(i)));
572 Changed |= Ops.back() != Node->getOperand(i);
573 }
574
575 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000576 std::vector<MVT::ValueType> RetTyVTs;
577 RetTyVTs.reserve(Node->getNumValues());
578 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000579 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000580 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
581 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000582 } else {
583 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000584 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000585 // Since calls produce multiple values, make sure to remember that we
586 // legalized all of them.
587 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
588 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
589 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000590 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000591 case ISD::BR:
592 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
593 if (Tmp1 != Node->getOperand(0))
594 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
595 break;
596
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000597 case ISD::BRCOND:
598 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000599
Chris Lattner47e92232005-01-18 19:27:06 +0000600 switch (getTypeAction(Node->getOperand(1).getValueType())) {
601 case Expand: assert(0 && "It's impossible to expand bools");
602 case Legal:
603 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
604 break;
605 case Promote:
606 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
607 break;
608 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000609
610 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
611 default: assert(0 && "This action is not supported yet!");
612 case TargetLowering::Expand:
613 // Expand brcond's setcc into its constituent parts and create a BR_CC
614 // Node.
615 if (Tmp2.getOpcode() == ISD::SETCC) {
616 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
617 Tmp2.getOperand(0), Tmp2.getOperand(1),
618 Node->getOperand(2));
619 } else {
620 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
621 DAG.getCondCode(ISD::SETNE), Tmp2,
622 DAG.getConstant(0, Tmp2.getValueType()),
623 Node->getOperand(2));
624 }
625 break;
626 case TargetLowering::Legal:
627 // Basic block destination (Op#2) is always legal.
628 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
629 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
630 Node->getOperand(2));
631 break;
632 }
633 break;
634 case ISD::BR_CC:
635 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
636
637 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
638 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
639 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
640 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
641 Tmp3 != Node->getOperand(3)) {
642 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
643 Tmp2, Tmp3, Node->getOperand(4));
644 }
645 break;
646 } else {
647 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
648 Node->getOperand(2), // LHS
649 Node->getOperand(3), // RHS
650 Node->getOperand(1)));
651 // If we get a SETCC back from legalizing the SETCC node we just
652 // created, then use its LHS, RHS, and CC directly in creating a new
653 // node. Otherwise, select between the true and false value based on
654 // comparing the result of the legalized with zero.
655 if (Tmp2.getOpcode() == ISD::SETCC) {
656 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
657 Tmp2.getOperand(0), Tmp2.getOperand(1),
658 Node->getOperand(4));
659 } else {
660 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
661 DAG.getCondCode(ISD::SETNE),
662 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
663 Node->getOperand(4));
664 }
665 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000666 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000667 case ISD::BRCONDTWOWAY:
668 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
669 switch (getTypeAction(Node->getOperand(1).getValueType())) {
670 case Expand: assert(0 && "It's impossible to expand bools");
671 case Legal:
672 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
673 break;
674 case Promote:
675 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
676 break;
677 }
678 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
679 // pair.
680 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
681 case TargetLowering::Promote:
682 default: assert(0 && "This action is not supported yet!");
683 case TargetLowering::Legal:
684 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
685 std::vector<SDOperand> Ops;
686 Ops.push_back(Tmp1);
687 Ops.push_back(Tmp2);
688 Ops.push_back(Node->getOperand(2));
689 Ops.push_back(Node->getOperand(3));
690 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
691 }
692 break;
693 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000694 // If BRTWOWAY_CC is legal for this target, then simply expand this node
695 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
696 // BRCOND/BR pair.
697 if (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other) ==
698 TargetLowering::Legal) {
699 if (Tmp2.getOpcode() == ISD::SETCC) {
700 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
701 Tmp2.getOperand(0), Tmp2.getOperand(1),
702 Node->getOperand(2), Node->getOperand(3));
703 } else {
704 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
705 DAG.getConstant(0, Tmp2.getValueType()),
706 Node->getOperand(2), Node->getOperand(3));
707 }
708 } else {
709 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000710 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000711 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
712 }
Chris Lattner411e8882005-04-09 03:30:19 +0000713 break;
714 }
715 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000716 case ISD::BRTWOWAY_CC:
717 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
718 if (getTypeAction(Node->getOperand(2).getValueType()) == Legal) {
719 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
720 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
721 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
722 Tmp3 != Node->getOperand(3)) {
723 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
724 Node->getOperand(4), Node->getOperand(5));
725 }
726 break;
727 } else {
728 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
729 Node->getOperand(2), // LHS
730 Node->getOperand(3), // RHS
731 Node->getOperand(1)));
732 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
733 // pair.
734 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
735 default: assert(0 && "This action is not supported yet!");
736 case TargetLowering::Legal:
737 // If we get a SETCC back from legalizing the SETCC node we just
738 // created, then use its LHS, RHS, and CC directly in creating a new
739 // node. Otherwise, select between the true and false value based on
740 // comparing the result of the legalized with zero.
741 if (Tmp2.getOpcode() == ISD::SETCC) {
742 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
743 Tmp2.getOperand(0), Tmp2.getOperand(1),
744 Node->getOperand(4), Node->getOperand(5));
745 } else {
746 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
747 DAG.getConstant(0, Tmp2.getValueType()),
748 Node->getOperand(4), Node->getOperand(5));
749 }
750 break;
751 case TargetLowering::Expand:
752 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
753 Node->getOperand(4));
754 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
755 break;
756 }
757 }
758 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000759 case ISD::LOAD:
760 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
761 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000762
Chris Lattner3e928bb2005-01-07 07:47:09 +0000763 if (Tmp1 != Node->getOperand(0) ||
764 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000765 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
766 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000767 else
768 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000769
Chris Lattner8afc48e2005-01-07 22:28:47 +0000770 // Since loads produce two values, make sure to remember that we legalized
771 // both of them.
772 AddLegalizedOperand(SDOperand(Node, 0), Result);
773 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
774 return Result.getValue(Op.ResNo);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000775
Chris Lattner0f69b292005-01-15 06:18:18 +0000776 case ISD::EXTLOAD:
777 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000778 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000779 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
780 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000781
Chris Lattner5f056bf2005-07-10 01:55:33 +0000782 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000783 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000784 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000785 case TargetLowering::Promote:
786 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000787 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
788 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000789 // Since loads produce two values, make sure to remember that we legalized
790 // both of them.
791 AddLegalizedOperand(SDOperand(Node, 0), Result);
792 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
793 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000794
Chris Lattner01ff7212005-04-10 22:54:25 +0000795 case TargetLowering::Legal:
796 if (Tmp1 != Node->getOperand(0) ||
797 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000798 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
799 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000800 else
801 Result = SDOperand(Node, 0);
802
803 // Since loads produce two values, make sure to remember that we legalized
804 // both of them.
805 AddLegalizedOperand(SDOperand(Node, 0), Result);
806 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
807 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000808 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000809 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
810 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
811 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000812 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000813 if (Op.ResNo)
814 return Load.getValue(1);
815 return Result;
816 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000817 assert(Node->getOpcode() != ISD::EXTLOAD &&
818 "EXTLOAD should always be supported!");
819 // Turn the unsupported load into an EXTLOAD followed by an explicit
820 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000821 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
822 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000823 SDOperand ValRes;
824 if (Node->getOpcode() == ISD::SEXTLOAD)
825 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000826 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000827 else
828 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000829 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
830 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
831 if (Op.ResNo)
832 return Result.getValue(1);
833 return ValRes;
834 }
835 assert(0 && "Unreachable");
836 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000837 case ISD::EXTRACT_ELEMENT:
838 // Get both the low and high parts.
839 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
840 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
841 Result = Tmp2; // 1 -> Hi
842 else
843 Result = Tmp1; // 0 -> Lo
844 break;
845
846 case ISD::CopyToReg:
847 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000848
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000849 assert(getTypeAction(Node->getOperand(2).getValueType()) == Legal &&
850 "Register type must be legal!");
851 // Legalize the incoming value (must be legal).
852 Tmp2 = LegalizeOp(Node->getOperand(2));
853 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
854 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
855 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000856 break;
857
858 case ISD::RET:
859 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
860 switch (Node->getNumOperands()) {
861 case 2: // ret val
862 switch (getTypeAction(Node->getOperand(1).getValueType())) {
863 case Legal:
864 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000865 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +0000866 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
867 break;
868 case Expand: {
869 SDOperand Lo, Hi;
870 ExpandOp(Node->getOperand(1), Lo, Hi);
871 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000872 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000873 }
874 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000875 Tmp2 = PromoteOp(Node->getOperand(1));
876 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
877 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000878 }
879 break;
880 case 1: // ret void
881 if (Tmp1 != Node->getOperand(0))
882 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
883 break;
884 default: { // ret <values>
885 std::vector<SDOperand> NewValues;
886 NewValues.push_back(Tmp1);
887 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
888 switch (getTypeAction(Node->getOperand(i).getValueType())) {
889 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +0000890 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000891 break;
892 case Expand: {
893 SDOperand Lo, Hi;
894 ExpandOp(Node->getOperand(i), Lo, Hi);
895 NewValues.push_back(Lo);
896 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000897 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000898 }
899 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +0000900 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000901 }
902 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
903 break;
904 }
905 }
906 break;
907 case ISD::STORE:
908 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
909 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
910
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000911 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +0000912 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000913 if (CFP->getValueType(0) == MVT::f32) {
914 union {
915 unsigned I;
916 float F;
917 } V;
918 V.F = CFP->getValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000919 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner6d5b8e12005-05-09 20:36:57 +0000920 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000921 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000922 } else {
923 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
924 union {
925 uint64_t I;
926 double F;
927 } V;
928 V.F = CFP->getValue();
Jeff Cohen00b168892005-07-27 06:12:32 +0000929 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner52d08bd2005-05-09 20:23:03 +0000930 DAG.getConstant(V.I, MVT::i64), Tmp2,
931 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000932 }
Chris Lattner84734ce2005-02-22 07:23:39 +0000933 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +0000934 }
935
Chris Lattner3e928bb2005-01-07 07:47:09 +0000936 switch (getTypeAction(Node->getOperand(1).getValueType())) {
937 case Legal: {
938 SDOperand Val = LegalizeOp(Node->getOperand(1));
939 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
940 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000941 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
942 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +0000943 break;
944 }
945 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000946 // Truncate the value and store the result.
947 Tmp3 = PromoteOp(Node->getOperand(1));
948 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000949 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000950 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +0000951 break;
952
Chris Lattner3e928bb2005-01-07 07:47:09 +0000953 case Expand:
954 SDOperand Lo, Hi;
955 ExpandOp(Node->getOperand(1), Lo, Hi);
956
957 if (!TLI.isLittleEndian())
958 std::swap(Lo, Hi);
959
Chris Lattneredb1add2005-05-11 04:51:16 +0000960 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
961 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000962 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000963 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
964 getIntPtrConstant(IncrementSize));
965 assert(isTypeLegal(Tmp2.getValueType()) &&
966 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000967 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +0000968 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
969 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +0000970 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
971 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000972 }
973 break;
Andrew Lenharth95762122005-03-31 21:24:06 +0000974 case ISD::PCMARKER:
975 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +0000976 if (Tmp1 != Node->getOperand(0))
977 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +0000978 break;
Chris Lattner0f69b292005-01-15 06:18:18 +0000979 case ISD::TRUNCSTORE:
980 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
981 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
982
983 switch (getTypeAction(Node->getOperand(1).getValueType())) {
984 case Legal:
985 Tmp2 = LegalizeOp(Node->getOperand(1));
986 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
987 Tmp3 != Node->getOperand(2))
Chris Lattner45b8caf2005-01-15 07:15:18 +0000988 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +0000989 Node->getOperand(3), Node->getOperand(4));
Chris Lattner0f69b292005-01-15 06:18:18 +0000990 break;
991 case Promote:
992 case Expand:
993 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
994 }
995 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +0000996 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +0000997 switch (getTypeAction(Node->getOperand(0).getValueType())) {
998 case Expand: assert(0 && "It's impossible to expand bools");
999 case Legal:
1000 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1001 break;
1002 case Promote:
1003 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1004 break;
1005 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001006 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001007 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001008
1009 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
1010 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001011 case TargetLowering::Expand:
1012 if (Tmp1.getOpcode() == ISD::SETCC) {
1013 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1014 Tmp2, Tmp3,
1015 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1016 } else {
1017 Result = DAG.getSelectCC(Tmp1,
1018 DAG.getConstant(0, Tmp1.getValueType()),
1019 Tmp2, Tmp3, ISD::SETNE);
1020 }
1021 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001022 case TargetLowering::Legal:
1023 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1024 Tmp3 != Node->getOperand(2))
1025 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1026 Tmp1, Tmp2, Tmp3);
1027 break;
1028 case TargetLowering::Promote: {
1029 MVT::ValueType NVT =
1030 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1031 unsigned ExtOp, TruncOp;
1032 if (MVT::isInteger(Tmp2.getValueType())) {
1033 ExtOp = ISD::ZERO_EXTEND;
1034 TruncOp = ISD::TRUNCATE;
1035 } else {
1036 ExtOp = ISD::FP_EXTEND;
1037 TruncOp = ISD::FP_ROUND;
1038 }
1039 // Promote each of the values to the new type.
1040 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1041 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1042 // Perform the larger operation, then round down.
1043 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1044 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1045 break;
1046 }
1047 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001048 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001049 case ISD::SELECT_CC:
1050 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1051 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1052
1053 if (getTypeAction(Node->getOperand(0).getValueType()) == Legal) {
1054 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1055 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1056 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1057 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1058 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1059 Tmp3, Tmp4, Node->getOperand(4));
1060 }
1061 break;
1062 } else {
1063 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1064 Node->getOperand(0), // LHS
1065 Node->getOperand(1), // RHS
1066 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001067 // If we get a SETCC back from legalizing the SETCC node we just
1068 // created, then use its LHS, RHS, and CC directly in creating a new
1069 // node. Otherwise, select between the true and false value based on
1070 // comparing the result of the legalized with zero.
1071 if (Tmp1.getOpcode() == ISD::SETCC) {
1072 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1073 Tmp1.getOperand(0), Tmp1.getOperand(1),
1074 Tmp3, Tmp4, Tmp1.getOperand(2));
1075 } else {
1076 Result = DAG.getSelectCC(Tmp1,
1077 DAG.getConstant(0, Tmp1.getValueType()),
1078 Tmp3, Tmp4, ISD::SETNE);
1079 }
Nate Begeman9373a812005-08-10 20:51:12 +00001080 }
1081 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001082 case ISD::SETCC:
1083 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1084 case Legal:
1085 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1086 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1087 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001088 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1089 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001090 break;
1091 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001092 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1093 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1094
1095 // If this is an FP compare, the operands have already been extended.
1096 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1097 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001098 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001099
1100 // Otherwise, we have to insert explicit sign or zero extends. Note
1101 // that we could insert sign extends for ALL conditions, but zero extend
1102 // is cheaper on many machines (an AND instead of two shifts), so prefer
1103 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001104 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001105 default: assert(0 && "Unknown integer comparison!");
1106 case ISD::SETEQ:
1107 case ISD::SETNE:
1108 case ISD::SETUGE:
1109 case ISD::SETUGT:
1110 case ISD::SETULE:
1111 case ISD::SETULT:
1112 // ALL of these operations will work if we either sign or zero extend
1113 // the operands (including the unsigned comparisons!). Zero extend is
1114 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001115 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1116 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001117 break;
1118 case ISD::SETGE:
1119 case ISD::SETGT:
1120 case ISD::SETLT:
1121 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001122 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1123 DAG.getValueType(VT));
1124 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1125 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001126 break;
1127 }
1128
1129 }
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001130 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1131 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001132 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001133 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001134 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1135 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1136 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001137 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001138 case ISD::SETEQ:
1139 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001140 if (RHSLo == RHSHi)
1141 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1142 if (RHSCST->isAllOnesValue()) {
1143 // Comparison to -1.
1144 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001145 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1146 RHSLo, Node->getOperand(2));
Misha Brukmanedf128a2005-04-21 22:36:52 +00001147 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001148 }
1149
Chris Lattner3e928bb2005-01-07 07:47:09 +00001150 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1151 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1152 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001153 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1,
1154 DAG.getConstant(0, Tmp1.getValueType()),
1155 Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001156 break;
1157 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001158 // If this is a comparison of the sign bit, just look at the top part.
1159 // X > -1, x < 0
1160 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001161 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001162 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001163 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001164 (CST->isAllOnesValue()))) // X > -1
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001165 return DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1166 Node->getOperand(2));
Chris Lattner5b95ed62005-04-12 02:19:10 +00001167
Chris Lattner3e928bb2005-01-07 07:47:09 +00001168 // FIXME: This generated code sucks.
1169 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001170 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001171 default: assert(0 && "Unknown integer setcc!");
1172 case ISD::SETLT:
1173 case ISD::SETULT: LowCC = ISD::SETULT; break;
1174 case ISD::SETGT:
1175 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1176 case ISD::SETLE:
1177 case ISD::SETULE: LowCC = ISD::SETULE; break;
1178 case ISD::SETGE:
1179 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1180 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001181
Chris Lattner3e928bb2005-01-07 07:47:09 +00001182 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1183 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1184 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1185
1186 // NOTE: on targets without efficient SELECT of bools, we can always use
1187 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001188 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1189 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1190 Node->getOperand(2));
1191 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Chris Lattnerf30b73b2005-01-18 02:52:03 +00001192 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1193 Result, Tmp1, Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001194 break;
1195 }
1196 }
1197 break;
1198
Chris Lattnere1bd8222005-01-11 05:57:22 +00001199 case ISD::MEMSET:
1200 case ISD::MEMCPY:
1201 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001202 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001203 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1204
1205 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1206 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1207 case Expand: assert(0 && "Cannot expand a byte!");
1208 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001209 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001210 break;
1211 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001212 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001213 break;
1214 }
1215 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001216 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001217 }
Chris Lattner272455b2005-02-02 03:44:41 +00001218
1219 SDOperand Tmp4;
1220 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001221 case Expand: {
1222 // Length is too big, just take the lo-part of the length.
1223 SDOperand HiPart;
1224 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1225 break;
1226 }
Chris Lattnere5605212005-01-28 22:29:18 +00001227 case Legal:
1228 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001229 break;
1230 case Promote:
1231 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001232 break;
1233 }
1234
1235 SDOperand Tmp5;
1236 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1237 case Expand: assert(0 && "Cannot expand this yet!");
1238 case Legal:
1239 Tmp5 = LegalizeOp(Node->getOperand(4));
1240 break;
1241 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001242 Tmp5 = PromoteOp(Node->getOperand(4));
1243 break;
1244 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001245
1246 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1247 default: assert(0 && "This action not implemented for this operation!");
1248 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001249 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1250 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1251 Tmp5 != Node->getOperand(4)) {
1252 std::vector<SDOperand> Ops;
1253 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1254 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1255 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1256 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001257 break;
1258 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001259 // Otherwise, the target does not support this operation. Lower the
1260 // operation to an explicit libcall as appropriate.
1261 MVT::ValueType IntPtr = TLI.getPointerTy();
1262 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1263 std::vector<std::pair<SDOperand, const Type*> > Args;
1264
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001265 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001266 if (Node->getOpcode() == ISD::MEMSET) {
1267 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1268 // Extend the ubyte argument to be an int value for the call.
1269 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1270 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1271 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1272
1273 FnName = "memset";
1274 } else if (Node->getOpcode() == ISD::MEMCPY ||
1275 Node->getOpcode() == ISD::MEMMOVE) {
1276 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1277 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1278 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1279 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1280 } else {
1281 assert(0 && "Unknown op!");
1282 }
Chris Lattner45982da2005-05-12 16:53:42 +00001283
Chris Lattnere1bd8222005-01-11 05:57:22 +00001284 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001285 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001286 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001287 Result = CallResult.second;
1288 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001289 break;
1290 }
1291 case TargetLowering::Custom:
1292 std::vector<SDOperand> Ops;
1293 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1294 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1295 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner50381b62005-05-14 05:50:48 +00001296 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001297 Result = LegalizeOp(Result);
1298 break;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001299 }
1300 break;
1301 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001302
1303 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001304 Tmp1 = LegalizeOp(Node->getOperand(0));
1305 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001306
Chris Lattner3e011362005-05-14 07:45:46 +00001307 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1308 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1309 std::vector<SDOperand> Ops;
1310 Ops.push_back(Tmp1);
1311 Ops.push_back(Tmp2);
1312 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1313 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001314 Result = SDOperand(Node, 0);
1315 // Since these produce two values, make sure to remember that we legalized
1316 // both of them.
1317 AddLegalizedOperand(SDOperand(Node, 0), Result);
1318 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1319 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001320 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001321 Tmp1 = LegalizeOp(Node->getOperand(0));
1322 Tmp2 = LegalizeOp(Node->getOperand(1));
1323 Tmp3 = LegalizeOp(Node->getOperand(2));
1324 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1325 Tmp3 != Node->getOperand(2))
1326 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1327 break;
1328
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001329 case ISD::READIO:
1330 Tmp1 = LegalizeOp(Node->getOperand(0));
1331 Tmp2 = LegalizeOp(Node->getOperand(1));
1332
1333 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1334 case TargetLowering::Custom:
1335 default: assert(0 && "This action not implemented for this operation!");
1336 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001337 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1338 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1339 std::vector<SDOperand> Ops;
1340 Ops.push_back(Tmp1);
1341 Ops.push_back(Tmp2);
1342 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1343 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001344 Result = SDOperand(Node, 0);
1345 break;
1346 case TargetLowering::Expand:
1347 // Replace this with a load from memory.
1348 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1349 Node->getOperand(1), DAG.getSrcValue(NULL));
1350 Result = LegalizeOp(Result);
1351 break;
1352 }
1353
1354 // Since these produce two values, make sure to remember that we legalized
1355 // both of them.
1356 AddLegalizedOperand(SDOperand(Node, 0), Result);
1357 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1358 return Result.getValue(Op.ResNo);
1359
1360 case ISD::WRITEIO:
1361 Tmp1 = LegalizeOp(Node->getOperand(0));
1362 Tmp2 = LegalizeOp(Node->getOperand(1));
1363 Tmp3 = LegalizeOp(Node->getOperand(2));
1364
1365 switch (TLI.getOperationAction(Node->getOpcode(),
1366 Node->getOperand(1).getValueType())) {
1367 case TargetLowering::Custom:
1368 default: assert(0 && "This action not implemented for this operation!");
1369 case TargetLowering::Legal:
1370 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1371 Tmp3 != Node->getOperand(2))
1372 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1373 break;
1374 case TargetLowering::Expand:
1375 // Replace this with a store to memory.
1376 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1377 Node->getOperand(1), Node->getOperand(2),
1378 DAG.getSrcValue(NULL));
1379 Result = LegalizeOp(Result);
1380 break;
1381 }
1382 break;
1383
Chris Lattner84f67882005-01-20 18:52:28 +00001384 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001385 case ISD::SUB_PARTS:
1386 case ISD::SHL_PARTS:
1387 case ISD::SRA_PARTS:
1388 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001389 std::vector<SDOperand> Ops;
1390 bool Changed = false;
1391 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1392 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1393 Changed |= Ops.back() != Node->getOperand(i);
1394 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001395 if (Changed) {
1396 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1397 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1398 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001399
1400 // Since these produce multiple values, make sure to remember that we
1401 // legalized all of them.
1402 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1403 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1404 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001405 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001406
1407 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001408 case ISD::ADD:
1409 case ISD::SUB:
1410 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001411 case ISD::MULHS:
1412 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001413 case ISD::UDIV:
1414 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001415 case ISD::AND:
1416 case ISD::OR:
1417 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001418 case ISD::SHL:
1419 case ISD::SRL:
1420 case ISD::SRA:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001421 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001422 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1423 case Expand: assert(0 && "Not possible");
1424 case Legal:
1425 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1426 break;
1427 case Promote:
1428 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1429 break;
1430 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001431 if (Tmp1 != Node->getOperand(0) ||
1432 Tmp2 != Node->getOperand(1))
1433 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1434 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001435
Nate Begemanc105e192005-04-06 00:23:54 +00001436 case ISD::UREM:
1437 case ISD::SREM:
1438 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1439 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1440 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1441 case TargetLowering::Legal:
1442 if (Tmp1 != Node->getOperand(0) ||
1443 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001444 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001445 Tmp2);
1446 break;
1447 case TargetLowering::Promote:
1448 case TargetLowering::Custom:
1449 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001450 case TargetLowering::Expand:
1451 if (MVT::isInteger(Node->getValueType(0))) {
1452 MVT::ValueType VT = Node->getValueType(0);
1453 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1454 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1455 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1456 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1457 } else {
1458 // Floating point mod -> fmod libcall.
1459 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1460 SDOperand Dummy;
1461 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001462 }
1463 break;
1464 }
1465 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001466
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001467 case ISD::CTPOP:
1468 case ISD::CTTZ:
1469 case ISD::CTLZ:
1470 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1471 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1472 case TargetLowering::Legal:
1473 if (Tmp1 != Node->getOperand(0))
1474 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1475 break;
1476 case TargetLowering::Promote: {
1477 MVT::ValueType OVT = Tmp1.getValueType();
1478 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001479
1480 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001481 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1482 // Perform the larger operation, then subtract if needed.
1483 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1484 switch(Node->getOpcode())
1485 {
1486 case ISD::CTPOP:
1487 Result = Tmp1;
1488 break;
1489 case ISD::CTTZ:
1490 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001491 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1492 DAG.getConstant(getSizeInBits(NVT), NVT),
1493 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001494 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001495 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1496 break;
1497 case ISD::CTLZ:
1498 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001499 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1500 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001501 getSizeInBits(OVT), NVT));
1502 break;
1503 }
1504 break;
1505 }
1506 case TargetLowering::Custom:
1507 assert(0 && "Cannot custom handle this yet!");
1508 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001509 switch(Node->getOpcode())
1510 {
1511 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001512 static const uint64_t mask[6] = {
1513 0x5555555555555555ULL, 0x3333333333333333ULL,
1514 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1515 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1516 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001517 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001518 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1519 unsigned len = getSizeInBits(VT);
1520 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001521 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001522 Tmp2 = DAG.getConstant(mask[i], VT);
1523 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001524 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001525 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1526 DAG.getNode(ISD::AND, VT,
1527 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1528 Tmp2));
1529 }
1530 Result = Tmp1;
1531 break;
1532 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001533 case ISD::CTLZ: {
1534 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001535 x = x | (x >> 1);
1536 x = x | (x >> 2);
1537 ...
1538 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001539 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001540 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001541
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001542 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1543 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001544 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1545 unsigned len = getSizeInBits(VT);
1546 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1547 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001548 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001549 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1550 }
1551 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001552 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001553 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001554 }
1555 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001556 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001557 // unless the target has ctlz but not ctpop, in which case we use:
1558 // { return 32 - nlz(~x & (x-1)); }
1559 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001560 MVT::ValueType VT = Tmp1.getValueType();
1561 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001562 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001563 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1564 DAG.getNode(ISD::SUB, VT, Tmp1,
1565 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001566 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1567 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1568 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001569 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001570 DAG.getConstant(getSizeInBits(VT), VT),
1571 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1572 } else {
1573 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1574 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001575 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001576 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001577 default:
1578 assert(0 && "Cannot expand this yet!");
1579 break;
1580 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001581 break;
1582 }
1583 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001584
Chris Lattner2c8086f2005-04-02 05:00:07 +00001585 // Unary operators
1586 case ISD::FABS:
1587 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001588 case ISD::FSQRT:
1589 case ISD::FSIN:
1590 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001591 Tmp1 = LegalizeOp(Node->getOperand(0));
1592 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1593 case TargetLowering::Legal:
1594 if (Tmp1 != Node->getOperand(0))
1595 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1596 break;
1597 case TargetLowering::Promote:
1598 case TargetLowering::Custom:
1599 assert(0 && "Cannot promote/custom handle this yet!");
1600 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001601 switch(Node->getOpcode()) {
1602 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001603 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1604 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1605 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1606 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001607 break;
1608 }
1609 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001610 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1611 MVT::ValueType VT = Node->getValueType(0);
1612 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001613 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001614 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1615 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1616 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001617 break;
1618 }
1619 case ISD::FSQRT:
1620 case ISD::FSIN:
1621 case ISD::FCOS: {
1622 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001623 const char *FnName = 0;
1624 switch(Node->getOpcode()) {
1625 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1626 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1627 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1628 default: assert(0 && "Unreachable!");
1629 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001630 SDOperand Dummy;
1631 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001632 break;
1633 }
1634 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001635 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001636 }
1637 break;
1638 }
1639 break;
1640
1641 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001642 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001643 case ISD::UINT_TO_FP: {
1644 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001645 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1646 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001647 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001648 Node->getOperand(0).getValueType())) {
1649 default: assert(0 && "Unknown operation action!");
1650 case TargetLowering::Expand:
1651 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP yet");
1652 Result = ExpandLegalUINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1653 Node->getValueType(0));
1654 AddLegalizedOperand(Op, Result);
1655 return Result;
1656 case TargetLowering::Promote:
1657 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1658 Node->getValueType(0),
1659 isSigned);
1660 AddLegalizedOperand(Op, Result);
1661 return Result;
1662 case TargetLowering::Legal:
1663 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001664 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001665
Chris Lattner3e928bb2005-01-07 07:47:09 +00001666 Tmp1 = LegalizeOp(Node->getOperand(0));
1667 if (Tmp1 != Node->getOperand(0))
1668 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1669 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001670 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001671 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1672 Node->getValueType(0), Node->getOperand(0));
1673 break;
1674 case Promote:
1675 if (isSigned) {
1676 Result = PromoteOp(Node->getOperand(0));
1677 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1678 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1679 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1680 } else {
1681 Result = PromoteOp(Node->getOperand(0));
1682 Result = DAG.getZeroExtendInReg(Result,
1683 Node->getOperand(0).getValueType());
1684 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001685 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001686 break;
1687 }
1688 break;
1689 }
1690 case ISD::TRUNCATE:
1691 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1692 case Legal:
1693 Tmp1 = LegalizeOp(Node->getOperand(0));
1694 if (Tmp1 != Node->getOperand(0))
1695 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1696 break;
1697 case Expand:
1698 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1699
1700 // Since the result is legal, we should just be able to truncate the low
1701 // part of the source.
1702 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1703 break;
1704 case Promote:
1705 Result = PromoteOp(Node->getOperand(0));
1706 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1707 break;
1708 }
1709 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001710
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001711 case ISD::FP_TO_SINT:
1712 case ISD::FP_TO_UINT:
1713 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1714 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001715 Tmp1 = LegalizeOp(Node->getOperand(0));
1716
Chris Lattner1618beb2005-07-29 00:11:56 +00001717 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
1718 default: assert(0 && "Unknown operation action!");
1719 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00001720 if (Node->getOpcode() == ISD::FP_TO_UINT) {
1721 SDOperand True, False;
1722 MVT::ValueType VT = Node->getOperand(0).getValueType();
1723 MVT::ValueType NVT = Node->getValueType(0);
1724 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
1725 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
1726 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
1727 Node->getOperand(0), Tmp2, ISD::SETLT);
1728 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
1729 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
1730 DAG.getNode(ISD::SUB, VT, Node->getOperand(0),
1731 Tmp2));
1732 False = DAG.getNode(ISD::XOR, NVT, False,
1733 DAG.getConstant(1ULL << ShiftAmt, NVT));
1734 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00001735 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00001736 } else {
1737 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
1738 }
1739 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00001740 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001741 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00001742 Node->getOpcode() == ISD::FP_TO_SINT);
1743 AddLegalizedOperand(Op, Result);
1744 return Result;
1745 case TargetLowering::Legal:
1746 break;
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00001747 case TargetLowering::Custom:
1748 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1749 Result = TLI.LowerOperation(Result, DAG);
1750 AddLegalizedOperand(Op, Result);
1751 NeedsAnotherIteration = true;
1752 return Result;
Chris Lattner1618beb2005-07-29 00:11:56 +00001753 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001754
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001755 if (Tmp1 != Node->getOperand(0))
1756 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1757 break;
1758 case Expand:
1759 assert(0 && "Shouldn't need to expand other operators here!");
1760 case Promote:
1761 Result = PromoteOp(Node->getOperand(0));
1762 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1763 break;
1764 }
1765 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001766
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001767 case ISD::ZERO_EXTEND:
1768 case ISD::SIGN_EXTEND:
1769 case ISD::FP_EXTEND:
1770 case ISD::FP_ROUND:
1771 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1772 case Legal:
1773 Tmp1 = LegalizeOp(Node->getOperand(0));
1774 if (Tmp1 != Node->getOperand(0))
1775 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1776 break;
1777 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001778 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00001779
Chris Lattner03c85462005-01-15 05:21:40 +00001780 case Promote:
1781 switch (Node->getOpcode()) {
Chris Lattner1713e732005-01-16 00:38:00 +00001782 case ISD::ZERO_EXTEND:
1783 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001784 // NOTE: Any extend would work here...
1785 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00001786 Result = DAG.getZeroExtendInReg(Result,
1787 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00001788 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001789 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001790 Result = PromoteOp(Node->getOperand(0));
Chris Lattner47e92232005-01-18 19:27:06 +00001791 // NOTE: Any extend would work here...
Chris Lattnerd5d56822005-01-18 21:57:59 +00001792 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00001793 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001794 Result,
1795 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00001796 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001797 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00001798 Result = PromoteOp(Node->getOperand(0));
1799 if (Result.getValueType() != Op.getValueType())
1800 // Dynamically dead while we have only 2 FP types.
1801 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1802 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001803 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00001804 Result = PromoteOp(Node->getOperand(0));
1805 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1806 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001807 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001808 }
1809 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00001810 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00001811 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00001812 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001813 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00001814
1815 // If this operation is not supported, convert it to a shl/shr or load/store
1816 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001817 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1818 default: assert(0 && "This action not supported for this op yet!");
1819 case TargetLowering::Legal:
1820 if (Tmp1 != Node->getOperand(0))
1821 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00001822 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001823 break;
1824 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00001825 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00001826 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001827 // NOTE: we could fall back on load/store here too for targets without
1828 // SAR. However, it is doubtful that any exist.
1829 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1830 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00001831 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00001832 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1833 Node->getOperand(0), ShiftCst);
1834 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1835 Result, ShiftCst);
1836 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1837 // The only way we can lower this is to turn it into a STORETRUNC,
1838 // EXTLOAD pair, targetting a temporary location (a stack slot).
1839
1840 // NOTE: there is a choice here between constantly creating new stack
1841 // slots and always reusing the same one. We currently always create
1842 // new ones, as reuse may inhibit scheduling.
1843 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1844 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1845 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1846 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00001847 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00001848 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1849 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1850 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00001851 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001852 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00001853 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1854 Result, StackSlot, DAG.getSrcValue(NULL),
1855 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00001856 } else {
1857 assert(0 && "Unknown op");
1858 }
1859 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001860 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00001861 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001862 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001863 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00001864 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001865
Chris Lattner45982da2005-05-12 16:53:42 +00001866 // Note that LegalizeOp may be reentered even from single-use nodes, which
1867 // means that we always must cache transformed nodes.
1868 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001869 return Result;
1870}
1871
Chris Lattner8b6fa222005-01-15 22:16:26 +00001872/// PromoteOp - Given an operation that produces a value in an invalid type,
1873/// promote it to compute the value into a larger type. The produced value will
1874/// have the correct bits for the low portion of the register, but no guarantee
1875/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00001876SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1877 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001878 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00001879 assert(getTypeAction(VT) == Promote &&
1880 "Caller should expand or legalize operands that are not promotable!");
1881 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1882 "Cannot promote to smaller type!");
1883
Chris Lattner03c85462005-01-15 05:21:40 +00001884 SDOperand Tmp1, Tmp2, Tmp3;
1885
1886 SDOperand Result;
1887 SDNode *Node = Op.Val;
1888
Chris Lattner45982da2005-05-12 16:53:42 +00001889 if (!Node->hasOneUse()) {
1890 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1891 if (I != PromotedNodes.end()) return I->second;
1892 } else {
1893 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1894 }
1895
Chris Lattner0f69b292005-01-15 06:18:18 +00001896 // Promotion needs an optimization step to clean up after it, and is not
1897 // careful to avoid operations the target does not support. Make sure that
1898 // all generated operations are legalized in the next iteration.
1899 NeedsAnotherIteration = true;
1900
Chris Lattner03c85462005-01-15 05:21:40 +00001901 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001902 case ISD::CopyFromReg:
1903 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00001904 default:
1905 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1906 assert(0 && "Do not know how to promote this operator!");
1907 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00001908 case ISD::UNDEF:
1909 Result = DAG.getNode(ISD::UNDEF, NVT);
1910 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001911 case ISD::Constant:
1912 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1913 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1914 break;
1915 case ISD::ConstantFP:
1916 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1917 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1918 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00001919
Chris Lattner82fbfb62005-01-18 02:59:52 +00001920 case ISD::SETCC:
1921 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1922 "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001923 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
1924 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00001925 Result = LegalizeOp(Result);
1926 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001927
1928 case ISD::TRUNCATE:
1929 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1930 case Legal:
1931 Result = LegalizeOp(Node->getOperand(0));
1932 assert(Result.getValueType() >= NVT &&
1933 "This truncation doesn't make sense!");
1934 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1935 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1936 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00001937 case Promote:
1938 // The truncation is not required, because we don't guarantee anything
1939 // about high bits anyway.
1940 Result = PromoteOp(Node->getOperand(0));
1941 break;
Chris Lattner03c85462005-01-15 05:21:40 +00001942 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00001943 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1944 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00001945 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00001946 }
1947 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00001948 case ISD::SIGN_EXTEND:
1949 case ISD::ZERO_EXTEND:
1950 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1951 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1952 case Legal:
1953 // Input is legal? Just do extend all the way to the larger type.
1954 Result = LegalizeOp(Node->getOperand(0));
1955 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1956 break;
1957 case Promote:
1958 // Promote the reg if it's smaller.
1959 Result = PromoteOp(Node->getOperand(0));
1960 // The high bits are not guaranteed to be anything. Insert an extend.
1961 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00001962 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00001963 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001964 else
Chris Lattner23993562005-04-13 02:38:47 +00001965 Result = DAG.getZeroExtendInReg(Result,
1966 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00001967 break;
1968 }
1969 break;
1970
1971 case ISD::FP_EXTEND:
1972 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1973 case ISD::FP_ROUND:
1974 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1975 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1976 case Promote: assert(0 && "Unreachable with 2 FP types!");
1977 case Legal:
1978 // Input is legal? Do an FP_ROUND_INREG.
1979 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00001980 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1981 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001982 break;
1983 }
1984 break;
1985
1986 case ISD::SINT_TO_FP:
1987 case ISD::UINT_TO_FP:
1988 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1989 case Legal:
1990 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00001991 // No extra round required here.
1992 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001993 break;
1994
1995 case Promote:
1996 Result = PromoteOp(Node->getOperand(0));
1997 if (Node->getOpcode() == ISD::SINT_TO_FP)
1998 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00001999 Result,
2000 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002001 else
Chris Lattner23993562005-04-13 02:38:47 +00002002 Result = DAG.getZeroExtendInReg(Result,
2003 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002004 // No extra round required here.
2005 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002006 break;
2007 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002008 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2009 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002010 // Round if we cannot tolerate excess precision.
2011 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002012 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2013 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002014 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002015 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002016 break;
2017
2018 case ISD::FP_TO_SINT:
2019 case ISD::FP_TO_UINT:
2020 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2021 case Legal:
2022 Tmp1 = LegalizeOp(Node->getOperand(0));
2023 break;
2024 case Promote:
2025 // The input result is prerounded, so we don't have to do anything
2026 // special.
2027 Tmp1 = PromoteOp(Node->getOperand(0));
2028 break;
2029 case Expand:
2030 assert(0 && "not implemented");
2031 }
Nate Begemand2558e32005-08-14 01:20:53 +00002032 // If we're promoting a UINT to a larger size, check to see if the new node
2033 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2034 // we can use that instead. This allows us to generate better code for
2035 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2036 // legal, such as PowerPC.
2037 if (Node->getOpcode() == ISD::FP_TO_UINT &&
2038 TargetLowering::Legal != TLI.getOperationAction(ISD::FP_TO_UINT, NVT) &&
2039 TargetLowering::Legal == TLI.getOperationAction(ISD::FP_TO_SINT, NVT)) {
2040 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2041 } else {
2042 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2043 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002044 break;
2045
Chris Lattner2c8086f2005-04-02 05:00:07 +00002046 case ISD::FABS:
2047 case ISD::FNEG:
2048 Tmp1 = PromoteOp(Node->getOperand(0));
2049 assert(Tmp1.getValueType() == NVT);
2050 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2051 // NOTE: we do not have to do any extra rounding here for
2052 // NoExcessFPPrecision, because we know the input will have the appropriate
2053 // precision, and these operations don't modify precision at all.
2054 break;
2055
Chris Lattnerda6ba872005-04-28 21:44:33 +00002056 case ISD::FSQRT:
2057 case ISD::FSIN:
2058 case ISD::FCOS:
2059 Tmp1 = PromoteOp(Node->getOperand(0));
2060 assert(Tmp1.getValueType() == NVT);
2061 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2062 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002063 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2064 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002065 break;
2066
Chris Lattner03c85462005-01-15 05:21:40 +00002067 case ISD::AND:
2068 case ISD::OR:
2069 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002070 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002071 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002072 case ISD::MUL:
2073 // The input may have strange things in the top bits of the registers, but
2074 // these operations don't care. They may have wierd bits going out, but
2075 // that too is okay if they are integer operations.
2076 Tmp1 = PromoteOp(Node->getOperand(0));
2077 Tmp2 = PromoteOp(Node->getOperand(1));
2078 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2079 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2080
2081 // However, if this is a floating point operation, they will give excess
2082 // precision that we may not be able to tolerate. If we DO allow excess
2083 // precision, just leave it, otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002084 // FIXME: Why would we need to round FP ops more than integer ones?
2085 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner0f69b292005-01-15 06:18:18 +00002086 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002087 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2088 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002089 break;
2090
Chris Lattner8b6fa222005-01-15 22:16:26 +00002091 case ISD::SDIV:
2092 case ISD::SREM:
2093 // These operators require that their input be sign extended.
2094 Tmp1 = PromoteOp(Node->getOperand(0));
2095 Tmp2 = PromoteOp(Node->getOperand(1));
2096 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002097 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2098 DAG.getValueType(VT));
2099 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2100 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002101 }
2102 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2103
2104 // Perform FP_ROUND: this is probably overly pessimistic.
2105 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002106 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2107 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002108 break;
2109
2110 case ISD::UDIV:
2111 case ISD::UREM:
2112 // These operators require that their input be zero extended.
2113 Tmp1 = PromoteOp(Node->getOperand(0));
2114 Tmp2 = PromoteOp(Node->getOperand(1));
2115 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002116 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2117 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002118 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2119 break;
2120
2121 case ISD::SHL:
2122 Tmp1 = PromoteOp(Node->getOperand(0));
2123 Tmp2 = LegalizeOp(Node->getOperand(1));
2124 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2125 break;
2126 case ISD::SRA:
2127 // The input value must be properly sign extended.
2128 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002129 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2130 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002131 Tmp2 = LegalizeOp(Node->getOperand(1));
2132 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2133 break;
2134 case ISD::SRL:
2135 // The input value must be properly zero extended.
2136 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002137 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002138 Tmp2 = LegalizeOp(Node->getOperand(1));
2139 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2140 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002141 case ISD::LOAD:
2142 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2143 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner232ee952005-04-10 04:33:47 +00002144 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner6841dec2005-04-10 17:40:35 +00002145 if (MVT::isInteger(NVT))
Chris Lattner5f056bf2005-07-10 01:55:33 +00002146 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
2147 Node->getOperand(2), VT);
Chris Lattner6841dec2005-04-10 17:40:35 +00002148 else
Chris Lattner5f056bf2005-07-10 01:55:33 +00002149 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2150 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002151
2152 // Remember that we legalized the chain.
2153 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2154 break;
2155 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002156 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2157 case Expand: assert(0 && "It's impossible to expand bools");
2158 case Legal:
2159 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2160 break;
2161 case Promote:
2162 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2163 break;
2164 }
Chris Lattner03c85462005-01-15 05:21:40 +00002165 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2166 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2167 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2168 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002169 case ISD::SELECT_CC:
2170 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2171 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2172 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2173 Node->getOperand(1), Tmp2, Tmp3,
2174 Node->getOperand(4));
2175 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002176 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002177 case ISD::CALL: {
2178 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2179 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2180
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002181 std::vector<SDOperand> Ops;
2182 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2183 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2184
Chris Lattner8ac532c2005-01-16 19:46:48 +00002185 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2186 "Can only promote single result calls");
2187 std::vector<MVT::ValueType> RetTyVTs;
2188 RetTyVTs.reserve(2);
2189 RetTyVTs.push_back(NVT);
2190 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002191 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2192 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002193 Result = SDOperand(NC, 0);
2194
2195 // Insert the new chain mapping.
2196 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2197 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002198 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002199 case ISD::CTPOP:
2200 case ISD::CTTZ:
2201 case ISD::CTLZ:
2202 Tmp1 = Node->getOperand(0);
2203 //Zero extend the argument
2204 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2205 // Perform the larger operation, then subtract if needed.
2206 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2207 switch(Node->getOpcode())
2208 {
2209 case ISD::CTPOP:
2210 Result = Tmp1;
2211 break;
2212 case ISD::CTTZ:
2213 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002214 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002215 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002216 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002217 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2218 break;
2219 case ISD::CTLZ:
2220 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002221 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2222 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002223 getSizeInBits(VT), NVT));
2224 break;
2225 }
2226 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002227 }
2228
2229 assert(Result.Val && "Didn't set a result!");
2230 AddPromotedOperand(Op, Result);
2231 return Result;
2232}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002233
Chris Lattner84f67882005-01-20 18:52:28 +00002234/// ExpandAddSub - Find a clever way to expand this add operation into
2235/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002236void SelectionDAGLegalize::
2237ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2238 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002239 // Expand the subcomponents.
2240 SDOperand LHSL, LHSH, RHSL, RHSH;
2241 ExpandOp(LHS, LHSL, LHSH);
2242 ExpandOp(RHS, RHSL, RHSH);
2243
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002244 // FIXME: this should be moved to the dag combiner someday.
Chris Lattnere89083a2005-05-14 07:25:05 +00002245 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
2246 if (LHSL.getValueType() == MVT::i32) {
2247 SDOperand LowEl;
2248 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
2249 if (C->getValue() == 0)
2250 LowEl = RHSL;
2251 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
2252 if (C->getValue() == 0)
2253 LowEl = LHSL;
2254 if (LowEl.Val) {
2255 // Turn this into an add/sub of the high part only.
2256 SDOperand HiEl =
2257 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2258 LowEl.getValueType(), LHSH, RHSH);
2259 Lo = LowEl;
2260 Hi = HiEl;
2261 return;
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002262 }
Chris Lattnere89083a2005-05-14 07:25:05 +00002263 }
Chris Lattnerbd0781e2005-04-11 20:29:59 +00002264
Chris Lattner84f67882005-01-20 18:52:28 +00002265 std::vector<SDOperand> Ops;
2266 Ops.push_back(LHSL);
2267 Ops.push_back(LHSH);
2268 Ops.push_back(RHSL);
2269 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002270
2271 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2272 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002273 Hi = Lo.getValue(1);
2274}
2275
Chris Lattner5b359c62005-04-02 04:00:59 +00002276void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2277 SDOperand Op, SDOperand Amt,
2278 SDOperand &Lo, SDOperand &Hi) {
2279 // Expand the subcomponents.
2280 SDOperand LHSL, LHSH;
2281 ExpandOp(Op, LHSL, LHSH);
2282
2283 std::vector<SDOperand> Ops;
2284 Ops.push_back(LHSL);
2285 Ops.push_back(LHSH);
2286 Ops.push_back(Amt);
Chris Lattnere89083a2005-05-14 07:25:05 +00002287 std::vector<MVT::ValueType> VTs;
2288 VTs.push_back(LHSL.getValueType());
2289 VTs.push_back(LHSH.getValueType());
2290 VTs.push_back(Amt.getValueType());
2291 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002292 Hi = Lo.getValue(1);
2293}
2294
2295
Chris Lattnere34b3962005-01-19 04:19:40 +00002296/// ExpandShift - Try to find a clever way to expand this shift operation out to
2297/// smaller elements. If we can't find a way that is more efficient than a
2298/// libcall on this target, return false. Otherwise, return true with the
2299/// low-parts expanded into Lo and Hi.
2300bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2301 SDOperand &Lo, SDOperand &Hi) {
2302 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2303 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002304
Chris Lattnere34b3962005-01-19 04:19:40 +00002305 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002306 SDOperand ShAmt = LegalizeOp(Amt);
2307 MVT::ValueType ShTy = ShAmt.getValueType();
2308 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2309 unsigned NVTBits = MVT::getSizeInBits(NVT);
2310
2311 // Handle the case when Amt is an immediate. Other cases are currently broken
2312 // and are disabled.
2313 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2314 unsigned Cst = CN->getValue();
2315 // Expand the incoming operand to be shifted, so that we have its parts
2316 SDOperand InL, InH;
2317 ExpandOp(Op, InL, InH);
2318 switch(Opc) {
2319 case ISD::SHL:
2320 if (Cst > VTBits) {
2321 Lo = DAG.getConstant(0, NVT);
2322 Hi = DAG.getConstant(0, NVT);
2323 } else if (Cst > NVTBits) {
2324 Lo = DAG.getConstant(0, NVT);
2325 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002326 } else if (Cst == NVTBits) {
2327 Lo = DAG.getConstant(0, NVT);
2328 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002329 } else {
2330 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2331 Hi = DAG.getNode(ISD::OR, NVT,
2332 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2333 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2334 }
2335 return true;
2336 case ISD::SRL:
2337 if (Cst > VTBits) {
2338 Lo = DAG.getConstant(0, NVT);
2339 Hi = DAG.getConstant(0, NVT);
2340 } else if (Cst > NVTBits) {
2341 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2342 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002343 } else if (Cst == NVTBits) {
2344 Lo = InH;
2345 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002346 } else {
2347 Lo = DAG.getNode(ISD::OR, NVT,
2348 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2349 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2350 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2351 }
2352 return true;
2353 case ISD::SRA:
2354 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002355 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002356 DAG.getConstant(NVTBits-1, ShTy));
2357 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002358 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002359 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002360 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002361 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002362 } else if (Cst == NVTBits) {
2363 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002364 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002365 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002366 } else {
2367 Lo = DAG.getNode(ISD::OR, NVT,
2368 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2369 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2370 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2371 }
2372 return true;
2373 }
2374 }
2375 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2376 // so disable it for now. Currently targets are handling this via SHL_PARTS
2377 // and friends.
2378 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002379
2380 // If we have an efficient select operation (or if the selects will all fold
2381 // away), lower to some complex code, otherwise just emit the libcall.
2382 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2383 !isa<ConstantSDNode>(Amt))
2384 return false;
2385
2386 SDOperand InL, InH;
2387 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002388 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2389 DAG.getConstant(NVTBits, ShTy), ShAmt);
2390
Chris Lattnere5544f82005-01-20 20:29:23 +00002391 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002392 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2393 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002394
Chris Lattnere34b3962005-01-19 04:19:40 +00002395 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2396 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2397 DAG.getConstant(NVTBits-1, ShTy));
2398 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2399 DAG.getConstant(NVTBits-1, ShTy));
2400 }
2401
2402 if (Opc == ISD::SHL) {
2403 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2404 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2405 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002406 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002407
Chris Lattnere34b3962005-01-19 04:19:40 +00002408 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2409 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2410 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002411 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002412 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2413 DAG.getConstant(32, ShTy),
2414 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002415 DAG.getConstant(0, NVT),
2416 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002417 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002418 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002419 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002420 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002421
2422 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002423 if (Opc == ISD::SRA)
2424 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2425 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002426 else
2427 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002428 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002429 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002430 }
2431 return true;
2432}
Chris Lattner77e77a62005-01-21 06:05:23 +00002433
Chris Lattner9530ddc2005-05-13 05:09:11 +00002434/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2435/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002436/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002437static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002438 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002439
Chris Lattner16cd04d2005-05-12 23:24:06 +00002440 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002441 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002442 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002443 Found = Node;
2444 return;
2445 }
2446
2447 // Otherwise, scan the operands of Node to see if any of them is a call.
2448 assert(Node->getNumOperands() != 0 &&
2449 "All leaves should have depth equal to the entry node!");
2450 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002451 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002452
2453 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002454 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002455 Found);
2456}
2457
2458
Chris Lattner9530ddc2005-05-13 05:09:11 +00002459/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2460/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002461/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002462static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2463 std::set<SDNode*> &Visited) {
2464 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2465 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002466
Chris Lattner16cd04d2005-05-12 23:24:06 +00002467 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002468 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002469 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002470 Found = Node;
2471 return;
2472 }
2473
2474 // Otherwise, scan the operands of Node to see if any of them is a call.
2475 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2476 if (UI == E) return;
2477 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002478 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002479
2480 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002481 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002482}
2483
Chris Lattner9530ddc2005-05-13 05:09:11 +00002484/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002485/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002486static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002487 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002488 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002489 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002490 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002491
2492 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002493 return FindCallSeqEnd(*Node->use_begin());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002494
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002495 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002496 if (TheChain.getValueType() != MVT::Other)
2497 TheChain = SDOperand(Node, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002498 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002499
2500 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002501 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002502
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002503 // Make sure to only follow users of our token chain.
2504 SDNode *User = *UI;
2505 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2506 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002507 if (SDNode *Result = FindCallSeqEnd(User))
2508 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002509 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002510 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002511}
2512
Chris Lattner9530ddc2005-05-13 05:09:11 +00002513/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002514/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002515static SDNode *FindCallSeqStart(SDNode *Node) {
2516 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002517 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002518
2519 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2520 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002521 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002522}
2523
2524
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002525/// FindInputOutputChains - If we are replacing an operation with a call we need
2526/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002527/// properly serialize the calls in the block. The returned operand is the
2528/// input chain value for the new call (e.g. the entry node or the previous
2529/// call), and OutChain is set to be the chain node to update to point to the
2530/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002531static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2532 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002533 SDNode *LatestCallSeqStart = Entry.Val;
2534 SDNode *LatestCallSeqEnd = 0;
2535 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2536 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002537
Chris Lattner16cd04d2005-05-12 23:24:06 +00002538 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002539 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002540 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2541 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002542 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2543 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanc7c16572005-04-11 03:01:51 +00002544 else
Chris Lattner9530ddc2005-05-13 05:09:11 +00002545 LatestCallSeqEnd = Entry.Val;
2546 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002547
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002548 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002549 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002550 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002551 std::set<SDNode*> Visited;
2552 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002553
Chris Lattner9530ddc2005-05-13 05:09:11 +00002554 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002555 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002556 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002557
Chris Lattner9530ddc2005-05-13 05:09:11 +00002558 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002559}
2560
Jeff Cohen00b168892005-07-27 06:12:32 +00002561/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002562void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2563 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002564 // Nothing to splice it into?
2565 if (OutChain == 0) return;
2566
2567 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2568 //OutChain->dump();
2569
2570 // Form a token factor node merging the old inval and the new inval.
2571 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2572 OutChain->getOperand(0));
2573 // Change the node to refer to the new token.
2574 OutChain->setAdjCallChain(InToken);
2575}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002576
2577
Chris Lattner77e77a62005-01-21 06:05:23 +00002578// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2579// does not fit into a register, return the lo part and set the hi part to the
2580// by-reg argument. If it does fit into a single register, return the result
2581// and leave the Hi part unset.
2582SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2583 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002584 SDNode *OutChain;
2585 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2586 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002587 if (InChain.Val == 0)
2588 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002589
Chris Lattner77e77a62005-01-21 06:05:23 +00002590 TargetLowering::ArgListTy Args;
2591 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2592 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2593 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2594 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2595 }
2596 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002597
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002598 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002599 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002600 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002601 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2602 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002603 SpliceCallInto(CallInfo.second, OutChain);
2604
2605 NeedsAnotherIteration = true;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002606
2607 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002608 default: assert(0 && "Unknown thing");
2609 case Legal:
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002610 return CallInfo.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002611 case Promote:
2612 assert(0 && "Cannot promote this yet!");
2613 case Expand:
2614 SDOperand Lo;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002615 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattner77e77a62005-01-21 06:05:23 +00002616 return Lo;
2617 }
2618}
2619
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002620
Chris Lattner77e77a62005-01-21 06:05:23 +00002621/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2622/// destination type is legal.
2623SDOperand SelectionDAGLegalize::
2624ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2625 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2626 assert(getTypeAction(Source.getValueType()) == Expand &&
2627 "This is not an expansion!");
2628 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2629
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002630 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002631 assert(Source.getValueType() == MVT::i64 &&
2632 "This only works for 64-bit -> FP");
2633 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2634 // incoming integer is set. To handle this, we dynamically test to see if
2635 // it is set, and, if so, add a fudge factor.
2636 SDOperand Lo, Hi;
2637 ExpandOp(Source, Lo, Hi);
2638
Chris Lattner66de05b2005-05-13 04:45:13 +00002639 // If this is unsigned, and not supported, first perform the conversion to
2640 // signed, then adjust the result if the sign bit is set.
2641 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2642 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2643
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002644 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2645 DAG.getConstant(0, Hi.getValueType()),
2646 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002647 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2648 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2649 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002650 uint64_t FF = 0x5f800000ULL;
2651 if (TLI.isLittleEndian()) FF <<= 32;
2652 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002653
2654 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2655 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2656 TLI.getPointerTy());
2657 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2658 SDOperand FudgeInReg;
2659 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002660 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2661 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002662 else {
2663 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002664 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2665 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002666 }
2667 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002668 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002669
Chris Lattnera88a2602005-05-14 05:33:54 +00002670 // Check to see if the target has a custom way to lower this. If so, use it.
2671 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2672 default: assert(0 && "This action not implemented for this operation!");
2673 case TargetLowering::Legal:
2674 case TargetLowering::Expand:
2675 break; // This case is handled below.
2676 case TargetLowering::Custom:
2677 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner50381b62005-05-14 05:50:48 +00002678 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnera88a2602005-05-14 05:33:54 +00002679 }
2680
Chris Lattner13689e22005-05-12 07:00:44 +00002681 // Expand the source, then glue it back together for the call. We must expand
2682 // the source in case it is shared (this pass of legalize must traverse it).
2683 SDOperand SrcLo, SrcHi;
2684 ExpandOp(Source, SrcLo, SrcHi);
2685 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2686
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002687 SDNode *OutChain = 0;
2688 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2689 DAG.getEntryNode());
2690 const char *FnName = 0;
2691 if (DestTy == MVT::f32)
2692 FnName = "__floatdisf";
2693 else {
2694 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2695 FnName = "__floatdidf";
2696 }
2697
Chris Lattner77e77a62005-01-21 06:05:23 +00002698 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2699
2700 TargetLowering::ArgListTy Args;
2701 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00002702
Chris Lattner77e77a62005-01-21 06:05:23 +00002703 Args.push_back(std::make_pair(Source, ArgTy));
2704
2705 // We don't care about token chains for libcalls. We just use the entry
2706 // node as our input and ignore the output chain. This allows us to place
2707 // calls wherever we need them to satisfy data dependences.
2708 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002709
2710 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00002711 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2712 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002713
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002714 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002715 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00002716}
Misha Brukmanedf128a2005-04-21 22:36:52 +00002717
Chris Lattnere34b3962005-01-19 04:19:40 +00002718
2719
Chris Lattner3e928bb2005-01-07 07:47:09 +00002720/// ExpandOp - Expand the specified SDOperand into its two component pieces
2721/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2722/// LegalizeNodes map is filled in for any results that are not expanded, the
2723/// ExpandedNodes map is filled in for any results that are expanded, and the
2724/// Lo/Hi values are returned.
2725void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2726 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002727 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002728 SDNode *Node = Op.Val;
2729 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2730 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2731 assert(MVT::isInteger(NVT) && NVT < VT &&
2732 "Cannot expand to FP value or to larger int value!");
2733
2734 // If there is more than one use of this, see if we already expanded it.
2735 // There is no use remembering values that only have a single use, as the map
2736 // entries will never be reused.
2737 if (!Node->hasOneUse()) {
2738 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2739 = ExpandedNodes.find(Op);
2740 if (I != ExpandedNodes.end()) {
2741 Lo = I->second.first;
2742 Hi = I->second.second;
2743 return;
2744 }
Chris Lattner45982da2005-05-12 16:53:42 +00002745 } else {
2746 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00002747 }
2748
Chris Lattner4e6c7462005-01-08 19:27:05 +00002749 // Expanding to multiple registers needs to perform an optimization step, and
2750 // is not careful to avoid operations the target does not support. Make sure
2751 // that all generated operations are legalized in the next iteration.
2752 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002753
Chris Lattner3e928bb2005-01-07 07:47:09 +00002754 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002755 case ISD::CopyFromReg:
2756 assert(0 && "CopyFromReg must be legal!");
2757 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002758 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2759 assert(0 && "Do not know how to expand this operator!");
2760 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002761 case ISD::UNDEF:
2762 Lo = DAG.getNode(ISD::UNDEF, NVT);
2763 Hi = DAG.getNode(ISD::UNDEF, NVT);
2764 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002765 case ISD::Constant: {
2766 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2767 Lo = DAG.getConstant(Cst, NVT);
2768 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2769 break;
2770 }
2771
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00002772 case ISD::BUILD_PAIR:
2773 // Legalize both operands. FIXME: in the future we should handle the case
2774 // where the two elements are not legal.
2775 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2776 Lo = LegalizeOp(Node->getOperand(0));
2777 Hi = LegalizeOp(Node->getOperand(1));
2778 break;
2779
Chris Lattneredb1add2005-05-11 04:51:16 +00002780 case ISD::CTPOP:
2781 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00002782 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2783 DAG.getNode(ISD::CTPOP, NVT, Lo),
2784 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00002785 Hi = DAG.getConstant(0, NVT);
2786 break;
2787
Chris Lattner39a8f332005-05-12 19:05:01 +00002788 case ISD::CTLZ: {
2789 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002790 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002791 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2792 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002793 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
2794 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002795 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2796 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2797
2798 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2799 Hi = DAG.getConstant(0, NVT);
2800 break;
2801 }
2802
2803 case ISD::CTTZ: {
2804 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00002805 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00002806 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2807 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002808 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
2809 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00002810 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2811 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2812
2813 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2814 Hi = DAG.getConstant(0, NVT);
2815 break;
2816 }
Chris Lattneredb1add2005-05-11 04:51:16 +00002817
Chris Lattner3e928bb2005-01-07 07:47:09 +00002818 case ISD::LOAD: {
2819 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2820 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002821 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002822
2823 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00002824 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002825 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2826 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00002827 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00002828 //are from the same instruction?
2829 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00002830
2831 // Build a factor node to remember that this load is independent of the
2832 // other one.
2833 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2834 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002835
Chris Lattner3e928bb2005-01-07 07:47:09 +00002836 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00002837 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002838 if (!TLI.isLittleEndian())
2839 std::swap(Lo, Hi);
2840 break;
2841 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00002842 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00002843 case ISD::CALL: {
2844 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2845 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2846
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002847 bool Changed = false;
2848 std::vector<SDOperand> Ops;
2849 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2850 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2851 Changed |= Ops.back() != Node->getOperand(i);
2852 }
2853
Chris Lattner3e928bb2005-01-07 07:47:09 +00002854 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2855 "Can only expand a call once so far, not i64 -> i16!");
2856
2857 std::vector<MVT::ValueType> RetTyVTs;
2858 RetTyVTs.reserve(3);
2859 RetTyVTs.push_back(NVT);
2860 RetTyVTs.push_back(NVT);
2861 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002862 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2863 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002864 Lo = SDOperand(NC, 0);
2865 Hi = SDOperand(NC, 1);
2866
2867 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00002868 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002869 break;
2870 }
2871 case ISD::AND:
2872 case ISD::OR:
2873 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2874 SDOperand LL, LH, RL, RH;
2875 ExpandOp(Node->getOperand(0), LL, LH);
2876 ExpandOp(Node->getOperand(1), RL, RH);
2877 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2878 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2879 break;
2880 }
2881 case ISD::SELECT: {
2882 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00002883
2884 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2885 case Expand: assert(0 && "It's impossible to expand bools");
2886 case Legal:
2887 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2888 break;
2889 case Promote:
2890 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2891 break;
2892 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002893 ExpandOp(Node->getOperand(1), LL, LH);
2894 ExpandOp(Node->getOperand(2), RL, RH);
2895 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2896 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2897 break;
2898 }
Nate Begeman9373a812005-08-10 20:51:12 +00002899 case ISD::SELECT_CC: {
2900 SDOperand TL, TH, FL, FH;
2901 ExpandOp(Node->getOperand(2), TL, TH);
2902 ExpandOp(Node->getOperand(3), FL, FH);
2903 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2904 Node->getOperand(1), TL, FL, Node->getOperand(4));
2905 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2906 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00002907 Lo = LegalizeOp(Lo);
2908 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00002909 break;
2910 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002911 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00002912 SDOperand In;
2913 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2914 case Expand: assert(0 && "expand-expand not implemented yet!");
2915 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2916 case Promote:
2917 In = PromoteOp(Node->getOperand(0));
2918 // Emit the appropriate sign_extend_inreg to get the value we want.
2919 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00002920 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00002921 break;
2922 }
2923
Chris Lattner3e928bb2005-01-07 07:47:09 +00002924 // The low part is just a sign extension of the input (which degenerates to
2925 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00002926 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002927
Chris Lattner3e928bb2005-01-07 07:47:09 +00002928 // The high part is obtained by SRA'ing all but one of the bits of the lo
2929 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00002930 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00002931 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2932 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00002933 break;
2934 }
Chris Lattner06098e02005-04-03 23:41:52 +00002935 case ISD::ZERO_EXTEND: {
2936 SDOperand In;
2937 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2938 case Expand: assert(0 && "expand-expand not implemented yet!");
2939 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2940 case Promote:
2941 In = PromoteOp(Node->getOperand(0));
2942 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00002943 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00002944 break;
2945 }
2946
Chris Lattner3e928bb2005-01-07 07:47:09 +00002947 // The low part is just a zero extension of the input (which degenerates to
2948 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00002949 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00002950
Chris Lattner3e928bb2005-01-07 07:47:09 +00002951 // The high part is just a zero.
2952 Hi = DAG.getConstant(0, NVT);
2953 break;
Chris Lattner06098e02005-04-03 23:41:52 +00002954 }
Chris Lattner4e6c7462005-01-08 19:27:05 +00002955 // These operators cannot be expanded directly, emit them as calls to
2956 // library functions.
2957 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00002958 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00002959 SDOperand Op;
2960 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2961 case Expand: assert(0 && "cannot expand FP!");
2962 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
2963 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
2964 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002965
Chris Lattnerf20d1832005-07-30 01:40:57 +00002966 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
2967
Chris Lattner80a3e942005-07-29 00:33:32 +00002968 // Now that the custom expander is done, expand the result, which is still
2969 // VT.
Chris Lattnerf20d1832005-07-30 01:40:57 +00002970 ExpandOp(Op, Lo, Hi);
Chris Lattner80a3e942005-07-29 00:33:32 +00002971 break;
2972 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002973
Chris Lattner4e6c7462005-01-08 19:27:05 +00002974 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002975 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002976 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002977 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002978 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002979
Chris Lattner4e6c7462005-01-08 19:27:05 +00002980 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00002981 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
2982 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
2983 LegalizeOp(Node->getOperand(0)));
2984 // Now that the custom expander is done, expand the result, which is still
2985 // VT.
2986 ExpandOp(TLI.LowerOperation(Op, DAG), Lo, Hi);
2987 break;
2988 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002989
Chris Lattner4e6c7462005-01-08 19:27:05 +00002990 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00002991 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002992 else
Chris Lattner77e77a62005-01-21 06:05:23 +00002993 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00002994 break;
2995
Chris Lattnere34b3962005-01-19 04:19:40 +00002996 case ISD::SHL:
2997 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00002998 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00002999 break;
Chris Lattner47599822005-04-02 03:38:53 +00003000
3001 // If this target supports SHL_PARTS, use it.
3002 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003003 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3004 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003005 break;
3006 }
3007
Chris Lattnere34b3962005-01-19 04:19:40 +00003008 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003009 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003010 break;
3011
3012 case ISD::SRA:
3013 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003014 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003015 break;
Chris Lattner47599822005-04-02 03:38:53 +00003016
3017 // If this target supports SRA_PARTS, use it.
3018 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003019 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3020 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003021 break;
3022 }
3023
Chris Lattnere34b3962005-01-19 04:19:40 +00003024 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003025 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003026 break;
3027 case ISD::SRL:
3028 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003029 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003030 break;
Chris Lattner47599822005-04-02 03:38:53 +00003031
3032 // If this target supports SRL_PARTS, use it.
3033 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003034 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3035 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003036 break;
3037 }
3038
Chris Lattnere34b3962005-01-19 04:19:40 +00003039 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003040 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003041 break;
3042
Misha Brukmanedf128a2005-04-21 22:36:52 +00003043 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003044 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3045 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003046 break;
3047 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003048 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3049 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003050 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003051 case ISD::MUL: {
3052 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
3053 SDOperand LL, LH, RL, RH;
3054 ExpandOp(Node->getOperand(0), LL, LH);
3055 ExpandOp(Node->getOperand(1), RL, RH);
3056 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3057 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3058 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3059 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3060 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3061 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3062 } else {
3063 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3064 }
3065 break;
3066 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003067 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3068 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3069 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3070 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003071 }
3072
3073 // Remember in a map if the values will be reused later.
3074 if (!Node->hasOneUse()) {
3075 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3076 std::make_pair(Lo, Hi))).second;
3077 assert(isNew && "Value already expanded?!?");
3078 }
3079}
3080
3081
3082// SelectionDAG::Legalize - This is the entry point for the file.
3083//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003084void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003085 /// run - This is the main entry point to this class.
3086 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003087 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003088}
3089