blob: 144de14aef6cddb18889a1b0347e68d8a601376c [file] [log] [blame]
Chris Lattnerdc750592005-01-07 07:47:09 +00001//===-- LegalizeDAG.cpp - Implement SelectionDAG::Legalize ----------------===//
Misha Brukman835702a2005-04-21 22:36:52 +00002//
Chris Lattnerdc750592005-01-07 07:47:09 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman835702a2005-04-21 22:36:52 +00007//
Chris Lattnerdc750592005-01-07 07:47:09 +00008//===----------------------------------------------------------------------===//
9//
10// This file implements the SelectionDAG::Legalize method.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/CodeGen/SelectionDAG.h"
15#include "llvm/CodeGen/MachineConstantPool.h"
16#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner99222f72005-01-15 07:15:18 +000017#include "llvm/CodeGen/MachineFrameInfo.h"
Chris Lattnerdc750592005-01-07 07:47:09 +000018#include "llvm/Target/TargetLowering.h"
Chris Lattner85d70c62005-01-11 05:57:22 +000019#include "llvm/Target/TargetData.h"
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +000020#include "llvm/Target/TargetOptions.h"
Chris 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>
24using namespace llvm;
25
26//===----------------------------------------------------------------------===//
27/// SelectionDAGLegalize - This takes an arbitrary SelectionDAG as input and
28/// hacks on it until the target machine can handle it. This involves
29/// eliminating value sizes the machine cannot handle (promoting small sizes to
30/// large sizes or splitting up large values into small values) as well as
31/// eliminating operations the machine cannot handle.
32///
33/// This code also does a small amount of optimization and recognition of idioms
34/// as part of its processing. For example, if a target does not support a
35/// 'setcc' instruction efficiently, but does support 'brcc' instruction, this
36/// will attempt merge setcc and brc instructions into brcc's.
37///
38namespace {
39class SelectionDAGLegalize {
40 TargetLowering &TLI;
41 SelectionDAG &DAG;
42
43 /// LegalizeAction - This enum indicates what action we should take for each
44 /// value type the can occur in the program.
45 enum LegalizeAction {
46 Legal, // The target natively supports this value type.
47 Promote, // This should be promoted to the next larger type.
48 Expand, // This integer type should be broken into smaller pieces.
49 };
50
Chris Lattnerdc750592005-01-07 07:47:09 +000051 /// ValueTypeActions - This is a bitvector that contains two bits for each
52 /// value type, where the two bits correspond to the LegalizeAction enum.
53 /// This can be queried with "getTypeAction(VT)".
54 unsigned ValueTypeActions;
55
56 /// NeedsAnotherIteration - This is set when we expand a large integer
57 /// operation into smaller integer operations, but the smaller operations are
58 /// not set. This occurs only rarely in practice, for targets that don't have
59 /// 32-bit or larger integer registers.
60 bool NeedsAnotherIteration;
61
62 /// LegalizedNodes - For nodes that are of legal width, and that have more
63 /// than one use, this map indicates what regularized operand to use. This
64 /// allows us to avoid legalizing the same thing more than once.
65 std::map<SDOperand, SDOperand> LegalizedNodes;
66
Chris Lattner1f2c9d82005-01-15 05:21:40 +000067 /// PromotedNodes - For nodes that are below legal width, and that have more
68 /// than one use, this map indicates what promoted value to use. This allows
69 /// us to avoid promoting the same thing more than once.
70 std::map<SDOperand, SDOperand> PromotedNodes;
71
Chris Lattnerdc750592005-01-07 07:47:09 +000072 /// ExpandedNodes - For nodes that need to be expanded, and which have more
73 /// than one use, this map indicates which which operands are the expanded
74 /// version of the input. This allows us to avoid expanding the same node
75 /// more than once.
76 std::map<SDOperand, std::pair<SDOperand, SDOperand> > ExpandedNodes;
77
Chris Lattnerea4ca942005-01-07 22:28:47 +000078 void AddLegalizedOperand(SDOperand From, SDOperand To) {
79 bool isNew = LegalizedNodes.insert(std::make_pair(From, To)).second;
80 assert(isNew && "Got into the map somehow?");
81 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +000082 void AddPromotedOperand(SDOperand From, SDOperand To) {
83 bool isNew = PromotedNodes.insert(std::make_pair(From, To)).second;
84 assert(isNew && "Got into the map somehow?");
85 }
Chris Lattnerea4ca942005-01-07 22:28:47 +000086
Chris Lattnerdc750592005-01-07 07:47:09 +000087public:
88
Chris Lattner4add7e32005-01-23 04:42:50 +000089 SelectionDAGLegalize(SelectionDAG &DAG);
Chris Lattnerdc750592005-01-07 07:47:09 +000090
91 /// Run - While there is still lowering to do, perform a pass over the DAG.
92 /// Most regularization can be done in a single pass, but targets that require
93 /// large values to be split into registers multiple times (e.g. i64 -> 4x
94 /// i16) require iteration for these values (the first iteration will demote
95 /// to i32, the second will demote to i16).
96 void Run() {
97 do {
98 NeedsAnotherIteration = false;
99 LegalizeDAG();
100 } while (NeedsAnotherIteration);
101 }
102
103 /// getTypeAction - Return how we should legalize values of this type, either
104 /// it is already legal or we need to expand it into multiple registers of
105 /// smaller integer type, or we need to promote it to a larger type.
106 LegalizeAction getTypeAction(MVT::ValueType VT) const {
107 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
108 }
109
110 /// isTypeLegal - Return true if this type is legal on this target.
111 ///
112 bool isTypeLegal(MVT::ValueType VT) const {
113 return getTypeAction(VT) == Legal;
114 }
115
116private:
117 void LegalizeDAG();
118
119 SDOperand LegalizeOp(SDOperand O);
120 void ExpandOp(SDOperand O, SDOperand &Lo, SDOperand &Hi);
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000121 SDOperand PromoteOp(SDOperand O);
Chris Lattnerdc750592005-01-07 07:47:09 +0000122
Chris Lattneraac464e2005-01-21 06:05:23 +0000123 SDOperand ExpandLibCall(const char *Name, SDNode *Node,
124 SDOperand &Hi);
125 SDOperand ExpandIntToFP(bool isSigned, MVT::ValueType DestTy,
126 SDOperand Source);
Chris Lattnere3e847b2005-07-16 00:19:57 +0000127
128 SDOperand ExpandLegalUINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT);
Nate Begeman7e74c832005-07-16 02:02:34 +0000129 SDOperand PromoteLegalINT_TO_FP(SDOperand LegalOp, MVT::ValueType DestVT,
130 bool isSigned);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000131
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000132 bool ExpandShift(unsigned Opc, SDOperand Op, SDOperand Amt,
133 SDOperand &Lo, SDOperand &Hi);
Chris Lattner4157c412005-04-02 04:00:59 +0000134 void ExpandShiftParts(unsigned NodeOp, SDOperand Op, SDOperand Amt,
135 SDOperand &Lo, SDOperand &Hi);
136 void ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
Chris Lattner2e5872c2005-04-02 03:38:53 +0000137 SDOperand &Lo, SDOperand &Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +0000138
Chris Lattnera5bf1032005-05-12 04:49:08 +0000139 void SpliceCallInto(const SDOperand &CallResult, SDNode *OutChain);
140
Chris Lattnerdc750592005-01-07 07:47:09 +0000141 SDOperand getIntPtrConstant(uint64_t Val) {
142 return DAG.getConstant(Val, TLI.getPointerTy());
143 }
144};
145}
146
147
Chris Lattner4add7e32005-01-23 04:42:50 +0000148SelectionDAGLegalize::SelectionDAGLegalize(SelectionDAG &dag)
149 : TLI(dag.getTargetLoweringInfo()), DAG(dag),
150 ValueTypeActions(TLI.getValueTypeActions()) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000151 assert(MVT::LAST_VALUETYPE <= 16 &&
152 "Too many value types for ValueTypeActions to hold!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000153}
154
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000155/// ExpandLegalUINT_TO_FP - This function is responsible for legalizing a
Chris Lattnere3e847b2005-07-16 00:19:57 +0000156/// UINT_TO_FP operation of the specified operand when the target requests that
157/// we expand it. At this point, we know that the result and operand types are
158/// legal for the target.
159SDOperand SelectionDAGLegalize::ExpandLegalUINT_TO_FP(SDOperand Op0,
160 MVT::ValueType DestVT) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000161 SDOperand Tmp1 = DAG.getNode(ISD::SINT_TO_FP, DestVT, Op0);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000162
163 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(),
Chris Lattnere3e847b2005-07-16 00:19:57 +0000164 Op0,
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000165 DAG.getConstant(0,
Chris Lattnere3e847b2005-07-16 00:19:57 +0000166 Op0.getValueType()));
167 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
168 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
169 SignSet, Four, Zero);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000170
Chris Lattnerb35912e2005-07-18 04:31:14 +0000171 // If the sign bit of the integer is set, the large number will be treated as
172 // a negative number. To counteract this, the dynamic code adds an offset
173 // depending on the data type.
174 uint64_t FF;
175 switch (Op0.getValueType()) {
176 default: assert(0 && "Unsupported integer type!");
177 case MVT::i8 : FF = 0x43800000ULL; break; // 2^8 (as a float)
178 case MVT::i16: FF = 0x47800000ULL; break; // 2^16 (as a float)
179 case MVT::i32: FF = 0x4F800000ULL; break; // 2^32 (as a float)
180 case MVT::i64: FF = 0x5F800000ULL; break; // 2^64 (as a float)
181 }
Chris Lattnere3e847b2005-07-16 00:19:57 +0000182 if (TLI.isLittleEndian()) FF <<= 32;
183 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000184
Chris Lattnere3e847b2005-07-16 00:19:57 +0000185 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
186 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
187 TLI.getPointerTy());
188 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
189 SDOperand FudgeInReg;
190 if (DestVT == MVT::f32)
191 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
192 DAG.getSrcValue(NULL));
193 else {
194 assert(DestVT == MVT::f64 && "Unexpected conversion");
195 FudgeInReg = LegalizeOp(DAG.getExtLoad(ISD::EXTLOAD, MVT::f64,
196 DAG.getEntryNode(), CPIdx,
197 DAG.getSrcValue(NULL), MVT::f32));
198 }
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000199
Chris Lattnere3e847b2005-07-16 00:19:57 +0000200 NeedsAnotherIteration = true;
201 return DAG.getNode(ISD::ADD, DestVT, Tmp1, FudgeInReg);
202}
203
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000204/// PromoteLegalUINT_TO_FP - This function is responsible for legalizing a
Chris Lattnere3e847b2005-07-16 00:19:57 +0000205/// UINT_TO_FP operation of the specified operand when the target requests that
206/// we promote it. At this point, we know that the result and operand types are
207/// legal for the target, and that there is a legal UINT_TO_FP or SINT_TO_FP
208/// operation that takes a larger input.
Nate Begeman7e74c832005-07-16 02:02:34 +0000209SDOperand SelectionDAGLegalize::PromoteLegalINT_TO_FP(SDOperand LegalOp,
210 MVT::ValueType DestVT,
211 bool isSigned) {
Chris Lattnere3e847b2005-07-16 00:19:57 +0000212 // First step, figure out the appropriate *INT_TO_FP operation to use.
213 MVT::ValueType NewInTy = LegalOp.getValueType();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000214
Chris Lattnere3e847b2005-07-16 00:19:57 +0000215 unsigned OpToUse = 0;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000216
Chris Lattnere3e847b2005-07-16 00:19:57 +0000217 // Scan for the appropriate larger type to use.
218 while (1) {
219 NewInTy = (MVT::ValueType)(NewInTy+1);
220 assert(MVT::isInteger(NewInTy) && "Ran out of possibilities!");
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000221
Chris Lattnere3e847b2005-07-16 00:19:57 +0000222 // If the target supports SINT_TO_FP of this type, use it.
223 switch (TLI.getOperationAction(ISD::SINT_TO_FP, NewInTy)) {
224 default: break;
225 case TargetLowering::Legal:
226 if (!TLI.hasNativeSupportFor(NewInTy))
227 break; // Can't use this datatype.
228 // FALL THROUGH.
229 case TargetLowering::Custom:
230 OpToUse = ISD::SINT_TO_FP;
231 break;
232 }
233 if (OpToUse) break;
Nate Begeman7e74c832005-07-16 02:02:34 +0000234 if (isSigned) continue;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000235
Chris Lattnere3e847b2005-07-16 00:19:57 +0000236 // If the target supports UINT_TO_FP of this type, use it.
237 switch (TLI.getOperationAction(ISD::UINT_TO_FP, NewInTy)) {
238 default: break;
239 case TargetLowering::Legal:
240 if (!TLI.hasNativeSupportFor(NewInTy))
241 break; // Can't use this datatype.
242 // FALL THROUGH.
243 case TargetLowering::Custom:
244 OpToUse = ISD::UINT_TO_FP;
245 break;
246 }
247 if (OpToUse) break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000248
Chris Lattnere3e847b2005-07-16 00:19:57 +0000249 // Otherwise, try a larger type.
250 }
251
252 // Make sure to legalize any nodes we create here in the next pass.
253 NeedsAnotherIteration = true;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000254
Chris Lattnere3e847b2005-07-16 00:19:57 +0000255 // Okay, we found the operation and type to use. Zero extend our input to the
256 // desired type then run the operation on it.
257 return DAG.getNode(OpToUse, DestVT,
Nate Begeman7e74c832005-07-16 02:02:34 +0000258 DAG.getNode(isSigned ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND,
259 NewInTy, LegalOp));
Chris Lattnere3e847b2005-07-16 00:19:57 +0000260}
261
Chris Lattnerdc750592005-01-07 07:47:09 +0000262void SelectionDAGLegalize::LegalizeDAG() {
263 SDOperand OldRoot = DAG.getRoot();
264 SDOperand NewRoot = LegalizeOp(OldRoot);
265 DAG.setRoot(NewRoot);
266
267 ExpandedNodes.clear();
268 LegalizedNodes.clear();
Chris Lattner87a769c2005-01-16 01:11:45 +0000269 PromotedNodes.clear();
Chris Lattnerdc750592005-01-07 07:47:09 +0000270
271 // Remove dead nodes now.
Chris Lattner473825c2005-01-07 21:09:37 +0000272 DAG.RemoveDeadNodes(OldRoot.Val);
Chris Lattnerdc750592005-01-07 07:47:09 +0000273}
274
275SDOperand SelectionDAGLegalize::LegalizeOp(SDOperand Op) {
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000276 assert(getTypeAction(Op.getValueType()) == Legal &&
277 "Caller should expand or promote operands that are not legal!");
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000278 SDNode *Node = Op.Val;
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000279
Chris Lattnerdc750592005-01-07 07:47:09 +0000280 // If this operation defines any values that cannot be represented in a
Chris Lattnerc0f31c52005-01-08 20:35:13 +0000281 // register on this target, make sure to expand or promote them.
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000282 if (Node->getNumValues() > 1) {
283 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
284 switch (getTypeAction(Node->getValueType(i))) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000285 case Legal: break; // Nothing to do.
286 case Expand: {
287 SDOperand T1, T2;
288 ExpandOp(Op.getValue(i), T1, T2);
289 assert(LegalizedNodes.count(Op) &&
290 "Expansion didn't add legal operands!");
291 return LegalizedNodes[Op];
292 }
293 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000294 PromoteOp(Op.getValue(i));
295 assert(LegalizedNodes.count(Op) &&
296 "Expansion didn't add legal operands!");
297 return LegalizedNodes[Op];
Chris Lattnerdc750592005-01-07 07:47:09 +0000298 }
299 }
300
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000301 // Note that LegalizeOp may be reentered even from single-use nodes, which
302 // means that we always must cache transformed nodes.
Chris Lattner85d70c62005-01-11 05:57:22 +0000303 std::map<SDOperand, SDOperand>::iterator I = LegalizedNodes.find(Op);
304 if (I != LegalizedNodes.end()) return I->second;
Chris Lattnerdc750592005-01-07 07:47:09 +0000305
Chris Lattnerec26b482005-01-09 19:03:49 +0000306 SDOperand Tmp1, Tmp2, Tmp3;
Chris Lattnerdc750592005-01-07 07:47:09 +0000307
308 SDOperand Result = Op;
Chris Lattnerdc750592005-01-07 07:47:09 +0000309
310 switch (Node->getOpcode()) {
311 default:
Chris Lattner3eb86932005-05-14 06:34:48 +0000312 if (Node->getOpcode() >= ISD::BUILTIN_OP_END) {
313 // If this is a target node, legalize it by legalizing the operands then
314 // passing it through.
315 std::vector<SDOperand> Ops;
316 bool Changed = false;
317 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
318 Ops.push_back(LegalizeOp(Node->getOperand(i)));
319 Changed = Changed || Node->getOperand(i) != Ops.back();
320 }
321 if (Changed)
322 if (Node->getNumValues() == 1)
323 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Ops);
324 else {
325 std::vector<MVT::ValueType> VTs(Node->value_begin(),
326 Node->value_end());
327 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
328 }
329
330 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
331 AddLegalizedOperand(Op.getValue(i), Result.getValue(i));
332 return Result.getValue(Op.ResNo);
333 }
334 // Otherwise this is an unhandled builtin node. splat.
Chris Lattnerdc750592005-01-07 07:47:09 +0000335 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
336 assert(0 && "Do not know how to legalize this operator!");
337 abort();
338 case ISD::EntryToken:
339 case ISD::FrameIndex:
340 case ISD::GlobalAddress:
Chris Lattner32f20bf2005-01-07 21:45:56 +0000341 case ISD::ExternalSymbol:
Chris Lattner3b8e7192005-01-14 22:38:01 +0000342 case ISD::ConstantPool: // Nothing to do.
Chris Lattnerdc750592005-01-07 07:47:09 +0000343 assert(getTypeAction(Node->getValueType(0)) == Legal &&
344 "This must be legal!");
345 break;
Chris Lattner3b8e7192005-01-14 22:38:01 +0000346 case ISD::CopyFromReg:
347 Tmp1 = LegalizeOp(Node->getOperand(0));
348 if (Tmp1 != Node->getOperand(0))
349 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(),
350 Node->getValueType(0), Tmp1);
Chris Lattnereb6614d2005-01-28 06:27:38 +0000351 else
352 Result = Op.getValue(0);
353
354 // Since CopyFromReg produces two values, make sure to remember that we
355 // legalized both of them.
356 AddLegalizedOperand(Op.getValue(0), Result);
357 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
358 return Result.getValue(Op.ResNo);
Chris Lattnere727af02005-01-13 20:50:02 +0000359 case ISD::ImplicitDef:
360 Tmp1 = LegalizeOp(Node->getOperand(0));
361 if (Tmp1 != Node->getOperand(0))
Chris Lattner39c67442005-01-14 22:08:15 +0000362 Result = DAG.getImplicitDef(Tmp1, cast<RegSDNode>(Node)->getReg());
Chris Lattnere727af02005-01-13 20:50:02 +0000363 break;
Nate Begemancda9aa72005-04-01 22:34:39 +0000364 case ISD::UNDEF: {
365 MVT::ValueType VT = Op.getValueType();
366 switch (TLI.getOperationAction(ISD::UNDEF, VT)) {
Nate Begeman69d39432005-04-02 00:41:14 +0000367 default: assert(0 && "This action is not supported yet!");
368 case TargetLowering::Expand:
369 case TargetLowering::Promote:
Nate Begemancda9aa72005-04-01 22:34:39 +0000370 if (MVT::isInteger(VT))
371 Result = DAG.getConstant(0, VT);
372 else if (MVT::isFloatingPoint(VT))
373 Result = DAG.getConstantFP(0, VT);
374 else
375 assert(0 && "Unknown value type!");
376 break;
Nate Begeman69d39432005-04-02 00:41:14 +0000377 case TargetLowering::Legal:
Nate Begemancda9aa72005-04-01 22:34:39 +0000378 break;
379 }
380 break;
381 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000382 case ISD::Constant:
383 // We know we don't need to expand constants here, constants only have one
384 // value and we check that it is fine above.
385
386 // FIXME: Maybe we should handle things like targets that don't support full
387 // 32-bit immediates?
388 break;
389 case ISD::ConstantFP: {
390 // Spill FP immediates to the constant pool if the target cannot directly
391 // codegen them. Targets often have some immediate values that can be
392 // efficiently generated into an FP register without a load. We explicitly
393 // leave these constants as ConstantFP nodes for the target to deal with.
394
395 ConstantFPSDNode *CFP = cast<ConstantFPSDNode>(Node);
396
397 // Check to see if this FP immediate is already legal.
398 bool isLegal = false;
399 for (TargetLowering::legal_fpimm_iterator I = TLI.legal_fpimm_begin(),
400 E = TLI.legal_fpimm_end(); I != E; ++I)
401 if (CFP->isExactlyValue(*I)) {
402 isLegal = true;
403 break;
404 }
405
406 if (!isLegal) {
407 // Otherwise we need to spill the constant to memory.
408 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
409
410 bool Extend = false;
411
412 // If a FP immediate is precise when represented as a float, we put it
413 // into the constant pool as a float, even if it's is statically typed
414 // as a double.
415 MVT::ValueType VT = CFP->getValueType(0);
416 bool isDouble = VT == MVT::f64;
417 ConstantFP *LLVMC = ConstantFP::get(isDouble ? Type::DoubleTy :
418 Type::FloatTy, CFP->getValue());
Chris Lattnerbc7497d2005-01-28 22:58:25 +0000419 if (isDouble && CFP->isExactlyValue((float)CFP->getValue()) &&
420 // Only do this if the target has a native EXTLOAD instruction from
421 // f32.
422 TLI.getOperationAction(ISD::EXTLOAD,
423 MVT::f32) == TargetLowering::Legal) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000424 LLVMC = cast<ConstantFP>(ConstantExpr::getCast(LLVMC, Type::FloatTy));
425 VT = MVT::f32;
426 Extend = true;
427 }
Misha Brukman835702a2005-04-21 22:36:52 +0000428
Chris Lattnerdc750592005-01-07 07:47:09 +0000429 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(LLVMC),
430 TLI.getPointerTy());
Chris Lattner3ba56b32005-01-16 05:06:12 +0000431 if (Extend) {
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000432 Result = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
433 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattner3ba56b32005-01-16 05:06:12 +0000434 } else {
Chris Lattner5385db52005-05-09 20:23:03 +0000435 Result = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
436 DAG.getSrcValue(NULL));
Chris Lattner3ba56b32005-01-16 05:06:12 +0000437 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000438 }
439 break;
440 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000441 case ISD::TokenFactor: {
442 std::vector<SDOperand> Ops;
443 bool Changed = false;
444 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
Chris Lattner55562fa2005-01-19 19:10:54 +0000445 SDOperand Op = Node->getOperand(i);
446 // Fold single-use TokenFactor nodes into this token factor as we go.
Chris Lattnerf09c0b42005-05-12 06:04:14 +0000447 // FIXME: This is something that the DAGCombiner should do!!
Chris Lattner55562fa2005-01-19 19:10:54 +0000448 if (Op.getOpcode() == ISD::TokenFactor && Op.hasOneUse()) {
449 Changed = true;
450 for (unsigned j = 0, e = Op.getNumOperands(); j != e; ++j)
451 Ops.push_back(LegalizeOp(Op.getOperand(j)));
452 } else {
453 Ops.push_back(LegalizeOp(Op)); // Legalize the operands
454 Changed |= Ops[i] != Op;
455 }
Chris Lattner05b4e372005-01-13 17:59:25 +0000456 }
457 if (Changed)
458 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Ops);
459 break;
460 }
461
Chris Lattner2dce7032005-05-12 23:24:06 +0000462 case ISD::CALLSEQ_START:
463 case ISD::CALLSEQ_END:
Chris Lattnerdc750592005-01-07 07:47:09 +0000464 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd34cd282005-05-12 23:24:44 +0000465 // Do not try to legalize the target-specific arguments (#1+)
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000466 Tmp2 = Node->getOperand(0);
467 if (Tmp1 != Tmp2) {
Chris Lattner8005e912005-05-12 00:17:04 +0000468 Node->setAdjCallChain(Tmp1);
Chris Lattnerb5a78e02005-05-12 16:53:42 +0000469
470 // If moving the operand from pointing to Tmp2 dropped its use count to 1,
471 // this will cause the maps used to memoize results to get confused.
472 // Create and add a dummy use, just to increase its use count. This will
473 // be removed at the end of legalize when dead nodes are removed.
474 if (Tmp2.Val->hasOneUse())
475 DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp2,
476 DAG.getConstant(0, MVT::i32));
477 }
Chris Lattner2dce7032005-05-12 23:24:06 +0000478 // Note that we do not create new CALLSEQ_DOWN/UP nodes here. These
Chris Lattner8005e912005-05-12 00:17:04 +0000479 // nodes are treated specially and are mutated in place. This makes the dag
480 // legalization process more efficient and also makes libcall insertion
481 // easier.
Chris Lattnerdc750592005-01-07 07:47:09 +0000482 break;
Chris Lattnerec26b482005-01-09 19:03:49 +0000483 case ISD::DYNAMIC_STACKALLOC:
484 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
485 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the size.
486 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the alignment.
487 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
Chris Lattner96c262e2005-05-14 07:29:57 +0000488 Tmp3 != Node->getOperand(2)) {
489 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
490 std::vector<SDOperand> Ops;
491 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
492 Result = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, Ops);
493 } else
Chris Lattner02f5ce22005-01-09 19:07:54 +0000494 Result = Op.getValue(0);
Chris Lattnerec26b482005-01-09 19:03:49 +0000495
496 // Since this op produces two values, make sure to remember that we
497 // legalized both of them.
498 AddLegalizedOperand(SDOperand(Node, 0), Result);
499 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
500 return Result.getValue(Op.ResNo);
501
Chris Lattnerd0feb642005-05-13 18:43:43 +0000502 case ISD::TAILCALL:
Chris Lattner3d95c142005-01-19 20:24:35 +0000503 case ISD::CALL: {
Chris Lattnerdc750592005-01-07 07:47:09 +0000504 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
505 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
Chris Lattner3d95c142005-01-19 20:24:35 +0000506
507 bool Changed = false;
508 std::vector<SDOperand> Ops;
509 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
510 Ops.push_back(LegalizeOp(Node->getOperand(i)));
511 Changed |= Ops.back() != Node->getOperand(i);
512 }
513
514 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) || Changed) {
Chris Lattnerdc750592005-01-07 07:47:09 +0000515 std::vector<MVT::ValueType> RetTyVTs;
516 RetTyVTs.reserve(Node->getNumValues());
517 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
Chris Lattnerf025d672005-01-07 21:34:13 +0000518 RetTyVTs.push_back(Node->getValueType(i));
Chris Lattnerd0feb642005-05-13 18:43:43 +0000519 Result = SDOperand(DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
520 Node->getOpcode() == ISD::TAILCALL), 0);
Chris Lattner9242c502005-01-09 19:43:23 +0000521 } else {
522 Result = Result.getValue(0);
Chris Lattnerdc750592005-01-07 07:47:09 +0000523 }
Chris Lattner9242c502005-01-09 19:43:23 +0000524 // Since calls produce multiple values, make sure to remember that we
525 // legalized all of them.
526 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
527 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
528 return Result.getValue(Op.ResNo);
Chris Lattner3d95c142005-01-19 20:24:35 +0000529 }
Chris Lattner68a12142005-01-07 22:12:08 +0000530 case ISD::BR:
531 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
532 if (Tmp1 != Node->getOperand(0))
533 Result = DAG.getNode(ISD::BR, MVT::Other, Tmp1, Node->getOperand(1));
534 break;
535
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000536 case ISD::BRCOND:
537 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000538
539 switch (getTypeAction(Node->getOperand(1).getValueType())) {
540 case Expand: assert(0 && "It's impossible to expand bools");
541 case Legal:
542 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
543 break;
544 case Promote:
545 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
546 break;
547 }
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000548 // Basic block destination (Op#2) is always legal.
549 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
550 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
551 Node->getOperand(2));
552 break;
Chris Lattnerfd986782005-04-09 03:30:19 +0000553 case ISD::BRCONDTWOWAY:
554 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
555 switch (getTypeAction(Node->getOperand(1).getValueType())) {
556 case Expand: assert(0 && "It's impossible to expand bools");
557 case Legal:
558 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the condition.
559 break;
560 case Promote:
561 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the condition.
562 break;
563 }
564 // If this target does not support BRCONDTWOWAY, lower it to a BRCOND/BR
565 // pair.
566 switch (TLI.getOperationAction(ISD::BRCONDTWOWAY, MVT::Other)) {
567 case TargetLowering::Promote:
568 default: assert(0 && "This action is not supported yet!");
569 case TargetLowering::Legal:
570 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
571 std::vector<SDOperand> Ops;
572 Ops.push_back(Tmp1);
573 Ops.push_back(Tmp2);
574 Ops.push_back(Node->getOperand(2));
575 Ops.push_back(Node->getOperand(3));
576 Result = DAG.getNode(ISD::BRCONDTWOWAY, MVT::Other, Ops);
577 }
578 break;
579 case TargetLowering::Expand:
580 Result = DAG.getNode(ISD::BRCOND, MVT::Other, Tmp1, Tmp2,
581 Node->getOperand(2));
582 Result = DAG.getNode(ISD::BR, MVT::Other, Result, Node->getOperand(3));
583 break;
584 }
585 break;
Chris Lattnerec3fe7c2005-01-07 08:19:42 +0000586
Chris Lattnerdc750592005-01-07 07:47:09 +0000587 case ISD::LOAD:
588 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
589 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000590
Chris Lattnerdc750592005-01-07 07:47:09 +0000591 if (Tmp1 != Node->getOperand(0) ||
592 Tmp2 != Node->getOperand(1))
Chris Lattner5385db52005-05-09 20:23:03 +0000593 Result = DAG.getLoad(Node->getValueType(0), Tmp1, Tmp2,
594 Node->getOperand(2));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000595 else
596 Result = SDOperand(Node, 0);
Misha Brukman835702a2005-04-21 22:36:52 +0000597
Chris Lattnerea4ca942005-01-07 22:28:47 +0000598 // Since loads produce two values, make sure to remember that we legalized
599 // both of them.
600 AddLegalizedOperand(SDOperand(Node, 0), Result);
601 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
602 return Result.getValue(Op.ResNo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000603
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000604 case ISD::EXTLOAD:
605 case ISD::SEXTLOAD:
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000606 case ISD::ZEXTLOAD: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000607 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
608 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000609
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000610 MVT::ValueType SrcVT = cast<VTSDNode>(Node->getOperand(3))->getVT();
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000611 switch (TLI.getOperationAction(Node->getOpcode(), SrcVT)) {
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000612 default: assert(0 && "This action is not supported yet!");
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000613 case TargetLowering::Promote:
614 assert(SrcVT == MVT::i1 && "Can only promote EXTLOAD from i1 -> i8!");
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000615 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
616 Tmp1, Tmp2, Node->getOperand(2), MVT::i8);
Chris Lattner0b73a6d2005-04-12 20:30:10 +0000617 // Since loads produce two values, make sure to remember that we legalized
618 // both of them.
619 AddLegalizedOperand(SDOperand(Node, 0), Result);
620 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
621 return Result.getValue(Op.ResNo);
Misha Brukman835702a2005-04-21 22:36:52 +0000622
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000623 case TargetLowering::Legal:
624 if (Tmp1 != Node->getOperand(0) ||
625 Tmp2 != Node->getOperand(1))
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000626 Result = DAG.getExtLoad(Node->getOpcode(), Node->getValueType(0),
627 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000628 else
629 Result = SDOperand(Node, 0);
630
631 // Since loads produce two values, make sure to remember that we legalized
632 // both of them.
633 AddLegalizedOperand(SDOperand(Node, 0), Result);
634 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
635 return Result.getValue(Op.ResNo);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000636 case TargetLowering::Expand:
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000637 //f64 = EXTLOAD f32 should expand to LOAD, FP_EXTEND
638 if (SrcVT == MVT::f32 && Node->getValueType(0) == MVT::f64) {
639 SDOperand Load = DAG.getLoad(SrcVT, Tmp1, Tmp2, Node->getOperand(2));
Andrew Lenharth0a370f42005-06-30 19:32:57 +0000640 Result = DAG.getNode(ISD::FP_EXTEND, Node->getValueType(0), Load);
Andrew Lenharthb5597e32005-06-30 19:22:37 +0000641 if (Op.ResNo)
642 return Load.getValue(1);
643 return Result;
644 }
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000645 assert(Node->getOpcode() != ISD::EXTLOAD &&
646 "EXTLOAD should always be supported!");
647 // Turn the unsupported load into an EXTLOAD followed by an explicit
648 // zero/sign extend inreg.
Chris Lattnerde0a4b12005-07-10 01:55:33 +0000649 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
650 Tmp1, Tmp2, Node->getOperand(2), SrcVT);
Chris Lattner0e852af2005-04-13 02:38:47 +0000651 SDOperand ValRes;
652 if (Node->getOpcode() == ISD::SEXTLOAD)
653 ValRes = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +0000654 Result, DAG.getValueType(SrcVT));
Chris Lattner0e852af2005-04-13 02:38:47 +0000655 else
656 ValRes = DAG.getZeroExtendInReg(Result, SrcVT);
Chris Lattnera3b7ef02005-04-10 22:54:25 +0000657 AddLegalizedOperand(SDOperand(Node, 0), ValRes);
658 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
659 if (Op.ResNo)
660 return Result.getValue(1);
661 return ValRes;
662 }
663 assert(0 && "Unreachable");
664 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000665 case ISD::EXTRACT_ELEMENT:
666 // Get both the low and high parts.
667 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
668 if (cast<ConstantSDNode>(Node->getOperand(1))->getValue())
669 Result = Tmp2; // 1 -> Hi
670 else
671 Result = Tmp1; // 0 -> Lo
672 break;
673
674 case ISD::CopyToReg:
675 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Misha Brukman835702a2005-04-21 22:36:52 +0000676
Chris Lattnerdc750592005-01-07 07:47:09 +0000677 switch (getTypeAction(Node->getOperand(1).getValueType())) {
678 case Legal:
679 // Legalize the incoming value (must be legal).
680 Tmp2 = LegalizeOp(Node->getOperand(1));
681 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnere727af02005-01-13 20:50:02 +0000682 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
Chris Lattnerdc750592005-01-07 07:47:09 +0000683 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +0000684 case Promote:
685 Tmp2 = PromoteOp(Node->getOperand(1));
686 Result = DAG.getCopyToReg(Tmp1, Tmp2, cast<RegSDNode>(Node)->getReg());
687 break;
688 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000689 SDOperand Lo, Hi;
Misha Brukman835702a2005-04-21 22:36:52 +0000690 ExpandOp(Node->getOperand(1), Lo, Hi);
Chris Lattnere727af02005-01-13 20:50:02 +0000691 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattner0d03eb42005-01-19 18:02:17 +0000692 Lo = DAG.getCopyToReg(Tmp1, Lo, Reg);
693 Hi = DAG.getCopyToReg(Tmp1, Hi, Reg+1);
694 // Note that the copytoreg nodes are independent of each other.
695 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
Chris Lattnerdc750592005-01-07 07:47:09 +0000696 assert(isTypeLegal(Result.getValueType()) &&
697 "Cannot expand multiple times yet (i64 -> i16)");
698 break;
699 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000700 break;
701
702 case ISD::RET:
703 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
704 switch (Node->getNumOperands()) {
705 case 2: // ret val
706 switch (getTypeAction(Node->getOperand(1).getValueType())) {
707 case Legal:
708 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerea4ca942005-01-07 22:28:47 +0000709 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
Chris Lattnerdc750592005-01-07 07:47:09 +0000710 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
711 break;
712 case Expand: {
713 SDOperand Lo, Hi;
714 ExpandOp(Node->getOperand(1), Lo, Hi);
715 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Lo, Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000716 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000717 }
718 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000719 Tmp2 = PromoteOp(Node->getOperand(1));
720 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1, Tmp2);
721 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000722 }
723 break;
724 case 1: // ret void
725 if (Tmp1 != Node->getOperand(0))
726 Result = DAG.getNode(ISD::RET, MVT::Other, Tmp1);
727 break;
728 default: { // ret <values>
729 std::vector<SDOperand> NewValues;
730 NewValues.push_back(Tmp1);
731 for (unsigned i = 1, e = Node->getNumOperands(); i != e; ++i)
732 switch (getTypeAction(Node->getOperand(i).getValueType())) {
733 case Legal:
Chris Lattner7e6eeba2005-01-08 19:27:05 +0000734 NewValues.push_back(LegalizeOp(Node->getOperand(i)));
Chris Lattnerdc750592005-01-07 07:47:09 +0000735 break;
736 case Expand: {
737 SDOperand Lo, Hi;
738 ExpandOp(Node->getOperand(i), Lo, Hi);
739 NewValues.push_back(Lo);
740 NewValues.push_back(Hi);
Misha Brukman835702a2005-04-21 22:36:52 +0000741 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000742 }
743 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000744 assert(0 && "Can't promote multiple return value yet!");
Chris Lattnerdc750592005-01-07 07:47:09 +0000745 }
746 Result = DAG.getNode(ISD::RET, MVT::Other, NewValues);
747 break;
748 }
749 }
750 break;
751 case ISD::STORE:
752 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
753 Tmp2 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
754
Chris Lattnere69daaf2005-01-08 06:25:56 +0000755 // Turn 'store float 1.0, Ptr' -> 'store int 0x12345678, Ptr'
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000756 if (ConstantFPSDNode *CFP =dyn_cast<ConstantFPSDNode>(Node->getOperand(1))){
Chris Lattnere69daaf2005-01-08 06:25:56 +0000757 if (CFP->getValueType(0) == MVT::f32) {
758 union {
759 unsigned I;
760 float F;
761 } V;
762 V.F = CFP->getValue();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000763 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattnerba45e6c2005-05-09 20:36:57 +0000764 DAG.getConstant(V.I, MVT::i32), Tmp2,
Chris Lattner5385db52005-05-09 20:23:03 +0000765 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000766 } else {
767 assert(CFP->getValueType(0) == MVT::f64 && "Unknown FP type!");
768 union {
769 uint64_t I;
770 double F;
771 } V;
772 V.F = CFP->getValue();
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +0000773 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1,
Chris Lattner5385db52005-05-09 20:23:03 +0000774 DAG.getConstant(V.I, MVT::i64), Tmp2,
775 Node->getOperand(3));
Chris Lattnere69daaf2005-01-08 06:25:56 +0000776 }
Chris Lattnera4743132005-02-22 07:23:39 +0000777 Node = Result.Val;
Chris Lattnere69daaf2005-01-08 06:25:56 +0000778 }
779
Chris Lattnerdc750592005-01-07 07:47:09 +0000780 switch (getTypeAction(Node->getOperand(1).getValueType())) {
781 case Legal: {
782 SDOperand Val = LegalizeOp(Node->getOperand(1));
783 if (Val != Node->getOperand(1) || Tmp1 != Node->getOperand(0) ||
784 Tmp2 != Node->getOperand(2))
Chris Lattner5385db52005-05-09 20:23:03 +0000785 Result = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Val, Tmp2,
786 Node->getOperand(3));
Chris Lattnerdc750592005-01-07 07:47:09 +0000787 break;
788 }
789 case Promote:
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000790 // Truncate the value and store the result.
791 Tmp3 = PromoteOp(Node->getOperand(1));
792 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp3, Tmp2,
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000793 Node->getOperand(3),
Chris Lattner36db1ed2005-07-10 00:29:18 +0000794 DAG.getValueType(Node->getOperand(1).getValueType()));
Chris Lattner1f2c9d82005-01-15 05:21:40 +0000795 break;
796
Chris Lattnerdc750592005-01-07 07:47:09 +0000797 case Expand:
798 SDOperand Lo, Hi;
799 ExpandOp(Node->getOperand(1), Lo, Hi);
800
801 if (!TLI.isLittleEndian())
802 std::swap(Lo, Hi);
803
Chris Lattner55e9cde2005-05-11 04:51:16 +0000804 Lo = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Lo, Tmp2,
805 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000806 unsigned IncrementSize = MVT::getSizeInBits(Hi.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +0000807 Tmp2 = DAG.getNode(ISD::ADD, Tmp2.getValueType(), Tmp2,
808 getIntPtrConstant(IncrementSize));
809 assert(isTypeLegal(Tmp2.getValueType()) &&
810 "Pointers must be legal!");
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +0000811 //Again, claiming both parts of the store came form the same Instr
Chris Lattner55e9cde2005-05-11 04:51:16 +0000812 Hi = DAG.getNode(ISD::STORE, MVT::Other, Tmp1, Hi, Tmp2,
813 Node->getOperand(3));
Chris Lattner0d03eb42005-01-19 18:02:17 +0000814 Result = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo, Hi);
815 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000816 }
817 break;
Andrew Lenharthdec53922005-03-31 21:24:06 +0000818 case ISD::PCMARKER:
819 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
Chris Lattner13fe99c2005-04-02 05:00:07 +0000820 if (Tmp1 != Node->getOperand(0))
821 Result = DAG.getNode(ISD::PCMARKER, MVT::Other, Tmp1,Node->getOperand(1));
Andrew Lenharthdec53922005-03-31 21:24:06 +0000822 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000823 case ISD::TRUNCSTORE:
824 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
825 Tmp3 = LegalizeOp(Node->getOperand(2)); // Legalize the pointer.
826
827 switch (getTypeAction(Node->getOperand(1).getValueType())) {
828 case Legal:
829 Tmp2 = LegalizeOp(Node->getOperand(1));
830 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
831 Tmp3 != Node->getOperand(2))
Chris Lattner99222f72005-01-15 07:15:18 +0000832 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, Tmp1, Tmp2, Tmp3,
Chris Lattner36db1ed2005-07-10 00:29:18 +0000833 Node->getOperand(3), Node->getOperand(4));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +0000834 break;
835 case Promote:
836 case Expand:
837 assert(0 && "Cannot handle illegal TRUNCSTORE yet!");
838 }
839 break;
Chris Lattner39c67442005-01-14 22:08:15 +0000840 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +0000841 switch (getTypeAction(Node->getOperand(0).getValueType())) {
842 case Expand: assert(0 && "It's impossible to expand bools");
843 case Legal:
844 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
845 break;
846 case Promote:
847 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
848 break;
849 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000850 Tmp2 = LegalizeOp(Node->getOperand(1)); // TrueVal
Chris Lattner39c67442005-01-14 22:08:15 +0000851 Tmp3 = LegalizeOp(Node->getOperand(2)); // FalseVal
Chris Lattner3c0dd462005-01-16 07:29:19 +0000852
853 switch (TLI.getOperationAction(Node->getOpcode(), Tmp2.getValueType())) {
854 default: assert(0 && "This action is not supported yet!");
855 case TargetLowering::Legal:
856 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
857 Tmp3 != Node->getOperand(2))
858 Result = DAG.getNode(ISD::SELECT, Node->getValueType(0),
859 Tmp1, Tmp2, Tmp3);
860 break;
861 case TargetLowering::Promote: {
862 MVT::ValueType NVT =
863 TLI.getTypeToPromoteTo(ISD::SELECT, Tmp2.getValueType());
864 unsigned ExtOp, TruncOp;
865 if (MVT::isInteger(Tmp2.getValueType())) {
866 ExtOp = ISD::ZERO_EXTEND;
867 TruncOp = ISD::TRUNCATE;
868 } else {
869 ExtOp = ISD::FP_EXTEND;
870 TruncOp = ISD::FP_ROUND;
871 }
872 // Promote each of the values to the new type.
873 Tmp2 = DAG.getNode(ExtOp, NVT, Tmp2);
874 Tmp3 = DAG.getNode(ExtOp, NVT, Tmp3);
875 // Perform the larger operation, then round down.
876 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2,Tmp3);
877 Result = DAG.getNode(TruncOp, Node->getValueType(0), Result);
878 break;
879 }
880 }
Chris Lattnerdc750592005-01-07 07:47:09 +0000881 break;
Chris Lattnerdc750592005-01-07 07:47:09 +0000882 case ISD::SETCC:
883 switch (getTypeAction(Node->getOperand(0).getValueType())) {
884 case Legal:
885 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
886 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
887 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1))
888 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000889 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000890 break;
891 case Promote:
Chris Lattner4d978642005-01-15 22:16:26 +0000892 Tmp1 = PromoteOp(Node->getOperand(0)); // LHS
893 Tmp2 = PromoteOp(Node->getOperand(1)); // RHS
894
895 // If this is an FP compare, the operands have already been extended.
896 if (MVT::isInteger(Node->getOperand(0).getValueType())) {
897 MVT::ValueType VT = Node->getOperand(0).getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +0000898 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000899
900 // Otherwise, we have to insert explicit sign or zero extends. Note
901 // that we could insert sign extends for ALL conditions, but zero extend
902 // is cheaper on many machines (an AND instead of two shifts), so prefer
903 // it.
904 switch (cast<SetCCSDNode>(Node)->getCondition()) {
905 default: assert(0 && "Unknown integer comparison!");
906 case ISD::SETEQ:
907 case ISD::SETNE:
908 case ISD::SETUGE:
909 case ISD::SETUGT:
910 case ISD::SETULE:
911 case ISD::SETULT:
912 // ALL of these operations will work if we either sign or zero extend
913 // the operands (including the unsigned comparisons!). Zero extend is
914 // usually a simpler/cheaper operation, so prefer it.
Chris Lattner0e852af2005-04-13 02:38:47 +0000915 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
916 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +0000917 break;
918 case ISD::SETGE:
919 case ISD::SETGT:
920 case ISD::SETLT:
921 case ISD::SETLE:
Chris Lattner0b6ba902005-07-10 00:07:11 +0000922 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
923 DAG.getValueType(VT));
924 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
925 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +0000926 break;
927 }
928
929 }
930 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000931 Node->getValueType(0), Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000932 break;
Misha Brukman835702a2005-04-21 22:36:52 +0000933 case Expand:
Chris Lattnerdc750592005-01-07 07:47:09 +0000934 SDOperand LHSLo, LHSHi, RHSLo, RHSHi;
935 ExpandOp(Node->getOperand(0), LHSLo, LHSHi);
936 ExpandOp(Node->getOperand(1), RHSLo, RHSHi);
937 switch (cast<SetCCSDNode>(Node)->getCondition()) {
938 case ISD::SETEQ:
939 case ISD::SETNE:
Chris Lattner71ff44e2005-04-12 01:46:05 +0000940 if (RHSLo == RHSHi)
941 if (ConstantSDNode *RHSCST = dyn_cast<ConstantSDNode>(RHSLo))
942 if (RHSCST->isAllOnesValue()) {
943 // Comparison to -1.
944 Tmp1 = DAG.getNode(ISD::AND, LHSLo.getValueType(), LHSLo, LHSHi);
Misha Brukman835702a2005-04-21 22:36:52 +0000945 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattner71ff44e2005-04-12 01:46:05 +0000946 Node->getValueType(0), Tmp1, RHSLo);
Misha Brukman835702a2005-04-21 22:36:52 +0000947 break;
Chris Lattner71ff44e2005-04-12 01:46:05 +0000948 }
949
Chris Lattnerdc750592005-01-07 07:47:09 +0000950 Tmp1 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSLo, RHSLo);
951 Tmp2 = DAG.getNode(ISD::XOR, LHSLo.getValueType(), LHSHi, RHSHi);
952 Tmp1 = DAG.getNode(ISD::OR, Tmp1.getValueType(), Tmp1, Tmp2);
Misha Brukman835702a2005-04-21 22:36:52 +0000953 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000954 Node->getValueType(0), Tmp1,
Chris Lattnerdc750592005-01-07 07:47:09 +0000955 DAG.getConstant(0, Tmp1.getValueType()));
956 break;
957 default:
Chris Lattneraedcabe2005-04-12 02:19:10 +0000958 // If this is a comparison of the sign bit, just look at the top part.
959 // X > -1, x < 0
960 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Node->getOperand(1)))
Misha Brukman835702a2005-04-21 22:36:52 +0000961 if ((cast<SetCCSDNode>(Node)->getCondition() == ISD::SETLT &&
Chris Lattneraedcabe2005-04-12 02:19:10 +0000962 CST->getValue() == 0) || // X < 0
963 (cast<SetCCSDNode>(Node)->getCondition() == ISD::SETGT &&
964 (CST->isAllOnesValue()))) // X > -1
965 return DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
966 Node->getValueType(0), LHSHi, RHSHi);
967
Chris Lattnerdc750592005-01-07 07:47:09 +0000968 // FIXME: This generated code sucks.
969 ISD::CondCode LowCC;
970 switch (cast<SetCCSDNode>(Node)->getCondition()) {
971 default: assert(0 && "Unknown integer setcc!");
972 case ISD::SETLT:
973 case ISD::SETULT: LowCC = ISD::SETULT; break;
974 case ISD::SETGT:
975 case ISD::SETUGT: LowCC = ISD::SETUGT; break;
976 case ISD::SETLE:
977 case ISD::SETULE: LowCC = ISD::SETULE; break;
978 case ISD::SETGE:
979 case ISD::SETUGE: LowCC = ISD::SETUGE; break;
980 }
Misha Brukman835702a2005-04-21 22:36:52 +0000981
Chris Lattnerdc750592005-01-07 07:47:09 +0000982 // Tmp1 = lo(op1) < lo(op2) // Always unsigned comparison
983 // Tmp2 = hi(op1) < hi(op2) // Signedness depends on operands
984 // dest = hi(op1) == hi(op2) ? Tmp1 : Tmp2;
985
986 // NOTE: on targets without efficient SELECT of bools, we can always use
987 // this identity: (B1 ? B2 : B3) --> (B1 & B2)|(!B1&B3)
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000988 Tmp1 = DAG.getSetCC(LowCC, Node->getValueType(0), LHSLo, RHSLo);
Chris Lattnerdc750592005-01-07 07:47:09 +0000989 Tmp2 = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
Chris Lattnerb07e2d22005-01-18 02:52:03 +0000990 Node->getValueType(0), LHSHi, RHSHi);
991 Result = DAG.getSetCC(ISD::SETEQ, Node->getValueType(0), LHSHi, RHSHi);
992 Result = DAG.getNode(ISD::SELECT, Tmp1.getValueType(),
993 Result, Tmp1, Tmp2);
Chris Lattnerdc750592005-01-07 07:47:09 +0000994 break;
995 }
996 }
997 break;
998
Chris Lattner85d70c62005-01-11 05:57:22 +0000999 case ISD::MEMSET:
1000 case ISD::MEMCPY:
1001 case ISD::MEMMOVE: {
Chris Lattner4487b2e2005-02-01 18:38:28 +00001002 Tmp1 = LegalizeOp(Node->getOperand(0)); // Chain
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001003 Tmp2 = LegalizeOp(Node->getOperand(1)); // Pointer
1004
1005 if (Node->getOpcode() == ISD::MEMSET) { // memset = ubyte
1006 switch (getTypeAction(Node->getOperand(2).getValueType())) {
1007 case Expand: assert(0 && "Cannot expand a byte!");
1008 case Legal:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001009 Tmp3 = LegalizeOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001010 break;
1011 case Promote:
Chris Lattner4487b2e2005-02-01 18:38:28 +00001012 Tmp3 = PromoteOp(Node->getOperand(2));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001013 break;
1014 }
1015 } else {
Misha Brukman835702a2005-04-21 22:36:52 +00001016 Tmp3 = LegalizeOp(Node->getOperand(2)); // memcpy/move = pointer,
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001017 }
Chris Lattner5aa75e42005-02-02 03:44:41 +00001018
1019 SDOperand Tmp4;
1020 switch (getTypeAction(Node->getOperand(3).getValueType())) {
Chris Lattnerba08a332005-07-13 01:42:45 +00001021 case Expand: {
1022 // Length is too big, just take the lo-part of the length.
1023 SDOperand HiPart;
1024 ExpandOp(Node->getOperand(3), HiPart, Tmp4);
1025 break;
1026 }
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001027 case Legal:
1028 Tmp4 = LegalizeOp(Node->getOperand(3));
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001029 break;
1030 case Promote:
1031 Tmp4 = PromoteOp(Node->getOperand(3));
Chris Lattner5aa75e42005-02-02 03:44:41 +00001032 break;
1033 }
1034
1035 SDOperand Tmp5;
1036 switch (getTypeAction(Node->getOperand(4).getValueType())) { // uint
1037 case Expand: assert(0 && "Cannot expand this yet!");
1038 case Legal:
1039 Tmp5 = LegalizeOp(Node->getOperand(4));
1040 break;
1041 case Promote:
Chris Lattnera4cfafe2005-01-28 22:29:18 +00001042 Tmp5 = PromoteOp(Node->getOperand(4));
1043 break;
1044 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001045
1046 switch (TLI.getOperationAction(Node->getOpcode(), MVT::Other)) {
1047 default: assert(0 && "This action not implemented for this operation!");
1048 case TargetLowering::Legal:
Chris Lattner85d70c62005-01-11 05:57:22 +00001049 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1050 Tmp3 != Node->getOperand(2) || Tmp4 != Node->getOperand(3) ||
1051 Tmp5 != Node->getOperand(4)) {
1052 std::vector<SDOperand> Ops;
1053 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1054 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1055 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
1056 }
Chris Lattner3c0dd462005-01-16 07:29:19 +00001057 break;
1058 case TargetLowering::Expand: {
Chris Lattner85d70c62005-01-11 05:57:22 +00001059 // Otherwise, the target does not support this operation. Lower the
1060 // operation to an explicit libcall as appropriate.
1061 MVT::ValueType IntPtr = TLI.getPointerTy();
1062 const Type *IntPtrTy = TLI.getTargetData().getIntPtrType();
1063 std::vector<std::pair<SDOperand, const Type*> > Args;
1064
Reid Spencer6dced922005-01-12 14:53:45 +00001065 const char *FnName = 0;
Chris Lattner85d70c62005-01-11 05:57:22 +00001066 if (Node->getOpcode() == ISD::MEMSET) {
1067 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1068 // Extend the ubyte argument to be an int value for the call.
1069 Tmp3 = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Tmp3);
1070 Args.push_back(std::make_pair(Tmp3, Type::IntTy));
1071 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1072
1073 FnName = "memset";
1074 } else if (Node->getOpcode() == ISD::MEMCPY ||
1075 Node->getOpcode() == ISD::MEMMOVE) {
1076 Args.push_back(std::make_pair(Tmp2, IntPtrTy));
1077 Args.push_back(std::make_pair(Tmp3, IntPtrTy));
1078 Args.push_back(std::make_pair(Tmp4, IntPtrTy));
1079 FnName = Node->getOpcode() == ISD::MEMMOVE ? "memmove" : "memcpy";
1080 } else {
1081 assert(0 && "Unknown op!");
1082 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001083
Chris Lattner85d70c62005-01-11 05:57:22 +00001084 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001085 TLI.LowerCallTo(Tmp1, Type::VoidTy, false, CallingConv::C, false,
Chris Lattner85d70c62005-01-11 05:57:22 +00001086 DAG.getExternalSymbol(FnName, IntPtr), Args, DAG);
Chris Lattnerf9ddfef2005-07-13 02:00:04 +00001087 Result = CallResult.second;
1088 NeedsAnotherIteration = true;
Chris Lattner3c0dd462005-01-16 07:29:19 +00001089 break;
1090 }
1091 case TargetLowering::Custom:
1092 std::vector<SDOperand> Ops;
1093 Ops.push_back(Tmp1); Ops.push_back(Tmp2); Ops.push_back(Tmp3);
1094 Ops.push_back(Tmp4); Ops.push_back(Tmp5);
1095 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Ops);
Chris Lattner29dcc712005-05-14 05:50:48 +00001096 Result = TLI.LowerOperation(Result, DAG);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001097 Result = LegalizeOp(Result);
1098 break;
Chris Lattner85d70c62005-01-11 05:57:22 +00001099 }
1100 break;
1101 }
Chris Lattner5385db52005-05-09 20:23:03 +00001102
1103 case ISD::READPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001104 Tmp1 = LegalizeOp(Node->getOperand(0));
1105 Tmp2 = LegalizeOp(Node->getOperand(1));
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001106
Chris Lattner86535992005-05-14 07:45:46 +00001107 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1108 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1109 std::vector<SDOperand> Ops;
1110 Ops.push_back(Tmp1);
1111 Ops.push_back(Tmp2);
1112 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1113 } else
Chris Lattner5385db52005-05-09 20:23:03 +00001114 Result = SDOperand(Node, 0);
1115 // Since these produce two values, make sure to remember that we legalized
1116 // both of them.
1117 AddLegalizedOperand(SDOperand(Node, 0), Result);
1118 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1119 return Result.getValue(Op.ResNo);
Chris Lattner5385db52005-05-09 20:23:03 +00001120 case ISD::WRITEPORT:
Chris Lattner5385db52005-05-09 20:23:03 +00001121 Tmp1 = LegalizeOp(Node->getOperand(0));
1122 Tmp2 = LegalizeOp(Node->getOperand(1));
1123 Tmp3 = LegalizeOp(Node->getOperand(2));
1124 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1125 Tmp3 != Node->getOperand(2))
1126 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1127 break;
1128
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001129 case ISD::READIO:
1130 Tmp1 = LegalizeOp(Node->getOperand(0));
1131 Tmp2 = LegalizeOp(Node->getOperand(1));
1132
1133 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1134 case TargetLowering::Custom:
1135 default: assert(0 && "This action not implemented for this operation!");
1136 case TargetLowering::Legal:
Chris Lattner86535992005-05-14 07:45:46 +00001137 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1)) {
1138 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1139 std::vector<SDOperand> Ops;
1140 Ops.push_back(Tmp1);
1141 Ops.push_back(Tmp2);
1142 Result = DAG.getNode(ISD::READPORT, VTs, Ops);
1143 } else
Chris Lattnerba45e6c2005-05-09 20:36:57 +00001144 Result = SDOperand(Node, 0);
1145 break;
1146 case TargetLowering::Expand:
1147 // Replace this with a load from memory.
1148 Result = DAG.getLoad(Node->getValueType(0), Node->getOperand(0),
1149 Node->getOperand(1), DAG.getSrcValue(NULL));
1150 Result = LegalizeOp(Result);
1151 break;
1152 }
1153
1154 // Since these produce two values, make sure to remember that we legalized
1155 // both of them.
1156 AddLegalizedOperand(SDOperand(Node, 0), Result);
1157 AddLegalizedOperand(SDOperand(Node, 1), Result.getValue(1));
1158 return Result.getValue(Op.ResNo);
1159
1160 case ISD::WRITEIO:
1161 Tmp1 = LegalizeOp(Node->getOperand(0));
1162 Tmp2 = LegalizeOp(Node->getOperand(1));
1163 Tmp3 = LegalizeOp(Node->getOperand(2));
1164
1165 switch (TLI.getOperationAction(Node->getOpcode(),
1166 Node->getOperand(1).getValueType())) {
1167 case TargetLowering::Custom:
1168 default: assert(0 && "This action not implemented for this operation!");
1169 case TargetLowering::Legal:
1170 if (Tmp1 != Node->getOperand(0) || Tmp2 != Node->getOperand(1) ||
1171 Tmp3 != Node->getOperand(2))
1172 Result = DAG.getNode(Node->getOpcode(), MVT::Other, Tmp1, Tmp2, Tmp3);
1173 break;
1174 case TargetLowering::Expand:
1175 // Replace this with a store to memory.
1176 Result = DAG.getNode(ISD::STORE, MVT::Other, Node->getOperand(0),
1177 Node->getOperand(1), Node->getOperand(2),
1178 DAG.getSrcValue(NULL));
1179 Result = LegalizeOp(Result);
1180 break;
1181 }
1182 break;
1183
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001184 case ISD::ADD_PARTS:
Chris Lattner4157c412005-04-02 04:00:59 +00001185 case ISD::SUB_PARTS:
1186 case ISD::SHL_PARTS:
1187 case ISD::SRA_PARTS:
1188 case ISD::SRL_PARTS: {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001189 std::vector<SDOperand> Ops;
1190 bool Changed = false;
1191 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
1192 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1193 Changed |= Ops.back() != Node->getOperand(i);
1194 }
Chris Lattner669e8c22005-05-14 07:25:05 +00001195 if (Changed) {
1196 std::vector<MVT::ValueType> VTs(Node->value_begin(), Node->value_end());
1197 Result = DAG.getNode(Node->getOpcode(), VTs, Ops);
1198 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001199
1200 // Since these produce multiple values, make sure to remember that we
1201 // legalized all of them.
1202 for (unsigned i = 0, e = Node->getNumValues(); i != e; ++i)
1203 AddLegalizedOperand(SDOperand(Node, i), Result.getValue(i));
1204 return Result.getValue(Op.ResNo);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001205 }
Chris Lattner13fe99c2005-04-02 05:00:07 +00001206
1207 // Binary operators
Chris Lattnerdc750592005-01-07 07:47:09 +00001208 case ISD::ADD:
1209 case ISD::SUB:
1210 case ISD::MUL:
Nate Begemanadd0c632005-04-11 03:01:51 +00001211 case ISD::MULHS:
1212 case ISD::MULHU:
Chris Lattnerdc750592005-01-07 07:47:09 +00001213 case ISD::UDIV:
1214 case ISD::SDIV:
Chris Lattnerdc750592005-01-07 07:47:09 +00001215 case ISD::AND:
1216 case ISD::OR:
1217 case ISD::XOR:
Chris Lattner32f20bf2005-01-07 21:45:56 +00001218 case ISD::SHL:
1219 case ISD::SRL:
1220 case ISD::SRA:
Chris Lattnerdc750592005-01-07 07:47:09 +00001221 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
Andrew Lenharth80fe4112005-07-05 19:52:39 +00001222 switch (getTypeAction(Node->getOperand(1).getValueType())) {
1223 case Expand: assert(0 && "Not possible");
1224 case Legal:
1225 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the RHS.
1226 break;
1227 case Promote:
1228 Tmp2 = PromoteOp(Node->getOperand(1)); // Promote the RHS.
1229 break;
1230 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001231 if (Tmp1 != Node->getOperand(0) ||
1232 Tmp2 != Node->getOperand(1))
1233 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,Tmp2);
1234 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001235
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001236 case ISD::UREM:
1237 case ISD::SREM:
1238 Tmp1 = LegalizeOp(Node->getOperand(0)); // LHS
1239 Tmp2 = LegalizeOp(Node->getOperand(1)); // RHS
1240 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1241 case TargetLowering::Legal:
1242 if (Tmp1 != Node->getOperand(0) ||
1243 Tmp2 != Node->getOperand(1))
Misha Brukman835702a2005-04-21 22:36:52 +00001244 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Nate Begeman20b7d2a2005-04-06 00:23:54 +00001245 Tmp2);
1246 break;
1247 case TargetLowering::Promote:
1248 case TargetLowering::Custom:
1249 assert(0 && "Cannot promote/custom handle this yet!");
1250 case TargetLowering::Expand: {
1251 MVT::ValueType VT = Node->getValueType(0);
1252 unsigned Opc = (Node->getOpcode() == ISD::UREM) ? ISD::UDIV : ISD::SDIV;
1253 Result = DAG.getNode(Opc, VT, Tmp1, Tmp2);
1254 Result = DAG.getNode(ISD::MUL, VT, Result, Tmp2);
1255 Result = DAG.getNode(ISD::SUB, VT, Tmp1, Result);
1256 }
1257 break;
1258 }
1259 break;
Chris Lattner13fe99c2005-04-02 05:00:07 +00001260
Andrew Lenharth5e177822005-05-03 17:19:30 +00001261 case ISD::CTPOP:
1262 case ISD::CTTZ:
1263 case ISD::CTLZ:
1264 Tmp1 = LegalizeOp(Node->getOperand(0)); // Op
1265 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1266 case TargetLowering::Legal:
1267 if (Tmp1 != Node->getOperand(0))
1268 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1269 break;
1270 case TargetLowering::Promote: {
1271 MVT::ValueType OVT = Tmp1.getValueType();
1272 MVT::ValueType NVT = TLI.getTypeToPromoteTo(Node->getOpcode(), OVT);
Chris Lattner55e9cde2005-05-11 04:51:16 +00001273
1274 // Zero extend the argument.
Andrew Lenharth5e177822005-05-03 17:19:30 +00001275 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1276 // Perform the larger operation, then subtract if needed.
1277 Tmp1 = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1278 switch(Node->getOpcode())
1279 {
1280 case ISD::CTPOP:
1281 Result = Tmp1;
1282 break;
1283 case ISD::CTTZ:
1284 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001285 Tmp2 = DAG.getSetCC(ISD::SETEQ, TLI.getSetCCResultTy(), Tmp1,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001286 DAG.getConstant(getSizeInBits(NVT), NVT));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001287 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharth5e177822005-05-03 17:19:30 +00001288 DAG.getConstant(getSizeInBits(OVT),NVT), Tmp1);
1289 break;
1290 case ISD::CTLZ:
1291 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001292 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1293 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharth5e177822005-05-03 17:19:30 +00001294 getSizeInBits(OVT), NVT));
1295 break;
1296 }
1297 break;
1298 }
1299 case TargetLowering::Custom:
1300 assert(0 && "Cannot custom handle this yet!");
1301 case TargetLowering::Expand:
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001302 switch(Node->getOpcode())
1303 {
1304 case ISD::CTPOP: {
Chris Lattner05309bf52005-05-11 05:21:31 +00001305 static const uint64_t mask[6] = {
1306 0x5555555555555555ULL, 0x3333333333333333ULL,
1307 0x0F0F0F0F0F0F0F0FULL, 0x00FF00FF00FF00FFULL,
1308 0x0000FFFF0000FFFFULL, 0x00000000FFFFFFFFULL
1309 };
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001310 MVT::ValueType VT = Tmp1.getValueType();
Chris Lattner05309bf52005-05-11 05:21:31 +00001311 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1312 unsigned len = getSizeInBits(VT);
1313 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001314 //x = (x & mask[i][len/8]) + (x >> (1 << i) & mask[i][len/8])
Chris Lattner05309bf52005-05-11 05:21:31 +00001315 Tmp2 = DAG.getConstant(mask[i], VT);
1316 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001317 Tmp1 = DAG.getNode(ISD::ADD, VT,
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001318 DAG.getNode(ISD::AND, VT, Tmp1, Tmp2),
1319 DAG.getNode(ISD::AND, VT,
1320 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3),
1321 Tmp2));
1322 }
1323 Result = Tmp1;
1324 break;
1325 }
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001326 case ISD::CTLZ: {
1327 /* for now, we do this:
Chris Lattner56add052005-05-11 18:35:21 +00001328 x = x | (x >> 1);
1329 x = x | (x >> 2);
1330 ...
1331 x = x | (x >>16);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001332 x = x | (x >>32); // for 64-bit input
Chris Lattner56add052005-05-11 18:35:21 +00001333 return popcount(~x);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001334
Chris Lattner56add052005-05-11 18:35:21 +00001335 but see also: http://www.hackersdelight.org/HDcode/nlz.cc */
1336 MVT::ValueType VT = Tmp1.getValueType();
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001337 MVT::ValueType ShVT = TLI.getShiftAmountTy();
1338 unsigned len = getSizeInBits(VT);
1339 for (unsigned i = 0; (1U << i) <= (len / 2); ++i) {
1340 Tmp3 = DAG.getConstant(1ULL << i, ShVT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001341 Tmp1 = DAG.getNode(ISD::OR, VT, Tmp1,
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001342 DAG.getNode(ISD::SRL, VT, Tmp1, Tmp3));
1343 }
1344 Tmp3 = DAG.getNode(ISD::XOR, VT, Tmp1, DAG.getConstant(~0ULL, VT));
Chris Lattner56add052005-05-11 18:35:21 +00001345 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
Chris Lattner72473242005-05-11 05:27:09 +00001346 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001347 }
1348 case ISD::CTTZ: {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001349 // for now, we use: { return popcount(~x & (x - 1)); }
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001350 // unless the target has ctlz but not ctpop, in which case we use:
1351 // { return 32 - nlz(~x & (x-1)); }
1352 // see also http://www.hackersdelight.org/HDcode/ntz.cc
Chris Lattner56add052005-05-11 18:35:21 +00001353 MVT::ValueType VT = Tmp1.getValueType();
1354 Tmp2 = DAG.getConstant(~0ULL, VT);
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001355 Tmp3 = DAG.getNode(ISD::AND, VT,
Chris Lattner56add052005-05-11 18:35:21 +00001356 DAG.getNode(ISD::XOR, VT, Tmp1, Tmp2),
1357 DAG.getNode(ISD::SUB, VT, Tmp1,
1358 DAG.getConstant(1, VT)));
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001359 // If ISD::CTLZ is legal and CTPOP isn't, then do that instead
1360 if (TLI.getOperationAction(ISD::CTPOP, VT) != TargetLowering::Legal &&
1361 TLI.getOperationAction(ISD::CTLZ, VT) == TargetLowering::Legal) {
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001362 Result = LegalizeOp(DAG.getNode(ISD::SUB, VT,
Nate Begeman99fa5bc2005-05-11 23:43:56 +00001363 DAG.getConstant(getSizeInBits(VT), VT),
1364 DAG.getNode(ISD::CTLZ, VT, Tmp3)));
1365 } else {
1366 Result = LegalizeOp(DAG.getNode(ISD::CTPOP, VT, Tmp3));
1367 }
Chris Lattner72473242005-05-11 05:27:09 +00001368 break;
Duraid Madinaa1ebbac2005-05-11 08:45:08 +00001369 }
Andrew Lenharth2dbbb3a2005-05-05 15:55:21 +00001370 default:
1371 assert(0 && "Cannot expand this yet!");
1372 break;
1373 }
Andrew Lenharth5e177822005-05-03 17:19:30 +00001374 break;
1375 }
1376 break;
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001377
Chris Lattner13fe99c2005-04-02 05:00:07 +00001378 // Unary operators
1379 case ISD::FABS:
1380 case ISD::FNEG:
Chris Lattner9d6fa982005-04-28 21:44:33 +00001381 case ISD::FSQRT:
1382 case ISD::FSIN:
1383 case ISD::FCOS:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001384 Tmp1 = LegalizeOp(Node->getOperand(0));
1385 switch (TLI.getOperationAction(Node->getOpcode(), Node->getValueType(0))) {
1386 case TargetLowering::Legal:
1387 if (Tmp1 != Node->getOperand(0))
1388 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1389 break;
1390 case TargetLowering::Promote:
1391 case TargetLowering::Custom:
1392 assert(0 && "Cannot promote/custom handle this yet!");
1393 case TargetLowering::Expand:
Chris Lattner80026402005-04-30 04:43:14 +00001394 switch(Node->getOpcode()) {
1395 case ISD::FNEG: {
Chris Lattner13fe99c2005-04-02 05:00:07 +00001396 // Expand Y = FNEG(X) -> Y = SUB -0.0, X
1397 Tmp2 = DAG.getConstantFP(-0.0, Node->getValueType(0));
1398 Result = LegalizeOp(DAG.getNode(ISD::SUB, Node->getValueType(0),
1399 Tmp2, Tmp1));
Chris Lattner80026402005-04-30 04:43:14 +00001400 break;
1401 }
1402 case ISD::FABS: {
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001403 // Expand Y = FABS(X) -> Y = (X >u 0.0) ? X : fneg(X).
1404 MVT::ValueType VT = Node->getValueType(0);
1405 Tmp2 = DAG.getConstantFP(0.0, VT);
1406 Tmp2 = DAG.getSetCC(ISD::SETUGT, TLI.getSetCCResultTy(), Tmp1, Tmp2);
1407 Tmp3 = DAG.getNode(ISD::FNEG, VT, Tmp1);
1408 Result = DAG.getNode(ISD::SELECT, VT, Tmp2, Tmp1, Tmp3);
1409 Result = LegalizeOp(Result);
Chris Lattner80026402005-04-30 04:43:14 +00001410 break;
1411 }
1412 case ISD::FSQRT:
1413 case ISD::FSIN:
1414 case ISD::FCOS: {
1415 MVT::ValueType VT = Node->getValueType(0);
1416 Type *T = VT == MVT::f32 ? Type::FloatTy : Type::DoubleTy;
1417 const char *FnName = 0;
1418 switch(Node->getOpcode()) {
1419 case ISD::FSQRT: FnName = VT == MVT::f32 ? "sqrtf" : "sqrt"; break;
1420 case ISD::FSIN: FnName = VT == MVT::f32 ? "sinf" : "sin"; break;
1421 case ISD::FCOS: FnName = VT == MVT::f32 ? "cosf" : "cos"; break;
1422 default: assert(0 && "Unreachable!");
1423 }
1424 std::vector<std::pair<SDOperand, const Type*> > Args;
1425 Args.push_back(std::make_pair(Tmp1, T));
Chris Lattner06bbeb62005-05-11 19:02:11 +00001426 // FIXME: should use ExpandLibCall!
Chris Lattner80026402005-04-30 04:43:14 +00001427 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00001428 TLI.LowerCallTo(DAG.getEntryNode(), T, false, CallingConv::C, true,
Chris Lattner80026402005-04-30 04:43:14 +00001429 DAG.getExternalSymbol(FnName, VT), Args, DAG);
1430 Result = LegalizeOp(CallResult.first);
1431 break;
1432 }
1433 default:
Chris Lattnera0c72cf2005-04-02 05:26:37 +00001434 assert(0 && "Unreachable!");
Chris Lattner13fe99c2005-04-02 05:00:07 +00001435 }
1436 break;
1437 }
1438 break;
1439
1440 // Conversion operators. The source and destination have different types.
Chris Lattner2a6db3c2005-01-08 08:08:56 +00001441 case ISD::SINT_TO_FP:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001442 case ISD::UINT_TO_FP: {
1443 bool isSigned = Node->getOpcode() == ISD::SINT_TO_FP;
Chris Lattnerdc750592005-01-07 07:47:09 +00001444 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1445 case Legal:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001446 switch (TLI.getOperationAction(Node->getOpcode(),
1447 Node->getOperand(0).getValueType())) {
1448 default: assert(0 && "Unknown operation action!");
1449 case TargetLowering::Expand:
1450 assert(!isSigned && "Legalize cannot Expand SINT_TO_FP yet");
1451 Result = ExpandLegalUINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1452 Node->getValueType(0));
1453 AddLegalizedOperand(Op, Result);
1454 return Result;
1455 case TargetLowering::Promote:
1456 Result = PromoteLegalINT_TO_FP(LegalizeOp(Node->getOperand(0)),
1457 Node->getValueType(0),
1458 isSigned);
1459 AddLegalizedOperand(Op, Result);
1460 return Result;
1461 case TargetLowering::Legal:
1462 break;
Andrew Lenharthd74877a2005-06-27 23:28:32 +00001463 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001464
Chris Lattnerdc750592005-01-07 07:47:09 +00001465 Tmp1 = LegalizeOp(Node->getOperand(0));
1466 if (Tmp1 != Node->getOperand(0))
1467 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1468 break;
Chris Lattnera65a2f02005-01-07 22:37:48 +00001469 case Expand:
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001470 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP,
1471 Node->getValueType(0), Node->getOperand(0));
1472 break;
1473 case Promote:
1474 if (isSigned) {
1475 Result = PromoteOp(Node->getOperand(0));
1476 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
1477 Result, DAG.getValueType(Node->getOperand(0).getValueType()));
1478 Result = DAG.getNode(ISD::SINT_TO_FP, Op.getValueType(), Result);
1479 } else {
1480 Result = PromoteOp(Node->getOperand(0));
1481 Result = DAG.getZeroExtendInReg(Result,
1482 Node->getOperand(0).getValueType());
1483 Result = DAG.getNode(ISD::UINT_TO_FP, Op.getValueType(), Result);
Chris Lattneraac464e2005-01-21 06:05:23 +00001484 }
Chris Lattnerf99f8f92005-07-28 23:31:12 +00001485 break;
1486 }
1487 break;
1488 }
1489 case ISD::TRUNCATE:
1490 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1491 case Legal:
1492 Tmp1 = LegalizeOp(Node->getOperand(0));
1493 if (Tmp1 != Node->getOperand(0))
1494 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1495 break;
1496 case Expand:
1497 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1498
1499 // Since the result is legal, we should just be able to truncate the low
1500 // part of the source.
1501 Result = DAG.getNode(ISD::TRUNCATE, Node->getValueType(0), Tmp1);
1502 break;
1503 case Promote:
1504 Result = PromoteOp(Node->getOperand(0));
1505 Result = DAG.getNode(ISD::TRUNCATE, Op.getValueType(), Result);
1506 break;
1507 }
1508 break;
1509
1510 case ISD::FP_TO_SINT:
1511 case ISD::FP_TO_UINT:
1512 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1513 case Legal:
1514 Tmp1 = LegalizeOp(Node->getOperand(0));
1515 if (Tmp1 != Node->getOperand(0))
1516 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1517 break;
1518 case Expand:
1519 assert(0 && "Shouldn't need to expand other operators here!");
1520 case Promote:
1521 Result = PromoteOp(Node->getOperand(0));
1522 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1523 break;
1524 }
1525 break;
1526
1527 case ISD::ZERO_EXTEND:
1528 case ISD::SIGN_EXTEND:
1529 case ISD::FP_EXTEND:
1530 case ISD::FP_ROUND:
1531 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1532 case Legal:
1533 Tmp1 = LegalizeOp(Node->getOperand(0));
1534 if (Tmp1 != Node->getOperand(0))
1535 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1);
1536 break;
1537 case Expand:
Chris Lattner13fe99c2005-04-02 05:00:07 +00001538 assert(0 && "Shouldn't need to expand other operators here!");
Chris Lattnera65a2f02005-01-07 22:37:48 +00001539
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001540 case Promote:
1541 switch (Node->getOpcode()) {
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001542 case ISD::ZERO_EXTEND:
1543 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001544 // NOTE: Any extend would work here...
1545 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner0e852af2005-04-13 02:38:47 +00001546 Result = DAG.getZeroExtendInReg(Result,
1547 Node->getOperand(0).getValueType());
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001548 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001549 case ISD::SIGN_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001550 Result = PromoteOp(Node->getOperand(0));
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001551 // NOTE: Any extend would work here...
Chris Lattner42993e42005-01-18 21:57:59 +00001552 Result = DAG.getNode(ISD::ZERO_EXTEND, Op.getValueType(), Result);
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001553 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001554 Result,
1555 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001556 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001557 case ISD::FP_EXTEND:
Chris Lattner71d7f6e2005-01-16 00:38:00 +00001558 Result = PromoteOp(Node->getOperand(0));
1559 if (Result.getValueType() != Op.getValueType())
1560 // Dynamically dead while we have only 2 FP types.
1561 Result = DAG.getNode(ISD::FP_EXTEND, Op.getValueType(), Result);
1562 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001563 case ISD::FP_ROUND:
Chris Lattner3ba56b32005-01-16 05:06:12 +00001564 Result = PromoteOp(Node->getOperand(0));
1565 Result = DAG.getNode(Node->getOpcode(), Op.getValueType(), Result);
1566 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001567 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001568 }
1569 break;
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001570 case ISD::FP_ROUND_INREG:
Chris Lattner0e852af2005-04-13 02:38:47 +00001571 case ISD::SIGN_EXTEND_INREG: {
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001572 Tmp1 = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001573 MVT::ValueType ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
Chris Lattner99222f72005-01-15 07:15:18 +00001574
1575 // If this operation is not supported, convert it to a shl/shr or load/store
1576 // pair.
Chris Lattner3c0dd462005-01-16 07:29:19 +00001577 switch (TLI.getOperationAction(Node->getOpcode(), ExtraVT)) {
1578 default: assert(0 && "This action not supported for this op yet!");
1579 case TargetLowering::Legal:
1580 if (Tmp1 != Node->getOperand(0))
1581 Result = DAG.getNode(Node->getOpcode(), Node->getValueType(0), Tmp1,
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001582 DAG.getValueType(ExtraVT));
Chris Lattner3c0dd462005-01-16 07:29:19 +00001583 break;
1584 case TargetLowering::Expand:
Chris Lattner99222f72005-01-15 07:15:18 +00001585 // If this is an integer extend and shifts are supported, do that.
Chris Lattner0e852af2005-04-13 02:38:47 +00001586 if (Node->getOpcode() == ISD::SIGN_EXTEND_INREG) {
Chris Lattner99222f72005-01-15 07:15:18 +00001587 // NOTE: we could fall back on load/store here too for targets without
1588 // SAR. However, it is doubtful that any exist.
1589 unsigned BitsDiff = MVT::getSizeInBits(Node->getValueType(0)) -
1590 MVT::getSizeInBits(ExtraVT);
Chris Lattnerec218372005-01-22 00:31:52 +00001591 SDOperand ShiftCst = DAG.getConstant(BitsDiff, TLI.getShiftAmountTy());
Chris Lattner99222f72005-01-15 07:15:18 +00001592 Result = DAG.getNode(ISD::SHL, Node->getValueType(0),
1593 Node->getOperand(0), ShiftCst);
1594 Result = DAG.getNode(ISD::SRA, Node->getValueType(0),
1595 Result, ShiftCst);
1596 } else if (Node->getOpcode() == ISD::FP_ROUND_INREG) {
1597 // The only way we can lower this is to turn it into a STORETRUNC,
1598 // EXTLOAD pair, targetting a temporary location (a stack slot).
1599
1600 // NOTE: there is a choice here between constantly creating new stack
1601 // slots and always reusing the same one. We currently always create
1602 // new ones, as reuse may inhibit scheduling.
1603 const Type *Ty = MVT::getTypeForValueType(ExtraVT);
1604 unsigned TySize = (unsigned)TLI.getTargetData().getTypeSize(Ty);
1605 unsigned Align = TLI.getTargetData().getTypeAlignment(Ty);
1606 MachineFunction &MF = DAG.getMachineFunction();
Misha Brukman835702a2005-04-21 22:36:52 +00001607 int SSFI =
Chris Lattner99222f72005-01-15 07:15:18 +00001608 MF.getFrameInfo()->CreateStackObject((unsigned)TySize, Align);
1609 SDOperand StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
1610 Result = DAG.getNode(ISD::TRUNCSTORE, MVT::Other, DAG.getEntryNode(),
Chris Lattner5385db52005-05-09 20:23:03 +00001611 Node->getOperand(0), StackSlot,
Chris Lattner36db1ed2005-07-10 00:29:18 +00001612 DAG.getSrcValue(NULL), DAG.getValueType(ExtraVT));
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001613 Result = DAG.getExtLoad(ISD::EXTLOAD, Node->getValueType(0),
1614 Result, StackSlot, DAG.getSrcValue(NULL),
1615 ExtraVT);
Chris Lattner99222f72005-01-15 07:15:18 +00001616 } else {
1617 assert(0 && "Unknown op");
1618 }
1619 Result = LegalizeOp(Result);
Chris Lattner3c0dd462005-01-16 07:29:19 +00001620 break;
Chris Lattner99222f72005-01-15 07:15:18 +00001621 }
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001622 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00001623 }
Chris Lattner99222f72005-01-15 07:15:18 +00001624 }
Chris Lattnerdc750592005-01-07 07:47:09 +00001625
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001626 // Note that LegalizeOp may be reentered even from single-use nodes, which
1627 // means that we always must cache transformed nodes.
1628 AddLegalizedOperand(Op, Result);
Chris Lattnerdc750592005-01-07 07:47:09 +00001629 return Result;
1630}
1631
Chris Lattner4d978642005-01-15 22:16:26 +00001632/// PromoteOp - Given an operation that produces a value in an invalid type,
1633/// promote it to compute the value into a larger type. The produced value will
1634/// have the correct bits for the low portion of the register, but no guarantee
1635/// is made about the top bits: it may be zero, sign-extended, or garbage.
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001636SDOperand SelectionDAGLegalize::PromoteOp(SDOperand Op) {
1637 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00001638 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001639 assert(getTypeAction(VT) == Promote &&
1640 "Caller should expand or legalize operands that are not promotable!");
1641 assert(NVT > VT && MVT::isInteger(NVT) == MVT::isInteger(VT) &&
1642 "Cannot promote to smaller type!");
1643
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001644 SDOperand Tmp1, Tmp2, Tmp3;
1645
1646 SDOperand Result;
1647 SDNode *Node = Op.Val;
1648
Chris Lattnerb5a78e02005-05-12 16:53:42 +00001649 if (!Node->hasOneUse()) {
1650 std::map<SDOperand, SDOperand>::iterator I = PromotedNodes.find(Op);
1651 if (I != PromotedNodes.end()) return I->second;
1652 } else {
1653 assert(!PromotedNodes.count(Op) && "Repromoted this node??");
1654 }
1655
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001656 // Promotion needs an optimization step to clean up after it, and is not
1657 // careful to avoid operations the target does not support. Make sure that
1658 // all generated operations are legalized in the next iteration.
1659 NeedsAnotherIteration = true;
1660
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001661 switch (Node->getOpcode()) {
1662 default:
1663 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
1664 assert(0 && "Do not know how to promote this operator!");
1665 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00001666 case ISD::UNDEF:
1667 Result = DAG.getNode(ISD::UNDEF, NVT);
1668 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001669 case ISD::Constant:
1670 Result = DAG.getNode(ISD::ZERO_EXTEND, NVT, Op);
1671 assert(isa<ConstantSDNode>(Result) && "Didn't constant fold zext?");
1672 break;
1673 case ISD::ConstantFP:
1674 Result = DAG.getNode(ISD::FP_EXTEND, NVT, Op);
1675 assert(isa<ConstantFPSDNode>(Result) && "Didn't constant fold fp_extend?");
1676 break;
Chris Lattner9f2c4a52005-01-18 17:54:55 +00001677 case ISD::CopyFromReg:
1678 Result = DAG.getCopyFromReg(cast<RegSDNode>(Node)->getReg(), NVT,
1679 Node->getOperand(0));
1680 // Remember that we legalized the chain.
1681 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1682 break;
1683
Chris Lattner2cb338d2005-01-18 02:59:52 +00001684 case ISD::SETCC:
1685 assert(getTypeAction(TLI.getSetCCResultTy()) == Legal &&
1686 "SetCC type is not legal??");
1687 Result = DAG.getSetCC(cast<SetCCSDNode>(Node)->getCondition(),
1688 TLI.getSetCCResultTy(), Node->getOperand(0),
1689 Node->getOperand(1));
1690 Result = LegalizeOp(Result);
1691 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001692
1693 case ISD::TRUNCATE:
1694 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1695 case Legal:
1696 Result = LegalizeOp(Node->getOperand(0));
1697 assert(Result.getValueType() >= NVT &&
1698 "This truncation doesn't make sense!");
1699 if (Result.getValueType() > NVT) // Truncate to NVT instead of VT
1700 Result = DAG.getNode(ISD::TRUNCATE, NVT, Result);
1701 break;
Chris Lattnerbf8c1ad2005-01-28 22:52:50 +00001702 case Promote:
1703 // The truncation is not required, because we don't guarantee anything
1704 // about high bits anyway.
1705 Result = PromoteOp(Node->getOperand(0));
1706 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001707 case Expand:
Nate Begemancc00a7c2005-04-04 00:57:08 +00001708 ExpandOp(Node->getOperand(0), Tmp1, Tmp2);
1709 // Truncate the low part of the expanded value to the result type
Misha Brukman835702a2005-04-21 22:36:52 +00001710 Result = DAG.getNode(ISD::TRUNCATE, VT, Tmp1);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001711 }
1712 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001713 case ISD::SIGN_EXTEND:
1714 case ISD::ZERO_EXTEND:
1715 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1716 case Expand: assert(0 && "BUG: Smaller reg should have been promoted!");
1717 case Legal:
1718 // Input is legal? Just do extend all the way to the larger type.
1719 Result = LegalizeOp(Node->getOperand(0));
1720 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
1721 break;
1722 case Promote:
1723 // Promote the reg if it's smaller.
1724 Result = PromoteOp(Node->getOperand(0));
1725 // The high bits are not guaranteed to be anything. Insert an extend.
1726 if (Node->getOpcode() == ISD::SIGN_EXTEND)
Chris Lattner05596912005-02-04 18:39:19 +00001727 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Result,
Chris Lattner0b6ba902005-07-10 00:07:11 +00001728 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00001729 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001730 Result = DAG.getZeroExtendInReg(Result,
1731 Node->getOperand(0).getValueType());
Chris Lattner4d978642005-01-15 22:16:26 +00001732 break;
1733 }
1734 break;
1735
1736 case ISD::FP_EXTEND:
1737 assert(0 && "Case not implemented. Dynamically dead with 2 FP types!");
1738 case ISD::FP_ROUND:
1739 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1740 case Expand: assert(0 && "BUG: Cannot expand FP regs!");
1741 case Promote: assert(0 && "Unreachable with 2 FP types!");
1742 case Legal:
1743 // Input is legal? Do an FP_ROUND_INREG.
1744 Result = LegalizeOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001745 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1746 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001747 break;
1748 }
1749 break;
1750
1751 case ISD::SINT_TO_FP:
1752 case ISD::UINT_TO_FP:
1753 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1754 case Legal:
1755 Result = LegalizeOp(Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001756 // No extra round required here.
1757 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001758 break;
1759
1760 case Promote:
1761 Result = PromoteOp(Node->getOperand(0));
1762 if (Node->getOpcode() == ISD::SINT_TO_FP)
1763 Result = DAG.getNode(ISD::SIGN_EXTEND_INREG, Result.getValueType(),
Chris Lattner0b6ba902005-07-10 00:07:11 +00001764 Result,
1765 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner4d978642005-01-15 22:16:26 +00001766 else
Chris Lattner0e852af2005-04-13 02:38:47 +00001767 Result = DAG.getZeroExtendInReg(Result,
1768 Node->getOperand(0).getValueType());
Chris Lattneraac464e2005-01-21 06:05:23 +00001769 // No extra round required here.
1770 Result = DAG.getNode(Node->getOpcode(), NVT, Result);
Chris Lattner4d978642005-01-15 22:16:26 +00001771 break;
1772 case Expand:
Chris Lattneraac464e2005-01-21 06:05:23 +00001773 Result = ExpandIntToFP(Node->getOpcode() == ISD::SINT_TO_FP, NVT,
1774 Node->getOperand(0));
Chris Lattneraac464e2005-01-21 06:05:23 +00001775 // Round if we cannot tolerate excess precision.
1776 if (NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001777 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1778 DAG.getValueType(VT));
Chris Lattneraac464e2005-01-21 06:05:23 +00001779 break;
Chris Lattner4d978642005-01-15 22:16:26 +00001780 }
Chris Lattner4d978642005-01-15 22:16:26 +00001781 break;
1782
1783 case ISD::FP_TO_SINT:
1784 case ISD::FP_TO_UINT:
1785 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1786 case Legal:
1787 Tmp1 = LegalizeOp(Node->getOperand(0));
1788 break;
1789 case Promote:
1790 // The input result is prerounded, so we don't have to do anything
1791 // special.
1792 Tmp1 = PromoteOp(Node->getOperand(0));
1793 break;
1794 case Expand:
1795 assert(0 && "not implemented");
1796 }
1797 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1798 break;
1799
Chris Lattner13fe99c2005-04-02 05:00:07 +00001800 case ISD::FABS:
1801 case ISD::FNEG:
1802 Tmp1 = PromoteOp(Node->getOperand(0));
1803 assert(Tmp1.getValueType() == NVT);
1804 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1805 // NOTE: we do not have to do any extra rounding here for
1806 // NoExcessFPPrecision, because we know the input will have the appropriate
1807 // precision, and these operations don't modify precision at all.
1808 break;
1809
Chris Lattner9d6fa982005-04-28 21:44:33 +00001810 case ISD::FSQRT:
1811 case ISD::FSIN:
1812 case ISD::FCOS:
1813 Tmp1 = PromoteOp(Node->getOperand(0));
1814 assert(Tmp1.getValueType() == NVT);
1815 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1816 if(NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001817 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1818 DAG.getValueType(VT));
Chris Lattner9d6fa982005-04-28 21:44:33 +00001819 break;
1820
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001821 case ISD::AND:
1822 case ISD::OR:
1823 case ISD::XOR:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001824 case ISD::ADD:
Chris Lattner4d978642005-01-15 22:16:26 +00001825 case ISD::SUB:
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001826 case ISD::MUL:
1827 // The input may have strange things in the top bits of the registers, but
1828 // these operations don't care. They may have wierd bits going out, but
1829 // that too is okay if they are integer operations.
1830 Tmp1 = PromoteOp(Node->getOperand(0));
1831 Tmp2 = PromoteOp(Node->getOperand(1));
1832 assert(Tmp1.getValueType() == NVT && Tmp2.getValueType() == NVT);
1833 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1834
1835 // However, if this is a floating point operation, they will give excess
1836 // precision that we may not be able to tolerate. If we DO allow excess
1837 // precision, just leave it, otherwise excise it.
Chris Lattner4d978642005-01-15 22:16:26 +00001838 // FIXME: Why would we need to round FP ops more than integer ones?
1839 // Is Round(Add(Add(A,B),C)) != Round(Add(Round(Add(A,B)), C))
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001840 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001841 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1842 DAG.getValueType(VT));
Chris Lattnerc6c9a5b2005-01-15 06:18:18 +00001843 break;
1844
Chris Lattner4d978642005-01-15 22:16:26 +00001845 case ISD::SDIV:
1846 case ISD::SREM:
1847 // These operators require that their input be sign extended.
1848 Tmp1 = PromoteOp(Node->getOperand(0));
1849 Tmp2 = PromoteOp(Node->getOperand(1));
1850 if (MVT::isInteger(NVT)) {
Chris Lattner0b6ba902005-07-10 00:07:11 +00001851 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1852 DAG.getValueType(VT));
1853 Tmp2 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp2,
1854 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001855 }
1856 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1857
1858 // Perform FP_ROUND: this is probably overly pessimistic.
1859 if (MVT::isFloatingPoint(NVT) && NoExcessFPPrecision)
Chris Lattner0b6ba902005-07-10 00:07:11 +00001860 Result = DAG.getNode(ISD::FP_ROUND_INREG, NVT, Result,
1861 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001862 break;
1863
1864 case ISD::UDIV:
1865 case ISD::UREM:
1866 // These operators require that their input be zero extended.
1867 Tmp1 = PromoteOp(Node->getOperand(0));
1868 Tmp2 = PromoteOp(Node->getOperand(1));
1869 assert(MVT::isInteger(NVT) && "Operators don't apply to FP!");
Chris Lattner0e852af2005-04-13 02:38:47 +00001870 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
1871 Tmp2 = DAG.getZeroExtendInReg(Tmp2, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001872 Result = DAG.getNode(Node->getOpcode(), NVT, Tmp1, Tmp2);
1873 break;
1874
1875 case ISD::SHL:
1876 Tmp1 = PromoteOp(Node->getOperand(0));
1877 Tmp2 = LegalizeOp(Node->getOperand(1));
1878 Result = DAG.getNode(ISD::SHL, NVT, Tmp1, Tmp2);
1879 break;
1880 case ISD::SRA:
1881 // The input value must be properly sign extended.
1882 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0b6ba902005-07-10 00:07:11 +00001883 Tmp1 = DAG.getNode(ISD::SIGN_EXTEND_INREG, NVT, Tmp1,
1884 DAG.getValueType(VT));
Chris Lattner4d978642005-01-15 22:16:26 +00001885 Tmp2 = LegalizeOp(Node->getOperand(1));
1886 Result = DAG.getNode(ISD::SRA, NVT, Tmp1, Tmp2);
1887 break;
1888 case ISD::SRL:
1889 // The input value must be properly zero extended.
1890 Tmp1 = PromoteOp(Node->getOperand(0));
Chris Lattner0e852af2005-04-13 02:38:47 +00001891 Tmp1 = DAG.getZeroExtendInReg(Tmp1, VT);
Chris Lattner4d978642005-01-15 22:16:26 +00001892 Tmp2 = LegalizeOp(Node->getOperand(1));
1893 Result = DAG.getNode(ISD::SRL, NVT, Tmp1, Tmp2);
1894 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001895 case ISD::LOAD:
1896 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1897 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Chris Lattnerc53cd502005-04-10 04:33:47 +00001898 // FIXME: When the DAG combiner exists, change this to use EXTLOAD!
Chris Lattner391a3512005-04-10 17:40:35 +00001899 if (MVT::isInteger(NVT))
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001900 Result = DAG.getExtLoad(ISD::ZEXTLOAD, NVT, Tmp1, Tmp2,
1901 Node->getOperand(2), VT);
Chris Lattner391a3512005-04-10 17:40:35 +00001902 else
Chris Lattnerde0a4b12005-07-10 01:55:33 +00001903 Result = DAG.getExtLoad(ISD::EXTLOAD, NVT, Tmp1, Tmp2,
1904 Node->getOperand(2), VT);
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001905
1906 // Remember that we legalized the chain.
1907 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1908 break;
1909 case ISD::SELECT:
Chris Lattnerd65c3f32005-01-18 19:27:06 +00001910 switch (getTypeAction(Node->getOperand(0).getValueType())) {
1911 case Expand: assert(0 && "It's impossible to expand bools");
1912 case Legal:
1913 Tmp1 = LegalizeOp(Node->getOperand(0));// Legalize the condition.
1914 break;
1915 case Promote:
1916 Tmp1 = PromoteOp(Node->getOperand(0)); // Promote the condition.
1917 break;
1918 }
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001919 Tmp2 = PromoteOp(Node->getOperand(1)); // Legalize the op0
1920 Tmp3 = PromoteOp(Node->getOperand(2)); // Legalize the op1
1921 Result = DAG.getNode(ISD::SELECT, NVT, Tmp1, Tmp2, Tmp3);
1922 break;
Chris Lattnerd0feb642005-05-13 18:43:43 +00001923 case ISD::TAILCALL:
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001924 case ISD::CALL: {
1925 Tmp1 = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
1926 Tmp2 = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
1927
Chris Lattner3d95c142005-01-19 20:24:35 +00001928 std::vector<SDOperand> Ops;
1929 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i)
1930 Ops.push_back(LegalizeOp(Node->getOperand(i)));
1931
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001932 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
1933 "Can only promote single result calls");
1934 std::vector<MVT::ValueType> RetTyVTs;
1935 RetTyVTs.reserve(2);
1936 RetTyVTs.push_back(NVT);
1937 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00001938 SDNode *NC = DAG.getCall(RetTyVTs, Tmp1, Tmp2, Ops,
1939 Node->getOpcode() == ISD::TAILCALL);
Chris Lattner5c8a85e2005-01-16 19:46:48 +00001940 Result = SDOperand(NC, 0);
1941
1942 // Insert the new chain mapping.
1943 AddLegalizedOperand(Op.getValue(1), Result.getValue(1));
1944 break;
Misha Brukman835702a2005-04-21 22:36:52 +00001945 }
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00001946 case ISD::CTPOP:
1947 case ISD::CTTZ:
1948 case ISD::CTLZ:
1949 Tmp1 = Node->getOperand(0);
1950 //Zero extend the argument
1951 Tmp1 = DAG.getNode(ISD::ZERO_EXTEND, NVT, Tmp1);
1952 // Perform the larger operation, then subtract if needed.
1953 Tmp1 = DAG.getNode(Node->getOpcode(), NVT, Tmp1);
1954 switch(Node->getOpcode())
1955 {
1956 case ISD::CTPOP:
1957 Result = Tmp1;
1958 break;
1959 case ISD::CTTZ:
1960 //if Tmp1 == sizeinbits(NVT) then Tmp1 = sizeinbits(Old VT)
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001961 Tmp2 = DAG.getSetCC(ISD::SETEQ, MVT::i1, Tmp1,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00001962 DAG.getConstant(getSizeInBits(NVT), NVT));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001963 Result = DAG.getNode(ISD::SELECT, NVT, Tmp2,
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00001964 DAG.getConstant(getSizeInBits(VT),NVT), Tmp1);
1965 break;
1966 case ISD::CTLZ:
1967 //Tmp1 = Tmp1 - (sizeinbits(NVT) - sizeinbits(Old VT))
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00001968 Result = DAG.getNode(ISD::SUB, NVT, Tmp1,
1969 DAG.getConstant(getSizeInBits(NVT) -
Andrew Lenharthdd426dd2005-05-04 19:11:05 +00001970 getSizeInBits(VT), NVT));
1971 break;
1972 }
1973 break;
Chris Lattner1f2c9d82005-01-15 05:21:40 +00001974 }
1975
1976 assert(Result.Val && "Didn't set a result!");
1977 AddPromotedOperand(Op, Result);
1978 return Result;
1979}
Chris Lattnerdc750592005-01-07 07:47:09 +00001980
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001981/// ExpandAddSub - Find a clever way to expand this add operation into
1982/// subcomponents.
Chris Lattner2e5872c2005-04-02 03:38:53 +00001983void SelectionDAGLegalize::
1984ExpandByParts(unsigned NodeOp, SDOperand LHS, SDOperand RHS,
1985 SDOperand &Lo, SDOperand &Hi) {
Chris Lattnerb3f83b282005-01-20 18:52:28 +00001986 // Expand the subcomponents.
1987 SDOperand LHSL, LHSH, RHSL, RHSH;
1988 ExpandOp(LHS, LHSL, LHSH);
1989 ExpandOp(RHS, RHSL, RHSH);
1990
Chris Lattner8ffd0042005-04-11 20:29:59 +00001991 // FIXME: this should be moved to the dag combiner someday.
Chris Lattner669e8c22005-05-14 07:25:05 +00001992 assert(NodeOp == ISD::ADD_PARTS || NodeOp == ISD::SUB_PARTS);
1993 if (LHSL.getValueType() == MVT::i32) {
1994 SDOperand LowEl;
1995 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(LHSL))
1996 if (C->getValue() == 0)
1997 LowEl = RHSL;
1998 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(RHSL))
1999 if (C->getValue() == 0)
2000 LowEl = LHSL;
2001 if (LowEl.Val) {
2002 // Turn this into an add/sub of the high part only.
2003 SDOperand HiEl =
2004 DAG.getNode(NodeOp == ISD::ADD_PARTS ? ISD::ADD : ISD::SUB,
2005 LowEl.getValueType(), LHSH, RHSH);
2006 Lo = LowEl;
2007 Hi = HiEl;
2008 return;
Chris Lattner8ffd0042005-04-11 20:29:59 +00002009 }
Chris Lattner669e8c22005-05-14 07:25:05 +00002010 }
Chris Lattner8ffd0042005-04-11 20:29:59 +00002011
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002012 std::vector<SDOperand> Ops;
2013 Ops.push_back(LHSL);
2014 Ops.push_back(LHSH);
2015 Ops.push_back(RHSL);
2016 Ops.push_back(RHSH);
Chris Lattner669e8c22005-05-14 07:25:05 +00002017
2018 std::vector<MVT::ValueType> VTs(2, LHSL.getValueType());
2019 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002020 Hi = Lo.getValue(1);
2021}
2022
Chris Lattner4157c412005-04-02 04:00:59 +00002023void SelectionDAGLegalize::ExpandShiftParts(unsigned NodeOp,
2024 SDOperand Op, SDOperand Amt,
2025 SDOperand &Lo, SDOperand &Hi) {
2026 // Expand the subcomponents.
2027 SDOperand LHSL, LHSH;
2028 ExpandOp(Op, LHSL, LHSH);
2029
2030 std::vector<SDOperand> Ops;
2031 Ops.push_back(LHSL);
2032 Ops.push_back(LHSH);
2033 Ops.push_back(Amt);
Chris Lattner669e8c22005-05-14 07:25:05 +00002034 std::vector<MVT::ValueType> VTs;
2035 VTs.push_back(LHSL.getValueType());
2036 VTs.push_back(LHSH.getValueType());
2037 VTs.push_back(Amt.getValueType());
2038 Lo = DAG.getNode(NodeOp, VTs, Ops);
Chris Lattner4157c412005-04-02 04:00:59 +00002039 Hi = Lo.getValue(1);
2040}
2041
2042
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002043/// ExpandShift - Try to find a clever way to expand this shift operation out to
2044/// smaller elements. If we can't find a way that is more efficient than a
2045/// libcall on this target, return false. Otherwise, return true with the
2046/// low-parts expanded into Lo and Hi.
2047bool SelectionDAGLegalize::ExpandShift(unsigned Opc, SDOperand Op,SDOperand Amt,
2048 SDOperand &Lo, SDOperand &Hi) {
2049 assert((Opc == ISD::SHL || Opc == ISD::SRA || Opc == ISD::SRL) &&
2050 "This is not a shift!");
Nate Begemanb0674922005-04-06 21:13:14 +00002051
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002052 MVT::ValueType NVT = TLI.getTypeToTransformTo(Op.getValueType());
Nate Begemanb0674922005-04-06 21:13:14 +00002053 SDOperand ShAmt = LegalizeOp(Amt);
2054 MVT::ValueType ShTy = ShAmt.getValueType();
2055 unsigned VTBits = MVT::getSizeInBits(Op.getValueType());
2056 unsigned NVTBits = MVT::getSizeInBits(NVT);
2057
2058 // Handle the case when Amt is an immediate. Other cases are currently broken
2059 // and are disabled.
2060 if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Amt.Val)) {
2061 unsigned Cst = CN->getValue();
2062 // Expand the incoming operand to be shifted, so that we have its parts
2063 SDOperand InL, InH;
2064 ExpandOp(Op, InL, InH);
2065 switch(Opc) {
2066 case ISD::SHL:
2067 if (Cst > VTBits) {
2068 Lo = DAG.getConstant(0, NVT);
2069 Hi = DAG.getConstant(0, NVT);
2070 } else if (Cst > NVTBits) {
2071 Lo = DAG.getConstant(0, NVT);
2072 Hi = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst-NVTBits,ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002073 } else if (Cst == NVTBits) {
2074 Lo = DAG.getConstant(0, NVT);
2075 Hi = InL;
Nate Begemanb0674922005-04-06 21:13:14 +00002076 } else {
2077 Lo = DAG.getNode(ISD::SHL, NVT, InL, DAG.getConstant(Cst, ShTy));
2078 Hi = DAG.getNode(ISD::OR, NVT,
2079 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(Cst, ShTy)),
2080 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(NVTBits-Cst, ShTy)));
2081 }
2082 return true;
2083 case ISD::SRL:
2084 if (Cst > VTBits) {
2085 Lo = DAG.getConstant(0, NVT);
2086 Hi = DAG.getConstant(0, NVT);
2087 } else if (Cst > NVTBits) {
2088 Lo = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst-NVTBits,ShTy));
2089 Hi = DAG.getConstant(0, NVT);
Chris Lattneredd19702005-04-11 20:08:52 +00002090 } else if (Cst == NVTBits) {
2091 Lo = InH;
2092 Hi = DAG.getConstant(0, NVT);
Nate Begemanb0674922005-04-06 21:13:14 +00002093 } else {
2094 Lo = DAG.getNode(ISD::OR, NVT,
2095 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2096 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2097 Hi = DAG.getNode(ISD::SRL, NVT, InH, DAG.getConstant(Cst, ShTy));
2098 }
2099 return true;
2100 case ISD::SRA:
2101 if (Cst > VTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002102 Hi = Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002103 DAG.getConstant(NVTBits-1, ShTy));
2104 } else if (Cst > NVTBits) {
Misha Brukman835702a2005-04-21 22:36:52 +00002105 Lo = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002106 DAG.getConstant(Cst-NVTBits, ShTy));
Misha Brukman835702a2005-04-21 22:36:52 +00002107 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Nate Begemanb0674922005-04-06 21:13:14 +00002108 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattneredd19702005-04-11 20:08:52 +00002109 } else if (Cst == NVTBits) {
2110 Lo = InH;
Misha Brukman835702a2005-04-21 22:36:52 +00002111 Hi = DAG.getNode(ISD::SRA, NVT, InH,
Chris Lattneredd19702005-04-11 20:08:52 +00002112 DAG.getConstant(NVTBits-1, ShTy));
Nate Begemanb0674922005-04-06 21:13:14 +00002113 } else {
2114 Lo = DAG.getNode(ISD::OR, NVT,
2115 DAG.getNode(ISD::SRL, NVT, InL, DAG.getConstant(Cst, ShTy)),
2116 DAG.getNode(ISD::SHL, NVT, InH, DAG.getConstant(NVTBits-Cst, ShTy)));
2117 Hi = DAG.getNode(ISD::SRA, NVT, InH, DAG.getConstant(Cst, ShTy));
2118 }
2119 return true;
2120 }
2121 }
2122 // FIXME: The following code for expanding shifts using ISD::SELECT is buggy,
2123 // so disable it for now. Currently targets are handling this via SHL_PARTS
2124 // and friends.
2125 return false;
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002126
2127 // If we have an efficient select operation (or if the selects will all fold
2128 // away), lower to some complex code, otherwise just emit the libcall.
2129 if (TLI.getOperationAction(ISD::SELECT, NVT) != TargetLowering::Legal &&
2130 !isa<ConstantSDNode>(Amt))
2131 return false;
2132
2133 SDOperand InL, InH;
2134 ExpandOp(Op, InL, InH);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002135 SDOperand NAmt = DAG.getNode(ISD::SUB, ShTy, // NAmt = 32-ShAmt
2136 DAG.getConstant(NVTBits, ShTy), ShAmt);
2137
Chris Lattner4d25c042005-01-20 20:29:23 +00002138 // Compare the unmasked shift amount against 32.
2139 SDOperand Cond = DAG.getSetCC(ISD::SETGE, TLI.getSetCCResultTy(), ShAmt,
2140 DAG.getConstant(NVTBits, ShTy));
2141
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002142 if (TLI.getShiftAmountFlavor() != TargetLowering::Mask) {
2143 ShAmt = DAG.getNode(ISD::AND, ShTy, ShAmt, // ShAmt &= 31
2144 DAG.getConstant(NVTBits-1, ShTy));
2145 NAmt = DAG.getNode(ISD::AND, ShTy, NAmt, // NAmt &= 31
2146 DAG.getConstant(NVTBits-1, ShTy));
2147 }
2148
2149 if (Opc == ISD::SHL) {
2150 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << Amt) | (Lo >> NAmt)
2151 DAG.getNode(ISD::SHL, NVT, InH, ShAmt),
2152 DAG.getNode(ISD::SRL, NVT, InL, NAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002153 SDOperand T2 = DAG.getNode(ISD::SHL, NVT, InL, ShAmt); // T2 = Lo << Amt&31
Misha Brukman835702a2005-04-21 22:36:52 +00002154
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002155 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
2156 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, DAG.getConstant(0, NVT), T2);
2157 } else {
Chris Lattneraac464e2005-01-21 06:05:23 +00002158 SDOperand HiLoPart = DAG.getNode(ISD::SELECT, NVT,
2159 DAG.getSetCC(ISD::SETEQ,
2160 TLI.getSetCCResultTy(), NAmt,
2161 DAG.getConstant(32, ShTy)),
2162 DAG.getConstant(0, NVT),
2163 DAG.getNode(ISD::SHL, NVT, InH, NAmt));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002164 SDOperand T1 = DAG.getNode(ISD::OR, NVT,// T1 = (Hi << NAmt) | (Lo >> Amt)
Chris Lattneraac464e2005-01-21 06:05:23 +00002165 HiLoPart,
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002166 DAG.getNode(ISD::SRL, NVT, InL, ShAmt));
Chris Lattner4d25c042005-01-20 20:29:23 +00002167 SDOperand T2 = DAG.getNode(Opc, NVT, InH, ShAmt); // T2 = InH >> ShAmt&31
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002168
2169 SDOperand HiPart;
Chris Lattneraac464e2005-01-21 06:05:23 +00002170 if (Opc == ISD::SRA)
2171 HiPart = DAG.getNode(ISD::SRA, NVT, InH,
2172 DAG.getConstant(NVTBits-1, ShTy));
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002173 else
2174 HiPart = DAG.getConstant(0, NVT);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002175 Lo = DAG.getNode(ISD::SELECT, NVT, Cond, T2, T1);
Chris Lattner4d25c042005-01-20 20:29:23 +00002176 Hi = DAG.getNode(ISD::SELECT, NVT, Cond, HiPart, T2);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002177 }
2178 return true;
2179}
Chris Lattneraac464e2005-01-21 06:05:23 +00002180
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002181/// FindLatestCallSeqStart - Scan up the dag to find the latest (highest
2182/// NodeDepth) node that is an CallSeqStart operation and occurs later than
Chris Lattner4add7e32005-01-23 04:42:50 +00002183/// Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002184static void FindLatestCallSeqStart(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002185 if (Node->getNodeDepth() <= Found->getNodeDepth()) return;
2186
Chris Lattner2dce7032005-05-12 23:24:06 +00002187 // If we found an CALLSEQ_START, we already know this node occurs later
Chris Lattner4add7e32005-01-23 04:42:50 +00002188 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002189 if (Node->getOpcode() == ISD::CALLSEQ_START) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002190 Found = Node;
2191 return;
2192 }
2193
2194 // Otherwise, scan the operands of Node to see if any of them is a call.
2195 assert(Node->getNumOperands() != 0 &&
2196 "All leaves should have depth equal to the entry node!");
2197 for (unsigned i = 0, e = Node->getNumOperands()-1; i != e; ++i)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002198 FindLatestCallSeqStart(Node->getOperand(i).Val, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002199
2200 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002201 FindLatestCallSeqStart(Node->getOperand(Node->getNumOperands()-1).Val,
Chris Lattner4add7e32005-01-23 04:42:50 +00002202 Found);
2203}
2204
2205
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002206/// FindEarliestCallSeqEnd - Scan down the dag to find the earliest (lowest
2207/// NodeDepth) node that is an CallSeqEnd operation and occurs more recent
Chris Lattner4add7e32005-01-23 04:42:50 +00002208/// than Found.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002209static void FindEarliestCallSeqEnd(SDNode *Node, SDNode *&Found) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002210 if (Found && Node->getNodeDepth() >= Found->getNodeDepth()) return;
2211
Chris Lattner2dce7032005-05-12 23:24:06 +00002212 // If we found an CALLSEQ_END, we already know this node occurs earlier
Chris Lattner4add7e32005-01-23 04:42:50 +00002213 // than the Found node. Just remember this node and return.
Chris Lattner2dce7032005-05-12 23:24:06 +00002214 if (Node->getOpcode() == ISD::CALLSEQ_END) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002215 Found = Node;
2216 return;
2217 }
2218
2219 // Otherwise, scan the operands of Node to see if any of them is a call.
2220 SDNode::use_iterator UI = Node->use_begin(), E = Node->use_end();
2221 if (UI == E) return;
2222 for (--E; UI != E; ++UI)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002223 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002224
2225 // Tail recurse for the last iteration.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002226 FindEarliestCallSeqEnd(*UI, Found);
Chris Lattner4add7e32005-01-23 04:42:50 +00002227}
2228
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002229/// FindCallSeqEnd - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002230/// find the CALLSEQ_END node that terminates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002231static SDNode *FindCallSeqEnd(SDNode *Node) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002232 if (Node->getOpcode() == ISD::CALLSEQ_END)
Chris Lattner4add7e32005-01-23 04:42:50 +00002233 return Node;
Chris Lattner07f97d52005-04-02 03:22:40 +00002234 if (Node->use_empty())
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002235 return 0; // No CallSeqEnd
Chris Lattner4add7e32005-01-23 04:42:50 +00002236
2237 if (Node->hasOneUse()) // Simple case, only has one user to check.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002238 return FindCallSeqEnd(*Node->use_begin());
Misha Brukman835702a2005-04-21 22:36:52 +00002239
Chris Lattner4add7e32005-01-23 04:42:50 +00002240 SDOperand TheChain(Node, Node->getNumValues()-1);
Chris Lattner3268f242005-05-14 08:34:53 +00002241 if (TheChain.getValueType() != MVT::Other)
2242 TheChain = SDOperand(Node, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002243 assert(TheChain.getValueType() == MVT::Other && "Is not a token chain!");
Misha Brukman835702a2005-04-21 22:36:52 +00002244
2245 for (SDNode::use_iterator UI = Node->use_begin(),
Chris Lattner4add7e32005-01-23 04:42:50 +00002246 E = Node->use_end(); ; ++UI) {
Chris Lattner2dce7032005-05-12 23:24:06 +00002247 assert(UI != E && "Didn't find a user of the tokchain, no CALLSEQ_END!");
Misha Brukman835702a2005-04-21 22:36:52 +00002248
Chris Lattner4add7e32005-01-23 04:42:50 +00002249 // Make sure to only follow users of our token chain.
2250 SDNode *User = *UI;
2251 for (unsigned i = 0, e = User->getNumOperands(); i != e; ++i)
2252 if (User->getOperand(i) == TheChain)
Chris Lattnerbb1d60d2005-05-13 05:17:00 +00002253 if (SDNode *Result = FindCallSeqEnd(User))
2254 return Result;
Chris Lattner4add7e32005-01-23 04:42:50 +00002255 }
2256 assert(0 && "Unreachable");
2257 abort();
2258}
2259
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002260/// FindCallSeqStart - Given a chained node that is part of a call sequence,
Chris Lattner2dce7032005-05-12 23:24:06 +00002261/// find the CALLSEQ_START node that initiates the call sequence.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002262static SDNode *FindCallSeqStart(SDNode *Node) {
2263 assert(Node && "Didn't find callseq_start for a call??");
Chris Lattner2dce7032005-05-12 23:24:06 +00002264 if (Node->getOpcode() == ISD::CALLSEQ_START) return Node;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002265
2266 assert(Node->getOperand(0).getValueType() == MVT::Other &&
2267 "Node doesn't have a token chain argument!");
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002268 return FindCallSeqStart(Node->getOperand(0).Val);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002269}
2270
2271
Chris Lattner4add7e32005-01-23 04:42:50 +00002272/// FindInputOutputChains - If we are replacing an operation with a call we need
2273/// to find the call that occurs before and the call that occurs after it to
Chris Lattner06bbeb62005-05-11 19:02:11 +00002274/// properly serialize the calls in the block. The returned operand is the
2275/// input chain value for the new call (e.g. the entry node or the previous
2276/// call), and OutChain is set to be the chain node to update to point to the
2277/// end of the call chain.
Chris Lattner4add7e32005-01-23 04:42:50 +00002278static SDOperand FindInputOutputChains(SDNode *OpNode, SDNode *&OutChain,
2279 SDOperand Entry) {
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002280 SDNode *LatestCallSeqStart = Entry.Val;
2281 SDNode *LatestCallSeqEnd = 0;
2282 FindLatestCallSeqStart(OpNode, LatestCallSeqStart);
2283 //std::cerr<<"Found node: "; LatestCallSeqStart->dump(); std::cerr <<"\n";
Misha Brukman835702a2005-04-21 22:36:52 +00002284
Chris Lattner2dce7032005-05-12 23:24:06 +00002285 // It is possible that no ISD::CALLSEQ_START was found because there is no
Nate Begemanadd0c632005-04-11 03:01:51 +00002286 // previous call in the function. LatestCallStackDown may in that case be
Chris Lattner2dce7032005-05-12 23:24:06 +00002287 // the entry node itself. Do not attempt to find a matching CALLSEQ_END
2288 // unless LatestCallStackDown is an CALLSEQ_START.
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002289 if (LatestCallSeqStart->getOpcode() == ISD::CALLSEQ_START)
2290 LatestCallSeqEnd = FindCallSeqEnd(LatestCallSeqStart);
Nate Begemanadd0c632005-04-11 03:01:51 +00002291 else
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002292 LatestCallSeqEnd = Entry.Val;
2293 assert(LatestCallSeqEnd && "NULL return from FindCallSeqEnd");
Misha Brukman835702a2005-04-21 22:36:52 +00002294
Chris Lattner06bbeb62005-05-11 19:02:11 +00002295 // Finally, find the first call that this must come before, first we find the
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002296 // CallSeqEnd that ends the call.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002297 OutChain = 0;
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002298 FindEarliestCallSeqEnd(OpNode, OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002299
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002300 // If we found one, translate from the adj up to the callseq_start.
Chris Lattner06bbeb62005-05-11 19:02:11 +00002301 if (OutChain)
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002302 OutChain = FindCallSeqStart(OutChain);
Chris Lattner4add7e32005-01-23 04:42:50 +00002303
Chris Lattner5a14c8a2005-05-13 05:09:11 +00002304 return SDOperand(LatestCallSeqEnd, 0);
Chris Lattner4add7e32005-01-23 04:42:50 +00002305}
2306
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002307/// SpliceCallInto - Given the result chain of a libcall (CallResult), and a
Chris Lattnera5bf1032005-05-12 04:49:08 +00002308void SelectionDAGLegalize::SpliceCallInto(const SDOperand &CallResult,
2309 SDNode *OutChain) {
Chris Lattner06bbeb62005-05-11 19:02:11 +00002310 // Nothing to splice it into?
2311 if (OutChain == 0) return;
2312
2313 assert(OutChain->getOperand(0).getValueType() == MVT::Other);
2314 //OutChain->dump();
2315
2316 // Form a token factor node merging the old inval and the new inval.
2317 SDOperand InToken = DAG.getNode(ISD::TokenFactor, MVT::Other, CallResult,
2318 OutChain->getOperand(0));
2319 // Change the node to refer to the new token.
2320 OutChain->setAdjCallChain(InToken);
2321}
Chris Lattner4add7e32005-01-23 04:42:50 +00002322
2323
Chris Lattneraac464e2005-01-21 06:05:23 +00002324// ExpandLibCall - Expand a node into a call to a libcall. If the result value
2325// does not fit into a register, return the lo part and set the hi part to the
2326// by-reg argument. If it does fit into a single register, return the result
2327// and leave the Hi part unset.
2328SDOperand SelectionDAGLegalize::ExpandLibCall(const char *Name, SDNode *Node,
2329 SDOperand &Hi) {
Chris Lattner4add7e32005-01-23 04:42:50 +00002330 SDNode *OutChain;
2331 SDOperand InChain = FindInputOutputChains(Node, OutChain,
2332 DAG.getEntryNode());
Chris Lattner07f97d52005-04-02 03:22:40 +00002333 if (InChain.Val == 0)
2334 InChain = DAG.getEntryNode();
Chris Lattner4add7e32005-01-23 04:42:50 +00002335
Chris Lattneraac464e2005-01-21 06:05:23 +00002336 TargetLowering::ArgListTy Args;
2337 for (unsigned i = 0, e = Node->getNumOperands(); i != e; ++i) {
2338 MVT::ValueType ArgVT = Node->getOperand(i).getValueType();
2339 const Type *ArgTy = MVT::getTypeForValueType(ArgVT);
2340 Args.push_back(std::make_pair(Node->getOperand(i), ArgTy));
2341 }
2342 SDOperand Callee = DAG.getExternalSymbol(Name, TLI.getPointerTy());
Misha Brukman835702a2005-04-21 22:36:52 +00002343
Chris Lattner06bbeb62005-05-11 19:02:11 +00002344 // Splice the libcall in wherever FindInputOutputChains tells us to.
Chris Lattneraac464e2005-01-21 06:05:23 +00002345 const Type *RetTy = MVT::getTypeForValueType(Node->getValueType(0));
Chris Lattner06bbeb62005-05-11 19:02:11 +00002346 std::pair<SDOperand,SDOperand> CallInfo =
Chris Lattner2e77db62005-05-13 18:50:42 +00002347 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, false,
2348 Callee, Args, DAG);
Chris Lattnera5bf1032005-05-12 04:49:08 +00002349 SpliceCallInto(CallInfo.second, OutChain);
2350
2351 NeedsAnotherIteration = true;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002352
2353 switch (getTypeAction(CallInfo.first.getValueType())) {
Chris Lattneraac464e2005-01-21 06:05:23 +00002354 default: assert(0 && "Unknown thing");
2355 case Legal:
Chris Lattner06bbeb62005-05-11 19:02:11 +00002356 return CallInfo.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002357 case Promote:
2358 assert(0 && "Cannot promote this yet!");
2359 case Expand:
2360 SDOperand Lo;
Chris Lattner06bbeb62005-05-11 19:02:11 +00002361 ExpandOp(CallInfo.first, Lo, Hi);
Chris Lattneraac464e2005-01-21 06:05:23 +00002362 return Lo;
2363 }
2364}
2365
Chris Lattner4add7e32005-01-23 04:42:50 +00002366
Chris Lattneraac464e2005-01-21 06:05:23 +00002367/// ExpandIntToFP - Expand a [US]INT_TO_FP operation, assuming that the
2368/// destination type is legal.
2369SDOperand SelectionDAGLegalize::
2370ExpandIntToFP(bool isSigned, MVT::ValueType DestTy, SDOperand Source) {
2371 assert(getTypeAction(DestTy) == Legal && "Destination type is not legal!");
2372 assert(getTypeAction(Source.getValueType()) == Expand &&
2373 "This is not an expansion!");
2374 assert(Source.getValueType() == MVT::i64 && "Only handle expand from i64!");
2375
Chris Lattner06bbeb62005-05-11 19:02:11 +00002376 if (!isSigned) {
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002377 assert(Source.getValueType() == MVT::i64 &&
2378 "This only works for 64-bit -> FP");
2379 // The 64-bit value loaded will be incorrectly if the 'sign bit' of the
2380 // incoming integer is set. To handle this, we dynamically test to see if
2381 // it is set, and, if so, add a fudge factor.
2382 SDOperand Lo, Hi;
2383 ExpandOp(Source, Lo, Hi);
2384
Chris Lattner2a4f7312005-05-13 04:45:13 +00002385 // If this is unsigned, and not supported, first perform the conversion to
2386 // signed, then adjust the result if the sign bit is set.
2387 SDOperand SignedConv = ExpandIntToFP(true, DestTy,
2388 DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), Lo, Hi));
2389
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002390 SDOperand SignSet = DAG.getSetCC(ISD::SETLT, TLI.getSetCCResultTy(), Hi,
2391 DAG.getConstant(0, Hi.getValueType()));
2392 SDOperand Zero = getIntPtrConstant(0), Four = getIntPtrConstant(4);
2393 SDOperand CstOffset = DAG.getNode(ISD::SELECT, Zero.getValueType(),
2394 SignSet, Four, Zero);
Chris Lattner26f03172005-05-12 18:52:34 +00002395 uint64_t FF = 0x5f800000ULL;
2396 if (TLI.isLittleEndian()) FF <<= 32;
2397 static Constant *FudgeFactor = ConstantUInt::get(Type::ULongTy, FF);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002398
2399 MachineConstantPool *CP = DAG.getMachineFunction().getConstantPool();
2400 SDOperand CPIdx = DAG.getConstantPool(CP->getConstantPoolIndex(FudgeFactor),
2401 TLI.getPointerTy());
2402 CPIdx = DAG.getNode(ISD::ADD, TLI.getPointerTy(), CPIdx, CstOffset);
2403 SDOperand FudgeInReg;
2404 if (DestTy == MVT::f32)
Chris Lattner5385db52005-05-09 20:23:03 +00002405 FudgeInReg = DAG.getLoad(MVT::f32, DAG.getEntryNode(), CPIdx,
2406 DAG.getSrcValue(NULL));
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002407 else {
2408 assert(DestTy == MVT::f64 && "Unexpected conversion");
Chris Lattnerde0a4b12005-07-10 01:55:33 +00002409 FudgeInReg = DAG.getExtLoad(ISD::EXTLOAD, MVT::f64, DAG.getEntryNode(),
2410 CPIdx, DAG.getSrcValue(NULL), MVT::f32);
Chris Lattnere69ad5f2005-04-13 05:09:42 +00002411 }
2412 return DAG.getNode(ISD::ADD, DestTy, SignedConv, FudgeInReg);
Chris Lattneraac464e2005-01-21 06:05:23 +00002413 }
Chris Lattner06bbeb62005-05-11 19:02:11 +00002414
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002415 // Check to see if the target has a custom way to lower this. If so, use it.
2416 switch (TLI.getOperationAction(ISD::SINT_TO_FP, Source.getValueType())) {
2417 default: assert(0 && "This action not implemented for this operation!");
2418 case TargetLowering::Legal:
2419 case TargetLowering::Expand:
2420 break; // This case is handled below.
2421 case TargetLowering::Custom:
2422 Source = DAG.getNode(ISD::SINT_TO_FP, DestTy, Source);
Chris Lattner29dcc712005-05-14 05:50:48 +00002423 return LegalizeOp(TLI.LowerOperation(Source, DAG));
Chris Lattnerd3cc9962005-05-14 05:33:54 +00002424 }
2425
Chris Lattner153587e2005-05-12 07:00:44 +00002426 // Expand the source, then glue it back together for the call. We must expand
2427 // the source in case it is shared (this pass of legalize must traverse it).
2428 SDOperand SrcLo, SrcHi;
2429 ExpandOp(Source, SrcLo, SrcHi);
2430 Source = DAG.getNode(ISD::BUILD_PAIR, Source.getValueType(), SrcLo, SrcHi);
2431
Chris Lattner06bbeb62005-05-11 19:02:11 +00002432 SDNode *OutChain = 0;
2433 SDOperand InChain = FindInputOutputChains(Source.Val, OutChain,
2434 DAG.getEntryNode());
2435 const char *FnName = 0;
2436 if (DestTy == MVT::f32)
2437 FnName = "__floatdisf";
2438 else {
2439 assert(DestTy == MVT::f64 && "Unknown fp value type!");
2440 FnName = "__floatdidf";
2441 }
2442
Chris Lattneraac464e2005-01-21 06:05:23 +00002443 SDOperand Callee = DAG.getExternalSymbol(FnName, TLI.getPointerTy());
2444
2445 TargetLowering::ArgListTy Args;
2446 const Type *ArgTy = MVT::getTypeForValueType(Source.getValueType());
Chris Lattner8a5ad842005-05-12 06:54:21 +00002447
Chris Lattneraac464e2005-01-21 06:05:23 +00002448 Args.push_back(std::make_pair(Source, ArgTy));
2449
2450 // We don't care about token chains for libcalls. We just use the entry
2451 // node as our input and ignore the output chain. This allows us to place
2452 // calls wherever we need them to satisfy data dependences.
2453 const Type *RetTy = MVT::getTypeForValueType(DestTy);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002454
2455 std::pair<SDOperand,SDOperand> CallResult =
Chris Lattner2e77db62005-05-13 18:50:42 +00002456 TLI.LowerCallTo(InChain, RetTy, false, CallingConv::C, true,
2457 Callee, Args, DAG);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002458
Chris Lattnera5bf1032005-05-12 04:49:08 +00002459 SpliceCallInto(CallResult.second, OutChain);
Chris Lattner06bbeb62005-05-11 19:02:11 +00002460 return CallResult.first;
Chris Lattneraac464e2005-01-21 06:05:23 +00002461}
Misha Brukman835702a2005-04-21 22:36:52 +00002462
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002463
2464
Chris Lattnerdc750592005-01-07 07:47:09 +00002465/// ExpandOp - Expand the specified SDOperand into its two component pieces
2466/// Lo&Hi. Note that the Op MUST be an expanded type. As a result of this, the
2467/// LegalizeNodes map is filled in for any results that are not expanded, the
2468/// ExpandedNodes map is filled in for any results that are expanded, and the
2469/// Lo/Hi values are returned.
2470void SelectionDAGLegalize::ExpandOp(SDOperand Op, SDOperand &Lo, SDOperand &Hi){
2471 MVT::ValueType VT = Op.getValueType();
Chris Lattner87a769c2005-01-16 01:11:45 +00002472 MVT::ValueType NVT = TLI.getTypeToTransformTo(VT);
Chris Lattnerdc750592005-01-07 07:47:09 +00002473 SDNode *Node = Op.Val;
2474 assert(getTypeAction(VT) == Expand && "Not an expanded type!");
2475 assert(MVT::isInteger(VT) && "Cannot expand FP values!");
2476 assert(MVT::isInteger(NVT) && NVT < VT &&
2477 "Cannot expand to FP value or to larger int value!");
2478
2479 // If there is more than one use of this, see if we already expanded it.
2480 // There is no use remembering values that only have a single use, as the map
2481 // entries will never be reused.
2482 if (!Node->hasOneUse()) {
2483 std::map<SDOperand, std::pair<SDOperand, SDOperand> >::iterator I
2484 = ExpandedNodes.find(Op);
2485 if (I != ExpandedNodes.end()) {
2486 Lo = I->second.first;
2487 Hi = I->second.second;
2488 return;
2489 }
Chris Lattnerb5a78e02005-05-12 16:53:42 +00002490 } else {
2491 assert(!ExpandedNodes.count(Op) && "Re-expanding a node!");
Chris Lattnerdc750592005-01-07 07:47:09 +00002492 }
2493
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002494 // Expanding to multiple registers needs to perform an optimization step, and
2495 // is not careful to avoid operations the target does not support. Make sure
2496 // that all generated operations are legalized in the next iteration.
2497 NeedsAnotherIteration = true;
Chris Lattnerdc750592005-01-07 07:47:09 +00002498
Chris Lattnerdc750592005-01-07 07:47:09 +00002499 switch (Node->getOpcode()) {
2500 default:
2501 std::cerr << "NODE: "; Node->dump(); std::cerr << "\n";
2502 assert(0 && "Do not know how to expand this operator!");
2503 abort();
Nate Begemancda9aa72005-04-01 22:34:39 +00002504 case ISD::UNDEF:
2505 Lo = DAG.getNode(ISD::UNDEF, NVT);
2506 Hi = DAG.getNode(ISD::UNDEF, NVT);
2507 break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002508 case ISD::Constant: {
2509 uint64_t Cst = cast<ConstantSDNode>(Node)->getValue();
2510 Lo = DAG.getConstant(Cst, NVT);
2511 Hi = DAG.getConstant(Cst >> MVT::getSizeInBits(NVT), NVT);
2512 break;
2513 }
2514
2515 case ISD::CopyFromReg: {
Chris Lattnere727af02005-01-13 20:50:02 +00002516 unsigned Reg = cast<RegSDNode>(Node)->getReg();
Chris Lattnerdc750592005-01-07 07:47:09 +00002517 // Aggregate register values are always in consequtive pairs.
Chris Lattner3b8e7192005-01-14 22:38:01 +00002518 Lo = DAG.getCopyFromReg(Reg, NVT, Node->getOperand(0));
2519 Hi = DAG.getCopyFromReg(Reg+1, NVT, Lo.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002520
Chris Lattner3b8e7192005-01-14 22:38:01 +00002521 // Remember that we legalized the chain.
2522 AddLegalizedOperand(Op.getValue(1), Hi.getValue(1));
2523
Chris Lattnerdc750592005-01-07 07:47:09 +00002524 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2525 break;
2526 }
2527
Chris Lattner32e08b72005-03-28 22:03:13 +00002528 case ISD::BUILD_PAIR:
2529 // Legalize both operands. FIXME: in the future we should handle the case
2530 // where the two elements are not legal.
2531 assert(isTypeLegal(NVT) && "Cannot expand this multiple times yet!");
2532 Lo = LegalizeOp(Node->getOperand(0));
2533 Hi = LegalizeOp(Node->getOperand(1));
2534 break;
2535
Chris Lattner55e9cde2005-05-11 04:51:16 +00002536 case ISD::CTPOP:
2537 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattner3740f392005-05-11 05:09:47 +00002538 Lo = DAG.getNode(ISD::ADD, NVT, // ctpop(HL) -> ctpop(H)+ctpop(L)
2539 DAG.getNode(ISD::CTPOP, NVT, Lo),
2540 DAG.getNode(ISD::CTPOP, NVT, Hi));
Chris Lattner55e9cde2005-05-11 04:51:16 +00002541 Hi = DAG.getConstant(0, NVT);
2542 break;
2543
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002544 case ISD::CTLZ: {
2545 // ctlz (HL) -> ctlz(H) != 32 ? ctlz(H) : (ctlz(L)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002546 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002547 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2548 SDOperand HLZ = DAG.getNode(ISD::CTLZ, NVT, Hi);
2549 SDOperand TopNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2550 HLZ, BitsC);
2551 SDOperand LowPart = DAG.getNode(ISD::CTLZ, NVT, Lo);
2552 LowPart = DAG.getNode(ISD::ADD, NVT, LowPart, BitsC);
2553
2554 Lo = DAG.getNode(ISD::SELECT, NVT, TopNotZero, HLZ, LowPart);
2555 Hi = DAG.getConstant(0, NVT);
2556 break;
2557 }
2558
2559 case ISD::CTTZ: {
2560 // cttz (HL) -> cttz(L) != 32 ? cttz(L) : (cttz(H)+32)
Chris Lattner0bfd1772005-05-12 19:27:51 +00002561 ExpandOp(Node->getOperand(0), Lo, Hi);
Chris Lattnercf5f6b02005-05-12 19:05:01 +00002562 SDOperand BitsC = DAG.getConstant(MVT::getSizeInBits(NVT), NVT);
2563 SDOperand LTZ = DAG.getNode(ISD::CTTZ, NVT, Lo);
2564 SDOperand BotNotZero = DAG.getSetCC(ISD::SETNE, TLI.getSetCCResultTy(),
2565 LTZ, BitsC);
2566 SDOperand HiPart = DAG.getNode(ISD::CTTZ, NVT, Hi);
2567 HiPart = DAG.getNode(ISD::ADD, NVT, HiPart, BitsC);
2568
2569 Lo = DAG.getNode(ISD::SELECT, NVT, BotNotZero, LTZ, HiPart);
2570 Hi = DAG.getConstant(0, NVT);
2571 break;
2572 }
Chris Lattner55e9cde2005-05-11 04:51:16 +00002573
Chris Lattnerdc750592005-01-07 07:47:09 +00002574 case ISD::LOAD: {
2575 SDOperand Ch = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2576 SDOperand Ptr = LegalizeOp(Node->getOperand(1)); // Legalize the pointer.
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002577 Lo = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002578
2579 // Increment the pointer to the other half.
Chris Lattner9242c502005-01-09 19:43:23 +00002580 unsigned IncrementSize = MVT::getSizeInBits(Lo.getValueType())/8;
Chris Lattnerdc750592005-01-07 07:47:09 +00002581 Ptr = DAG.getNode(ISD::ADD, Ptr.getValueType(), Ptr,
2582 getIntPtrConstant(IncrementSize));
Jeff Cohen5f4ef3c2005-07-27 06:12:32 +00002583 //Is this safe? declaring that the two parts of the split load
Andrew Lenharth4a73c2c2005-04-27 20:10:01 +00002584 //are from the same instruction?
2585 Hi = DAG.getLoad(NVT, Ch, Ptr, Node->getOperand(2));
Chris Lattner0d03eb42005-01-19 18:02:17 +00002586
2587 // Build a factor node to remember that this load is independent of the
2588 // other one.
2589 SDOperand TF = DAG.getNode(ISD::TokenFactor, MVT::Other, Lo.getValue(1),
2590 Hi.getValue(1));
Misha Brukman835702a2005-04-21 22:36:52 +00002591
Chris Lattnerdc750592005-01-07 07:47:09 +00002592 // Remember that we legalized the chain.
Chris Lattner0d03eb42005-01-19 18:02:17 +00002593 AddLegalizedOperand(Op.getValue(1), TF);
Chris Lattnerdc750592005-01-07 07:47:09 +00002594 if (!TLI.isLittleEndian())
2595 std::swap(Lo, Hi);
2596 break;
2597 }
Chris Lattnerd0feb642005-05-13 18:43:43 +00002598 case ISD::TAILCALL:
Chris Lattnerdc750592005-01-07 07:47:09 +00002599 case ISD::CALL: {
2600 SDOperand Chain = LegalizeOp(Node->getOperand(0)); // Legalize the chain.
2601 SDOperand Callee = LegalizeOp(Node->getOperand(1)); // Legalize the callee.
2602
Chris Lattner3d95c142005-01-19 20:24:35 +00002603 bool Changed = false;
2604 std::vector<SDOperand> Ops;
2605 for (unsigned i = 2, e = Node->getNumOperands(); i != e; ++i) {
2606 Ops.push_back(LegalizeOp(Node->getOperand(i)));
2607 Changed |= Ops.back() != Node->getOperand(i);
2608 }
2609
Chris Lattnerdc750592005-01-07 07:47:09 +00002610 assert(Node->getNumValues() == 2 && Op.ResNo == 0 &&
2611 "Can only expand a call once so far, not i64 -> i16!");
2612
2613 std::vector<MVT::ValueType> RetTyVTs;
2614 RetTyVTs.reserve(3);
2615 RetTyVTs.push_back(NVT);
2616 RetTyVTs.push_back(NVT);
2617 RetTyVTs.push_back(MVT::Other);
Chris Lattnerd0feb642005-05-13 18:43:43 +00002618 SDNode *NC = DAG.getCall(RetTyVTs, Chain, Callee, Ops,
2619 Node->getOpcode() == ISD::TAILCALL);
Chris Lattnerdc750592005-01-07 07:47:09 +00002620 Lo = SDOperand(NC, 0);
2621 Hi = SDOperand(NC, 1);
2622
2623 // Insert the new chain mapping.
Chris Lattnerc0f31c52005-01-08 20:35:13 +00002624 AddLegalizedOperand(Op.getValue(1), Hi.getValue(2));
Chris Lattnerdc750592005-01-07 07:47:09 +00002625 break;
2626 }
2627 case ISD::AND:
2628 case ISD::OR:
2629 case ISD::XOR: { // Simple logical operators -> two trivial pieces.
2630 SDOperand LL, LH, RL, RH;
2631 ExpandOp(Node->getOperand(0), LL, LH);
2632 ExpandOp(Node->getOperand(1), RL, RH);
2633 Lo = DAG.getNode(Node->getOpcode(), NVT, LL, RL);
2634 Hi = DAG.getNode(Node->getOpcode(), NVT, LH, RH);
2635 break;
2636 }
2637 case ISD::SELECT: {
2638 SDOperand C, LL, LH, RL, RH;
Chris Lattnerd65c3f32005-01-18 19:27:06 +00002639
2640 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2641 case Expand: assert(0 && "It's impossible to expand bools");
2642 case Legal:
2643 C = LegalizeOp(Node->getOperand(0)); // Legalize the condition.
2644 break;
2645 case Promote:
2646 C = PromoteOp(Node->getOperand(0)); // Promote the condition.
2647 break;
2648 }
Chris Lattnerdc750592005-01-07 07:47:09 +00002649 ExpandOp(Node->getOperand(1), LL, LH);
2650 ExpandOp(Node->getOperand(2), RL, RH);
2651 Lo = DAG.getNode(ISD::SELECT, NVT, C, LL, RL);
2652 Hi = DAG.getNode(ISD::SELECT, NVT, C, LH, RH);
2653 break;
2654 }
2655 case ISD::SIGN_EXTEND: {
Chris Lattner47844892005-04-03 23:41:52 +00002656 SDOperand In;
2657 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2658 case Expand: assert(0 && "expand-expand not implemented yet!");
2659 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2660 case Promote:
2661 In = PromoteOp(Node->getOperand(0));
2662 // Emit the appropriate sign_extend_inreg to get the value we want.
2663 In = DAG.getNode(ISD::SIGN_EXTEND_INREG, In.getValueType(), In,
Chris Lattner0b6ba902005-07-10 00:07:11 +00002664 DAG.getValueType(Node->getOperand(0).getValueType()));
Chris Lattner47844892005-04-03 23:41:52 +00002665 break;
2666 }
2667
Chris Lattnerdc750592005-01-07 07:47:09 +00002668 // The low part is just a sign extension of the input (which degenerates to
2669 // a copy).
Chris Lattner47844892005-04-03 23:41:52 +00002670 Lo = DAG.getNode(ISD::SIGN_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002671
Chris Lattnerdc750592005-01-07 07:47:09 +00002672 // The high part is obtained by SRA'ing all but one of the bits of the lo
2673 // part.
Chris Lattner9864b082005-01-12 18:19:52 +00002674 unsigned LoSize = MVT::getSizeInBits(Lo.getValueType());
Chris Lattnerec218372005-01-22 00:31:52 +00002675 Hi = DAG.getNode(ISD::SRA, NVT, Lo, DAG.getConstant(LoSize-1,
2676 TLI.getShiftAmountTy()));
Chris Lattnerdc750592005-01-07 07:47:09 +00002677 break;
2678 }
Chris Lattner47844892005-04-03 23:41:52 +00002679 case ISD::ZERO_EXTEND: {
2680 SDOperand In;
2681 switch (getTypeAction(Node->getOperand(0).getValueType())) {
2682 case Expand: assert(0 && "expand-expand not implemented yet!");
2683 case Legal: In = LegalizeOp(Node->getOperand(0)); break;
2684 case Promote:
2685 In = PromoteOp(Node->getOperand(0));
2686 // Emit the appropriate zero_extend_inreg to get the value we want.
Chris Lattner0e852af2005-04-13 02:38:47 +00002687 In = DAG.getZeroExtendInReg(In, Node->getOperand(0).getValueType());
Chris Lattner47844892005-04-03 23:41:52 +00002688 break;
2689 }
2690
Chris Lattnerdc750592005-01-07 07:47:09 +00002691 // The low part is just a zero extension of the input (which degenerates to
2692 // a copy).
Chris Lattnerd8cbfe82005-04-10 01:13:15 +00002693 Lo = DAG.getNode(ISD::ZERO_EXTEND, NVT, In);
Misha Brukman835702a2005-04-21 22:36:52 +00002694
Chris Lattnerdc750592005-01-07 07:47:09 +00002695 // The high part is just a zero.
2696 Hi = DAG.getConstant(0, NVT);
2697 break;
Chris Lattner47844892005-04-03 23:41:52 +00002698 }
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002699 // These operators cannot be expanded directly, emit them as calls to
2700 // library functions.
2701 case ISD::FP_TO_SINT:
2702 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002703 Lo = ExpandLibCall("__fixsfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002704 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002705 Lo = ExpandLibCall("__fixdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002706 break;
2707 case ISD::FP_TO_UINT:
2708 if (Node->getOperand(0).getValueType() == MVT::f32)
Chris Lattneraac464e2005-01-21 06:05:23 +00002709 Lo = ExpandLibCall("__fixunssfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002710 else
Chris Lattneraac464e2005-01-21 06:05:23 +00002711 Lo = ExpandLibCall("__fixunsdfdi", Node, Hi);
Chris Lattner7e6eeba2005-01-08 19:27:05 +00002712 break;
2713
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002714 case ISD::SHL:
2715 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002716 if (ExpandShift(ISD::SHL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002717 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002718
2719 // If this target supports SHL_PARTS, use it.
2720 if (TLI.getOperationAction(ISD::SHL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002721 ExpandShiftParts(ISD::SHL_PARTS, Node->getOperand(0), Node->getOperand(1),
2722 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002723 break;
2724 }
2725
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002726 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002727 Lo = ExpandLibCall("__ashldi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002728 break;
2729
2730 case ISD::SRA:
2731 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002732 if (ExpandShift(ISD::SRA, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002733 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002734
2735 // If this target supports SRA_PARTS, use it.
2736 if (TLI.getOperationAction(ISD::SRA_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002737 ExpandShiftParts(ISD::SRA_PARTS, Node->getOperand(0), Node->getOperand(1),
2738 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002739 break;
2740 }
2741
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002742 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002743 Lo = ExpandLibCall("__ashrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002744 break;
2745 case ISD::SRL:
2746 // If we can emit an efficient shift operation, do so now.
Chris Lattneraac464e2005-01-21 06:05:23 +00002747 if (ExpandShift(ISD::SRL, Node->getOperand(0), Node->getOperand(1), Lo, Hi))
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002748 break;
Chris Lattner2e5872c2005-04-02 03:38:53 +00002749
2750 // If this target supports SRL_PARTS, use it.
2751 if (TLI.getOperationAction(ISD::SRL_PARTS, NVT) == TargetLowering::Legal) {
Chris Lattner4157c412005-04-02 04:00:59 +00002752 ExpandShiftParts(ISD::SRL_PARTS, Node->getOperand(0), Node->getOperand(1),
2753 Lo, Hi);
Chris Lattner2e5872c2005-04-02 03:38:53 +00002754 break;
2755 }
2756
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002757 // Otherwise, emit a libcall.
Chris Lattneraac464e2005-01-21 06:05:23 +00002758 Lo = ExpandLibCall("__lshrdi3", Node, Hi);
Chris Lattner2a7f8a92005-01-19 04:19:40 +00002759 break;
2760
Misha Brukman835702a2005-04-21 22:36:52 +00002761 case ISD::ADD:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002762 ExpandByParts(ISD::ADD_PARTS, Node->getOperand(0), Node->getOperand(1),
2763 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002764 break;
2765 case ISD::SUB:
Chris Lattner2e5872c2005-04-02 03:38:53 +00002766 ExpandByParts(ISD::SUB_PARTS, Node->getOperand(0), Node->getOperand(1),
2767 Lo, Hi);
Chris Lattnerb3f83b282005-01-20 18:52:28 +00002768 break;
Nate Begemanadd0c632005-04-11 03:01:51 +00002769 case ISD::MUL: {
2770 if (TLI.getOperationAction(ISD::MULHU, NVT) == TargetLowering::Legal) {
2771 SDOperand LL, LH, RL, RH;
2772 ExpandOp(Node->getOperand(0), LL, LH);
2773 ExpandOp(Node->getOperand(1), RL, RH);
2774 Hi = DAG.getNode(ISD::MULHU, NVT, LL, RL);
2775 RH = DAG.getNode(ISD::MUL, NVT, LL, RH);
2776 LH = DAG.getNode(ISD::MUL, NVT, LH, RL);
2777 Hi = DAG.getNode(ISD::ADD, NVT, Hi, RH);
2778 Hi = DAG.getNode(ISD::ADD, NVT, Hi, LH);
2779 Lo = DAG.getNode(ISD::MUL, NVT, LL, RL);
2780 } else {
2781 Lo = ExpandLibCall("__muldi3" , Node, Hi); break;
2782 }
2783 break;
2784 }
Chris Lattneraac464e2005-01-21 06:05:23 +00002785 case ISD::SDIV: Lo = ExpandLibCall("__divdi3" , Node, Hi); break;
2786 case ISD::UDIV: Lo = ExpandLibCall("__udivdi3", Node, Hi); break;
2787 case ISD::SREM: Lo = ExpandLibCall("__moddi3" , Node, Hi); break;
2788 case ISD::UREM: Lo = ExpandLibCall("__umoddi3", Node, Hi); break;
Chris Lattnerdc750592005-01-07 07:47:09 +00002789 }
2790
2791 // Remember in a map if the values will be reused later.
2792 if (!Node->hasOneUse()) {
2793 bool isNew = ExpandedNodes.insert(std::make_pair(Op,
2794 std::make_pair(Lo, Hi))).second;
2795 assert(isNew && "Value already expanded?!?");
2796 }
2797}
2798
2799
2800// SelectionDAG::Legalize - This is the entry point for the file.
2801//
Chris Lattner4add7e32005-01-23 04:42:50 +00002802void SelectionDAG::Legalize() {
Chris Lattnerdc750592005-01-07 07:47:09 +00002803 /// run - This is the main entry point to this class.
2804 ///
Chris Lattner4add7e32005-01-23 04:42:50 +00002805 SelectionDAGLegalize(*this).Run();
Chris Lattnerdc750592005-01-07 07:47:09 +00002806}
2807