blob: 92144c1245238d061b47f42b6bf2e2e47c89eec9 [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"
Chris Lattner3e928bb2005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner45b8caf2005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey02659d22005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.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
Jim Laskey6269ed12005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman5a8441e2005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner1618beb2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen00b168892005-07-27 06:12:32 +0000136
Chris Lattnere34b3962005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner5b359c62005-04-02 04:00:59 +0000139 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
140 SDOperand &Lo, SDOperand &Hi);
141 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner47599822005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +0000143
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattner3e928bb2005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattnerb89175f2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000155 case ISD::VADD: return MVT::isInteger(VT) ? ISD::ADD : ISD::FADD;
156 case ISD::VSUB: return MVT::isInteger(VT) ? ISD::SUB : ISD::FSUB;
157 case ISD::VMUL: return MVT::isInteger(VT) ? ISD::MUL : ISD::FMUL;
158 }
159}
Chris Lattner3e928bb2005-01-07 07:47:09 +0000160
Chris Lattner9c32d3b2005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000164 assert(MVT::LAST_VALUETYPE <= 16 &&
165 "Too many value types for ValueTypeActions to hold!");
Chris Lattner3e928bb2005-01-07 07:47:09 +0000166}
167
Jim Laskey6269ed12005-08-17 00:39:29 +0000168/// ExpandLegalINT_TO_FP - This function is responsible for legalizing a
169/// INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000170/// we expand it. At this point, we know that the result and operand types are
171/// legal for the target.
Jim Laskey6269ed12005-08-17 00:39:29 +0000172SDOperand SelectionDAGLegalize::ExpandLegalINT_TO_FP(bool isSigned,
173 SDOperand Op0,
174 MVT::ValueType DestVT) {
175 if (Op0.getValueType() == MVT::i32) {
176 // simple 32-bit [signed|unsigned] integer to float/double expansion
177
178 // get the stack frame index of a 8 byte buffer
179 MachineFunction &MF = DAG.getMachineFunction();
180 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
181 // get address of 8 byte buffer
182 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
183 // word offset constant for Hi/Lo address computation
184 SDOperand WordOff = DAG.getConstant(sizeof(int), TLI.getPointerTy());
185 // set up Hi and Lo (into buffer) address based on endian
186 SDOperand Hi, Lo;
187 if (TLI.isLittleEndian()) {
188 Hi = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
189 Lo = StackSlot;
190 } else {
191 Hi = StackSlot;
192 Lo = DAG.getNode(ISD::ADD, TLI.getPointerTy(), StackSlot, WordOff);
193 }
194 // if signed map to unsigned space
195 SDOperand Op0Mapped;
196 if (isSigned) {
197 // constant used to invert sign bit (signed to unsigned mapping)
198 SDOperand SignBit = DAG.getConstant(0x80000000u, MVT::i32);
199 Op0Mapped = DAG.getNode(ISD::XOR, MVT::i32, Op0, SignBit);
200 } else {
201 Op0Mapped = Op0;
202 }
203 // store the lo of the constructed double - based on integer input
204 SDOperand Store1 = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(),
205 Op0Mapped, Lo, DAG.getSrcValue(NULL));
206 // initial hi portion of constructed double
207 SDOperand InitialHi = DAG.getConstant(0x43300000u, MVT::i32);
208 // store the hi of the constructed double - biased exponent
209 SDOperand Store2 = DAG.getNode(ISD::STORE, MVT::Other, Store1,
210 InitialHi, Hi, DAG.getSrcValue(NULL));
211 // load the constructed double
212 SDOperand Load = DAG.getLoad(MVT::f64, Store2, StackSlot,
213 DAG.getSrcValue(NULL));
214 // FP constant to bias correct the final result
Jim Laskey02659d22005-08-17 17:42:52 +0000215 SDOperand Bias = DAG.getConstantFP(isSigned ?
216 BitsToDouble(0x4330000080000000ULL)
217 : BitsToDouble(0x4330000000000000ULL),
Jim Laskey6269ed12005-08-17 00:39:29 +0000218 MVT::f64);
219 // subtract the bias
Chris Lattner01b3d732005-09-28 22:28:18 +0000220 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskey6269ed12005-08-17 00:39:29 +0000221 // final result
222 SDOperand Result;
223 // handle final rounding
224 if (DestVT == MVT::f64) {
225 // do nothing
226 Result = Sub;
227 } else {
228 // if f32 then cast to f32
229 Result = DAG.getNode(ISD::FP_ROUND, MVT::f32, Sub);
230 }
231 NeedsAnotherIteration = true;
232 return Result;
233 }
234 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP for i64 yet");
Chris Lattnercad063f2005-07-16 00:19:57 +0000235 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen00b168892005-07-27 06:12:32 +0000236
Chris Lattner7cf7e3f2005-08-09 20:20:18 +0000237 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
238 DAG.getConstant(0, Op0.getValueType()),
239 ISD::SETLT);
Chris Lattnercad063f2005-07-16 00:19:57 +0000240 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
241 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
242 SignSet, Four, Zero);
Jeff Cohen00b168892005-07-27 06:12:32 +0000243
Jim Laskey6269ed12005-08-17 00:39:29 +0000244 // If the sign bit of the integer is set, the large number will be treated
245 // as a negative number. To counteract this, the dynamic code adds an
246 // offset depending on the data type.
Chris Lattnerf4d32722005-07-18 04:31:14 +0000247 uint64_t FF;
248 switch (Op0.getValueType()) {
249 default: assert(0 && "Unsupported integer type!");
250 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
251 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
252 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
253 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
254 }
Chris Lattnercad063f2005-07-16 00:19:57 +0000255 if (TLI.isLittleEndian()) FF <<= 32;
256 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen00b168892005-07-27 06:12:32 +0000257
Chris Lattner5839bf22005-08-26 17:15:30 +0000258 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnercad063f2005-07-16 00:19:57 +0000259 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
260 SDOperand FudgeInReg;
261 if (DestVT == MVT::f32)
262 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
263 DAG.getSrcValue(NULL));
264 else {
265 assert(DestVT == MVT::f64 && "Unexpected conversion");
266 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
267 DAG.getEntryNode(), CPIdx,
268 DAG.getSrcValue(NULL), MVT::f32));
269 }
Jeff Cohen00b168892005-07-27 06:12:32 +0000270
Chris Lattnercad063f2005-07-16 00:19:57 +0000271 NeedsAnotherIteration = true;
Chris Lattner473a9902005-09-29 06:44:39 +0000272 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnercad063f2005-07-16 00:19:57 +0000273}
274
Chris Lattner149c58c2005-08-16 18:17:10 +0000275/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner1618beb2005-07-29 00:11:56 +0000276/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnercad063f2005-07-16 00:19:57 +0000277/// we promote it. At this point, we know that the result and operand types are
278/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
279/// operation that takes a larger input.
Nate Begeman5a8441e2005-07-16 02:02:34 +0000280SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
281 MVT::ValueType DestVT,
282 bool isSigned) {
Chris Lattnercad063f2005-07-16 00:19:57 +0000283 // First step, figure out the appropriate *INT_TO_FP operation to use.
284 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen00b168892005-07-27 06:12:32 +0000285
Chris Lattnercad063f2005-07-16 00:19:57 +0000286 unsigned OpToUse = 0;
Jeff Cohen00b168892005-07-27 06:12:32 +0000287
Chris Lattnercad063f2005-07-16 00:19:57 +0000288 // Scan for the appropriate larger type to use.
289 while (1) {
290 NewInTy = (MVT::ValueType)(NewInTy+1);
291 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen00b168892005-07-27 06:12:32 +0000292
Chris Lattnercad063f2005-07-16 00:19:57 +0000293 // If the target supports SINT_TO_FP of this type, use it.
294 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
295 default: break;
296 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000297 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000298 break; // Can't use this datatype.
299 // FALL THROUGH.
300 case TargetLowering::Custom:
301 OpToUse = ISD::SINT_TO_FP;
302 break;
303 }
304 if (OpToUse) break;
Nate Begeman5a8441e2005-07-16 02:02:34 +0000305 if (isSigned) continue;
Jeff Cohen00b168892005-07-27 06:12:32 +0000306
Chris Lattnercad063f2005-07-16 00:19:57 +0000307 // If the target supports UINT_TO_FP of this type, use it.
308 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
309 default: break;
310 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000311 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnercad063f2005-07-16 00:19:57 +0000312 break; // Can't use this datatype.
313 // FALL THROUGH.
314 case TargetLowering::Custom:
315 OpToUse = ISD::UINT_TO_FP;
316 break;
317 }
318 if (OpToUse) break;
Jeff Cohen00b168892005-07-27 06:12:32 +0000319
Chris Lattnercad063f2005-07-16 00:19:57 +0000320 // Otherwise, try a larger type.
321 }
322
323 // Make sure to legalize any nodes we create here in the next pass.
324 NeedsAnotherIteration = true;
Jeff Cohen00b168892005-07-27 06:12:32 +0000325
Chris Lattnercad063f2005-07-16 00:19:57 +0000326 // Okay, we found the operation and type to use. Zero extend our input to the
327 // desired type then run the operation on it.
328 return DAG.getNode(OpToUse, DestVT,
Nate Begeman5a8441e2005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattnercad063f2005-07-16 00:19:57 +0000331}
332
Chris Lattner1618beb2005-07-29 00:11:56 +0000333/// PromoteLegalFP_TO_INT - This function is responsible for legalizing a
334/// FP_TO_*INT operation of the specified operand when the target requests that
335/// we promote it. At this point, we know that the result and operand types are
336/// legal for the target, and that there is a legal FP_TO_UINT or FP_TO_SINT
337/// operation that returns a larger result.
338SDOperand SelectionDAGLegalize::PromoteLegalFP_TO_INT(SDOperand LegalOp,
339 MVT::ValueType DestVT,
340 bool isSigned) {
341 // First step, figure out the appropriate FP_TO*INT operation to use.
342 MVT::ValueType NewOutTy = DestVT;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000343
Chris Lattner1618beb2005-07-29 00:11:56 +0000344 unsigned OpToUse = 0;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000345
Chris Lattner1618beb2005-07-29 00:11:56 +0000346 // Scan for the appropriate larger type to use.
347 while (1) {
348 NewOutTy = (MVT::ValueType)(NewOutTy+1);
349 assert(MVT::isInteger(NewOutTy) && "Ran out of possibilities!");
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000350
Chris Lattner1618beb2005-07-29 00:11:56 +0000351 // If the target supports FP_TO_SINT returning this type, use it.
352 switch (TLI.getOperationAction(ISD::FP_TO_SINT, NewOutTy)) {
353 default: break;
354 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000355 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000356 break; // Can't use this datatype.
357 // FALL THROUGH.
358 case TargetLowering::Custom:
359 OpToUse = ISD::FP_TO_SINT;
360 break;
361 }
362 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000363
Chris Lattner1618beb2005-07-29 00:11:56 +0000364 // If the target supports FP_TO_UINT of this type, use it.
365 switch (TLI.getOperationAction(ISD::FP_TO_UINT, NewOutTy)) {
366 default: break;
367 case TargetLowering::Legal:
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000368 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner1618beb2005-07-29 00:11:56 +0000369 break; // Can't use this datatype.
370 // FALL THROUGH.
371 case TargetLowering::Custom:
372 OpToUse = ISD::FP_TO_UINT;
373 break;
374 }
375 if (OpToUse) break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000376
Chris Lattner1618beb2005-07-29 00:11:56 +0000377 // Otherwise, try a larger type.
378 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000379
Chris Lattner1618beb2005-07-29 00:11:56 +0000380 // Make sure to legalize any nodes we create here in the next pass.
381 NeedsAnotherIteration = true;
Jeff Cohend29b6aa2005-07-30 18:33:25 +0000382
Chris Lattner1618beb2005-07-29 00:11:56 +0000383 // Okay, we found the operation and type to use. Truncate the result of the
384 // extended FP_TO_*INT operation to the desired size.
385 return DAG.getNode(ISD::TRUNCATE, DestVT,
386 DAG.getNode(OpToUse, NewOutTy, LegalOp));
387}
388
Chris Lattner32fca002005-10-06 01:20:27 +0000389/// ComputeTopDownOrdering - Add the specified node to the Order list if it has
390/// not been visited yet and if all of its operands have already been visited.
391static void ComputeTopDownOrdering(SDNode *N, std::vector<SDNode*> &Order,
392 std::map<SDNode*, unsigned> &Visited) {
393 if (++Visited[N] != N->getNumOperands())
394 return; // Haven't visited all operands yet
395
396 Order.push_back(N);
397
398 if (N->hasOneUse()) { // Tail recurse in common case.
399 ComputeTopDownOrdering(*N->use_begin(), Order, Visited);
400 return;
401 }
402
403 // Now that we have N in, add anything that uses it if all of their operands
404 // are now done.
Chris Lattner32fca002005-10-06 01:20:27 +0000405 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E;++UI)
406 ComputeTopDownOrdering(*UI, Order, Visited);
407}
408
Chris Lattner1618beb2005-07-29 00:11:56 +0000409
Chris Lattner3e928bb2005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattnerab510a72005-10-02 17:49:46 +0000411 // The legalize process is inherently a bottom-up recursive process (users
412 // legalize their uses before themselves). Given infinite stack space, we
413 // could just start legalizing on the root and traverse the whole graph. In
414 // practice however, this causes us to run out of stack space on large basic
Chris Lattner32fca002005-10-06 01:20:27 +0000415 // blocks. To avoid this problem, compute an ordering of the nodes where each
416 // node is only legalized after all of its operands are legalized.
417 std::map<SDNode*, unsigned> Visited;
418 std::vector<SDNode*> Order;
Chris Lattnerab510a72005-10-02 17:49:46 +0000419
Chris Lattner32fca002005-10-06 01:20:27 +0000420 // Compute ordering from all of the leaves in the graphs, those (like the
421 // entry node) that have no operands.
422 for (SelectionDAG::allnodes_iterator I = DAG.allnodes_begin(),
423 E = DAG.allnodes_end(); I != E; ++I) {
Chris Lattnerde202b32005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattnerab510a72005-10-02 17:49:46 +0000427 }
Chris Lattnerab510a72005-10-02 17:49:46 +0000428 }
429
Chris Lattnerde202b32005-11-09 23:47:37 +0000430 assert(Order.size() == Visited.size() &&
431 Order.size() ==
432 (unsigned)std::distance(DAG.allnodes_begin(), DAG.allnodes_end()) &&
Chris Lattner32fca002005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattnerab510a72005-10-02 17:49:46 +0000435
Chris Lattner32fca002005-10-06 01:20:27 +0000436 for (unsigned i = 0, e = Order.size(); i != e; ++i) {
437 SDNode *N = Order[i];
438 switch (getTypeAction(N->getValueType(0))) {
439 default: assert(0 && "Bad type action!");
440 case Legal:
441 LegalizeOp(SDOperand(N, 0));
442 break;
443 case Promote:
444 PromoteOp(SDOperand(N, 0));
445 break;
446 case Expand: {
447 SDOperand X, Y;
448 ExpandOp(SDOperand(N, 0), X, Y);
449 break;
450 }
451 }
452 }
453
454 // Finally, it's possible the root changed. Get the new root.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner32fca002005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner71c42a02005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattner3e928bb2005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner62fd2692005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnere3304a32005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattner45982da2005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnere3304a32005-01-08 20:35:13 +0000471
Chris Lattner3e928bb2005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnere3304a32005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattner45982da2005-05-12 16:53:42 +0000474 if (Node->getNumValues() > 1) {
475 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
476 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000477 case Legal: break; // Nothing to do.
478 case Expand: {
479 SDOperand T1, T2;
480 ExpandOp(Op.getValue(i), T1, T2);
481 assert(LegalizedNodes.count(Op) &&
482 "Expansion didn't add legal operands!");
483 return LegalizedNodes[Op];
484 }
485 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +0000486 PromoteOp(Op.getValue(i));
487 assert(LegalizedNodes.count(Op) &&
488 "Expansion didn't add legal operands!");
489 return LegalizedNodes[Op];
Chris Lattner3e928bb2005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattner45982da2005-05-12 16:53:42 +0000493 // Note that LegalizeOp may be reentered even from single-use nodes, which
494 // means that we always must cache transformed nodes.
Chris Lattnere1bd8222005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000497
Nate Begeman9373a812005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattnerd73cc5d2005-05-14 06:34:48 +0000504 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
505 // If this is a target node, legalize it by legalizing the operands then
506 // passing it through.
507 std::vector<SDOperand> Ops;
508 bool Changed = false;
509 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
510 Ops.push_back(LegalizeOp(Node->getOperand(i)));
511 Changed = Changed || Node->getOperand(i) != Ops.back();
512 }
513 if (Changed)
514 if (Node->getNumValues() == 1)
515 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
516 else {
517 std::vector<MVT::ValueType> VTs(Node->value_begin(),
518 Node->value_end());
519 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
520 }
521
522 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
523 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
524 return Result.getValue(Op.ResNo);
525 }
526 // Otherwise this is an unhandled builtin node. splat.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000527 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
528 assert(0 && "Do not know how to legalize this operator!");
529 abort();
530 case ISD::EntryToken:
531 case ISD::FrameIndex:
Chris Lattner32fca002005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000535 case ISD::GlobalAddress:
Chris Lattnerb9debbf2005-11-17 05:52:24 +0000536 case ISD::TargetGlobalAddress:
Chris Lattner03c0cf82005-01-07 21:45:56 +0000537 case ISD::ExternalSymbol:
Chris Lattner69a52152005-01-14 22:38:01 +0000538 case ISD::ConstantPool: // Nothing to do.
Chris Lattner32fca002005-10-06 01:20:27 +0000539 case ISD::BasicBlock:
540 case ISD::CONDCODE:
541 case ISD::VALUETYPE:
542 case ISD::SRCVALUE:
Chris Lattner0c8fbe32005-11-17 06:41:44 +0000543 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
544 default: assert(0 && "This action is not supported yet!");
545 case TargetLowering::Custom: {
546 SDOperand Tmp = TLI.LowerOperation(Op, DAG);
547 if (Tmp.Val) {
548 Result = LegalizeOp(Tmp);
549 break;
550 }
551 } // FALLTHROUGH if the target doesn't want to lower this op after all.
552 case TargetLowering::Legal:
553 assert(isTypeLegal(Node->getValueType(0)) && "This must be legal!");
554 break;
555 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000556 break;
Chris Lattner08951a32005-09-02 01:15:01 +0000557 case ISD::AssertSext:
558 case ISD::AssertZext:
559 Tmp1 = LegalizeOp(Node->getOperand(0));
560 if (Tmp1 != Node->getOperand(0))
561 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
562 Node->getOperand(1));
563 break;
Chris Lattner308575b2005-11-20 22:56:56 +0000564 case ISD::MERGE_VALUES:
565 return LegalizeOp(Node->getOperand(Op.ResNo));
Chris Lattner69a52152005-01-14 22:38:01 +0000566 case ISD::CopyFromReg:
567 Tmp1 = LegalizeOp(Node->getOperand(0));
568 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000569 Result = DAG.getCopyFromReg(Tmp1,
570 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
571 Node->getValueType(0));
Chris Lattner13c184d2005-01-28 06:27:38 +0000572 else
573 Result = Op.getValue(0);
574
575 // Since CopyFromReg produces two values, make sure to remember that we
576 // legalized both of them.
577 AddLegalizedOperand(Op.getValue(0), Result);
578 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
579 return Result.getValue(Op.ResNo);
Chris Lattner18c2f132005-01-13 20:50:02 +0000580 case ISD::ImplicitDef:
581 Tmp1 = LegalizeOp(Node->getOperand(0));
582 if (Tmp1 != Node->getOperand(0))
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +0000583 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
584 Tmp1, Node->getOperand(1));
Chris Lattner18c2f132005-01-13 20:50:02 +0000585 break;
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000586 case ISD::UNDEF: {
587 MVT::ValueType VT = Op.getValueType();
588 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begemanea19cd52005-04-02 00:41:14 +0000589 default: assert(0 && "This action is not supported yet!");
590 case TargetLowering::Expand:
591 case TargetLowering::Promote:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000592 if (MVT::isInteger(VT))
593 Result = DAG.getConstant(0, VT);
594 else if (MVT::isFloatingPoint(VT))
595 Result = DAG.getConstantFP(0, VT);
596 else
597 assert(0 && "Unknown value type!");
598 break;
Nate Begemanea19cd52005-04-02 00:41:14 +0000599 case TargetLowering::Legal:
Nate Begemanfc1b1da2005-04-01 22:34:39 +0000600 break;
601 }
602 break;
603 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000604 case ISD::Constant:
605 // We know we don't need to expand constants here, constants only have one
606 // value and we check that it is fine above.
607
608 // FIXME: Maybe we should handle things like targets that don't support full
609 // 32-bit immediates?
610 break;
611 case ISD::ConstantFP: {
612 // Spill FP immediates to the constant pool if the target cannot directly
613 // codegen them. Targets often have some immediate values that can be
614 // efficiently generated into an FP register without a load. We explicitly
615 // leave these constants as ConstantFP nodes for the target to deal with.
616
617 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
618
619 // Check to see if this FP immediate is already legal.
620 bool isLegal = false;
621 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
622 E = TLI.legal_fpimm_end(); I != E; ++I)
623 if (CFP->isExactlyValue(*I)) {
624 isLegal = true;
625 break;
626 }
627
628 if (!isLegal) {
629 // Otherwise we need to spill the constant to memory.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000630 bool Extend = false;
631
632 // If a FP immediate is precise when represented as a float, we put it
633 // into the constant pool as a float, even if it's is statically typed
634 // as a double.
635 MVT::ValueType VT = CFP->getValueType(0);
636 bool isDouble = VT == MVT::f64;
637 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
638 Type::FloatTy, CFP->getValue());
Chris Lattner99939d32005-01-28 22:58:25 +0000639 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
640 // Only do this if the target has a native EXTLOAD instruction from
641 // f32.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000642 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000643 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
644 VT = MVT::f32;
645 Extend = true;
646 }
Misha Brukmanedf128a2005-04-21 22:36:52 +0000647
Chris Lattner5839bf22005-08-26 17:15:30 +0000648 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattnerf8161d82005-01-16 05:06:12 +0000649 if (Extend) {
Chris Lattner5f056bf2005-07-10 01:55:33 +0000650 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
651 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnerf8161d82005-01-16 05:06:12 +0000652 } else {
Chris Lattner52d08bd2005-05-09 20:23:03 +0000653 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
654 DAG.getSrcValue(NULL));
Chris Lattnerf8161d82005-01-16 05:06:12 +0000655 }
Chris Lattner3e928bb2005-01-07 07:47:09 +0000656 }
657 break;
658 }
Chris Lattner040c11c2005-11-09 18:48:57 +0000659 case ISD::TokenFactor:
660 if (Node->getNumOperands() == 2) {
661 bool Changed = false;
662 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
663 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
664 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
665 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
666 } else {
667 std::vector<SDOperand> Ops;
668 bool Changed = false;
669 // Legalize the operands.
670 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
671 SDOperand Op = Node->getOperand(i);
672 Ops.push_back(LegalizeOp(Op));
673 Changed |= Ops[i] != Op;
674 }
675 if (Changed)
676 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattnera385e9b2005-01-13 17:59:25 +0000677 }
Chris Lattnera385e9b2005-01-13 17:59:25 +0000678 break;
Chris Lattnera385e9b2005-01-13 17:59:25 +0000679
Chris Lattner16cd04d2005-05-12 23:24:06 +0000680 case ISD::CALLSEQ_START:
681 case ISD::CALLSEQ_END:
Chris Lattner3e928bb2005-01-07 07:47:09 +0000682 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner128b52d2005-05-12 23:24:44 +0000683 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattner45982da2005-05-12 16:53:42 +0000684 Tmp2 = Node->getOperand(0);
Nate Begeman1aa19722005-10-04 02:10:55 +0000685 if (Tmp1 != Tmp2)
Chris Lattner88de6e72005-05-12 00:17:04 +0000686 Node->setAdjCallChain(Tmp1);
Nate Begeman27d404c2005-10-04 00:37:37 +0000687
Chris Lattner16cd04d2005-05-12 23:24:06 +0000688 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner88de6e72005-05-12 00:17:04 +0000689 // nodes are treated specially and are mutated in place. This makes the dag
690 // legalization process more efficient and also makes libcall insertion
691 // easier.
Chris Lattner3e928bb2005-01-07 07:47:09 +0000692 break;
Chris Lattnerfa404e82005-01-09 19:03:49 +0000693 case ISD::DYNAMIC_STACKALLOC:
694 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
695 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
696 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
697 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattneradf6c2a2005-05-14 07:29:57 +0000698 Tmp3 != Node->getOperand(2)) {
699 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
700 std::vector<SDOperand> Ops;
701 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
702 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
703 } else
Chris Lattner513e52e2005-01-09 19:07:54 +0000704 Result = Op.getValue(0);
Chris Lattnerfa404e82005-01-09 19:03:49 +0000705
706 // Since this op produces two values, make sure to remember that we
707 // legalized both of them.
708 AddLegalizedOperand(SDOperand(Node, 0), Result);
709 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
710 return Result.getValue(Op.ResNo);
711
Chris Lattnerd71c0412005-05-13 18:43:43 +0000712 case ISD::TAILCALL:
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000713 case ISD::CALL: {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000714 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
715 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000716
717 bool Changed = false;
718 std::vector<SDOperand> Ops;
719 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
720 Ops.push_back(LegalizeOp(Node->getOperand(i)));
721 Changed |= Ops.back() != Node->getOperand(i);
722 }
723
724 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattner3e928bb2005-01-07 07:47:09 +0000725 std::vector<MVT::ValueType> RetTyVTs;
726 RetTyVTs.reserve(Node->getNumValues());
727 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerebda9422005-01-07 21:34:13 +0000728 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd71c0412005-05-13 18:43:43 +0000729 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
730 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner38d6be52005-01-09 19:43:23 +0000731 } else {
732 Result = Result.getValue(0);
Chris Lattner3e928bb2005-01-07 07:47:09 +0000733 }
Chris Lattner38d6be52005-01-09 19:43:23 +0000734 // Since calls produce multiple values, make sure to remember that we
735 // legalized all of them.
736 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
737 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
738 return Result.getValue(Op.ResNo);
Chris Lattner3d9dffc2005-01-19 20:24:35 +0000739 }
Chris Lattnerc7af1792005-01-07 22:12:08 +0000740 case ISD::BR:
741 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
742 if (Tmp1 != Node->getOperand(0))
743 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
744 break;
745
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000746 case ISD::BRCOND:
747 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman7cbd5252005-08-16 19:49:35 +0000748
Chris Lattner47e92232005-01-18 19:27:06 +0000749 switch (getTypeAction(Node->getOperand(1).getValueType())) {
750 case Expand: assert(0 && "It's impossible to expand bools");
751 case Legal:
752 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
753 break;
754 case Promote:
755 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
756 break;
757 }
Nate Begeman7cbd5252005-08-16 19:49:35 +0000758
759 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
760 default: assert(0 && "This action is not supported yet!");
761 case TargetLowering::Expand:
762 // Expand brcond's setcc into its constituent parts and create a BR_CC
763 // Node.
764 if (Tmp2.getOpcode() == ISD::SETCC) {
765 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
766 Tmp2.getOperand(0), Tmp2.getOperand(1),
767 Node->getOperand(2));
768 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +0000769 // Make sure the condition is either zero or one. It may have been
770 // promoted from something else.
771 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
772
Nate Begeman7cbd5252005-08-16 19:49:35 +0000773 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
774 DAG.getCondCode(ISD::SETNE), Tmp2,
775 DAG.getConstant(0, Tmp2.getValueType()),
776 Node->getOperand(2));
777 }
778 break;
779 case TargetLowering::Legal:
780 // Basic block destination (Op#2) is always legal.
781 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
782 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
783 Node->getOperand(2));
784 break;
785 }
786 break;
787 case ISD::BR_CC:
788 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
789
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000790 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000791 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
792 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
793 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
794 Tmp3 != Node->getOperand(3)) {
795 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
796 Tmp2, Tmp3, Node->getOperand(4));
797 }
798 break;
799 } else {
800 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
801 Node->getOperand(2), // LHS
802 Node->getOperand(3), // RHS
803 Node->getOperand(1)));
804 // If we get a SETCC back from legalizing the SETCC node we just
805 // created, then use its LHS, RHS, and CC directly in creating a new
806 // node. Otherwise, select between the true and false value based on
807 // comparing the result of the legalized with zero.
808 if (Tmp2.getOpcode() == ISD::SETCC) {
809 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
810 Tmp2.getOperand(0), Tmp2.getOperand(1),
811 Node->getOperand(4));
812 } else {
813 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
814 DAG.getCondCode(ISD::SETNE),
815 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
816 Node->getOperand(4));
817 }
818 }
Chris Lattnerc18ae4c2005-01-07 08:19:42 +0000819 break;
Chris Lattner411e8882005-04-09 03:30:19 +0000820 case ISD::BRCONDTWOWAY:
821 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
822 switch (getTypeAction(Node->getOperand(1).getValueType())) {
823 case Expand: assert(0 && "It's impossible to expand bools");
824 case Legal:
825 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
826 break;
827 case Promote:
828 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
829 break;
830 }
831 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
832 // pair.
833 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
834 case TargetLowering::Promote:
835 default: assert(0 && "This action is not supported yet!");
836 case TargetLowering::Legal:
837 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
838 std::vector<SDOperand> Ops;
839 Ops.push_back(Tmp1);
840 Ops.push_back(Tmp2);
841 Ops.push_back(Node->getOperand(2));
842 Ops.push_back(Node->getOperand(3));
843 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
844 }
845 break;
846 case TargetLowering::Expand:
Nate Begeman7cbd5252005-08-16 19:49:35 +0000847 // If BRTWOWAY_CC is legal for this target, then simply expand this node
848 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
849 // BRCOND/BR pair.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000850 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000851 if (Tmp2.getOpcode() == ISD::SETCC) {
852 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
853 Tmp2.getOperand(0), Tmp2.getOperand(1),
854 Node->getOperand(2), Node->getOperand(3));
855 } else {
856 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
857 DAG.getConstant(0, Tmp2.getValueType()),
858 Node->getOperand(2), Node->getOperand(3));
859 }
860 } else {
861 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattner411e8882005-04-09 03:30:19 +0000862 Node->getOperand(2));
Nate Begeman7cbd5252005-08-16 19:49:35 +0000863 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
864 }
Chris Lattner411e8882005-04-09 03:30:19 +0000865 break;
866 }
867 break;
Nate Begeman7cbd5252005-08-16 19:49:35 +0000868 case ISD::BRTWOWAY_CC:
869 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerc9c60f62005-08-24 16:35:28 +0000870 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman7cbd5252005-08-16 19:49:35 +0000871 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
872 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
873 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
874 Tmp3 != Node->getOperand(3)) {
875 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
876 Node->getOperand(4), Node->getOperand(5));
877 }
878 break;
879 } else {
880 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
881 Node->getOperand(2), // LHS
882 Node->getOperand(3), // RHS
883 Node->getOperand(1)));
884 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
885 // pair.
886 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
887 default: assert(0 && "This action is not supported yet!");
888 case TargetLowering::Legal:
889 // If we get a SETCC back from legalizing the SETCC node we just
890 // created, then use its LHS, RHS, and CC directly in creating a new
891 // node. Otherwise, select between the true and false value based on
892 // comparing the result of the legalized with zero.
893 if (Tmp2.getOpcode() == ISD::SETCC) {
894 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
895 Tmp2.getOperand(0), Tmp2.getOperand(1),
896 Node->getOperand(4), Node->getOperand(5));
897 } else {
898 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
899 DAG.getConstant(0, Tmp2.getValueType()),
900 Node->getOperand(4), Node->getOperand(5));
901 }
902 break;
903 case TargetLowering::Expand:
904 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
905 Node->getOperand(4));
906 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
907 break;
908 }
909 }
910 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +0000911 case ISD::LOAD:
912 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
913 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +0000914
Chris Lattner3e928bb2005-01-07 07:47:09 +0000915 if (Tmp1 != Node->getOperand(0) ||
916 Tmp2 != Node->getOperand(1))
Chris Lattner52d08bd2005-05-09 20:23:03 +0000917 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
918 Node->getOperand(2));
Chris Lattner8afc48e2005-01-07 22:28:47 +0000919 else
920 Result = SDOperand(Node, 0);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000921
Chris Lattner8afc48e2005-01-07 22:28:47 +0000922 // Since loads produce two values, make sure to remember that we legalized
923 // both of them.
924 AddLegalizedOperand(SDOperand(Node, 0), Result);
925 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
926 return Result.getValue(Op.ResNo);
Nate Begeman5fbb5d22005-11-19 00:36:38 +0000927
928 case ISD::VLOAD:
929 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
930 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
931
932 // If we just have one element, scalarize the result. Otherwise, check to
933 // see if we support this operation on this type at this width. If not,
934 // split the vector in half and try again.
935 if (1 == cast<ConstantSDNode>(Node->getOperand(2))->getValue()) {
936 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
937 Result = LegalizeOp(DAG.getLoad(SVT, Tmp1, Tmp2, Node->getOperand(4)));
938 } else {
939 assert(0 && "Expand case for vectors unimplemented");
940 }
941
942 // Since loads produce two values, make sure to remember that we legalized
943 // both of them.
944 AddLegalizedOperand(SDOperand(Node, 0), Result);
945 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
946 return Result.getValue(Op.ResNo);
947
Chris Lattner0f69b292005-01-15 06:18:18 +0000948 case ISD::EXTLOAD:
949 case ISD::SEXTLOAD:
Chris Lattner01ff7212005-04-10 22:54:25 +0000950 case ISD::ZEXTLOAD: {
Chris Lattner0f69b292005-01-15 06:18:18 +0000951 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
952 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner0f69b292005-01-15 06:18:18 +0000953
Chris Lattner5f056bf2005-07-10 01:55:33 +0000954 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattner01ff7212005-04-10 22:54:25 +0000955 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattner01ff7212005-04-10 22:54:25 +0000956 default: assert(0 && "This action is not supported yet!");
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000957 case TargetLowering::Promote:
958 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattner5f056bf2005-07-10 01:55:33 +0000959 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
960 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner1c51c6a2005-04-12 20:30:10 +0000961 // Since loads produce two values, make sure to remember that we legalized
962 // both of them.
963 AddLegalizedOperand(SDOperand(Node, 0), Result);
964 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
965 return Result.getValue(Op.ResNo);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000966
Chris Lattner01ff7212005-04-10 22:54:25 +0000967 case TargetLowering::Legal:
968 if (Tmp1 != Node->getOperand(0) ||
969 Tmp2 != Node->getOperand(1))
Chris Lattner5f056bf2005-07-10 01:55:33 +0000970 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
971 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +0000972 else
973 Result = SDOperand(Node, 0);
974
975 // Since loads produce two values, make sure to remember that we legalized
976 // both of them.
977 AddLegalizedOperand(SDOperand(Node, 0), Result);
978 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
979 return Result.getValue(Op.ResNo);
Chris Lattner01ff7212005-04-10 22:54:25 +0000980 case TargetLowering::Expand:
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000981 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
982 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
983 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth31559082005-06-30 19:32:57 +0000984 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharth9d416f72005-06-30 19:22:37 +0000985 if (Op.ResNo)
986 return Load.getValue(1);
987 return Result;
988 }
Chris Lattner01ff7212005-04-10 22:54:25 +0000989 assert(Node->getOpcode() != ISD::EXTLOAD &&
990 "EXTLOAD should always be supported!");
991 // Turn the unsupported load into an EXTLOAD followed by an explicit
992 // zero/sign extend inreg.
Chris Lattner5f056bf2005-07-10 01:55:33 +0000993 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
994 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner23993562005-04-13 02:38:47 +0000995 SDOperand ValRes;
996 if (Node->getOpcode() == ISD::SEXTLOAD)
997 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +0000998 Result, DAG.getValueType(SrcVT));
Chris Lattner23993562005-04-13 02:38:47 +0000999 else
1000 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattner01ff7212005-04-10 22:54:25 +00001001 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1002 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1003 if (Op.ResNo)
1004 return Result.getValue(1);
1005 return ValRes;
1006 }
1007 assert(0 && "Unreachable");
1008 }
Nate Begeman5dc897b2005-10-19 00:06:56 +00001009 case ISD::EXTRACT_ELEMENT: {
1010 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1011 switch (getTypeAction(OpTy)) {
1012 default:
1013 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1014 break;
1015 case Legal:
1016 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1017 // 1 -> Hi
1018 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1019 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1020 TLI.getShiftAmountTy()));
1021 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1022 } else {
1023 // 0 -> Lo
1024 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1025 Node->getOperand(0));
1026 }
1027 Result = LegalizeOp(Result);
1028 break;
1029 case Expand:
1030 // Get both the low and high parts.
1031 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1032 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1033 Result = Tmp2; // 1 -> Hi
1034 else
1035 Result = Tmp1; // 0 -> Lo
1036 break;
1037 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001038 break;
Nate Begeman5dc897b2005-10-19 00:06:56 +00001039 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001040
1041 case ISD::CopyToReg:
1042 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukmanedf128a2005-04-21 22:36:52 +00001043
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001044 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00001045 "Register type must be legal!");
1046 // Legalize the incoming value (must be legal).
1047 Tmp2 = LegalizeOp(Node->getOperand(2));
1048 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1049 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1050 Node->getOperand(1), Tmp2);
Chris Lattner3e928bb2005-01-07 07:47:09 +00001051 break;
1052
1053 case ISD::RET:
1054 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1055 switch (Node->getNumOperands()) {
1056 case 2: // ret val
1057 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1058 case Legal:
1059 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner8afc48e2005-01-07 22:28:47 +00001060 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattner3e928bb2005-01-07 07:47:09 +00001061 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1062 break;
1063 case Expand: {
1064 SDOperand Lo, Hi;
1065 ExpandOp(Node->getOperand(1), Lo, Hi);
1066 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001067 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001068 }
1069 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001070 Tmp2 = PromoteOp(Node->getOperand(1));
1071 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1072 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001073 }
1074 break;
1075 case 1: // ret void
1076 if (Tmp1 != Node->getOperand(0))
1077 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1078 break;
1079 default: { // ret <values>
1080 std::vector<SDOperand> NewValues;
1081 NewValues.push_back(Tmp1);
1082 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1083 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1084 case Legal:
Chris Lattner4e6c7462005-01-08 19:27:05 +00001085 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001086 break;
1087 case Expand: {
1088 SDOperand Lo, Hi;
1089 ExpandOp(Node->getOperand(i), Lo, Hi);
1090 NewValues.push_back(Lo);
1091 NewValues.push_back(Hi);
Misha Brukmanedf128a2005-04-21 22:36:52 +00001092 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001093 }
1094 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001095 assert(0 && "Can't promote multiple return value yet!");
Chris Lattner3e928bb2005-01-07 07:47:09 +00001096 }
1097 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1098 break;
1099 }
1100 }
1101 break;
1102 case ISD::STORE:
1103 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1104 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1105
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001106 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner03c85462005-01-15 05:21:40 +00001107 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001108 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001109 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001110 DAG.getConstant(FloatToBits(CFP->getValue()),
1111 MVT::i32),
1112 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001113 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001114 } else {
1115 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen00b168892005-07-27 06:12:32 +00001116 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeycb6682f2005-08-17 19:34:49 +00001117 DAG.getConstant(DoubleToBits(CFP->getValue()),
1118 MVT::i64),
1119 Tmp2,
Chris Lattner52d08bd2005-05-09 20:23:03 +00001120 Node->getOperand(3));
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001121 }
Chris Lattner84734ce2005-02-22 07:23:39 +00001122 Node = Result.Val;
Chris Lattner5d2c6c72005-01-08 06:25:56 +00001123 }
1124
Chris Lattner3e928bb2005-01-07 07:47:09 +00001125 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1126 case Legal: {
1127 SDOperand Val = LegalizeOp(Node->getOperand(1));
1128 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1129 Tmp2 != Node->getOperand(2))
Chris Lattner52d08bd2005-05-09 20:23:03 +00001130 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1131 Node->getOperand(3));
Chris Lattner3e928bb2005-01-07 07:47:09 +00001132 break;
1133 }
1134 case Promote:
Chris Lattner03c85462005-01-15 05:21:40 +00001135 // Truncate the value and store the result.
1136 Tmp3 = PromoteOp(Node->getOperand(1));
1137 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001138 Node->getOperand(3),
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001139 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner03c85462005-01-15 05:21:40 +00001140 break;
1141
Chris Lattner3e928bb2005-01-07 07:47:09 +00001142 case Expand:
1143 SDOperand Lo, Hi;
1144 ExpandOp(Node->getOperand(1), Lo, Hi);
1145
1146 if (!TLI.isLittleEndian())
1147 std::swap(Lo, Hi);
1148
Chris Lattneredb1add2005-05-11 04:51:16 +00001149 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1150 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001151 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001152 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1153 getIntPtrConstant(IncrementSize));
1154 assert(isTypeLegal(Tmp2.getValueType()) &&
1155 "Pointers must be legal!");
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00001156 //Again, claiming both parts of the store came form the same Instr
Chris Lattneredb1add2005-05-11 04:51:16 +00001157 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1158 Node->getOperand(3));
Chris Lattnerec39a452005-01-19 18:02:17 +00001159 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1160 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001161 }
1162 break;
Andrew Lenharth95762122005-03-31 21:24:06 +00001163 case ISD::PCMARKER:
1164 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner2c8086f2005-04-02 05:00:07 +00001165 if (Tmp1 != Node->getOperand(0))
1166 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharth95762122005-03-31 21:24:06 +00001167 break;
Andrew Lenharth51b8d542005-11-11 16:47:30 +00001168 case ISD::READCYCLECOUNTER:
1169 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
1170 if (Tmp1 != Node->getOperand(0))
1171 Result = DAG.getNode(ISD::READCYCLECOUNTER, MVT::i64, Tmp1);
1172 break;
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00001173
Chris Lattner0f69b292005-01-15 06:18:18 +00001174 case ISD::TRUNCSTORE:
1175 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1176 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1177
1178 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1179 case Legal:
1180 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner13d58e72005-09-10 00:20:18 +00001181
1182 // The only promote case we handle is TRUNCSTORE:i1 X into
1183 // -> TRUNCSTORE:i8 (and X, 1)
1184 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1185 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1186 TargetLowering::Promote) {
1187 // Promote the bool to a mask then store.
1188 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1189 DAG.getConstant(1, Tmp2.getValueType()));
1190 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1191 Node->getOperand(3), DAG.getValueType(MVT::i8));
1192
1193 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1194 Tmp3 != Node->getOperand(2)) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00001195 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00001196 Node->getOperand(3), Node->getOperand(4));
Chris Lattner13d58e72005-09-10 00:20:18 +00001197 }
Chris Lattner0f69b292005-01-15 06:18:18 +00001198 break;
1199 case Promote:
1200 case Expand:
1201 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1202 }
1203 break;
Chris Lattner2ee743f2005-01-14 22:08:15 +00001204 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00001205 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1206 case Expand: assert(0 && "It's impossible to expand bools");
1207 case Legal:
1208 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1209 break;
1210 case Promote:
1211 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1212 break;
1213 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001214 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner2ee743f2005-01-14 22:08:15 +00001215 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001216
Nate Begemanb942a3d2005-08-23 04:29:48 +00001217 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001218 default: assert(0 && "This action is not supported yet!");
Nate Begeman9373a812005-08-10 20:51:12 +00001219 case TargetLowering::Expand:
1220 if (Tmp1.getOpcode() == ISD::SETCC) {
1221 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1222 Tmp2, Tmp3,
1223 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1224 } else {
Chris Lattner550b1e52005-08-21 18:03:09 +00001225 // Make sure the condition is either zero or one. It may have been
1226 // promoted from something else.
1227 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begeman9373a812005-08-10 20:51:12 +00001228 Result = DAG.getSelectCC(Tmp1,
1229 DAG.getConstant(0, Tmp1.getValueType()),
1230 Tmp2, Tmp3, ISD::SETNE);
1231 }
1232 break;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001233 case TargetLowering::Legal:
1234 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1235 Tmp3 != Node->getOperand(2))
1236 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1237 Tmp1, Tmp2, Tmp3);
1238 break;
1239 case TargetLowering::Promote: {
1240 MVT::ValueType NVT =
1241 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1242 unsigned ExtOp, TruncOp;
1243 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner13c78e22005-09-02 00:18:10 +00001244 ExtOp = ISD::ANY_EXTEND;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001245 TruncOp = ISD::TRUNCATE;
1246 } else {
1247 ExtOp = ISD::FP_EXTEND;
1248 TruncOp = ISD::FP_ROUND;
1249 }
1250 // Promote each of the values to the new type.
1251 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1252 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1253 // Perform the larger operation, then round down.
1254 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1255 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1256 break;
1257 }
1258 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001259 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001260 case ISD::SELECT_CC:
1261 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1262 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1263
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001264 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner23004e52005-08-26 00:23:59 +00001265 // Everything is legal, see if we should expand this op or something.
1266 switch (TLI.getOperationAction(ISD::SELECT_CC,
1267 Node->getOperand(0).getValueType())) {
1268 default: assert(0 && "This action is not supported yet!");
1269 case TargetLowering::Custom: {
1270 SDOperand Tmp =
1271 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1272 Node->getOperand(0),
1273 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerd7050a92005-08-26 00:43:46 +00001274 Node->getOperand(4)), DAG);
Chris Lattner23004e52005-08-26 00:23:59 +00001275 if (Tmp.Val) {
1276 Result = LegalizeOp(Tmp);
1277 break;
1278 }
1279 } // FALLTHROUGH if the target can't lower this operation after all.
1280 case TargetLowering::Legal:
1281 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1282 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1283 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1284 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1285 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1286 Tmp3, Tmp4, Node->getOperand(4));
1287 }
1288 break;
Nate Begeman9373a812005-08-10 20:51:12 +00001289 }
1290 break;
1291 } else {
1292 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1293 Node->getOperand(0), // LHS
1294 Node->getOperand(1), // RHS
1295 Node->getOperand(4)));
Nate Begeman7cbd5252005-08-16 19:49:35 +00001296 // If we get a SETCC back from legalizing the SETCC node we just
1297 // created, then use its LHS, RHS, and CC directly in creating a new
1298 // node. Otherwise, select between the true and false value based on
1299 // comparing the result of the legalized with zero.
1300 if (Tmp1.getOpcode() == ISD::SETCC) {
1301 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1302 Tmp1.getOperand(0), Tmp1.getOperand(1),
1303 Tmp3, Tmp4, Tmp1.getOperand(2));
1304 } else {
1305 Result = DAG.getSelectCC(Tmp1,
1306 DAG.getConstant(0, Tmp1.getValueType()),
1307 Tmp3, Tmp4, ISD::SETNE);
1308 }
Nate Begeman9373a812005-08-10 20:51:12 +00001309 }
1310 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001311 case ISD::SETCC:
1312 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1313 case Legal:
1314 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1315 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattner3e928bb2005-01-07 07:47:09 +00001316 break;
1317 case Promote:
Chris Lattner8b6fa222005-01-15 22:16:26 +00001318 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1319 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1320
1321 // If this is an FP compare, the operands have already been extended.
1322 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1323 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00001324 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001325
1326 // Otherwise, we have to insert explicit sign or zero extends. Note
1327 // that we could insert sign extends for ALL conditions, but zero extend
1328 // is cheaper on many machines (an AND instead of two shifts), so prefer
1329 // it.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001330 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner8b6fa222005-01-15 22:16:26 +00001331 default: assert(0 && "Unknown integer comparison!");
1332 case ISD::SETEQ:
1333 case ISD::SETNE:
1334 case ISD::SETUGE:
1335 case ISD::SETUGT:
1336 case ISD::SETULE:
1337 case ISD::SETULT:
1338 // ALL of these operations will work if we either sign or zero extend
1339 // the operands (including the unsigned comparisons!). Zero extend is
1340 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner23993562005-04-13 02:38:47 +00001341 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1342 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00001343 break;
1344 case ISD::SETGE:
1345 case ISD::SETGT:
1346 case ISD::SETLT:
1347 case ISD::SETLE:
Chris Lattner15e4b012005-07-10 00:07:11 +00001348 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1349 DAG.getValueType(VT));
1350 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1351 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00001352 break;
1353 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00001354 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001355 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001356 case Expand:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001357 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1358 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1359 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001360 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001361 case ISD::SETEQ:
1362 case ISD::SETNE:
Chris Lattner08b698e2005-04-12 01:46:05 +00001363 if (RHSLo == RHSHi)
1364 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1365 if (RHSCST->isAllOnesValue()) {
1366 // Comparison to -1.
1367 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001368 Tmp2 = RHSLo;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001369 break;
Chris Lattner08b698e2005-04-12 01:46:05 +00001370 }
1371
Chris Lattner3e928bb2005-01-07 07:47:09 +00001372 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1373 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1374 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001375 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattner3e928bb2005-01-07 07:47:09 +00001376 break;
1377 default:
Chris Lattner5b95ed62005-04-12 02:19:10 +00001378 // If this is a comparison of the sign bit, just look at the top part.
1379 // X > -1, x < 0
1380 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001381 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattner5b95ed62005-04-12 02:19:10 +00001382 CST->getValue() == 0) || // X < 0
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001383 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begemanb942a3d2005-08-23 04:29:48 +00001384 (CST->isAllOnesValue()))) { // X > -1
1385 Tmp1 = LHSHi;
1386 Tmp2 = RHSHi;
1387 break;
1388 }
Chris Lattner5b95ed62005-04-12 02:19:10 +00001389
Chris Lattner3e928bb2005-01-07 07:47:09 +00001390 // FIXME: This generated code sucks.
1391 ISD::CondCode LowCC;
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001392 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner3e928bb2005-01-07 07:47:09 +00001393 default: assert(0 && "Unknown integer setcc!");
1394 case ISD::SETLT:
1395 case ISD::SETULT: LowCC = ISD::SETULT; break;
1396 case ISD::SETGT:
1397 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1398 case ISD::SETLE:
1399 case ISD::SETULE: LowCC = ISD::SETULE; break;
1400 case ISD::SETGE:
1401 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1402 }
Misha Brukmanedf128a2005-04-21 22:36:52 +00001403
Chris Lattner3e928bb2005-01-07 07:47:09 +00001404 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1405 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1406 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1407
1408 // NOTE: on targets without efficient SELECT of bools, we can always use
1409 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001410 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1411 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1412 Node->getOperand(2));
1413 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begemanb942a3d2005-08-23 04:29:48 +00001414 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1415 Result, Tmp1, Tmp2));
1416 return Result;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001417 }
1418 }
Nate Begemanb942a3d2005-08-23 04:29:48 +00001419
1420 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1421 default:
1422 assert(0 && "Cannot handle this action for SETCC yet!");
1423 break;
Andrew Lenharth5e3efbc2005-08-29 20:46:51 +00001424 case TargetLowering::Promote:
1425 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1426 Node->getOperand(2));
1427 break;
Nate Begemanb942a3d2005-08-23 04:29:48 +00001428 case TargetLowering::Legal:
1429 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1430 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1431 Node->getOperand(2));
1432 break;
1433 case TargetLowering::Expand:
1434 // Expand a setcc node into a select_cc of the same condition, lhs, and
1435 // rhs that selects between const 1 (true) and const 0 (false).
1436 MVT::ValueType VT = Node->getValueType(0);
1437 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1438 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1439 Node->getOperand(2));
1440 Result = LegalizeOp(Result);
1441 break;
1442 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001443 break;
1444
Chris Lattnere1bd8222005-01-11 05:57:22 +00001445 case ISD::MEMSET:
1446 case ISD::MEMCPY:
1447 case ISD::MEMMOVE: {
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001448 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnere5605212005-01-28 22:29:18 +00001449 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1450
1451 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1452 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1453 case Expand: assert(0 && "Cannot expand a byte!");
1454 case Legal:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001455 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001456 break;
1457 case Promote:
Chris Lattnerdeb692e2005-02-01 18:38:28 +00001458 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnere5605212005-01-28 22:29:18 +00001459 break;
1460 }
1461 } else {
Misha Brukmanedf128a2005-04-21 22:36:52 +00001462 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnere5605212005-01-28 22:29:18 +00001463 }
Chris Lattner272455b2005-02-02 03:44:41 +00001464
1465 SDOperand Tmp4;
1466 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattner6814f152005-07-13 01:42:45 +00001467 case Expand: {
1468 // Length is too big, just take the lo-part of the length.
1469 SDOperand HiPart;
1470 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1471 break;
1472 }
Chris Lattnere5605212005-01-28 22:29:18 +00001473 case Legal:
1474 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnere5605212005-01-28 22:29:18 +00001475 break;
1476 case Promote:
1477 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner272455b2005-02-02 03:44:41 +00001478 break;
1479 }
1480
1481 SDOperand Tmp5;
1482 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1483 case Expand: assert(0 && "Cannot expand this yet!");
1484 case Legal:
1485 Tmp5 = LegalizeOp(Node->getOperand(4));
1486 break;
1487 case Promote:
Chris Lattnere5605212005-01-28 22:29:18 +00001488 Tmp5 = PromoteOp(Node->getOperand(4));
1489 break;
1490 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001491
1492 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1493 default: assert(0 && "This action not implemented for this operation!");
Chris Lattner07dffd62005-08-26 00:14:16 +00001494 case TargetLowering::Custom: {
1495 SDOperand Tmp =
1496 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1497 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1498 if (Tmp.Val) {
1499 Result = LegalizeOp(Tmp);
1500 break;
1501 }
1502 // FALLTHROUGH if the target thinks it is legal.
1503 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001504 case TargetLowering::Legal:
Chris Lattnere1bd8222005-01-11 05:57:22 +00001505 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1506 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1507 Tmp5 != Node->getOperand(4)) {
1508 std::vector<SDOperand> Ops;
1509 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1510 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1511 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1512 }
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001513 break;
1514 case TargetLowering::Expand: {
Chris Lattnere1bd8222005-01-11 05:57:22 +00001515 // Otherwise, the target does not support this operation. Lower the
1516 // operation to an explicit libcall as appropriate.
1517 MVT::ValueType IntPtr = TLI.getPointerTy();
1518 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1519 std::vector<std::pair<SDOperand, const Type*> > Args;
1520
Reid Spencer3bfbf4e2005-01-12 14:53:45 +00001521 const char *FnName = 0;
Chris Lattnere1bd8222005-01-11 05:57:22 +00001522 if (Node->getOpcode() == ISD::MEMSET) {
1523 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1524 // Extend the ubyte argument to be an int value for the call.
1525 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1526 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1527 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1528
1529 FnName = "memset";
1530 } else if (Node->getOpcode() == ISD::MEMCPY ||
1531 Node->getOpcode() == ISD::MEMMOVE) {
1532 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1533 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1534 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1535 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1536 } else {
1537 assert(0 && "Unknown op!");
1538 }
Chris Lattner45982da2005-05-12 16:53:42 +00001539
Chris Lattnere1bd8222005-01-11 05:57:22 +00001540 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00001541 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattnere1bd8222005-01-11 05:57:22 +00001542 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerc087a432005-07-13 02:00:04 +00001543 Result = CallResult.second;
1544 NeedsAnotherIteration = true;
Chris Lattner55ba8fb2005-01-16 07:29:19 +00001545 break;
1546 }
Chris Lattnere1bd8222005-01-11 05:57:22 +00001547 }
1548 break;
1549 }
Chris Lattner52d08bd2005-05-09 20:23:03 +00001550
1551 case ISD::READPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001552 Tmp1 = LegalizeOp(Node->getOperand(0));
1553 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001554
Chris Lattner3e011362005-05-14 07:45:46 +00001555 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1556 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1557 std::vector<SDOperand> Ops;
1558 Ops.push_back(Tmp1);
1559 Ops.push_back(Tmp2);
1560 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1561 } else
Chris Lattner52d08bd2005-05-09 20:23:03 +00001562 Result = SDOperand(Node, 0);
1563 // Since these produce two values, make sure to remember that we legalized
1564 // both of them.
1565 AddLegalizedOperand(SDOperand(Node, 0), Result);
1566 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1567 return Result.getValue(Op.ResNo);
Chris Lattner52d08bd2005-05-09 20:23:03 +00001568 case ISD::WRITEPORT:
Chris Lattner52d08bd2005-05-09 20:23:03 +00001569 Tmp1 = LegalizeOp(Node->getOperand(0));
1570 Tmp2 = LegalizeOp(Node->getOperand(1));
1571 Tmp3 = LegalizeOp(Node->getOperand(2));
1572 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1573 Tmp3 != Node->getOperand(2))
1574 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1575 break;
1576
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001577 case ISD::READIO:
1578 Tmp1 = LegalizeOp(Node->getOperand(0));
1579 Tmp2 = LegalizeOp(Node->getOperand(1));
1580
1581 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1582 case TargetLowering::Custom:
1583 default: assert(0 && "This action not implemented for this operation!");
1584 case TargetLowering::Legal:
Chris Lattner3e011362005-05-14 07:45:46 +00001585 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1586 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1587 std::vector<SDOperand> Ops;
1588 Ops.push_back(Tmp1);
1589 Ops.push_back(Tmp2);
1590 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1591 } else
Chris Lattner6d5b8e12005-05-09 20:36:57 +00001592 Result = SDOperand(Node, 0);
1593 break;
1594 case TargetLowering::Expand:
1595 // Replace this with a load from memory.
1596 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1597 Node->getOperand(1), DAG.getSrcValue(NULL));
1598 Result = LegalizeOp(Result);
1599 break;
1600 }
1601
1602 // Since these produce two values, make sure to remember that we legalized
1603 // both of them.
1604 AddLegalizedOperand(SDOperand(Node, 0), Result);
1605 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1606 return Result.getValue(Op.ResNo);
1607
1608 case ISD::WRITEIO:
1609 Tmp1 = LegalizeOp(Node->getOperand(0));
1610 Tmp2 = LegalizeOp(Node->getOperand(1));
1611 Tmp3 = LegalizeOp(Node->getOperand(2));
1612
1613 switch (TLI.getOperationAction(Node->getOpcode(),
1614 Node->getOperand(1).getValueType())) {
1615 case TargetLowering::Custom:
1616 default: assert(0 && "This action not implemented for this operation!");
1617 case TargetLowering::Legal:
1618 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1619 Tmp3 != Node->getOperand(2))
1620 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1621 break;
1622 case TargetLowering::Expand:
1623 // Replace this with a store to memory.
1624 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1625 Node->getOperand(1), Node->getOperand(2),
1626 DAG.getSrcValue(NULL));
1627 Result = LegalizeOp(Result);
1628 break;
1629 }
1630 break;
1631
Chris Lattner84f67882005-01-20 18:52:28 +00001632 case ISD::ADD_PARTS:
Chris Lattner5b359c62005-04-02 04:00:59 +00001633 case ISD::SUB_PARTS:
1634 case ISD::SHL_PARTS:
1635 case ISD::SRA_PARTS:
1636 case ISD::SRL_PARTS: {
Chris Lattner84f67882005-01-20 18:52:28 +00001637 std::vector<SDOperand> Ops;
1638 bool Changed = false;
1639 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1640 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1641 Changed |= Ops.back() != Node->getOperand(i);
1642 }
Chris Lattnere89083a2005-05-14 07:25:05 +00001643 if (Changed) {
1644 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1645 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1646 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001647
1648 // Since these produce multiple values, make sure to remember that we
1649 // legalized all of them.
1650 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1651 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1652 return Result.getValue(Op.ResNo);
Chris Lattner84f67882005-01-20 18:52:28 +00001653 }
Chris Lattner2c8086f2005-04-02 05:00:07 +00001654
1655 // Binary operators
Chris Lattner3e928bb2005-01-07 07:47:09 +00001656 case ISD::ADD:
1657 case ISD::SUB:
1658 case ISD::MUL:
Nate Begemanc7c16572005-04-11 03:01:51 +00001659 case ISD::MULHS:
1660 case ISD::MULHU:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001661 case ISD::UDIV:
1662 case ISD::SDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001663 case ISD::AND:
1664 case ISD::OR:
1665 case ISD::XOR:
Chris Lattner03c0cf82005-01-07 21:45:56 +00001666 case ISD::SHL:
1667 case ISD::SRL:
1668 case ISD::SRA:
Chris Lattner01b3d732005-09-28 22:28:18 +00001669 case ISD::FADD:
1670 case ISD::FSUB:
1671 case ISD::FMUL:
1672 case ISD::FDIV:
Chris Lattner3e928bb2005-01-07 07:47:09 +00001673 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharthf2eb1392005-07-05 19:52:39 +00001674 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1675 case Expand: assert(0 && "Not possible");
1676 case Legal:
1677 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1678 break;
1679 case Promote:
1680 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1681 break;
1682 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00001683 if (Tmp1 != Node->getOperand(0) ||
1684 Tmp2 != Node->getOperand(1))
1685 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1686 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00001687
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001688 // Vector binary operators
1689 case ISD::VADD:
1690 case ISD::VSUB:
1691 case ISD::VMUL: {
1692 Tmp1 = Node->getOperand(0); // Element Count
1693 Tmp2 = Node->getOperand(1); // Element Type
1694
1695 // If we just have one element, scalarize the result. Otherwise, check to
1696 // see if we support this operation on this type at this width. If not,
1697 // split the vector in half and try again.
1698 if (1 == cast<ConstantSDNode>(Tmp1)->getValue()) {
1699 MVT::ValueType SVT = cast<VTSDNode>(Tmp2)->getVT();
1700
Chris Lattnerb89175f2005-11-19 05:51:46 +00001701 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), SVT), SVT,
Nate Begeman5fbb5d22005-11-19 00:36:38 +00001702 LegalizeOp(Node->getOperand(2)),
1703 LegalizeOp(Node->getOperand(3)));
1704 } else {
1705 assert(0 && "Expand case for vectors unimplemented");
1706 }
1707 break;
1708 }
1709
Nate Begeman419f8b62005-10-18 00:27:41 +00001710 case ISD::BUILD_PAIR: {
1711 MVT::ValueType PairTy = Node->getValueType(0);
1712 // TODO: handle the case where the Lo and Hi operands are not of legal type
1713 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1714 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1715 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1716 case TargetLowering::Legal:
1717 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1718 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1719 break;
1720 case TargetLowering::Promote:
1721 case TargetLowering::Custom:
1722 assert(0 && "Cannot promote/custom this yet!");
1723 case TargetLowering::Expand:
1724 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1725 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1726 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1727 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1728 TLI.getShiftAmountTy()));
1729 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1730 break;
1731 }
1732 break;
1733 }
1734
Nate Begemanc105e192005-04-06 00:23:54 +00001735 case ISD::UREM:
1736 case ISD::SREM:
Chris Lattner01b3d732005-09-28 22:28:18 +00001737 case ISD::FREM:
Nate Begemanc105e192005-04-06 00:23:54 +00001738 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1739 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1740 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1741 case TargetLowering::Legal:
1742 if (Tmp1 != Node->getOperand(0) ||
1743 Tmp2 != Node->getOperand(1))
Misha Brukmanedf128a2005-04-21 22:36:52 +00001744 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begemanc105e192005-04-06 00:23:54 +00001745 Tmp2);
1746 break;
1747 case TargetLowering::Promote:
1748 case TargetLowering::Custom:
1749 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner4c64dd72005-08-03 20:31:37 +00001750 case TargetLowering::Expand:
1751 if (MVT::isInteger(Node->getValueType(0))) {
1752 MVT::ValueType VT = Node->getValueType(0);
1753 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1754 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1755 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1756 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1757 } else {
1758 // Floating point mod -> fmod libcall.
1759 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1760 SDOperand Dummy;
1761 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begemanc105e192005-04-06 00:23:54 +00001762 }
1763 break;
1764 }
1765 break;
Chris Lattner2c8086f2005-04-02 05:00:07 +00001766
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001767 case ISD::CTPOP:
1768 case ISD::CTTZ:
1769 case ISD::CTLZ:
1770 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1771 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1772 case TargetLowering::Legal:
1773 if (Tmp1 != Node->getOperand(0))
1774 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1775 break;
1776 case TargetLowering::Promote: {
1777 MVT::ValueType OVT = Tmp1.getValueType();
1778 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattneredb1add2005-05-11 04:51:16 +00001779
1780 // Zero extend the argument.
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001781 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1782 // Perform the larger operation, then subtract if needed.
1783 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1784 switch(Node->getOpcode())
1785 {
1786 case ISD::CTPOP:
1787 Result = Tmp1;
1788 break;
1789 case ISD::CTTZ:
1790 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001791 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1792 DAG.getConstant(getSizeInBits(NVT), NVT),
1793 ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00001794 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001795 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1796 break;
1797 case ISD::CTLZ:
1798 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00001799 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1800 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001801 getSizeInBits(OVT), NVT));
1802 break;
1803 }
1804 break;
1805 }
1806 case TargetLowering::Custom:
1807 assert(0 && "Cannot custom handle this yet!");
1808 case TargetLowering::Expand:
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001809 switch(Node->getOpcode())
1810 {
1811 case ISD::CTPOP: {
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001812 static const uint64_t mask[6] = {
1813 0x5555555555555555ULL, 0x3333333333333333ULL,
1814 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1815 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1816 };
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001817 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001818 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1819 unsigned len = getSizeInBits(VT);
1820 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001821 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattnere3ef0a82005-05-11 05:21:31 +00001822 Tmp2 = DAG.getConstant(mask[i], VT);
1823 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001824 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001825 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1826 DAG.getNode(ISD::AND, VT,
1827 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1828 Tmp2));
1829 }
1830 Result = Tmp1;
1831 break;
1832 }
Duraid Madina57ff7e52005-05-11 08:45:08 +00001833 case ISD::CTLZ: {
1834 /* for now, we do this:
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001835 x = x | (x >> 1);
1836 x = x | (x >> 2);
1837 ...
1838 x = x | (x >>16);
Jeff Cohen00b168892005-07-27 06:12:32 +00001839 x = x | (x >>32); // for 64-bit input
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001840 return popcount(~x);
Jeff Cohen00b168892005-07-27 06:12:32 +00001841
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001842 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1843 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madina57ff7e52005-05-11 08:45:08 +00001844 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1845 unsigned len = getSizeInBits(VT);
1846 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1847 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001848 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madina57ff7e52005-05-11 08:45:08 +00001849 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1850 }
1851 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001852 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner18aa6802005-05-11 05:27:09 +00001853 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001854 }
1855 case ISD::CTTZ: {
Jeff Cohen00b168892005-07-27 06:12:32 +00001856 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001857 // unless the target has ctlz but not ctpop, in which case we use:
1858 // { return 32 - nlz(~x & (x-1)); }
1859 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001860 MVT::ValueType VT = Tmp1.getValueType();
1861 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen00b168892005-07-27 06:12:32 +00001862 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner5c33c9a2005-05-11 18:35:21 +00001863 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1864 DAG.getNode(ISD::SUB, VT, Tmp1,
1865 DAG.getConstant(1, VT)));
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001866 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerc9c60f62005-08-24 16:35:28 +00001867 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1868 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen00b168892005-07-27 06:12:32 +00001869 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begemand7c4a4a2005-05-11 23:43:56 +00001870 DAG.getConstant(getSizeInBits(VT), VT),
1871 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1872 } else {
1873 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1874 }
Chris Lattner18aa6802005-05-11 05:27:09 +00001875 break;
Duraid Madina57ff7e52005-05-11 08:45:08 +00001876 }
Andrew Lenharthded10bf2005-05-05 15:55:21 +00001877 default:
1878 assert(0 && "Cannot expand this yet!");
1879 break;
1880 }
Andrew Lenharth691ef2b2005-05-03 17:19:30 +00001881 break;
1882 }
1883 break;
Jeff Cohen00b168892005-07-27 06:12:32 +00001884
Chris Lattner2c8086f2005-04-02 05:00:07 +00001885 // Unary operators
1886 case ISD::FABS:
1887 case ISD::FNEG:
Chris Lattnerda6ba872005-04-28 21:44:33 +00001888 case ISD::FSQRT:
1889 case ISD::FSIN:
1890 case ISD::FCOS:
Chris Lattner2c8086f2005-04-02 05:00:07 +00001891 Tmp1 = LegalizeOp(Node->getOperand(0));
1892 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1893 case TargetLowering::Legal:
1894 if (Tmp1 != Node->getOperand(0))
1895 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1896 break;
1897 case TargetLowering::Promote:
1898 case TargetLowering::Custom:
1899 assert(0 && "Cannot promote/custom handle this yet!");
1900 case TargetLowering::Expand:
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001901 switch(Node->getOpcode()) {
1902 case ISD::FNEG: {
Chris Lattner2c8086f2005-04-02 05:00:07 +00001903 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1904 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner01b3d732005-09-28 22:28:18 +00001905 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner2c8086f2005-04-02 05:00:07 +00001906 Tmp2, Tmp1));
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001907 break;
1908 }
1909 case ISD::FABS: {
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001910 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1911 MVT::ValueType VT = Node->getValueType(0);
1912 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00001913 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001914 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1915 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1916 Result = LegalizeOp(Result);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001917 break;
1918 }
1919 case ISD::FSQRT:
1920 case ISD::FSIN:
1921 case ISD::FCOS: {
1922 MVT::ValueType VT = Node->getValueType(0);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001923 const char *FnName = 0;
1924 switch(Node->getOpcode()) {
1925 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1926 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1927 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1928 default: assert(0 && "Unreachable!");
1929 }
Nate Begeman2ac4fc02005-08-04 21:43:28 +00001930 SDOperand Dummy;
1931 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattnerf76e7dc2005-04-30 04:43:14 +00001932 break;
1933 }
1934 default:
Chris Lattner4af6e0d2005-04-02 05:26:37 +00001935 assert(0 && "Unreachable!");
Chris Lattner2c8086f2005-04-02 05:00:07 +00001936 }
1937 break;
1938 }
1939 break;
1940
1941 // Conversion operators. The source and destination have different types.
Chris Lattnerae0aacb2005-01-08 08:08:56 +00001942 case ISD::SINT_TO_FP:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001943 case ISD::UINT_TO_FP: {
1944 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattner3e928bb2005-01-07 07:47:09 +00001945 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1946 case Legal:
Jeff Cohend29b6aa2005-07-30 18:33:25 +00001947 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001948 Node->getOperand(0).getValueType())) {
1949 default: assert(0 && "Unknown operation action!");
1950 case TargetLowering::Expand:
Jim Laskey6269ed12005-08-17 00:39:29 +00001951 Result = ExpandLegalINT_TO_FP(isSigned,
1952 LegalizeOp(Node->getOperand(0)),
1953 Node->getValueType(0));
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001954 AddLegalizedOperand(Op, Result);
1955 return Result;
1956 case TargetLowering::Promote:
1957 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1958 Node->getValueType(0),
1959 isSigned);
1960 AddLegalizedOperand(Op, Result);
1961 return Result;
1962 case TargetLowering::Legal:
1963 break;
Andrew Lenharthf4b32782005-06-27 23:28:32 +00001964 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001965
Chris Lattner3e928bb2005-01-07 07:47:09 +00001966 Tmp1 = LegalizeOp(Node->getOperand(0));
1967 if (Tmp1 != Node->getOperand(0))
1968 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1969 break;
Chris Lattnerb00a6422005-01-07 22:37:48 +00001970 case Expand:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001971 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1972 Node->getValueType(0), Node->getOperand(0));
1973 break;
1974 case Promote:
1975 if (isSigned) {
1976 Result = PromoteOp(Node->getOperand(0));
1977 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1978 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1979 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1980 } else {
1981 Result = PromoteOp(Node->getOperand(0));
1982 Result = DAG.getZeroExtendInReg(Result,
1983 Node->getOperand(0).getValueType());
1984 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattner77e77a62005-01-21 06:05:23 +00001985 }
Chris Lattnerfa9c8012005-07-28 23:31:12 +00001986 break;
1987 }
1988 break;
1989 }
1990 case ISD::TRUNCATE:
1991 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1992 case Legal:
1993 Tmp1 = LegalizeOp(Node->getOperand(0));
1994 if (Tmp1 != Node->getOperand(0))
1995 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1996 break;
1997 case Expand:
1998 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1999
2000 // Since the result is legal, we should just be able to truncate the low
2001 // part of the source.
2002 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2003 break;
2004 case Promote:
2005 Result = PromoteOp(Node->getOperand(0));
2006 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2007 break;
2008 }
2009 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002010
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002011 case ISD::FP_TO_SINT:
2012 case ISD::FP_TO_UINT:
2013 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2014 case Legal:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002015 Tmp1 = LegalizeOp(Node->getOperand(0));
2016
Chris Lattner1618beb2005-07-29 00:11:56 +00002017 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2018 default: assert(0 && "Unknown operation action!");
2019 case TargetLowering::Expand:
Nate Begemand2558e32005-08-14 01:20:53 +00002020 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2021 SDOperand True, False;
2022 MVT::ValueType VT = Node->getOperand(0).getValueType();
2023 MVT::ValueType NVT = Node->getValueType(0);
2024 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2025 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2026 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2027 Node->getOperand(0), Tmp2, ISD::SETLT);
2028 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2029 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner01b3d732005-09-28 22:28:18 +00002030 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begemand2558e32005-08-14 01:20:53 +00002031 Tmp2));
2032 False = DAG.getNode(ISD::XOR, NVT, False,
2033 DAG.getConstant(1ULL << ShiftAmt, NVT));
2034 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begeman2d56e722005-08-14 18:38:32 +00002035 return Result;
Nate Begemand2558e32005-08-14 01:20:53 +00002036 } else {
2037 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2038 }
2039 break;
Chris Lattner1618beb2005-07-29 00:11:56 +00002040 case TargetLowering::Promote:
Chris Lattnerf1fa74e2005-07-30 00:04:12 +00002041 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner1618beb2005-07-29 00:11:56 +00002042 Node->getOpcode() == ISD::FP_TO_SINT);
2043 AddLegalizedOperand(Op, Result);
2044 return Result;
Chris Lattner07dffd62005-08-26 00:14:16 +00002045 case TargetLowering::Custom: {
2046 SDOperand Tmp =
2047 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2048 Tmp = TLI.LowerOperation(Tmp, DAG);
2049 if (Tmp.Val) {
2050 AddLegalizedOperand(Op, Tmp);
2051 NeedsAnotherIteration = true;
Chris Lattner507f7522005-08-29 17:30:00 +00002052 return Tmp;
Chris Lattner07dffd62005-08-26 00:14:16 +00002053 } else {
2054 // The target thinks this is legal afterall.
2055 break;
2056 }
2057 }
Chris Lattner1618beb2005-07-29 00:11:56 +00002058 case TargetLowering::Legal:
2059 break;
2060 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002061
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002062 if (Tmp1 != Node->getOperand(0))
2063 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2064 break;
2065 case Expand:
2066 assert(0 && "Shouldn't need to expand other operators here!");
2067 case Promote:
2068 Result = PromoteOp(Node->getOperand(0));
2069 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2070 break;
2071 }
2072 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00002073
Chris Lattner13c78e22005-09-02 00:18:10 +00002074 case ISD::ANY_EXTEND:
Chris Lattnerfa9c8012005-07-28 23:31:12 +00002075 case ISD::ZERO_EXTEND:
2076 case ISD::SIGN_EXTEND:
2077 case ISD::FP_EXTEND:
2078 case ISD::FP_ROUND:
2079 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2080 case Legal:
2081 Tmp1 = LegalizeOp(Node->getOperand(0));
2082 if (Tmp1 != Node->getOperand(0))
2083 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2084 break;
2085 case Expand:
Chris Lattner2c8086f2005-04-02 05:00:07 +00002086 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnerb00a6422005-01-07 22:37:48 +00002087
Chris Lattner03c85462005-01-15 05:21:40 +00002088 case Promote:
2089 switch (Node->getOpcode()) {
Chris Lattner13c78e22005-09-02 00:18:10 +00002090 case ISD::ANY_EXTEND:
2091 Result = PromoteOp(Node->getOperand(0));
2092 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2093 break;
Chris Lattner1713e732005-01-16 00:38:00 +00002094 case ISD::ZERO_EXTEND:
2095 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002096 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner23993562005-04-13 02:38:47 +00002097 Result = DAG.getZeroExtendInReg(Result,
2098 Node->getOperand(0).getValueType());
Chris Lattner03c85462005-01-15 05:21:40 +00002099 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002100 case ISD::SIGN_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002101 Result = PromoteOp(Node->getOperand(0));
Chris Lattner13c78e22005-09-02 00:18:10 +00002102 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner1713e732005-01-16 00:38:00 +00002103 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002104 Result,
2105 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner1713e732005-01-16 00:38:00 +00002106 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002107 case ISD::FP_EXTEND:
Chris Lattner1713e732005-01-16 00:38:00 +00002108 Result = PromoteOp(Node->getOperand(0));
2109 if (Result.getValueType() != Op.getValueType())
2110 // Dynamically dead while we have only 2 FP types.
2111 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2112 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002113 case ISD::FP_ROUND:
Chris Lattnerf8161d82005-01-16 05:06:12 +00002114 Result = PromoteOp(Node->getOperand(0));
2115 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2116 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002117 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002118 }
2119 break;
Chris Lattner0f69b292005-01-15 06:18:18 +00002120 case ISD::FP_ROUND_INREG:
Chris Lattner23993562005-04-13 02:38:47 +00002121 case ISD::SIGN_EXTEND_INREG: {
Chris Lattner0f69b292005-01-15 06:18:18 +00002122 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002123 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner45b8caf2005-01-15 07:15:18 +00002124
2125 // If this operation is not supported, convert it to a shl/shr or load/store
2126 // pair.
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002127 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2128 default: assert(0 && "This action not supported for this op yet!");
2129 case TargetLowering::Legal:
2130 if (Tmp1 != Node->getOperand(0))
2131 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattner5f056bf2005-07-10 01:55:33 +00002132 DAG.getValueType(ExtraVT));
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002133 break;
2134 case TargetLowering::Expand:
Chris Lattner45b8caf2005-01-15 07:15:18 +00002135 // If this is an integer extend and shifts are supported, do that.
Chris Lattner23993562005-04-13 02:38:47 +00002136 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner45b8caf2005-01-15 07:15:18 +00002137 // NOTE: we could fall back on load/store here too for targets without
2138 // SAR. However, it is doubtful that any exist.
2139 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2140 MVT::getSizeInBits(ExtraVT);
Chris Lattner27ff1122005-01-22 00:31:52 +00002141 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner45b8caf2005-01-15 07:15:18 +00002142 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2143 Node->getOperand(0), ShiftCst);
2144 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2145 Result, ShiftCst);
2146 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2147 // The only way we can lower this is to turn it into a STORETRUNC,
2148 // EXTLOAD pair, targetting a temporary location (a stack slot).
2149
2150 // NOTE: there is a choice here between constantly creating new stack
2151 // slots and always reusing the same one. We currently always create
2152 // new ones, as reuse may inhibit scheduling.
2153 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2154 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2155 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2156 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukmanedf128a2005-04-21 22:36:52 +00002157 int SSFI =
Chris Lattner45b8caf2005-01-15 07:15:18 +00002158 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2159 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2160 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner52d08bd2005-05-09 20:23:03 +00002161 Node->getOperand(0), StackSlot,
Chris Lattner9fadb4c2005-07-10 00:29:18 +00002162 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattner5f056bf2005-07-10 01:55:33 +00002163 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2164 Result, StackSlot, DAG.getSrcValue(NULL),
2165 ExtraVT);
Chris Lattner45b8caf2005-01-15 07:15:18 +00002166 } else {
2167 assert(0 && "Unknown op");
2168 }
2169 Result = LegalizeOp(Result);
Chris Lattner55ba8fb2005-01-16 07:29:19 +00002170 break;
Chris Lattner45b8caf2005-01-15 07:15:18 +00002171 }
Chris Lattner0f69b292005-01-15 06:18:18 +00002172 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00002173 }
Chris Lattner45b8caf2005-01-15 07:15:18 +00002174 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00002175
Chris Lattner45982da2005-05-12 16:53:42 +00002176 // Note that LegalizeOp may be reentered even from single-use nodes, which
2177 // means that we always must cache transformed nodes.
2178 AddLegalizedOperand(Op, Result);
Chris Lattner3e928bb2005-01-07 07:47:09 +00002179 return Result;
2180}
2181
Chris Lattner8b6fa222005-01-15 22:16:26 +00002182/// PromoteOp - Given an operation that produces a value in an invalid type,
2183/// promote it to compute the value into a larger type. The produced value will
2184/// have the correct bits for the low portion of the register, but no guarantee
2185/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner03c85462005-01-15 05:21:40 +00002186SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2187 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00002188 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002189 assert(getTypeAction(VT) == Promote &&
2190 "Caller should expand or legalize operands that are not promotable!");
2191 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2192 "Cannot promote to smaller type!");
2193
Chris Lattner03c85462005-01-15 05:21:40 +00002194 SDOperand Tmp1, Tmp2, Tmp3;
2195
2196 SDOperand Result;
2197 SDNode *Node = Op.Val;
2198
Chris Lattner6fdcb252005-09-02 20:32:45 +00002199 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2200 if (I != PromotedNodes.end()) return I->second;
Chris Lattner45982da2005-05-12 16:53:42 +00002201
Chris Lattner0f69b292005-01-15 06:18:18 +00002202 // Promotion needs an optimization step to clean up after it, and is not
2203 // careful to avoid operations the target does not support. Make sure that
2204 // all generated operations are legalized in the next iteration.
2205 NeedsAnotherIteration = true;
2206
Chris Lattner03c85462005-01-15 05:21:40 +00002207 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00002208 case ISD::CopyFromReg:
2209 assert(0 && "CopyFromReg must be legal!");
Chris Lattner03c85462005-01-15 05:21:40 +00002210 default:
2211 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2212 assert(0 && "Do not know how to promote this operator!");
2213 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00002214 case ISD::UNDEF:
2215 Result = DAG.getNode(ISD::UNDEF, NVT);
2216 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002217 case ISD::Constant:
Chris Lattnerec176e32005-08-30 16:56:19 +00002218 if (VT != MVT::i1)
2219 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2220 else
2221 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner03c85462005-01-15 05:21:40 +00002222 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2223 break;
2224 case ISD::ConstantFP:
2225 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2226 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2227 break;
Chris Lattneref5cd1d2005-01-18 17:54:55 +00002228
Chris Lattner82fbfb62005-01-18 02:59:52 +00002229 case ISD::SETCC:
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002230 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002231 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2232 Node->getOperand(1), Node->getOperand(2));
Chris Lattner82fbfb62005-01-18 02:59:52 +00002233 Result = LegalizeOp(Result);
2234 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002235
2236 case ISD::TRUNCATE:
2237 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2238 case Legal:
2239 Result = LegalizeOp(Node->getOperand(0));
2240 assert(Result.getValueType() >= NVT &&
2241 "This truncation doesn't make sense!");
2242 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2243 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2244 break;
Chris Lattnere76ad6d2005-01-28 22:52:50 +00002245 case Promote:
2246 // The truncation is not required, because we don't guarantee anything
2247 // about high bits anyway.
2248 Result = PromoteOp(Node->getOperand(0));
2249 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002250 case Expand:
Nate Begeman79e46ac2005-04-04 00:57:08 +00002251 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2252 // Truncate the low part of the expanded value to the result type
Chris Lattnere21c3052005-08-01 18:16:37 +00002253 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner03c85462005-01-15 05:21:40 +00002254 }
2255 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002256 case ISD::SIGN_EXTEND:
2257 case ISD::ZERO_EXTEND:
Chris Lattner13c78e22005-09-02 00:18:10 +00002258 case ISD::ANY_EXTEND:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002259 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2260 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2261 case Legal:
2262 // Input is legal? Just do extend all the way to the larger type.
2263 Result = LegalizeOp(Node->getOperand(0));
2264 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2265 break;
2266 case Promote:
2267 // Promote the reg if it's smaller.
2268 Result = PromoteOp(Node->getOperand(0));
2269 // The high bits are not guaranteed to be anything. Insert an extend.
2270 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner595dc542005-02-04 18:39:19 +00002271 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner15e4b012005-07-10 00:07:11 +00002272 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner13c78e22005-09-02 00:18:10 +00002273 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner23993562005-04-13 02:38:47 +00002274 Result = DAG.getZeroExtendInReg(Result,
2275 Node->getOperand(0).getValueType());
Chris Lattner8b6fa222005-01-15 22:16:26 +00002276 break;
2277 }
2278 break;
2279
2280 case ISD::FP_EXTEND:
2281 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2282 case ISD::FP_ROUND:
2283 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2284 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2285 case Promote: assert(0 && "Unreachable with 2 FP types!");
2286 case Legal:
2287 // Input is legal? Do an FP_ROUND_INREG.
2288 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002289 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2290 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002291 break;
2292 }
2293 break;
2294
2295 case ISD::SINT_TO_FP:
2296 case ISD::UINT_TO_FP:
2297 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2298 case Legal:
2299 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002300 // No extra round required here.
2301 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002302 break;
2303
2304 case Promote:
2305 Result = PromoteOp(Node->getOperand(0));
2306 if (Node->getOpcode() == ISD::SINT_TO_FP)
2307 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner15e4b012005-07-10 00:07:11 +00002308 Result,
2309 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002310 else
Chris Lattner23993562005-04-13 02:38:47 +00002311 Result = DAG.getZeroExtendInReg(Result,
2312 Node->getOperand(0).getValueType());
Chris Lattner77e77a62005-01-21 06:05:23 +00002313 // No extra round required here.
2314 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002315 break;
2316 case Expand:
Chris Lattner77e77a62005-01-21 06:05:23 +00002317 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2318 Node->getOperand(0));
Chris Lattner77e77a62005-01-21 06:05:23 +00002319 // Round if we cannot tolerate excess precision.
2320 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002321 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2322 DAG.getValueType(VT));
Chris Lattner77e77a62005-01-21 06:05:23 +00002323 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002324 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002325 break;
2326
2327 case ISD::FP_TO_SINT:
2328 case ISD::FP_TO_UINT:
2329 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2330 case Legal:
2331 Tmp1 = LegalizeOp(Node->getOperand(0));
2332 break;
2333 case Promote:
2334 // The input result is prerounded, so we don't have to do anything
2335 // special.
2336 Tmp1 = PromoteOp(Node->getOperand(0));
2337 break;
2338 case Expand:
2339 assert(0 && "not implemented");
2340 }
Nate Begemand2558e32005-08-14 01:20:53 +00002341 // If we're promoting a UINT to a larger size, check to see if the new node
2342 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2343 // we can use that instead. This allows us to generate better code for
2344 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2345 // legal, such as PowerPC.
2346 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002347 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemanb7f6ef12005-10-25 23:47:25 +00002348 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2349 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begemand2558e32005-08-14 01:20:53 +00002350 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2351 } else {
2352 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2353 }
Chris Lattner8b6fa222005-01-15 22:16:26 +00002354 break;
2355
Chris Lattner2c8086f2005-04-02 05:00:07 +00002356 case ISD::FABS:
2357 case ISD::FNEG:
2358 Tmp1 = PromoteOp(Node->getOperand(0));
2359 assert(Tmp1.getValueType() == NVT);
2360 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2361 // NOTE: we do not have to do any extra rounding here for
2362 // NoExcessFPPrecision, because we know the input will have the appropriate
2363 // precision, and these operations don't modify precision at all.
2364 break;
2365
Chris Lattnerda6ba872005-04-28 21:44:33 +00002366 case ISD::FSQRT:
2367 case ISD::FSIN:
2368 case ISD::FCOS:
2369 Tmp1 = PromoteOp(Node->getOperand(0));
2370 assert(Tmp1.getValueType() == NVT);
2371 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2372 if(NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002373 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2374 DAG.getValueType(VT));
Chris Lattnerda6ba872005-04-28 21:44:33 +00002375 break;
2376
Chris Lattner03c85462005-01-15 05:21:40 +00002377 case ISD::AND:
2378 case ISD::OR:
2379 case ISD::XOR:
Chris Lattner0f69b292005-01-15 06:18:18 +00002380 case ISD::ADD:
Chris Lattner8b6fa222005-01-15 22:16:26 +00002381 case ISD::SUB:
Chris Lattner0f69b292005-01-15 06:18:18 +00002382 case ISD::MUL:
2383 // The input may have strange things in the top bits of the registers, but
Chris Lattner01b3d732005-09-28 22:28:18 +00002384 // these operations don't care. They may have weird bits going out, but
Chris Lattner0f69b292005-01-15 06:18:18 +00002385 // that too is okay if they are integer operations.
2386 Tmp1 = PromoteOp(Node->getOperand(0));
2387 Tmp2 = PromoteOp(Node->getOperand(1));
2388 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2389 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner01b3d732005-09-28 22:28:18 +00002390 break;
2391 case ISD::FADD:
2392 case ISD::FSUB:
2393 case ISD::FMUL:
2394 // The input may have strange things in the top bits of the registers, but
2395 // these operations don't care.
2396 Tmp1 = PromoteOp(Node->getOperand(0));
2397 Tmp2 = PromoteOp(Node->getOperand(1));
2398 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2399 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2400
2401 // Floating point operations will give excess precision that we may not be
2402 // able to tolerate. If we DO allow excess precision, just leave it,
2403 // otherwise excise it.
Chris Lattner8b6fa222005-01-15 22:16:26 +00002404 // FIXME: Why would we need to round FP ops more than integer ones?
2405 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner01b3d732005-09-28 22:28:18 +00002406 if (NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002407 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2408 DAG.getValueType(VT));
Chris Lattner0f69b292005-01-15 06:18:18 +00002409 break;
2410
Chris Lattner8b6fa222005-01-15 22:16:26 +00002411 case ISD::SDIV:
2412 case ISD::SREM:
2413 // These operators require that their input be sign extended.
2414 Tmp1 = PromoteOp(Node->getOperand(0));
2415 Tmp2 = PromoteOp(Node->getOperand(1));
2416 if (MVT::isInteger(NVT)) {
Chris Lattner15e4b012005-07-10 00:07:11 +00002417 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2418 DAG.getValueType(VT));
2419 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2420 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002421 }
2422 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2423
2424 // Perform FP_ROUND: this is probably overly pessimistic.
2425 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner15e4b012005-07-10 00:07:11 +00002426 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2427 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002428 break;
Chris Lattner01b3d732005-09-28 22:28:18 +00002429 case ISD::FDIV:
2430 case ISD::FREM:
2431 // These operators require that their input be fp extended.
2432 Tmp1 = PromoteOp(Node->getOperand(0));
2433 Tmp2 = PromoteOp(Node->getOperand(1));
2434 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2435
2436 // Perform FP_ROUND: this is probably overly pessimistic.
2437 if (NoExcessFPPrecision)
2438 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2439 DAG.getValueType(VT));
2440 break;
Chris Lattner8b6fa222005-01-15 22:16:26 +00002441
2442 case ISD::UDIV:
2443 case ISD::UREM:
2444 // These operators require that their input be zero extended.
2445 Tmp1 = PromoteOp(Node->getOperand(0));
2446 Tmp2 = PromoteOp(Node->getOperand(1));
2447 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner23993562005-04-13 02:38:47 +00002448 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2449 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002450 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2451 break;
2452
2453 case ISD::SHL:
2454 Tmp1 = PromoteOp(Node->getOperand(0));
2455 Tmp2 = LegalizeOp(Node->getOperand(1));
2456 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2457 break;
2458 case ISD::SRA:
2459 // The input value must be properly sign extended.
2460 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner15e4b012005-07-10 00:07:11 +00002461 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2462 DAG.getValueType(VT));
Chris Lattner8b6fa222005-01-15 22:16:26 +00002463 Tmp2 = LegalizeOp(Node->getOperand(1));
2464 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2465 break;
2466 case ISD::SRL:
2467 // The input value must be properly zero extended.
2468 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner23993562005-04-13 02:38:47 +00002469 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner8b6fa222005-01-15 22:16:26 +00002470 Tmp2 = LegalizeOp(Node->getOperand(1));
2471 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2472 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002473 case ISD::LOAD:
2474 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2475 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begemanded49632005-10-13 03:11:28 +00002476 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2477 Node->getOperand(2), VT);
Chris Lattner03c85462005-01-15 05:21:40 +00002478 // Remember that we legalized the chain.
2479 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2480 break;
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002481 case ISD::SEXTLOAD:
2482 case ISD::ZEXTLOAD:
2483 case ISD::EXTLOAD:
2484 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2485 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattner8136cda2005-10-15 20:24:07 +00002486 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2487 Node->getOperand(2),
2488 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattner4c8f8f02005-10-13 20:07:41 +00002489 // Remember that we legalized the chain.
2490 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2491 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002492 case ISD::SELECT:
Chris Lattner47e92232005-01-18 19:27:06 +00002493 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2494 case Expand: assert(0 && "It's impossible to expand bools");
2495 case Legal:
2496 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2497 break;
2498 case Promote:
2499 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2500 break;
2501 }
Chris Lattner03c85462005-01-15 05:21:40 +00002502 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2503 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2504 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2505 break;
Nate Begeman9373a812005-08-10 20:51:12 +00002506 case ISD::SELECT_CC:
2507 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2508 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2509 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2510 Node->getOperand(1), Tmp2, Tmp3,
2511 Node->getOperand(4));
2512 break;
Chris Lattnerd71c0412005-05-13 18:43:43 +00002513 case ISD::TAILCALL:
Chris Lattner8ac532c2005-01-16 19:46:48 +00002514 case ISD::CALL: {
2515 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2516 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2517
Chris Lattner3d9dffc2005-01-19 20:24:35 +00002518 std::vector<SDOperand> Ops;
2519 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2520 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2521
Chris Lattner8ac532c2005-01-16 19:46:48 +00002522 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2523 "Can only promote single result calls");
2524 std::vector<MVT::ValueType> RetTyVTs;
2525 RetTyVTs.reserve(2);
2526 RetTyVTs.push_back(NVT);
2527 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00002528 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2529 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner8ac532c2005-01-16 19:46:48 +00002530 Result = SDOperand(NC, 0);
2531
2532 // Insert the new chain mapping.
2533 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2534 break;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002535 }
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002536 case ISD::CTPOP:
2537 case ISD::CTTZ:
2538 case ISD::CTLZ:
2539 Tmp1 = Node->getOperand(0);
2540 //Zero extend the argument
2541 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2542 // Perform the larger operation, then subtract if needed.
2543 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2544 switch(Node->getOpcode())
2545 {
2546 case ISD::CTPOP:
2547 Result = Tmp1;
2548 break;
2549 case ISD::CTTZ:
2550 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begemand2558e32005-08-14 01:20:53 +00002551 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002552 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen00b168892005-07-27 06:12:32 +00002553 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002554 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2555 break;
2556 case ISD::CTLZ:
2557 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen00b168892005-07-27 06:12:32 +00002558 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2559 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthfecf0952005-05-04 19:11:05 +00002560 getSizeInBits(VT), NVT));
2561 break;
2562 }
2563 break;
Chris Lattner03c85462005-01-15 05:21:40 +00002564 }
2565
2566 assert(Result.Val && "Didn't set a result!");
2567 AddPromotedOperand(Op, Result);
2568 return Result;
2569}
Chris Lattner3e928bb2005-01-07 07:47:09 +00002570
Chris Lattner84f67882005-01-20 18:52:28 +00002571/// ExpandAddSub - Find a clever way to expand this add operation into
2572/// subcomponents.
Chris Lattner47599822005-04-02 03:38:53 +00002573void SelectionDAGLegalize::
2574ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2575 SDOperand &Lo, SDOperand &Hi) {
Chris Lattner84f67882005-01-20 18:52:28 +00002576 // Expand the subcomponents.
2577 SDOperand LHSL, LHSH, RHSL, RHSH;
2578 ExpandOp(LHS, LHSL, LHSH);
2579 ExpandOp(RHS, RHSL, RHSH);
2580
Chris Lattner84f67882005-01-20 18:52:28 +00002581 std::vector<SDOperand> Ops;
2582 Ops.push_back(LHSL);
2583 Ops.push_back(LHSH);
2584 Ops.push_back(RHSL);
2585 Ops.push_back(RHSH);
Chris Lattnere89083a2005-05-14 07:25:05 +00002586 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2587 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner84f67882005-01-20 18:52:28 +00002588 Hi = Lo.getValue(1);
2589}
2590
Chris Lattner5b359c62005-04-02 04:00:59 +00002591void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2592 SDOperand Op, SDOperand Amt,
2593 SDOperand &Lo, SDOperand &Hi) {
2594 // Expand the subcomponents.
2595 SDOperand LHSL, LHSH;
2596 ExpandOp(Op, LHSL, LHSH);
2597
2598 std::vector<SDOperand> Ops;
2599 Ops.push_back(LHSL);
2600 Ops.push_back(LHSH);
2601 Ops.push_back(Amt);
Chris Lattnercc0675a2005-08-30 17:21:17 +00002602 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattnere89083a2005-05-14 07:25:05 +00002603 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner5b359c62005-04-02 04:00:59 +00002604 Hi = Lo.getValue(1);
2605}
2606
2607
Chris Lattnere34b3962005-01-19 04:19:40 +00002608/// ExpandShift - Try to find a clever way to expand this shift operation out to
2609/// smaller elements. If we can't find a way that is more efficient than a
2610/// libcall on this target, return false. Otherwise, return true with the
2611/// low-parts expanded into Lo and Hi.
2612bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2613 SDOperand &Lo, SDOperand &Hi) {
2614 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2615 "This is not a shift!");
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002616
Chris Lattnere34b3962005-01-19 04:19:40 +00002617 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002618 SDOperand ShAmt = LegalizeOp(Amt);
2619 MVT::ValueType ShTy = ShAmt.getValueType();
2620 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2621 unsigned NVTBits = MVT::getSizeInBits(NVT);
2622
2623 // Handle the case when Amt is an immediate. Other cases are currently broken
2624 // and are disabled.
2625 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2626 unsigned Cst = CN->getValue();
2627 // Expand the incoming operand to be shifted, so that we have its parts
2628 SDOperand InL, InH;
2629 ExpandOp(Op, InL, InH);
2630 switch(Opc) {
2631 case ISD::SHL:
2632 if (Cst > VTBits) {
2633 Lo = DAG.getConstant(0, NVT);
2634 Hi = DAG.getConstant(0, NVT);
2635 } else if (Cst > NVTBits) {
2636 Lo = DAG.getConstant(0, NVT);
2637 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002638 } else if (Cst == NVTBits) {
2639 Lo = DAG.getConstant(0, NVT);
2640 Hi = InL;
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002641 } else {
2642 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2643 Hi = DAG.getNode(ISD::OR, NVT,
2644 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2645 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2646 }
2647 return true;
2648 case ISD::SRL:
2649 if (Cst > VTBits) {
2650 Lo = DAG.getConstant(0, NVT);
2651 Hi = DAG.getConstant(0, NVT);
2652 } else if (Cst > NVTBits) {
2653 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2654 Hi = DAG.getConstant(0, NVT);
Chris Lattneree27f572005-04-11 20:08:52 +00002655 } else if (Cst == NVTBits) {
2656 Lo = InH;
2657 Hi = DAG.getConstant(0, NVT);
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002658 } else {
2659 Lo = DAG.getNode(ISD::OR, NVT,
2660 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2661 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2662 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2663 }
2664 return true;
2665 case ISD::SRA:
2666 if (Cst > VTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002667 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002668 DAG.getConstant(NVTBits-1, ShTy));
2669 } else if (Cst > NVTBits) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002670 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002671 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukmanedf128a2005-04-21 22:36:52 +00002672 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002673 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneree27f572005-04-11 20:08:52 +00002674 } else if (Cst == NVTBits) {
2675 Lo = InH;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002676 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneree27f572005-04-11 20:08:52 +00002677 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanf1fe32e2005-04-06 21:13:14 +00002678 } else {
2679 Lo = DAG.getNode(ISD::OR, NVT,
2680 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2681 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2682 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2683 }
2684 return true;
2685 }
2686 }
2687 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2688 // so disable it for now. Currently targets are handling this via SHL_PARTS
2689 // and friends.
2690 return false;
Chris Lattnere34b3962005-01-19 04:19:40 +00002691
2692 // If we have an efficient select operation (or if the selects will all fold
2693 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002694 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattnere34b3962005-01-19 04:19:40 +00002695 return false;
2696
2697 SDOperand InL, InH;
2698 ExpandOp(Op, InL, InH);
Chris Lattnere34b3962005-01-19 04:19:40 +00002699 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2700 DAG.getConstant(NVTBits, ShTy), ShAmt);
2701
Chris Lattnere5544f82005-01-20 20:29:23 +00002702 // Compare the unmasked shift amount against 32.
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002703 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2704 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattnere5544f82005-01-20 20:29:23 +00002705
Chris Lattnere34b3962005-01-19 04:19:40 +00002706 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2707 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2708 DAG.getConstant(NVTBits-1, ShTy));
2709 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2710 DAG.getConstant(NVTBits-1, ShTy));
2711 }
2712
2713 if (Opc == ISD::SHL) {
2714 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2715 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2716 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002717 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukmanedf128a2005-04-21 22:36:52 +00002718
Chris Lattnere34b3962005-01-19 04:19:40 +00002719 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2720 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2721 } else {
Chris Lattner77e77a62005-01-21 06:05:23 +00002722 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002723 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2724 DAG.getConstant(32, ShTy),
2725 ISD::SETEQ),
Chris Lattner77e77a62005-01-21 06:05:23 +00002726 DAG.getConstant(0, NVT),
2727 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattnere34b3962005-01-19 04:19:40 +00002728 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattner77e77a62005-01-21 06:05:23 +00002729 HiLoPart,
Chris Lattnere34b3962005-01-19 04:19:40 +00002730 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattnere5544f82005-01-20 20:29:23 +00002731 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattnere34b3962005-01-19 04:19:40 +00002732
2733 SDOperand HiPart;
Chris Lattner77e77a62005-01-21 06:05:23 +00002734 if (Opc == ISD::SRA)
2735 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2736 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattnere34b3962005-01-19 04:19:40 +00002737 else
2738 HiPart = DAG.getConstant(0, NVT);
Chris Lattnere34b3962005-01-19 04:19:40 +00002739 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattnere5544f82005-01-20 20:29:23 +00002740 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattnere34b3962005-01-19 04:19:40 +00002741 }
2742 return true;
2743}
Chris Lattner77e77a62005-01-21 06:05:23 +00002744
Chris Lattner9530ddc2005-05-13 05:09:11 +00002745/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2746/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002747/// Found.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002748static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002749 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattner2f4eca32005-08-05 16:23:57 +00002750
Chris Lattner16cd04d2005-05-12 23:24:06 +00002751 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002752 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002753 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002754 Found = Node;
2755 return;
2756 }
2757
2758 // Otherwise, scan the operands of Node to see if any of them is a call.
2759 assert(Node->getNumOperands() != 0 &&
2760 "All leaves should have depth equal to the entry node!");
Nate Begeman829cb812005-10-05 21:44:10 +00002761 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002762 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002763
2764 // Tail recurse for the last iteration.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002765 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002766 Found);
2767}
2768
2769
Chris Lattner9530ddc2005-05-13 05:09:11 +00002770/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2771/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002772/// than Found.
Chris Lattner82299e72005-08-05 18:10:27 +00002773static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2774 std::set<SDNode*> &Visited) {
2775 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2776 !Visited.insert(Node).second) return;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002777
Chris Lattner16cd04d2005-05-12 23:24:06 +00002778 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002779 // than the Found node. Just remember this node and return.
Chris Lattner16cd04d2005-05-12 23:24:06 +00002780 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002781 Found = Node;
2782 return;
2783 }
2784
2785 // Otherwise, scan the operands of Node to see if any of them is a call.
2786 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2787 if (UI == E) return;
2788 for (--E; UI != E; ++UI)
Chris Lattner82299e72005-08-05 18:10:27 +00002789 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002790
2791 // Tail recurse for the last iteration.
Chris Lattner82299e72005-08-05 18:10:27 +00002792 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002793}
2794
Chris Lattner9530ddc2005-05-13 05:09:11 +00002795/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002796/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002797static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner16cd04d2005-05-12 23:24:06 +00002798 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002799 return Node;
Chris Lattnerf4b45792005-04-02 03:22:40 +00002800 if (Node->use_empty())
Chris Lattner9530ddc2005-05-13 05:09:11 +00002801 return 0; // No CallSeqEnd
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002802
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002803 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner2789bde2005-05-14 08:34:53 +00002804 if (TheChain.getValueType() != MVT::Other)
2805 TheChain = SDOperand(Node, 0);
Nate Begeman1aa19722005-10-04 02:10:55 +00002806 if (TheChain.getValueType() != MVT::Other)
2807 return 0;
Misha Brukmanedf128a2005-04-21 22:36:52 +00002808
2809 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner2f4eca32005-08-05 16:23:57 +00002810 E = Node->use_end(); UI != E; ++UI) {
Misha Brukmanedf128a2005-04-21 22:36:52 +00002811
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002812 // Make sure to only follow users of our token chain.
2813 SDNode *User = *UI;
2814 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2815 if (User->getOperand(i) == TheChain)
Chris Lattnereb516e72005-05-13 05:17:00 +00002816 if (SDNode *Result = FindCallSeqEnd(User))
2817 return Result;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002818 }
Chris Lattner2f4eca32005-08-05 16:23:57 +00002819 return 0;
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002820}
2821
Chris Lattner9530ddc2005-05-13 05:09:11 +00002822/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner16cd04d2005-05-12 23:24:06 +00002823/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner9530ddc2005-05-13 05:09:11 +00002824static SDNode *FindCallSeqStart(SDNode *Node) {
2825 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner16cd04d2005-05-12 23:24:06 +00002826 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002827
2828 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2829 "Node doesn't have a token chain argument!");
Chris Lattner9530ddc2005-05-13 05:09:11 +00002830 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002831}
2832
2833
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002834/// FindInputOutputChains - If we are replacing an operation with a call we need
2835/// to find the call that occurs before and the call that occurs after it to
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002836/// properly serialize the calls in the block. The returned operand is the
2837/// input chain value for the new call (e.g. the entry node or the previous
2838/// call), and OutChain is set to be the chain node to update to point to the
2839/// end of the call chain.
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002840static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2841 SDOperand Entry) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002842 SDNode *LatestCallSeqStart = Entry.Val;
2843 SDNode *LatestCallSeqEnd = 0;
2844 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2845 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukmanedf128a2005-04-21 22:36:52 +00002846
Chris Lattner16cd04d2005-05-12 23:24:06 +00002847 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanc7c16572005-04-11 03:01:51 +00002848 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner16cd04d2005-05-12 23:24:06 +00002849 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2850 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman1aa19722005-10-04 02:10:55 +00002851 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002852 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman1aa19722005-10-04 02:10:55 +00002853 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2854 } else {
Chris Lattner9530ddc2005-05-13 05:09:11 +00002855 LatestCallSeqEnd = Entry.Val;
Nate Begeman1aa19722005-10-04 02:10:55 +00002856 }
Chris Lattner9530ddc2005-05-13 05:09:11 +00002857 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukmanedf128a2005-04-21 22:36:52 +00002858
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002859 // Finally, find the first call that this must come before, first we find the
Chris Lattner9530ddc2005-05-13 05:09:11 +00002860 // CallSeqEnd that ends the call.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002861 OutChain = 0;
Chris Lattner82299e72005-08-05 18:10:27 +00002862 std::set<SDNode*> Visited;
2863 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002864
Chris Lattner9530ddc2005-05-13 05:09:11 +00002865 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002866 if (OutChain)
Chris Lattner9530ddc2005-05-13 05:09:11 +00002867 OutChain = FindCallSeqStart(OutChain);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002868
Chris Lattner9530ddc2005-05-13 05:09:11 +00002869 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002870}
2871
Jeff Cohen00b168892005-07-27 06:12:32 +00002872/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002873void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2874 SDNode *OutChain) {
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002875 // Nothing to splice it into?
2876 if (OutChain == 0) return;
2877
2878 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2879 //OutChain->dump();
2880
2881 // Form a token factor node merging the old inval and the new inval.
2882 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2883 OutChain->getOperand(0));
2884 // Change the node to refer to the new token.
2885 OutChain->setAdjCallChain(InToken);
2886}
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002887
2888
Chris Lattner77e77a62005-01-21 06:05:23 +00002889// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2890// does not fit into a register, return the lo part and set the hi part to the
2891// by-reg argument. If it does fit into a single register, return the result
2892// and leave the Hi part unset.
2893SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2894 SDOperand &Hi) {
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002895 SDNode *OutChain;
2896 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2897 DAG.getEntryNode());
Chris Lattnerf4b45792005-04-02 03:22:40 +00002898 if (InChain.Val == 0)
2899 InChain = DAG.getEntryNode();
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002900
Chris Lattner77e77a62005-01-21 06:05:23 +00002901 TargetLowering::ArgListTy Args;
2902 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2903 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2904 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2905 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2906 }
2907 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukmanedf128a2005-04-21 22:36:52 +00002908
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002909 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattner77e77a62005-01-21 06:05:23 +00002910 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002911 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattneradf6a962005-05-13 18:50:42 +00002912 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2913 Callee, Args, DAG);
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00002914
Chris Lattner99c25b82005-09-02 20:26:58 +00002915 SDOperand Result;
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002916 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattner77e77a62005-01-21 06:05:23 +00002917 default: assert(0 && "Unknown thing");
2918 case Legal:
Chris Lattner99c25b82005-09-02 20:26:58 +00002919 Result = CallInfo.first;
2920 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002921 case Promote:
2922 assert(0 && "Cannot promote this yet!");
2923 case Expand:
Chris Lattner99c25b82005-09-02 20:26:58 +00002924 ExpandOp(CallInfo.first, Result, Hi);
2925 CallInfo.second = LegalizeOp(CallInfo.second);
2926 break;
Chris Lattner77e77a62005-01-21 06:05:23 +00002927 }
Chris Lattner99c25b82005-09-02 20:26:58 +00002928
2929 SpliceCallInto(CallInfo.second, OutChain);
2930 NeedsAnotherIteration = true;
2931 return Result;
Chris Lattner77e77a62005-01-21 06:05:23 +00002932}
2933
Chris Lattner9c32d3b2005-01-23 04:42:50 +00002934
Chris Lattner77e77a62005-01-21 06:05:23 +00002935/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2936/// destination type is legal.
2937SDOperand SelectionDAGLegalize::
2938ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00002939 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattner77e77a62005-01-21 06:05:23 +00002940 assert(getTypeAction(Source.getValueType()) == Expand &&
2941 "This is not an expansion!");
2942 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2943
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002944 if (!isSigned) {
Chris Lattnere9c35e72005-04-13 05:09:42 +00002945 assert(Source.getValueType() == MVT::i64 &&
2946 "This only works for 64-bit -> FP");
2947 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2948 // incoming integer is set. To handle this, we dynamically test to see if
2949 // it is set, and, if so, add a fudge factor.
2950 SDOperand Lo, Hi;
2951 ExpandOp(Source, Lo, Hi);
2952
Chris Lattner66de05b2005-05-13 04:45:13 +00002953 // If this is unsigned, and not supported, first perform the conversion to
2954 // signed, then adjust the result if the sign bit is set.
2955 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2956 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2957
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00002958 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2959 DAG.getConstant(0, Hi.getValueType()),
2960 ISD::SETLT);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002961 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2962 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2963 SignSet, Four, Zero);
Chris Lattner383203b2005-05-12 18:52:34 +00002964 uint64_t FF = 0x5f800000ULL;
2965 if (TLI.isLittleEndian()) FF <<= 32;
2966 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002967
Chris Lattner5839bf22005-08-26 17:15:30 +00002968 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere9c35e72005-04-13 05:09:42 +00002969 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2970 SDOperand FudgeInReg;
2971 if (DestTy == MVT::f32)
Chris Lattner52d08bd2005-05-09 20:23:03 +00002972 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2973 DAG.getSrcValue(NULL));
Chris Lattnere9c35e72005-04-13 05:09:42 +00002974 else {
2975 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattner5f056bf2005-07-10 01:55:33 +00002976 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2977 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere9c35e72005-04-13 05:09:42 +00002978 }
Chris Lattner473a9902005-09-29 06:44:39 +00002979 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattner77e77a62005-01-21 06:05:23 +00002980 }
Chris Lattner0d67f0c2005-05-11 19:02:11 +00002981
Chris Lattnera88a2602005-05-14 05:33:54 +00002982 // Check to see if the target has a custom way to lower this. If so, use it.
2983 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2984 default: assert(0 && "This action not implemented for this operation!");
2985 case TargetLowering::Legal:
2986 case TargetLowering::Expand:
2987 break; // This case is handled below.
Chris Lattner07dffd62005-08-26 00:14:16 +00002988 case TargetLowering::Custom: {
2989 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2990 Source), DAG);
2991 if (NV.Val)
2992 return LegalizeOp(NV);
2993 break; // The target decided this was legal after all
2994 }
Chris Lattnera88a2602005-05-14 05:33:54 +00002995 }
2996
Chris Lattner13689e22005-05-12 07:00:44 +00002997 // Expand the source, then glue it back together for the call. We must expand
2998 // the source in case it is shared (this pass of legalize must traverse it).
2999 SDOperand SrcLo, SrcHi;
3000 ExpandOp(Source, SrcLo, SrcHi);
3001 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
3002
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003003 SDNode *OutChain = 0;
3004 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3005 DAG.getEntryNode());
3006 const char *FnName = 0;
3007 if (DestTy == MVT::f32)
3008 FnName = "__floatdisf";
3009 else {
3010 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3011 FnName = "__floatdidf";
3012 }
3013
Chris Lattner77e77a62005-01-21 06:05:23 +00003014 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3015
3016 TargetLowering::ArgListTy Args;
3017 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner44d105b2005-05-12 06:54:21 +00003018
Chris Lattner77e77a62005-01-21 06:05:23 +00003019 Args.push_back(std::make_pair(Source, ArgTy));
3020
3021 // We don't care about token chains for libcalls. We just use the entry
3022 // node as our input and ignore the output chain. This allows us to place
3023 // calls wherever we need them to satisfy data dependences.
3024 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003025
3026 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattneradf6a962005-05-13 18:50:42 +00003027 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3028 Callee, Args, DAG);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003029
Chris Lattnerb9fa3bc2005-05-12 04:49:08 +00003030 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner0d67f0c2005-05-11 19:02:11 +00003031 return CallResult.first;
Chris Lattner77e77a62005-01-21 06:05:23 +00003032}
Misha Brukmanedf128a2005-04-21 22:36:52 +00003033
Chris Lattnere34b3962005-01-19 04:19:40 +00003034
3035
Chris Lattner3e928bb2005-01-07 07:47:09 +00003036/// ExpandOp - Expand the specified SDOperand into its two component pieces
3037/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3038/// LegalizeNodes map is filled in for any results that are not expanded, the
3039/// ExpandedNodes map is filled in for any results that are expanded, and the
3040/// Lo/Hi values are returned.
3041void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3042 MVT::ValueType VT = Op.getValueType();
Chris Lattner71c42a02005-01-16 01:11:45 +00003043 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003044 SDNode *Node = Op.Val;
3045 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
3046 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
3047 assert(MVT::isInteger(NVT) && NVT < VT &&
3048 "Cannot expand to FP value or to larger int value!");
3049
Chris Lattner6fdcb252005-09-02 20:32:45 +00003050 // See if we already expanded it.
3051 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3052 = ExpandedNodes.find(Op);
3053 if (I != ExpandedNodes.end()) {
3054 Lo = I->second.first;
3055 Hi = I->second.second;
3056 return;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003057 }
3058
Chris Lattner4e6c7462005-01-08 19:27:05 +00003059 // Expanding to multiple registers needs to perform an optimization step, and
3060 // is not careful to avoid operations the target does not support. Make sure
3061 // that all generated operations are legalized in the next iteration.
3062 NeedsAnotherIteration = true;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003063
Chris Lattner3e928bb2005-01-07 07:47:09 +00003064 switch (Node->getOpcode()) {
Chris Lattnerd5d0f9b2005-08-16 21:55:35 +00003065 case ISD::CopyFromReg:
3066 assert(0 && "CopyFromReg must be legal!");
3067 default:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003068 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3069 assert(0 && "Do not know how to expand this operator!");
3070 abort();
Nate Begemanfc1b1da2005-04-01 22:34:39 +00003071 case ISD::UNDEF:
3072 Lo = DAG.getNode(ISD::UNDEF, NVT);
3073 Hi = DAG.getNode(ISD::UNDEF, NVT);
3074 break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003075 case ISD::Constant: {
3076 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3077 Lo = DAG.getConstant(Cst, NVT);
3078 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3079 break;
3080 }
3081
Chris Lattnerd4e50bb2005-03-28 22:03:13 +00003082 case ISD::BUILD_PAIR:
3083 // Legalize both operands. FIXME: in the future we should handle the case
3084 // where the two elements are not legal.
3085 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3086 Lo = LegalizeOp(Node->getOperand(0));
3087 Hi = LegalizeOp(Node->getOperand(1));
3088 break;
3089
Chris Lattneredb1add2005-05-11 04:51:16 +00003090 case ISD::CTPOP:
3091 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner9b583b42005-05-11 05:09:47 +00003092 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3093 DAG.getNode(ISD::CTPOP, NVT, Lo),
3094 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattneredb1add2005-05-11 04:51:16 +00003095 Hi = DAG.getConstant(0, NVT);
3096 break;
3097
Chris Lattner39a8f332005-05-12 19:05:01 +00003098 case ISD::CTLZ: {
3099 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003100 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003101 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3102 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003103 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3104 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003105 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3106 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3107
3108 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3109 Hi = DAG.getConstant(0, NVT);
3110 break;
3111 }
3112
3113 case ISD::CTTZ: {
3114 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner3becf202005-05-12 19:27:51 +00003115 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner39a8f332005-05-12 19:05:01 +00003116 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3117 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattner7cf7e3f2005-08-09 20:20:18 +00003118 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3119 ISD::SETNE);
Chris Lattner39a8f332005-05-12 19:05:01 +00003120 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3121 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3122
3123 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3124 Hi = DAG.getConstant(0, NVT);
3125 break;
3126 }
Chris Lattneredb1add2005-05-11 04:51:16 +00003127
Chris Lattner3e928bb2005-01-07 07:47:09 +00003128 case ISD::LOAD: {
3129 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3130 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003131 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003132
3133 // Increment the pointer to the other half.
Chris Lattner38d6be52005-01-09 19:43:23 +00003134 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003135 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3136 getIntPtrConstant(IncrementSize));
Jeff Cohen00b168892005-07-27 06:12:32 +00003137 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth2d86ea22005-04-27 20:10:01 +00003138 //are from the same instruction?
3139 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerec39a452005-01-19 18:02:17 +00003140
3141 // Build a factor node to remember that this load is independent of the
3142 // other one.
3143 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3144 Hi.getValue(1));
Misha Brukmanedf128a2005-04-21 22:36:52 +00003145
Chris Lattner3e928bb2005-01-07 07:47:09 +00003146 // Remember that we legalized the chain.
Chris Lattnerec39a452005-01-19 18:02:17 +00003147 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003148 if (!TLI.isLittleEndian())
3149 std::swap(Lo, Hi);
3150 break;
3151 }
Chris Lattnerd71c0412005-05-13 18:43:43 +00003152 case ISD::TAILCALL:
Chris Lattner3e928bb2005-01-07 07:47:09 +00003153 case ISD::CALL: {
3154 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3155 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3156
Chris Lattner3d9dffc2005-01-19 20:24:35 +00003157 bool Changed = false;
3158 std::vector<SDOperand> Ops;
3159 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3160 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3161 Changed |= Ops.back() != Node->getOperand(i);
3162 }
3163
Chris Lattner3e928bb2005-01-07 07:47:09 +00003164 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3165 "Can only expand a call once so far, not i64 -> i16!");
3166
3167 std::vector<MVT::ValueType> RetTyVTs;
3168 RetTyVTs.reserve(3);
3169 RetTyVTs.push_back(NVT);
3170 RetTyVTs.push_back(NVT);
3171 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd71c0412005-05-13 18:43:43 +00003172 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3173 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner3e928bb2005-01-07 07:47:09 +00003174 Lo = SDOperand(NC, 0);
3175 Hi = SDOperand(NC, 1);
3176
3177 // Insert the new chain mapping.
Chris Lattnere3304a32005-01-08 20:35:13 +00003178 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003179 break;
3180 }
3181 case ISD::AND:
3182 case ISD::OR:
3183 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3184 SDOperand LL, LH, RL, RH;
3185 ExpandOp(Node->getOperand(0), LL, LH);
3186 ExpandOp(Node->getOperand(1), RL, RH);
3187 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3188 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3189 break;
3190 }
3191 case ISD::SELECT: {
3192 SDOperand C, LL, LH, RL, RH;
Chris Lattner47e92232005-01-18 19:27:06 +00003193
3194 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3195 case Expand: assert(0 && "It's impossible to expand bools");
3196 case Legal:
3197 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3198 break;
3199 case Promote:
3200 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3201 break;
3202 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003203 ExpandOp(Node->getOperand(1), LL, LH);
3204 ExpandOp(Node->getOperand(2), RL, RH);
3205 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3206 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3207 break;
3208 }
Nate Begeman9373a812005-08-10 20:51:12 +00003209 case ISD::SELECT_CC: {
3210 SDOperand TL, TH, FL, FH;
3211 ExpandOp(Node->getOperand(2), TL, TH);
3212 ExpandOp(Node->getOperand(3), FL, FH);
3213 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3214 Node->getOperand(1), TL, FL, Node->getOperand(4));
3215 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3216 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begemane5d63822005-08-11 01:12:20 +00003217 Lo = LegalizeOp(Lo);
3218 Hi = LegalizeOp(Hi);
Nate Begeman9373a812005-08-10 20:51:12 +00003219 break;
3220 }
Nate Begeman144ff662005-10-13 17:15:37 +00003221 case ISD::SEXTLOAD: {
3222 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3223 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3224 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3225
3226 if (EVT == NVT)
3227 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3228 else
3229 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3230 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003231
3232 // Remember that we legalized the chain.
3233 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3234
Nate Begeman144ff662005-10-13 17:15:37 +00003235 // The high part is obtained by SRA'ing all but one of the bits of the lo
3236 // part.
3237 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3238 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3239 TLI.getShiftAmountTy()));
3240 Lo = LegalizeOp(Lo);
3241 Hi = LegalizeOp(Hi);
3242 break;
3243 }
3244 case ISD::ZEXTLOAD: {
3245 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3246 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3247 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3248
3249 if (EVT == NVT)
3250 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3251 else
3252 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3253 EVT);
Chris Lattner9ad84812005-10-13 21:44:47 +00003254
3255 // Remember that we legalized the chain.
3256 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3257
Nate Begeman144ff662005-10-13 17:15:37 +00003258 // The high part is just a zero.
Chris Lattner9ad84812005-10-13 21:44:47 +00003259 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begeman144ff662005-10-13 17:15:37 +00003260 Lo = LegalizeOp(Lo);
Chris Lattner9ad84812005-10-13 21:44:47 +00003261 break;
3262 }
3263 case ISD::EXTLOAD: {
3264 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3265 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3266 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3267
3268 if (EVT == NVT)
3269 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3270 else
3271 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3272 EVT);
3273
3274 // Remember that we legalized the chain.
3275 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3276
3277 // The high part is undefined.
3278 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3279 Lo = LegalizeOp(Lo);
Nate Begeman144ff662005-10-13 17:15:37 +00003280 break;
3281 }
Chris Lattner13c78e22005-09-02 00:18:10 +00003282 case ISD::ANY_EXTEND: {
3283 SDOperand In;
3284 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3285 case Expand: assert(0 && "expand-expand not implemented yet!");
3286 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3287 case Promote:
3288 In = PromoteOp(Node->getOperand(0));
3289 break;
3290 }
3291
3292 // The low part is any extension of the input (which degenerates to a copy).
3293 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3294 // The high part is undefined.
3295 Hi = DAG.getNode(ISD::UNDEF, NVT);
3296 break;
3297 }
Chris Lattner3e928bb2005-01-07 07:47:09 +00003298 case ISD::SIGN_EXTEND: {
Chris Lattner06098e02005-04-03 23:41:52 +00003299 SDOperand In;
3300 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3301 case Expand: assert(0 && "expand-expand not implemented yet!");
3302 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3303 case Promote:
3304 In = PromoteOp(Node->getOperand(0));
3305 // Emit the appropriate sign_extend_inreg to get the value we want.
3306 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner15e4b012005-07-10 00:07:11 +00003307 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner06098e02005-04-03 23:41:52 +00003308 break;
3309 }
3310
Chris Lattner3e928bb2005-01-07 07:47:09 +00003311 // The low part is just a sign extension of the input (which degenerates to
3312 // a copy).
Chris Lattner06098e02005-04-03 23:41:52 +00003313 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003314
Chris Lattner3e928bb2005-01-07 07:47:09 +00003315 // The high part is obtained by SRA'ing all but one of the bits of the lo
3316 // part.
Chris Lattner2dad4542005-01-12 18:19:52 +00003317 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattner27ff1122005-01-22 00:31:52 +00003318 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3319 TLI.getShiftAmountTy()));
Chris Lattner3e928bb2005-01-07 07:47:09 +00003320 break;
3321 }
Chris Lattner06098e02005-04-03 23:41:52 +00003322 case ISD::ZERO_EXTEND: {
3323 SDOperand In;
3324 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3325 case Expand: assert(0 && "expand-expand not implemented yet!");
3326 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3327 case Promote:
3328 In = PromoteOp(Node->getOperand(0));
3329 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner23993562005-04-13 02:38:47 +00003330 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner06098e02005-04-03 23:41:52 +00003331 break;
3332 }
3333
Chris Lattner3e928bb2005-01-07 07:47:09 +00003334 // The low part is just a zero extension of the input (which degenerates to
3335 // a copy).
Chris Lattnerdea29e22005-04-10 01:13:15 +00003336 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukmanedf128a2005-04-21 22:36:52 +00003337
Chris Lattner3e928bb2005-01-07 07:47:09 +00003338 // The high part is just a zero.
3339 Hi = DAG.getConstant(0, NVT);
3340 break;
Chris Lattner06098e02005-04-03 23:41:52 +00003341 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003342
Chris Lattner308575b2005-11-20 22:56:56 +00003343 case ISD::READCYCLECOUNTER: {
3344 assert(TLI.getOperationAction(ISD::READCYCLECOUNTER, VT) ==
3345 TargetLowering::Custom &&
3346 "Must custom expand ReadCycleCounter");
3347 SDOperand T = TLI.LowerOperation(Op, DAG);
3348 assert(T.Val && "Node must be custom expanded!");
3349 Lo = LegalizeOp(T.getValue(0));
3350 Hi = LegalizeOp(T.getValue(1));
3351 AddLegalizedOperand(SDOperand(Node, 1), // Remember we legalized the chain.
3352 LegalizeOp(T.getValue(2)));
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003353 break;
Chris Lattner308575b2005-11-20 22:56:56 +00003354 }
Andrew Lenharthf70e30b2005-11-20 21:32:07 +00003355
Chris Lattner4e6c7462005-01-08 19:27:05 +00003356 // These operators cannot be expanded directly, emit them as calls to
3357 // library functions.
3358 case ISD::FP_TO_SINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003359 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattnerf20d1832005-07-30 01:40:57 +00003360 SDOperand Op;
3361 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3362 case Expand: assert(0 && "cannot expand FP!");
3363 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3364 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3365 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003366
Chris Lattnerf20d1832005-07-30 01:40:57 +00003367 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3368
Chris Lattner80a3e942005-07-29 00:33:32 +00003369 // Now that the custom expander is done, expand the result, which is still
3370 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003371 if (Op.Val) {
3372 ExpandOp(Op, Lo, Hi);
3373 break;
3374 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003375 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003376
Chris Lattner4e6c7462005-01-08 19:27:05 +00003377 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003378 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003379 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003380 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003381 break;
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003382
Chris Lattner4e6c7462005-01-08 19:27:05 +00003383 case ISD::FP_TO_UINT:
Chris Lattner80a3e942005-07-29 00:33:32 +00003384 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3385 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3386 LegalizeOp(Node->getOperand(0)));
3387 // Now that the custom expander is done, expand the result, which is still
3388 // VT.
Chris Lattner07dffd62005-08-26 00:14:16 +00003389 Op = TLI.LowerOperation(Op, DAG);
3390 if (Op.Val) {
3391 ExpandOp(Op, Lo, Hi);
3392 break;
3393 }
Chris Lattner80a3e942005-07-29 00:33:32 +00003394 }
Jeff Cohend29b6aa2005-07-30 18:33:25 +00003395
Chris Lattner4e6c7462005-01-08 19:27:05 +00003396 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattner77e77a62005-01-21 06:05:23 +00003397 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003398 else
Chris Lattner77e77a62005-01-21 06:05:23 +00003399 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner4e6c7462005-01-08 19:27:05 +00003400 break;
3401
Chris Lattnere34b3962005-01-19 04:19:40 +00003402 case ISD::SHL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003403 // If the target wants custom lowering, do so.
3404 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3405 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3406 LegalizeOp(Node->getOperand(1)));
3407 Op = TLI.LowerOperation(Op, DAG);
3408 if (Op.Val) {
3409 // Now that the custom expander is done, expand the result, which is
3410 // still VT.
3411 ExpandOp(Op, Lo, Hi);
3412 break;
3413 }
3414 }
3415
Chris Lattnere34b3962005-01-19 04:19:40 +00003416 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003417 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003418 break;
Chris Lattner47599822005-04-02 03:38:53 +00003419
3420 // If this target supports SHL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003421 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003422 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3423 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003424 break;
3425 }
3426
Chris Lattnere34b3962005-01-19 04:19:40 +00003427 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003428 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003429 break;
3430
3431 case ISD::SRA:
Chris Lattner50ec8972005-08-31 19:01:53 +00003432 // If the target wants custom lowering, do so.
3433 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3434 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3435 LegalizeOp(Node->getOperand(1)));
3436 Op = TLI.LowerOperation(Op, DAG);
3437 if (Op.Val) {
3438 // Now that the custom expander is done, expand the result, which is
3439 // still VT.
3440 ExpandOp(Op, Lo, Hi);
3441 break;
3442 }
3443 }
3444
Chris Lattnere34b3962005-01-19 04:19:40 +00003445 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003446 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003447 break;
Chris Lattner47599822005-04-02 03:38:53 +00003448
3449 // If this target supports SRA_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003450 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003451 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3452 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003453 break;
3454 }
3455
Chris Lattnere34b3962005-01-19 04:19:40 +00003456 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003457 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003458 break;
3459 case ISD::SRL:
Chris Lattner50ec8972005-08-31 19:01:53 +00003460 // If the target wants custom lowering, do so.
3461 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3462 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3463 LegalizeOp(Node->getOperand(1)));
3464 Op = TLI.LowerOperation(Op, DAG);
3465 if (Op.Val) {
3466 // Now that the custom expander is done, expand the result, which is
3467 // still VT.
3468 ExpandOp(Op, Lo, Hi);
3469 break;
3470 }
3471 }
3472
Chris Lattnere34b3962005-01-19 04:19:40 +00003473 // If we can emit an efficient shift operation, do so now.
Chris Lattner77e77a62005-01-21 06:05:23 +00003474 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattnere34b3962005-01-19 04:19:40 +00003475 break;
Chris Lattner47599822005-04-02 03:38:53 +00003476
3477 // If this target supports SRL_PARTS, use it.
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003478 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner5b359c62005-04-02 04:00:59 +00003479 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3480 Lo, Hi);
Chris Lattner47599822005-04-02 03:38:53 +00003481 break;
3482 }
3483
Chris Lattnere34b3962005-01-19 04:19:40 +00003484 // Otherwise, emit a libcall.
Chris Lattner77e77a62005-01-21 06:05:23 +00003485 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattnere34b3962005-01-19 04:19:40 +00003486 break;
3487
Misha Brukmanedf128a2005-04-21 22:36:52 +00003488 case ISD::ADD:
Chris Lattner47599822005-04-02 03:38:53 +00003489 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3490 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003491 break;
3492 case ISD::SUB:
Chris Lattner47599822005-04-02 03:38:53 +00003493 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3494 Lo, Hi);
Chris Lattner84f67882005-01-20 18:52:28 +00003495 break;
Nate Begemanc7c16572005-04-11 03:01:51 +00003496 case ISD::MUL: {
Chris Lattnerc9c60f62005-08-24 16:35:28 +00003497 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanc7c16572005-04-11 03:01:51 +00003498 SDOperand LL, LH, RL, RH;
3499 ExpandOp(Node->getOperand(0), LL, LH);
3500 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman56eb8682005-08-30 02:44:00 +00003501 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3502 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3503 // extended the sign bit of the low half through the upper half, and if so
3504 // emit a MULHS instead of the alternate sequence that is valid for any
3505 // i64 x i64 multiply.
3506 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3507 // is RH an extension of the sign bit of RL?
3508 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3509 RH.getOperand(1).getOpcode() == ISD::Constant &&
3510 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3511 // is LH an extension of the sign bit of LL?
3512 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3513 LH.getOperand(1).getOpcode() == ISD::Constant &&
3514 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3515 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3516 } else {
3517 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3518 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3519 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3520 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3521 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3522 }
Nate Begemanc7c16572005-04-11 03:01:51 +00003523 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3524 } else {
3525 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3526 }
3527 break;
3528 }
Chris Lattner77e77a62005-01-21 06:05:23 +00003529 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3530 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3531 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3532 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattner3e928bb2005-01-07 07:47:09 +00003533 }
3534
3535 // Remember in a map if the values will be reused later.
Chris Lattner6fdcb252005-09-02 20:32:45 +00003536 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3537 std::make_pair(Lo, Hi))).second;
3538 assert(isNew && "Value already expanded?!?");
Chris Lattner3e928bb2005-01-07 07:47:09 +00003539}
3540
3541
3542// SelectionDAG::Legalize - This is the entry point for the file.
3543//
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003544void SelectionDAG::Legalize() {
Chris Lattner3e928bb2005-01-07 07:47:09 +00003545 /// run - This is the main entry point to this class.
3546 ///
Chris Lattner9c32d3b2005-01-23 04:42:50 +00003547 SelectionDAGLegalize(*this).Run();
Chris Lattner3e928bb2005-01-07 07:47:09 +00003548}
3549