blob: b9f1aa45078ecca5862a0708d2f2ce0f5d2f7da6 [file] [log] [blame]
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001//===-- SelectionDAGBuild.cpp - Selection-DAG building --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
15#include "SelectionDAGBuild.h"
16#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000017#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000018#include "llvm/Analysis/AliasAnalysis.h"
19#include "llvm/Constants.h"
20#include "llvm/CallingConv.h"
21#include "llvm/DerivedTypes.h"
22#include "llvm/Function.h"
23#include "llvm/GlobalVariable.h"
24#include "llvm/InlineAsm.h"
25#include "llvm/Instructions.h"
26#include "llvm/Intrinsics.h"
27#include "llvm/IntrinsicInst.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000028#include "llvm/CodeGen/FastISel.h"
29#include "llvm/CodeGen/GCStrategy.h"
30#include "llvm/CodeGen/GCMetadata.h"
31#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineFrameInfo.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
34#include "llvm/CodeGen/MachineJumpTableInfo.h"
35#include "llvm/CodeGen/MachineModuleInfo.h"
36#include "llvm/CodeGen/MachineRegisterInfo.h"
37#include "llvm/CodeGen/SelectionDAG.h"
38#include "llvm/Target/TargetRegisterInfo.h"
39#include "llvm/Target/TargetData.h"
40#include "llvm/Target/TargetFrameInfo.h"
41#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetLowering.h"
43#include "llvm/Target/TargetMachine.h"
44#include "llvm/Target/TargetOptions.h"
45#include "llvm/Support/Compiler.h"
46#include "llvm/Support/Debug.h"
47#include "llvm/Support/MathExtras.h"
48#include <algorithm>
49using namespace llvm;
50
Dale Johannesen601d3c02008-09-05 01:48:15 +000051/// LimitFloatPrecision - Generate low-precision inline sequences for
52/// some float libcalls (6, 8 or 12 bits).
53static unsigned LimitFloatPrecision;
54
55static cl::opt<unsigned, true>
56LimitFPPrecision("limit-float-precision",
57 cl::desc("Generate low-precision inline sequences "
58 "for some float libcalls"),
59 cl::location(LimitFloatPrecision),
60 cl::init(0));
61
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000062/// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
63/// insertvalue or extractvalue indices that identify a member, return
64/// the linearized index of the start of the member.
65///
66static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty,
67 const unsigned *Indices,
68 const unsigned *IndicesEnd,
69 unsigned CurIndex = 0) {
70 // Base case: We're done.
71 if (Indices && Indices == IndicesEnd)
72 return CurIndex;
73
74 // Given a struct type, recursively traverse the elements.
75 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
76 for (StructType::element_iterator EB = STy->element_begin(),
77 EI = EB,
78 EE = STy->element_end();
79 EI != EE; ++EI) {
80 if (Indices && *Indices == unsigned(EI - EB))
81 return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex);
82 CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex);
83 }
84 }
85 // Given an array type, recursively traverse the elements.
86 else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
87 const Type *EltTy = ATy->getElementType();
88 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
89 if (Indices && *Indices == i)
90 return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex);
91 CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex);
92 }
93 }
94 // We haven't found the type we're looking for, so keep searching.
95 return CurIndex + 1;
96}
97
98/// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
99/// MVTs that represent all the individual underlying
100/// non-aggregate types that comprise it.
101///
102/// If Offsets is non-null, it points to a vector to be filled in
103/// with the in-memory offsets of each of the individual values.
104///
105static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty,
106 SmallVectorImpl<MVT> &ValueVTs,
107 SmallVectorImpl<uint64_t> *Offsets = 0,
108 uint64_t StartingOffset = 0) {
109 // Given a struct type, recursively traverse the elements.
110 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
111 const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy);
112 for (StructType::element_iterator EB = STy->element_begin(),
113 EI = EB,
114 EE = STy->element_end();
115 EI != EE; ++EI)
116 ComputeValueVTs(TLI, *EI, ValueVTs, Offsets,
117 StartingOffset + SL->getElementOffset(EI - EB));
118 return;
119 }
120 // Given an array type, recursively traverse the elements.
121 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
122 const Type *EltTy = ATy->getElementType();
123 uint64_t EltSize = TLI.getTargetData()->getABITypeSize(EltTy);
124 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
125 ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets,
126 StartingOffset + i * EltSize);
127 return;
128 }
129 // Base case: we can get an MVT for this LLVM IR type.
130 ValueVTs.push_back(TLI.getValueType(Ty));
131 if (Offsets)
132 Offsets->push_back(StartingOffset);
133}
134
Dan Gohman2a7c6712008-09-03 23:18:39 +0000135namespace llvm {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000136 /// RegsForValue - This struct represents the registers (physical or virtual)
137 /// that a particular set of values is assigned, and the type information about
138 /// the value. The most common situation is to represent one value at a time,
139 /// but struct or array values are handled element-wise as multiple values.
140 /// The splitting of aggregates is performed recursively, so that we never
141 /// have aggregate-typed registers. The values at this point do not necessarily
142 /// have legal types, so each value may require one or more registers of some
143 /// legal type.
144 ///
145 struct VISIBILITY_HIDDEN RegsForValue {
146 /// TLI - The TargetLowering object.
147 ///
148 const TargetLowering *TLI;
149
150 /// ValueVTs - The value types of the values, which may not be legal, and
151 /// may need be promoted or synthesized from one or more registers.
152 ///
153 SmallVector<MVT, 4> ValueVTs;
154
155 /// RegVTs - The value types of the registers. This is the same size as
156 /// ValueVTs and it records, for each value, what the type of the assigned
157 /// register or registers are. (Individual values are never synthesized
158 /// from more than one type of register.)
159 ///
160 /// With virtual registers, the contents of RegVTs is redundant with TLI's
161 /// getRegisterType member function, however when with physical registers
162 /// it is necessary to have a separate record of the types.
163 ///
164 SmallVector<MVT, 4> RegVTs;
165
166 /// Regs - This list holds the registers assigned to the values.
167 /// Each legal or promoted value requires one register, and each
168 /// expanded value requires multiple registers.
169 ///
170 SmallVector<unsigned, 4> Regs;
171
172 RegsForValue() : TLI(0) {}
173
174 RegsForValue(const TargetLowering &tli,
175 const SmallVector<unsigned, 4> &regs,
176 MVT regvt, MVT valuevt)
177 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
178 RegsForValue(const TargetLowering &tli,
179 const SmallVector<unsigned, 4> &regs,
180 const SmallVector<MVT, 4> &regvts,
181 const SmallVector<MVT, 4> &valuevts)
182 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
183 RegsForValue(const TargetLowering &tli,
184 unsigned Reg, const Type *Ty) : TLI(&tli) {
185 ComputeValueVTs(tli, Ty, ValueVTs);
186
187 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
188 MVT ValueVT = ValueVTs[Value];
189 unsigned NumRegs = TLI->getNumRegisters(ValueVT);
190 MVT RegisterVT = TLI->getRegisterType(ValueVT);
191 for (unsigned i = 0; i != NumRegs; ++i)
192 Regs.push_back(Reg + i);
193 RegVTs.push_back(RegisterVT);
194 Reg += NumRegs;
195 }
196 }
197
198 /// append - Add the specified values to this one.
199 void append(const RegsForValue &RHS) {
200 TLI = RHS.TLI;
201 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
202 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
203 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
204 }
205
206
207 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
208 /// this value and returns the result as a ValueVTs value. This uses
209 /// Chain/Flag as the input and updates them for the output Chain/Flag.
210 /// If the Flag pointer is NULL, no flag is used.
211 SDValue getCopyFromRegs(SelectionDAG &DAG,
212 SDValue &Chain, SDValue *Flag) const;
213
214 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
215 /// specified value into the registers specified by this object. This uses
216 /// Chain/Flag as the input and updates them for the output Chain/Flag.
217 /// If the Flag pointer is NULL, no flag is used.
218 void getCopyToRegs(SDValue Val, SelectionDAG &DAG,
219 SDValue &Chain, SDValue *Flag) const;
220
221 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
222 /// operand list. This adds the code marker and includes the number of
223 /// values added into it.
224 void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
225 std::vector<SDValue> &Ops) const;
226 };
227}
228
229/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
230/// PHI nodes or outside of the basic block that defines it, or used by a
231/// switch or atomic instruction, which may expand to multiple basic blocks.
232static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
233 if (isa<PHINode>(I)) return true;
234 BasicBlock *BB = I->getParent();
235 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
236 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) ||
237 // FIXME: Remove switchinst special case.
238 isa<SwitchInst>(*UI))
239 return true;
240 return false;
241}
242
243/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
244/// entry block, return true. This includes arguments used by switches, since
245/// the switch may expand into multiple basic blocks.
246static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) {
247 // With FastISel active, we may be splitting blocks, so force creation
248 // of virtual registers for all non-dead arguments.
Dan Gohman33134c42008-09-25 17:05:24 +0000249 // Don't force virtual registers for byval arguments though, because
250 // fast-isel can't handle those in all cases.
251 if (EnableFastISel && !A->hasByValAttr())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000252 return A->use_empty();
253
254 BasicBlock *Entry = A->getParent()->begin();
255 for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
256 if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
257 return false; // Use not in entry block.
258 return true;
259}
260
261FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli)
262 : TLI(tli) {
263}
264
265void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf,
266 bool EnableFastISel) {
267 Fn = &fn;
268 MF = &mf;
269 RegInfo = &MF->getRegInfo();
270
271 // Create a vreg for each argument register that is not dead and is used
272 // outside of the entry block for the function.
273 for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
274 AI != E; ++AI)
275 if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
276 InitializeRegForValue(AI);
277
278 // Initialize the mapping of values to registers. This is only set up for
279 // instruction values that are used outside of the block that defines
280 // them.
281 Function::iterator BB = Fn->begin(), EB = Fn->end();
282 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
283 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
284 if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
285 const Type *Ty = AI->getAllocatedType();
286 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
287 unsigned Align =
288 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
289 AI->getAlignment());
290
291 TySize *= CUI->getZExtValue(); // Get total allocated size.
292 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
293 StaticAllocaMap[AI] =
294 MF->getFrameInfo()->CreateStackObject(TySize, Align);
295 }
296
297 for (; BB != EB; ++BB)
298 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
299 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
300 if (!isa<AllocaInst>(I) ||
301 !StaticAllocaMap.count(cast<AllocaInst>(I)))
302 InitializeRegForValue(I);
303
304 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
305 // also creates the initial PHI MachineInstrs, though none of the input
306 // operands are populated.
307 for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) {
308 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
309 MBBMap[BB] = MBB;
310 MF->push_back(MBB);
311
312 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
313 // appropriate.
314 PHINode *PN;
315 for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){
316 if (PN->use_empty()) continue;
317
318 unsigned PHIReg = ValueMap[PN];
319 assert(PHIReg && "PHI node does not have an assigned virtual register!");
320
321 SmallVector<MVT, 4> ValueVTs;
322 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
323 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
324 MVT VT = ValueVTs[vti];
325 unsigned NumRegisters = TLI.getNumRegisters(VT);
Dan Gohman6448d912008-09-04 15:39:15 +0000326 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000327 for (unsigned i = 0; i != NumRegisters; ++i)
328 BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i);
329 PHIReg += NumRegisters;
330 }
331 }
332 }
333}
334
335unsigned FunctionLoweringInfo::MakeReg(MVT VT) {
336 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
337}
338
339/// CreateRegForValue - Allocate the appropriate number of virtual registers of
340/// the correctly promoted or expanded types. Assign these registers
341/// consecutive vreg numbers and return the first assigned number.
342///
343/// In the case that the given value has struct or array type, this function
344/// will assign registers for each member or element.
345///
346unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
347 SmallVector<MVT, 4> ValueVTs;
348 ComputeValueVTs(TLI, V->getType(), ValueVTs);
349
350 unsigned FirstReg = 0;
351 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
352 MVT ValueVT = ValueVTs[Value];
353 MVT RegisterVT = TLI.getRegisterType(ValueVT);
354
355 unsigned NumRegs = TLI.getNumRegisters(ValueVT);
356 for (unsigned i = 0; i != NumRegs; ++i) {
357 unsigned R = MakeReg(RegisterVT);
358 if (!FirstReg) FirstReg = R;
359 }
360 }
361 return FirstReg;
362}
363
364/// getCopyFromParts - Create a value that contains the specified legal parts
365/// combined into the value they represent. If the parts combine to a type
366/// larger then ValueVT then AssertOp can be used to specify whether the extra
367/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
368/// (ISD::AssertSext).
369static SDValue getCopyFromParts(SelectionDAG &DAG,
370 const SDValue *Parts,
371 unsigned NumParts,
372 MVT PartVT,
373 MVT ValueVT,
374 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
375 assert(NumParts > 0 && "No parts to assemble!");
376 TargetLowering &TLI = DAG.getTargetLoweringInfo();
377 SDValue Val = Parts[0];
378
379 if (NumParts > 1) {
380 // Assemble the value from multiple parts.
381 if (!ValueVT.isVector()) {
382 unsigned PartBits = PartVT.getSizeInBits();
383 unsigned ValueBits = ValueVT.getSizeInBits();
384
385 // Assemble the power of 2 part.
386 unsigned RoundParts = NumParts & (NumParts - 1) ?
387 1 << Log2_32(NumParts) : NumParts;
388 unsigned RoundBits = PartBits * RoundParts;
389 MVT RoundVT = RoundBits == ValueBits ?
390 ValueVT : MVT::getIntegerVT(RoundBits);
391 SDValue Lo, Hi;
392
393 if (RoundParts > 2) {
394 MVT HalfVT = MVT::getIntegerVT(RoundBits/2);
395 Lo = getCopyFromParts(DAG, Parts, RoundParts/2, PartVT, HalfVT);
396 Hi = getCopyFromParts(DAG, Parts+RoundParts/2, RoundParts/2,
397 PartVT, HalfVT);
398 } else {
399 Lo = Parts[0];
400 Hi = Parts[1];
401 }
402 if (TLI.isBigEndian())
403 std::swap(Lo, Hi);
404 Val = DAG.getNode(ISD::BUILD_PAIR, RoundVT, Lo, Hi);
405
406 if (RoundParts < NumParts) {
407 // Assemble the trailing non-power-of-2 part.
408 unsigned OddParts = NumParts - RoundParts;
409 MVT OddVT = MVT::getIntegerVT(OddParts * PartBits);
410 Hi = getCopyFromParts(DAG, Parts+RoundParts, OddParts, PartVT, OddVT);
411
412 // Combine the round and odd parts.
413 Lo = Val;
414 if (TLI.isBigEndian())
415 std::swap(Lo, Hi);
416 MVT TotalVT = MVT::getIntegerVT(NumParts * PartBits);
417 Hi = DAG.getNode(ISD::ANY_EXTEND, TotalVT, Hi);
418 Hi = DAG.getNode(ISD::SHL, TotalVT, Hi,
419 DAG.getConstant(Lo.getValueType().getSizeInBits(),
420 TLI.getShiftAmountTy()));
421 Lo = DAG.getNode(ISD::ZERO_EXTEND, TotalVT, Lo);
422 Val = DAG.getNode(ISD::OR, TotalVT, Lo, Hi);
423 }
424 } else {
425 // Handle a multi-element vector.
426 MVT IntermediateVT, RegisterVT;
427 unsigned NumIntermediates;
428 unsigned NumRegs =
429 TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates,
430 RegisterVT);
431 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
432 NumParts = NumRegs; // Silence a compiler warning.
433 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
434 assert(RegisterVT == Parts[0].getValueType() &&
435 "Part type doesn't match part!");
436
437 // Assemble the parts into intermediate operands.
438 SmallVector<SDValue, 8> Ops(NumIntermediates);
439 if (NumIntermediates == NumParts) {
440 // If the register was not expanded, truncate or copy the value,
441 // as appropriate.
442 for (unsigned i = 0; i != NumParts; ++i)
443 Ops[i] = getCopyFromParts(DAG, &Parts[i], 1,
444 PartVT, IntermediateVT);
445 } else if (NumParts > 0) {
446 // If the intermediate type was expanded, build the intermediate operands
447 // from the parts.
448 assert(NumParts % NumIntermediates == 0 &&
449 "Must expand into a divisible number of parts!");
450 unsigned Factor = NumParts / NumIntermediates;
451 for (unsigned i = 0; i != NumIntermediates; ++i)
452 Ops[i] = getCopyFromParts(DAG, &Parts[i * Factor], Factor,
453 PartVT, IntermediateVT);
454 }
455
456 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
457 // operands.
458 Val = DAG.getNode(IntermediateVT.isVector() ?
459 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR,
460 ValueVT, &Ops[0], NumIntermediates);
461 }
462 }
463
464 // There is now one part, held in Val. Correct it to match ValueVT.
465 PartVT = Val.getValueType();
466
467 if (PartVT == ValueVT)
468 return Val;
469
470 if (PartVT.isVector()) {
471 assert(ValueVT.isVector() && "Unknown vector conversion!");
472 return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val);
473 }
474
475 if (ValueVT.isVector()) {
476 assert(ValueVT.getVectorElementType() == PartVT &&
477 ValueVT.getVectorNumElements() == 1 &&
478 "Only trivial scalar-to-vector conversions should get here!");
479 return DAG.getNode(ISD::BUILD_VECTOR, ValueVT, Val);
480 }
481
482 if (PartVT.isInteger() &&
483 ValueVT.isInteger()) {
484 if (ValueVT.bitsLT(PartVT)) {
485 // For a truncate, see if we have any information to
486 // indicate whether the truncated bits will always be
487 // zero or sign-extension.
488 if (AssertOp != ISD::DELETED_NODE)
489 Val = DAG.getNode(AssertOp, PartVT, Val,
490 DAG.getValueType(ValueVT));
491 return DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
492 } else {
493 return DAG.getNode(ISD::ANY_EXTEND, ValueVT, Val);
494 }
495 }
496
497 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
498 if (ValueVT.bitsLT(Val.getValueType()))
499 // FP_ROUND's are always exact here.
500 return DAG.getNode(ISD::FP_ROUND, ValueVT, Val,
501 DAG.getIntPtrConstant(1));
502 return DAG.getNode(ISD::FP_EXTEND, ValueVT, Val);
503 }
504
505 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
506 return DAG.getNode(ISD::BIT_CONVERT, ValueVT, Val);
507
508 assert(0 && "Unknown mismatch!");
509 return SDValue();
510}
511
512/// getCopyToParts - Create a series of nodes that contain the specified value
513/// split into legal parts. If the parts contain more bits than Val, then, for
514/// integers, ExtendKind can be used to specify how to generate the extra bits.
515static void getCopyToParts(SelectionDAG &DAG,
516 SDValue Val,
517 SDValue *Parts,
518 unsigned NumParts,
519 MVT PartVT,
520 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
521 TargetLowering &TLI = DAG.getTargetLoweringInfo();
522 MVT PtrVT = TLI.getPointerTy();
523 MVT ValueVT = Val.getValueType();
524 unsigned PartBits = PartVT.getSizeInBits();
525 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
526
527 if (!NumParts)
528 return;
529
530 if (!ValueVT.isVector()) {
531 if (PartVT == ValueVT) {
532 assert(NumParts == 1 && "No-op copy with multiple parts!");
533 Parts[0] = Val;
534 return;
535 }
536
537 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
538 // If the parts cover more bits than the value has, promote the value.
539 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
540 assert(NumParts == 1 && "Do not know what to promote to!");
541 Val = DAG.getNode(ISD::FP_EXTEND, PartVT, Val);
542 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
543 ValueVT = MVT::getIntegerVT(NumParts * PartBits);
544 Val = DAG.getNode(ExtendKind, ValueVT, Val);
545 } else {
546 assert(0 && "Unknown mismatch!");
547 }
548 } else if (PartBits == ValueVT.getSizeInBits()) {
549 // Different types of the same size.
550 assert(NumParts == 1 && PartVT != ValueVT);
551 Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val);
552 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
553 // If the parts cover less bits than value has, truncate the value.
554 if (PartVT.isInteger() && ValueVT.isInteger()) {
555 ValueVT = MVT::getIntegerVT(NumParts * PartBits);
556 Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
557 } else {
558 assert(0 && "Unknown mismatch!");
559 }
560 }
561
562 // The value may have changed - recompute ValueVT.
563 ValueVT = Val.getValueType();
564 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
565 "Failed to tile the value with PartVT!");
566
567 if (NumParts == 1) {
568 assert(PartVT == ValueVT && "Type conversion failed!");
569 Parts[0] = Val;
570 return;
571 }
572
573 // Expand the value into multiple parts.
574 if (NumParts & (NumParts - 1)) {
575 // The number of parts is not a power of 2. Split off and copy the tail.
576 assert(PartVT.isInteger() && ValueVT.isInteger() &&
577 "Do not know what to expand to!");
578 unsigned RoundParts = 1 << Log2_32(NumParts);
579 unsigned RoundBits = RoundParts * PartBits;
580 unsigned OddParts = NumParts - RoundParts;
581 SDValue OddVal = DAG.getNode(ISD::SRL, ValueVT, Val,
582 DAG.getConstant(RoundBits,
583 TLI.getShiftAmountTy()));
584 getCopyToParts(DAG, OddVal, Parts + RoundParts, OddParts, PartVT);
585 if (TLI.isBigEndian())
586 // The odd parts were reversed by getCopyToParts - unreverse them.
587 std::reverse(Parts + RoundParts, Parts + NumParts);
588 NumParts = RoundParts;
589 ValueVT = MVT::getIntegerVT(NumParts * PartBits);
590 Val = DAG.getNode(ISD::TRUNCATE, ValueVT, Val);
591 }
592
593 // The number of parts is a power of 2. Repeatedly bisect the value using
594 // EXTRACT_ELEMENT.
595 Parts[0] = DAG.getNode(ISD::BIT_CONVERT,
596 MVT::getIntegerVT(ValueVT.getSizeInBits()),
597 Val);
598 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
599 for (unsigned i = 0; i < NumParts; i += StepSize) {
600 unsigned ThisBits = StepSize * PartBits / 2;
601 MVT ThisVT = MVT::getIntegerVT (ThisBits);
602 SDValue &Part0 = Parts[i];
603 SDValue &Part1 = Parts[i+StepSize/2];
604
605 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0,
606 DAG.getConstant(1, PtrVT));
607 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, ThisVT, Part0,
608 DAG.getConstant(0, PtrVT));
609
610 if (ThisBits == PartBits && ThisVT != PartVT) {
611 Part0 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part0);
612 Part1 = DAG.getNode(ISD::BIT_CONVERT, PartVT, Part1);
613 }
614 }
615 }
616
617 if (TLI.isBigEndian())
618 std::reverse(Parts, Parts + NumParts);
619
620 return;
621 }
622
623 // Vector ValueVT.
624 if (NumParts == 1) {
625 if (PartVT != ValueVT) {
626 if (PartVT.isVector()) {
627 Val = DAG.getNode(ISD::BIT_CONVERT, PartVT, Val);
628 } else {
629 assert(ValueVT.getVectorElementType() == PartVT &&
630 ValueVT.getVectorNumElements() == 1 &&
631 "Only trivial vector-to-scalar conversions should get here!");
632 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, PartVT, Val,
633 DAG.getConstant(0, PtrVT));
634 }
635 }
636
637 Parts[0] = Val;
638 return;
639 }
640
641 // Handle a multi-element vector.
642 MVT IntermediateVT, RegisterVT;
643 unsigned NumIntermediates;
644 unsigned NumRegs =
645 DAG.getTargetLoweringInfo()
646 .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates,
647 RegisterVT);
648 unsigned NumElements = ValueVT.getVectorNumElements();
649
650 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
651 NumParts = NumRegs; // Silence a compiler warning.
652 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
653
654 // Split the vector into intermediate operands.
655 SmallVector<SDValue, 8> Ops(NumIntermediates);
656 for (unsigned i = 0; i != NumIntermediates; ++i)
657 if (IntermediateVT.isVector())
658 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR,
659 IntermediateVT, Val,
660 DAG.getConstant(i * (NumElements / NumIntermediates),
661 PtrVT));
662 else
663 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
664 IntermediateVT, Val,
665 DAG.getConstant(i, PtrVT));
666
667 // Split the intermediate operands into legal parts.
668 if (NumParts == NumIntermediates) {
669 // If the register was not expanded, promote or copy the value,
670 // as appropriate.
671 for (unsigned i = 0; i != NumParts; ++i)
672 getCopyToParts(DAG, Ops[i], &Parts[i], 1, PartVT);
673 } else if (NumParts > 0) {
674 // If the intermediate type was expanded, split each the value into
675 // legal parts.
676 assert(NumParts % NumIntermediates == 0 &&
677 "Must expand into a divisible number of parts!");
678 unsigned Factor = NumParts / NumIntermediates;
679 for (unsigned i = 0; i != NumIntermediates; ++i)
680 getCopyToParts(DAG, Ops[i], &Parts[i * Factor], Factor, PartVT);
681 }
682}
683
684
685void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
686 AA = &aa;
687 GFI = gfi;
688 TD = DAG.getTarget().getTargetData();
689}
690
691/// clear - Clear out the curret SelectionDAG and the associated
692/// state and prepare this SelectionDAGLowering object to be used
693/// for a new block. This doesn't clear out information about
694/// additional blocks that are needed to complete switch lowering
695/// or PHI node updating; that information is cleared out as it is
696/// consumed.
697void SelectionDAGLowering::clear() {
698 NodeMap.clear();
699 PendingLoads.clear();
700 PendingExports.clear();
701 DAG.clear();
702}
703
704/// getRoot - Return the current virtual root of the Selection DAG,
705/// flushing any PendingLoad items. This must be done before emitting
706/// a store or any other node that may need to be ordered after any
707/// prior load instructions.
708///
709SDValue SelectionDAGLowering::getRoot() {
710 if (PendingLoads.empty())
711 return DAG.getRoot();
712
713 if (PendingLoads.size() == 1) {
714 SDValue Root = PendingLoads[0];
715 DAG.setRoot(Root);
716 PendingLoads.clear();
717 return Root;
718 }
719
720 // Otherwise, we have to make a token factor node.
721 SDValue Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
722 &PendingLoads[0], PendingLoads.size());
723 PendingLoads.clear();
724 DAG.setRoot(Root);
725 return Root;
726}
727
728/// getControlRoot - Similar to getRoot, but instead of flushing all the
729/// PendingLoad items, flush all the PendingExports items. It is necessary
730/// to do this before emitting a terminator instruction.
731///
732SDValue SelectionDAGLowering::getControlRoot() {
733 SDValue Root = DAG.getRoot();
734
735 if (PendingExports.empty())
736 return Root;
737
738 // Turn all of the CopyToReg chains into one factored node.
739 if (Root.getOpcode() != ISD::EntryToken) {
740 unsigned i = 0, e = PendingExports.size();
741 for (; i != e; ++i) {
742 assert(PendingExports[i].getNode()->getNumOperands() > 1);
743 if (PendingExports[i].getNode()->getOperand(0) == Root)
744 break; // Don't add the root if we already indirectly depend on it.
745 }
746
747 if (i == e)
748 PendingExports.push_back(Root);
749 }
750
751 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
752 &PendingExports[0],
753 PendingExports.size());
754 PendingExports.clear();
755 DAG.setRoot(Root);
756 return Root;
757}
758
759void SelectionDAGLowering::visit(Instruction &I) {
760 visit(I.getOpcode(), I);
761}
762
763void SelectionDAGLowering::visit(unsigned Opcode, User &I) {
764 // Note: this doesn't use InstVisitor, because it has to work with
765 // ConstantExpr's in addition to instructions.
766 switch (Opcode) {
767 default: assert(0 && "Unknown instruction type encountered!");
768 abort();
769 // Build the switch statement using the Instruction.def file.
770#define HANDLE_INST(NUM, OPCODE, CLASS) \
771 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
772#include "llvm/Instruction.def"
773 }
774}
775
776void SelectionDAGLowering::visitAdd(User &I) {
777 if (I.getType()->isFPOrFPVector())
778 visitBinary(I, ISD::FADD);
779 else
780 visitBinary(I, ISD::ADD);
781}
782
783void SelectionDAGLowering::visitMul(User &I) {
784 if (I.getType()->isFPOrFPVector())
785 visitBinary(I, ISD::FMUL);
786 else
787 visitBinary(I, ISD::MUL);
788}
789
790SDValue SelectionDAGLowering::getValue(const Value *V) {
791 SDValue &N = NodeMap[V];
792 if (N.getNode()) return N;
793
794 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
795 MVT VT = TLI.getValueType(V->getType(), true);
796
797 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000798 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000799
800 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
801 return N = DAG.getGlobalAddress(GV, VT);
802
803 if (isa<ConstantPointerNull>(C))
804 return N = DAG.getConstant(0, TLI.getPointerTy());
805
806 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000807 return N = DAG.getConstantFP(*CFP, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000808
809 if (isa<UndefValue>(C) && !isa<VectorType>(V->getType()) &&
810 !V->getType()->isAggregateType())
811 return N = DAG.getNode(ISD::UNDEF, VT);
812
813 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
814 visit(CE->getOpcode(), *CE);
815 SDValue N1 = NodeMap[V];
816 assert(N1.getNode() && "visit didn't populate the ValueMap!");
817 return N1;
818 }
819
820 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
821 SmallVector<SDValue, 4> Constants;
822 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
823 OI != OE; ++OI) {
824 SDNode *Val = getValue(*OI).getNode();
825 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
826 Constants.push_back(SDValue(Val, i));
827 }
828 return DAG.getMergeValues(&Constants[0], Constants.size());
829 }
830
831 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
832 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
833 "Unknown struct or array constant!");
834
835 SmallVector<MVT, 4> ValueVTs;
836 ComputeValueVTs(TLI, C->getType(), ValueVTs);
837 unsigned NumElts = ValueVTs.size();
838 if (NumElts == 0)
839 return SDValue(); // empty struct
840 SmallVector<SDValue, 4> Constants(NumElts);
841 for (unsigned i = 0; i != NumElts; ++i) {
842 MVT EltVT = ValueVTs[i];
843 if (isa<UndefValue>(C))
844 Constants[i] = DAG.getNode(ISD::UNDEF, EltVT);
845 else if (EltVT.isFloatingPoint())
846 Constants[i] = DAG.getConstantFP(0, EltVT);
847 else
848 Constants[i] = DAG.getConstant(0, EltVT);
849 }
850 return DAG.getMergeValues(&Constants[0], NumElts);
851 }
852
853 const VectorType *VecTy = cast<VectorType>(V->getType());
854 unsigned NumElements = VecTy->getNumElements();
855
856 // Now that we know the number and type of the elements, get that number of
857 // elements into the Ops array based on what kind of constant it is.
858 SmallVector<SDValue, 16> Ops;
859 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
860 for (unsigned i = 0; i != NumElements; ++i)
861 Ops.push_back(getValue(CP->getOperand(i)));
862 } else {
863 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
864 "Unknown vector constant!");
865 MVT EltVT = TLI.getValueType(VecTy->getElementType());
866
867 SDValue Op;
868 if (isa<UndefValue>(C))
869 Op = DAG.getNode(ISD::UNDEF, EltVT);
870 else if (EltVT.isFloatingPoint())
871 Op = DAG.getConstantFP(0, EltVT);
872 else
873 Op = DAG.getConstant(0, EltVT);
874 Ops.assign(NumElements, Op);
875 }
876
877 // Create a BUILD_VECTOR node.
878 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, VT, &Ops[0], Ops.size());
879 }
880
881 // If this is a static alloca, generate it as the frameindex instead of
882 // computation.
883 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
884 DenseMap<const AllocaInst*, int>::iterator SI =
885 FuncInfo.StaticAllocaMap.find(AI);
886 if (SI != FuncInfo.StaticAllocaMap.end())
887 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
888 }
889
890 unsigned InReg = FuncInfo.ValueMap[V];
891 assert(InReg && "Value not in map!");
892
893 RegsForValue RFV(TLI, InReg, V->getType());
894 SDValue Chain = DAG.getEntryNode();
895 return RFV.getCopyFromRegs(DAG, Chain, NULL);
896}
897
898
899void SelectionDAGLowering::visitRet(ReturnInst &I) {
900 if (I.getNumOperands() == 0) {
901 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other, getControlRoot()));
902 return;
903 }
904
905 SmallVector<SDValue, 8> NewValues;
906 NewValues.push_back(getControlRoot());
907 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
908 SDValue RetOp = getValue(I.getOperand(i));
909
910 SmallVector<MVT, 4> ValueVTs;
911 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
912 for (unsigned j = 0, f = ValueVTs.size(); j != f; ++j) {
913 MVT VT = ValueVTs[j];
914
915 // FIXME: C calling convention requires the return type to be promoted to
916 // at least 32-bit. But this is not necessary for non-C calling conventions.
917 if (VT.isInteger()) {
918 MVT MinVT = TLI.getRegisterType(MVT::i32);
919 if (VT.bitsLT(MinVT))
920 VT = MinVT;
921 }
922
923 unsigned NumParts = TLI.getNumRegisters(VT);
924 MVT PartVT = TLI.getRegisterType(VT);
925 SmallVector<SDValue, 4> Parts(NumParts);
926 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
927
928 const Function *F = I.getParent()->getParent();
929 if (F->paramHasAttr(0, ParamAttr::SExt))
930 ExtendKind = ISD::SIGN_EXTEND;
931 else if (F->paramHasAttr(0, ParamAttr::ZExt))
932 ExtendKind = ISD::ZERO_EXTEND;
933
934 getCopyToParts(DAG, SDValue(RetOp.getNode(), RetOp.getResNo() + j),
935 &Parts[0], NumParts, PartVT, ExtendKind);
936
937 for (unsigned i = 0; i < NumParts; ++i) {
938 NewValues.push_back(Parts[i]);
939 NewValues.push_back(DAG.getArgFlags(ISD::ArgFlagsTy()));
940 }
941 }
942 }
943 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other,
944 &NewValues[0], NewValues.size()));
945}
946
947/// ExportFromCurrentBlock - If this condition isn't known to be exported from
948/// the current basic block, add it to ValueMap now so that we'll get a
949/// CopyTo/FromReg.
950void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
951 // No need to export constants.
952 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
953
954 // Already exported?
955 if (FuncInfo.isExportedInst(V)) return;
956
957 unsigned Reg = FuncInfo.InitializeRegForValue(V);
958 CopyValueToVirtualRegister(V, Reg);
959}
960
961bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
962 const BasicBlock *FromBB) {
963 // The operands of the setcc have to be in this block. We don't know
964 // how to export them from some other block.
965 if (Instruction *VI = dyn_cast<Instruction>(V)) {
966 // Can export from current BB.
967 if (VI->getParent() == FromBB)
968 return true;
969
970 // Is already exported, noop.
971 return FuncInfo.isExportedInst(V);
972 }
973
974 // If this is an argument, we can export it if the BB is the entry block or
975 // if it is already exported.
976 if (isa<Argument>(V)) {
977 if (FromBB == &FromBB->getParent()->getEntryBlock())
978 return true;
979
980 // Otherwise, can only export this if it is already exported.
981 return FuncInfo.isExportedInst(V);
982 }
983
984 // Otherwise, constants can always be exported.
985 return true;
986}
987
988static bool InBlock(const Value *V, const BasicBlock *BB) {
989 if (const Instruction *I = dyn_cast<Instruction>(V))
990 return I->getParent() == BB;
991 return true;
992}
993
994/// FindMergedConditions - If Cond is an expression like
995void SelectionDAGLowering::FindMergedConditions(Value *Cond,
996 MachineBasicBlock *TBB,
997 MachineBasicBlock *FBB,
998 MachineBasicBlock *CurBB,
999 unsigned Opc) {
1000 // If this node is not part of the or/and tree, emit it as a branch.
1001 Instruction *BOp = dyn_cast<Instruction>(Cond);
1002
1003 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
1004 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1005 BOp->getParent() != CurBB->getBasicBlock() ||
1006 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1007 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1008 const BasicBlock *BB = CurBB->getBasicBlock();
1009
1010 // If the leaf of the tree is a comparison, merge the condition into
1011 // the caseblock.
1012 if ((isa<ICmpInst>(Cond) || isa<FCmpInst>(Cond)) &&
1013 // The operands of the cmp have to be in this block. We don't know
1014 // how to export them from some other block. If this is the first block
1015 // of the sequence, no exporting is needed.
1016 (CurBB == CurMBB ||
1017 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1018 isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) {
1019 BOp = cast<Instruction>(Cond);
1020 ISD::CondCode Condition;
1021 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
1022 switch (IC->getPredicate()) {
1023 default: assert(0 && "Unknown icmp predicate opcode!");
1024 case ICmpInst::ICMP_EQ: Condition = ISD::SETEQ; break;
1025 case ICmpInst::ICMP_NE: Condition = ISD::SETNE; break;
1026 case ICmpInst::ICMP_SLE: Condition = ISD::SETLE; break;
1027 case ICmpInst::ICMP_ULE: Condition = ISD::SETULE; break;
1028 case ICmpInst::ICMP_SGE: Condition = ISD::SETGE; break;
1029 case ICmpInst::ICMP_UGE: Condition = ISD::SETUGE; break;
1030 case ICmpInst::ICMP_SLT: Condition = ISD::SETLT; break;
1031 case ICmpInst::ICMP_ULT: Condition = ISD::SETULT; break;
1032 case ICmpInst::ICMP_SGT: Condition = ISD::SETGT; break;
1033 case ICmpInst::ICMP_UGT: Condition = ISD::SETUGT; break;
1034 }
1035 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
1036 ISD::CondCode FPC, FOC;
1037 switch (FC->getPredicate()) {
1038 default: assert(0 && "Unknown fcmp predicate opcode!");
1039 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1040 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1041 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1042 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1043 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1044 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1045 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1046 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1047 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1048 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1049 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1050 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1051 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1052 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1053 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1054 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1055 }
1056 if (FiniteOnlyFPMath())
1057 Condition = FOC;
1058 else
1059 Condition = FPC;
1060 } else {
1061 Condition = ISD::SETEQ; // silence warning.
1062 assert(0 && "Unknown compare instruction");
1063 }
1064
1065 CaseBlock CB(Condition, BOp->getOperand(0),
1066 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1067 SwitchCases.push_back(CB);
1068 return;
1069 }
1070
1071 // Create a CaseBlock record representing this branch.
1072 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(),
1073 NULL, TBB, FBB, CurBB);
1074 SwitchCases.push_back(CB);
1075 return;
1076 }
1077
1078
1079 // Create TmpBB after CurBB.
1080 MachineFunction::iterator BBI = CurBB;
1081 MachineFunction &MF = DAG.getMachineFunction();
1082 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1083 CurBB->getParent()->insert(++BBI, TmpBB);
1084
1085 if (Opc == Instruction::Or) {
1086 // Codegen X | Y as:
1087 // jmp_if_X TBB
1088 // jmp TmpBB
1089 // TmpBB:
1090 // jmp_if_Y TBB
1091 // jmp FBB
1092 //
1093
1094 // Emit the LHS condition.
1095 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
1096
1097 // Emit the RHS condition into TmpBB.
1098 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1099 } else {
1100 assert(Opc == Instruction::And && "Unknown merge op!");
1101 // Codegen X & Y as:
1102 // jmp_if_X TmpBB
1103 // jmp FBB
1104 // TmpBB:
1105 // jmp_if_Y TBB
1106 // jmp FBB
1107 //
1108 // This requires creation of TmpBB after CurBB.
1109
1110 // Emit the LHS condition.
1111 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
1112
1113 // Emit the RHS condition into TmpBB.
1114 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1115 }
1116}
1117
1118/// If the set of cases should be emitted as a series of branches, return true.
1119/// If we should emit this as a bunch of and/or'd together conditions, return
1120/// false.
1121bool
1122SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
1123 if (Cases.size() != 2) return true;
1124
1125 // If this is two comparisons of the same values or'd or and'd together, they
1126 // will get folded into a single comparison, so don't emit two blocks.
1127 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1128 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1129 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1130 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1131 return false;
1132 }
1133
1134 return true;
1135}
1136
1137void SelectionDAGLowering::visitBr(BranchInst &I) {
1138 // Update machine-CFG edges.
1139 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1140
1141 // Figure out which block is immediately after the current one.
1142 MachineBasicBlock *NextBlock = 0;
1143 MachineFunction::iterator BBI = CurMBB;
1144 if (++BBI != CurMBB->getParent()->end())
1145 NextBlock = BBI;
1146
1147 if (I.isUnconditional()) {
1148 // Update machine-CFG edges.
1149 CurMBB->addSuccessor(Succ0MBB);
1150
1151 // If this is not a fall-through branch, emit the branch.
1152 if (Succ0MBB != NextBlock)
1153 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1154 DAG.getBasicBlock(Succ0MBB)));
1155 return;
1156 }
1157
1158 // If this condition is one of the special cases we handle, do special stuff
1159 // now.
1160 Value *CondVal = I.getCondition();
1161 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1162
1163 // If this is a series of conditions that are or'd or and'd together, emit
1164 // this as a sequence of branches instead of setcc's with and/or operations.
1165 // For example, instead of something like:
1166 // cmp A, B
1167 // C = seteq
1168 // cmp D, E
1169 // F = setle
1170 // or C, F
1171 // jnz foo
1172 // Emit:
1173 // cmp A, B
1174 // je foo
1175 // cmp D, E
1176 // jle foo
1177 //
1178 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
1179 if (BOp->hasOneUse() &&
1180 (BOp->getOpcode() == Instruction::And ||
1181 BOp->getOpcode() == Instruction::Or)) {
1182 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1183 // If the compares in later blocks need to use values not currently
1184 // exported from this block, export them now. This block should always
1185 // be the first entry.
1186 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
1187
1188 // Allow some cases to be rejected.
1189 if (ShouldEmitAsBranches(SwitchCases)) {
1190 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1191 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1192 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1193 }
1194
1195 // Emit the branch for this block.
1196 visitSwitchCase(SwitchCases[0]);
1197 SwitchCases.erase(SwitchCases.begin());
1198 return;
1199 }
1200
1201 // Okay, we decided not to do this, remove any inserted MBB's and clear
1202 // SwitchCases.
1203 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
1204 CurMBB->getParent()->erase(SwitchCases[i].ThisBB);
1205
1206 SwitchCases.clear();
1207 }
1208 }
1209
1210 // Create a CaseBlock record representing this branch.
1211 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(),
1212 NULL, Succ0MBB, Succ1MBB, CurMBB);
1213 // Use visitSwitchCase to actually insert the fast branch sequence for this
1214 // cond branch.
1215 visitSwitchCase(CB);
1216}
1217
1218/// visitSwitchCase - Emits the necessary code to represent a single node in
1219/// the binary search tree resulting from lowering a switch instruction.
1220void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) {
1221 SDValue Cond;
1222 SDValue CondLHS = getValue(CB.CmpLHS);
1223
1224 // Build the setcc now.
1225 if (CB.CmpMHS == NULL) {
1226 // Fold "(X == true)" to X and "(X == false)" to !X to
1227 // handle common cases produced by branch lowering.
1228 if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ)
1229 Cond = CondLHS;
1230 else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) {
1231 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
1232 Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True);
1233 } else
1234 Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
1235 } else {
1236 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1237
1238 uint64_t Low = cast<ConstantInt>(CB.CmpLHS)->getSExtValue();
1239 uint64_t High = cast<ConstantInt>(CB.CmpRHS)->getSExtValue();
1240
1241 SDValue CmpOp = getValue(CB.CmpMHS);
1242 MVT VT = CmpOp.getValueType();
1243
1244 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
1245 Cond = DAG.getSetCC(MVT::i1, CmpOp, DAG.getConstant(High, VT), ISD::SETLE);
1246 } else {
1247 SDValue SUB = DAG.getNode(ISD::SUB, VT, CmpOp, DAG.getConstant(Low, VT));
1248 Cond = DAG.getSetCC(MVT::i1, SUB,
1249 DAG.getConstant(High-Low, VT), ISD::SETULE);
1250 }
1251 }
1252
1253 // Update successor info
1254 CurMBB->addSuccessor(CB.TrueBB);
1255 CurMBB->addSuccessor(CB.FalseBB);
1256
1257 // Set NextBlock to be the MBB immediately after the current one, if any.
1258 // This is used to avoid emitting unnecessary branches to the next block.
1259 MachineBasicBlock *NextBlock = 0;
1260 MachineFunction::iterator BBI = CurMBB;
1261 if (++BBI != CurMBB->getParent()->end())
1262 NextBlock = BBI;
1263
1264 // If the lhs block is the next block, invert the condition so that we can
1265 // fall through to the lhs instead of the rhs block.
1266 if (CB.TrueBB == NextBlock) {
1267 std::swap(CB.TrueBB, CB.FalseBB);
1268 SDValue True = DAG.getConstant(1, Cond.getValueType());
1269 Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
1270 }
1271 SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), Cond,
1272 DAG.getBasicBlock(CB.TrueBB));
1273
1274 // If the branch was constant folded, fix up the CFG.
1275 if (BrCond.getOpcode() == ISD::BR) {
1276 CurMBB->removeSuccessor(CB.FalseBB);
1277 DAG.setRoot(BrCond);
1278 } else {
1279 // Otherwise, go ahead and insert the false branch.
1280 if (BrCond == getControlRoot())
1281 CurMBB->removeSuccessor(CB.TrueBB);
1282
1283 if (CB.FalseBB == NextBlock)
1284 DAG.setRoot(BrCond);
1285 else
1286 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond,
1287 DAG.getBasicBlock(CB.FalseBB)));
1288 }
1289}
1290
1291/// visitJumpTable - Emit JumpTable node in the current MBB
1292void SelectionDAGLowering::visitJumpTable(JumpTable &JT) {
1293 // Emit the code for the jump table
1294 assert(JT.Reg != -1U && "Should lower JT Header first!");
1295 MVT PTy = TLI.getPointerTy();
1296 SDValue Index = DAG.getCopyFromReg(getControlRoot(), JT.Reg, PTy);
1297 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
1298 DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1),
1299 Table, Index));
1300 return;
1301}
1302
1303/// visitJumpTableHeader - This function emits necessary code to produce index
1304/// in the JumpTable from switch case.
1305void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT,
1306 JumpTableHeader &JTH) {
1307 // Subtract the lowest switch case value from the value being switched on
1308 // and conditional branch to default mbb if the result is greater than the
1309 // difference between smallest and largest cases.
1310 SDValue SwitchOp = getValue(JTH.SValue);
1311 MVT VT = SwitchOp.getValueType();
1312 SDValue SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
1313 DAG.getConstant(JTH.First, VT));
1314
1315 // The SDNode we just created, which holds the value being switched on
1316 // minus the the smallest case value, needs to be copied to a virtual
1317 // register so it can be used as an index into the jump table in a
1318 // subsequent basic block. This value may be smaller or larger than the
1319 // target's pointer type, and therefore require extension or truncating.
1320 if (VT.bitsGT(TLI.getPointerTy()))
1321 SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB);
1322 else
1323 SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB);
1324
1325 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
1326 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), JumpTableReg, SwitchOp);
1327 JT.Reg = JumpTableReg;
1328
1329 // Emit the range check for the jump table, and branch to the default
1330 // block for the switch statement if the value being switched on exceeds
1331 // the largest case in the switch.
1332 SDValue CMP = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB,
1333 DAG.getConstant(JTH.Last-JTH.First,VT),
1334 ISD::SETUGT);
1335
1336 // Set NextBlock to be the MBB immediately after the current one, if any.
1337 // This is used to avoid emitting unnecessary branches to the next block.
1338 MachineBasicBlock *NextBlock = 0;
1339 MachineFunction::iterator BBI = CurMBB;
1340 if (++BBI != CurMBB->getParent()->end())
1341 NextBlock = BBI;
1342
1343 SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP,
1344 DAG.getBasicBlock(JT.Default));
1345
1346 if (JT.MBB == NextBlock)
1347 DAG.setRoot(BrCond);
1348 else
1349 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond,
1350 DAG.getBasicBlock(JT.MBB)));
1351
1352 return;
1353}
1354
1355/// visitBitTestHeader - This function emits necessary code to produce value
1356/// suitable for "bit tests"
1357void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) {
1358 // Subtract the minimum value
1359 SDValue SwitchOp = getValue(B.SValue);
1360 MVT VT = SwitchOp.getValueType();
1361 SDValue SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
1362 DAG.getConstant(B.First, VT));
1363
1364 // Check range
1365 SDValue RangeCmp = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB,
1366 DAG.getConstant(B.Range, VT),
1367 ISD::SETUGT);
1368
1369 SDValue ShiftOp;
1370 if (VT.bitsGT(TLI.getShiftAmountTy()))
1371 ShiftOp = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), SUB);
1372 else
1373 ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), SUB);
1374
1375 // Make desired shift
1376 SDValue SwitchVal = DAG.getNode(ISD::SHL, TLI.getPointerTy(),
1377 DAG.getConstant(1, TLI.getPointerTy()),
1378 ShiftOp);
1379
1380 unsigned SwitchReg = FuncInfo.MakeReg(TLI.getPointerTy());
1381 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), SwitchReg, SwitchVal);
1382 B.Reg = SwitchReg;
1383
1384 // Set NextBlock to be the MBB immediately after the current one, if any.
1385 // This is used to avoid emitting unnecessary branches to the next block.
1386 MachineBasicBlock *NextBlock = 0;
1387 MachineFunction::iterator BBI = CurMBB;
1388 if (++BBI != CurMBB->getParent()->end())
1389 NextBlock = BBI;
1390
1391 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1392
1393 CurMBB->addSuccessor(B.Default);
1394 CurMBB->addSuccessor(MBB);
1395
1396 SDValue BrRange = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, RangeCmp,
1397 DAG.getBasicBlock(B.Default));
1398
1399 if (MBB == NextBlock)
1400 DAG.setRoot(BrRange);
1401 else
1402 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, CopyTo,
1403 DAG.getBasicBlock(MBB)));
1404
1405 return;
1406}
1407
1408/// visitBitTestCase - this function produces one "bit test"
1409void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB,
1410 unsigned Reg,
1411 BitTestCase &B) {
1412 // Emit bit tests and jumps
1413 SDValue SwitchVal = DAG.getCopyFromReg(getControlRoot(), Reg,
1414 TLI.getPointerTy());
1415
1416 SDValue AndOp = DAG.getNode(ISD::AND, TLI.getPointerTy(), SwitchVal,
1417 DAG.getConstant(B.Mask, TLI.getPointerTy()));
1418 SDValue AndCmp = DAG.getSetCC(TLI.getSetCCResultType(AndOp), AndOp,
1419 DAG.getConstant(0, TLI.getPointerTy()),
1420 ISD::SETNE);
1421
1422 CurMBB->addSuccessor(B.TargetBB);
1423 CurMBB->addSuccessor(NextMBB);
1424
1425 SDValue BrAnd = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(),
1426 AndCmp, DAG.getBasicBlock(B.TargetBB));
1427
1428 // Set NextBlock to be the MBB immediately after the current one, if any.
1429 // This is used to avoid emitting unnecessary branches to the next block.
1430 MachineBasicBlock *NextBlock = 0;
1431 MachineFunction::iterator BBI = CurMBB;
1432 if (++BBI != CurMBB->getParent()->end())
1433 NextBlock = BBI;
1434
1435 if (NextMBB == NextBlock)
1436 DAG.setRoot(BrAnd);
1437 else
1438 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrAnd,
1439 DAG.getBasicBlock(NextMBB)));
1440
1441 return;
1442}
1443
1444void SelectionDAGLowering::visitInvoke(InvokeInst &I) {
1445 // Retrieve successors.
1446 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1447 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1448
1449 if (isa<InlineAsm>(I.getCalledValue()))
1450 visitInlineAsm(&I);
1451 else
1452 LowerCallTo(&I, getValue(I.getOperand(0)), false, LandingPad);
1453
1454 // If the value of the invoke is used outside of its defining block, make it
1455 // available as a virtual register.
1456 if (!I.use_empty()) {
1457 DenseMap<const Value*, unsigned>::iterator VMI = FuncInfo.ValueMap.find(&I);
1458 if (VMI != FuncInfo.ValueMap.end())
1459 CopyValueToVirtualRegister(&I, VMI->second);
1460 }
1461
1462 // Update successor info
1463 CurMBB->addSuccessor(Return);
1464 CurMBB->addSuccessor(LandingPad);
1465
1466 // Drop into normal successor.
1467 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1468 DAG.getBasicBlock(Return)));
1469}
1470
1471void SelectionDAGLowering::visitUnwind(UnwindInst &I) {
1472}
1473
1474/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1475/// small case ranges).
1476bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR,
1477 CaseRecVector& WorkList,
1478 Value* SV,
1479 MachineBasicBlock* Default) {
1480 Case& BackCase = *(CR.Range.second-1);
1481
1482 // Size is the number of Cases represented by this range.
1483 unsigned Size = CR.Range.second - CR.Range.first;
1484 if (Size > 3)
1485 return false;
1486
1487 // Get the MachineFunction which holds the current MBB. This is used when
1488 // inserting any additional MBBs necessary to represent the switch.
1489 MachineFunction *CurMF = CurMBB->getParent();
1490
1491 // Figure out which block is immediately after the current one.
1492 MachineBasicBlock *NextBlock = 0;
1493 MachineFunction::iterator BBI = CR.CaseBB;
1494
1495 if (++BBI != CurMBB->getParent()->end())
1496 NextBlock = BBI;
1497
1498 // TODO: If any two of the cases has the same destination, and if one value
1499 // is the same as the other, but has one bit unset that the other has set,
1500 // use bit manipulation to do two compares at once. For example:
1501 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
1502
1503 // Rearrange the case blocks so that the last one falls through if possible.
1504 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1505 // The last case block won't fall through into 'NextBlock' if we emit the
1506 // branches in this order. See if rearranging a case value would help.
1507 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1508 if (I->BB == NextBlock) {
1509 std::swap(*I, BackCase);
1510 break;
1511 }
1512 }
1513 }
1514
1515 // Create a CaseBlock record representing a conditional branch to
1516 // the Case's target mbb if the value being switched on SV is equal
1517 // to C.
1518 MachineBasicBlock *CurBlock = CR.CaseBB;
1519 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1520 MachineBasicBlock *FallThrough;
1521 if (I != E-1) {
1522 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1523 CurMF->insert(BBI, FallThrough);
1524 } else {
1525 // If the last case doesn't match, go to the default block.
1526 FallThrough = Default;
1527 }
1528
1529 Value *RHS, *LHS, *MHS;
1530 ISD::CondCode CC;
1531 if (I->High == I->Low) {
1532 // This is just small small case range :) containing exactly 1 case
1533 CC = ISD::SETEQ;
1534 LHS = SV; RHS = I->High; MHS = NULL;
1535 } else {
1536 CC = ISD::SETLE;
1537 LHS = I->Low; MHS = SV; RHS = I->High;
1538 }
1539 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
1540
1541 // If emitting the first comparison, just call visitSwitchCase to emit the
1542 // code into the current block. Otherwise, push the CaseBlock onto the
1543 // vector to be later processed by SDISel, and insert the node's MBB
1544 // before the next MBB.
1545 if (CurBlock == CurMBB)
1546 visitSwitchCase(CB);
1547 else
1548 SwitchCases.push_back(CB);
1549
1550 CurBlock = FallThrough;
1551 }
1552
1553 return true;
1554}
1555
1556static inline bool areJTsAllowed(const TargetLowering &TLI) {
1557 return !DisableJumpTables &&
1558 (TLI.isOperationLegal(ISD::BR_JT, MVT::Other) ||
1559 TLI.isOperationLegal(ISD::BRIND, MVT::Other));
1560}
1561
1562/// handleJTSwitchCase - Emit jumptable for current switch case range
1563bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR,
1564 CaseRecVector& WorkList,
1565 Value* SV,
1566 MachineBasicBlock* Default) {
1567 Case& FrontCase = *CR.Range.first;
1568 Case& BackCase = *(CR.Range.second-1);
1569
1570 int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue();
1571 int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue();
1572
1573 uint64_t TSize = 0;
1574 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1575 I!=E; ++I)
1576 TSize += I->size();
1577
1578 if (!areJTsAllowed(TLI) || TSize <= 3)
1579 return false;
1580
1581 double Density = (double)TSize / (double)((Last - First) + 1ULL);
1582 if (Density < 0.4)
1583 return false;
1584
1585 DOUT << "Lowering jump table\n"
1586 << "First entry: " << First << ". Last entry: " << Last << "\n"
1587 << "Size: " << TSize << ". Density: " << Density << "\n\n";
1588
1589 // Get the MachineFunction which holds the current MBB. This is used when
1590 // inserting any additional MBBs necessary to represent the switch.
1591 MachineFunction *CurMF = CurMBB->getParent();
1592
1593 // Figure out which block is immediately after the current one.
1594 MachineBasicBlock *NextBlock = 0;
1595 MachineFunction::iterator BBI = CR.CaseBB;
1596
1597 if (++BBI != CurMBB->getParent()->end())
1598 NextBlock = BBI;
1599
1600 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1601
1602 // Create a new basic block to hold the code for loading the address
1603 // of the jump table, and jumping to it. Update successor information;
1604 // we will either branch to the default case for the switch, or the jump
1605 // table.
1606 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1607 CurMF->insert(BBI, JumpTableBB);
1608 CR.CaseBB->addSuccessor(Default);
1609 CR.CaseBB->addSuccessor(JumpTableBB);
1610
1611 // Build a vector of destination BBs, corresponding to each target
1612 // of the jump table. If the value of the jump table slot corresponds to
1613 // a case statement, push the case's BB onto the vector, otherwise, push
1614 // the default BB.
1615 std::vector<MachineBasicBlock*> DestBBs;
1616 int64_t TEI = First;
1617 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
1618 int64_t Low = cast<ConstantInt>(I->Low)->getSExtValue();
1619 int64_t High = cast<ConstantInt>(I->High)->getSExtValue();
1620
1621 if ((Low <= TEI) && (TEI <= High)) {
1622 DestBBs.push_back(I->BB);
1623 if (TEI==High)
1624 ++I;
1625 } else {
1626 DestBBs.push_back(Default);
1627 }
1628 }
1629
1630 // Update successor info. Add one edge to each unique successor.
1631 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1632 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
1633 E = DestBBs.end(); I != E; ++I) {
1634 if (!SuccsHandled[(*I)->getNumber()]) {
1635 SuccsHandled[(*I)->getNumber()] = true;
1636 JumpTableBB->addSuccessor(*I);
1637 }
1638 }
1639
1640 // Create a jump table index for this jump table, or return an existing
1641 // one.
1642 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
1643
1644 // Set the jump table information so that we can codegen it as a second
1645 // MachineBasicBlock
1646 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1647 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1648 if (CR.CaseBB == CurMBB)
1649 visitJumpTableHeader(JT, JTH);
1650
1651 JTCases.push_back(JumpTableBlock(JTH, JT));
1652
1653 return true;
1654}
1655
1656/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1657/// 2 subtrees.
1658bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR,
1659 CaseRecVector& WorkList,
1660 Value* SV,
1661 MachineBasicBlock* Default) {
1662 // Get the MachineFunction which holds the current MBB. This is used when
1663 // inserting any additional MBBs necessary to represent the switch.
1664 MachineFunction *CurMF = CurMBB->getParent();
1665
1666 // Figure out which block is immediately after the current one.
1667 MachineBasicBlock *NextBlock = 0;
1668 MachineFunction::iterator BBI = CR.CaseBB;
1669
1670 if (++BBI != CurMBB->getParent()->end())
1671 NextBlock = BBI;
1672
1673 Case& FrontCase = *CR.Range.first;
1674 Case& BackCase = *(CR.Range.second-1);
1675 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1676
1677 // Size is the number of Cases represented by this range.
1678 unsigned Size = CR.Range.second - CR.Range.first;
1679
1680 int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue();
1681 int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue();
1682 double FMetric = 0;
1683 CaseItr Pivot = CR.Range.first + Size/2;
1684
1685 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1686 // (heuristically) allow us to emit JumpTable's later.
1687 uint64_t TSize = 0;
1688 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1689 I!=E; ++I)
1690 TSize += I->size();
1691
1692 uint64_t LSize = FrontCase.size();
1693 uint64_t RSize = TSize-LSize;
1694 DOUT << "Selecting best pivot: \n"
1695 << "First: " << First << ", Last: " << Last <<"\n"
1696 << "LSize: " << LSize << ", RSize: " << RSize << "\n";
1697 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1698 J!=E; ++I, ++J) {
1699 int64_t LEnd = cast<ConstantInt>(I->High)->getSExtValue();
1700 int64_t RBegin = cast<ConstantInt>(J->Low)->getSExtValue();
1701 assert((RBegin-LEnd>=1) && "Invalid case distance");
1702 double LDensity = (double)LSize / (double)((LEnd - First) + 1ULL);
1703 double RDensity = (double)RSize / (double)((Last - RBegin) + 1ULL);
1704 double Metric = Log2_64(RBegin-LEnd)*(LDensity+RDensity);
1705 // Should always split in some non-trivial place
1706 DOUT <<"=>Step\n"
1707 << "LEnd: " << LEnd << ", RBegin: " << RBegin << "\n"
1708 << "LDensity: " << LDensity << ", RDensity: " << RDensity << "\n"
1709 << "Metric: " << Metric << "\n";
1710 if (FMetric < Metric) {
1711 Pivot = J;
1712 FMetric = Metric;
1713 DOUT << "Current metric set to: " << FMetric << "\n";
1714 }
1715
1716 LSize += J->size();
1717 RSize -= J->size();
1718 }
1719 if (areJTsAllowed(TLI)) {
1720 // If our case is dense we *really* should handle it earlier!
1721 assert((FMetric > 0) && "Should handle dense range earlier!");
1722 } else {
1723 Pivot = CR.Range.first + Size/2;
1724 }
1725
1726 CaseRange LHSR(CR.Range.first, Pivot);
1727 CaseRange RHSR(Pivot, CR.Range.second);
1728 Constant *C = Pivot->Low;
1729 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
1730
1731 // We know that we branch to the LHS if the Value being switched on is
1732 // less than the Pivot value, C. We use this to optimize our binary
1733 // tree a bit, by recognizing that if SV is greater than or equal to the
1734 // LHS's Case Value, and that Case Value is exactly one less than the
1735 // Pivot's Value, then we can branch directly to the LHS's Target,
1736 // rather than creating a leaf node for it.
1737 if ((LHSR.second - LHSR.first) == 1 &&
1738 LHSR.first->High == CR.GE &&
1739 cast<ConstantInt>(C)->getSExtValue() ==
1740 (cast<ConstantInt>(CR.GE)->getSExtValue() + 1LL)) {
1741 TrueBB = LHSR.first->BB;
1742 } else {
1743 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1744 CurMF->insert(BBI, TrueBB);
1745 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
1746 }
1747
1748 // Similar to the optimization above, if the Value being switched on is
1749 // known to be less than the Constant CR.LT, and the current Case Value
1750 // is CR.LT - 1, then we can branch directly to the target block for
1751 // the current Case Value, rather than emitting a RHS leaf node for it.
1752 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
1753 cast<ConstantInt>(RHSR.first->Low)->getSExtValue() ==
1754 (cast<ConstantInt>(CR.LT)->getSExtValue() - 1LL)) {
1755 FalseBB = RHSR.first->BB;
1756 } else {
1757 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1758 CurMF->insert(BBI, FalseBB);
1759 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
1760 }
1761
1762 // Create a CaseBlock record representing a conditional branch to
1763 // the LHS node if the value being switched on SV is less than C.
1764 // Otherwise, branch to LHS.
1765 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1766
1767 if (CR.CaseBB == CurMBB)
1768 visitSwitchCase(CB);
1769 else
1770 SwitchCases.push_back(CB);
1771
1772 return true;
1773}
1774
1775/// handleBitTestsSwitchCase - if current case range has few destination and
1776/// range span less, than machine word bitwidth, encode case range into series
1777/// of masks and emit bit tests with these masks.
1778bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1779 CaseRecVector& WorkList,
1780 Value* SV,
1781 MachineBasicBlock* Default){
1782 unsigned IntPtrBits = TLI.getPointerTy().getSizeInBits();
1783
1784 Case& FrontCase = *CR.Range.first;
1785 Case& BackCase = *(CR.Range.second-1);
1786
1787 // Get the MachineFunction which holds the current MBB. This is used when
1788 // inserting any additional MBBs necessary to represent the switch.
1789 MachineFunction *CurMF = CurMBB->getParent();
1790
1791 unsigned numCmps = 0;
1792 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1793 I!=E; ++I) {
1794 // Single case counts one, case range - two.
1795 if (I->Low == I->High)
1796 numCmps +=1;
1797 else
1798 numCmps +=2;
1799 }
1800
1801 // Count unique destinations
1802 SmallSet<MachineBasicBlock*, 4> Dests;
1803 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1804 Dests.insert(I->BB);
1805 if (Dests.size() > 3)
1806 // Don't bother the code below, if there are too much unique destinations
1807 return false;
1808 }
1809 DOUT << "Total number of unique destinations: " << Dests.size() << "\n"
1810 << "Total number of comparisons: " << numCmps << "\n";
1811
1812 // Compute span of values.
1813 Constant* minValue = FrontCase.Low;
1814 Constant* maxValue = BackCase.High;
1815 uint64_t range = cast<ConstantInt>(maxValue)->getSExtValue() -
1816 cast<ConstantInt>(minValue)->getSExtValue();
1817 DOUT << "Compare range: " << range << "\n"
1818 << "Low bound: " << cast<ConstantInt>(minValue)->getSExtValue() << "\n"
1819 << "High bound: " << cast<ConstantInt>(maxValue)->getSExtValue() << "\n";
1820
1821 if (range>=IntPtrBits ||
1822 (!(Dests.size() == 1 && numCmps >= 3) &&
1823 !(Dests.size() == 2 && numCmps >= 5) &&
1824 !(Dests.size() >= 3 && numCmps >= 6)))
1825 return false;
1826
1827 DOUT << "Emitting bit tests\n";
1828 int64_t lowBound = 0;
1829
1830 // Optimize the case where all the case values fit in a
1831 // word without having to subtract minValue. In this case,
1832 // we can optimize away the subtraction.
1833 if (cast<ConstantInt>(minValue)->getSExtValue() >= 0 &&
1834 cast<ConstantInt>(maxValue)->getSExtValue() < IntPtrBits) {
1835 range = cast<ConstantInt>(maxValue)->getSExtValue();
1836 } else {
1837 lowBound = cast<ConstantInt>(minValue)->getSExtValue();
1838 }
1839
1840 CaseBitsVector CasesBits;
1841 unsigned i, count = 0;
1842
1843 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1844 MachineBasicBlock* Dest = I->BB;
1845 for (i = 0; i < count; ++i)
1846 if (Dest == CasesBits[i].BB)
1847 break;
1848
1849 if (i == count) {
1850 assert((count < 3) && "Too much destinations to test!");
1851 CasesBits.push_back(CaseBits(0, Dest, 0));
1852 count++;
1853 }
1854
1855 uint64_t lo = cast<ConstantInt>(I->Low)->getSExtValue() - lowBound;
1856 uint64_t hi = cast<ConstantInt>(I->High)->getSExtValue() - lowBound;
1857
1858 for (uint64_t j = lo; j <= hi; j++) {
1859 CasesBits[i].Mask |= 1ULL << j;
1860 CasesBits[i].Bits++;
1861 }
1862
1863 }
1864 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
1865
1866 BitTestInfo BTC;
1867
1868 // Figure out which block is immediately after the current one.
1869 MachineFunction::iterator BBI = CR.CaseBB;
1870 ++BBI;
1871
1872 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1873
1874 DOUT << "Cases:\n";
1875 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
1876 DOUT << "Mask: " << CasesBits[i].Mask << ", Bits: " << CasesBits[i].Bits
1877 << ", BB: " << CasesBits[i].BB << "\n";
1878
1879 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1880 CurMF->insert(BBI, CaseBB);
1881 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1882 CaseBB,
1883 CasesBits[i].BB));
1884 }
1885
1886 BitTestBlock BTB(lowBound, range, SV,
1887 -1U, (CR.CaseBB == CurMBB),
1888 CR.CaseBB, Default, BTC);
1889
1890 if (CR.CaseBB == CurMBB)
1891 visitBitTestHeader(BTB);
1892
1893 BitTestCases.push_back(BTB);
1894
1895 return true;
1896}
1897
1898
1899/// Clusterify - Transform simple list of Cases into list of CaseRange's
1900unsigned SelectionDAGLowering::Clusterify(CaseVector& Cases,
1901 const SwitchInst& SI) {
1902 unsigned numCmps = 0;
1903
1904 // Start with "simple" cases
1905 for (unsigned i = 1; i < SI.getNumSuccessors(); ++i) {
1906 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1907 Cases.push_back(Case(SI.getSuccessorValue(i),
1908 SI.getSuccessorValue(i),
1909 SMBB));
1910 }
1911 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1912
1913 // Merge case into clusters
1914 if (Cases.size()>=2)
1915 // Must recompute end() each iteration because it may be
1916 // invalidated by erase if we hold on to it
1917 for (CaseItr I=Cases.begin(), J=++(Cases.begin()); J!=Cases.end(); ) {
1918 int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue();
1919 int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue();
1920 MachineBasicBlock* nextBB = J->BB;
1921 MachineBasicBlock* currentBB = I->BB;
1922
1923 // If the two neighboring cases go to the same destination, merge them
1924 // into a single case.
1925 if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
1926 I->High = J->High;
1927 J = Cases.erase(J);
1928 } else {
1929 I = J++;
1930 }
1931 }
1932
1933 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1934 if (I->Low != I->High)
1935 // A range counts double, since it requires two compares.
1936 ++numCmps;
1937 }
1938
1939 return numCmps;
1940}
1941
1942void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {
1943 // Figure out which block is immediately after the current one.
1944 MachineBasicBlock *NextBlock = 0;
1945 MachineFunction::iterator BBI = CurMBB;
1946
1947 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1948
1949 // If there is only the default destination, branch to it if it is not the
1950 // next basic block. Otherwise, just fall through.
1951 if (SI.getNumOperands() == 2) {
1952 // Update machine-CFG edges.
1953
1954 // If this is not a fall-through branch, emit the branch.
1955 CurMBB->addSuccessor(Default);
1956 if (Default != NextBlock)
1957 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1958 DAG.getBasicBlock(Default)));
1959
1960 return;
1961 }
1962
1963 // If there are any non-default case statements, create a vector of Cases
1964 // representing each one, and sort the vector so that we can efficiently
1965 // create a binary search tree from them.
1966 CaseVector Cases;
1967 unsigned numCmps = Clusterify(Cases, SI);
1968 DOUT << "Clusterify finished. Total clusters: " << Cases.size()
1969 << ". Total compares: " << numCmps << "\n";
1970
1971 // Get the Value to be switched on and default basic blocks, which will be
1972 // inserted into CaseBlock records, representing basic blocks in the binary
1973 // search tree.
1974 Value *SV = SI.getOperand(0);
1975
1976 // Push the initial CaseRec onto the worklist
1977 CaseRecVector WorkList;
1978 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
1979
1980 while (!WorkList.empty()) {
1981 // Grab a record representing a case range to process off the worklist
1982 CaseRec CR = WorkList.back();
1983 WorkList.pop_back();
1984
1985 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
1986 continue;
1987
1988 // If the range has few cases (two or less) emit a series of specific
1989 // tests.
1990 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
1991 continue;
1992
1993 // If the switch has more than 5 blocks, and at least 40% dense, and the
1994 // target supports indirect branches, then emit a jump table rather than
1995 // lowering the switch to a binary tree of conditional branches.
1996 if (handleJTSwitchCase(CR, WorkList, SV, Default))
1997 continue;
1998
1999 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2000 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2001 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2002 }
2003}
2004
2005
2006void SelectionDAGLowering::visitSub(User &I) {
2007 // -0.0 - X --> fneg
2008 const Type *Ty = I.getType();
2009 if (isa<VectorType>(Ty)) {
2010 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2011 const VectorType *DestTy = cast<VectorType>(I.getType());
2012 const Type *ElTy = DestTy->getElementType();
2013 if (ElTy->isFloatingPoint()) {
2014 unsigned VL = DestTy->getNumElements();
2015 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
2016 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
2017 if (CV == CNZ) {
2018 SDValue Op2 = getValue(I.getOperand(1));
2019 setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
2020 return;
2021 }
2022 }
2023 }
2024 }
2025 if (Ty->isFloatingPoint()) {
2026 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
2027 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
2028 SDValue Op2 = getValue(I.getOperand(1));
2029 setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
2030 return;
2031 }
2032 }
2033
2034 visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB);
2035}
2036
2037void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
2038 SDValue Op1 = getValue(I.getOperand(0));
2039 SDValue Op2 = getValue(I.getOperand(1));
2040
2041 setValue(&I, DAG.getNode(OpCode, Op1.getValueType(), Op1, Op2));
2042}
2043
2044void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2045 SDValue Op1 = getValue(I.getOperand(0));
2046 SDValue Op2 = getValue(I.getOperand(1));
2047 if (!isa<VectorType>(I.getType())) {
2048 if (TLI.getShiftAmountTy().bitsLT(Op2.getValueType()))
2049 Op2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Op2);
2050 else if (TLI.getShiftAmountTy().bitsGT(Op2.getValueType()))
2051 Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2);
2052 }
2053
2054 setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
2055}
2056
2057void SelectionDAGLowering::visitICmp(User &I) {
2058 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2059 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2060 predicate = IC->getPredicate();
2061 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2062 predicate = ICmpInst::Predicate(IC->getPredicate());
2063 SDValue Op1 = getValue(I.getOperand(0));
2064 SDValue Op2 = getValue(I.getOperand(1));
2065 ISD::CondCode Opcode;
2066 switch (predicate) {
2067 case ICmpInst::ICMP_EQ : Opcode = ISD::SETEQ; break;
2068 case ICmpInst::ICMP_NE : Opcode = ISD::SETNE; break;
2069 case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break;
2070 case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break;
2071 case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break;
2072 case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break;
2073 case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break;
2074 case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break;
2075 case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break;
2076 case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break;
2077 default:
2078 assert(!"Invalid ICmp predicate value");
2079 Opcode = ISD::SETEQ;
2080 break;
2081 }
2082 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
2083}
2084
2085void SelectionDAGLowering::visitFCmp(User &I) {
2086 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2087 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2088 predicate = FC->getPredicate();
2089 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2090 predicate = FCmpInst::Predicate(FC->getPredicate());
2091 SDValue Op1 = getValue(I.getOperand(0));
2092 SDValue Op2 = getValue(I.getOperand(1));
2093 ISD::CondCode Condition, FOC, FPC;
2094 switch (predicate) {
2095 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
2096 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
2097 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
2098 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
2099 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
2100 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
2101 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
2102 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
2103 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
2104 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
2105 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
2106 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
2107 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
2108 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
2109 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
2110 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
2111 default:
2112 assert(!"Invalid FCmp predicate value");
2113 FOC = FPC = ISD::SETFALSE;
2114 break;
2115 }
2116 if (FiniteOnlyFPMath())
2117 Condition = FOC;
2118 else
2119 Condition = FPC;
2120 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition));
2121}
2122
2123void SelectionDAGLowering::visitVICmp(User &I) {
2124 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2125 if (VICmpInst *IC = dyn_cast<VICmpInst>(&I))
2126 predicate = IC->getPredicate();
2127 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2128 predicate = ICmpInst::Predicate(IC->getPredicate());
2129 SDValue Op1 = getValue(I.getOperand(0));
2130 SDValue Op2 = getValue(I.getOperand(1));
2131 ISD::CondCode Opcode;
2132 switch (predicate) {
2133 case ICmpInst::ICMP_EQ : Opcode = ISD::SETEQ; break;
2134 case ICmpInst::ICMP_NE : Opcode = ISD::SETNE; break;
2135 case ICmpInst::ICMP_UGT : Opcode = ISD::SETUGT; break;
2136 case ICmpInst::ICMP_UGE : Opcode = ISD::SETUGE; break;
2137 case ICmpInst::ICMP_ULT : Opcode = ISD::SETULT; break;
2138 case ICmpInst::ICMP_ULE : Opcode = ISD::SETULE; break;
2139 case ICmpInst::ICMP_SGT : Opcode = ISD::SETGT; break;
2140 case ICmpInst::ICMP_SGE : Opcode = ISD::SETGE; break;
2141 case ICmpInst::ICMP_SLT : Opcode = ISD::SETLT; break;
2142 case ICmpInst::ICMP_SLE : Opcode = ISD::SETLE; break;
2143 default:
2144 assert(!"Invalid ICmp predicate value");
2145 Opcode = ISD::SETEQ;
2146 break;
2147 }
2148 setValue(&I, DAG.getVSetCC(Op1.getValueType(), Op1, Op2, Opcode));
2149}
2150
2151void SelectionDAGLowering::visitVFCmp(User &I) {
2152 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2153 if (VFCmpInst *FC = dyn_cast<VFCmpInst>(&I))
2154 predicate = FC->getPredicate();
2155 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2156 predicate = FCmpInst::Predicate(FC->getPredicate());
2157 SDValue Op1 = getValue(I.getOperand(0));
2158 SDValue Op2 = getValue(I.getOperand(1));
2159 ISD::CondCode Condition, FOC, FPC;
2160 switch (predicate) {
2161 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
2162 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
2163 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
2164 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
2165 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
2166 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
2167 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
2168 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
2169 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
2170 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
2171 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
2172 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
2173 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
2174 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
2175 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
2176 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
2177 default:
2178 assert(!"Invalid VFCmp predicate value");
2179 FOC = FPC = ISD::SETFALSE;
2180 break;
2181 }
2182 if (FiniteOnlyFPMath())
2183 Condition = FOC;
2184 else
2185 Condition = FPC;
2186
2187 MVT DestVT = TLI.getValueType(I.getType());
2188
2189 setValue(&I, DAG.getVSetCC(DestVT, Op1, Op2, Condition));
2190}
2191
2192void SelectionDAGLowering::visitSelect(User &I) {
2193 SDValue Cond = getValue(I.getOperand(0));
2194 SDValue TrueVal = getValue(I.getOperand(1));
2195 SDValue FalseVal = getValue(I.getOperand(2));
2196 setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
2197 TrueVal, FalseVal));
2198}
2199
2200
2201void SelectionDAGLowering::visitTrunc(User &I) {
2202 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2203 SDValue N = getValue(I.getOperand(0));
2204 MVT DestVT = TLI.getValueType(I.getType());
2205 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
2206}
2207
2208void SelectionDAGLowering::visitZExt(User &I) {
2209 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2210 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2211 SDValue N = getValue(I.getOperand(0));
2212 MVT DestVT = TLI.getValueType(I.getType());
2213 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
2214}
2215
2216void SelectionDAGLowering::visitSExt(User &I) {
2217 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2218 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2219 SDValue N = getValue(I.getOperand(0));
2220 MVT DestVT = TLI.getValueType(I.getType());
2221 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N));
2222}
2223
2224void SelectionDAGLowering::visitFPTrunc(User &I) {
2225 // FPTrunc is never a no-op cast, no need to check
2226 SDValue N = getValue(I.getOperand(0));
2227 MVT DestVT = TLI.getValueType(I.getType());
2228 setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N, DAG.getIntPtrConstant(0)));
2229}
2230
2231void SelectionDAGLowering::visitFPExt(User &I){
2232 // FPTrunc is never a no-op cast, no need to check
2233 SDValue N = getValue(I.getOperand(0));
2234 MVT DestVT = TLI.getValueType(I.getType());
2235 setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N));
2236}
2237
2238void SelectionDAGLowering::visitFPToUI(User &I) {
2239 // FPToUI is never a no-op cast, no need to check
2240 SDValue N = getValue(I.getOperand(0));
2241 MVT DestVT = TLI.getValueType(I.getType());
2242 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N));
2243}
2244
2245void SelectionDAGLowering::visitFPToSI(User &I) {
2246 // FPToSI is never a no-op cast, no need to check
2247 SDValue N = getValue(I.getOperand(0));
2248 MVT DestVT = TLI.getValueType(I.getType());
2249 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N));
2250}
2251
2252void SelectionDAGLowering::visitUIToFP(User &I) {
2253 // UIToFP is never a no-op cast, no need to check
2254 SDValue N = getValue(I.getOperand(0));
2255 MVT DestVT = TLI.getValueType(I.getType());
2256 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N));
2257}
2258
2259void SelectionDAGLowering::visitSIToFP(User &I){
2260 // UIToFP is never a no-op cast, no need to check
2261 SDValue N = getValue(I.getOperand(0));
2262 MVT DestVT = TLI.getValueType(I.getType());
2263 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N));
2264}
2265
2266void SelectionDAGLowering::visitPtrToInt(User &I) {
2267 // What to do depends on the size of the integer and the size of the pointer.
2268 // We can either truncate, zero extend, or no-op, accordingly.
2269 SDValue N = getValue(I.getOperand(0));
2270 MVT SrcVT = N.getValueType();
2271 MVT DestVT = TLI.getValueType(I.getType());
2272 SDValue Result;
2273 if (DestVT.bitsLT(SrcVT))
2274 Result = DAG.getNode(ISD::TRUNCATE, DestVT, N);
2275 else
2276 // Note: ZERO_EXTEND can handle cases where the sizes are equal too
2277 Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N);
2278 setValue(&I, Result);
2279}
2280
2281void SelectionDAGLowering::visitIntToPtr(User &I) {
2282 // What to do depends on the size of the integer and the size of the pointer.
2283 // We can either truncate, zero extend, or no-op, accordingly.
2284 SDValue N = getValue(I.getOperand(0));
2285 MVT SrcVT = N.getValueType();
2286 MVT DestVT = TLI.getValueType(I.getType());
2287 if (DestVT.bitsLT(SrcVT))
2288 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
2289 else
2290 // Note: ZERO_EXTEND can handle cases where the sizes are equal too
2291 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
2292}
2293
2294void SelectionDAGLowering::visitBitCast(User &I) {
2295 SDValue N = getValue(I.getOperand(0));
2296 MVT DestVT = TLI.getValueType(I.getType());
2297
2298 // BitCast assures us that source and destination are the same size so this
2299 // is either a BIT_CONVERT or a no-op.
2300 if (DestVT != N.getValueType())
2301 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types
2302 else
2303 setValue(&I, N); // noop cast.
2304}
2305
2306void SelectionDAGLowering::visitInsertElement(User &I) {
2307 SDValue InVec = getValue(I.getOperand(0));
2308 SDValue InVal = getValue(I.getOperand(1));
2309 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
2310 getValue(I.getOperand(2)));
2311
2312 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT,
2313 TLI.getValueType(I.getType()),
2314 InVec, InVal, InIdx));
2315}
2316
2317void SelectionDAGLowering::visitExtractElement(User &I) {
2318 SDValue InVec = getValue(I.getOperand(0));
2319 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
2320 getValue(I.getOperand(1)));
2321 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
2322 TLI.getValueType(I.getType()), InVec, InIdx));
2323}
2324
2325void SelectionDAGLowering::visitShuffleVector(User &I) {
2326 SDValue V1 = getValue(I.getOperand(0));
2327 SDValue V2 = getValue(I.getOperand(1));
2328 SDValue Mask = getValue(I.getOperand(2));
2329
2330 setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE,
2331 TLI.getValueType(I.getType()),
2332 V1, V2, Mask));
2333}
2334
2335void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) {
2336 const Value *Op0 = I.getOperand(0);
2337 const Value *Op1 = I.getOperand(1);
2338 const Type *AggTy = I.getType();
2339 const Type *ValTy = Op1->getType();
2340 bool IntoUndef = isa<UndefValue>(Op0);
2341 bool FromUndef = isa<UndefValue>(Op1);
2342
2343 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2344 I.idx_begin(), I.idx_end());
2345
2346 SmallVector<MVT, 4> AggValueVTs;
2347 ComputeValueVTs(TLI, AggTy, AggValueVTs);
2348 SmallVector<MVT, 4> ValValueVTs;
2349 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2350
2351 unsigned NumAggValues = AggValueVTs.size();
2352 unsigned NumValValues = ValValueVTs.size();
2353 SmallVector<SDValue, 4> Values(NumAggValues);
2354
2355 SDValue Agg = getValue(Op0);
2356 SDValue Val = getValue(Op1);
2357 unsigned i = 0;
2358 // Copy the beginning value(s) from the original aggregate.
2359 for (; i != LinearIndex; ++i)
2360 Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2361 SDValue(Agg.getNode(), Agg.getResNo() + i);
2362 // Copy values from the inserted value(s).
2363 for (; i != LinearIndex + NumValValues; ++i)
2364 Values[i] = FromUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2365 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2366 // Copy remaining value(s) from the original aggregate.
2367 for (; i != NumAggValues; ++i)
2368 Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2369 SDValue(Agg.getNode(), Agg.getResNo() + i);
2370
2371 setValue(&I, DAG.getMergeValues(DAG.getVTList(&AggValueVTs[0], NumAggValues),
2372 &Values[0], NumAggValues));
2373}
2374
2375void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) {
2376 const Value *Op0 = I.getOperand(0);
2377 const Type *AggTy = Op0->getType();
2378 const Type *ValTy = I.getType();
2379 bool OutOfUndef = isa<UndefValue>(Op0);
2380
2381 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2382 I.idx_begin(), I.idx_end());
2383
2384 SmallVector<MVT, 4> ValValueVTs;
2385 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2386
2387 unsigned NumValValues = ValValueVTs.size();
2388 SmallVector<SDValue, 4> Values(NumValValues);
2389
2390 SDValue Agg = getValue(Op0);
2391 // Copy out the selected value(s).
2392 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2393 Values[i - LinearIndex] =
2394 OutOfUndef ? DAG.getNode(ISD::UNDEF, Agg.getNode()->getValueType(Agg.getResNo() + i)) :
2395 SDValue(Agg.getNode(), Agg.getResNo() + i);
2396
2397 setValue(&I, DAG.getMergeValues(DAG.getVTList(&ValValueVTs[0], NumValValues),
2398 &Values[0], NumValValues));
2399}
2400
2401
2402void SelectionDAGLowering::visitGetElementPtr(User &I) {
2403 SDValue N = getValue(I.getOperand(0));
2404 const Type *Ty = I.getOperand(0)->getType();
2405
2406 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2407 OI != E; ++OI) {
2408 Value *Idx = *OI;
2409 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2410 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2411 if (Field) {
2412 // N = N + Offset
2413 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
2414 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
2415 DAG.getIntPtrConstant(Offset));
2416 }
2417 Ty = StTy->getElementType(Field);
2418 } else {
2419 Ty = cast<SequentialType>(Ty)->getElementType();
2420
2421 // If this is a constant subscript, handle it quickly.
2422 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2423 if (CI->getZExtValue() == 0) continue;
2424 uint64_t Offs =
2425 TD->getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
2426 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
2427 DAG.getIntPtrConstant(Offs));
2428 continue;
2429 }
2430
2431 // N = N + Idx * ElementSize;
2432 uint64_t ElementSize = TD->getABITypeSize(Ty);
2433 SDValue IdxN = getValue(Idx);
2434
2435 // If the index is smaller or larger than intptr_t, truncate or extend
2436 // it.
2437 if (IdxN.getValueType().bitsLT(N.getValueType()))
2438 IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN);
2439 else if (IdxN.getValueType().bitsGT(N.getValueType()))
2440 IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN);
2441
2442 // If this is a multiply by a power of two, turn it into a shl
2443 // immediately. This is a very common case.
2444 if (ElementSize != 1) {
2445 if (isPowerOf2_64(ElementSize)) {
2446 unsigned Amt = Log2_64(ElementSize);
2447 IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN,
2448 DAG.getConstant(Amt, TLI.getShiftAmountTy()));
2449 } else {
2450 SDValue Scale = DAG.getIntPtrConstant(ElementSize);
2451 IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
2452 }
2453 }
2454
2455 N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
2456 }
2457 }
2458 setValue(&I, N);
2459}
2460
2461void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2462 // If this is a fixed sized alloca in the entry block of the function,
2463 // allocate it statically on the stack.
2464 if (FuncInfo.StaticAllocaMap.count(&I))
2465 return; // getValue will auto-populate this.
2466
2467 const Type *Ty = I.getAllocatedType();
2468 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
2469 unsigned Align =
2470 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2471 I.getAlignment());
2472
2473 SDValue AllocSize = getValue(I.getArraySize());
2474 MVT IntPtr = TLI.getPointerTy();
2475 if (IntPtr.bitsLT(AllocSize.getValueType()))
2476 AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize);
2477 else if (IntPtr.bitsGT(AllocSize.getValueType()))
2478 AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize);
2479
2480 AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
2481 DAG.getIntPtrConstant(TySize));
2482
2483 // Handle alignment. If the requested alignment is less than or equal to
2484 // the stack alignment, ignore it. If the size is greater than or equal to
2485 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2486 unsigned StackAlign =
2487 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2488 if (Align <= StackAlign)
2489 Align = 0;
2490
2491 // Round the size of the allocation up to the stack alignment size
2492 // by add SA-1 to the size.
2493 AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
2494 DAG.getIntPtrConstant(StackAlign-1));
2495 // Mask out the low bits for alignment purposes.
2496 AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
2497 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2498
2499 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
2500 const MVT *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(),
2501 MVT::Other);
2502 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3);
2503 setValue(&I, DSA);
2504 DAG.setRoot(DSA.getValue(1));
2505
2506 // Inform the Frame Information that we have just allocated a variable-sized
2507 // object.
2508 CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
2509}
2510
2511void SelectionDAGLowering::visitLoad(LoadInst &I) {
2512 const Value *SV = I.getOperand(0);
2513 SDValue Ptr = getValue(SV);
2514
2515 const Type *Ty = I.getType();
2516 bool isVolatile = I.isVolatile();
2517 unsigned Alignment = I.getAlignment();
2518
2519 SmallVector<MVT, 4> ValueVTs;
2520 SmallVector<uint64_t, 4> Offsets;
2521 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2522 unsigned NumValues = ValueVTs.size();
2523 if (NumValues == 0)
2524 return;
2525
2526 SDValue Root;
2527 bool ConstantMemory = false;
2528 if (I.isVolatile())
2529 // Serialize volatile loads with other side effects.
2530 Root = getRoot();
2531 else if (AA->pointsToConstantMemory(SV)) {
2532 // Do not serialize (non-volatile) loads of constant memory with anything.
2533 Root = DAG.getEntryNode();
2534 ConstantMemory = true;
2535 } else {
2536 // Do not serialize non-volatile loads against each other.
2537 Root = DAG.getRoot();
2538 }
2539
2540 SmallVector<SDValue, 4> Values(NumValues);
2541 SmallVector<SDValue, 4> Chains(NumValues);
2542 MVT PtrVT = Ptr.getValueType();
2543 for (unsigned i = 0; i != NumValues; ++i) {
2544 SDValue L = DAG.getLoad(ValueVTs[i], Root,
2545 DAG.getNode(ISD::ADD, PtrVT, Ptr,
2546 DAG.getConstant(Offsets[i], PtrVT)),
2547 SV, Offsets[i],
2548 isVolatile, Alignment);
2549 Values[i] = L;
2550 Chains[i] = L.getValue(1);
2551 }
2552
2553 if (!ConstantMemory) {
2554 SDValue Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2555 &Chains[0], NumValues);
2556 if (isVolatile)
2557 DAG.setRoot(Chain);
2558 else
2559 PendingLoads.push_back(Chain);
2560 }
2561
2562 setValue(&I, DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], NumValues),
2563 &Values[0], NumValues));
2564}
2565
2566
2567void SelectionDAGLowering::visitStore(StoreInst &I) {
2568 Value *SrcV = I.getOperand(0);
2569 Value *PtrV = I.getOperand(1);
2570
2571 SmallVector<MVT, 4> ValueVTs;
2572 SmallVector<uint64_t, 4> Offsets;
2573 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2574 unsigned NumValues = ValueVTs.size();
2575 if (NumValues == 0)
2576 return;
2577
2578 // Get the lowered operands. Note that we do this after
2579 // checking if NumResults is zero, because with zero results
2580 // the operands won't have values in the map.
2581 SDValue Src = getValue(SrcV);
2582 SDValue Ptr = getValue(PtrV);
2583
2584 SDValue Root = getRoot();
2585 SmallVector<SDValue, 4> Chains(NumValues);
2586 MVT PtrVT = Ptr.getValueType();
2587 bool isVolatile = I.isVolatile();
2588 unsigned Alignment = I.getAlignment();
2589 for (unsigned i = 0; i != NumValues; ++i)
2590 Chains[i] = DAG.getStore(Root, SDValue(Src.getNode(), Src.getResNo() + i),
2591 DAG.getNode(ISD::ADD, PtrVT, Ptr,
2592 DAG.getConstant(Offsets[i], PtrVT)),
2593 PtrV, Offsets[i],
2594 isVolatile, Alignment);
2595
2596 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumValues));
2597}
2598
2599/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2600/// node.
2601void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
2602 unsigned Intrinsic) {
2603 bool HasChain = !I.doesNotAccessMemory();
2604 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2605
2606 // Build the operand list.
2607 SmallVector<SDValue, 8> Ops;
2608 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2609 if (OnlyLoad) {
2610 // We don't need to serialize loads against other loads.
2611 Ops.push_back(DAG.getRoot());
2612 } else {
2613 Ops.push_back(getRoot());
2614 }
2615 }
2616
2617 // Add the intrinsic ID as an integer operand.
2618 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
2619
2620 // Add all operands of the call to the operand list.
2621 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2622 SDValue Op = getValue(I.getOperand(i));
2623 assert(TLI.isTypeLegal(Op.getValueType()) &&
2624 "Intrinsic uses a non-legal type?");
2625 Ops.push_back(Op);
2626 }
2627
2628 std::vector<MVT> VTs;
2629 if (I.getType() != Type::VoidTy) {
2630 MVT VT = TLI.getValueType(I.getType());
2631 if (VT.isVector()) {
2632 const VectorType *DestTy = cast<VectorType>(I.getType());
2633 MVT EltVT = TLI.getValueType(DestTy->getElementType());
2634
2635 VT = MVT::getVectorVT(EltVT, DestTy->getNumElements());
2636 assert(VT != MVT::Other && "Intrinsic uses a non-legal type?");
2637 }
2638
2639 assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?");
2640 VTs.push_back(VT);
2641 }
2642 if (HasChain)
2643 VTs.push_back(MVT::Other);
2644
2645 const MVT *VTList = DAG.getNodeValueTypes(VTs);
2646
2647 // Create the node.
2648 SDValue Result;
2649 if (!HasChain)
2650 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(),
2651 &Ops[0], Ops.size());
2652 else if (I.getType() != Type::VoidTy)
2653 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(),
2654 &Ops[0], Ops.size());
2655 else
2656 Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(),
2657 &Ops[0], Ops.size());
2658
2659 if (HasChain) {
2660 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2661 if (OnlyLoad)
2662 PendingLoads.push_back(Chain);
2663 else
2664 DAG.setRoot(Chain);
2665 }
2666 if (I.getType() != Type::VoidTy) {
2667 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
2668 MVT VT = TLI.getValueType(PTy);
2669 Result = DAG.getNode(ISD::BIT_CONVERT, VT, Result);
2670 }
2671 setValue(&I, Result);
2672 }
2673}
2674
2675/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
2676static GlobalVariable *ExtractTypeInfo(Value *V) {
2677 V = V->stripPointerCasts();
2678 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
2679 assert ((GV || isa<ConstantPointerNull>(V)) &&
2680 "TypeInfo must be a global variable or NULL");
2681 return GV;
2682}
2683
2684namespace llvm {
2685
2686/// AddCatchInfo - Extract the personality and type infos from an eh.selector
2687/// call, and add them to the specified machine basic block.
2688void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
2689 MachineBasicBlock *MBB) {
2690 // Inform the MachineModuleInfo of the personality for this landing pad.
2691 ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
2692 assert(CE->getOpcode() == Instruction::BitCast &&
2693 isa<Function>(CE->getOperand(0)) &&
2694 "Personality should be a function");
2695 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
2696
2697 // Gather all the type infos for this landing pad and pass them along to
2698 // MachineModuleInfo.
2699 std::vector<GlobalVariable *> TyInfo;
2700 unsigned N = I.getNumOperands();
2701
2702 for (unsigned i = N - 1; i > 2; --i) {
2703 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
2704 unsigned FilterLength = CI->getZExtValue();
2705 unsigned FirstCatch = i + FilterLength + !FilterLength;
2706 assert (FirstCatch <= N && "Invalid filter length");
2707
2708 if (FirstCatch < N) {
2709 TyInfo.reserve(N - FirstCatch);
2710 for (unsigned j = FirstCatch; j < N; ++j)
2711 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2712 MMI->addCatchTypeInfo(MBB, TyInfo);
2713 TyInfo.clear();
2714 }
2715
2716 if (!FilterLength) {
2717 // Cleanup.
2718 MMI->addCleanup(MBB);
2719 } else {
2720 // Filter.
2721 TyInfo.reserve(FilterLength - 1);
2722 for (unsigned j = i + 1; j < FirstCatch; ++j)
2723 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2724 MMI->addFilterTypeInfo(MBB, TyInfo);
2725 TyInfo.clear();
2726 }
2727
2728 N = i;
2729 }
2730 }
2731
2732 if (N > 3) {
2733 TyInfo.reserve(N - 3);
2734 for (unsigned j = 3; j < N; ++j)
2735 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2736 MMI->addCatchTypeInfo(MBB, TyInfo);
2737 }
2738}
2739
2740}
2741
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002742/// GetSignificand - Get the significand and build it into a floating-point
2743/// number with exponent of 1:
2744///
2745/// Op = (Op & 0x007fffff) | 0x3f800000;
2746///
2747/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002748static SDValue
2749GetSignificand(SelectionDAG &DAG, SDValue Op) {
2750 SDValue t1 = DAG.getNode(ISD::AND, MVT::i32, Op,
2751 DAG.getConstant(0x007fffff, MVT::i32));
2752 SDValue t2 = DAG.getNode(ISD::OR, MVT::i32, t1,
2753 DAG.getConstant(0x3f800000, MVT::i32));
2754 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t2);
2755}
2756
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002757/// GetExponent - Get the exponent:
2758///
2759/// (float)((Op1 >> 23) - 127);
2760///
2761/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002762static SDValue
2763GetExponent(SelectionDAG &DAG, SDValue Op) {
Bill Wendlingfc2508e2008-09-10 06:26:10 +00002764 SDValue t1 = DAG.getNode(ISD::SRL, MVT::i32, Op,
Bill Wendling39150252008-09-09 20:39:27 +00002765 DAG.getConstant(23, MVT::i32));
Bill Wendlingfc2508e2008-09-10 06:26:10 +00002766 SDValue t2 = DAG.getNode(ISD::SUB, MVT::i32, t1,
Bill Wendling39150252008-09-09 20:39:27 +00002767 DAG.getConstant(127, MVT::i32));
Bill Wendlingfc2508e2008-09-10 06:26:10 +00002768 return DAG.getNode(ISD::UINT_TO_FP, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002769}
2770
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002771/// getF32Constant - Get 32-bit floating point constant.
2772static SDValue
2773getF32Constant(SelectionDAG &DAG, unsigned Flt) {
2774 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
2775}
2776
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002777/// Inlined utility function to implement binary input atomic intrinsics for
2778/// visitIntrinsicCall: I is a call instruction
2779/// Op is the associated NodeType for I
2780const char *
2781SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
2782 SDValue Root = getRoot();
2783 SDValue L = DAG.getAtomic(Op, Root,
2784 getValue(I.getOperand(1)),
2785 getValue(I.getOperand(2)),
2786 I.getOperand(1));
2787 setValue(&I, L);
2788 DAG.setRoot(L.getValue(1));
2789 return 0;
2790}
2791
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002792/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2793/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002794void
2795SelectionDAGLowering::visitExp(CallInst &I) {
2796 SDValue result;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002797
2798 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
2799 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2800 SDValue Op = getValue(I.getOperand(1));
2801
2802 // Put the exponent in the right bit position for later addition to the
2803 // final result:
2804 //
2805 // #define LOG2OFe 1.4426950f
2806 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
2807 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002808 getF32Constant(DAG, 0x3fb8aa3b));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002809 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, t0);
2810
2811 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
2812 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
2813 SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, t0, t1);
2814
2815 // IntegerPartOfX <<= 23;
2816 IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
2817 DAG.getConstant(23, MVT::i32));
2818
2819 if (LimitFloatPrecision <= 6) {
2820 // For floating-point precision of 6:
2821 //
2822 // TwoToFractionalPartOfX =
2823 // 0.997535578f +
2824 // (0.735607626f + 0.252464424f * x) * x;
2825 //
2826 // error 0.0144103317, which is 6 bits
2827 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002828 getF32Constant(DAG, 0x3e814304));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002829 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002830 getF32Constant(DAG, 0x3f3c50c8));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002831 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2832 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002833 getF32Constant(DAG, 0x3f7f5e7e));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002834 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
2835
2836 // Add the exponent into the result in integer domain.
2837 SDValue t6 = DAG.getNode(ISD::ADD, MVT::i32,
2838 TwoToFracPartOfX, IntegerPartOfX);
2839
2840 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t6);
2841 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2842 // For floating-point precision of 12:
2843 //
2844 // TwoToFractionalPartOfX =
2845 // 0.999892986f +
2846 // (0.696457318f +
2847 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2848 //
2849 // 0.000107046256 error, which is 13 to 14 bits
2850 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002851 getF32Constant(DAG, 0x3da235e3));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002852 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002853 getF32Constant(DAG, 0x3e65b8f3));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002854 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2855 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002856 getF32Constant(DAG, 0x3f324b07));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002857 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2858 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002859 getF32Constant(DAG, 0x3f7ff8fd));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002860 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
2861
2862 // Add the exponent into the result in integer domain.
2863 SDValue t8 = DAG.getNode(ISD::ADD, MVT::i32,
2864 TwoToFracPartOfX, IntegerPartOfX);
2865
2866 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t8);
2867 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2868 // For floating-point precision of 18:
2869 //
2870 // TwoToFractionalPartOfX =
2871 // 0.999999982f +
2872 // (0.693148872f +
2873 // (0.240227044f +
2874 // (0.554906021e-1f +
2875 // (0.961591928e-2f +
2876 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
2877 //
2878 // error 2.47208000*10^(-7), which is better than 18 bits
2879 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002880 getF32Constant(DAG, 0x3924b03e));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002881 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002882 getF32Constant(DAG, 0x3ab24b87));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002883 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2884 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002885 getF32Constant(DAG, 0x3c1d8c17));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002886 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2887 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002888 getF32Constant(DAG, 0x3d634a1d));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002889 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
2890 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002891 getF32Constant(DAG, 0x3e75fe14));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002892 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
2893 SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002894 getF32Constant(DAG, 0x3f317234));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002895 SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
2896 SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002897 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002898 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
2899
2900 // Add the exponent into the result in integer domain.
2901 SDValue t14 = DAG.getNode(ISD::ADD, MVT::i32,
2902 TwoToFracPartOfX, IntegerPartOfX);
2903
2904 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t14);
2905 }
2906 } else {
2907 // No special expansion.
2908 result = DAG.getNode(ISD::FEXP,
2909 getValue(I.getOperand(1)).getValueType(),
2910 getValue(I.getOperand(1)));
2911 }
2912
Dale Johannesen59e577f2008-09-05 18:38:42 +00002913 setValue(&I, result);
2914}
2915
Bill Wendling39150252008-09-09 20:39:27 +00002916/// visitLog - Lower a log intrinsic. Handles the special sequences for
2917/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002918void
2919SelectionDAGLowering::visitLog(CallInst &I) {
2920 SDValue result;
Bill Wendling39150252008-09-09 20:39:27 +00002921
2922 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
2923 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2924 SDValue Op = getValue(I.getOperand(1));
2925 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
2926
2927 // Scale the exponent by log(2) [0.69314718f].
2928 SDValue Exp = GetExponent(DAG, Op1);
2929 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002930 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00002931
2932 // Get the significand and build it into a floating-point number with
2933 // exponent of 1.
2934 SDValue X = GetSignificand(DAG, Op1);
2935
2936 if (LimitFloatPrecision <= 6) {
2937 // For floating-point precision of 6:
2938 //
2939 // LogofMantissa =
2940 // -1.1609546f +
2941 // (1.4034025f - 0.23903021f * x) * x;
2942 //
2943 // error 0.0034276066, which is better than 8 bits
2944 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002945 getF32Constant(DAG, 0xbe74c456));
Bill Wendling39150252008-09-09 20:39:27 +00002946 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002947 getF32Constant(DAG, 0x3fb3a2b1));
Bill Wendling39150252008-09-09 20:39:27 +00002948 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2949 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002950 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00002951
2952 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
2953 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2954 // For floating-point precision of 12:
2955 //
2956 // LogOfMantissa =
2957 // -1.7417939f +
2958 // (2.8212026f +
2959 // (-1.4699568f +
2960 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
2961 //
2962 // error 0.000061011436, which is 14 bits
2963 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002964 getF32Constant(DAG, 0xbd67b6d6));
Bill Wendling39150252008-09-09 20:39:27 +00002965 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002966 getF32Constant(DAG, 0x3ee4f4b8));
Bill Wendling39150252008-09-09 20:39:27 +00002967 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2968 SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002969 getF32Constant(DAG, 0x3fbc278b));
Bill Wendling39150252008-09-09 20:39:27 +00002970 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2971 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002972 getF32Constant(DAG, 0x40348e95));
Bill Wendling39150252008-09-09 20:39:27 +00002973 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2974 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002975 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00002976
2977 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
2978 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2979 // For floating-point precision of 18:
2980 //
2981 // LogOfMantissa =
2982 // -2.1072184f +
2983 // (4.2372794f +
2984 // (-3.7029485f +
2985 // (2.2781945f +
2986 // (-0.87823314f +
2987 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
2988 //
2989 // error 0.0000023660568, which is better than 18 bits
2990 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002991 getF32Constant(DAG, 0xbc91e5ac));
Bill Wendling39150252008-09-09 20:39:27 +00002992 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002993 getF32Constant(DAG, 0x3e4350aa));
Bill Wendling39150252008-09-09 20:39:27 +00002994 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2995 SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002996 getF32Constant(DAG, 0x3f60d3e3));
Bill Wendling39150252008-09-09 20:39:27 +00002997 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2998 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002999 getF32Constant(DAG, 0x4011cdf0));
Bill Wendling39150252008-09-09 20:39:27 +00003000 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3001 SDValue t7 = DAG.getNode(ISD::FSUB, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003002 getF32Constant(DAG, 0x406cfd1c));
Bill Wendling39150252008-09-09 20:39:27 +00003003 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3004 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003005 getF32Constant(DAG, 0x408797cb));
Bill Wendling39150252008-09-09 20:39:27 +00003006 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3007 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003008 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003009
3010 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
3011 }
3012 } else {
3013 // No special expansion.
3014 result = DAG.getNode(ISD::FLOG,
3015 getValue(I.getOperand(1)).getValueType(),
3016 getValue(I.getOperand(1)));
3017 }
3018
Dale Johannesen59e577f2008-09-05 18:38:42 +00003019 setValue(&I, result);
3020}
3021
Bill Wendling3eb59402008-09-09 00:28:24 +00003022/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3023/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003024void
3025SelectionDAGLowering::visitLog2(CallInst &I) {
3026 SDValue result;
Bill Wendling3eb59402008-09-09 00:28:24 +00003027
Dale Johannesen853244f2008-09-05 23:49:37 +00003028 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003029 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3030 SDValue Op = getValue(I.getOperand(1));
3031 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
3032
Bill Wendling39150252008-09-09 20:39:27 +00003033 // Get the exponent.
3034 SDValue LogOfExponent = GetExponent(DAG, Op1);
Bill Wendling3eb59402008-09-09 00:28:24 +00003035
3036 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003037 // exponent of 1.
3038 SDValue X = GetSignificand(DAG, Op1);
Bill Wendling3eb59402008-09-09 00:28:24 +00003039
3040 // Different possible minimax approximations of significand in
3041 // floating-point for various degrees of accuracy over [1,2].
3042 if (LimitFloatPrecision <= 6) {
3043 // For floating-point precision of 6:
3044 //
3045 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3046 //
3047 // error 0.0049451742, which is more than 7 bits
Bill Wendling39150252008-09-09 20:39:27 +00003048 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003049 getF32Constant(DAG, 0xbeb08fe0));
Bill Wendling39150252008-09-09 20:39:27 +00003050 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003051 getF32Constant(DAG, 0x40019463));
Bill Wendling39150252008-09-09 20:39:27 +00003052 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3053 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003054 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003055
3056 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
3057 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3058 // For floating-point precision of 12:
3059 //
3060 // Log2ofMantissa =
3061 // -2.51285454f +
3062 // (4.07009056f +
3063 // (-2.12067489f +
3064 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
3065 //
3066 // error 0.0000876136000, which is better than 13 bits
Bill Wendling39150252008-09-09 20:39:27 +00003067 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003068 getF32Constant(DAG, 0xbda7262e));
Bill Wendling39150252008-09-09 20:39:27 +00003069 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003070 getF32Constant(DAG, 0x3f25280b));
Bill Wendling39150252008-09-09 20:39:27 +00003071 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3072 SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003073 getF32Constant(DAG, 0x4007b923));
Bill Wendling39150252008-09-09 20:39:27 +00003074 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3075 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003076 getF32Constant(DAG, 0x40823e2f));
Bill Wendling39150252008-09-09 20:39:27 +00003077 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3078 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003079 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003080
3081 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
3082 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3083 // For floating-point precision of 18:
3084 //
3085 // Log2ofMantissa =
3086 // -3.0400495f +
3087 // (6.1129976f +
3088 // (-5.3420409f +
3089 // (3.2865683f +
3090 // (-1.2669343f +
3091 // (0.27515199f -
3092 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3093 //
3094 // error 0.0000018516, which is better than 18 bits
Bill Wendling39150252008-09-09 20:39:27 +00003095 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003096 getF32Constant(DAG, 0xbcd2769e));
Bill Wendling39150252008-09-09 20:39:27 +00003097 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003098 getF32Constant(DAG, 0x3e8ce0b9));
Bill Wendling39150252008-09-09 20:39:27 +00003099 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3100 SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003101 getF32Constant(DAG, 0x3fa22ae7));
Bill Wendling39150252008-09-09 20:39:27 +00003102 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3103 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003104 getF32Constant(DAG, 0x40525723));
Bill Wendling39150252008-09-09 20:39:27 +00003105 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3106 SDValue t7 = DAG.getNode(ISD::FSUB, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003107 getF32Constant(DAG, 0x40aaf200));
Bill Wendling39150252008-09-09 20:39:27 +00003108 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3109 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003110 getF32Constant(DAG, 0x40c39dad));
Bill Wendling3eb59402008-09-09 00:28:24 +00003111 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
Bill Wendling39150252008-09-09 20:39:27 +00003112 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003113 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003114
3115 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
3116 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003117 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003118 // No special expansion.
Dale Johannesen853244f2008-09-05 23:49:37 +00003119 result = DAG.getNode(ISD::FLOG2,
3120 getValue(I.getOperand(1)).getValueType(),
3121 getValue(I.getOperand(1)));
3122 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003123
Dale Johannesen59e577f2008-09-05 18:38:42 +00003124 setValue(&I, result);
3125}
3126
Bill Wendling3eb59402008-09-09 00:28:24 +00003127/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3128/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003129void
3130SelectionDAGLowering::visitLog10(CallInst &I) {
3131 SDValue result;
Dale Johannesen852680a2008-09-05 21:27:19 +00003132 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003133 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3134 SDValue Op = getValue(I.getOperand(1));
3135 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
3136
Bill Wendling39150252008-09-09 20:39:27 +00003137 // Scale the exponent by log10(2) [0.30102999f].
3138 SDValue Exp = GetExponent(DAG, Op1);
3139 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003140 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003141
3142 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003143 // exponent of 1.
3144 SDValue X = GetSignificand(DAG, Op1);
Bill Wendling3eb59402008-09-09 00:28:24 +00003145
3146 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003147 // For floating-point precision of 6:
3148 //
3149 // Log10ofMantissa =
3150 // -0.50419619f +
3151 // (0.60948995f - 0.10380950f * x) * x;
3152 //
3153 // error 0.0014886165, which is 6 bits
Bill Wendling39150252008-09-09 20:39:27 +00003154 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003155 getF32Constant(DAG, 0xbdd49a13));
Bill Wendling39150252008-09-09 20:39:27 +00003156 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003157 getF32Constant(DAG, 0x3f1c0789));
Bill Wendling39150252008-09-09 20:39:27 +00003158 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3159 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003160 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003161
3162 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003163 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3164 // For floating-point precision of 12:
3165 //
3166 // Log10ofMantissa =
3167 // -0.64831180f +
3168 // (0.91751397f +
3169 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3170 //
3171 // error 0.00019228036, which is better than 12 bits
Bill Wendling39150252008-09-09 20:39:27 +00003172 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003173 getF32Constant(DAG, 0x3d431f31));
Bill Wendling39150252008-09-09 20:39:27 +00003174 SDValue t1 = DAG.getNode(ISD::FSUB, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003175 getF32Constant(DAG, 0x3ea21fb2));
Bill Wendling39150252008-09-09 20:39:27 +00003176 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3177 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003178 getF32Constant(DAG, 0x3f6ae232));
Bill Wendling39150252008-09-09 20:39:27 +00003179 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3180 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003181 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003182
3183 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
3184 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003185 // For floating-point precision of 18:
3186 //
3187 // Log10ofMantissa =
3188 // -0.84299375f +
3189 // (1.5327582f +
3190 // (-1.0688956f +
3191 // (0.49102474f +
3192 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3193 //
3194 // error 0.0000037995730, which is better than 18 bits
Bill Wendling39150252008-09-09 20:39:27 +00003195 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003196 getF32Constant(DAG, 0x3c5d51ce));
Bill Wendling39150252008-09-09 20:39:27 +00003197 SDValue t1 = DAG.getNode(ISD::FSUB, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003198 getF32Constant(DAG, 0x3e00685a));
Bill Wendling39150252008-09-09 20:39:27 +00003199 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3200 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003201 getF32Constant(DAG, 0x3efb6798));
Bill Wendling39150252008-09-09 20:39:27 +00003202 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3203 SDValue t5 = DAG.getNode(ISD::FSUB, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003204 getF32Constant(DAG, 0x3f88d192));
Bill Wendling39150252008-09-09 20:39:27 +00003205 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3206 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003207 getF32Constant(DAG, 0x3fc4316c));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003208 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
Bill Wendling39150252008-09-09 20:39:27 +00003209 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003210 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003211
3212 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003213 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003214 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003215 // No special expansion.
Dale Johannesen852680a2008-09-05 21:27:19 +00003216 result = DAG.getNode(ISD::FLOG10,
3217 getValue(I.getOperand(1)).getValueType(),
3218 getValue(I.getOperand(1)));
3219 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003220
Dale Johannesen59e577f2008-09-05 18:38:42 +00003221 setValue(&I, result);
3222}
3223
Bill Wendlinge10c8142008-09-09 22:39:21 +00003224/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3225/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003226void
3227SelectionDAGLowering::visitExp2(CallInst &I) {
3228 SDValue result;
Bill Wendlinge10c8142008-09-09 22:39:21 +00003229
Dale Johannesen601d3c02008-09-05 01:48:15 +00003230 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003231 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3232 SDValue Op = getValue(I.getOperand(1));
3233
3234 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, Op);
3235
3236 // FractionalPartOfX = x - (float)IntegerPartOfX;
3237 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
3238 SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, Op, t1);
3239
3240 // IntegerPartOfX <<= 23;
3241 IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
3242 DAG.getConstant(23, MVT::i32));
3243
3244 if (LimitFloatPrecision <= 6) {
3245 // For floating-point precision of 6:
3246 //
3247 // TwoToFractionalPartOfX =
3248 // 0.997535578f +
3249 // (0.735607626f + 0.252464424f * x) * x;
3250 //
3251 // error 0.0144103317, which is 6 bits
3252 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003253 getF32Constant(DAG, 0x3e814304));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003254 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003255 getF32Constant(DAG, 0x3f3c50c8));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003256 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3257 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003258 getF32Constant(DAG, 0x3f7f5e7e));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003259 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
3260 SDValue TwoToFractionalPartOfX =
3261 DAG.getNode(ISD::ADD, MVT::i32, t6, IntegerPartOfX);
3262
3263 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3264 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3265 // For floating-point precision of 12:
3266 //
3267 // TwoToFractionalPartOfX =
3268 // 0.999892986f +
3269 // (0.696457318f +
3270 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3271 //
3272 // error 0.000107046256, which is 13 to 14 bits
3273 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003274 getF32Constant(DAG, 0x3da235e3));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003275 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003276 getF32Constant(DAG, 0x3e65b8f3));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003277 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3278 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003279 getF32Constant(DAG, 0x3f324b07));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003280 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3281 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003282 getF32Constant(DAG, 0x3f7ff8fd));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003283 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
3284 SDValue TwoToFractionalPartOfX =
3285 DAG.getNode(ISD::ADD, MVT::i32, t8, IntegerPartOfX);
3286
3287 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3288 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3289 // For floating-point precision of 18:
3290 //
3291 // TwoToFractionalPartOfX =
3292 // 0.999999982f +
3293 // (0.693148872f +
3294 // (0.240227044f +
3295 // (0.554906021e-1f +
3296 // (0.961591928e-2f +
3297 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3298 // error 2.47208000*10^(-7), which is better than 18 bits
3299 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003300 getF32Constant(DAG, 0x3924b03e));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003301 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003302 getF32Constant(DAG, 0x3ab24b87));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003303 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3304 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003305 getF32Constant(DAG, 0x3c1d8c17));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003306 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3307 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003308 getF32Constant(DAG, 0x3d634a1d));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003309 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3310 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003311 getF32Constant(DAG, 0x3e75fe14));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003312 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3313 SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003314 getF32Constant(DAG, 0x3f317234));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003315 SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
3316 SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003317 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003318 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
3319 SDValue TwoToFractionalPartOfX =
3320 DAG.getNode(ISD::ADD, MVT::i32, t14, IntegerPartOfX);
3321
3322 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3323 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003324 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003325 // No special expansion.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003326 result = DAG.getNode(ISD::FEXP2,
3327 getValue(I.getOperand(1)).getValueType(),
3328 getValue(I.getOperand(1)));
3329 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003330
Dale Johannesen601d3c02008-09-05 01:48:15 +00003331 setValue(&I, result);
3332}
3333
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003334/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3335/// limited-precision mode with x == 10.0f.
3336void
3337SelectionDAGLowering::visitPow(CallInst &I) {
3338 SDValue result;
3339 Value *Val = I.getOperand(1);
3340 bool IsExp10 = false;
3341
3342 if (getValue(Val).getValueType() == MVT::f32 &&
Bill Wendling277fc242008-09-10 00:24:59 +00003343 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003344 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3345 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3346 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3347 APFloat Ten(10.0f);
3348 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3349 }
3350 }
3351 }
3352
3353 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3354 SDValue Op = getValue(I.getOperand(2));
3355
3356 // Put the exponent in the right bit position for later addition to the
3357 // final result:
3358 //
3359 // #define LOG2OF10 3.3219281f
3360 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
3361 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003362 getF32Constant(DAG, 0x40549a78));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003363 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, t0);
3364
3365 // FractionalPartOfX = x - (float)IntegerPartOfX;
3366 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
3367 SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, t0, t1);
3368
3369 // IntegerPartOfX <<= 23;
3370 IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
3371 DAG.getConstant(23, MVT::i32));
3372
3373 if (LimitFloatPrecision <= 6) {
3374 // For floating-point precision of 6:
3375 //
3376 // twoToFractionalPartOfX =
3377 // 0.997535578f +
3378 // (0.735607626f + 0.252464424f * x) * x;
3379 //
3380 // error 0.0144103317, which is 6 bits
3381 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003382 getF32Constant(DAG, 0x3e814304));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003383 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003384 getF32Constant(DAG, 0x3f3c50c8));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003385 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3386 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003387 getF32Constant(DAG, 0x3f7f5e7e));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003388 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
3389 SDValue TwoToFractionalPartOfX =
3390 DAG.getNode(ISD::ADD, MVT::i32, t6, IntegerPartOfX);
3391
3392 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3393 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3394 // For floating-point precision of 12:
3395 //
3396 // TwoToFractionalPartOfX =
3397 // 0.999892986f +
3398 // (0.696457318f +
3399 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3400 //
3401 // error 0.000107046256, which is 13 to 14 bits
3402 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003403 getF32Constant(DAG, 0x3da235e3));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003404 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003405 getF32Constant(DAG, 0x3e65b8f3));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003406 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3407 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003408 getF32Constant(DAG, 0x3f324b07));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003409 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3410 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003411 getF32Constant(DAG, 0x3f7ff8fd));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003412 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
3413 SDValue TwoToFractionalPartOfX =
3414 DAG.getNode(ISD::ADD, MVT::i32, t8, IntegerPartOfX);
3415
3416 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3417 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3418 // For floating-point precision of 18:
3419 //
3420 // TwoToFractionalPartOfX =
3421 // 0.999999982f +
3422 // (0.693148872f +
3423 // (0.240227044f +
3424 // (0.554906021e-1f +
3425 // (0.961591928e-2f +
3426 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3427 // error 2.47208000*10^(-7), which is better than 18 bits
3428 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003429 getF32Constant(DAG, 0x3924b03e));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003430 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003431 getF32Constant(DAG, 0x3ab24b87));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003432 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3433 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003434 getF32Constant(DAG, 0x3c1d8c17));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003435 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3436 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003437 getF32Constant(DAG, 0x3d634a1d));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003438 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3439 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003440 getF32Constant(DAG, 0x3e75fe14));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003441 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3442 SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003443 getF32Constant(DAG, 0x3f317234));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003444 SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
3445 SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003446 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003447 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
3448 SDValue TwoToFractionalPartOfX =
3449 DAG.getNode(ISD::ADD, MVT::i32, t14, IntegerPartOfX);
3450
3451 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3452 }
3453 } else {
3454 // No special expansion.
3455 result = DAG.getNode(ISD::FPOW,
3456 getValue(I.getOperand(1)).getValueType(),
3457 getValue(I.getOperand(1)),
3458 getValue(I.getOperand(2)));
3459 }
3460
3461 setValue(&I, result);
3462}
3463
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003464/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3465/// we want to emit this as a call to a named external function, return the name
3466/// otherwise lower it and return null.
3467const char *
3468SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
3469 switch (Intrinsic) {
3470 default:
3471 // By default, turn this into a target intrinsic node.
3472 visitTargetIntrinsic(I, Intrinsic);
3473 return 0;
3474 case Intrinsic::vastart: visitVAStart(I); return 0;
3475 case Intrinsic::vaend: visitVAEnd(I); return 0;
3476 case Intrinsic::vacopy: visitVACopy(I); return 0;
3477 case Intrinsic::returnaddress:
3478 setValue(&I, DAG.getNode(ISD::RETURNADDR, TLI.getPointerTy(),
3479 getValue(I.getOperand(1))));
3480 return 0;
3481 case Intrinsic::frameaddress:
3482 setValue(&I, DAG.getNode(ISD::FRAMEADDR, TLI.getPointerTy(),
3483 getValue(I.getOperand(1))));
3484 return 0;
3485 case Intrinsic::setjmp:
3486 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3487 break;
3488 case Intrinsic::longjmp:
3489 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3490 break;
3491 case Intrinsic::memcpy_i32:
3492 case Intrinsic::memcpy_i64: {
3493 SDValue Op1 = getValue(I.getOperand(1));
3494 SDValue Op2 = getValue(I.getOperand(2));
3495 SDValue Op3 = getValue(I.getOperand(3));
3496 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3497 DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
3498 I.getOperand(1), 0, I.getOperand(2), 0));
3499 return 0;
3500 }
3501 case Intrinsic::memset_i32:
3502 case Intrinsic::memset_i64: {
3503 SDValue Op1 = getValue(I.getOperand(1));
3504 SDValue Op2 = getValue(I.getOperand(2));
3505 SDValue Op3 = getValue(I.getOperand(3));
3506 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3507 DAG.setRoot(DAG.getMemset(getRoot(), Op1, Op2, Op3, Align,
3508 I.getOperand(1), 0));
3509 return 0;
3510 }
3511 case Intrinsic::memmove_i32:
3512 case Intrinsic::memmove_i64: {
3513 SDValue Op1 = getValue(I.getOperand(1));
3514 SDValue Op2 = getValue(I.getOperand(2));
3515 SDValue Op3 = getValue(I.getOperand(3));
3516 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3517
3518 // If the source and destination are known to not be aliases, we can
3519 // lower memmove as memcpy.
3520 uint64_t Size = -1ULL;
3521 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003522 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003523 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3524 AliasAnalysis::NoAlias) {
3525 DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
3526 I.getOperand(1), 0, I.getOperand(2), 0));
3527 return 0;
3528 }
3529
3530 DAG.setRoot(DAG.getMemmove(getRoot(), Op1, Op2, Op3, Align,
3531 I.getOperand(1), 0, I.getOperand(2), 0));
3532 return 0;
3533 }
3534 case Intrinsic::dbg_stoppoint: {
3535 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3536 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
3537 if (MMI && SPI.getContext() && MMI->Verify(SPI.getContext())) {
3538 DebugInfoDesc *DD = MMI->getDescFor(SPI.getContext());
3539 assert(DD && "Not a debug information descriptor");
3540 DAG.setRoot(DAG.getDbgStopPoint(getRoot(),
3541 SPI.getLine(),
3542 SPI.getColumn(),
3543 cast<CompileUnitDesc>(DD)));
3544 }
3545
3546 return 0;
3547 }
3548 case Intrinsic::dbg_region_start: {
3549 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3550 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
3551 if (MMI && RSI.getContext() && MMI->Verify(RSI.getContext())) {
3552 unsigned LabelID = MMI->RecordRegionStart(RSI.getContext());
3553 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
3554 }
3555
3556 return 0;
3557 }
3558 case Intrinsic::dbg_region_end: {
3559 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3560 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
3561 if (MMI && REI.getContext() && MMI->Verify(REI.getContext())) {
3562 unsigned LabelID = MMI->RecordRegionEnd(REI.getContext());
3563 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
3564 }
3565
3566 return 0;
3567 }
3568 case Intrinsic::dbg_func_start: {
3569 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3570 if (!MMI) return 0;
3571 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
3572 Value *SP = FSI.getSubprogram();
3573 if (SP && MMI->Verify(SP)) {
3574 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
3575 // what (most?) gdb expects.
3576 DebugInfoDesc *DD = MMI->getDescFor(SP);
3577 assert(DD && "Not a debug information descriptor");
3578 SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
3579 const CompileUnitDesc *CompileUnit = Subprogram->getFile();
3580 unsigned SrcFile = MMI->RecordSource(CompileUnit);
3581 // Record the source line but does create a label. It will be emitted
3582 // at asm emission time.
3583 MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
3584 }
3585
3586 return 0;
3587 }
3588 case Intrinsic::dbg_declare: {
3589 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3590 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3591 Value *Variable = DI.getVariable();
3592 if (MMI && Variable && MMI->Verify(Variable))
3593 DAG.setRoot(DAG.getNode(ISD::DECLARE, MVT::Other, getRoot(),
3594 getValue(DI.getAddress()), getValue(Variable)));
3595 return 0;
3596 }
3597
3598 case Intrinsic::eh_exception: {
3599 if (!CurMBB->isLandingPad()) {
3600 // FIXME: Mark exception register as live in. Hack for PR1508.
3601 unsigned Reg = TLI.getExceptionAddressRegister();
3602 if (Reg) CurMBB->addLiveIn(Reg);
3603 }
3604 // Insert the EXCEPTIONADDR instruction.
3605 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3606 SDValue Ops[1];
3607 Ops[0] = DAG.getRoot();
3608 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, VTs, Ops, 1);
3609 setValue(&I, Op);
3610 DAG.setRoot(Op.getValue(1));
3611 return 0;
3612 }
3613
3614 case Intrinsic::eh_selector_i32:
3615 case Intrinsic::eh_selector_i64: {
3616 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3617 MVT VT = (Intrinsic == Intrinsic::eh_selector_i32 ?
3618 MVT::i32 : MVT::i64);
3619
3620 if (MMI) {
3621 if (CurMBB->isLandingPad())
3622 AddCatchInfo(I, MMI, CurMBB);
3623 else {
3624#ifndef NDEBUG
3625 FuncInfo.CatchInfoLost.insert(&I);
3626#endif
3627 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3628 unsigned Reg = TLI.getExceptionSelectorRegister();
3629 if (Reg) CurMBB->addLiveIn(Reg);
3630 }
3631
3632 // Insert the EHSELECTION instruction.
3633 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
3634 SDValue Ops[2];
3635 Ops[0] = getValue(I.getOperand(1));
3636 Ops[1] = getRoot();
3637 SDValue Op = DAG.getNode(ISD::EHSELECTION, VTs, Ops, 2);
3638 setValue(&I, Op);
3639 DAG.setRoot(Op.getValue(1));
3640 } else {
3641 setValue(&I, DAG.getConstant(0, VT));
3642 }
3643
3644 return 0;
3645 }
3646
3647 case Intrinsic::eh_typeid_for_i32:
3648 case Intrinsic::eh_typeid_for_i64: {
3649 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3650 MVT VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ?
3651 MVT::i32 : MVT::i64);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003652
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003653 if (MMI) {
3654 // Find the type id for the given typeinfo.
3655 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3656
3657 unsigned TypeID = MMI->getTypeIDFor(GV);
3658 setValue(&I, DAG.getConstant(TypeID, VT));
3659 } else {
3660 // Return something different to eh_selector.
3661 setValue(&I, DAG.getConstant(1, VT));
3662 }
3663
3664 return 0;
3665 }
3666
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003667 case Intrinsic::eh_return_i32:
3668 case Intrinsic::eh_return_i64:
3669 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003670 MMI->setCallsEHReturn(true);
3671 DAG.setRoot(DAG.getNode(ISD::EH_RETURN,
3672 MVT::Other,
3673 getControlRoot(),
3674 getValue(I.getOperand(1)),
3675 getValue(I.getOperand(2))));
3676 } else {
3677 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
3678 }
3679
3680 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003681 case Intrinsic::eh_unwind_init:
3682 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3683 MMI->setCallsUnwindInit(true);
3684 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003685
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003686 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003687
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003688 case Intrinsic::eh_dwarf_cfa: {
3689 MVT VT = getValue(I.getOperand(1)).getValueType();
3690 SDValue CfaArg;
3691 if (VT.bitsGT(TLI.getPointerTy()))
3692 CfaArg = DAG.getNode(ISD::TRUNCATE,
3693 TLI.getPointerTy(), getValue(I.getOperand(1)));
3694 else
3695 CfaArg = DAG.getNode(ISD::SIGN_EXTEND,
3696 TLI.getPointerTy(), getValue(I.getOperand(1)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003697
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003698 SDValue Offset = DAG.getNode(ISD::ADD,
3699 TLI.getPointerTy(),
3700 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET,
3701 TLI.getPointerTy()),
3702 CfaArg);
3703 setValue(&I, DAG.getNode(ISD::ADD,
3704 TLI.getPointerTy(),
3705 DAG.getNode(ISD::FRAMEADDR,
3706 TLI.getPointerTy(),
3707 DAG.getConstant(0,
3708 TLI.getPointerTy())),
3709 Offset));
3710 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003711 }
3712
3713 case Intrinsic::sqrt:
3714 setValue(&I, DAG.getNode(ISD::FSQRT,
3715 getValue(I.getOperand(1)).getValueType(),
3716 getValue(I.getOperand(1))));
3717 return 0;
3718 case Intrinsic::powi:
3719 setValue(&I, DAG.getNode(ISD::FPOWI,
3720 getValue(I.getOperand(1)).getValueType(),
3721 getValue(I.getOperand(1)),
3722 getValue(I.getOperand(2))));
3723 return 0;
3724 case Intrinsic::sin:
3725 setValue(&I, DAG.getNode(ISD::FSIN,
3726 getValue(I.getOperand(1)).getValueType(),
3727 getValue(I.getOperand(1))));
3728 return 0;
3729 case Intrinsic::cos:
3730 setValue(&I, DAG.getNode(ISD::FCOS,
3731 getValue(I.getOperand(1)).getValueType(),
3732 getValue(I.getOperand(1))));
3733 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003734 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003735 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003736 return 0;
3737 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003738 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003739 return 0;
3740 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003741 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003742 return 0;
3743 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003744 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003745 return 0;
3746 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003747 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003748 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003749 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003750 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003751 return 0;
3752 case Intrinsic::pcmarker: {
3753 SDValue Tmp = getValue(I.getOperand(1));
3754 DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
3755 return 0;
3756 }
3757 case Intrinsic::readcyclecounter: {
3758 SDValue Op = getRoot();
3759 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER,
3760 DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2,
3761 &Op, 1);
3762 setValue(&I, Tmp);
3763 DAG.setRoot(Tmp.getValue(1));
3764 return 0;
3765 }
3766 case Intrinsic::part_select: {
3767 // Currently not implemented: just abort
3768 assert(0 && "part_select intrinsic not implemented");
3769 abort();
3770 }
3771 case Intrinsic::part_set: {
3772 // Currently not implemented: just abort
3773 assert(0 && "part_set intrinsic not implemented");
3774 abort();
3775 }
3776 case Intrinsic::bswap:
3777 setValue(&I, DAG.getNode(ISD::BSWAP,
3778 getValue(I.getOperand(1)).getValueType(),
3779 getValue(I.getOperand(1))));
3780 return 0;
3781 case Intrinsic::cttz: {
3782 SDValue Arg = getValue(I.getOperand(1));
3783 MVT Ty = Arg.getValueType();
3784 SDValue result = DAG.getNode(ISD::CTTZ, Ty, Arg);
3785 setValue(&I, result);
3786 return 0;
3787 }
3788 case Intrinsic::ctlz: {
3789 SDValue Arg = getValue(I.getOperand(1));
3790 MVT Ty = Arg.getValueType();
3791 SDValue result = DAG.getNode(ISD::CTLZ, Ty, Arg);
3792 setValue(&I, result);
3793 return 0;
3794 }
3795 case Intrinsic::ctpop: {
3796 SDValue Arg = getValue(I.getOperand(1));
3797 MVT Ty = Arg.getValueType();
3798 SDValue result = DAG.getNode(ISD::CTPOP, Ty, Arg);
3799 setValue(&I, result);
3800 return 0;
3801 }
3802 case Intrinsic::stacksave: {
3803 SDValue Op = getRoot();
3804 SDValue Tmp = DAG.getNode(ISD::STACKSAVE,
3805 DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1);
3806 setValue(&I, Tmp);
3807 DAG.setRoot(Tmp.getValue(1));
3808 return 0;
3809 }
3810 case Intrinsic::stackrestore: {
3811 SDValue Tmp = getValue(I.getOperand(1));
3812 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
3813 return 0;
3814 }
3815 case Intrinsic::var_annotation:
3816 // Discard annotate attributes
3817 return 0;
3818
3819 case Intrinsic::init_trampoline: {
3820 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
3821
3822 SDValue Ops[6];
3823 Ops[0] = getRoot();
3824 Ops[1] = getValue(I.getOperand(1));
3825 Ops[2] = getValue(I.getOperand(2));
3826 Ops[3] = getValue(I.getOperand(3));
3827 Ops[4] = DAG.getSrcValue(I.getOperand(1));
3828 Ops[5] = DAG.getSrcValue(F);
3829
3830 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE,
3831 DAG.getNodeValueTypes(TLI.getPointerTy(),
3832 MVT::Other), 2,
3833 Ops, 6);
3834
3835 setValue(&I, Tmp);
3836 DAG.setRoot(Tmp.getValue(1));
3837 return 0;
3838 }
3839
3840 case Intrinsic::gcroot:
3841 if (GFI) {
3842 Value *Alloca = I.getOperand(1);
3843 Constant *TypeMap = cast<Constant>(I.getOperand(2));
3844
3845 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
3846 GFI->addStackRoot(FI->getIndex(), TypeMap);
3847 }
3848 return 0;
3849
3850 case Intrinsic::gcread:
3851 case Intrinsic::gcwrite:
3852 assert(0 && "GC failed to lower gcread/gcwrite intrinsics!");
3853 return 0;
3854
3855 case Intrinsic::flt_rounds: {
3856 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, MVT::i32));
3857 return 0;
3858 }
3859
3860 case Intrinsic::trap: {
3861 DAG.setRoot(DAG.getNode(ISD::TRAP, MVT::Other, getRoot()));
3862 return 0;
3863 }
3864 case Intrinsic::prefetch: {
3865 SDValue Ops[4];
3866 Ops[0] = getRoot();
3867 Ops[1] = getValue(I.getOperand(1));
3868 Ops[2] = getValue(I.getOperand(2));
3869 Ops[3] = getValue(I.getOperand(3));
3870 DAG.setRoot(DAG.getNode(ISD::PREFETCH, MVT::Other, &Ops[0], 4));
3871 return 0;
3872 }
3873
3874 case Intrinsic::memory_barrier: {
3875 SDValue Ops[6];
3876 Ops[0] = getRoot();
3877 for (int x = 1; x < 6; ++x)
3878 Ops[x] = getValue(I.getOperand(x));
3879
3880 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, MVT::Other, &Ops[0], 6));
3881 return 0;
3882 }
3883 case Intrinsic::atomic_cmp_swap: {
3884 SDValue Root = getRoot();
3885 SDValue L;
3886 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3887 case MVT::i8:
3888 L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_8, Root,
3889 getValue(I.getOperand(1)),
3890 getValue(I.getOperand(2)),
3891 getValue(I.getOperand(3)),
3892 I.getOperand(1));
3893 break;
3894 case MVT::i16:
3895 L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_16, Root,
3896 getValue(I.getOperand(1)),
3897 getValue(I.getOperand(2)),
3898 getValue(I.getOperand(3)),
3899 I.getOperand(1));
3900 break;
3901 case MVT::i32:
3902 L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_32, Root,
3903 getValue(I.getOperand(1)),
3904 getValue(I.getOperand(2)),
3905 getValue(I.getOperand(3)),
3906 I.getOperand(1));
3907 break;
3908 case MVT::i64:
3909 L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_64, Root,
3910 getValue(I.getOperand(1)),
3911 getValue(I.getOperand(2)),
3912 getValue(I.getOperand(3)),
3913 I.getOperand(1));
3914 break;
3915 default:
3916 assert(0 && "Invalid atomic type");
3917 abort();
3918 }
3919 setValue(&I, L);
3920 DAG.setRoot(L.getValue(1));
3921 return 0;
3922 }
3923 case Intrinsic::atomic_load_add:
3924 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3925 case MVT::i8:
3926 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_8);
3927 case MVT::i16:
3928 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_16);
3929 case MVT::i32:
3930 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_32);
3931 case MVT::i64:
3932 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_64);
3933 default:
3934 assert(0 && "Invalid atomic type");
3935 abort();
3936 }
3937 case Intrinsic::atomic_load_sub:
3938 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3939 case MVT::i8:
3940 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_8);
3941 case MVT::i16:
3942 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_16);
3943 case MVT::i32:
3944 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_32);
3945 case MVT::i64:
3946 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_64);
3947 default:
3948 assert(0 && "Invalid atomic type");
3949 abort();
3950 }
3951 case Intrinsic::atomic_load_or:
3952 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3953 case MVT::i8:
3954 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_8);
3955 case MVT::i16:
3956 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_16);
3957 case MVT::i32:
3958 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_32);
3959 case MVT::i64:
3960 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_64);
3961 default:
3962 assert(0 && "Invalid atomic type");
3963 abort();
3964 }
3965 case Intrinsic::atomic_load_xor:
3966 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3967 case MVT::i8:
3968 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_8);
3969 case MVT::i16:
3970 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_16);
3971 case MVT::i32:
3972 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_32);
3973 case MVT::i64:
3974 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_64);
3975 default:
3976 assert(0 && "Invalid atomic type");
3977 abort();
3978 }
3979 case Intrinsic::atomic_load_and:
3980 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3981 case MVT::i8:
3982 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_8);
3983 case MVT::i16:
3984 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_16);
3985 case MVT::i32:
3986 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_32);
3987 case MVT::i64:
3988 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_64);
3989 default:
3990 assert(0 && "Invalid atomic type");
3991 abort();
3992 }
3993 case Intrinsic::atomic_load_nand:
3994 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3995 case MVT::i8:
3996 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_8);
3997 case MVT::i16:
3998 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_16);
3999 case MVT::i32:
4000 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_32);
4001 case MVT::i64:
4002 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_64);
4003 default:
4004 assert(0 && "Invalid atomic type");
4005 abort();
4006 }
4007 case Intrinsic::atomic_load_max:
4008 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4009 case MVT::i8:
4010 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_8);
4011 case MVT::i16:
4012 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_16);
4013 case MVT::i32:
4014 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_32);
4015 case MVT::i64:
4016 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_64);
4017 default:
4018 assert(0 && "Invalid atomic type");
4019 abort();
4020 }
4021 case Intrinsic::atomic_load_min:
4022 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4023 case MVT::i8:
4024 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_8);
4025 case MVT::i16:
4026 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_16);
4027 case MVT::i32:
4028 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_32);
4029 case MVT::i64:
4030 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_64);
4031 default:
4032 assert(0 && "Invalid atomic type");
4033 abort();
4034 }
4035 case Intrinsic::atomic_load_umin:
4036 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4037 case MVT::i8:
4038 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_8);
4039 case MVT::i16:
4040 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_16);
4041 case MVT::i32:
4042 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_32);
4043 case MVT::i64:
4044 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_64);
4045 default:
4046 assert(0 && "Invalid atomic type");
4047 abort();
4048 }
4049 case Intrinsic::atomic_load_umax:
4050 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4051 case MVT::i8:
4052 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_8);
4053 case MVT::i16:
4054 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_16);
4055 case MVT::i32:
4056 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_32);
4057 case MVT::i64:
4058 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_64);
4059 default:
4060 assert(0 && "Invalid atomic type");
4061 abort();
4062 }
4063 case Intrinsic::atomic_swap:
4064 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4065 case MVT::i8:
4066 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_8);
4067 case MVT::i16:
4068 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_16);
4069 case MVT::i32:
4070 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_32);
4071 case MVT::i64:
4072 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_64);
4073 default:
4074 assert(0 && "Invalid atomic type");
4075 abort();
4076 }
4077 }
4078}
4079
4080
4081void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee,
4082 bool IsTailCall,
4083 MachineBasicBlock *LandingPad) {
4084 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4085 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
4086 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4087 unsigned BeginLabel = 0, EndLabel = 0;
4088
4089 TargetLowering::ArgListTy Args;
4090 TargetLowering::ArgListEntry Entry;
4091 Args.reserve(CS.arg_size());
4092 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
4093 i != e; ++i) {
4094 SDValue ArgNode = getValue(*i);
4095 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4096
4097 unsigned attrInd = i - CS.arg_begin() + 1;
4098 Entry.isSExt = CS.paramHasAttr(attrInd, ParamAttr::SExt);
4099 Entry.isZExt = CS.paramHasAttr(attrInd, ParamAttr::ZExt);
4100 Entry.isInReg = CS.paramHasAttr(attrInd, ParamAttr::InReg);
4101 Entry.isSRet = CS.paramHasAttr(attrInd, ParamAttr::StructRet);
4102 Entry.isNest = CS.paramHasAttr(attrInd, ParamAttr::Nest);
4103 Entry.isByVal = CS.paramHasAttr(attrInd, ParamAttr::ByVal);
4104 Entry.Alignment = CS.getParamAlignment(attrInd);
4105 Args.push_back(Entry);
4106 }
4107
4108 if (LandingPad && MMI) {
4109 // Insert a label before the invoke call to mark the try range. This can be
4110 // used to detect deletion of the invoke via the MachineModuleInfo.
4111 BeginLabel = MMI->NextLabelID();
4112 // Both PendingLoads and PendingExports must be flushed here;
4113 // this call might not return.
4114 (void)getRoot();
4115 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getControlRoot(), BeginLabel));
4116 }
4117
4118 std::pair<SDValue,SDValue> Result =
4119 TLI.LowerCallTo(getRoot(), CS.getType(),
4120 CS.paramHasAttr(0, ParamAttr::SExt),
4121 CS.paramHasAttr(0, ParamAttr::ZExt),
Dan Gohman1937e2f2008-09-16 01:42:28 +00004122 FTy->isVarArg(), CS.getCallingConv(),
4123 IsTailCall && PerformTailCallOpt,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004124 Callee, Args, DAG);
4125 if (CS.getType() != Type::VoidTy)
4126 setValue(CS.getInstruction(), Result.first);
4127 DAG.setRoot(Result.second);
4128
4129 if (LandingPad && MMI) {
4130 // Insert a label at the end of the invoke call to mark the try range. This
4131 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4132 EndLabel = MMI->NextLabelID();
4133 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getRoot(), EndLabel));
4134
4135 // Inform MachineModuleInfo of range.
4136 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4137 }
4138}
4139
4140
4141void SelectionDAGLowering::visitCall(CallInst &I) {
4142 const char *RenameFn = 0;
4143 if (Function *F = I.getCalledFunction()) {
4144 if (F->isDeclaration()) {
4145 if (unsigned IID = F->getIntrinsicID()) {
4146 RenameFn = visitIntrinsicCall(I, IID);
4147 if (!RenameFn)
4148 return;
4149 }
4150 }
4151
4152 // Check for well-known libc/libm calls. If the function is internal, it
4153 // can't be a library call.
4154 unsigned NameLen = F->getNameLen();
4155 if (!F->hasInternalLinkage() && NameLen) {
4156 const char *NameStr = F->getNameStart();
4157 if (NameStr[0] == 'c' &&
4158 ((NameLen == 8 && !strcmp(NameStr, "copysign")) ||
4159 (NameLen == 9 && !strcmp(NameStr, "copysignf")))) {
4160 if (I.getNumOperands() == 3 && // Basic sanity checks.
4161 I.getOperand(1)->getType()->isFloatingPoint() &&
4162 I.getType() == I.getOperand(1)->getType() &&
4163 I.getType() == I.getOperand(2)->getType()) {
4164 SDValue LHS = getValue(I.getOperand(1));
4165 SDValue RHS = getValue(I.getOperand(2));
4166 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
4167 LHS, RHS));
4168 return;
4169 }
4170 } else if (NameStr[0] == 'f' &&
4171 ((NameLen == 4 && !strcmp(NameStr, "fabs")) ||
4172 (NameLen == 5 && !strcmp(NameStr, "fabsf")) ||
4173 (NameLen == 5 && !strcmp(NameStr, "fabsl")))) {
4174 if (I.getNumOperands() == 2 && // Basic sanity checks.
4175 I.getOperand(1)->getType()->isFloatingPoint() &&
4176 I.getType() == I.getOperand(1)->getType()) {
4177 SDValue Tmp = getValue(I.getOperand(1));
4178 setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
4179 return;
4180 }
4181 } else if (NameStr[0] == 's' &&
4182 ((NameLen == 3 && !strcmp(NameStr, "sin")) ||
4183 (NameLen == 4 && !strcmp(NameStr, "sinf")) ||
4184 (NameLen == 4 && !strcmp(NameStr, "sinl")))) {
4185 if (I.getNumOperands() == 2 && // Basic sanity checks.
4186 I.getOperand(1)->getType()->isFloatingPoint() &&
4187 I.getType() == I.getOperand(1)->getType()) {
4188 SDValue Tmp = getValue(I.getOperand(1));
4189 setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
4190 return;
4191 }
4192 } else if (NameStr[0] == 'c' &&
4193 ((NameLen == 3 && !strcmp(NameStr, "cos")) ||
4194 (NameLen == 4 && !strcmp(NameStr, "cosf")) ||
4195 (NameLen == 4 && !strcmp(NameStr, "cosl")))) {
4196 if (I.getNumOperands() == 2 && // Basic sanity checks.
4197 I.getOperand(1)->getType()->isFloatingPoint() &&
4198 I.getType() == I.getOperand(1)->getType()) {
4199 SDValue Tmp = getValue(I.getOperand(1));
4200 setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
4201 return;
4202 }
4203 }
4204 }
4205 } else if (isa<InlineAsm>(I.getOperand(0))) {
4206 visitInlineAsm(&I);
4207 return;
4208 }
4209
4210 SDValue Callee;
4211 if (!RenameFn)
4212 Callee = getValue(I.getOperand(0));
4213 else
Bill Wendling056292f2008-09-16 21:48:12 +00004214 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004215
4216 LowerCallTo(&I, Callee, I.isTailCall());
4217}
4218
4219
4220/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
4221/// this value and returns the result as a ValueVT value. This uses
4222/// Chain/Flag as the input and updates them for the output Chain/Flag.
4223/// If the Flag pointer is NULL, no flag is used.
4224SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
4225 SDValue &Chain,
4226 SDValue *Flag) const {
4227 // Assemble the legal parts into the final values.
4228 SmallVector<SDValue, 4> Values(ValueVTs.size());
4229 SmallVector<SDValue, 8> Parts;
4230 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4231 // Copy the legal parts from the registers.
4232 MVT ValueVT = ValueVTs[Value];
4233 unsigned NumRegs = TLI->getNumRegisters(ValueVT);
4234 MVT RegisterVT = RegVTs[Value];
4235
4236 Parts.resize(NumRegs);
4237 for (unsigned i = 0; i != NumRegs; ++i) {
4238 SDValue P;
4239 if (Flag == 0)
4240 P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT);
4241 else {
4242 P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT, *Flag);
4243 *Flag = P.getValue(2);
4244 }
4245 Chain = P.getValue(1);
4246
4247 // If the source register was virtual and if we know something about it,
4248 // add an assert node.
4249 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4250 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4251 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4252 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4253 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4254 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
4255
4256 unsigned RegSize = RegisterVT.getSizeInBits();
4257 unsigned NumSignBits = LOI.NumSignBits;
4258 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
4259
4260 // FIXME: We capture more information than the dag can represent. For
4261 // now, just use the tightest assertzext/assertsext possible.
4262 bool isSExt = true;
4263 MVT FromVT(MVT::Other);
4264 if (NumSignBits == RegSize)
4265 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
4266 else if (NumZeroBits >= RegSize-1)
4267 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
4268 else if (NumSignBits > RegSize-8)
4269 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
4270 else if (NumZeroBits >= RegSize-9)
4271 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
4272 else if (NumSignBits > RegSize-16)
4273 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
4274 else if (NumZeroBits >= RegSize-17)
4275 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
4276 else if (NumSignBits > RegSize-32)
4277 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
4278 else if (NumZeroBits >= RegSize-33)
4279 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
4280
4281 if (FromVT != MVT::Other) {
4282 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext,
4283 RegisterVT, P, DAG.getValueType(FromVT));
4284
4285 }
4286 }
4287 }
4288
4289 Parts[i] = P;
4290 }
4291
4292 Values[Value] = getCopyFromParts(DAG, Parts.begin(), NumRegs, RegisterVT,
4293 ValueVT);
4294 Part += NumRegs;
4295 Parts.clear();
4296 }
4297
4298 return DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4299 &Values[0], ValueVTs.size());
4300}
4301
4302/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
4303/// specified value into the registers specified by this object. This uses
4304/// Chain/Flag as the input and updates them for the output Chain/Flag.
4305/// If the Flag pointer is NULL, no flag is used.
4306void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG,
4307 SDValue &Chain, SDValue *Flag) const {
4308 // Get the list of the values's legal parts.
4309 unsigned NumRegs = Regs.size();
4310 SmallVector<SDValue, 8> Parts(NumRegs);
4311 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4312 MVT ValueVT = ValueVTs[Value];
4313 unsigned NumParts = TLI->getNumRegisters(ValueVT);
4314 MVT RegisterVT = RegVTs[Value];
4315
4316 getCopyToParts(DAG, Val.getValue(Val.getResNo() + Value),
4317 &Parts[Part], NumParts, RegisterVT);
4318 Part += NumParts;
4319 }
4320
4321 // Copy the parts into the registers.
4322 SmallVector<SDValue, 8> Chains(NumRegs);
4323 for (unsigned i = 0; i != NumRegs; ++i) {
4324 SDValue Part;
4325 if (Flag == 0)
4326 Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i]);
4327 else {
4328 Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i], *Flag);
4329 *Flag = Part.getValue(1);
4330 }
4331 Chains[i] = Part.getValue(0);
4332 }
4333
4334 if (NumRegs == 1 || Flag)
4335 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
4336 // flagged to it. That is the CopyToReg nodes and the user are considered
4337 // a single scheduling unit. If we create a TokenFactor and return it as
4338 // chain, then the TokenFactor is both a predecessor (operand) of the
4339 // user as well as a successor (the TF operands are flagged to the user).
4340 // c1, f1 = CopyToReg
4341 // c2, f2 = CopyToReg
4342 // c3 = TokenFactor c1, c2
4343 // ...
4344 // = op c3, ..., f2
4345 Chain = Chains[NumRegs-1];
4346 else
4347 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumRegs);
4348}
4349
4350/// AddInlineAsmOperands - Add this value to the specified inlineasm node
4351/// operand list. This adds the code marker and includes the number of
4352/// values added into it.
4353void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
4354 std::vector<SDValue> &Ops) const {
4355 MVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
4356 Ops.push_back(DAG.getTargetConstant(Code | (Regs.size() << 3), IntPtrTy));
4357 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
4358 unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]);
4359 MVT RegisterVT = RegVTs[Value];
4360 for (unsigned i = 0; i != NumRegs; ++i)
4361 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
4362 }
4363}
4364
4365/// isAllocatableRegister - If the specified register is safe to allocate,
4366/// i.e. it isn't a stack pointer or some other special register, return the
4367/// register class for the register. Otherwise, return null.
4368static const TargetRegisterClass *
4369isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4370 const TargetLowering &TLI,
4371 const TargetRegisterInfo *TRI) {
4372 MVT FoundVT = MVT::Other;
4373 const TargetRegisterClass *FoundRC = 0;
4374 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4375 E = TRI->regclass_end(); RCI != E; ++RCI) {
4376 MVT ThisVT = MVT::Other;
4377
4378 const TargetRegisterClass *RC = *RCI;
4379 // If none of the the value types for this register class are valid, we
4380 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4381 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4382 I != E; ++I) {
4383 if (TLI.isTypeLegal(*I)) {
4384 // If we have already found this register in a different register class,
4385 // choose the one with the largest VT specified. For example, on
4386 // PowerPC, we favor f64 register classes over f32.
4387 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
4388 ThisVT = *I;
4389 break;
4390 }
4391 }
4392 }
4393
4394 if (ThisVT == MVT::Other) continue;
4395
4396 // NOTE: This isn't ideal. In particular, this might allocate the
4397 // frame pointer in functions that need it (due to them not being taken
4398 // out of allocation, because a variable sized allocation hasn't been seen
4399 // yet). This is a slight code pessimization, but should still work.
4400 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4401 E = RC->allocation_order_end(MF); I != E; ++I)
4402 if (*I == Reg) {
4403 // We found a matching register class. Keep looking at others in case
4404 // we find one with larger registers that this physreg is also in.
4405 FoundRC = RC;
4406 FoundVT = ThisVT;
4407 break;
4408 }
4409 }
4410 return FoundRC;
4411}
4412
4413
4414namespace llvm {
4415/// AsmOperandInfo - This contains information for each constraint that we are
4416/// lowering.
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004417struct VISIBILITY_HIDDEN SDISelAsmOperandInfo :
4418 public TargetLowering::AsmOperandInfo {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004419 /// CallOperand - If this is the result output operand or a clobber
4420 /// this is null, otherwise it is the incoming operand to the CallInst.
4421 /// This gets modified as the asm is processed.
4422 SDValue CallOperand;
4423
4424 /// AssignedRegs - If this is a register or register class operand, this
4425 /// contains the set of register corresponding to the operand.
4426 RegsForValue AssignedRegs;
4427
4428 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4429 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4430 }
4431
4432 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4433 /// busy in OutputRegs/InputRegs.
4434 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
4435 std::set<unsigned> &OutputRegs,
4436 std::set<unsigned> &InputRegs,
4437 const TargetRegisterInfo &TRI) const {
4438 if (isOutReg) {
4439 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4440 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4441 }
4442 if (isInReg) {
4443 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4444 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4445 }
4446 }
4447
4448private:
4449 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4450 /// specified set.
4451 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
4452 const TargetRegisterInfo &TRI) {
4453 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4454 Regs.insert(Reg);
4455 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4456 for (; *Aliases; ++Aliases)
4457 Regs.insert(*Aliases);
4458 }
4459};
4460} // end llvm namespace.
4461
4462
4463/// GetRegistersForValue - Assign registers (virtual or physical) for the
4464/// specified operand. We prefer to assign virtual registers, to allow the
4465/// register allocator handle the assignment process. However, if the asm uses
4466/// features that we can't model on machineinstrs, we have SDISel do the
4467/// allocation. This produces generally horrible, but correct, code.
4468///
4469/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004470/// Input and OutputRegs are the set of already allocated physical registers.
4471///
4472void SelectionDAGLowering::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004473GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004474 std::set<unsigned> &OutputRegs,
4475 std::set<unsigned> &InputRegs) {
4476 // Compute whether this value requires an input register, an output register,
4477 // or both.
4478 bool isOutReg = false;
4479 bool isInReg = false;
4480 switch (OpInfo.Type) {
4481 case InlineAsm::isOutput:
4482 isOutReg = true;
4483
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004484 // If there is an input constraint that matches this, we need to reserve
4485 // the input register so no other inputs allocate to it.
4486 isInReg = OpInfo.hasMatchingInput;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004487 break;
4488 case InlineAsm::isInput:
4489 isInReg = true;
4490 isOutReg = false;
4491 break;
4492 case InlineAsm::isClobber:
4493 isOutReg = true;
4494 isInReg = true;
4495 break;
4496 }
4497
4498
4499 MachineFunction &MF = DAG.getMachineFunction();
4500 SmallVector<unsigned, 4> Regs;
4501
4502 // If this is a constraint for a single physreg, or a constraint for a
4503 // register class, find it.
4504 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
4505 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4506 OpInfo.ConstraintVT);
4507
4508 unsigned NumRegs = 1;
4509 if (OpInfo.ConstraintVT != MVT::Other)
4510 NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT);
4511 MVT RegVT;
4512 MVT ValueVT = OpInfo.ConstraintVT;
4513
4514
4515 // If this is a constraint for a specific physical register, like {r17},
4516 // assign it now.
4517 if (PhysReg.first) {
4518 if (OpInfo.ConstraintVT == MVT::Other)
4519 ValueVT = *PhysReg.second->vt_begin();
4520
4521 // Get the actual register value type. This is important, because the user
4522 // may have asked for (e.g.) the AX register in i32 type. We need to
4523 // remember that AX is actually i16 to get the right extension.
4524 RegVT = *PhysReg.second->vt_begin();
4525
4526 // This is a explicit reference to a physical register.
4527 Regs.push_back(PhysReg.first);
4528
4529 // If this is an expanded reference, add the rest of the regs to Regs.
4530 if (NumRegs != 1) {
4531 TargetRegisterClass::iterator I = PhysReg.second->begin();
4532 for (; *I != PhysReg.first; ++I)
4533 assert(I != PhysReg.second->end() && "Didn't find reg!");
4534
4535 // Already added the first reg.
4536 --NumRegs; ++I;
4537 for (; NumRegs; --NumRegs, ++I) {
4538 assert(I != PhysReg.second->end() && "Ran out of registers to allocate!");
4539 Regs.push_back(*I);
4540 }
4541 }
4542 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4543 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4544 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4545 return;
4546 }
4547
4548 // Otherwise, if this was a reference to an LLVM register class, create vregs
4549 // for this reference.
4550 std::vector<unsigned> RegClassRegs;
4551 const TargetRegisterClass *RC = PhysReg.second;
4552 if (RC) {
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004553 // If this is a tied register, our regalloc doesn't know how to maintain
4554 // the constraint. If it isn't, go ahead and create vreg
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004555 // and let the regalloc do the right thing.
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004556 if (!OpInfo.hasMatchingInput) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004557 RegVT = *PhysReg.second->vt_begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004558 if (OpInfo.ConstraintVT == MVT::Other)
4559 ValueVT = RegVT;
4560
4561 // Create the appropriate number of virtual registers.
4562 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4563 for (; NumRegs; --NumRegs)
4564 Regs.push_back(RegInfo.createVirtualRegister(PhysReg.second));
4565
4566 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4567 return;
4568 }
4569
4570 // Otherwise, we can't allocate it. Let the code below figure out how to
4571 // maintain these constraints.
4572 RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end());
4573
4574 } else {
4575 // This is a reference to a register class that doesn't directly correspond
4576 // to an LLVM register class. Allocate NumRegs consecutive, available,
4577 // registers from the class.
4578 RegClassRegs = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
4579 OpInfo.ConstraintVT);
4580 }
4581
4582 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4583 unsigned NumAllocated = 0;
4584 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
4585 unsigned Reg = RegClassRegs[i];
4586 // See if this register is available.
4587 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
4588 (isInReg && InputRegs.count(Reg))) { // Already used.
4589 // Make sure we find consecutive registers.
4590 NumAllocated = 0;
4591 continue;
4592 }
4593
4594 // Check to see if this register is allocatable (i.e. don't give out the
4595 // stack pointer).
4596 if (RC == 0) {
4597 RC = isAllocatableRegister(Reg, MF, TLI, TRI);
4598 if (!RC) { // Couldn't allocate this register.
4599 // Reset NumAllocated to make sure we return consecutive registers.
4600 NumAllocated = 0;
4601 continue;
4602 }
4603 }
4604
4605 // Okay, this register is good, we can use it.
4606 ++NumAllocated;
4607
4608 // If we allocated enough consecutive registers, succeed.
4609 if (NumAllocated == NumRegs) {
4610 unsigned RegStart = (i-NumAllocated)+1;
4611 unsigned RegEnd = i+1;
4612 // Mark all of the allocated registers used.
4613 for (unsigned i = RegStart; i != RegEnd; ++i)
4614 Regs.push_back(RegClassRegs[i]);
4615
4616 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
4617 OpInfo.ConstraintVT);
4618 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4619 return;
4620 }
4621 }
4622
4623 // Otherwise, we couldn't allocate enough registers for this.
4624}
4625
Evan Chengda43bcf2008-09-24 00:05:32 +00004626/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
4627/// processed uses a memory 'm' constraint.
4628static bool
4629hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
4630 TargetLowering &TLI) {
4631 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
4632 InlineAsm::ConstraintInfo &CI = CInfos[i];
4633 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
4634 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
4635 if (CType == TargetLowering::C_Memory)
4636 return true;
4637 }
4638 }
4639
4640 return false;
4641}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004642
4643/// visitInlineAsm - Handle a call to an InlineAsm object.
4644///
4645void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
4646 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
4647
4648 /// ConstraintOperands - Information about all of the constraints.
4649 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
4650
4651 SDValue Chain = getRoot();
4652 SDValue Flag;
4653
4654 std::set<unsigned> OutputRegs, InputRegs;
4655
4656 // Do a prepass over the constraints, canonicalizing them, and building up the
4657 // ConstraintOperands list.
4658 std::vector<InlineAsm::ConstraintInfo>
4659 ConstraintInfos = IA->ParseConstraints();
4660
Evan Chengda43bcf2008-09-24 00:05:32 +00004661 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004662
4663 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
4664 unsigned ResNo = 0; // ResNo - The result number of the next output.
4665 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4666 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
4667 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
4668
4669 MVT OpVT = MVT::Other;
4670
4671 // Compute the value type for each operand.
4672 switch (OpInfo.Type) {
4673 case InlineAsm::isOutput:
4674 // Indirect outputs just consume an argument.
4675 if (OpInfo.isIndirect) {
4676 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4677 break;
4678 }
4679 // The return value of the call is this value. As such, there is no
4680 // corresponding argument.
4681 assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
4682 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
4683 OpVT = TLI.getValueType(STy->getElementType(ResNo));
4684 } else {
4685 assert(ResNo == 0 && "Asm only has one result!");
4686 OpVT = TLI.getValueType(CS.getType());
4687 }
4688 ++ResNo;
4689 break;
4690 case InlineAsm::isInput:
4691 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4692 break;
4693 case InlineAsm::isClobber:
4694 // Nothing to do.
4695 break;
4696 }
4697
4698 // If this is an input or an indirect output, process the call argument.
4699 // BasicBlocks are labels, currently appearing only in asm's.
4700 if (OpInfo.CallOperandVal) {
4701 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal))
4702 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
4703 else {
4704 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
4705 const Type *OpTy = OpInfo.CallOperandVal->getType();
4706 // If this is an indirect operand, the operand is a pointer to the
4707 // accessed type.
4708 if (OpInfo.isIndirect)
4709 OpTy = cast<PointerType>(OpTy)->getElementType();
4710
4711 // If OpTy is not a single value, it may be a struct/union that we
4712 // can tile with integers.
4713 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4714 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4715 switch (BitSize) {
4716 default: break;
4717 case 1:
4718 case 8:
4719 case 16:
4720 case 32:
4721 case 64:
4722 OpTy = IntegerType::get(BitSize);
4723 break;
4724 }
4725 }
4726
4727 OpVT = TLI.getValueType(OpTy, true);
4728 }
4729 }
4730
4731 OpInfo.ConstraintVT = OpVT;
4732
4733 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00004734 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004735
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004736 // If this is a memory input, and if the operand is not indirect, do what we
4737 // need to to provide an address for the memory input.
4738 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
4739 !OpInfo.isIndirect) {
4740 assert(OpInfo.Type == InlineAsm::isInput &&
4741 "Can only indirectify direct input operands!");
4742
4743 // Memory operands really want the address of the value. If we don't have
4744 // an indirect input, put it in the constpool if we can, otherwise spill
4745 // it to a stack slot.
4746
4747 // If the operand is a float, integer, or vector constant, spill to a
4748 // constant pool entry to get its address.
4749 Value *OpVal = OpInfo.CallOperandVal;
4750 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
4751 isa<ConstantVector>(OpVal)) {
4752 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
4753 TLI.getPointerTy());
4754 } else {
4755 // Otherwise, create a stack slot and emit a store to it before the
4756 // asm.
4757 const Type *Ty = OpVal->getType();
4758 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
4759 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
4760 MachineFunction &MF = DAG.getMachineFunction();
4761 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
4762 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4763 Chain = DAG.getStore(Chain, OpInfo.CallOperand, StackSlot, NULL, 0);
4764 OpInfo.CallOperand = StackSlot;
4765 }
4766
4767 // There is no longer a Value* corresponding to this operand.
4768 OpInfo.CallOperandVal = 0;
4769 // It is now an indirect operand.
4770 OpInfo.isIndirect = true;
4771 }
4772
4773 // If this constraint is for a specific register, allocate it before
4774 // anything else.
4775 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004776 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004777 }
4778 ConstraintInfos.clear();
4779
4780
4781 // Second pass - Loop over all of the operands, assigning virtual or physregs
4782 // to registerclass operands.
4783 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
4784 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
4785
4786 // C_Register operands have already been allocated, Other/Memory don't need
4787 // to be.
4788 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004789 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790 }
4791
4792 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
4793 std::vector<SDValue> AsmNodeOperands;
4794 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
4795 AsmNodeOperands.push_back(
Bill Wendling056292f2008-09-16 21:48:12 +00004796 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797
4798
4799 // Loop over all of the inputs, copying the operand values into the
4800 // appropriate registers and processing the output regs.
4801 RegsForValue RetValRegs;
4802
4803 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
4804 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
4805
4806 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
4807 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
4808
4809 switch (OpInfo.Type) {
4810 case InlineAsm::isOutput: {
4811 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
4812 OpInfo.ConstraintType != TargetLowering::C_Register) {
4813 // Memory output, or 'other' output (e.g. 'X' constraint).
4814 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
4815
4816 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00004817 unsigned ResOpType = 4/*MEM*/ | (1<<3);
4818 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004819 TLI.getPointerTy()));
4820 AsmNodeOperands.push_back(OpInfo.CallOperand);
4821 break;
4822 }
4823
4824 // Otherwise, this is a register or register class output.
4825
4826 // Copy the output from the appropriate register. Find a register that
4827 // we can use.
4828 if (OpInfo.AssignedRegs.Regs.empty()) {
4829 cerr << "Couldn't allocate output reg for constraint '"
4830 << OpInfo.ConstraintCode << "'!\n";
4831 exit(1);
4832 }
4833
4834 // If this is an indirect operand, store through the pointer after the
4835 // asm.
4836 if (OpInfo.isIndirect) {
4837 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
4838 OpInfo.CallOperandVal));
4839 } else {
4840 // This is the result value of the call.
4841 assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
4842 // Concatenate this output onto the outputs list.
4843 RetValRegs.append(OpInfo.AssignedRegs);
4844 }
4845
4846 // Add information to the INLINEASM node to know that this register is
4847 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00004848 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
4849 6 /* EARLYCLOBBER REGDEF */ :
4850 2 /* REGDEF */ ,
4851 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004852 break;
4853 }
4854 case InlineAsm::isInput: {
4855 SDValue InOperandVal = OpInfo.CallOperand;
4856
4857 if (isdigit(OpInfo.ConstraintCode[0])) { // Matching constraint?
4858 // If this is required to match an output register we have already set,
4859 // just use its register.
4860 unsigned OperandNo = atoi(OpInfo.ConstraintCode.c_str());
4861
4862 // Scan until we find the definition we already emitted of this operand.
4863 // When we find it, create a RegsForValue operand.
4864 unsigned CurOp = 2; // The first operand.
4865 for (; OperandNo; --OperandNo) {
4866 // Advance to the next operand.
4867 unsigned NumOps =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004868 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004869 assert(((NumOps & 7) == 2 /*REGDEF*/ ||
Dale Johannesen913d3df2008-09-12 17:49:03 +00004870 (NumOps & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
Dale Johannesen86b49f82008-09-24 01:07:17 +00004871 (NumOps & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004872 "Skipped past definitions?");
4873 CurOp += (NumOps>>3)+1;
4874 }
4875
4876 unsigned NumOps =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004877 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Dale Johannesen913d3df2008-09-12 17:49:03 +00004878 if ((NumOps & 7) == 2 /*REGDEF*/
4879 || (NumOps & 7) == 6 /* EARLYCLOBBER REGDEF */) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004880 // Add NumOps>>3 registers to MatchedRegs.
4881 RegsForValue MatchedRegs;
4882 MatchedRegs.TLI = &TLI;
4883 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
4884 MatchedRegs.RegVTs.push_back(AsmNodeOperands[CurOp+1].getValueType());
4885 for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
4886 unsigned Reg =
4887 cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
4888 MatchedRegs.Regs.push_back(Reg);
4889 }
4890
4891 // Use the produced MatchedRegs object to
4892 MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag);
Dale Johannesen86b49f82008-09-24 01:07:17 +00004893 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004894 break;
4895 } else {
Dale Johannesen86b49f82008-09-24 01:07:17 +00004896 assert(((NumOps & 7) == 4) && "Unknown matching constraint!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004897 assert((NumOps >> 3) == 1 && "Unexpected number of operands");
4898 // Add information to the INLINEASM node to know about this input.
Dale Johannesen91aac102008-09-17 21:13:11 +00004899 AsmNodeOperands.push_back(DAG.getTargetConstant(NumOps,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004900 TLI.getPointerTy()));
4901 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
4902 break;
4903 }
4904 }
4905
4906 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
4907 assert(!OpInfo.isIndirect &&
4908 "Don't know how to handle indirect other inputs yet!");
4909
4910 std::vector<SDValue> Ops;
4911 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00004912 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004913 if (Ops.empty()) {
4914 cerr << "Invalid operand for inline asm constraint '"
4915 << OpInfo.ConstraintCode << "'!\n";
4916 exit(1);
4917 }
4918
4919 // Add information to the INLINEASM node to know about this input.
4920 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
4921 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
4922 TLI.getPointerTy()));
4923 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
4924 break;
4925 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
4926 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
4927 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
4928 "Memory operands expect pointer values");
4929
4930 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00004931 unsigned ResOpType = 4/*MEM*/ | (1<<3);
4932 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004933 TLI.getPointerTy()));
4934 AsmNodeOperands.push_back(InOperandVal);
4935 break;
4936 }
4937
4938 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
4939 OpInfo.ConstraintType == TargetLowering::C_Register) &&
4940 "Unknown constraint type!");
4941 assert(!OpInfo.isIndirect &&
4942 "Don't know how to handle indirect register inputs yet!");
4943
4944 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00004945 if (OpInfo.AssignedRegs.Regs.empty()) {
4946 cerr << "Couldn't allocate output reg for constraint '"
4947 << OpInfo.ConstraintCode << "'!\n";
4948 exit(1);
4949 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004950
4951 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag);
4952
Dale Johannesen86b49f82008-09-24 01:07:17 +00004953 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/,
4954 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004955 break;
4956 }
4957 case InlineAsm::isClobber: {
4958 // Add the clobbered value to the operand list, so that the register
4959 // allocator is aware that the physreg got clobbered.
4960 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00004961 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
4962 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004963 break;
4964 }
4965 }
4966 }
4967
4968 // Finish up input operands.
4969 AsmNodeOperands[0] = Chain;
4970 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
4971
4972 Chain = DAG.getNode(ISD::INLINEASM,
4973 DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2,
4974 &AsmNodeOperands[0], AsmNodeOperands.size());
4975 Flag = Chain.getValue(1);
4976
4977 // If this asm returns a register value, copy the result from that register
4978 // and set it as the value of the call.
4979 if (!RetValRegs.Regs.empty()) {
4980 SDValue Val = RetValRegs.getCopyFromRegs(DAG, Chain, &Flag);
4981
4982 // If any of the results of the inline asm is a vector, it may have the
4983 // wrong width/num elts. This can happen for register classes that can
4984 // contain multiple different value types. The preg or vreg allocated may
4985 // not have the same VT as was expected. Convert it to the right type with
4986 // bit_convert.
4987 if (const StructType *ResSTy = dyn_cast<StructType>(CS.getType())) {
4988 for (unsigned i = 0, e = ResSTy->getNumElements(); i != e; ++i) {
4989 if (Val.getNode()->getValueType(i).isVector())
4990 Val = DAG.getNode(ISD::BIT_CONVERT,
4991 TLI.getValueType(ResSTy->getElementType(i)), Val);
4992 }
4993 } else {
4994 if (Val.getValueType().isVector())
4995 Val = DAG.getNode(ISD::BIT_CONVERT, TLI.getValueType(CS.getType()),
4996 Val);
4997 }
4998
4999 setValue(CS.getInstruction(), Val);
5000 }
5001
5002 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
5003
5004 // Process indirect outputs, first output all of the flagged copies out of
5005 // physregs.
5006 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5007 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5008 Value *Ptr = IndirectStoresToEmit[i].second;
5009 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, Chain, &Flag);
5010 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5011 }
5012
5013 // Emit the non-flagged stores from the physregs.
5014 SmallVector<SDValue, 8> OutChains;
5015 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
5016 OutChains.push_back(DAG.getStore(Chain, StoresToEmit[i].first,
5017 getValue(StoresToEmit[i].second),
5018 StoresToEmit[i].second, 0));
5019 if (!OutChains.empty())
5020 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5021 &OutChains[0], OutChains.size());
5022 DAG.setRoot(Chain);
5023}
5024
5025
5026void SelectionDAGLowering::visitMalloc(MallocInst &I) {
5027 SDValue Src = getValue(I.getOperand(0));
5028
5029 MVT IntPtr = TLI.getPointerTy();
5030
5031 if (IntPtr.bitsLT(Src.getValueType()))
5032 Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
5033 else if (IntPtr.bitsGT(Src.getValueType()))
5034 Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
5035
5036 // Scale the source by the type size.
5037 uint64_t ElementSize = TD->getABITypeSize(I.getType()->getElementType());
5038 Src = DAG.getNode(ISD::MUL, Src.getValueType(),
5039 Src, DAG.getIntPtrConstant(ElementSize));
5040
5041 TargetLowering::ArgListTy Args;
5042 TargetLowering::ArgListEntry Entry;
5043 Entry.Node = Src;
5044 Entry.Ty = TLI.getTargetData()->getIntPtrType();
5045 Args.push_back(Entry);
5046
5047 std::pair<SDValue,SDValue> Result =
5048 TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, CallingConv::C,
Bill Wendling056292f2008-09-16 21:48:12 +00005049 PerformTailCallOpt, DAG.getExternalSymbol("malloc", IntPtr),
Dan Gohman1937e2f2008-09-16 01:42:28 +00005050 Args, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005051 setValue(&I, Result.first); // Pointers always fit in registers
5052 DAG.setRoot(Result.second);
5053}
5054
5055void SelectionDAGLowering::visitFree(FreeInst &I) {
5056 TargetLowering::ArgListTy Args;
5057 TargetLowering::ArgListEntry Entry;
5058 Entry.Node = getValue(I.getOperand(0));
5059 Entry.Ty = TLI.getTargetData()->getIntPtrType();
5060 Args.push_back(Entry);
5061 MVT IntPtr = TLI.getPointerTy();
5062 std::pair<SDValue,SDValue> Result =
5063 TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false,
Dan Gohman1937e2f2008-09-16 01:42:28 +00005064 CallingConv::C, PerformTailCallOpt,
Bill Wendling056292f2008-09-16 21:48:12 +00005065 DAG.getExternalSymbol("free", IntPtr), Args, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005066 DAG.setRoot(Result.second);
5067}
5068
5069void SelectionDAGLowering::visitVAStart(CallInst &I) {
5070 DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(),
5071 getValue(I.getOperand(1)),
5072 DAG.getSrcValue(I.getOperand(1))));
5073}
5074
5075void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
5076 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
5077 getValue(I.getOperand(0)),
5078 DAG.getSrcValue(I.getOperand(0)));
5079 setValue(&I, V);
5080 DAG.setRoot(V.getValue(1));
5081}
5082
5083void SelectionDAGLowering::visitVAEnd(CallInst &I) {
5084 DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(),
5085 getValue(I.getOperand(1)),
5086 DAG.getSrcValue(I.getOperand(1))));
5087}
5088
5089void SelectionDAGLowering::visitVACopy(CallInst &I) {
5090 DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(),
5091 getValue(I.getOperand(1)),
5092 getValue(I.getOperand(2)),
5093 DAG.getSrcValue(I.getOperand(1)),
5094 DAG.getSrcValue(I.getOperand(2))));
5095}
5096
5097/// TargetLowering::LowerArguments - This is the default LowerArguments
5098/// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all
5099/// targets are migrated to using FORMAL_ARGUMENTS, this hook should be
5100/// integrated into SDISel.
5101void TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG,
5102 SmallVectorImpl<SDValue> &ArgValues) {
5103 // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node.
5104 SmallVector<SDValue, 3+16> Ops;
5105 Ops.push_back(DAG.getRoot());
5106 Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy()));
5107 Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy()));
5108
5109 // Add one result value for each formal argument.
5110 SmallVector<MVT, 16> RetVals;
5111 unsigned j = 1;
5112 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5113 I != E; ++I, ++j) {
5114 SmallVector<MVT, 4> ValueVTs;
5115 ComputeValueVTs(*this, I->getType(), ValueVTs);
5116 for (unsigned Value = 0, NumValues = ValueVTs.size();
5117 Value != NumValues; ++Value) {
5118 MVT VT = ValueVTs[Value];
5119 const Type *ArgTy = VT.getTypeForMVT();
5120 ISD::ArgFlagsTy Flags;
5121 unsigned OriginalAlignment =
5122 getTargetData()->getABITypeAlignment(ArgTy);
5123
5124 if (F.paramHasAttr(j, ParamAttr::ZExt))
5125 Flags.setZExt();
5126 if (F.paramHasAttr(j, ParamAttr::SExt))
5127 Flags.setSExt();
5128 if (F.paramHasAttr(j, ParamAttr::InReg))
5129 Flags.setInReg();
5130 if (F.paramHasAttr(j, ParamAttr::StructRet))
5131 Flags.setSRet();
5132 if (F.paramHasAttr(j, ParamAttr::ByVal)) {
5133 Flags.setByVal();
5134 const PointerType *Ty = cast<PointerType>(I->getType());
5135 const Type *ElementTy = Ty->getElementType();
5136 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
5137 unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy);
5138 // For ByVal, alignment should be passed from FE. BE will guess if
5139 // this info is not there but there are cases it cannot get right.
5140 if (F.getParamAlignment(j))
5141 FrameAlign = F.getParamAlignment(j);
5142 Flags.setByValAlign(FrameAlign);
5143 Flags.setByValSize(FrameSize);
5144 }
5145 if (F.paramHasAttr(j, ParamAttr::Nest))
5146 Flags.setNest();
5147 Flags.setOrigAlign(OriginalAlignment);
5148
5149 MVT RegisterVT = getRegisterType(VT);
5150 unsigned NumRegs = getNumRegisters(VT);
5151 for (unsigned i = 0; i != NumRegs; ++i) {
5152 RetVals.push_back(RegisterVT);
5153 ISD::ArgFlagsTy MyFlags = Flags;
5154 if (NumRegs > 1 && i == 0)
5155 MyFlags.setSplit();
5156 // if it isn't first piece, alignment must be 1
5157 else if (i > 0)
5158 MyFlags.setOrigAlign(1);
5159 Ops.push_back(DAG.getArgFlags(MyFlags));
5160 }
5161 }
5162 }
5163
5164 RetVals.push_back(MVT::Other);
5165
5166 // Create the node.
5167 SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS,
5168 DAG.getVTList(&RetVals[0], RetVals.size()),
5169 &Ops[0], Ops.size()).getNode();
5170
5171 // Prelower FORMAL_ARGUMENTS. This isn't required for functionality, but
5172 // allows exposing the loads that may be part of the argument access to the
5173 // first DAGCombiner pass.
5174 SDValue TmpRes = LowerOperation(SDValue(Result, 0), DAG);
5175
5176 // The number of results should match up, except that the lowered one may have
5177 // an extra flag result.
5178 assert((Result->getNumValues() == TmpRes.getNode()->getNumValues() ||
5179 (Result->getNumValues()+1 == TmpRes.getNode()->getNumValues() &&
5180 TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag))
5181 && "Lowering produced unexpected number of results!");
5182
5183 // The FORMAL_ARGUMENTS node itself is likely no longer needed.
5184 if (Result != TmpRes.getNode() && Result->use_empty()) {
5185 HandleSDNode Dummy(DAG.getRoot());
5186 DAG.RemoveDeadNode(Result);
5187 }
5188
5189 Result = TmpRes.getNode();
5190
5191 unsigned NumArgRegs = Result->getNumValues() - 1;
5192 DAG.setRoot(SDValue(Result, NumArgRegs));
5193
5194 // Set up the return result vector.
5195 unsigned i = 0;
5196 unsigned Idx = 1;
5197 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5198 ++I, ++Idx) {
5199 SmallVector<MVT, 4> ValueVTs;
5200 ComputeValueVTs(*this, I->getType(), ValueVTs);
5201 for (unsigned Value = 0, NumValues = ValueVTs.size();
5202 Value != NumValues; ++Value) {
5203 MVT VT = ValueVTs[Value];
5204 MVT PartVT = getRegisterType(VT);
5205
5206 unsigned NumParts = getNumRegisters(VT);
5207 SmallVector<SDValue, 4> Parts(NumParts);
5208 for (unsigned j = 0; j != NumParts; ++j)
5209 Parts[j] = SDValue(Result, i++);
5210
5211 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5212 if (F.paramHasAttr(Idx, ParamAttr::SExt))
5213 AssertOp = ISD::AssertSext;
5214 else if (F.paramHasAttr(Idx, ParamAttr::ZExt))
5215 AssertOp = ISD::AssertZext;
5216
5217 ArgValues.push_back(getCopyFromParts(DAG, &Parts[0], NumParts, PartVT, VT,
5218 AssertOp));
5219 }
5220 }
5221 assert(i == NumArgRegs && "Argument register count mismatch!");
5222}
5223
5224
5225/// TargetLowering::LowerCallTo - This is the default LowerCallTo
5226/// implementation, which just inserts an ISD::CALL node, which is later custom
5227/// lowered by the target to something concrete. FIXME: When all targets are
5228/// migrated to using ISD::CALL, this hook should be integrated into SDISel.
5229std::pair<SDValue, SDValue>
5230TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5231 bool RetSExt, bool RetZExt, bool isVarArg,
5232 unsigned CallingConv, bool isTailCall,
5233 SDValue Callee,
5234 ArgListTy &Args, SelectionDAG &DAG) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00005235 assert((!isTailCall || PerformTailCallOpt) &&
5236 "isTailCall set when tail-call optimizations are disabled!");
5237
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005238 SmallVector<SDValue, 32> Ops;
5239 Ops.push_back(Chain); // Op#0 - Chain
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 Ops.push_back(Callee);
5241
5242 // Handle all of the outgoing arguments.
5243 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
5244 SmallVector<MVT, 4> ValueVTs;
5245 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5246 for (unsigned Value = 0, NumValues = ValueVTs.size();
5247 Value != NumValues; ++Value) {
5248 MVT VT = ValueVTs[Value];
5249 const Type *ArgTy = VT.getTypeForMVT();
5250 SDValue Op = SDValue(Args[i].Node.getNode(), Args[i].Node.getResNo() + Value);
5251 ISD::ArgFlagsTy Flags;
5252 unsigned OriginalAlignment =
5253 getTargetData()->getABITypeAlignment(ArgTy);
5254
5255 if (Args[i].isZExt)
5256 Flags.setZExt();
5257 if (Args[i].isSExt)
5258 Flags.setSExt();
5259 if (Args[i].isInReg)
5260 Flags.setInReg();
5261 if (Args[i].isSRet)
5262 Flags.setSRet();
5263 if (Args[i].isByVal) {
5264 Flags.setByVal();
5265 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5266 const Type *ElementTy = Ty->getElementType();
5267 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
5268 unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy);
5269 // For ByVal, alignment should come from FE. BE will guess if this
5270 // info is not there but there are cases it cannot get right.
5271 if (Args[i].Alignment)
5272 FrameAlign = Args[i].Alignment;
5273 Flags.setByValAlign(FrameAlign);
5274 Flags.setByValSize(FrameSize);
5275 }
5276 if (Args[i].isNest)
5277 Flags.setNest();
5278 Flags.setOrigAlign(OriginalAlignment);
5279
5280 MVT PartVT = getRegisterType(VT);
5281 unsigned NumParts = getNumRegisters(VT);
5282 SmallVector<SDValue, 4> Parts(NumParts);
5283 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5284
5285 if (Args[i].isSExt)
5286 ExtendKind = ISD::SIGN_EXTEND;
5287 else if (Args[i].isZExt)
5288 ExtendKind = ISD::ZERO_EXTEND;
5289
5290 getCopyToParts(DAG, Op, &Parts[0], NumParts, PartVT, ExtendKind);
5291
5292 for (unsigned i = 0; i != NumParts; ++i) {
5293 // if it isn't first piece, alignment must be 1
5294 ISD::ArgFlagsTy MyFlags = Flags;
5295 if (NumParts > 1 && i == 0)
5296 MyFlags.setSplit();
5297 else if (i != 0)
5298 MyFlags.setOrigAlign(1);
5299
5300 Ops.push_back(Parts[i]);
5301 Ops.push_back(DAG.getArgFlags(MyFlags));
5302 }
5303 }
5304 }
5305
5306 // Figure out the result value types. We start by making a list of
5307 // the potentially illegal return value types.
5308 SmallVector<MVT, 4> LoweredRetTys;
5309 SmallVector<MVT, 4> RetTys;
5310 ComputeValueVTs(*this, RetTy, RetTys);
5311
5312 // Then we translate that to a list of legal types.
5313 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5314 MVT VT = RetTys[I];
5315 MVT RegisterVT = getRegisterType(VT);
5316 unsigned NumRegs = getNumRegisters(VT);
5317 for (unsigned i = 0; i != NumRegs; ++i)
5318 LoweredRetTys.push_back(RegisterVT);
5319 }
5320
5321 LoweredRetTys.push_back(MVT::Other); // Always has a chain.
5322
5323 // Create the CALL node.
Dan Gohman095cc292008-09-13 01:54:27 +00005324 SDValue Res = DAG.getCall(CallingConv, isVarArg, isTailCall,
5325 DAG.getVTList(&LoweredRetTys[0],
5326 LoweredRetTys.size()),
5327 &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005328 Chain = Res.getValue(LoweredRetTys.size() - 1);
5329
5330 // Gather up the call result into a single value.
5331 if (RetTy != Type::VoidTy) {
5332 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5333
5334 if (RetSExt)
5335 AssertOp = ISD::AssertSext;
5336 else if (RetZExt)
5337 AssertOp = ISD::AssertZext;
5338
5339 SmallVector<SDValue, 4> ReturnValues;
5340 unsigned RegNo = 0;
5341 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5342 MVT VT = RetTys[I];
5343 MVT RegisterVT = getRegisterType(VT);
5344 unsigned NumRegs = getNumRegisters(VT);
5345 unsigned RegNoEnd = NumRegs + RegNo;
5346 SmallVector<SDValue, 4> Results;
5347 for (; RegNo != RegNoEnd; ++RegNo)
5348 Results.push_back(Res.getValue(RegNo));
5349 SDValue ReturnValue =
5350 getCopyFromParts(DAG, &Results[0], NumRegs, RegisterVT, VT,
5351 AssertOp);
5352 ReturnValues.push_back(ReturnValue);
5353 }
5354 Res = DAG.getMergeValues(DAG.getVTList(&RetTys[0], RetTys.size()),
5355 &ReturnValues[0], ReturnValues.size());
5356 }
5357
5358 return std::make_pair(Res, Chain);
5359}
5360
5361SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
5362 assert(0 && "LowerOperation not implemented for this target!");
5363 abort();
5364 return SDValue();
5365}
5366
5367
5368void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
5369 SDValue Op = getValue(V);
5370 assert((Op.getOpcode() != ISD::CopyFromReg ||
5371 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5372 "Copy from a reg to the same reg!");
5373 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5374
5375 RegsForValue RFV(TLI, Reg, V->getType());
5376 SDValue Chain = DAG.getEntryNode();
5377 RFV.getCopyToRegs(Op, DAG, Chain, 0);
5378 PendingExports.push_back(Chain);
5379}
5380
5381#include "llvm/CodeGen/SelectionDAGISel.h"
5382
5383void SelectionDAGISel::
5384LowerArguments(BasicBlock *LLVMBB) {
5385 // If this is the entry block, emit arguments.
5386 Function &F = *LLVMBB->getParent();
5387 SDValue OldRoot = SDL->DAG.getRoot();
5388 SmallVector<SDValue, 16> Args;
5389 TLI.LowerArguments(F, SDL->DAG, Args);
5390
5391 unsigned a = 0;
5392 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
5393 AI != E; ++AI) {
5394 SmallVector<MVT, 4> ValueVTs;
5395 ComputeValueVTs(TLI, AI->getType(), ValueVTs);
5396 unsigned NumValues = ValueVTs.size();
5397 if (!AI->use_empty()) {
5398 SDL->setValue(AI, SDL->DAG.getMergeValues(&Args[a], NumValues));
5399 // If this argument is live outside of the entry block, insert a copy from
5400 // whereever we got it to the vreg that other BB's will reference it as.
5401 DenseMap<const Value*, unsigned>::iterator VMI=FuncInfo->ValueMap.find(AI);
5402 if (VMI != FuncInfo->ValueMap.end()) {
5403 SDL->CopyValueToVirtualRegister(AI, VMI->second);
5404 }
5405 }
5406 a += NumValues;
5407 }
5408
5409 // Finally, if the target has anything special to do, allow it to do so.
5410 // FIXME: this should insert code into the DAG!
5411 EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction());
5412}
5413
5414/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5415/// ensure constants are generated when needed. Remember the virtual registers
5416/// that need to be added to the Machine PHI nodes as input. We cannot just
5417/// directly add them, because expansion might result in multiple MBB's for one
5418/// BB. As such, the start of the BB might correspond to a different MBB than
5419/// the end.
5420///
5421void
5422SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5423 TerminatorInst *TI = LLVMBB->getTerminator();
5424
5425 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5426
5427 // Check successor nodes' PHI nodes that expect a constant to be available
5428 // from this block.
5429 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5430 BasicBlock *SuccBB = TI->getSuccessor(succ);
5431 if (!isa<PHINode>(SuccBB->begin())) continue;
5432 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
5433
5434 // If this terminator has multiple identical successors (common for
5435 // switches), only handle each succ once.
5436 if (!SuccsHandled.insert(SuccMBB)) continue;
5437
5438 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5439 PHINode *PN;
5440
5441 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5442 // nodes and Machine PHI nodes, but the incoming operands have not been
5443 // emitted yet.
5444 for (BasicBlock::iterator I = SuccBB->begin();
5445 (PN = dyn_cast<PHINode>(I)); ++I) {
5446 // Ignore dead phi's.
5447 if (PN->use_empty()) continue;
5448
5449 unsigned Reg;
5450 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5451
5452 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
5453 unsigned &RegOut = SDL->ConstantsOut[C];
5454 if (RegOut == 0) {
5455 RegOut = FuncInfo->CreateRegForValue(C);
5456 SDL->CopyValueToVirtualRegister(C, RegOut);
5457 }
5458 Reg = RegOut;
5459 } else {
5460 Reg = FuncInfo->ValueMap[PHIOp];
5461 if (Reg == 0) {
5462 assert(isa<AllocaInst>(PHIOp) &&
5463 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5464 "Didn't codegen value into a register!??");
5465 Reg = FuncInfo->CreateRegForValue(PHIOp);
5466 SDL->CopyValueToVirtualRegister(PHIOp, Reg);
5467 }
5468 }
5469
5470 // Remember that this register needs to added to the machine PHI node as
5471 // the input for this MBB.
5472 SmallVector<MVT, 4> ValueVTs;
5473 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5474 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
5475 MVT VT = ValueVTs[vti];
5476 unsigned NumRegisters = TLI.getNumRegisters(VT);
5477 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
5478 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
5479 Reg += NumRegisters;
5480 }
5481 }
5482 }
5483 SDL->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005484}
5485
Dan Gohman3df24e62008-09-03 23:12:08 +00005486/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5487/// supports legal types, and it emits MachineInstrs directly instead of
5488/// creating SelectionDAG nodes.
5489///
5490bool
5491SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5492 FastISel *F) {
5493 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005494
Dan Gohman3df24e62008-09-03 23:12:08 +00005495 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5496 unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size();
5497
5498 // Check successor nodes' PHI nodes that expect a constant to be available
5499 // from this block.
5500 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5501 BasicBlock *SuccBB = TI->getSuccessor(succ);
5502 if (!isa<PHINode>(SuccBB->begin())) continue;
5503 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
5504
5505 // If this terminator has multiple identical successors (common for
5506 // switches), only handle each succ once.
5507 if (!SuccsHandled.insert(SuccMBB)) continue;
5508
5509 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5510 PHINode *PN;
5511
5512 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5513 // nodes and Machine PHI nodes, but the incoming operands have not been
5514 // emitted yet.
5515 for (BasicBlock::iterator I = SuccBB->begin();
5516 (PN = dyn_cast<PHINode>(I)); ++I) {
5517 // Ignore dead phi's.
5518 if (PN->use_empty()) continue;
5519
5520 // Only handle legal types. Two interesting things to note here. First,
5521 // by bailing out early, we may leave behind some dead instructions,
5522 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
5523 // own moves. Second, this check is necessary becuase FastISel doesn't
5524 // use CreateRegForValue to create registers, so it always creates
5525 // exactly one register for each non-void instruction.
5526 MVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
5527 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Dan Gohman74321ab2008-09-10 21:01:31 +00005528 // Promote MVT::i1.
5529 if (VT == MVT::i1)
5530 VT = TLI.getTypeToTransformTo(VT);
5531 else {
5532 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5533 return false;
5534 }
Dan Gohman3df24e62008-09-03 23:12:08 +00005535 }
5536
5537 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5538
5539 unsigned Reg = F->getRegForValue(PHIOp);
5540 if (Reg == 0) {
5541 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5542 return false;
5543 }
5544 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
5545 }
5546 }
5547
5548 return true;
5549}