blob: 31b0e0815d3a18b8b1c75cd91c233ec789ac9e0d [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000015#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000016#include "llvm/CodeGen/MachineFrameInfo.h"
Jim Laskey686d6a12005-08-17 17:42:52 +000017#include "llvm/Support/MathExtras.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris Lattner2e77db62005-05-13 18:50:42 +000021#include "llvm/CallingConv.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000022#include "llvm/Constants.h"
23#include <iostream>
Chris Lattner96ad3132005-08-05 18:10:27 +000024#include <set>
Chris Lattnerdc750592005-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 Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-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 Lattnerea4ca942005-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 Lattner1f2c9d82005-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 Lattnerea4ca942005-01-07 22:28:47 +000087
Chris Lattnerdc750592005-01-07 07:47:09 +000088public:
89
Chris Lattner4add7e32005-01-23 04:42:50 +000090 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-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 Lattner1f2c9d82005-01-15 05:21:40 +0000122 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000123
Chris Lattneraac464e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000128
Jim Laskeyf2516a92005-08-17 00:39:29 +0000129 SDOperand ExpandLegalINT_TO_FP(bool isSigned,
130 SDOperand LegalOp,
131 MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000132 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
133 bool isSigned);
Chris Lattner44fe26f2005-07-29 00:11:56 +0000134 SDOperand PromoteLegalFP_TO_INT(SDOperand LegalOp, MVT::ValueType DestVT,
135 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000136
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000137 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
138 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-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 Lattner2e5872c2005-04-02 03:38:53 +0000142 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000143
Chris Lattnera5bf1032005-05-12 04:49:08 +0000144 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
145
Chris Lattnerdc750592005-01-07 07:47:09 +0000146 SDOperand getIntPtrConstant(uint64_t Val) {
147 return DAG.getConstant(Val, TLI.getPointerTy());
148 }
149};
150}
151
Chris Lattner301015a2005-11-19 05:51:46 +0000152static unsigned getScalarizedOpcode(unsigned VecOp, MVT::ValueType VT) {
Nate Begemanb2e089c2005-11-19 00:36:38 +0000153 switch (VecOp) {
154 default: assert(0 && "Don't know how to scalarize this opcode!");
Nate Begemanb2e089c2005-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 Lattnerdc750592005-01-07 07:47:09 +0000160
Chris Lattner4add7e32005-01-23 04:42:50 +0000161SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
162 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
163 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000164 assert(MVT::LAST_VALUETYPE <= 16 &&
165 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000166}
167
Jim Laskeyf2516a92005-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 Lattnere3e847b2005-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 Laskeyf2516a92005-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 Laskey686d6a12005-08-17 17:42:52 +0000215 SDOperand Bias = DAG.getConstantFP(isSigned ?
216 BitsToDouble(0x4330000080000000ULL)
217 : BitsToDouble(0x4330000000000000ULL),
Jim Laskeyf2516a92005-08-17 00:39:29 +0000218 MVT::f64);
219 // subtract the bias
Chris Lattner6f3b5772005-09-28 22:28:18 +0000220 SDOperand Sub = DAG.getNode(ISD::FSUB, MVT::f64, Load, Bias);
Jim Laskeyf2516a92005-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 Lattnere3e847b2005-07-16 00:19:57 +0000235 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000236
Chris Lattnerd47675e2005-08-09 20:20:18 +0000237 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Op0,
238 DAG.getConstant(0, Op0.getValueType()),
239 ISD::SETLT);
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000243
Jim Laskeyf2516a92005-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 Lattnerb35912e2005-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 Lattnere3e847b2005-07-16 00:19:57 +0000255 if (TLI.isLittleEndian()) FF <<= 32;
256 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000257
Chris Lattnerc30405e2005-08-26 17:15:30 +0000258 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000270
Chris Lattnere3e847b2005-07-16 00:19:57 +0000271 NeedsAnotherIteration = true;
Chris Lattner5b2be1f2005-09-29 06:44:39 +0000272 return DAG.getNode(ISD::FADD, DestVT, Tmp1, FudgeInReg);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000273}
274
Chris Lattner19732782005-08-16 18:17:10 +0000275/// PromoteLegalINT_TO_FP - This function is responsible for legalizing a
Chris Lattner44fe26f2005-07-29 00:11:56 +0000276/// *INT_TO_FP operation of the specified operand when the target requests that
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000280SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
281 MVT::ValueType DestVT,
282 bool isSigned) {
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000285
Chris Lattnere3e847b2005-07-16 00:19:57 +0000286 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000287
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000292
Chris Lattnere3e847b2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000297 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000305 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000306
Chris Lattnere3e847b2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000311 if (!TLI.isTypeLegal(NewInTy))
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000319
Chris Lattnere3e847b2005-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 Cohen5f4ef3c2005-07-27 06:12:32 +0000325
Chris Lattnere3e847b2005-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 Begeman7e74c832005-07-16 02:02:34 +0000329 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
330 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000331}
332
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000343
Chris Lattner44fe26f2005-07-29 00:11:56 +0000344 unsigned OpToUse = 0;
Jeff Cohen546fd592005-07-30 18:33:25 +0000345
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000350
Chris Lattner44fe26f2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000355 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000363
Chris Lattner44fe26f2005-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 Lattnerf12eb4d2005-08-24 16:35:28 +0000368 if (!TLI.isTypeLegal(NewOutTy))
Chris Lattner44fe26f2005-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 Cohen546fd592005-07-30 18:33:25 +0000376
Chris Lattner44fe26f2005-07-29 00:11:56 +0000377 // Otherwise, try a larger type.
378 }
Jeff Cohen546fd592005-07-30 18:33:25 +0000379
Chris Lattner44fe26f2005-07-29 00:11:56 +0000380 // Make sure to legalize any nodes we create here in the next pass.
381 NeedsAnotherIteration = true;
Jeff Cohen546fd592005-07-30 18:33:25 +0000382
Chris Lattner44fe26f2005-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 Lattner4bbbb9e2005-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 Lattner4bbbb9e2005-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 Lattner44fe26f2005-07-29 00:11:56 +0000409
Chris Lattnerdc750592005-01-07 07:47:09 +0000410void SelectionDAGLegalize::LegalizeDAG() {
Chris Lattner9cfccfb2005-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 Lattner4bbbb9e2005-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 Lattner9cfccfb2005-10-02 17:49:46 +0000419
Chris Lattner4bbbb9e2005-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 Lattnerbf4f23322005-11-09 23:47:37 +0000424 if (I->getNumOperands() == 0) {
425 Visited[I] = 0 - 1U;
426 ComputeTopDownOrdering(I, Order, Visited);
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000427 }
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000428 }
429
Chris Lattnerbf4f23322005-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000433 "Error: DAG is cyclic!");
434 Visited.clear();
Chris Lattner9cfccfb2005-10-02 17:49:46 +0000435
Chris Lattner4bbbb9e2005-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 Lattnerdc750592005-01-07 07:47:09 +0000455 SDOperand OldRoot = DAG.getRoot();
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000456 assert(LegalizedNodes.count(OldRoot) && "Root didn't get legalized?");
457 DAG.setRoot(LegalizedNodes[OldRoot]);
Chris Lattnerdc750592005-01-07 07:47:09 +0000458
459 ExpandedNodes.clear();
460 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000461 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000462
463 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000464 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000465}
466
467SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000468 assert(isTypeLegal(Op.getValueType()) &&
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000469 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000470 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000471
Chris Lattnerdc750592005-01-07 07:47:09 +0000472 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000473 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-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 Lattnerdc750592005-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 Lattner1f2c9d82005-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 Lattnerdc750592005-01-07 07:47:09 +0000490 }
491 }
492
Chris Lattnerb5a78e02005-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 Lattner85d70c62005-01-11 05:57:22 +0000495 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
496 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000497
Nate Begemane5b86d72005-08-10 20:51:12 +0000498 SDOperand Tmp1, Tmp2, Tmp3, Tmp4;
Chris Lattnerdc750592005-01-07 07:47:09 +0000499
500 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000501
502 switch (Node->getOpcode()) {
503 default:
Chris Lattner3eb86932005-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 Lattnerdc750592005-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 Lattner4bbbb9e2005-10-06 01:20:27 +0000532 case ISD::TargetFrameIndex:
533 case ISD::Register:
534 case ISD::TargetConstant:
Chris Lattnerdc750592005-01-07 07:47:09 +0000535 case ISD::GlobalAddress:
Chris Lattner4ff65ec2005-11-17 05:52:24 +0000536 case ISD::TargetGlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000537 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000538 case ISD::ConstantPool: // Nothing to do.
Chris Lattner4bbbb9e2005-10-06 01:20:27 +0000539 case ISD::BasicBlock:
540 case ISD::CONDCODE:
541 case ISD::VALUETYPE:
542 case ISD::SRCVALUE:
Chris Lattner45ca1c02005-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 Lattnerdc750592005-01-07 07:47:09 +0000556 break;
Chris Lattnerd9af1aa2005-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 Lattner3b8e7192005-01-14 22:38:01 +0000564 case ISD::CopyFromReg:
565 Tmp1 = LegalizeOp(Node->getOperand(0));
566 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000567 Result = DAG.getCopyFromReg(Tmp1,
568 cast<RegisterSDNode>(Node->getOperand(1))->getReg(),
569 Node->getValueType(0));
Chris Lattnereb6614d2005-01-28 06:27:38 +0000570 else
571 Result = Op.getValue(0);
572
573 // Since CopyFromReg produces two values, make sure to remember that we
574 // legalized both of them.
575 AddLegalizedOperand(Op.getValue(0), Result);
576 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
577 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000578 case ISD::ImplicitDef:
579 Tmp1 = LegalizeOp(Node->getOperand(0));
580 if (Tmp1 != Node->getOperand(0))
Chris Lattner33182322005-08-16 21:55:35 +0000581 Result = DAG.getNode(ISD::ImplicitDef, MVT::Other,
582 Tmp1, Node->getOperand(1));
Chris Lattnere727af02005-01-13 20:50:02 +0000583 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000584 case ISD::UNDEF: {
585 MVT::ValueType VT = Op.getValueType();
586 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000587 default: assert(0 && "This action is not supported yet!");
588 case TargetLowering::Expand:
589 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000590 if (MVT::isInteger(VT))
591 Result = DAG.getConstant(0, VT);
592 else if (MVT::isFloatingPoint(VT))
593 Result = DAG.getConstantFP(0, VT);
594 else
595 assert(0 && "Unknown value type!");
596 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000597 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000598 break;
599 }
600 break;
601 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000602 case ISD::Constant:
603 // We know we don't need to expand constants here, constants only have one
604 // value and we check that it is fine above.
605
606 // FIXME: Maybe we should handle things like targets that don't support full
607 // 32-bit immediates?
608 break;
609 case ISD::ConstantFP: {
610 // Spill FP immediates to the constant pool if the target cannot directly
611 // codegen them. Targets often have some immediate values that can be
612 // efficiently generated into an FP register without a load. We explicitly
613 // leave these constants as ConstantFP nodes for the target to deal with.
614
615 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
616
617 // Check to see if this FP immediate is already legal.
618 bool isLegal = false;
619 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
620 E = TLI.legal_fpimm_end(); I != E; ++I)
621 if (CFP->isExactlyValue(*I)) {
622 isLegal = true;
623 break;
624 }
625
626 if (!isLegal) {
627 // Otherwise we need to spill the constant to memory.
Chris Lattnerdc750592005-01-07 07:47:09 +0000628 bool Extend = false;
629
630 // If a FP immediate is precise when represented as a float, we put it
631 // into the constant pool as a float, even if it's is statically typed
632 // as a double.
633 MVT::ValueType VT = CFP->getValueType(0);
634 bool isDouble = VT == MVT::f64;
635 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
636 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000637 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
638 // Only do this if the target has a native EXTLOAD instruction from
639 // f32.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000640 TLI.isOperationLegal(ISD::EXTLOAD, MVT::f32)) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000641 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
642 VT = MVT::f32;
643 Extend = true;
644 }
Misha Brukman835702a2005-04-21 22:36:52 +0000645
Chris Lattnerc30405e2005-08-26 17:15:30 +0000646 SDOperand CPIdx = DAG.getConstantPool(LLVMC, TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000647 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000648 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
649 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000650 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000651 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
652 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000653 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000654 }
655 break;
656 }
Chris Lattneraf3aefa2005-11-09 18:48:57 +0000657 case ISD::TokenFactor:
658 if (Node->getNumOperands() == 2) {
659 bool Changed = false;
660 SDOperand Op0 = LegalizeOp(Node->getOperand(0));
661 SDOperand Op1 = LegalizeOp(Node->getOperand(1));
662 if (Op0 != Node->getOperand(0) || Op1 != Node->getOperand(1))
663 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Op0, Op1);
664 } else {
665 std::vector<SDOperand> Ops;
666 bool Changed = false;
667 // Legalize the operands.
668 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
669 SDOperand Op = Node->getOperand(i);
670 Ops.push_back(LegalizeOp(Op));
671 Changed |= Ops[i] != Op;
672 }
673 if (Changed)
674 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
Chris Lattner05b4e372005-01-13 17:59:25 +0000675 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000676 break;
Chris Lattner05b4e372005-01-13 17:59:25 +0000677
Chris Lattner2dce7032005-05-12 23:24:06 +0000678 case ISD::CALLSEQ_START:
679 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000680 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000681 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000682 Tmp2 = Node->getOperand(0);
Nate Begeman5da69082005-10-04 02:10:55 +0000683 if (Tmp1 != Tmp2)
Chris Lattner8005e912005-05-12 00:17:04 +0000684 Node->setAdjCallChain(Tmp1);
Nate Begeman54fb5002005-10-04 00:37:37 +0000685
Chris Lattner2dce7032005-05-12 23:24:06 +0000686 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000687 // nodes are treated specially and are mutated in place. This makes the dag
688 // legalization process more efficient and also makes libcall insertion
689 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000690 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000691 case ISD::DYNAMIC_STACKALLOC:
692 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
693 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
694 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
695 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000696 Tmp3 != Node->getOperand(2)) {
697 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
698 std::vector<SDOperand> Ops;
699 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
700 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
701 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000702 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000703
704 // Since this op produces two values, make sure to remember that we
705 // legalized both of them.
706 AddLegalizedOperand(SDOperand(Node, 0), Result);
707 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
708 return Result.getValue(Op.ResNo);
709
Chris Lattnerd0feb642005-05-13 18:43:43 +0000710 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000711 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000712 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
713 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000714
715 bool Changed = false;
716 std::vector<SDOperand> Ops;
717 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
718 Ops.push_back(LegalizeOp(Node->getOperand(i)));
719 Changed |= Ops.back() != Node->getOperand(i);
720 }
721
722 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000723 std::vector<MVT::ValueType> RetTyVTs;
724 RetTyVTs.reserve(Node->getNumValues());
725 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000726 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000727 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
728 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000729 } else {
730 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000731 }
Chris Lattner9242c502005-01-09 19:43:23 +0000732 // Since calls produce multiple values, make sure to remember that we
733 // legalized all of them.
734 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
735 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
736 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000737 }
Chris Lattner68a12142005-01-07 22:12:08 +0000738 case ISD::BR:
739 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
740 if (Tmp1 != Node->getOperand(0))
741 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
742 break;
743
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000744 case ISD::BRCOND:
745 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Nate Begeman371e4952005-08-16 19:49:35 +0000746
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000747 switch (getTypeAction(Node->getOperand(1).getValueType())) {
748 case Expand: assert(0 && "It's impossible to expand bools");
749 case Legal:
750 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
751 break;
752 case Promote:
753 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
754 break;
755 }
Nate Begeman371e4952005-08-16 19:49:35 +0000756
757 switch (TLI.getOperationAction(ISD::BRCOND, MVT::Other)) {
758 default: assert(0 && "This action is not supported yet!");
759 case TargetLowering::Expand:
760 // Expand brcond's setcc into its constituent parts and create a BR_CC
761 // Node.
762 if (Tmp2.getOpcode() == ISD::SETCC) {
763 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
764 Tmp2.getOperand(0), Tmp2.getOperand(1),
765 Node->getOperand(2));
766 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +0000767 // Make sure the condition is either zero or one. It may have been
768 // promoted from something else.
769 Tmp2 = DAG.getZeroExtendInReg(Tmp2, MVT::i1);
770
Nate Begeman371e4952005-08-16 19:49:35 +0000771 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
772 DAG.getCondCode(ISD::SETNE), Tmp2,
773 DAG.getConstant(0, Tmp2.getValueType()),
774 Node->getOperand(2));
775 }
776 break;
777 case TargetLowering::Legal:
778 // Basic block destination (Op#2) is always legal.
779 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
780 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
781 Node->getOperand(2));
782 break;
783 }
784 break;
785 case ISD::BR_CC:
786 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
787
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000788 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000789 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
790 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
791 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
792 Tmp3 != Node->getOperand(3)) {
793 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Node->getOperand(1),
794 Tmp2, Tmp3, Node->getOperand(4));
795 }
796 break;
797 } else {
798 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
799 Node->getOperand(2), // LHS
800 Node->getOperand(3), // RHS
801 Node->getOperand(1)));
802 // If we get a SETCC back from legalizing the SETCC node we just
803 // created, then use its LHS, RHS, and CC directly in creating a new
804 // node. Otherwise, select between the true and false value based on
805 // comparing the result of the legalized with zero.
806 if (Tmp2.getOpcode() == ISD::SETCC) {
807 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1, Tmp2.getOperand(2),
808 Tmp2.getOperand(0), Tmp2.getOperand(1),
809 Node->getOperand(4));
810 } else {
811 Result = DAG.getNode(ISD::BR_CC, MVT::Other, Tmp1,
812 DAG.getCondCode(ISD::SETNE),
813 Tmp2, DAG.getConstant(0, Tmp2.getValueType()),
814 Node->getOperand(4));
815 }
816 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000817 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000818 case ISD::BRCONDTWOWAY:
819 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
820 switch (getTypeAction(Node->getOperand(1).getValueType())) {
821 case Expand: assert(0 && "It's impossible to expand bools");
822 case Legal:
823 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
824 break;
825 case Promote:
826 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
827 break;
828 }
829 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
830 // pair.
831 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
832 case TargetLowering::Promote:
833 default: assert(0 && "This action is not supported yet!");
834 case TargetLowering::Legal:
835 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
836 std::vector<SDOperand> Ops;
837 Ops.push_back(Tmp1);
838 Ops.push_back(Tmp2);
839 Ops.push_back(Node->getOperand(2));
840 Ops.push_back(Node->getOperand(3));
841 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
842 }
843 break;
844 case TargetLowering::Expand:
Nate Begeman371e4952005-08-16 19:49:35 +0000845 // If BRTWOWAY_CC is legal for this target, then simply expand this node
846 // to that. Otherwise, skip BRTWOWAY_CC and expand directly to a
847 // BRCOND/BR pair.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000848 if (TLI.isOperationLegal(ISD::BRTWOWAY_CC, MVT::Other)) {
Nate Begeman371e4952005-08-16 19:49:35 +0000849 if (Tmp2.getOpcode() == ISD::SETCC) {
850 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
851 Tmp2.getOperand(0), Tmp2.getOperand(1),
852 Node->getOperand(2), Node->getOperand(3));
853 } else {
854 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
855 DAG.getConstant(0, Tmp2.getValueType()),
856 Node->getOperand(2), Node->getOperand(3));
857 }
858 } else {
859 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
Chris Lattnerfd986782005-04-09 03:30:19 +0000860 Node->getOperand(2));
Nate Begeman371e4952005-08-16 19:49:35 +0000861 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
862 }
Chris Lattnerfd986782005-04-09 03:30:19 +0000863 break;
864 }
865 break;
Nate Begeman371e4952005-08-16 19:49:35 +0000866 case ISD::BRTWOWAY_CC:
867 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +0000868 if (isTypeLegal(Node->getOperand(2).getValueType())) {
Nate Begeman371e4952005-08-16 19:49:35 +0000869 Tmp2 = LegalizeOp(Node->getOperand(2)); // LHS
870 Tmp3 = LegalizeOp(Node->getOperand(3)); // RHS
871 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2) ||
872 Tmp3 != Node->getOperand(3)) {
873 Result = DAG.getBR2Way_CC(Tmp1, Node->getOperand(1), Tmp2, Tmp3,
874 Node->getOperand(4), Node->getOperand(5));
875 }
876 break;
877 } else {
878 Tmp2 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
879 Node->getOperand(2), // LHS
880 Node->getOperand(3), // RHS
881 Node->getOperand(1)));
882 // If this target does not support BRTWOWAY_CC, lower it to a BRCOND/BR
883 // pair.
884 switch (TLI.getOperationAction(ISD::BRTWOWAY_CC, MVT::Other)) {
885 default: assert(0 && "This action is not supported yet!");
886 case TargetLowering::Legal:
887 // If we get a SETCC back from legalizing the SETCC node we just
888 // created, then use its LHS, RHS, and CC directly in creating a new
889 // node. Otherwise, select between the true and false value based on
890 // comparing the result of the legalized with zero.
891 if (Tmp2.getOpcode() == ISD::SETCC) {
892 Result = DAG.getBR2Way_CC(Tmp1, Tmp2.getOperand(2),
893 Tmp2.getOperand(0), Tmp2.getOperand(1),
894 Node->getOperand(4), Node->getOperand(5));
895 } else {
896 Result = DAG.getBR2Way_CC(Tmp1, DAG.getCondCode(ISD::SETNE), Tmp2,
897 DAG.getConstant(0, Tmp2.getValueType()),
898 Node->getOperand(4), Node->getOperand(5));
899 }
900 break;
901 case TargetLowering::Expand:
902 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
903 Node->getOperand(4));
904 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(5));
905 break;
906 }
907 }
908 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000909 case ISD::LOAD:
910 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
911 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000912
Chris Lattnerdc750592005-01-07 07:47:09 +0000913 if (Tmp1 != Node->getOperand(0) ||
914 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000915 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
916 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000917 else
918 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000919
Chris Lattnerea4ca942005-01-07 22:28:47 +0000920 // Since loads produce two values, make sure to remember that we legalized
921 // both of them.
922 AddLegalizedOperand(SDOperand(Node, 0), Result);
923 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
924 return Result.getValue(Op.ResNo);
Nate Begemanb2e089c2005-11-19 00:36:38 +0000925
926 case ISD::VLOAD:
927 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
928 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
929
930 // If we just have one element, scalarize the result. Otherwise, check to
931 // see if we support this operation on this type at this width. If not,
932 // split the vector in half and try again.
933 if (1 == cast<ConstantSDNode>(Node->getOperand(2))->getValue()) {
934 MVT::ValueType SVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
935 Result = LegalizeOp(DAG.getLoad(SVT, Tmp1, Tmp2, Node->getOperand(4)));
936 } else {
937 assert(0 && "Expand case for vectors unimplemented");
938 }
939
940 // Since loads produce two values, make sure to remember that we legalized
941 // both of them.
942 AddLegalizedOperand(SDOperand(Node, 0), Result);
943 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
944 return Result.getValue(Op.ResNo);
945
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000946 case ISD::EXTLOAD:
947 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000948 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000949 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
950 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000951
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000952 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000953 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000954 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000955 case TargetLowering::Promote:
956 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000957 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
958 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000959 // Since loads produce two values, make sure to remember that we legalized
960 // both of them.
961 AddLegalizedOperand(SDOperand(Node, 0), Result);
962 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
963 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000964
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000965 case TargetLowering::Legal:
966 if (Tmp1 != Node->getOperand(0) ||
967 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000968 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
969 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000970 else
971 Result = SDOperand(Node, 0);
972
973 // Since loads produce two values, make sure to remember that we legalized
974 // both of them.
975 AddLegalizedOperand(SDOperand(Node, 0), Result);
976 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
977 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000978 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000979 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
980 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
981 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000982 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000983 if (Op.ResNo)
984 return Load.getValue(1);
985 return Result;
986 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000987 assert(Node->getOpcode() != ISD::EXTLOAD &&
988 "EXTLOAD should always be supported!");
989 // Turn the unsupported load into an EXTLOAD followed by an explicit
990 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000991 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
992 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000993 SDOperand ValRes;
994 if (Node->getOpcode() == ISD::SEXTLOAD)
995 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000996 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000997 else
998 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000999 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
1000 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1001 if (Op.ResNo)
1002 return Result.getValue(1);
1003 return ValRes;
1004 }
1005 assert(0 && "Unreachable");
1006 }
Nate Begeman5172ce62005-10-19 00:06:56 +00001007 case ISD::EXTRACT_ELEMENT: {
1008 MVT::ValueType OpTy = Node->getOperand(0).getValueType();
1009 switch (getTypeAction(OpTy)) {
1010 default:
1011 assert(0 && "EXTRACT_ELEMENT action for type unimplemented!");
1012 break;
1013 case Legal:
1014 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue()) {
1015 // 1 -> Hi
1016 Result = DAG.getNode(ISD::SRL, OpTy, Node->getOperand(0),
1017 DAG.getConstant(MVT::getSizeInBits(OpTy)/2,
1018 TLI.getShiftAmountTy()));
1019 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Result);
1020 } else {
1021 // 0 -> Lo
1022 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0),
1023 Node->getOperand(0));
1024 }
1025 Result = LegalizeOp(Result);
1026 break;
1027 case Expand:
1028 // Get both the low and high parts.
1029 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1030 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
1031 Result = Tmp2; // 1 -> Hi
1032 else
1033 Result = Tmp1; // 0 -> Lo
1034 break;
1035 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001036 break;
Nate Begeman5172ce62005-10-19 00:06:56 +00001037 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001038
1039 case ISD::CopyToReg:
1040 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +00001041
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001042 assert(isTypeLegal(Node->getOperand(2).getValueType()) &&
Chris Lattner33182322005-08-16 21:55:35 +00001043 "Register type must be legal!");
1044 // Legalize the incoming value (must be legal).
1045 Tmp2 = LegalizeOp(Node->getOperand(2));
1046 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(2))
1047 Result = DAG.getNode(ISD::CopyToReg, MVT::Other, Tmp1,
1048 Node->getOperand(1), Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +00001049 break;
1050
1051 case ISD::RET:
1052 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1053 switch (Node->getNumOperands()) {
1054 case 2: // ret val
1055 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1056 case Legal:
1057 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +00001058 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +00001059 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1060 break;
1061 case Expand: {
1062 SDOperand Lo, Hi;
1063 ExpandOp(Node->getOperand(1), Lo, Hi);
1064 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001065 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001066 }
1067 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001068 Tmp2 = PromoteOp(Node->getOperand(1));
1069 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
1070 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001071 }
1072 break;
1073 case 1: // ret void
1074 if (Tmp1 != Node->getOperand(0))
1075 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
1076 break;
1077 default: { // ret <values>
1078 std::vector<SDOperand> NewValues;
1079 NewValues.push_back(Tmp1);
1080 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
1081 switch (getTypeAction(Node->getOperand(i).getValueType())) {
1082 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +00001083 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +00001084 break;
1085 case Expand: {
1086 SDOperand Lo, Hi;
1087 ExpandOp(Node->getOperand(i), Lo, Hi);
1088 NewValues.push_back(Lo);
1089 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +00001090 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001091 }
1092 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001093 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +00001094 }
1095 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
1096 break;
1097 }
1098 }
1099 break;
1100 case ISD::STORE:
1101 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1102 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1103
Chris Lattnere69daaf2005-01-08 06:25:56 +00001104 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001105 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +00001106 if (CFP->getValueType(0) == MVT::f32) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001107 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001108 DAG.getConstant(FloatToBits(CFP->getValue()),
1109 MVT::i32),
1110 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001111 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001112 } else {
1113 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001114 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Jim Laskeyb74c6662005-08-17 19:34:49 +00001115 DAG.getConstant(DoubleToBits(CFP->getValue()),
1116 MVT::i64),
1117 Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +00001118 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +00001119 }
Chris Lattnera4743132005-02-22 07:23:39 +00001120 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +00001121 }
1122
Chris Lattnerdc750592005-01-07 07:47:09 +00001123 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1124 case Legal: {
1125 SDOperand Val = LegalizeOp(Node->getOperand(1));
1126 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
1127 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +00001128 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
1129 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +00001130 break;
1131 }
1132 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001133 // Truncate the value and store the result.
1134 Tmp3 = PromoteOp(Node->getOperand(1));
1135 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001136 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +00001137 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001138 break;
1139
Chris Lattnerdc750592005-01-07 07:47:09 +00001140 case Expand:
1141 SDOperand Lo, Hi;
1142 ExpandOp(Node->getOperand(1), Lo, Hi);
1143
1144 if (!TLI.isLittleEndian())
1145 std::swap(Lo, Hi);
1146
Chris Lattner55e9cde2005-05-11 04:51:16 +00001147 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
1148 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001149 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00001150 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
1151 getIntPtrConstant(IncrementSize));
1152 assert(isTypeLegal(Tmp2.getValueType()) &&
1153 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00001154 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +00001155 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
1156 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +00001157 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
1158 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001159 }
1160 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +00001161 case ISD::PCMARKER:
1162 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +00001163 if (Tmp1 != Node->getOperand(0))
1164 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +00001165 break;
Andrew Lenharth01aa5632005-11-11 16:47:30 +00001166 case ISD::READCYCLECOUNTER:
1167 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain
1168 if (Tmp1 != Node->getOperand(0))
1169 Result = DAG.getNode(ISD::READCYCLECOUNTER, MVT::i64, Tmp1);
1170 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001171 case ISD::TRUNCSTORE:
1172 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1173 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
1174
1175 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1176 case Legal:
1177 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001178
1179 // The only promote case we handle is TRUNCSTORE:i1 X into
1180 // -> TRUNCSTORE:i8 (and X, 1)
1181 if (cast<VTSDNode>(Node->getOperand(4))->getVT() == MVT::i1 &&
1182 TLI.getOperationAction(ISD::TRUNCSTORE, MVT::i1) ==
1183 TargetLowering::Promote) {
1184 // Promote the bool to a mask then store.
1185 Tmp2 = DAG.getNode(ISD::AND, Tmp2.getValueType(), Tmp2,
1186 DAG.getConstant(1, Tmp2.getValueType()));
1187 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
1188 Node->getOperand(3), DAG.getValueType(MVT::i8));
1189
1190 } else if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1191 Tmp3 != Node->getOperand(2)) {
Chris Lattner99222f72005-01-15 07:15:18 +00001192 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001193 Node->getOperand(3), Node->getOperand(4));
Chris Lattner2d454bf2005-09-10 00:20:18 +00001194 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001195 break;
1196 case Promote:
1197 case Expand:
1198 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
1199 }
1200 break;
Chris Lattner39c67442005-01-14 22:08:15 +00001201 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001202 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1203 case Expand: assert(0 && "It's impossible to expand bools");
1204 case Legal:
1205 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
1206 break;
1207 case Promote:
1208 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1209 break;
1210 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001211 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +00001212 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +00001213
Nate Begeman987121a2005-08-23 04:29:48 +00001214 switch (TLI.getOperationAction(ISD::SELECT, Tmp2.getValueType())) {
Chris Lattner3c0dd462005-01-16 07:29:19 +00001215 default: assert(0 && "This action is not supported yet!");
Nate Begemane5b86d72005-08-10 20:51:12 +00001216 case TargetLowering::Expand:
1217 if (Tmp1.getOpcode() == ISD::SETCC) {
1218 Result = DAG.getSelectCC(Tmp1.getOperand(0), Tmp1.getOperand(1),
1219 Tmp2, Tmp3,
1220 cast<CondCodeSDNode>(Tmp1.getOperand(2))->get());
1221 } else {
Chris Lattner539c3fa2005-08-21 18:03:09 +00001222 // Make sure the condition is either zero or one. It may have been
1223 // promoted from something else.
1224 Tmp1 = DAG.getZeroExtendInReg(Tmp1, MVT::i1);
Nate Begemane5b86d72005-08-10 20:51:12 +00001225 Result = DAG.getSelectCC(Tmp1,
1226 DAG.getConstant(0, Tmp1.getValueType()),
1227 Tmp2, Tmp3, ISD::SETNE);
1228 }
1229 break;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001230 case TargetLowering::Legal:
1231 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1232 Tmp3 != Node->getOperand(2))
1233 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
1234 Tmp1, Tmp2, Tmp3);
1235 break;
1236 case TargetLowering::Promote: {
1237 MVT::ValueType NVT =
1238 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
1239 unsigned ExtOp, TruncOp;
1240 if (MVT::isInteger(Tmp2.getValueType())) {
Chris Lattner7753f172005-09-02 00:18:10 +00001241 ExtOp = ISD::ANY_EXTEND;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001242 TruncOp = ISD::TRUNCATE;
1243 } else {
1244 ExtOp = ISD::FP_EXTEND;
1245 TruncOp = ISD::FP_ROUND;
1246 }
1247 // Promote each of the values to the new type.
1248 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
1249 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
1250 // Perform the larger operation, then round down.
1251 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
1252 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
1253 break;
1254 }
1255 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001256 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001257 case ISD::SELECT_CC:
1258 Tmp3 = LegalizeOp(Node->getOperand(2)); // True
1259 Tmp4 = LegalizeOp(Node->getOperand(3)); // False
1260
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001261 if (isTypeLegal(Node->getOperand(0).getValueType())) {
Chris Lattner5f573412005-08-26 00:23:59 +00001262 // Everything is legal, see if we should expand this op or something.
1263 switch (TLI.getOperationAction(ISD::SELECT_CC,
1264 Node->getOperand(0).getValueType())) {
1265 default: assert(0 && "This action is not supported yet!");
1266 case TargetLowering::Custom: {
1267 SDOperand Tmp =
1268 TLI.LowerOperation(DAG.getNode(ISD::SELECT_CC, Node->getValueType(0),
1269 Node->getOperand(0),
1270 Node->getOperand(1), Tmp3, Tmp4,
Chris Lattnerc6d481d2005-08-26 00:43:46 +00001271 Node->getOperand(4)), DAG);
Chris Lattner5f573412005-08-26 00:23:59 +00001272 if (Tmp.Val) {
1273 Result = LegalizeOp(Tmp);
1274 break;
1275 }
1276 } // FALLTHROUGH if the target can't lower this operation after all.
1277 case TargetLowering::Legal:
1278 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1279 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1280 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1281 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3)) {
1282 Result = DAG.getNode(ISD::SELECT_CC, Node->getValueType(0), Tmp1, Tmp2,
1283 Tmp3, Tmp4, Node->getOperand(4));
1284 }
1285 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00001286 }
1287 break;
1288 } else {
1289 Tmp1 = LegalizeOp(DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),
1290 Node->getOperand(0), // LHS
1291 Node->getOperand(1), // RHS
1292 Node->getOperand(4)));
Nate Begeman371e4952005-08-16 19:49:35 +00001293 // If we get a SETCC back from legalizing the SETCC node we just
1294 // created, then use its LHS, RHS, and CC directly in creating a new
1295 // node. Otherwise, select between the true and false value based on
1296 // comparing the result of the legalized with zero.
1297 if (Tmp1.getOpcode() == ISD::SETCC) {
1298 Result = DAG.getNode(ISD::SELECT_CC, Tmp3.getValueType(),
1299 Tmp1.getOperand(0), Tmp1.getOperand(1),
1300 Tmp3, Tmp4, Tmp1.getOperand(2));
1301 } else {
1302 Result = DAG.getSelectCC(Tmp1,
1303 DAG.getConstant(0, Tmp1.getValueType()),
1304 Tmp3, Tmp4, ISD::SETNE);
1305 }
Nate Begemane5b86d72005-08-10 20:51:12 +00001306 }
1307 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001308 case ISD::SETCC:
1309 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1310 case Legal:
1311 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1312 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
Chris Lattnerdc750592005-01-07 07:47:09 +00001313 break;
1314 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +00001315 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
1316 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
1317
1318 // If this is an FP compare, the operands have already been extended.
1319 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
1320 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001321 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001322
1323 // Otherwise, we have to insert explicit sign or zero extends. Note
1324 // that we could insert sign extends for ALL conditions, but zero extend
1325 // is cheaper on many machines (an AND instead of two shifts), so prefer
1326 // it.
Chris Lattnerd47675e2005-08-09 20:20:18 +00001327 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattner4d978642005-01-15 22:16:26 +00001328 default: assert(0 && "Unknown integer comparison!");
1329 case ISD::SETEQ:
1330 case ISD::SETNE:
1331 case ISD::SETUGE:
1332 case ISD::SETUGT:
1333 case ISD::SETULE:
1334 case ISD::SETULT:
1335 // ALL of these operations will work if we either sign or zero extend
1336 // the operands (including the unsigned comparisons!). Zero extend is
1337 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +00001338 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1339 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001340 break;
1341 case ISD::SETGE:
1342 case ISD::SETGT:
1343 case ISD::SETLT:
1344 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +00001345 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1346 DAG.getValueType(VT));
1347 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1348 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001349 break;
1350 }
Chris Lattner4d978642005-01-15 22:16:26 +00001351 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001352 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001353 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +00001354 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
1355 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
1356 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001357 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001358 case ISD::SETEQ:
1359 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +00001360 if (RHSLo == RHSHi)
1361 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
1362 if (RHSCST->isAllOnesValue()) {
1363 // Comparison to -1.
1364 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Nate Begeman987121a2005-08-23 04:29:48 +00001365 Tmp2 = RHSLo;
Misha Brukman835702a2005-04-21 22:36:52 +00001366 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +00001367 }
1368
Chris Lattnerdc750592005-01-07 07:47:09 +00001369 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
1370 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
1371 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Nate Begeman987121a2005-08-23 04:29:48 +00001372 Tmp2 = DAG.getConstant(0, Tmp1.getValueType());
Chris Lattnerdc750592005-01-07 07:47:09 +00001373 break;
1374 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +00001375 // If this is a comparison of the sign bit, just look at the top part.
1376 // X > -1, x < 0
1377 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Chris Lattnerd47675e2005-08-09 20:20:18 +00001378 if ((cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +00001379 CST->getValue() == 0) || // X < 0
Chris Lattnerd47675e2005-08-09 20:20:18 +00001380 (cast<CondCodeSDNode>(Node->getOperand(2))->get() == ISD::SETGT &&
Nate Begeman987121a2005-08-23 04:29:48 +00001381 (CST->isAllOnesValue()))) { // X > -1
1382 Tmp1 = LHSHi;
1383 Tmp2 = RHSHi;
1384 break;
1385 }
Chris Lattneraedcabe2005-04-12 02:19:10 +00001386
Chris Lattnerdc750592005-01-07 07:47:09 +00001387 // FIXME: This generated code sucks.
1388 ISD::CondCode LowCC;
Chris Lattnerd47675e2005-08-09 20:20:18 +00001389 switch (cast<CondCodeSDNode>(Node->getOperand(2))->get()) {
Chris Lattnerdc750592005-01-07 07:47:09 +00001390 default: assert(0 && "Unknown integer setcc!");
1391 case ISD::SETLT:
1392 case ISD::SETULT: LowCC = ISD::SETULT; break;
1393 case ISD::SETGT:
1394 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
1395 case ISD::SETLE:
1396 case ISD::SETULE: LowCC = ISD::SETULE; break;
1397 case ISD::SETGE:
1398 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
1399 }
Misha Brukman835702a2005-04-21 22:36:52 +00001400
Chris Lattnerdc750592005-01-07 07:47:09 +00001401 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
1402 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
1403 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
1404
1405 // NOTE: on targets without efficient SELECT of bools, we can always use
1406 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001407 Tmp1 = DAG.getSetCC(Node->getValueType(0), LHSLo, RHSLo, LowCC);
1408 Tmp2 = DAG.getNode(ISD::SETCC, Node->getValueType(0), LHSHi, RHSHi,
1409 Node->getOperand(2));
1410 Result = DAG.getSetCC(Node->getValueType(0), LHSHi, RHSHi, ISD::SETEQ);
Nate Begeman987121a2005-08-23 04:29:48 +00001411 Result = LegalizeOp(DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
1412 Result, Tmp1, Tmp2));
1413 return Result;
Chris Lattnerdc750592005-01-07 07:47:09 +00001414 }
1415 }
Nate Begeman987121a2005-08-23 04:29:48 +00001416
1417 switch(TLI.getOperationAction(ISD::SETCC, Node->getOperand(0).getValueType())) {
1418 default:
1419 assert(0 && "Cannot handle this action for SETCC yet!");
1420 break;
Andrew Lenharth835cbb32005-08-29 20:46:51 +00001421 case TargetLowering::Promote:
1422 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1423 Node->getOperand(2));
1424 break;
Nate Begeman987121a2005-08-23 04:29:48 +00001425 case TargetLowering::Legal:
1426 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1427 Result = DAG.getNode(ISD::SETCC, Node->getValueType(0), Tmp1, Tmp2,
1428 Node->getOperand(2));
1429 break;
1430 case TargetLowering::Expand:
1431 // Expand a setcc node into a select_cc of the same condition, lhs, and
1432 // rhs that selects between const 1 (true) and const 0 (false).
1433 MVT::ValueType VT = Node->getValueType(0);
1434 Result = DAG.getNode(ISD::SELECT_CC, VT, Tmp1, Tmp2,
1435 DAG.getConstant(1, VT), DAG.getConstant(0, VT),
1436 Node->getOperand(2));
1437 Result = LegalizeOp(Result);
1438 break;
1439 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001440 break;
1441
Chris Lattner85d70c62005-01-11 05:57:22 +00001442 case ISD::MEMSET:
1443 case ISD::MEMCPY:
1444 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001445 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001446 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1447
1448 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1449 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1450 case Expand: assert(0 && "Cannot expand a byte!");
1451 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001452 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001453 break;
1454 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001455 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001456 break;
1457 }
1458 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001459 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001460 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001461
1462 SDOperand Tmp4;
1463 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001464 case Expand: {
1465 // Length is too big, just take the lo-part of the length.
1466 SDOperand HiPart;
1467 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1468 break;
1469 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001470 case Legal:
1471 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001472 break;
1473 case Promote:
1474 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001475 break;
1476 }
1477
1478 SDOperand Tmp5;
1479 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1480 case Expand: assert(0 && "Cannot expand this yet!");
1481 case Legal:
1482 Tmp5 = LegalizeOp(Node->getOperand(4));
1483 break;
1484 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001485 Tmp5 = PromoteOp(Node->getOperand(4));
1486 break;
1487 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001488
1489 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1490 default: assert(0 && "This action not implemented for this operation!");
Chris Lattnerdff50ca2005-08-26 00:14:16 +00001491 case TargetLowering::Custom: {
1492 SDOperand Tmp =
1493 TLI.LowerOperation(DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1,
1494 Tmp2, Tmp3, Tmp4, Tmp5), DAG);
1495 if (Tmp.Val) {
1496 Result = LegalizeOp(Tmp);
1497 break;
1498 }
1499 // FALLTHROUGH if the target thinks it is legal.
1500 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001501 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001502 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1503 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1504 Tmp5 != Node->getOperand(4)) {
1505 std::vector<SDOperand> Ops;
1506 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1507 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1508 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1509 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001510 break;
1511 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001512 // Otherwise, the target does not support this operation. Lower the
1513 // operation to an explicit libcall as appropriate.
1514 MVT::ValueType IntPtr = TLI.getPointerTy();
1515 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1516 std::vector<std::pair<SDOperand, const Type*> > Args;
1517
Reid Spencer6dced922005-01-12 14:53:45 +00001518 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001519 if (Node->getOpcode() == ISD::MEMSET) {
1520 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1521 // Extend the ubyte argument to be an int value for the call.
1522 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1523 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1524 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1525
1526 FnName = "memset";
1527 } else if (Node->getOpcode() == ISD::MEMCPY ||
1528 Node->getOpcode() == ISD::MEMMOVE) {
1529 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1530 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1531 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1532 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1533 } else {
1534 assert(0 && "Unknown op!");
1535 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001536
Chris Lattner85d70c62005-01-11 05:57:22 +00001537 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001538 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001539 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001540 Result = CallResult.second;
1541 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001542 break;
1543 }
Chris Lattner85d70c62005-01-11 05:57:22 +00001544 }
1545 break;
1546 }
Chris Lattner5385db52005-05-09 20:23:03 +00001547
1548 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001549 Tmp1 = LegalizeOp(Node->getOperand(0));
1550 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001551
Chris Lattner86535992005-05-14 07:45:46 +00001552 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1553 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1554 std::vector<SDOperand> Ops;
1555 Ops.push_back(Tmp1);
1556 Ops.push_back(Tmp2);
1557 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1558 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001559 Result = SDOperand(Node, 0);
1560 // Since these produce two values, make sure to remember that we legalized
1561 // both of them.
1562 AddLegalizedOperand(SDOperand(Node, 0), Result);
1563 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1564 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001565 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001566 Tmp1 = LegalizeOp(Node->getOperand(0));
1567 Tmp2 = LegalizeOp(Node->getOperand(1));
1568 Tmp3 = LegalizeOp(Node->getOperand(2));
1569 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1570 Tmp3 != Node->getOperand(2))
1571 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1572 break;
1573
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001574 case ISD::READIO:
1575 Tmp1 = LegalizeOp(Node->getOperand(0));
1576 Tmp2 = LegalizeOp(Node->getOperand(1));
1577
1578 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1579 case TargetLowering::Custom:
1580 default: assert(0 && "This action not implemented for this operation!");
1581 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001582 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1583 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1584 std::vector<SDOperand> Ops;
1585 Ops.push_back(Tmp1);
1586 Ops.push_back(Tmp2);
1587 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1588 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001589 Result = SDOperand(Node, 0);
1590 break;
1591 case TargetLowering::Expand:
1592 // Replace this with a load from memory.
1593 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1594 Node->getOperand(1), DAG.getSrcValue(NULL));
1595 Result = LegalizeOp(Result);
1596 break;
1597 }
1598
1599 // Since these produce two values, make sure to remember that we legalized
1600 // both of them.
1601 AddLegalizedOperand(SDOperand(Node, 0), Result);
1602 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1603 return Result.getValue(Op.ResNo);
1604
1605 case ISD::WRITEIO:
1606 Tmp1 = LegalizeOp(Node->getOperand(0));
1607 Tmp2 = LegalizeOp(Node->getOperand(1));
1608 Tmp3 = LegalizeOp(Node->getOperand(2));
1609
1610 switch (TLI.getOperationAction(Node->getOpcode(),
1611 Node->getOperand(1).getValueType())) {
1612 case TargetLowering::Custom:
1613 default: assert(0 && "This action not implemented for this operation!");
1614 case TargetLowering::Legal:
1615 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1616 Tmp3 != Node->getOperand(2))
1617 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1618 break;
1619 case TargetLowering::Expand:
1620 // Replace this with a store to memory.
1621 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1622 Node->getOperand(1), Node->getOperand(2),
1623 DAG.getSrcValue(NULL));
1624 Result = LegalizeOp(Result);
1625 break;
1626 }
1627 break;
1628
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001629 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001630 case ISD::SUB_PARTS:
1631 case ISD::SHL_PARTS:
1632 case ISD::SRA_PARTS:
1633 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001634 std::vector<SDOperand> Ops;
1635 bool Changed = false;
1636 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1637 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1638 Changed |= Ops.back() != Node->getOperand(i);
1639 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001640 if (Changed) {
1641 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1642 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1643 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001644
1645 // Since these produce multiple values, make sure to remember that we
1646 // legalized all of them.
1647 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1648 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1649 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001650 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001651
1652 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001653 case ISD::ADD:
1654 case ISD::SUB:
1655 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001656 case ISD::MULHS:
1657 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001658 case ISD::UDIV:
1659 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001660 case ISD::AND:
1661 case ISD::OR:
1662 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001663 case ISD::SHL:
1664 case ISD::SRL:
1665 case ISD::SRA:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001666 case ISD::FADD:
1667 case ISD::FSUB:
1668 case ISD::FMUL:
1669 case ISD::FDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001670 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001671 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1672 case Expand: assert(0 && "Not possible");
1673 case Legal:
1674 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1675 break;
1676 case Promote:
1677 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1678 break;
1679 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001680 if (Tmp1 != Node->getOperand(0) ||
1681 Tmp2 != Node->getOperand(1))
1682 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1683 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001684
Nate Begemanb2e089c2005-11-19 00:36:38 +00001685 // Vector binary operators
1686 case ISD::VADD:
1687 case ISD::VSUB:
1688 case ISD::VMUL: {
1689 Tmp1 = Node->getOperand(0); // Element Count
1690 Tmp2 = Node->getOperand(1); // Element Type
1691
1692 // If we just have one element, scalarize the result. Otherwise, check to
1693 // see if we support this operation on this type at this width. If not,
1694 // split the vector in half and try again.
1695 if (1 == cast<ConstantSDNode>(Tmp1)->getValue()) {
1696 MVT::ValueType SVT = cast<VTSDNode>(Tmp2)->getVT();
1697
Chris Lattner301015a2005-11-19 05:51:46 +00001698 Result = DAG.getNode(getScalarizedOpcode(Node->getOpcode(), SVT), SVT,
Nate Begemanb2e089c2005-11-19 00:36:38 +00001699 LegalizeOp(Node->getOperand(2)),
1700 LegalizeOp(Node->getOperand(3)));
1701 } else {
1702 assert(0 && "Expand case for vectors unimplemented");
1703 }
1704 break;
1705 }
1706
Nate Begemanbd5f41a2005-10-18 00:27:41 +00001707 case ISD::BUILD_PAIR: {
1708 MVT::ValueType PairTy = Node->getValueType(0);
1709 // TODO: handle the case where the Lo and Hi operands are not of legal type
1710 Tmp1 = LegalizeOp(Node->getOperand(0)); // Lo
1711 Tmp2 = LegalizeOp(Node->getOperand(1)); // Hi
1712 switch (TLI.getOperationAction(ISD::BUILD_PAIR, PairTy)) {
1713 case TargetLowering::Legal:
1714 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
1715 Result = DAG.getNode(ISD::BUILD_PAIR, PairTy, Tmp1, Tmp2);
1716 break;
1717 case TargetLowering::Promote:
1718 case TargetLowering::Custom:
1719 assert(0 && "Cannot promote/custom this yet!");
1720 case TargetLowering::Expand:
1721 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, PairTy, Tmp1);
1722 Tmp2 = DAG.getNode(ISD::ANY_EXTEND, PairTy, Tmp2);
1723 Tmp2 = DAG.getNode(ISD::SHL, PairTy, Tmp2,
1724 DAG.getConstant(MVT::getSizeInBits(PairTy)/2,
1725 TLI.getShiftAmountTy()));
1726 Result = LegalizeOp(DAG.getNode(ISD::OR, PairTy, Tmp1, Tmp2));
1727 break;
1728 }
1729 break;
1730 }
1731
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001732 case ISD::UREM:
1733 case ISD::SREM:
Chris Lattner6f3b5772005-09-28 22:28:18 +00001734 case ISD::FREM:
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001735 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1736 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1737 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1738 case TargetLowering::Legal:
1739 if (Tmp1 != Node->getOperand(0) ||
1740 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001741 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001742 Tmp2);
1743 break;
1744 case TargetLowering::Promote:
1745 case TargetLowering::Custom:
1746 assert(0 && "Cannot promote/custom handle this yet!");
Chris Lattner81914422005-08-03 20:31:37 +00001747 case TargetLowering::Expand:
1748 if (MVT::isInteger(Node->getValueType(0))) {
1749 MVT::ValueType VT = Node->getValueType(0);
1750 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1751 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1752 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1753 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1754 } else {
1755 // Floating point mod -> fmod libcall.
1756 const char *FnName = Node->getValueType(0) == MVT::f32 ? "fmodf":"fmod";
1757 SDOperand Dummy;
1758 Result = ExpandLibCall(FnName, Node, Dummy);
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001759 }
1760 break;
1761 }
1762 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001763
Andrew Lenharth5e177822005-05-03 17:19:30 +00001764 case ISD::CTPOP:
1765 case ISD::CTTZ:
1766 case ISD::CTLZ:
1767 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1768 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1769 case TargetLowering::Legal:
1770 if (Tmp1 != Node->getOperand(0))
1771 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1772 break;
1773 case TargetLowering::Promote: {
1774 MVT::ValueType OVT = Tmp1.getValueType();
1775 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001776
1777 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001778 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1779 // Perform the larger operation, then subtract if needed.
1780 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1781 switch(Node->getOpcode())
1782 {
1783 case ISD::CTPOP:
1784 Result = Tmp1;
1785 break;
1786 case ISD::CTTZ:
1787 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Chris Lattnerd47675e2005-08-09 20:20:18 +00001788 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
1789 DAG.getConstant(getSizeInBits(NVT), NVT),
1790 ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001791 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001792 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1793 break;
1794 case ISD::CTLZ:
1795 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001796 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1797 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001798 getSizeInBits(OVT), NVT));
1799 break;
1800 }
1801 break;
1802 }
1803 case TargetLowering::Custom:
1804 assert(0 && "Cannot custom handle this yet!");
1805 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001806 switch(Node->getOpcode())
1807 {
1808 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001809 static const uint64_t mask[6] = {
1810 0x5555555555555555ULL, 0x3333333333333333ULL,
1811 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1812 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1813 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001814 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001815 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1816 unsigned len = getSizeInBits(VT);
1817 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001818 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001819 Tmp2 = DAG.getConstant(mask[i], VT);
1820 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001821 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001822 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1823 DAG.getNode(ISD::AND, VT,
1824 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1825 Tmp2));
1826 }
1827 Result = Tmp1;
1828 break;
1829 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001830 case ISD::CTLZ: {
1831 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001832 x = x | (x >> 1);
1833 x = x | (x >> 2);
1834 ...
1835 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001836 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001837 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001838
Chris Lattner56add052005-05-11 18:35:21 +00001839 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1840 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001841 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1842 unsigned len = getSizeInBits(VT);
1843 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1844 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001845 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001846 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1847 }
1848 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001849 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001850 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001851 }
1852 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001853 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001854 // unless the target has ctlz but not ctpop, in which case we use:
1855 // { return 32 - nlz(~x & (x-1)); }
1856 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001857 MVT::ValueType VT = Tmp1.getValueType();
1858 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001859 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001860 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1861 DAG.getNode(ISD::SUB, VT, Tmp1,
1862 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001863 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00001864 if (!TLI.isOperationLegal(ISD::CTPOP, VT) &&
1865 TLI.isOperationLegal(ISD::CTLZ, VT)) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001866 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001867 DAG.getConstant(getSizeInBits(VT), VT),
1868 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1869 } else {
1870 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1871 }
Chris Lattner72473242005-05-11 05:27:09 +00001872 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001873 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001874 default:
1875 assert(0 && "Cannot expand this yet!");
1876 break;
1877 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001878 break;
1879 }
1880 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001881
Chris Lattner13fe99c2005-04-02 05:00:07 +00001882 // Unary operators
1883 case ISD::FABS:
1884 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001885 case ISD::FSQRT:
1886 case ISD::FSIN:
1887 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001888 Tmp1 = LegalizeOp(Node->getOperand(0));
1889 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1890 case TargetLowering::Legal:
1891 if (Tmp1 != Node->getOperand(0))
1892 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1893 break;
1894 case TargetLowering::Promote:
1895 case TargetLowering::Custom:
1896 assert(0 && "Cannot promote/custom handle this yet!");
1897 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001898 switch(Node->getOpcode()) {
1899 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001900 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1901 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
Chris Lattner6f3b5772005-09-28 22:28:18 +00001902 Result = LegalizeOp(DAG.getNode(ISD::FSUB, Node->getValueType(0),
Chris Lattner13fe99c2005-04-02 05:00:07 +00001903 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001904 break;
1905 }
1906 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001907 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1908 MVT::ValueType VT = Node->getValueType(0);
1909 Tmp2 = DAG.getConstantFP(0.0, VT);
Chris Lattnerd47675e2005-08-09 20:20:18 +00001910 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1, Tmp2, ISD::SETUGT);
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001911 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1912 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1913 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001914 break;
1915 }
1916 case ISD::FSQRT:
1917 case ISD::FSIN:
1918 case ISD::FCOS: {
1919 MVT::ValueType VT = Node->getValueType(0);
Chris Lattner80026402005-04-30 04:43:14 +00001920 const char *FnName = 0;
1921 switch(Node->getOpcode()) {
1922 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1923 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1924 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1925 default: assert(0 && "Unreachable!");
1926 }
Nate Begeman77558da2005-08-04 21:43:28 +00001927 SDOperand Dummy;
1928 Result = ExpandLibCall(FnName, Node, Dummy);
Chris Lattner80026402005-04-30 04:43:14 +00001929 break;
1930 }
1931 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001932 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001933 }
1934 break;
1935 }
1936 break;
1937
1938 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001939 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001940 case ISD::UINT_TO_FP: {
1941 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001942 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1943 case Legal:
Jeff Cohen546fd592005-07-30 18:33:25 +00001944 switch (TLI.getOperationAction(Node->getOpcode(),
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001945 Node->getOperand(0).getValueType())) {
1946 default: assert(0 && "Unknown operation action!");
1947 case TargetLowering::Expand:
Jim Laskeyf2516a92005-08-17 00:39:29 +00001948 Result = ExpandLegalINT_TO_FP(isSigned,
1949 LegalizeOp(Node->getOperand(0)),
1950 Node->getValueType(0));
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001951 AddLegalizedOperand(Op, Result);
1952 return Result;
1953 case TargetLowering::Promote:
1954 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1955 Node->getValueType(0),
1956 isSigned);
1957 AddLegalizedOperand(Op, Result);
1958 return Result;
1959 case TargetLowering::Legal:
1960 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001961 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001962
Chris Lattnerdc750592005-01-07 07:47:09 +00001963 Tmp1 = LegalizeOp(Node->getOperand(0));
1964 if (Tmp1 != Node->getOperand(0))
1965 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1966 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001967 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001968 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1969 Node->getValueType(0), Node->getOperand(0));
1970 break;
1971 case Promote:
1972 if (isSigned) {
1973 Result = PromoteOp(Node->getOperand(0));
1974 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1975 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1976 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1977 } else {
1978 Result = PromoteOp(Node->getOperand(0));
1979 Result = DAG.getZeroExtendInReg(Result,
1980 Node->getOperand(0).getValueType());
1981 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001982 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001983 break;
1984 }
1985 break;
1986 }
1987 case ISD::TRUNCATE:
1988 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1989 case Legal:
1990 Tmp1 = LegalizeOp(Node->getOperand(0));
1991 if (Tmp1 != Node->getOperand(0))
1992 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1993 break;
1994 case Expand:
1995 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1996
1997 // Since the result is legal, we should just be able to truncate the low
1998 // part of the source.
1999 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
2000 break;
2001 case Promote:
2002 Result = PromoteOp(Node->getOperand(0));
2003 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
2004 break;
2005 }
2006 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002007
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002008 case ISD::FP_TO_SINT:
2009 case ISD::FP_TO_UINT:
2010 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2011 case Legal:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002012 Tmp1 = LegalizeOp(Node->getOperand(0));
2013
Chris Lattner44fe26f2005-07-29 00:11:56 +00002014 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))){
2015 default: assert(0 && "Unknown operation action!");
2016 case TargetLowering::Expand:
Nate Begeman36853ee2005-08-14 01:20:53 +00002017 if (Node->getOpcode() == ISD::FP_TO_UINT) {
2018 SDOperand True, False;
2019 MVT::ValueType VT = Node->getOperand(0).getValueType();
2020 MVT::ValueType NVT = Node->getValueType(0);
2021 unsigned ShiftAmt = MVT::getSizeInBits(Node->getValueType(0))-1;
2022 Tmp2 = DAG.getConstantFP((double)(1ULL << ShiftAmt), VT);
2023 Tmp3 = DAG.getSetCC(TLI.getSetCCResultTy(),
2024 Node->getOperand(0), Tmp2, ISD::SETLT);
2025 True = DAG.getNode(ISD::FP_TO_SINT, NVT, Node->getOperand(0));
2026 False = DAG.getNode(ISD::FP_TO_SINT, NVT,
Chris Lattner6f3b5772005-09-28 22:28:18 +00002027 DAG.getNode(ISD::FSUB, VT, Node->getOperand(0),
Nate Begeman36853ee2005-08-14 01:20:53 +00002028 Tmp2));
2029 False = DAG.getNode(ISD::XOR, NVT, False,
2030 DAG.getConstant(1ULL << ShiftAmt, NVT));
2031 Result = LegalizeOp(DAG.getNode(ISD::SELECT, NVT, Tmp3, True, False));
Nate Begemand5e739d2005-08-14 18:38:32 +00002032 return Result;
Nate Begeman36853ee2005-08-14 01:20:53 +00002033 } else {
2034 assert(0 && "Do not know how to expand FP_TO_SINT yet!");
2035 }
2036 break;
Chris Lattner44fe26f2005-07-29 00:11:56 +00002037 case TargetLowering::Promote:
Chris Lattnerf59b2da2005-07-30 00:04:12 +00002038 Result = PromoteLegalFP_TO_INT(Tmp1, Node->getValueType(0),
Chris Lattner44fe26f2005-07-29 00:11:56 +00002039 Node->getOpcode() == ISD::FP_TO_SINT);
2040 AddLegalizedOperand(Op, Result);
2041 return Result;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002042 case TargetLowering::Custom: {
2043 SDOperand Tmp =
2044 DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2045 Tmp = TLI.LowerOperation(Tmp, DAG);
2046 if (Tmp.Val) {
2047 AddLegalizedOperand(Op, Tmp);
2048 NeedsAnotherIteration = true;
Chris Lattnerdcde1b22005-08-29 17:30:00 +00002049 return Tmp;
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002050 } else {
2051 // The target thinks this is legal afterall.
2052 break;
2053 }
2054 }
Chris Lattner44fe26f2005-07-29 00:11:56 +00002055 case TargetLowering::Legal:
2056 break;
2057 }
Jeff Cohen546fd592005-07-30 18:33:25 +00002058
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002059 if (Tmp1 != Node->getOperand(0))
2060 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2061 break;
2062 case Expand:
2063 assert(0 && "Shouldn't need to expand other operators here!");
2064 case Promote:
2065 Result = PromoteOp(Node->getOperand(0));
2066 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2067 break;
2068 }
2069 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00002070
Chris Lattner7753f172005-09-02 00:18:10 +00002071 case ISD::ANY_EXTEND:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00002072 case ISD::ZERO_EXTEND:
2073 case ISD::SIGN_EXTEND:
2074 case ISD::FP_EXTEND:
2075 case ISD::FP_ROUND:
2076 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2077 case Legal:
2078 Tmp1 = LegalizeOp(Node->getOperand(0));
2079 if (Tmp1 != Node->getOperand(0))
2080 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
2081 break;
2082 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00002083 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00002084
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002085 case Promote:
2086 switch (Node->getOpcode()) {
Chris Lattner7753f172005-09-02 00:18:10 +00002087 case ISD::ANY_EXTEND:
2088 Result = PromoteOp(Node->getOperand(0));
2089 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
2090 break;
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002091 case ISD::ZERO_EXTEND:
2092 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002093 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00002094 Result = DAG.getZeroExtendInReg(Result,
2095 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002096 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002097 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002098 Result = PromoteOp(Node->getOperand(0));
Chris Lattner7753f172005-09-02 00:18:10 +00002099 Result = DAG.getNode(ISD::ANY_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002100 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002101 Result,
2102 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002103 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002104 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00002105 Result = PromoteOp(Node->getOperand(0));
2106 if (Result.getValueType() != Op.getValueType())
2107 // Dynamically dead while we have only 2 FP types.
2108 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
2109 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002110 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00002111 Result = PromoteOp(Node->getOperand(0));
2112 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
2113 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002114 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002115 }
2116 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002117 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00002118 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002119 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002120 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00002121
2122 // If this operation is not supported, convert it to a shl/shr or load/store
2123 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00002124 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
2125 default: assert(0 && "This action not supported for this op yet!");
2126 case TargetLowering::Legal:
2127 if (Tmp1 != Node->getOperand(0))
2128 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002129 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00002130 break;
2131 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00002132 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00002133 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00002134 // NOTE: we could fall back on load/store here too for targets without
2135 // SAR. However, it is doubtful that any exist.
2136 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
2137 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00002138 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00002139 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
2140 Node->getOperand(0), ShiftCst);
2141 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
2142 Result, ShiftCst);
2143 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
2144 // The only way we can lower this is to turn it into a STORETRUNC,
2145 // EXTLOAD pair, targetting a temporary location (a stack slot).
2146
2147 // NOTE: there is a choice here between constantly creating new stack
2148 // slots and always reusing the same one. We currently always create
2149 // new ones, as reuse may inhibit scheduling.
2150 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
2151 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
2152 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
2153 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00002154 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00002155 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
2156 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
2157 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00002158 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00002159 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002160 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
2161 Result, StackSlot, DAG.getSrcValue(NULL),
2162 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00002163 } else {
2164 assert(0 && "Unknown op");
2165 }
2166 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00002167 break;
Chris Lattner99222f72005-01-15 07:15:18 +00002168 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002169 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002170 }
Chris Lattner99222f72005-01-15 07:15:18 +00002171 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002172
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002173 // Note that LegalizeOp may be reentered even from single-use nodes, which
2174 // means that we always must cache transformed nodes.
2175 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00002176 return Result;
2177}
2178
Chris Lattner4d978642005-01-15 22:16:26 +00002179/// PromoteOp - Given an operation that produces a value in an invalid type,
2180/// promote it to compute the value into a larger type. The produced value will
2181/// have the correct bits for the low portion of the register, but no guarantee
2182/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002183SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
2184 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002185 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002186 assert(getTypeAction(VT) == Promote &&
2187 "Caller should expand or legalize operands that are not promotable!");
2188 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
2189 "Cannot promote to smaller type!");
2190
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002191 SDOperand Tmp1, Tmp2, Tmp3;
2192
2193 SDOperand Result;
2194 SDNode *Node = Op.Val;
2195
Chris Lattner1a570f12005-09-02 20:32:45 +00002196 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
2197 if (I != PromotedNodes.end()) return I->second;
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002198
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002199 // Promotion needs an optimization step to clean up after it, and is not
2200 // careful to avoid operations the target does not support. Make sure that
2201 // all generated operations are legalized in the next iteration.
2202 NeedsAnotherIteration = true;
2203
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002204 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00002205 case ISD::CopyFromReg:
2206 assert(0 && "CopyFromReg must be legal!");
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002207 default:
2208 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2209 assert(0 && "Do not know how to promote this operator!");
2210 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002211 case ISD::UNDEF:
2212 Result = DAG.getNode(ISD::UNDEF, NVT);
2213 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002214 case ISD::Constant:
Chris Lattner9a4ad482005-08-30 16:56:19 +00002215 if (VT != MVT::i1)
2216 Result = DAG.getNode(ISD::SIGN_EXTEND, NVT, Op);
2217 else
2218 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002219 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
2220 break;
2221 case ISD::ConstantFP:
2222 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
2223 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
2224 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00002225
Chris Lattner2cb338d2005-01-18 02:59:52 +00002226 case ISD::SETCC:
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002227 assert(isTypeLegal(TLI.getSetCCResultTy()) && "SetCC type is not legal??");
Chris Lattnerd47675e2005-08-09 20:20:18 +00002228 Result = DAG.getNode(ISD::SETCC, TLI.getSetCCResultTy(),Node->getOperand(0),
2229 Node->getOperand(1), Node->getOperand(2));
Chris Lattner2cb338d2005-01-18 02:59:52 +00002230 Result = LegalizeOp(Result);
2231 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002232
2233 case ISD::TRUNCATE:
2234 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2235 case Legal:
2236 Result = LegalizeOp(Node->getOperand(0));
2237 assert(Result.getValueType() >= NVT &&
2238 "This truncation doesn't make sense!");
2239 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
2240 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
2241 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00002242 case Promote:
2243 // The truncation is not required, because we don't guarantee anything
2244 // about high bits anyway.
2245 Result = PromoteOp(Node->getOperand(0));
2246 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002247 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00002248 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
2249 // Truncate the low part of the expanded value to the result type
Chris Lattner4398daf2005-08-01 18:16:37 +00002250 Result = DAG.getNode(ISD::TRUNCATE, NVT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002251 }
2252 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002253 case ISD::SIGN_EXTEND:
2254 case ISD::ZERO_EXTEND:
Chris Lattner7753f172005-09-02 00:18:10 +00002255 case ISD::ANY_EXTEND:
Chris Lattner4d978642005-01-15 22:16:26 +00002256 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2257 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
2258 case Legal:
2259 // Input is legal? Just do extend all the way to the larger type.
2260 Result = LegalizeOp(Node->getOperand(0));
2261 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
2262 break;
2263 case Promote:
2264 // Promote the reg if it's smaller.
2265 Result = PromoteOp(Node->getOperand(0));
2266 // The high bits are not guaranteed to be anything. Insert an extend.
2267 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00002268 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002269 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner7753f172005-09-02 00:18:10 +00002270 else if (Node->getOpcode() == ISD::ZERO_EXTEND)
Chris Lattner0e852af2005-04-13 02:38:47 +00002271 Result = DAG.getZeroExtendInReg(Result,
2272 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00002273 break;
2274 }
2275 break;
2276
2277 case ISD::FP_EXTEND:
2278 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
2279 case ISD::FP_ROUND:
2280 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2281 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
2282 case Promote: assert(0 && "Unreachable with 2 FP types!");
2283 case Legal:
2284 // Input is legal? Do an FP_ROUND_INREG.
2285 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002286 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2287 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002288 break;
2289 }
2290 break;
2291
2292 case ISD::SINT_TO_FP:
2293 case ISD::UINT_TO_FP:
2294 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2295 case Legal:
2296 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002297 // No extra round required here.
2298 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002299 break;
2300
2301 case Promote:
2302 Result = PromoteOp(Node->getOperand(0));
2303 if (Node->getOpcode() == ISD::SINT_TO_FP)
2304 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00002305 Result,
2306 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00002307 else
Chris Lattner0e852af2005-04-13 02:38:47 +00002308 Result = DAG.getZeroExtendInReg(Result,
2309 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00002310 // No extra round required here.
2311 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00002312 break;
2313 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00002314 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
2315 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00002316 // Round if we cannot tolerate excess precision.
2317 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002318 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2319 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00002320 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002321 }
Chris Lattner4d978642005-01-15 22:16:26 +00002322 break;
2323
2324 case ISD::FP_TO_SINT:
2325 case ISD::FP_TO_UINT:
2326 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2327 case Legal:
2328 Tmp1 = LegalizeOp(Node->getOperand(0));
2329 break;
2330 case Promote:
2331 // The input result is prerounded, so we don't have to do anything
2332 // special.
2333 Tmp1 = PromoteOp(Node->getOperand(0));
2334 break;
2335 case Expand:
2336 assert(0 && "not implemented");
2337 }
Nate Begeman36853ee2005-08-14 01:20:53 +00002338 // If we're promoting a UINT to a larger size, check to see if the new node
2339 // will be legal. If it isn't, check to see if FP_TO_SINT is legal, since
2340 // we can use that instead. This allows us to generate better code for
2341 // FP_TO_UINT for small destination sizes on targets where FP_TO_UINT is not
2342 // legal, such as PowerPC.
2343 if (Node->getOpcode() == ISD::FP_TO_UINT &&
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002344 !TLI.isOperationLegal(ISD::FP_TO_UINT, NVT) &&
Nate Begemand8f2a1a2005-10-25 23:47:25 +00002345 (TLI.isOperationLegal(ISD::FP_TO_SINT, NVT) ||
2346 TLI.getOperationAction(ISD::FP_TO_SINT, NVT)==TargetLowering::Custom)){
Nate Begeman36853ee2005-08-14 01:20:53 +00002347 Result = DAG.getNode(ISD::FP_TO_SINT, NVT, Tmp1);
2348 } else {
2349 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2350 }
Chris Lattner4d978642005-01-15 22:16:26 +00002351 break;
2352
Chris Lattner13fe99c2005-04-02 05:00:07 +00002353 case ISD::FABS:
2354 case ISD::FNEG:
2355 Tmp1 = PromoteOp(Node->getOperand(0));
2356 assert(Tmp1.getValueType() == NVT);
2357 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2358 // NOTE: we do not have to do any extra rounding here for
2359 // NoExcessFPPrecision, because we know the input will have the appropriate
2360 // precision, and these operations don't modify precision at all.
2361 break;
2362
Chris Lattner9d6fa982005-04-28 21:44:33 +00002363 case ISD::FSQRT:
2364 case ISD::FSIN:
2365 case ISD::FCOS:
2366 Tmp1 = PromoteOp(Node->getOperand(0));
2367 assert(Tmp1.getValueType() == NVT);
2368 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2369 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002370 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2371 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00002372 break;
2373
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002374 case ISD::AND:
2375 case ISD::OR:
2376 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002377 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00002378 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002379 case ISD::MUL:
2380 // The input may have strange things in the top bits of the registers, but
Chris Lattner6f3b5772005-09-28 22:28:18 +00002381 // these operations don't care. They may have weird bits going out, but
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002382 // that too is okay if they are integer operations.
2383 Tmp1 = PromoteOp(Node->getOperand(0));
2384 Tmp2 = PromoteOp(Node->getOperand(1));
2385 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2386 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
Chris Lattner6f3b5772005-09-28 22:28:18 +00002387 break;
2388 case ISD::FADD:
2389 case ISD::FSUB:
2390 case ISD::FMUL:
2391 // The input may have strange things in the top bits of the registers, but
2392 // these operations don't care.
2393 Tmp1 = PromoteOp(Node->getOperand(0));
2394 Tmp2 = PromoteOp(Node->getOperand(1));
2395 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
2396 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2397
2398 // Floating point operations will give excess precision that we may not be
2399 // able to tolerate. If we DO allow excess precision, just leave it,
2400 // otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00002401 // FIXME: Why would we need to round FP ops more than integer ones?
2402 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattner6f3b5772005-09-28 22:28:18 +00002403 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002404 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2405 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00002406 break;
2407
Chris Lattner4d978642005-01-15 22:16:26 +00002408 case ISD::SDIV:
2409 case ISD::SREM:
2410 // These operators require that their input be sign extended.
2411 Tmp1 = PromoteOp(Node->getOperand(0));
2412 Tmp2 = PromoteOp(Node->getOperand(1));
2413 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00002414 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2415 DAG.getValueType(VT));
2416 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
2417 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002418 }
2419 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2420
2421 // Perform FP_ROUND: this is probably overly pessimistic.
2422 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00002423 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2424 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002425 break;
Chris Lattner6f3b5772005-09-28 22:28:18 +00002426 case ISD::FDIV:
2427 case ISD::FREM:
2428 // These operators require that their input be fp extended.
2429 Tmp1 = PromoteOp(Node->getOperand(0));
2430 Tmp2 = PromoteOp(Node->getOperand(1));
2431 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2432
2433 // Perform FP_ROUND: this is probably overly pessimistic.
2434 if (NoExcessFPPrecision)
2435 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
2436 DAG.getValueType(VT));
2437 break;
Chris Lattner4d978642005-01-15 22:16:26 +00002438
2439 case ISD::UDIV:
2440 case ISD::UREM:
2441 // These operators require that their input be zero extended.
2442 Tmp1 = PromoteOp(Node->getOperand(0));
2443 Tmp2 = PromoteOp(Node->getOperand(1));
2444 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00002445 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
2446 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002447 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
2448 break;
2449
2450 case ISD::SHL:
2451 Tmp1 = PromoteOp(Node->getOperand(0));
2452 Tmp2 = LegalizeOp(Node->getOperand(1));
2453 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
2454 break;
2455 case ISD::SRA:
2456 // The input value must be properly sign extended.
2457 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00002458 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
2459 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00002460 Tmp2 = LegalizeOp(Node->getOperand(1));
2461 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
2462 break;
2463 case ISD::SRL:
2464 // The input value must be properly zero extended.
2465 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00002466 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00002467 Tmp2 = LegalizeOp(Node->getOperand(1));
2468 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
2469 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002470 case ISD::LOAD:
2471 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2472 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Nate Begeman02b23c62005-10-13 03:11:28 +00002473 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
2474 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002475 // Remember that we legalized the chain.
2476 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2477 break;
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002478 case ISD::SEXTLOAD:
2479 case ISD::ZEXTLOAD:
2480 case ISD::EXTLOAD:
2481 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2482 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerb986f472005-10-15 20:24:07 +00002483 Result = DAG.getExtLoad(Node->getOpcode(), NVT, Tmp1, Tmp2,
2484 Node->getOperand(2),
2485 cast<VTSDNode>(Node->getOperand(3))->getVT());
Chris Lattnerd23f4b72005-10-13 20:07:41 +00002486 // Remember that we legalized the chain.
2487 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2488 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002489 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002490 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2491 case Expand: assert(0 && "It's impossible to expand bools");
2492 case Legal:
2493 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
2494 break;
2495 case Promote:
2496 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
2497 break;
2498 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002499 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
2500 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
2501 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
2502 break;
Nate Begemane5b86d72005-08-10 20:51:12 +00002503 case ISD::SELECT_CC:
2504 Tmp2 = PromoteOp(Node->getOperand(2)); // True
2505 Tmp3 = PromoteOp(Node->getOperand(3)); // False
2506 Result = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
2507 Node->getOperand(1), Tmp2, Tmp3,
2508 Node->getOperand(4));
2509 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00002510 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002511 case ISD::CALL: {
2512 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2513 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2514
Chris Lattner3d95c142005-01-19 20:24:35 +00002515 std::vector<SDOperand> Ops;
2516 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
2517 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2518
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002519 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2520 "Can only promote single result calls");
2521 std::vector<MVT::ValueType> RetTyVTs;
2522 RetTyVTs.reserve(2);
2523 RetTyVTs.push_back(NVT);
2524 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002525 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
2526 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00002527 Result = SDOperand(NC, 0);
2528
2529 // Insert the new chain mapping.
2530 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
2531 break;
Misha Brukman835702a2005-04-21 22:36:52 +00002532 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002533 case ISD::CTPOP:
2534 case ISD::CTTZ:
2535 case ISD::CTLZ:
2536 Tmp1 = Node->getOperand(0);
2537 //Zero extend the argument
2538 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
2539 // Perform the larger operation, then subtract if needed.
2540 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
2541 switch(Node->getOpcode())
2542 {
2543 case ISD::CTPOP:
2544 Result = Tmp1;
2545 break;
2546 case ISD::CTTZ:
2547 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Nate Begeman36853ee2005-08-14 01:20:53 +00002548 Tmp2 = DAG.getSetCC(TLI.getSetCCResultTy(), Tmp1,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002549 DAG.getConstant(getSizeInBits(NVT), NVT), ISD::SETEQ);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002550 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002551 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
2552 break;
2553 case ISD::CTLZ:
2554 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002555 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
2556 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00002557 getSizeInBits(VT), NVT));
2558 break;
2559 }
2560 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00002561 }
2562
2563 assert(Result.Val && "Didn't set a result!");
2564 AddPromotedOperand(Op, Result);
2565 return Result;
2566}
Chris Lattnerdc750592005-01-07 07:47:09 +00002567
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002568/// ExpandAddSub - Find a clever way to expand this add operation into
2569/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00002570void SelectionDAGLegalize::
2571ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
2572 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002573 // Expand the subcomponents.
2574 SDOperand LHSL, LHSH, RHSL, RHSH;
2575 ExpandOp(LHS, LHSL, LHSH);
2576 ExpandOp(RHS, RHSL, RHSH);
2577
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002578 std::vector<SDOperand> Ops;
2579 Ops.push_back(LHSL);
2580 Ops.push_back(LHSH);
2581 Ops.push_back(RHSL);
2582 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002583 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2584 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002585 Hi = Lo.getValue(1);
2586}
2587
Chris Lattner4157c412005-04-02 04:00:59 +00002588void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2589 SDOperand Op, SDOperand Amt,
2590 SDOperand &Lo, SDOperand &Hi) {
2591 // Expand the subcomponents.
2592 SDOperand LHSL, LHSH;
2593 ExpandOp(Op, LHSL, LHSH);
2594
2595 std::vector<SDOperand> Ops;
2596 Ops.push_back(LHSL);
2597 Ops.push_back(LHSH);
2598 Ops.push_back(Amt);
Chris Lattner61d21b12005-08-30 17:21:17 +00002599 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
Chris Lattner669e8c22005-05-14 07:25:05 +00002600 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002601 Hi = Lo.getValue(1);
2602}
2603
2604
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002605/// ExpandShift - Try to find a clever way to expand this shift operation out to
2606/// smaller elements. If we can't find a way that is more efficient than a
2607/// libcall on this target, return false. Otherwise, return true with the
2608/// low-parts expanded into Lo and Hi.
2609bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2610 SDOperand &Lo, SDOperand &Hi) {
2611 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2612 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002613
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002614 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002615 SDOperand ShAmt = LegalizeOp(Amt);
2616 MVT::ValueType ShTy = ShAmt.getValueType();
2617 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2618 unsigned NVTBits = MVT::getSizeInBits(NVT);
2619
2620 // Handle the case when Amt is an immediate. Other cases are currently broken
2621 // and are disabled.
2622 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2623 unsigned Cst = CN->getValue();
2624 // Expand the incoming operand to be shifted, so that we have its parts
2625 SDOperand InL, InH;
2626 ExpandOp(Op, InL, InH);
2627 switch(Opc) {
2628 case ISD::SHL:
2629 if (Cst > VTBits) {
2630 Lo = DAG.getConstant(0, NVT);
2631 Hi = DAG.getConstant(0, NVT);
2632 } else if (Cst > NVTBits) {
2633 Lo = DAG.getConstant(0, NVT);
2634 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002635 } else if (Cst == NVTBits) {
2636 Lo = DAG.getConstant(0, NVT);
2637 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002638 } else {
2639 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2640 Hi = DAG.getNode(ISD::OR, NVT,
2641 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2642 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2643 }
2644 return true;
2645 case ISD::SRL:
2646 if (Cst > VTBits) {
2647 Lo = DAG.getConstant(0, NVT);
2648 Hi = DAG.getConstant(0, NVT);
2649 } else if (Cst > NVTBits) {
2650 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2651 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002652 } else if (Cst == NVTBits) {
2653 Lo = InH;
2654 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002655 } else {
2656 Lo = DAG.getNode(ISD::OR, NVT,
2657 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2658 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2659 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2660 }
2661 return true;
2662 case ISD::SRA:
2663 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002664 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002665 DAG.getConstant(NVTBits-1, ShTy));
2666 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002667 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002668 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002669 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002670 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002671 } else if (Cst == NVTBits) {
2672 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002673 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002674 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002675 } else {
2676 Lo = DAG.getNode(ISD::OR, NVT,
2677 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2678 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2679 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2680 }
2681 return true;
2682 }
2683 }
2684 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2685 // so disable it for now. Currently targets are handling this via SHL_PARTS
2686 // and friends.
2687 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002688
2689 // If we have an efficient select operation (or if the selects will all fold
2690 // away), lower to some complex code, otherwise just emit the libcall.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002691 if (!TLI.isOperationLegal(ISD::SELECT, NVT) && !isa<ConstantSDNode>(Amt))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002692 return false;
2693
2694 SDOperand InL, InH;
2695 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002696 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2697 DAG.getConstant(NVTBits, ShTy), ShAmt);
2698
Chris Lattner4d25c042005-01-20 20:29:23 +00002699 // Compare the unmasked shift amount against 32.
Chris Lattnerd47675e2005-08-09 20:20:18 +00002700 SDOperand Cond = DAG.getSetCC(TLI.getSetCCResultTy(), ShAmt,
2701 DAG.getConstant(NVTBits, ShTy), ISD::SETGE);
Chris Lattner4d25c042005-01-20 20:29:23 +00002702
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002703 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2704 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2705 DAG.getConstant(NVTBits-1, ShTy));
2706 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2707 DAG.getConstant(NVTBits-1, ShTy));
2708 }
2709
2710 if (Opc == ISD::SHL) {
2711 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2712 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2713 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002714 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002715
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002716 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2717 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2718 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002719 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
Chris Lattnerd47675e2005-08-09 20:20:18 +00002720 DAG.getSetCC(TLI.getSetCCResultTy(), NAmt,
2721 DAG.getConstant(32, ShTy),
2722 ISD::SETEQ),
Chris Lattneraac464e2005-01-21 06:05:23 +00002723 DAG.getConstant(0, NVT),
2724 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002725 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002726 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002727 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002728 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002729
2730 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002731 if (Opc == ISD::SRA)
2732 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2733 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002734 else
2735 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002736 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002737 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002738 }
2739 return true;
2740}
Chris Lattneraac464e2005-01-21 06:05:23 +00002741
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002742/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2743/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002744/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002745static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002746 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
Chris Lattnercabdc342005-08-05 16:23:57 +00002747
Chris Lattner2dce7032005-05-12 23:24:06 +00002748 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002749 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002750 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002751 Found = Node;
2752 return;
2753 }
2754
2755 // Otherwise, scan the operands of Node to see if any of them is a call.
2756 assert(Node->getNumOperands() != 0 &&
2757 "All leaves should have depth equal to the entry node!");
Nate Begemanf8221c52005-10-05 21:44:10 +00002758 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002759 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002760
2761 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002762 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002763 Found);
2764}
2765
2766
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002767/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2768/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002769/// than Found.
Chris Lattner96ad3132005-08-05 18:10:27 +00002770static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found,
2771 std::set<SDNode*> &Visited) {
2772 if ((Found && Node->getNodeDepth() >= Found->getNodeDepth()) ||
2773 !Visited.insert(Node).second) return;
Chris Lattner4add7e32005-01-23 04:42:50 +00002774
Chris Lattner2dce7032005-05-12 23:24:06 +00002775 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002776 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002777 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002778 Found = Node;
2779 return;
2780 }
2781
2782 // Otherwise, scan the operands of Node to see if any of them is a call.
2783 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2784 if (UI == E) return;
2785 for (--E; UI != E; ++UI)
Chris Lattner96ad3132005-08-05 18:10:27 +00002786 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002787
2788 // Tail recurse for the last iteration.
Chris Lattner96ad3132005-08-05 18:10:27 +00002789 FindEarliestCallSeqEnd(*UI, Found, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002790}
2791
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002792/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002793/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002794static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002795 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002796 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002797 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002798 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002799
Chris Lattner4add7e32005-01-23 04:42:50 +00002800 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002801 if (TheChain.getValueType() != MVT::Other)
2802 TheChain = SDOperand(Node, 0);
Nate Begeman5da69082005-10-04 02:10:55 +00002803 if (TheChain.getValueType() != MVT::Other)
2804 return 0;
Misha Brukman835702a2005-04-21 22:36:52 +00002805
2806 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattnercabdc342005-08-05 16:23:57 +00002807 E = Node->use_end(); UI != E; ++UI) {
Misha Brukman835702a2005-04-21 22:36:52 +00002808
Chris Lattner4add7e32005-01-23 04:42:50 +00002809 // Make sure to only follow users of our token chain.
2810 SDNode *User = *UI;
2811 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2812 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002813 if (SDNode *Result = FindCallSeqEnd(User))
2814 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002815 }
Chris Lattnercabdc342005-08-05 16:23:57 +00002816 return 0;
Chris Lattner4add7e32005-01-23 04:42:50 +00002817}
2818
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002819/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002820/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002821static SDNode *FindCallSeqStart(SDNode *Node) {
2822 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002823 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002824
2825 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2826 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002827 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002828}
2829
2830
Chris Lattner4add7e32005-01-23 04:42:50 +00002831/// FindInputOutputChains - If we are replacing an operation with a call we need
2832/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002833/// properly serialize the calls in the block. The returned operand is the
2834/// input chain value for the new call (e.g. the entry node or the previous
2835/// call), and OutChain is set to be the chain node to update to point to the
2836/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002837static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2838 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002839 SDNode *LatestCallSeqStart = Entry.Val;
2840 SDNode *LatestCallSeqEnd = 0;
2841 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2842 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002843
Chris Lattner2dce7032005-05-12 23:24:06 +00002844 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002845 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002846 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2847 // unless LatestCallStackDown is an CALLSEQ_START.
Nate Begeman5da69082005-10-04 02:10:55 +00002848 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002849 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begeman5da69082005-10-04 02:10:55 +00002850 //std::cerr<<"Found end node: "; LatestCallSeqEnd->dump(); std::cerr <<"\n";
2851 } else {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002852 LatestCallSeqEnd = Entry.Val;
Nate Begeman5da69082005-10-04 02:10:55 +00002853 }
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002854 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002855
Chris Lattner06bbeb62005-05-11 19:02:11 +00002856 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002857 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002858 OutChain = 0;
Chris Lattner96ad3132005-08-05 18:10:27 +00002859 std::set<SDNode*> Visited;
2860 FindEarliestCallSeqEnd(OpNode, OutChain, Visited);
Chris Lattner4add7e32005-01-23 04:42:50 +00002861
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002862 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002863 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002864 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002865
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002866 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002867}
2868
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002869/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002870void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2871 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002872 // Nothing to splice it into?
2873 if (OutChain == 0) return;
2874
2875 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2876 //OutChain->dump();
2877
2878 // Form a token factor node merging the old inval and the new inval.
2879 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2880 OutChain->getOperand(0));
2881 // Change the node to refer to the new token.
2882 OutChain->setAdjCallChain(InToken);
2883}
Chris Lattner4add7e32005-01-23 04:42:50 +00002884
2885
Chris Lattneraac464e2005-01-21 06:05:23 +00002886// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2887// does not fit into a register, return the lo part and set the hi part to the
2888// by-reg argument. If it does fit into a single register, return the result
2889// and leave the Hi part unset.
2890SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2891 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002892 SDNode *OutChain;
2893 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2894 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002895 if (InChain.Val == 0)
2896 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002897
Chris Lattneraac464e2005-01-21 06:05:23 +00002898 TargetLowering::ArgListTy Args;
2899 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2900 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2901 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2902 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2903 }
2904 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002905
Chris Lattner06bbeb62005-05-11 19:02:11 +00002906 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002907 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002908 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002909 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2910 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002911
Chris Lattner63022662005-09-02 20:26:58 +00002912 SDOperand Result;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002913 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002914 default: assert(0 && "Unknown thing");
2915 case Legal:
Chris Lattner63022662005-09-02 20:26:58 +00002916 Result = CallInfo.first;
2917 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002918 case Promote:
2919 assert(0 && "Cannot promote this yet!");
2920 case Expand:
Chris Lattner63022662005-09-02 20:26:58 +00002921 ExpandOp(CallInfo.first, Result, Hi);
2922 CallInfo.second = LegalizeOp(CallInfo.second);
2923 break;
Chris Lattneraac464e2005-01-21 06:05:23 +00002924 }
Chris Lattner63022662005-09-02 20:26:58 +00002925
2926 SpliceCallInto(CallInfo.second, OutChain);
2927 NeedsAnotherIteration = true;
2928 return Result;
Chris Lattneraac464e2005-01-21 06:05:23 +00002929}
2930
Chris Lattner4add7e32005-01-23 04:42:50 +00002931
Chris Lattneraac464e2005-01-21 06:05:23 +00002932/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2933/// destination type is legal.
2934SDOperand SelectionDAGLegalize::
2935ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00002936 assert(isTypeLegal(DestTy) && "Destination type is not legal!");
Chris Lattneraac464e2005-01-21 06:05:23 +00002937 assert(getTypeAction(Source.getValueType()) == Expand &&
2938 "This is not an expansion!");
2939 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2940
Chris Lattner06bbeb62005-05-11 19:02:11 +00002941 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002942 assert(Source.getValueType() == MVT::i64 &&
2943 "This only works for 64-bit -> FP");
2944 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2945 // incoming integer is set. To handle this, we dynamically test to see if
2946 // it is set, and, if so, add a fudge factor.
2947 SDOperand Lo, Hi;
2948 ExpandOp(Source, Lo, Hi);
2949
Chris Lattner2a4f7312005-05-13 04:45:13 +00002950 // If this is unsigned, and not supported, first perform the conversion to
2951 // signed, then adjust the result if the sign bit is set.
2952 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2953 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2954
Chris Lattnerd47675e2005-08-09 20:20:18 +00002955 SDOperand SignSet = DAG.getSetCC(TLI.getSetCCResultTy(), Hi,
2956 DAG.getConstant(0, Hi.getValueType()),
2957 ISD::SETLT);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002958 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2959 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2960 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002961 uint64_t FF = 0x5f800000ULL;
2962 if (TLI.isLittleEndian()) FF <<= 32;
2963 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002964
Chris Lattnerc30405e2005-08-26 17:15:30 +00002965 SDOperand CPIdx = DAG.getConstantPool(FudgeFactor, TLI.getPointerTy());
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002966 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2967 SDOperand FudgeInReg;
2968 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002969 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2970 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002971 else {
2972 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002973 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2974 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002975 }
Chris Lattner5b2be1f2005-09-29 06:44:39 +00002976 return DAG.getNode(ISD::FADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002977 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002978
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002979 // Check to see if the target has a custom way to lower this. If so, use it.
2980 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2981 default: assert(0 && "This action not implemented for this operation!");
2982 case TargetLowering::Legal:
2983 case TargetLowering::Expand:
2984 break; // This case is handled below.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00002985 case TargetLowering::Custom: {
2986 SDOperand NV = TLI.LowerOperation(DAG.getNode(ISD::SINT_TO_FP, DestTy,
2987 Source), DAG);
2988 if (NV.Val)
2989 return LegalizeOp(NV);
2990 break; // The target decided this was legal after all
2991 }
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002992 }
2993
Chris Lattner153587e2005-05-12 07:00:44 +00002994 // Expand the source, then glue it back together for the call. We must expand
2995 // the source in case it is shared (this pass of legalize must traverse it).
2996 SDOperand SrcLo, SrcHi;
2997 ExpandOp(Source, SrcLo, SrcHi);
2998 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2999
Chris Lattner06bbeb62005-05-11 19:02:11 +00003000 SDNode *OutChain = 0;
3001 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
3002 DAG.getEntryNode());
3003 const char *FnName = 0;
3004 if (DestTy == MVT::f32)
3005 FnName = "__floatdisf";
3006 else {
3007 assert(DestTy == MVT::f64 && "Unknown fp value type!");
3008 FnName = "__floatdidf";
3009 }
3010
Chris Lattneraac464e2005-01-21 06:05:23 +00003011 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
3012
3013 TargetLowering::ArgListTy Args;
3014 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00003015
Chris Lattneraac464e2005-01-21 06:05:23 +00003016 Args.push_back(std::make_pair(Source, ArgTy));
3017
3018 // We don't care about token chains for libcalls. We just use the entry
3019 // node as our input and ignore the output chain. This allows us to place
3020 // calls wherever we need them to satisfy data dependences.
3021 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003022
3023 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00003024 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
3025 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003026
Chris Lattnera5bf1032005-05-12 04:49:08 +00003027 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00003028 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00003029}
Misha Brukman835702a2005-04-21 22:36:52 +00003030
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003031
3032
Chris Lattnerdc750592005-01-07 07:47:09 +00003033/// ExpandOp - Expand the specified SDOperand into its two component pieces
3034/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
3035/// LegalizeNodes map is filled in for any results that are not expanded, the
3036/// ExpandedNodes map is filled in for any results that are expanded, and the
3037/// Lo/Hi values are returned.
3038void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
3039 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00003040 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00003041 SDNode *Node = Op.Val;
3042 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
3043 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
3044 assert(MVT::isInteger(NVT) && NVT < VT &&
3045 "Cannot expand to FP value or to larger int value!");
3046
Chris Lattner1a570f12005-09-02 20:32:45 +00003047 // See if we already expanded it.
3048 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
3049 = ExpandedNodes.find(Op);
3050 if (I != ExpandedNodes.end()) {
3051 Lo = I->second.first;
3052 Hi = I->second.second;
3053 return;
Chris Lattnerdc750592005-01-07 07:47:09 +00003054 }
3055
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003056 // Expanding to multiple registers needs to perform an optimization step, and
3057 // is not careful to avoid operations the target does not support. Make sure
3058 // that all generated operations are legalized in the next iteration.
3059 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00003060
Chris Lattnerdc750592005-01-07 07:47:09 +00003061 switch (Node->getOpcode()) {
Chris Lattner33182322005-08-16 21:55:35 +00003062 case ISD::CopyFromReg:
3063 assert(0 && "CopyFromReg must be legal!");
3064 default:
Chris Lattnerdc750592005-01-07 07:47:09 +00003065 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
3066 assert(0 && "Do not know how to expand this operator!");
3067 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00003068 case ISD::UNDEF:
3069 Lo = DAG.getNode(ISD::UNDEF, NVT);
3070 Hi = DAG.getNode(ISD::UNDEF, NVT);
3071 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003072 case ISD::Constant: {
3073 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
3074 Lo = DAG.getConstant(Cst, NVT);
3075 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
3076 break;
3077 }
3078
Chris Lattner32e08b72005-03-28 22:03:13 +00003079 case ISD::BUILD_PAIR:
3080 // Legalize both operands. FIXME: in the future we should handle the case
3081 // where the two elements are not legal.
3082 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
3083 Lo = LegalizeOp(Node->getOperand(0));
3084 Hi = LegalizeOp(Node->getOperand(1));
3085 break;
3086
Chris Lattner55e9cde2005-05-11 04:51:16 +00003087 case ISD::CTPOP:
3088 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00003089 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
3090 DAG.getNode(ISD::CTPOP, NVT, Lo),
3091 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00003092 Hi = DAG.getConstant(0, NVT);
3093 break;
3094
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003095 case ISD::CTLZ: {
3096 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003097 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003098 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3099 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003100 SDOperand TopNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), HLZ, BitsC,
3101 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003102 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
3103 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
3104
3105 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
3106 Hi = DAG.getConstant(0, NVT);
3107 break;
3108 }
3109
3110 case ISD::CTTZ: {
3111 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00003112 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003113 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
3114 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
Chris Lattnerd47675e2005-08-09 20:20:18 +00003115 SDOperand BotNotZero = DAG.getSetCC(TLI.getSetCCResultTy(), LTZ, BitsC,
3116 ISD::SETNE);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00003117 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
3118 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
3119
3120 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
3121 Hi = DAG.getConstant(0, NVT);
3122 break;
3123 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00003124
Chris Lattnerdc750592005-01-07 07:47:09 +00003125 case ISD::LOAD: {
3126 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3127 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003128 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003129
3130 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00003131 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00003132 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
3133 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00003134 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00003135 //are from the same instruction?
3136 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00003137
3138 // Build a factor node to remember that this load is independent of the
3139 // other one.
3140 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
3141 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00003142
Chris Lattnerdc750592005-01-07 07:47:09 +00003143 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00003144 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00003145 if (!TLI.isLittleEndian())
3146 std::swap(Lo, Hi);
3147 break;
3148 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00003149 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00003150 case ISD::CALL: {
3151 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
3152 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
3153
Chris Lattner3d95c142005-01-19 20:24:35 +00003154 bool Changed = false;
3155 std::vector<SDOperand> Ops;
3156 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
3157 Ops.push_back(LegalizeOp(Node->getOperand(i)));
3158 Changed |= Ops.back() != Node->getOperand(i);
3159 }
3160
Chris Lattnerdc750592005-01-07 07:47:09 +00003161 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
3162 "Can only expand a call once so far, not i64 -> i16!");
3163
3164 std::vector<MVT::ValueType> RetTyVTs;
3165 RetTyVTs.reserve(3);
3166 RetTyVTs.push_back(NVT);
3167 RetTyVTs.push_back(NVT);
3168 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00003169 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
3170 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00003171 Lo = SDOperand(NC, 0);
3172 Hi = SDOperand(NC, 1);
3173
3174 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00003175 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00003176 break;
3177 }
3178 case ISD::AND:
3179 case ISD::OR:
3180 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
3181 SDOperand LL, LH, RL, RH;
3182 ExpandOp(Node->getOperand(0), LL, LH);
3183 ExpandOp(Node->getOperand(1), RL, RH);
3184 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
3185 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
3186 break;
3187 }
3188 case ISD::SELECT: {
3189 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00003190
3191 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3192 case Expand: assert(0 && "It's impossible to expand bools");
3193 case Legal:
3194 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
3195 break;
3196 case Promote:
3197 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
3198 break;
3199 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003200 ExpandOp(Node->getOperand(1), LL, LH);
3201 ExpandOp(Node->getOperand(2), RL, RH);
3202 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
3203 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
3204 break;
3205 }
Nate Begemane5b86d72005-08-10 20:51:12 +00003206 case ISD::SELECT_CC: {
3207 SDOperand TL, TH, FL, FH;
3208 ExpandOp(Node->getOperand(2), TL, TH);
3209 ExpandOp(Node->getOperand(3), FL, FH);
3210 Lo = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3211 Node->getOperand(1), TL, FL, Node->getOperand(4));
3212 Hi = DAG.getNode(ISD::SELECT_CC, NVT, Node->getOperand(0),
3213 Node->getOperand(1), TH, FH, Node->getOperand(4));
Nate Begeman180b0882005-08-11 01:12:20 +00003214 Lo = LegalizeOp(Lo);
3215 Hi = LegalizeOp(Hi);
Nate Begemane5b86d72005-08-10 20:51:12 +00003216 break;
3217 }
Nate Begemanc3a89c52005-10-13 17:15:37 +00003218 case ISD::SEXTLOAD: {
3219 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3220 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3221 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3222
3223 if (EVT == NVT)
3224 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3225 else
3226 Lo = DAG.getExtLoad(ISD::SEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3227 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003228
3229 // Remember that we legalized the chain.
3230 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3231
Nate Begemanc3a89c52005-10-13 17:15:37 +00003232 // The high part is obtained by SRA'ing all but one of the bits of the lo
3233 // part.
3234 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
3235 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3236 TLI.getShiftAmountTy()));
3237 Lo = LegalizeOp(Lo);
3238 Hi = LegalizeOp(Hi);
3239 break;
3240 }
3241 case ISD::ZEXTLOAD: {
3242 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3243 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3244 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3245
3246 if (EVT == NVT)
3247 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3248 else
3249 Lo = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3250 EVT);
Chris Lattner258521d2005-10-13 21:44:47 +00003251
3252 // Remember that we legalized the chain.
3253 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3254
Nate Begemanc3a89c52005-10-13 17:15:37 +00003255 // The high part is just a zero.
Chris Lattner258521d2005-10-13 21:44:47 +00003256 Hi = LegalizeOp(DAG.getConstant(0, NVT));
Nate Begemanc3a89c52005-10-13 17:15:37 +00003257 Lo = LegalizeOp(Lo);
Chris Lattner258521d2005-10-13 21:44:47 +00003258 break;
3259 }
3260 case ISD::EXTLOAD: {
3261 SDOperand Chain = LegalizeOp(Node->getOperand(0));
3262 SDOperand Ptr = LegalizeOp(Node->getOperand(1));
3263 MVT::ValueType EVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
3264
3265 if (EVT == NVT)
3266 Lo = DAG.getLoad(NVT, Chain, Ptr, Node->getOperand(2));
3267 else
3268 Lo = DAG.getExtLoad(ISD::EXTLOAD, NVT, Chain, Ptr, Node->getOperand(2),
3269 EVT);
3270
3271 // Remember that we legalized the chain.
3272 AddLegalizedOperand(SDOperand(Node, 1), Lo.getValue(1));
3273
3274 // The high part is undefined.
3275 Hi = LegalizeOp(DAG.getNode(ISD::UNDEF, NVT));
3276 Lo = LegalizeOp(Lo);
Nate Begemanc3a89c52005-10-13 17:15:37 +00003277 break;
3278 }
Chris Lattner7753f172005-09-02 00:18:10 +00003279 case ISD::ANY_EXTEND: {
3280 SDOperand In;
3281 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3282 case Expand: assert(0 && "expand-expand not implemented yet!");
3283 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3284 case Promote:
3285 In = PromoteOp(Node->getOperand(0));
3286 break;
3287 }
3288
3289 // The low part is any extension of the input (which degenerates to a copy).
3290 Lo = DAG.getNode(ISD::ANY_EXTEND, NVT, In);
3291 // The high part is undefined.
3292 Hi = DAG.getNode(ISD::UNDEF, NVT);
3293 break;
3294 }
Chris Lattnerdc750592005-01-07 07:47:09 +00003295 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00003296 SDOperand In;
3297 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3298 case Expand: assert(0 && "expand-expand not implemented yet!");
3299 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3300 case Promote:
3301 In = PromoteOp(Node->getOperand(0));
3302 // Emit the appropriate sign_extend_inreg to get the value we want.
3303 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00003304 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00003305 break;
3306 }
3307
Chris Lattnerdc750592005-01-07 07:47:09 +00003308 // The low part is just a sign extension of the input (which degenerates to
3309 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00003310 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003311
Chris Lattnerdc750592005-01-07 07:47:09 +00003312 // The high part is obtained by SRA'ing all but one of the bits of the lo
3313 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00003314 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00003315 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
3316 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00003317 break;
3318 }
Chris Lattner47844892005-04-03 23:41:52 +00003319 case ISD::ZERO_EXTEND: {
3320 SDOperand In;
3321 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3322 case Expand: assert(0 && "expand-expand not implemented yet!");
3323 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
3324 case Promote:
3325 In = PromoteOp(Node->getOperand(0));
3326 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00003327 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00003328 break;
3329 }
3330
Chris Lattnerdc750592005-01-07 07:47:09 +00003331 // The low part is just a zero extension of the input (which degenerates to
3332 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00003333 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00003334
Chris Lattnerdc750592005-01-07 07:47:09 +00003335 // The high part is just a zero.
3336 Hi = DAG.getConstant(0, NVT);
3337 break;
Chris Lattner47844892005-04-03 23:41:52 +00003338 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003339 // These operators cannot be expanded directly, emit them as calls to
3340 // library functions.
3341 case ISD::FP_TO_SINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003342 if (TLI.getOperationAction(ISD::FP_TO_SINT, VT) == TargetLowering::Custom) {
Chris Lattner941d84a2005-07-30 01:40:57 +00003343 SDOperand Op;
3344 switch (getTypeAction(Node->getOperand(0).getValueType())) {
3345 case Expand: assert(0 && "cannot expand FP!");
3346 case Legal: Op = LegalizeOp(Node->getOperand(0)); break;
3347 case Promote: Op = PromoteOp(Node->getOperand(0)); break;
3348 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003349
Chris Lattner941d84a2005-07-30 01:40:57 +00003350 Op = TLI.LowerOperation(DAG.getNode(ISD::FP_TO_SINT, VT, Op), DAG);
3351
Chris Lattnerfe68d752005-07-29 00:33:32 +00003352 // Now that the custom expander is done, expand the result, which is still
3353 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003354 if (Op.Val) {
3355 ExpandOp(Op, Lo, Hi);
3356 break;
3357 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003358 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003359
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003360 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003361 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003362 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003363 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003364 break;
Jeff Cohen546fd592005-07-30 18:33:25 +00003365
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003366 case ISD::FP_TO_UINT:
Chris Lattnerfe68d752005-07-29 00:33:32 +00003367 if (TLI.getOperationAction(ISD::FP_TO_UINT, VT) == TargetLowering::Custom) {
3368 SDOperand Op = DAG.getNode(ISD::FP_TO_UINT, VT,
3369 LegalizeOp(Node->getOperand(0)));
3370 // Now that the custom expander is done, expand the result, which is still
3371 // VT.
Chris Lattnerdff50ca2005-08-26 00:14:16 +00003372 Op = TLI.LowerOperation(Op, DAG);
3373 if (Op.Val) {
3374 ExpandOp(Op, Lo, Hi);
3375 break;
3376 }
Chris Lattnerfe68d752005-07-29 00:33:32 +00003377 }
Jeff Cohen546fd592005-07-30 18:33:25 +00003378
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003379 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00003380 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003381 else
Chris Lattneraac464e2005-01-21 06:05:23 +00003382 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00003383 break;
3384
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003385 case ISD::SHL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003386 // If the target wants custom lowering, do so.
3387 if (TLI.getOperationAction(ISD::SHL, VT) == TargetLowering::Custom) {
3388 SDOperand Op = DAG.getNode(ISD::SHL, VT, Node->getOperand(0),
3389 LegalizeOp(Node->getOperand(1)));
3390 Op = TLI.LowerOperation(Op, DAG);
3391 if (Op.Val) {
3392 // Now that the custom expander is done, expand the result, which is
3393 // still VT.
3394 ExpandOp(Op, Lo, Hi);
3395 break;
3396 }
3397 }
3398
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003399 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003400 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003401 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003402
3403 // If this target supports SHL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003404 if (TLI.isOperationLegal(ISD::SHL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003405 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
3406 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003407 break;
3408 }
3409
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003410 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003411 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003412 break;
3413
3414 case ISD::SRA:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003415 // If the target wants custom lowering, do so.
3416 if (TLI.getOperationAction(ISD::SRA, VT) == TargetLowering::Custom) {
3417 SDOperand Op = DAG.getNode(ISD::SRA, VT, Node->getOperand(0),
3418 LegalizeOp(Node->getOperand(1)));
3419 Op = TLI.LowerOperation(Op, DAG);
3420 if (Op.Val) {
3421 // Now that the custom expander is done, expand the result, which is
3422 // still VT.
3423 ExpandOp(Op, Lo, Hi);
3424 break;
3425 }
3426 }
3427
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003428 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003429 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003430 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003431
3432 // If this target supports SRA_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003433 if (TLI.isOperationLegal(ISD::SRA_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003434 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
3435 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003436 break;
3437 }
3438
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003439 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003440 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003441 break;
3442 case ISD::SRL:
Chris Lattner8a1a5f22005-08-31 19:01:53 +00003443 // If the target wants custom lowering, do so.
3444 if (TLI.getOperationAction(ISD::SRL, VT) == TargetLowering::Custom) {
3445 SDOperand Op = DAG.getNode(ISD::SRL, VT, Node->getOperand(0),
3446 LegalizeOp(Node->getOperand(1)));
3447 Op = TLI.LowerOperation(Op, DAG);
3448 if (Op.Val) {
3449 // Now that the custom expander is done, expand the result, which is
3450 // still VT.
3451 ExpandOp(Op, Lo, Hi);
3452 break;
3453 }
3454 }
3455
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003456 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00003457 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003458 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00003459
3460 // If this target supports SRL_PARTS, use it.
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003461 if (TLI.isOperationLegal(ISD::SRL_PARTS, NVT)) {
Chris Lattner4157c412005-04-02 04:00:59 +00003462 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
3463 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00003464 break;
3465 }
3466
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003467 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00003468 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00003469 break;
3470
Misha Brukman835702a2005-04-21 22:36:52 +00003471 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003472 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
3473 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003474 break;
3475 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00003476 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
3477 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00003478 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00003479 case ISD::MUL: {
Chris Lattnerf12eb4d2005-08-24 16:35:28 +00003480 if (TLI.isOperationLegal(ISD::MULHU, NVT)) {
Nate Begemanadd0c632005-04-11 03:01:51 +00003481 SDOperand LL, LH, RL, RH;
3482 ExpandOp(Node->getOperand(0), LL, LH);
3483 ExpandOp(Node->getOperand(1), RL, RH);
Nate Begeman43144a22005-08-30 02:44:00 +00003484 unsigned SH = MVT::getSizeInBits(RH.getValueType())-1;
3485 // MULHS implicitly sign extends its inputs. Check to see if ExpandOp
3486 // extended the sign bit of the low half through the upper half, and if so
3487 // emit a MULHS instead of the alternate sequence that is valid for any
3488 // i64 x i64 multiply.
3489 if (TLI.isOperationLegal(ISD::MULHS, NVT) &&
3490 // is RH an extension of the sign bit of RL?
3491 RH.getOpcode() == ISD::SRA && RH.getOperand(0) == RL &&
3492 RH.getOperand(1).getOpcode() == ISD::Constant &&
3493 cast<ConstantSDNode>(RH.getOperand(1))->getValue() == SH &&
3494 // is LH an extension of the sign bit of LL?
3495 LH.getOpcode() == ISD::SRA && LH.getOperand(0) == LL &&
3496 LH.getOperand(1).getOpcode() == ISD::Constant &&
3497 cast<ConstantSDNode>(LH.getOperand(1))->getValue() == SH) {
3498 Hi = DAG.getNode(ISD::MULHS, NVT, LL, RL);
3499 } else {
3500 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
3501 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
3502 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
3503 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
3504 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
3505 }
Nate Begemanadd0c632005-04-11 03:01:51 +00003506 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
3507 } else {
3508 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
3509 }
3510 break;
3511 }
Chris Lattneraac464e2005-01-21 06:05:23 +00003512 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
3513 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
3514 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
3515 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00003516 }
3517
3518 // Remember in a map if the values will be reused later.
Chris Lattner1a570f12005-09-02 20:32:45 +00003519 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
3520 std::make_pair(Lo, Hi))).second;
3521 assert(isNew && "Value already expanded?!?");
Chris Lattnerdc750592005-01-07 07:47:09 +00003522}
3523
3524
3525// SelectionDAG::Legalize - This is the entry point for the file.
3526//
Chris Lattner4add7e32005-01-23 04:42:50 +00003527void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00003528 /// run - This is the main entry point to this class.
3529 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00003530 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00003531}
3532