blob: dc99879490120fd2e131b0f412cfa18be2576f02 [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
Dale Johannesenc9c6da62008-09-25 20:47:45 +0000916 // at least 32-bit. But this is not necessary for non-C calling
917 // conventions.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000918 if (VT.isInteger()) {
919 MVT MinVT = TLI.getRegisterType(MVT::i32);
920 if (VT.bitsLT(MinVT))
921 VT = MinVT;
922 }
923
924 unsigned NumParts = TLI.getNumRegisters(VT);
925 MVT PartVT = TLI.getRegisterType(VT);
926 SmallVector<SDValue, 4> Parts(NumParts);
927 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
928
929 const Function *F = I.getParent()->getParent();
Devang Patel05988662008-09-25 21:00:45 +0000930 if (F->paramHasAttr(0, Attribute::SExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000931 ExtendKind = ISD::SIGN_EXTEND;
Devang Patel05988662008-09-25 21:00:45 +0000932 else if (F->paramHasAttr(0, Attribute::ZExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000933 ExtendKind = ISD::ZERO_EXTEND;
934
935 getCopyToParts(DAG, SDValue(RetOp.getNode(), RetOp.getResNo() + j),
936 &Parts[0], NumParts, PartVT, ExtendKind);
937
Dale Johannesenc9c6da62008-09-25 20:47:45 +0000938 // 'inreg' on function refers to return value
939 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Devang Patel05988662008-09-25 21:00:45 +0000940 if (F->paramHasAttr(0, Attribute::InReg))
Dale Johannesenc9c6da62008-09-25 20:47:45 +0000941 Flags.setInReg();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000942 for (unsigned i = 0; i < NumParts; ++i) {
943 NewValues.push_back(Parts[i]);
Dale Johannesenc9c6da62008-09-25 20:47:45 +0000944 NewValues.push_back(DAG.getArgFlags(Flags));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000945 }
946 }
947 }
948 DAG.setRoot(DAG.getNode(ISD::RET, MVT::Other,
949 &NewValues[0], NewValues.size()));
950}
951
952/// ExportFromCurrentBlock - If this condition isn't known to be exported from
953/// the current basic block, add it to ValueMap now so that we'll get a
954/// CopyTo/FromReg.
955void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
956 // No need to export constants.
957 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
958
959 // Already exported?
960 if (FuncInfo.isExportedInst(V)) return;
961
962 unsigned Reg = FuncInfo.InitializeRegForValue(V);
963 CopyValueToVirtualRegister(V, Reg);
964}
965
966bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
967 const BasicBlock *FromBB) {
968 // The operands of the setcc have to be in this block. We don't know
969 // how to export them from some other block.
970 if (Instruction *VI = dyn_cast<Instruction>(V)) {
971 // Can export from current BB.
972 if (VI->getParent() == FromBB)
973 return true;
974
975 // Is already exported, noop.
976 return FuncInfo.isExportedInst(V);
977 }
978
979 // If this is an argument, we can export it if the BB is the entry block or
980 // if it is already exported.
981 if (isa<Argument>(V)) {
982 if (FromBB == &FromBB->getParent()->getEntryBlock())
983 return true;
984
985 // Otherwise, can only export this if it is already exported.
986 return FuncInfo.isExportedInst(V);
987 }
988
989 // Otherwise, constants can always be exported.
990 return true;
991}
992
993static bool InBlock(const Value *V, const BasicBlock *BB) {
994 if (const Instruction *I = dyn_cast<Instruction>(V))
995 return I->getParent() == BB;
996 return true;
997}
998
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000999/// getFCmpCondCode - Return the ISD condition code corresponding to
1000/// the given LLVM IR floating-point condition code. This includes
1001/// consideration of global floating-point math flags.
1002///
1003static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1004 ISD::CondCode FPC, FOC;
1005 switch (Pred) {
1006 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1007 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1008 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1009 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1010 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1011 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1012 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1013 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1014 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1015 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1016 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1017 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1018 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1019 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1020 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1021 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1022 default:
1023 assert(0 && "Invalid FCmp predicate opcode!");
1024 FOC = FPC = ISD::SETFALSE;
1025 break;
1026 }
1027 if (FiniteOnlyFPMath())
1028 return FOC;
1029 else
1030 return FPC;
1031}
1032
1033/// getICmpCondCode - Return the ISD condition code corresponding to
1034/// the given LLVM IR integer condition code.
1035///
1036static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1037 switch (Pred) {
1038 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1039 case ICmpInst::ICMP_NE: return ISD::SETNE;
1040 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1041 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1042 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1043 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1044 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1045 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1046 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1047 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1048 default:
1049 assert(0 && "Invalid ICmp predicate opcode!");
1050 return ISD::SETNE;
1051 }
1052}
1053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001054/// FindMergedConditions - If Cond is an expression like
1055void SelectionDAGLowering::FindMergedConditions(Value *Cond,
1056 MachineBasicBlock *TBB,
1057 MachineBasicBlock *FBB,
1058 MachineBasicBlock *CurBB,
1059 unsigned Opc) {
1060 // If this node is not part of the or/and tree, emit it as a branch.
1061 Instruction *BOp = dyn_cast<Instruction>(Cond);
1062
1063 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
1064 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1065 BOp->getParent() != CurBB->getBasicBlock() ||
1066 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1067 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1068 const BasicBlock *BB = CurBB->getBasicBlock();
1069
1070 // If the leaf of the tree is a comparison, merge the condition into
1071 // the caseblock.
Chris Lattner3c261012008-10-11 00:08:02 +00001072 if (isa<CmpInst>(Cond) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001073 // The operands of the cmp have to be in this block. We don't know
1074 // how to export them from some other block. If this is the first block
1075 // of the sequence, no exporting is needed.
1076 (CurBB == CurMBB ||
1077 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1078 isExportableFromCurrentBlock(BOp->getOperand(1), BB)))) {
1079 BOp = cast<Instruction>(Cond);
1080 ISD::CondCode Condition;
1081 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001082 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001084 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001085 } else {
1086 Condition = ISD::SETEQ; // silence warning.
1087 assert(0 && "Unknown compare instruction");
1088 }
1089
1090 CaseBlock CB(Condition, BOp->getOperand(0),
1091 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1092 SwitchCases.push_back(CB);
1093 return;
1094 }
1095
1096 // Create a CaseBlock record representing this branch.
1097 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(),
1098 NULL, TBB, FBB, CurBB);
1099 SwitchCases.push_back(CB);
1100 return;
1101 }
1102
1103
1104 // Create TmpBB after CurBB.
1105 MachineFunction::iterator BBI = CurBB;
1106 MachineFunction &MF = DAG.getMachineFunction();
1107 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1108 CurBB->getParent()->insert(++BBI, TmpBB);
1109
1110 if (Opc == Instruction::Or) {
1111 // Codegen X | Y as:
1112 // jmp_if_X TBB
1113 // jmp TmpBB
1114 // TmpBB:
1115 // jmp_if_Y TBB
1116 // jmp FBB
1117 //
1118
1119 // Emit the LHS condition.
1120 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
1121
1122 // Emit the RHS condition into TmpBB.
1123 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1124 } else {
1125 assert(Opc == Instruction::And && "Unknown merge op!");
1126 // Codegen X & Y as:
1127 // jmp_if_X TmpBB
1128 // jmp FBB
1129 // TmpBB:
1130 // jmp_if_Y TBB
1131 // jmp FBB
1132 //
1133 // This requires creation of TmpBB after CurBB.
1134
1135 // Emit the LHS condition.
1136 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
1137
1138 // Emit the RHS condition into TmpBB.
1139 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1140 }
1141}
1142
1143/// If the set of cases should be emitted as a series of branches, return true.
1144/// If we should emit this as a bunch of and/or'd together conditions, return
1145/// false.
1146bool
1147SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
1148 if (Cases.size() != 2) return true;
1149
1150 // If this is two comparisons of the same values or'd or and'd together, they
1151 // will get folded into a single comparison, so don't emit two blocks.
1152 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1153 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1154 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1155 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1156 return false;
1157 }
1158
1159 return true;
1160}
1161
1162void SelectionDAGLowering::visitBr(BranchInst &I) {
1163 // Update machine-CFG edges.
1164 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1165
1166 // Figure out which block is immediately after the current one.
1167 MachineBasicBlock *NextBlock = 0;
1168 MachineFunction::iterator BBI = CurMBB;
1169 if (++BBI != CurMBB->getParent()->end())
1170 NextBlock = BBI;
1171
1172 if (I.isUnconditional()) {
1173 // Update machine-CFG edges.
1174 CurMBB->addSuccessor(Succ0MBB);
1175
1176 // If this is not a fall-through branch, emit the branch.
1177 if (Succ0MBB != NextBlock)
1178 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1179 DAG.getBasicBlock(Succ0MBB)));
1180 return;
1181 }
1182
1183 // If this condition is one of the special cases we handle, do special stuff
1184 // now.
1185 Value *CondVal = I.getCondition();
1186 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1187
1188 // If this is a series of conditions that are or'd or and'd together, emit
1189 // this as a sequence of branches instead of setcc's with and/or operations.
1190 // For example, instead of something like:
1191 // cmp A, B
1192 // C = seteq
1193 // cmp D, E
1194 // F = setle
1195 // or C, F
1196 // jnz foo
1197 // Emit:
1198 // cmp A, B
1199 // je foo
1200 // cmp D, E
1201 // jle foo
1202 //
1203 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
1204 if (BOp->hasOneUse() &&
1205 (BOp->getOpcode() == Instruction::And ||
1206 BOp->getOpcode() == Instruction::Or)) {
1207 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1208 // If the compares in later blocks need to use values not currently
1209 // exported from this block, export them now. This block should always
1210 // be the first entry.
1211 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
1212
1213 // Allow some cases to be rejected.
1214 if (ShouldEmitAsBranches(SwitchCases)) {
1215 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1216 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1217 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1218 }
1219
1220 // Emit the branch for this block.
1221 visitSwitchCase(SwitchCases[0]);
1222 SwitchCases.erase(SwitchCases.begin());
1223 return;
1224 }
1225
1226 // Okay, we decided not to do this, remove any inserted MBB's and clear
1227 // SwitchCases.
1228 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
1229 CurMBB->getParent()->erase(SwitchCases[i].ThisBB);
1230
1231 SwitchCases.clear();
1232 }
1233 }
1234
1235 // Create a CaseBlock record representing this branch.
1236 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(),
1237 NULL, Succ0MBB, Succ1MBB, CurMBB);
1238 // Use visitSwitchCase to actually insert the fast branch sequence for this
1239 // cond branch.
1240 visitSwitchCase(CB);
1241}
1242
1243/// visitSwitchCase - Emits the necessary code to represent a single node in
1244/// the binary search tree resulting from lowering a switch instruction.
1245void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) {
1246 SDValue Cond;
1247 SDValue CondLHS = getValue(CB.CmpLHS);
1248
1249 // Build the setcc now.
1250 if (CB.CmpMHS == NULL) {
1251 // Fold "(X == true)" to X and "(X == false)" to !X to
1252 // handle common cases produced by branch lowering.
1253 if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ)
1254 Cond = CondLHS;
1255 else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) {
1256 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
1257 Cond = DAG.getNode(ISD::XOR, CondLHS.getValueType(), CondLHS, True);
1258 } else
1259 Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
1260 } else {
1261 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1262
1263 uint64_t Low = cast<ConstantInt>(CB.CmpLHS)->getSExtValue();
1264 uint64_t High = cast<ConstantInt>(CB.CmpRHS)->getSExtValue();
1265
1266 SDValue CmpOp = getValue(CB.CmpMHS);
1267 MVT VT = CmpOp.getValueType();
1268
1269 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
1270 Cond = DAG.getSetCC(MVT::i1, CmpOp, DAG.getConstant(High, VT), ISD::SETLE);
1271 } else {
1272 SDValue SUB = DAG.getNode(ISD::SUB, VT, CmpOp, DAG.getConstant(Low, VT));
1273 Cond = DAG.getSetCC(MVT::i1, SUB,
1274 DAG.getConstant(High-Low, VT), ISD::SETULE);
1275 }
1276 }
1277
1278 // Update successor info
1279 CurMBB->addSuccessor(CB.TrueBB);
1280 CurMBB->addSuccessor(CB.FalseBB);
1281
1282 // Set NextBlock to be the MBB immediately after the current one, if any.
1283 // This is used to avoid emitting unnecessary branches to the next block.
1284 MachineBasicBlock *NextBlock = 0;
1285 MachineFunction::iterator BBI = CurMBB;
1286 if (++BBI != CurMBB->getParent()->end())
1287 NextBlock = BBI;
1288
1289 // If the lhs block is the next block, invert the condition so that we can
1290 // fall through to the lhs instead of the rhs block.
1291 if (CB.TrueBB == NextBlock) {
1292 std::swap(CB.TrueBB, CB.FalseBB);
1293 SDValue True = DAG.getConstant(1, Cond.getValueType());
1294 Cond = DAG.getNode(ISD::XOR, Cond.getValueType(), Cond, True);
1295 }
1296 SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(), Cond,
1297 DAG.getBasicBlock(CB.TrueBB));
1298
1299 // If the branch was constant folded, fix up the CFG.
1300 if (BrCond.getOpcode() == ISD::BR) {
1301 CurMBB->removeSuccessor(CB.FalseBB);
1302 DAG.setRoot(BrCond);
1303 } else {
1304 // Otherwise, go ahead and insert the false branch.
1305 if (BrCond == getControlRoot())
1306 CurMBB->removeSuccessor(CB.TrueBB);
1307
1308 if (CB.FalseBB == NextBlock)
1309 DAG.setRoot(BrCond);
1310 else
1311 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond,
1312 DAG.getBasicBlock(CB.FalseBB)));
1313 }
1314}
1315
1316/// visitJumpTable - Emit JumpTable node in the current MBB
1317void SelectionDAGLowering::visitJumpTable(JumpTable &JT) {
1318 // Emit the code for the jump table
1319 assert(JT.Reg != -1U && "Should lower JT Header first!");
1320 MVT PTy = TLI.getPointerTy();
1321 SDValue Index = DAG.getCopyFromReg(getControlRoot(), JT.Reg, PTy);
1322 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
1323 DAG.setRoot(DAG.getNode(ISD::BR_JT, MVT::Other, Index.getValue(1),
1324 Table, Index));
1325 return;
1326}
1327
1328/// visitJumpTableHeader - This function emits necessary code to produce index
1329/// in the JumpTable from switch case.
1330void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT,
1331 JumpTableHeader &JTH) {
1332 // Subtract the lowest switch case value from the value being switched on
1333 // and conditional branch to default mbb if the result is greater than the
1334 // difference between smallest and largest cases.
1335 SDValue SwitchOp = getValue(JTH.SValue);
1336 MVT VT = SwitchOp.getValueType();
1337 SDValue SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
1338 DAG.getConstant(JTH.First, VT));
1339
1340 // The SDNode we just created, which holds the value being switched on
1341 // minus the the smallest case value, needs to be copied to a virtual
1342 // register so it can be used as an index into the jump table in a
1343 // subsequent basic block. This value may be smaller or larger than the
1344 // target's pointer type, and therefore require extension or truncating.
1345 if (VT.bitsGT(TLI.getPointerTy()))
1346 SwitchOp = DAG.getNode(ISD::TRUNCATE, TLI.getPointerTy(), SUB);
1347 else
1348 SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(), SUB);
1349
1350 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
1351 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), JumpTableReg, SwitchOp);
1352 JT.Reg = JumpTableReg;
1353
1354 // Emit the range check for the jump table, and branch to the default
1355 // block for the switch statement if the value being switched on exceeds
1356 // the largest case in the switch.
1357 SDValue CMP = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB,
1358 DAG.getConstant(JTH.Last-JTH.First,VT),
1359 ISD::SETUGT);
1360
1361 // Set NextBlock to be the MBB immediately after the current one, if any.
1362 // This is used to avoid emitting unnecessary branches to the next block.
1363 MachineBasicBlock *NextBlock = 0;
1364 MachineFunction::iterator BBI = CurMBB;
1365 if (++BBI != CurMBB->getParent()->end())
1366 NextBlock = BBI;
1367
1368 SDValue BrCond = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, CMP,
1369 DAG.getBasicBlock(JT.Default));
1370
1371 if (JT.MBB == NextBlock)
1372 DAG.setRoot(BrCond);
1373 else
1374 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrCond,
1375 DAG.getBasicBlock(JT.MBB)));
1376
1377 return;
1378}
1379
1380/// visitBitTestHeader - This function emits necessary code to produce value
1381/// suitable for "bit tests"
1382void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) {
1383 // Subtract the minimum value
1384 SDValue SwitchOp = getValue(B.SValue);
1385 MVT VT = SwitchOp.getValueType();
1386 SDValue SUB = DAG.getNode(ISD::SUB, VT, SwitchOp,
1387 DAG.getConstant(B.First, VT));
1388
1389 // Check range
1390 SDValue RangeCmp = DAG.getSetCC(TLI.getSetCCResultType(SUB), SUB,
1391 DAG.getConstant(B.Range, VT),
1392 ISD::SETUGT);
1393
1394 SDValue ShiftOp;
1395 if (VT.bitsGT(TLI.getShiftAmountTy()))
1396 ShiftOp = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), SUB);
1397 else
1398 ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, TLI.getShiftAmountTy(), SUB);
1399
1400 // Make desired shift
1401 SDValue SwitchVal = DAG.getNode(ISD::SHL, TLI.getPointerTy(),
1402 DAG.getConstant(1, TLI.getPointerTy()),
1403 ShiftOp);
1404
1405 unsigned SwitchReg = FuncInfo.MakeReg(TLI.getPointerTy());
1406 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), SwitchReg, SwitchVal);
1407 B.Reg = SwitchReg;
1408
1409 // Set NextBlock to be the MBB immediately after the current one, if any.
1410 // This is used to avoid emitting unnecessary branches to the next block.
1411 MachineBasicBlock *NextBlock = 0;
1412 MachineFunction::iterator BBI = CurMBB;
1413 if (++BBI != CurMBB->getParent()->end())
1414 NextBlock = BBI;
1415
1416 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1417
1418 CurMBB->addSuccessor(B.Default);
1419 CurMBB->addSuccessor(MBB);
1420
1421 SDValue BrRange = DAG.getNode(ISD::BRCOND, MVT::Other, CopyTo, RangeCmp,
1422 DAG.getBasicBlock(B.Default));
1423
1424 if (MBB == NextBlock)
1425 DAG.setRoot(BrRange);
1426 else
1427 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, CopyTo,
1428 DAG.getBasicBlock(MBB)));
1429
1430 return;
1431}
1432
1433/// visitBitTestCase - this function produces one "bit test"
1434void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB,
1435 unsigned Reg,
1436 BitTestCase &B) {
1437 // Emit bit tests and jumps
1438 SDValue SwitchVal = DAG.getCopyFromReg(getControlRoot(), Reg,
1439 TLI.getPointerTy());
1440
1441 SDValue AndOp = DAG.getNode(ISD::AND, TLI.getPointerTy(), SwitchVal,
1442 DAG.getConstant(B.Mask, TLI.getPointerTy()));
1443 SDValue AndCmp = DAG.getSetCC(TLI.getSetCCResultType(AndOp), AndOp,
1444 DAG.getConstant(0, TLI.getPointerTy()),
1445 ISD::SETNE);
1446
1447 CurMBB->addSuccessor(B.TargetBB);
1448 CurMBB->addSuccessor(NextMBB);
1449
1450 SDValue BrAnd = DAG.getNode(ISD::BRCOND, MVT::Other, getControlRoot(),
1451 AndCmp, DAG.getBasicBlock(B.TargetBB));
1452
1453 // Set NextBlock to be the MBB immediately after the current one, if any.
1454 // This is used to avoid emitting unnecessary branches to the next block.
1455 MachineBasicBlock *NextBlock = 0;
1456 MachineFunction::iterator BBI = CurMBB;
1457 if (++BBI != CurMBB->getParent()->end())
1458 NextBlock = BBI;
1459
1460 if (NextMBB == NextBlock)
1461 DAG.setRoot(BrAnd);
1462 else
1463 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, BrAnd,
1464 DAG.getBasicBlock(NextMBB)));
1465
1466 return;
1467}
1468
1469void SelectionDAGLowering::visitInvoke(InvokeInst &I) {
1470 // Retrieve successors.
1471 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1472 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1473
1474 if (isa<InlineAsm>(I.getCalledValue()))
1475 visitInlineAsm(&I);
1476 else
1477 LowerCallTo(&I, getValue(I.getOperand(0)), false, LandingPad);
1478
1479 // If the value of the invoke is used outside of its defining block, make it
1480 // available as a virtual register.
1481 if (!I.use_empty()) {
1482 DenseMap<const Value*, unsigned>::iterator VMI = FuncInfo.ValueMap.find(&I);
1483 if (VMI != FuncInfo.ValueMap.end())
1484 CopyValueToVirtualRegister(&I, VMI->second);
1485 }
1486
1487 // Update successor info
1488 CurMBB->addSuccessor(Return);
1489 CurMBB->addSuccessor(LandingPad);
1490
1491 // Drop into normal successor.
1492 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1493 DAG.getBasicBlock(Return)));
1494}
1495
1496void SelectionDAGLowering::visitUnwind(UnwindInst &I) {
1497}
1498
1499/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1500/// small case ranges).
1501bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR,
1502 CaseRecVector& WorkList,
1503 Value* SV,
1504 MachineBasicBlock* Default) {
1505 Case& BackCase = *(CR.Range.second-1);
1506
1507 // Size is the number of Cases represented by this range.
1508 unsigned Size = CR.Range.second - CR.Range.first;
1509 if (Size > 3)
1510 return false;
1511
1512 // Get the MachineFunction which holds the current MBB. This is used when
1513 // inserting any additional MBBs necessary to represent the switch.
1514 MachineFunction *CurMF = CurMBB->getParent();
1515
1516 // Figure out which block is immediately after the current one.
1517 MachineBasicBlock *NextBlock = 0;
1518 MachineFunction::iterator BBI = CR.CaseBB;
1519
1520 if (++BBI != CurMBB->getParent()->end())
1521 NextBlock = BBI;
1522
1523 // TODO: If any two of the cases has the same destination, and if one value
1524 // is the same as the other, but has one bit unset that the other has set,
1525 // use bit manipulation to do two compares at once. For example:
1526 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
1527
1528 // Rearrange the case blocks so that the last one falls through if possible.
1529 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1530 // The last case block won't fall through into 'NextBlock' if we emit the
1531 // branches in this order. See if rearranging a case value would help.
1532 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1533 if (I->BB == NextBlock) {
1534 std::swap(*I, BackCase);
1535 break;
1536 }
1537 }
1538 }
1539
1540 // Create a CaseBlock record representing a conditional branch to
1541 // the Case's target mbb if the value being switched on SV is equal
1542 // to C.
1543 MachineBasicBlock *CurBlock = CR.CaseBB;
1544 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1545 MachineBasicBlock *FallThrough;
1546 if (I != E-1) {
1547 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1548 CurMF->insert(BBI, FallThrough);
1549 } else {
1550 // If the last case doesn't match, go to the default block.
1551 FallThrough = Default;
1552 }
1553
1554 Value *RHS, *LHS, *MHS;
1555 ISD::CondCode CC;
1556 if (I->High == I->Low) {
1557 // This is just small small case range :) containing exactly 1 case
1558 CC = ISD::SETEQ;
1559 LHS = SV; RHS = I->High; MHS = NULL;
1560 } else {
1561 CC = ISD::SETLE;
1562 LHS = I->Low; MHS = SV; RHS = I->High;
1563 }
1564 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
1565
1566 // If emitting the first comparison, just call visitSwitchCase to emit the
1567 // code into the current block. Otherwise, push the CaseBlock onto the
1568 // vector to be later processed by SDISel, and insert the node's MBB
1569 // before the next MBB.
1570 if (CurBlock == CurMBB)
1571 visitSwitchCase(CB);
1572 else
1573 SwitchCases.push_back(CB);
1574
1575 CurBlock = FallThrough;
1576 }
1577
1578 return true;
1579}
1580
1581static inline bool areJTsAllowed(const TargetLowering &TLI) {
1582 return !DisableJumpTables &&
1583 (TLI.isOperationLegal(ISD::BR_JT, MVT::Other) ||
1584 TLI.isOperationLegal(ISD::BRIND, MVT::Other));
1585}
1586
1587/// handleJTSwitchCase - Emit jumptable for current switch case range
1588bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR,
1589 CaseRecVector& WorkList,
1590 Value* SV,
1591 MachineBasicBlock* Default) {
1592 Case& FrontCase = *CR.Range.first;
1593 Case& BackCase = *(CR.Range.second-1);
1594
1595 int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue();
1596 int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue();
1597
1598 uint64_t TSize = 0;
1599 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1600 I!=E; ++I)
1601 TSize += I->size();
1602
1603 if (!areJTsAllowed(TLI) || TSize <= 3)
1604 return false;
1605
1606 double Density = (double)TSize / (double)((Last - First) + 1ULL);
1607 if (Density < 0.4)
1608 return false;
1609
1610 DOUT << "Lowering jump table\n"
1611 << "First entry: " << First << ". Last entry: " << Last << "\n"
1612 << "Size: " << TSize << ". Density: " << Density << "\n\n";
1613
1614 // Get the MachineFunction which holds the current MBB. This is used when
1615 // inserting any additional MBBs necessary to represent the switch.
1616 MachineFunction *CurMF = CurMBB->getParent();
1617
1618 // Figure out which block is immediately after the current one.
1619 MachineBasicBlock *NextBlock = 0;
1620 MachineFunction::iterator BBI = CR.CaseBB;
1621
1622 if (++BBI != CurMBB->getParent()->end())
1623 NextBlock = BBI;
1624
1625 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1626
1627 // Create a new basic block to hold the code for loading the address
1628 // of the jump table, and jumping to it. Update successor information;
1629 // we will either branch to the default case for the switch, or the jump
1630 // table.
1631 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1632 CurMF->insert(BBI, JumpTableBB);
1633 CR.CaseBB->addSuccessor(Default);
1634 CR.CaseBB->addSuccessor(JumpTableBB);
1635
1636 // Build a vector of destination BBs, corresponding to each target
1637 // of the jump table. If the value of the jump table slot corresponds to
1638 // a case statement, push the case's BB onto the vector, otherwise, push
1639 // the default BB.
1640 std::vector<MachineBasicBlock*> DestBBs;
1641 int64_t TEI = First;
1642 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
1643 int64_t Low = cast<ConstantInt>(I->Low)->getSExtValue();
1644 int64_t High = cast<ConstantInt>(I->High)->getSExtValue();
1645
1646 if ((Low <= TEI) && (TEI <= High)) {
1647 DestBBs.push_back(I->BB);
1648 if (TEI==High)
1649 ++I;
1650 } else {
1651 DestBBs.push_back(Default);
1652 }
1653 }
1654
1655 // Update successor info. Add one edge to each unique successor.
1656 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1657 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
1658 E = DestBBs.end(); I != E; ++I) {
1659 if (!SuccsHandled[(*I)->getNumber()]) {
1660 SuccsHandled[(*I)->getNumber()] = true;
1661 JumpTableBB->addSuccessor(*I);
1662 }
1663 }
1664
1665 // Create a jump table index for this jump table, or return an existing
1666 // one.
1667 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
1668
1669 // Set the jump table information so that we can codegen it as a second
1670 // MachineBasicBlock
1671 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1672 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1673 if (CR.CaseBB == CurMBB)
1674 visitJumpTableHeader(JT, JTH);
1675
1676 JTCases.push_back(JumpTableBlock(JTH, JT));
1677
1678 return true;
1679}
1680
1681/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1682/// 2 subtrees.
1683bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR,
1684 CaseRecVector& WorkList,
1685 Value* SV,
1686 MachineBasicBlock* Default) {
1687 // Get the MachineFunction which holds the current MBB. This is used when
1688 // inserting any additional MBBs necessary to represent the switch.
1689 MachineFunction *CurMF = CurMBB->getParent();
1690
1691 // Figure out which block is immediately after the current one.
1692 MachineBasicBlock *NextBlock = 0;
1693 MachineFunction::iterator BBI = CR.CaseBB;
1694
1695 if (++BBI != CurMBB->getParent()->end())
1696 NextBlock = BBI;
1697
1698 Case& FrontCase = *CR.Range.first;
1699 Case& BackCase = *(CR.Range.second-1);
1700 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1701
1702 // Size is the number of Cases represented by this range.
1703 unsigned Size = CR.Range.second - CR.Range.first;
1704
1705 int64_t First = cast<ConstantInt>(FrontCase.Low)->getSExtValue();
1706 int64_t Last = cast<ConstantInt>(BackCase.High)->getSExtValue();
1707 double FMetric = 0;
1708 CaseItr Pivot = CR.Range.first + Size/2;
1709
1710 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1711 // (heuristically) allow us to emit JumpTable's later.
1712 uint64_t TSize = 0;
1713 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1714 I!=E; ++I)
1715 TSize += I->size();
1716
1717 uint64_t LSize = FrontCase.size();
1718 uint64_t RSize = TSize-LSize;
1719 DOUT << "Selecting best pivot: \n"
1720 << "First: " << First << ", Last: " << Last <<"\n"
1721 << "LSize: " << LSize << ", RSize: " << RSize << "\n";
1722 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1723 J!=E; ++I, ++J) {
1724 int64_t LEnd = cast<ConstantInt>(I->High)->getSExtValue();
1725 int64_t RBegin = cast<ConstantInt>(J->Low)->getSExtValue();
1726 assert((RBegin-LEnd>=1) && "Invalid case distance");
1727 double LDensity = (double)LSize / (double)((LEnd - First) + 1ULL);
1728 double RDensity = (double)RSize / (double)((Last - RBegin) + 1ULL);
1729 double Metric = Log2_64(RBegin-LEnd)*(LDensity+RDensity);
1730 // Should always split in some non-trivial place
1731 DOUT <<"=>Step\n"
1732 << "LEnd: " << LEnd << ", RBegin: " << RBegin << "\n"
1733 << "LDensity: " << LDensity << ", RDensity: " << RDensity << "\n"
1734 << "Metric: " << Metric << "\n";
1735 if (FMetric < Metric) {
1736 Pivot = J;
1737 FMetric = Metric;
1738 DOUT << "Current metric set to: " << FMetric << "\n";
1739 }
1740
1741 LSize += J->size();
1742 RSize -= J->size();
1743 }
1744 if (areJTsAllowed(TLI)) {
1745 // If our case is dense we *really* should handle it earlier!
1746 assert((FMetric > 0) && "Should handle dense range earlier!");
1747 } else {
1748 Pivot = CR.Range.first + Size/2;
1749 }
1750
1751 CaseRange LHSR(CR.Range.first, Pivot);
1752 CaseRange RHSR(Pivot, CR.Range.second);
1753 Constant *C = Pivot->Low;
1754 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
1755
1756 // We know that we branch to the LHS if the Value being switched on is
1757 // less than the Pivot value, C. We use this to optimize our binary
1758 // tree a bit, by recognizing that if SV is greater than or equal to the
1759 // LHS's Case Value, and that Case Value is exactly one less than the
1760 // Pivot's Value, then we can branch directly to the LHS's Target,
1761 // rather than creating a leaf node for it.
1762 if ((LHSR.second - LHSR.first) == 1 &&
1763 LHSR.first->High == CR.GE &&
1764 cast<ConstantInt>(C)->getSExtValue() ==
1765 (cast<ConstantInt>(CR.GE)->getSExtValue() + 1LL)) {
1766 TrueBB = LHSR.first->BB;
1767 } else {
1768 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1769 CurMF->insert(BBI, TrueBB);
1770 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
1771 }
1772
1773 // Similar to the optimization above, if the Value being switched on is
1774 // known to be less than the Constant CR.LT, and the current Case Value
1775 // is CR.LT - 1, then we can branch directly to the target block for
1776 // the current Case Value, rather than emitting a RHS leaf node for it.
1777 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
1778 cast<ConstantInt>(RHSR.first->Low)->getSExtValue() ==
1779 (cast<ConstantInt>(CR.LT)->getSExtValue() - 1LL)) {
1780 FalseBB = RHSR.first->BB;
1781 } else {
1782 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1783 CurMF->insert(BBI, FalseBB);
1784 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
1785 }
1786
1787 // Create a CaseBlock record representing a conditional branch to
1788 // the LHS node if the value being switched on SV is less than C.
1789 // Otherwise, branch to LHS.
1790 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1791
1792 if (CR.CaseBB == CurMBB)
1793 visitSwitchCase(CB);
1794 else
1795 SwitchCases.push_back(CB);
1796
1797 return true;
1798}
1799
1800/// handleBitTestsSwitchCase - if current case range has few destination and
1801/// range span less, than machine word bitwidth, encode case range into series
1802/// of masks and emit bit tests with these masks.
1803bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1804 CaseRecVector& WorkList,
1805 Value* SV,
1806 MachineBasicBlock* Default){
1807 unsigned IntPtrBits = TLI.getPointerTy().getSizeInBits();
1808
1809 Case& FrontCase = *CR.Range.first;
1810 Case& BackCase = *(CR.Range.second-1);
1811
1812 // Get the MachineFunction which holds the current MBB. This is used when
1813 // inserting any additional MBBs necessary to represent the switch.
1814 MachineFunction *CurMF = CurMBB->getParent();
1815
1816 unsigned numCmps = 0;
1817 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1818 I!=E; ++I) {
1819 // Single case counts one, case range - two.
1820 if (I->Low == I->High)
1821 numCmps +=1;
1822 else
1823 numCmps +=2;
1824 }
1825
1826 // Count unique destinations
1827 SmallSet<MachineBasicBlock*, 4> Dests;
1828 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1829 Dests.insert(I->BB);
1830 if (Dests.size() > 3)
1831 // Don't bother the code below, if there are too much unique destinations
1832 return false;
1833 }
1834 DOUT << "Total number of unique destinations: " << Dests.size() << "\n"
1835 << "Total number of comparisons: " << numCmps << "\n";
1836
1837 // Compute span of values.
1838 Constant* minValue = FrontCase.Low;
1839 Constant* maxValue = BackCase.High;
1840 uint64_t range = cast<ConstantInt>(maxValue)->getSExtValue() -
1841 cast<ConstantInt>(minValue)->getSExtValue();
1842 DOUT << "Compare range: " << range << "\n"
1843 << "Low bound: " << cast<ConstantInt>(minValue)->getSExtValue() << "\n"
1844 << "High bound: " << cast<ConstantInt>(maxValue)->getSExtValue() << "\n";
1845
1846 if (range>=IntPtrBits ||
1847 (!(Dests.size() == 1 && numCmps >= 3) &&
1848 !(Dests.size() == 2 && numCmps >= 5) &&
1849 !(Dests.size() >= 3 && numCmps >= 6)))
1850 return false;
1851
1852 DOUT << "Emitting bit tests\n";
1853 int64_t lowBound = 0;
1854
1855 // Optimize the case where all the case values fit in a
1856 // word without having to subtract minValue. In this case,
1857 // we can optimize away the subtraction.
1858 if (cast<ConstantInt>(minValue)->getSExtValue() >= 0 &&
1859 cast<ConstantInt>(maxValue)->getSExtValue() < IntPtrBits) {
1860 range = cast<ConstantInt>(maxValue)->getSExtValue();
1861 } else {
1862 lowBound = cast<ConstantInt>(minValue)->getSExtValue();
1863 }
1864
1865 CaseBitsVector CasesBits;
1866 unsigned i, count = 0;
1867
1868 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1869 MachineBasicBlock* Dest = I->BB;
1870 for (i = 0; i < count; ++i)
1871 if (Dest == CasesBits[i].BB)
1872 break;
1873
1874 if (i == count) {
1875 assert((count < 3) && "Too much destinations to test!");
1876 CasesBits.push_back(CaseBits(0, Dest, 0));
1877 count++;
1878 }
1879
1880 uint64_t lo = cast<ConstantInt>(I->Low)->getSExtValue() - lowBound;
1881 uint64_t hi = cast<ConstantInt>(I->High)->getSExtValue() - lowBound;
1882
1883 for (uint64_t j = lo; j <= hi; j++) {
1884 CasesBits[i].Mask |= 1ULL << j;
1885 CasesBits[i].Bits++;
1886 }
1887
1888 }
1889 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
1890
1891 BitTestInfo BTC;
1892
1893 // Figure out which block is immediately after the current one.
1894 MachineFunction::iterator BBI = CR.CaseBB;
1895 ++BBI;
1896
1897 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1898
1899 DOUT << "Cases:\n";
1900 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
1901 DOUT << "Mask: " << CasesBits[i].Mask << ", Bits: " << CasesBits[i].Bits
1902 << ", BB: " << CasesBits[i].BB << "\n";
1903
1904 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1905 CurMF->insert(BBI, CaseBB);
1906 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1907 CaseBB,
1908 CasesBits[i].BB));
1909 }
1910
1911 BitTestBlock BTB(lowBound, range, SV,
1912 -1U, (CR.CaseBB == CurMBB),
1913 CR.CaseBB, Default, BTC);
1914
1915 if (CR.CaseBB == CurMBB)
1916 visitBitTestHeader(BTB);
1917
1918 BitTestCases.push_back(BTB);
1919
1920 return true;
1921}
1922
1923
1924/// Clusterify - Transform simple list of Cases into list of CaseRange's
1925unsigned SelectionDAGLowering::Clusterify(CaseVector& Cases,
1926 const SwitchInst& SI) {
1927 unsigned numCmps = 0;
1928
1929 // Start with "simple" cases
1930 for (unsigned i = 1; i < SI.getNumSuccessors(); ++i) {
1931 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1932 Cases.push_back(Case(SI.getSuccessorValue(i),
1933 SI.getSuccessorValue(i),
1934 SMBB));
1935 }
1936 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1937
1938 // Merge case into clusters
1939 if (Cases.size()>=2)
1940 // Must recompute end() each iteration because it may be
1941 // invalidated by erase if we hold on to it
1942 for (CaseItr I=Cases.begin(), J=++(Cases.begin()); J!=Cases.end(); ) {
1943 int64_t nextValue = cast<ConstantInt>(J->Low)->getSExtValue();
1944 int64_t currentValue = cast<ConstantInt>(I->High)->getSExtValue();
1945 MachineBasicBlock* nextBB = J->BB;
1946 MachineBasicBlock* currentBB = I->BB;
1947
1948 // If the two neighboring cases go to the same destination, merge them
1949 // into a single case.
1950 if ((nextValue-currentValue==1) && (currentBB == nextBB)) {
1951 I->High = J->High;
1952 J = Cases.erase(J);
1953 } else {
1954 I = J++;
1955 }
1956 }
1957
1958 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1959 if (I->Low != I->High)
1960 // A range counts double, since it requires two compares.
1961 ++numCmps;
1962 }
1963
1964 return numCmps;
1965}
1966
1967void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {
1968 // Figure out which block is immediately after the current one.
1969 MachineBasicBlock *NextBlock = 0;
1970 MachineFunction::iterator BBI = CurMBB;
1971
1972 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1973
1974 // If there is only the default destination, branch to it if it is not the
1975 // next basic block. Otherwise, just fall through.
1976 if (SI.getNumOperands() == 2) {
1977 // Update machine-CFG edges.
1978
1979 // If this is not a fall-through branch, emit the branch.
1980 CurMBB->addSuccessor(Default);
1981 if (Default != NextBlock)
1982 DAG.setRoot(DAG.getNode(ISD::BR, MVT::Other, getControlRoot(),
1983 DAG.getBasicBlock(Default)));
1984
1985 return;
1986 }
1987
1988 // If there are any non-default case statements, create a vector of Cases
1989 // representing each one, and sort the vector so that we can efficiently
1990 // create a binary search tree from them.
1991 CaseVector Cases;
1992 unsigned numCmps = Clusterify(Cases, SI);
1993 DOUT << "Clusterify finished. Total clusters: " << Cases.size()
1994 << ". Total compares: " << numCmps << "\n";
1995
1996 // Get the Value to be switched on and default basic blocks, which will be
1997 // inserted into CaseBlock records, representing basic blocks in the binary
1998 // search tree.
1999 Value *SV = SI.getOperand(0);
2000
2001 // Push the initial CaseRec onto the worklist
2002 CaseRecVector WorkList;
2003 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2004
2005 while (!WorkList.empty()) {
2006 // Grab a record representing a case range to process off the worklist
2007 CaseRec CR = WorkList.back();
2008 WorkList.pop_back();
2009
2010 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2011 continue;
2012
2013 // If the range has few cases (two or less) emit a series of specific
2014 // tests.
2015 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2016 continue;
2017
2018 // If the switch has more than 5 blocks, and at least 40% dense, and the
2019 // target supports indirect branches, then emit a jump table rather than
2020 // lowering the switch to a binary tree of conditional branches.
2021 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2022 continue;
2023
2024 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2025 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2026 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2027 }
2028}
2029
2030
2031void SelectionDAGLowering::visitSub(User &I) {
2032 // -0.0 - X --> fneg
2033 const Type *Ty = I.getType();
2034 if (isa<VectorType>(Ty)) {
2035 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2036 const VectorType *DestTy = cast<VectorType>(I.getType());
2037 const Type *ElTy = DestTy->getElementType();
2038 if (ElTy->isFloatingPoint()) {
2039 unsigned VL = DestTy->getNumElements();
2040 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
2041 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
2042 if (CV == CNZ) {
2043 SDValue Op2 = getValue(I.getOperand(1));
2044 setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
2045 return;
2046 }
2047 }
2048 }
2049 }
2050 if (Ty->isFloatingPoint()) {
2051 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
2052 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
2053 SDValue Op2 = getValue(I.getOperand(1));
2054 setValue(&I, DAG.getNode(ISD::FNEG, Op2.getValueType(), Op2));
2055 return;
2056 }
2057 }
2058
2059 visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB);
2060}
2061
2062void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
2063 SDValue Op1 = getValue(I.getOperand(0));
2064 SDValue Op2 = getValue(I.getOperand(1));
2065
2066 setValue(&I, DAG.getNode(OpCode, Op1.getValueType(), Op1, Op2));
2067}
2068
2069void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2070 SDValue Op1 = getValue(I.getOperand(0));
2071 SDValue Op2 = getValue(I.getOperand(1));
2072 if (!isa<VectorType>(I.getType())) {
2073 if (TLI.getShiftAmountTy().bitsLT(Op2.getValueType()))
2074 Op2 = DAG.getNode(ISD::TRUNCATE, TLI.getShiftAmountTy(), Op2);
2075 else if (TLI.getShiftAmountTy().bitsGT(Op2.getValueType()))
2076 Op2 = DAG.getNode(ISD::ANY_EXTEND, TLI.getShiftAmountTy(), Op2);
2077 }
2078
2079 setValue(&I, DAG.getNode(Opcode, Op1.getValueType(), Op1, Op2));
2080}
2081
2082void SelectionDAGLowering::visitICmp(User &I) {
2083 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2084 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2085 predicate = IC->getPredicate();
2086 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2087 predicate = ICmpInst::Predicate(IC->getPredicate());
2088 SDValue Op1 = getValue(I.getOperand(0));
2089 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002090 ISD::CondCode Opcode = getICmpCondCode(predicate);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002091 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
2092}
2093
2094void SelectionDAGLowering::visitFCmp(User &I) {
2095 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2096 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2097 predicate = FC->getPredicate();
2098 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2099 predicate = FCmpInst::Predicate(FC->getPredicate());
2100 SDValue Op1 = getValue(I.getOperand(0));
2101 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002102 ISD::CondCode Condition = getFCmpCondCode(predicate);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002103 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition));
2104}
2105
2106void SelectionDAGLowering::visitVICmp(User &I) {
2107 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2108 if (VICmpInst *IC = dyn_cast<VICmpInst>(&I))
2109 predicate = IC->getPredicate();
2110 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2111 predicate = ICmpInst::Predicate(IC->getPredicate());
2112 SDValue Op1 = getValue(I.getOperand(0));
2113 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002114 ISD::CondCode Opcode = getICmpCondCode(predicate);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002115 setValue(&I, DAG.getVSetCC(Op1.getValueType(), Op1, Op2, Opcode));
2116}
2117
2118void SelectionDAGLowering::visitVFCmp(User &I) {
2119 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2120 if (VFCmpInst *FC = dyn_cast<VFCmpInst>(&I))
2121 predicate = FC->getPredicate();
2122 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2123 predicate = FCmpInst::Predicate(FC->getPredicate());
2124 SDValue Op1 = getValue(I.getOperand(0));
2125 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002126 ISD::CondCode Condition = getFCmpCondCode(predicate);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127 MVT DestVT = TLI.getValueType(I.getType());
2128
2129 setValue(&I, DAG.getVSetCC(DestVT, Op1, Op2, Condition));
2130}
2131
2132void SelectionDAGLowering::visitSelect(User &I) {
2133 SDValue Cond = getValue(I.getOperand(0));
2134 SDValue TrueVal = getValue(I.getOperand(1));
2135 SDValue FalseVal = getValue(I.getOperand(2));
2136 setValue(&I, DAG.getNode(ISD::SELECT, TrueVal.getValueType(), Cond,
2137 TrueVal, FalseVal));
2138}
2139
2140
2141void SelectionDAGLowering::visitTrunc(User &I) {
2142 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2143 SDValue N = getValue(I.getOperand(0));
2144 MVT DestVT = TLI.getValueType(I.getType());
2145 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
2146}
2147
2148void SelectionDAGLowering::visitZExt(User &I) {
2149 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2150 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2151 SDValue N = getValue(I.getOperand(0));
2152 MVT DestVT = TLI.getValueType(I.getType());
2153 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
2154}
2155
2156void SelectionDAGLowering::visitSExt(User &I) {
2157 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2158 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2159 SDValue N = getValue(I.getOperand(0));
2160 MVT DestVT = TLI.getValueType(I.getType());
2161 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, DestVT, N));
2162}
2163
2164void SelectionDAGLowering::visitFPTrunc(User &I) {
2165 // FPTrunc is never a no-op cast, no need to check
2166 SDValue N = getValue(I.getOperand(0));
2167 MVT DestVT = TLI.getValueType(I.getType());
2168 setValue(&I, DAG.getNode(ISD::FP_ROUND, DestVT, N, DAG.getIntPtrConstant(0)));
2169}
2170
2171void SelectionDAGLowering::visitFPExt(User &I){
2172 // FPTrunc is never a no-op cast, no need to check
2173 SDValue N = getValue(I.getOperand(0));
2174 MVT DestVT = TLI.getValueType(I.getType());
2175 setValue(&I, DAG.getNode(ISD::FP_EXTEND, DestVT, N));
2176}
2177
2178void SelectionDAGLowering::visitFPToUI(User &I) {
2179 // FPToUI is never a no-op cast, no need to check
2180 SDValue N = getValue(I.getOperand(0));
2181 MVT DestVT = TLI.getValueType(I.getType());
2182 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, DestVT, N));
2183}
2184
2185void SelectionDAGLowering::visitFPToSI(User &I) {
2186 // FPToSI is never a no-op cast, no need to check
2187 SDValue N = getValue(I.getOperand(0));
2188 MVT DestVT = TLI.getValueType(I.getType());
2189 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, DestVT, N));
2190}
2191
2192void SelectionDAGLowering::visitUIToFP(User &I) {
2193 // UIToFP is never a no-op cast, no need to check
2194 SDValue N = getValue(I.getOperand(0));
2195 MVT DestVT = TLI.getValueType(I.getType());
2196 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, DestVT, N));
2197}
2198
2199void SelectionDAGLowering::visitSIToFP(User &I){
2200 // UIToFP is never a no-op cast, no need to check
2201 SDValue N = getValue(I.getOperand(0));
2202 MVT DestVT = TLI.getValueType(I.getType());
2203 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, DestVT, N));
2204}
2205
2206void SelectionDAGLowering::visitPtrToInt(User &I) {
2207 // What to do depends on the size of the integer and the size of the pointer.
2208 // We can either truncate, zero extend, or no-op, accordingly.
2209 SDValue N = getValue(I.getOperand(0));
2210 MVT SrcVT = N.getValueType();
2211 MVT DestVT = TLI.getValueType(I.getType());
2212 SDValue Result;
2213 if (DestVT.bitsLT(SrcVT))
2214 Result = DAG.getNode(ISD::TRUNCATE, DestVT, N);
2215 else
2216 // Note: ZERO_EXTEND can handle cases where the sizes are equal too
2217 Result = DAG.getNode(ISD::ZERO_EXTEND, DestVT, N);
2218 setValue(&I, Result);
2219}
2220
2221void SelectionDAGLowering::visitIntToPtr(User &I) {
2222 // What to do depends on the size of the integer and the size of the pointer.
2223 // We can either truncate, zero extend, or no-op, accordingly.
2224 SDValue N = getValue(I.getOperand(0));
2225 MVT SrcVT = N.getValueType();
2226 MVT DestVT = TLI.getValueType(I.getType());
2227 if (DestVT.bitsLT(SrcVT))
2228 setValue(&I, DAG.getNode(ISD::TRUNCATE, DestVT, N));
2229 else
2230 // Note: ZERO_EXTEND can handle cases where the sizes are equal too
2231 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, DestVT, N));
2232}
2233
2234void SelectionDAGLowering::visitBitCast(User &I) {
2235 SDValue N = getValue(I.getOperand(0));
2236 MVT DestVT = TLI.getValueType(I.getType());
2237
2238 // BitCast assures us that source and destination are the same size so this
2239 // is either a BIT_CONVERT or a no-op.
2240 if (DestVT != N.getValueType())
2241 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, DestVT, N)); // convert types
2242 else
2243 setValue(&I, N); // noop cast.
2244}
2245
2246void SelectionDAGLowering::visitInsertElement(User &I) {
2247 SDValue InVec = getValue(I.getOperand(0));
2248 SDValue InVal = getValue(I.getOperand(1));
2249 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
2250 getValue(I.getOperand(2)));
2251
2252 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT,
2253 TLI.getValueType(I.getType()),
2254 InVec, InVal, InIdx));
2255}
2256
2257void SelectionDAGLowering::visitExtractElement(User &I) {
2258 SDValue InVec = getValue(I.getOperand(0));
2259 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, TLI.getPointerTy(),
2260 getValue(I.getOperand(1)));
2261 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT,
2262 TLI.getValueType(I.getType()), InVec, InIdx));
2263}
2264
2265void SelectionDAGLowering::visitShuffleVector(User &I) {
2266 SDValue V1 = getValue(I.getOperand(0));
2267 SDValue V2 = getValue(I.getOperand(1));
2268 SDValue Mask = getValue(I.getOperand(2));
2269
2270 setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE,
2271 TLI.getValueType(I.getType()),
2272 V1, V2, Mask));
2273}
2274
2275void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) {
2276 const Value *Op0 = I.getOperand(0);
2277 const Value *Op1 = I.getOperand(1);
2278 const Type *AggTy = I.getType();
2279 const Type *ValTy = Op1->getType();
2280 bool IntoUndef = isa<UndefValue>(Op0);
2281 bool FromUndef = isa<UndefValue>(Op1);
2282
2283 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2284 I.idx_begin(), I.idx_end());
2285
2286 SmallVector<MVT, 4> AggValueVTs;
2287 ComputeValueVTs(TLI, AggTy, AggValueVTs);
2288 SmallVector<MVT, 4> ValValueVTs;
2289 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2290
2291 unsigned NumAggValues = AggValueVTs.size();
2292 unsigned NumValValues = ValValueVTs.size();
2293 SmallVector<SDValue, 4> Values(NumAggValues);
2294
2295 SDValue Agg = getValue(Op0);
2296 SDValue Val = getValue(Op1);
2297 unsigned i = 0;
2298 // Copy the beginning value(s) from the original aggregate.
2299 for (; i != LinearIndex; ++i)
2300 Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2301 SDValue(Agg.getNode(), Agg.getResNo() + i);
2302 // Copy values from the inserted value(s).
2303 for (; i != LinearIndex + NumValValues; ++i)
2304 Values[i] = FromUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2305 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2306 // Copy remaining value(s) from the original aggregate.
2307 for (; i != NumAggValues; ++i)
2308 Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, AggValueVTs[i]) :
2309 SDValue(Agg.getNode(), Agg.getResNo() + i);
2310
2311 setValue(&I, DAG.getMergeValues(DAG.getVTList(&AggValueVTs[0], NumAggValues),
2312 &Values[0], NumAggValues));
2313}
2314
2315void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) {
2316 const Value *Op0 = I.getOperand(0);
2317 const Type *AggTy = Op0->getType();
2318 const Type *ValTy = I.getType();
2319 bool OutOfUndef = isa<UndefValue>(Op0);
2320
2321 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2322 I.idx_begin(), I.idx_end());
2323
2324 SmallVector<MVT, 4> ValValueVTs;
2325 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2326
2327 unsigned NumValValues = ValValueVTs.size();
2328 SmallVector<SDValue, 4> Values(NumValValues);
2329
2330 SDValue Agg = getValue(Op0);
2331 // Copy out the selected value(s).
2332 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2333 Values[i - LinearIndex] =
2334 OutOfUndef ? DAG.getNode(ISD::UNDEF, Agg.getNode()->getValueType(Agg.getResNo() + i)) :
2335 SDValue(Agg.getNode(), Agg.getResNo() + i);
2336
2337 setValue(&I, DAG.getMergeValues(DAG.getVTList(&ValValueVTs[0], NumValValues),
2338 &Values[0], NumValValues));
2339}
2340
2341
2342void SelectionDAGLowering::visitGetElementPtr(User &I) {
2343 SDValue N = getValue(I.getOperand(0));
2344 const Type *Ty = I.getOperand(0)->getType();
2345
2346 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2347 OI != E; ++OI) {
2348 Value *Idx = *OI;
2349 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2350 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2351 if (Field) {
2352 // N = N + Offset
2353 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
2354 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
2355 DAG.getIntPtrConstant(Offset));
2356 }
2357 Ty = StTy->getElementType(Field);
2358 } else {
2359 Ty = cast<SequentialType>(Ty)->getElementType();
2360
2361 // If this is a constant subscript, handle it quickly.
2362 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2363 if (CI->getZExtValue() == 0) continue;
2364 uint64_t Offs =
2365 TD->getABITypeSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
2366 N = DAG.getNode(ISD::ADD, N.getValueType(), N,
2367 DAG.getIntPtrConstant(Offs));
2368 continue;
2369 }
2370
2371 // N = N + Idx * ElementSize;
2372 uint64_t ElementSize = TD->getABITypeSize(Ty);
2373 SDValue IdxN = getValue(Idx);
2374
2375 // If the index is smaller or larger than intptr_t, truncate or extend
2376 // it.
2377 if (IdxN.getValueType().bitsLT(N.getValueType()))
2378 IdxN = DAG.getNode(ISD::SIGN_EXTEND, N.getValueType(), IdxN);
2379 else if (IdxN.getValueType().bitsGT(N.getValueType()))
2380 IdxN = DAG.getNode(ISD::TRUNCATE, N.getValueType(), IdxN);
2381
2382 // If this is a multiply by a power of two, turn it into a shl
2383 // immediately. This is a very common case.
2384 if (ElementSize != 1) {
2385 if (isPowerOf2_64(ElementSize)) {
2386 unsigned Amt = Log2_64(ElementSize);
2387 IdxN = DAG.getNode(ISD::SHL, N.getValueType(), IdxN,
2388 DAG.getConstant(Amt, TLI.getShiftAmountTy()));
2389 } else {
2390 SDValue Scale = DAG.getIntPtrConstant(ElementSize);
2391 IdxN = DAG.getNode(ISD::MUL, N.getValueType(), IdxN, Scale);
2392 }
2393 }
2394
2395 N = DAG.getNode(ISD::ADD, N.getValueType(), N, IdxN);
2396 }
2397 }
2398 setValue(&I, N);
2399}
2400
2401void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2402 // If this is a fixed sized alloca in the entry block of the function,
2403 // allocate it statically on the stack.
2404 if (FuncInfo.StaticAllocaMap.count(&I))
2405 return; // getValue will auto-populate this.
2406
2407 const Type *Ty = I.getAllocatedType();
2408 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
2409 unsigned Align =
2410 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2411 I.getAlignment());
2412
2413 SDValue AllocSize = getValue(I.getArraySize());
2414 MVT IntPtr = TLI.getPointerTy();
2415 if (IntPtr.bitsLT(AllocSize.getValueType()))
2416 AllocSize = DAG.getNode(ISD::TRUNCATE, IntPtr, AllocSize);
2417 else if (IntPtr.bitsGT(AllocSize.getValueType()))
2418 AllocSize = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, AllocSize);
2419
2420 AllocSize = DAG.getNode(ISD::MUL, IntPtr, AllocSize,
2421 DAG.getIntPtrConstant(TySize));
2422
2423 // Handle alignment. If the requested alignment is less than or equal to
2424 // the stack alignment, ignore it. If the size is greater than or equal to
2425 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2426 unsigned StackAlign =
2427 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2428 if (Align <= StackAlign)
2429 Align = 0;
2430
2431 // Round the size of the allocation up to the stack alignment size
2432 // by add SA-1 to the size.
2433 AllocSize = DAG.getNode(ISD::ADD, AllocSize.getValueType(), AllocSize,
2434 DAG.getIntPtrConstant(StackAlign-1));
2435 // Mask out the low bits for alignment purposes.
2436 AllocSize = DAG.getNode(ISD::AND, AllocSize.getValueType(), AllocSize,
2437 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2438
2439 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
2440 const MVT *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(),
2441 MVT::Other);
2442 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, VTs, 2, Ops, 3);
2443 setValue(&I, DSA);
2444 DAG.setRoot(DSA.getValue(1));
2445
2446 // Inform the Frame Information that we have just allocated a variable-sized
2447 // object.
2448 CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
2449}
2450
2451void SelectionDAGLowering::visitLoad(LoadInst &I) {
2452 const Value *SV = I.getOperand(0);
2453 SDValue Ptr = getValue(SV);
2454
2455 const Type *Ty = I.getType();
2456 bool isVolatile = I.isVolatile();
2457 unsigned Alignment = I.getAlignment();
2458
2459 SmallVector<MVT, 4> ValueVTs;
2460 SmallVector<uint64_t, 4> Offsets;
2461 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2462 unsigned NumValues = ValueVTs.size();
2463 if (NumValues == 0)
2464 return;
2465
2466 SDValue Root;
2467 bool ConstantMemory = false;
2468 if (I.isVolatile())
2469 // Serialize volatile loads with other side effects.
2470 Root = getRoot();
2471 else if (AA->pointsToConstantMemory(SV)) {
2472 // Do not serialize (non-volatile) loads of constant memory with anything.
2473 Root = DAG.getEntryNode();
2474 ConstantMemory = true;
2475 } else {
2476 // Do not serialize non-volatile loads against each other.
2477 Root = DAG.getRoot();
2478 }
2479
2480 SmallVector<SDValue, 4> Values(NumValues);
2481 SmallVector<SDValue, 4> Chains(NumValues);
2482 MVT PtrVT = Ptr.getValueType();
2483 for (unsigned i = 0; i != NumValues; ++i) {
2484 SDValue L = DAG.getLoad(ValueVTs[i], Root,
2485 DAG.getNode(ISD::ADD, PtrVT, Ptr,
2486 DAG.getConstant(Offsets[i], PtrVT)),
2487 SV, Offsets[i],
2488 isVolatile, Alignment);
2489 Values[i] = L;
2490 Chains[i] = L.getValue(1);
2491 }
2492
2493 if (!ConstantMemory) {
2494 SDValue Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
2495 &Chains[0], NumValues);
2496 if (isVolatile)
2497 DAG.setRoot(Chain);
2498 else
2499 PendingLoads.push_back(Chain);
2500 }
2501
2502 setValue(&I, DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], NumValues),
2503 &Values[0], NumValues));
2504}
2505
2506
2507void SelectionDAGLowering::visitStore(StoreInst &I) {
2508 Value *SrcV = I.getOperand(0);
2509 Value *PtrV = I.getOperand(1);
2510
2511 SmallVector<MVT, 4> ValueVTs;
2512 SmallVector<uint64_t, 4> Offsets;
2513 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2514 unsigned NumValues = ValueVTs.size();
2515 if (NumValues == 0)
2516 return;
2517
2518 // Get the lowered operands. Note that we do this after
2519 // checking if NumResults is zero, because with zero results
2520 // the operands won't have values in the map.
2521 SDValue Src = getValue(SrcV);
2522 SDValue Ptr = getValue(PtrV);
2523
2524 SDValue Root = getRoot();
2525 SmallVector<SDValue, 4> Chains(NumValues);
2526 MVT PtrVT = Ptr.getValueType();
2527 bool isVolatile = I.isVolatile();
2528 unsigned Alignment = I.getAlignment();
2529 for (unsigned i = 0; i != NumValues; ++i)
2530 Chains[i] = DAG.getStore(Root, SDValue(Src.getNode(), Src.getResNo() + i),
2531 DAG.getNode(ISD::ADD, PtrVT, Ptr,
2532 DAG.getConstant(Offsets[i], PtrVT)),
2533 PtrV, Offsets[i],
2534 isVolatile, Alignment);
2535
2536 DAG.setRoot(DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumValues));
2537}
2538
2539/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2540/// node.
2541void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
2542 unsigned Intrinsic) {
2543 bool HasChain = !I.doesNotAccessMemory();
2544 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2545
2546 // Build the operand list.
2547 SmallVector<SDValue, 8> Ops;
2548 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2549 if (OnlyLoad) {
2550 // We don't need to serialize loads against other loads.
2551 Ops.push_back(DAG.getRoot());
2552 } else {
2553 Ops.push_back(getRoot());
2554 }
2555 }
2556
2557 // Add the intrinsic ID as an integer operand.
2558 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
2559
2560 // Add all operands of the call to the operand list.
2561 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2562 SDValue Op = getValue(I.getOperand(i));
2563 assert(TLI.isTypeLegal(Op.getValueType()) &&
2564 "Intrinsic uses a non-legal type?");
2565 Ops.push_back(Op);
2566 }
2567
2568 std::vector<MVT> VTs;
2569 if (I.getType() != Type::VoidTy) {
2570 MVT VT = TLI.getValueType(I.getType());
2571 if (VT.isVector()) {
2572 const VectorType *DestTy = cast<VectorType>(I.getType());
2573 MVT EltVT = TLI.getValueType(DestTy->getElementType());
2574
2575 VT = MVT::getVectorVT(EltVT, DestTy->getNumElements());
2576 assert(VT != MVT::Other && "Intrinsic uses a non-legal type?");
2577 }
2578
2579 assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?");
2580 VTs.push_back(VT);
2581 }
2582 if (HasChain)
2583 VTs.push_back(MVT::Other);
2584
2585 const MVT *VTList = DAG.getNodeValueTypes(VTs);
2586
2587 // Create the node.
2588 SDValue Result;
2589 if (!HasChain)
2590 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VTList, VTs.size(),
2591 &Ops[0], Ops.size());
2592 else if (I.getType() != Type::VoidTy)
2593 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, VTList, VTs.size(),
2594 &Ops[0], Ops.size());
2595 else
2596 Result = DAG.getNode(ISD::INTRINSIC_VOID, VTList, VTs.size(),
2597 &Ops[0], Ops.size());
2598
2599 if (HasChain) {
2600 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2601 if (OnlyLoad)
2602 PendingLoads.push_back(Chain);
2603 else
2604 DAG.setRoot(Chain);
2605 }
2606 if (I.getType() != Type::VoidTy) {
2607 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
2608 MVT VT = TLI.getValueType(PTy);
2609 Result = DAG.getNode(ISD::BIT_CONVERT, VT, Result);
2610 }
2611 setValue(&I, Result);
2612 }
2613}
2614
2615/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
2616static GlobalVariable *ExtractTypeInfo(Value *V) {
2617 V = V->stripPointerCasts();
2618 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
2619 assert ((GV || isa<ConstantPointerNull>(V)) &&
2620 "TypeInfo must be a global variable or NULL");
2621 return GV;
2622}
2623
2624namespace llvm {
2625
2626/// AddCatchInfo - Extract the personality and type infos from an eh.selector
2627/// call, and add them to the specified machine basic block.
2628void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
2629 MachineBasicBlock *MBB) {
2630 // Inform the MachineModuleInfo of the personality for this landing pad.
2631 ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
2632 assert(CE->getOpcode() == Instruction::BitCast &&
2633 isa<Function>(CE->getOperand(0)) &&
2634 "Personality should be a function");
2635 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
2636
2637 // Gather all the type infos for this landing pad and pass them along to
2638 // MachineModuleInfo.
2639 std::vector<GlobalVariable *> TyInfo;
2640 unsigned N = I.getNumOperands();
2641
2642 for (unsigned i = N - 1; i > 2; --i) {
2643 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
2644 unsigned FilterLength = CI->getZExtValue();
2645 unsigned FirstCatch = i + FilterLength + !FilterLength;
2646 assert (FirstCatch <= N && "Invalid filter length");
2647
2648 if (FirstCatch < N) {
2649 TyInfo.reserve(N - FirstCatch);
2650 for (unsigned j = FirstCatch; j < N; ++j)
2651 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2652 MMI->addCatchTypeInfo(MBB, TyInfo);
2653 TyInfo.clear();
2654 }
2655
2656 if (!FilterLength) {
2657 // Cleanup.
2658 MMI->addCleanup(MBB);
2659 } else {
2660 // Filter.
2661 TyInfo.reserve(FilterLength - 1);
2662 for (unsigned j = i + 1; j < FirstCatch; ++j)
2663 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2664 MMI->addFilterTypeInfo(MBB, TyInfo);
2665 TyInfo.clear();
2666 }
2667
2668 N = i;
2669 }
2670 }
2671
2672 if (N > 3) {
2673 TyInfo.reserve(N - 3);
2674 for (unsigned j = 3; j < N; ++j)
2675 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2676 MMI->addCatchTypeInfo(MBB, TyInfo);
2677 }
2678}
2679
2680}
2681
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002682/// GetSignificand - Get the significand and build it into a floating-point
2683/// number with exponent of 1:
2684///
2685/// Op = (Op & 0x007fffff) | 0x3f800000;
2686///
2687/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002688static SDValue
2689GetSignificand(SelectionDAG &DAG, SDValue Op) {
2690 SDValue t1 = DAG.getNode(ISD::AND, MVT::i32, Op,
2691 DAG.getConstant(0x007fffff, MVT::i32));
2692 SDValue t2 = DAG.getNode(ISD::OR, MVT::i32, t1,
2693 DAG.getConstant(0x3f800000, MVT::i32));
2694 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t2);
2695}
2696
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002697/// GetExponent - Get the exponent:
2698///
2699/// (float)((Op1 >> 23) - 127);
2700///
2701/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002702static SDValue
2703GetExponent(SelectionDAG &DAG, SDValue Op) {
Bill Wendlingfc2508e2008-09-10 06:26:10 +00002704 SDValue t1 = DAG.getNode(ISD::SRL, MVT::i32, Op,
Bill Wendling39150252008-09-09 20:39:27 +00002705 DAG.getConstant(23, MVT::i32));
Bill Wendlingfc2508e2008-09-10 06:26:10 +00002706 SDValue t2 = DAG.getNode(ISD::SUB, MVT::i32, t1,
Bill Wendling39150252008-09-09 20:39:27 +00002707 DAG.getConstant(127, MVT::i32));
Bill Wendlingfc2508e2008-09-10 06:26:10 +00002708 return DAG.getNode(ISD::UINT_TO_FP, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002709}
2710
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002711/// getF32Constant - Get 32-bit floating point constant.
2712static SDValue
2713getF32Constant(SelectionDAG &DAG, unsigned Flt) {
2714 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
2715}
2716
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002717/// Inlined utility function to implement binary input atomic intrinsics for
2718/// visitIntrinsicCall: I is a call instruction
2719/// Op is the associated NodeType for I
2720const char *
2721SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
2722 SDValue Root = getRoot();
2723 SDValue L = DAG.getAtomic(Op, Root,
2724 getValue(I.getOperand(1)),
2725 getValue(I.getOperand(2)),
2726 I.getOperand(1));
2727 setValue(&I, L);
2728 DAG.setRoot(L.getValue(1));
2729 return 0;
2730}
2731
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002732/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2733/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002734void
2735SelectionDAGLowering::visitExp(CallInst &I) {
2736 SDValue result;
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002737
2738 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
2739 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2740 SDValue Op = getValue(I.getOperand(1));
2741
2742 // Put the exponent in the right bit position for later addition to the
2743 // final result:
2744 //
2745 // #define LOG2OFe 1.4426950f
2746 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
2747 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002748 getF32Constant(DAG, 0x3fb8aa3b));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002749 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, t0);
2750
2751 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
2752 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
2753 SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, t0, t1);
2754
2755 // IntegerPartOfX <<= 23;
2756 IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
2757 DAG.getConstant(23, MVT::i32));
2758
2759 if (LimitFloatPrecision <= 6) {
2760 // For floating-point precision of 6:
2761 //
2762 // TwoToFractionalPartOfX =
2763 // 0.997535578f +
2764 // (0.735607626f + 0.252464424f * x) * x;
2765 //
2766 // error 0.0144103317, which is 6 bits
2767 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002768 getF32Constant(DAG, 0x3e814304));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002769 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002770 getF32Constant(DAG, 0x3f3c50c8));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002771 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2772 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002773 getF32Constant(DAG, 0x3f7f5e7e));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002774 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
2775
2776 // Add the exponent into the result in integer domain.
2777 SDValue t6 = DAG.getNode(ISD::ADD, MVT::i32,
2778 TwoToFracPartOfX, IntegerPartOfX);
2779
2780 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t6);
2781 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2782 // For floating-point precision of 12:
2783 //
2784 // TwoToFractionalPartOfX =
2785 // 0.999892986f +
2786 // (0.696457318f +
2787 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2788 //
2789 // 0.000107046256 error, which is 13 to 14 bits
2790 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002791 getF32Constant(DAG, 0x3da235e3));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002792 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002793 getF32Constant(DAG, 0x3e65b8f3));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002794 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2795 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002796 getF32Constant(DAG, 0x3f324b07));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002797 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2798 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002799 getF32Constant(DAG, 0x3f7ff8fd));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002800 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
2801
2802 // Add the exponent into the result in integer domain.
2803 SDValue t8 = DAG.getNode(ISD::ADD, MVT::i32,
2804 TwoToFracPartOfX, IntegerPartOfX);
2805
2806 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t8);
2807 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2808 // For floating-point precision of 18:
2809 //
2810 // TwoToFractionalPartOfX =
2811 // 0.999999982f +
2812 // (0.693148872f +
2813 // (0.240227044f +
2814 // (0.554906021e-1f +
2815 // (0.961591928e-2f +
2816 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
2817 //
2818 // error 2.47208000*10^(-7), which is better than 18 bits
2819 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002820 getF32Constant(DAG, 0x3924b03e));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002821 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002822 getF32Constant(DAG, 0x3ab24b87));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002823 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2824 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002825 getF32Constant(DAG, 0x3c1d8c17));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002826 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2827 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002828 getF32Constant(DAG, 0x3d634a1d));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002829 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
2830 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002831 getF32Constant(DAG, 0x3e75fe14));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002832 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
2833 SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002834 getF32Constant(DAG, 0x3f317234));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002835 SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
2836 SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002837 getF32Constant(DAG, 0x3f800000));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002838 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
2839
2840 // Add the exponent into the result in integer domain.
2841 SDValue t14 = DAG.getNode(ISD::ADD, MVT::i32,
2842 TwoToFracPartOfX, IntegerPartOfX);
2843
2844 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, t14);
2845 }
2846 } else {
2847 // No special expansion.
2848 result = DAG.getNode(ISD::FEXP,
2849 getValue(I.getOperand(1)).getValueType(),
2850 getValue(I.getOperand(1)));
2851 }
2852
Dale Johannesen59e577f2008-09-05 18:38:42 +00002853 setValue(&I, result);
2854}
2855
Bill Wendling39150252008-09-09 20:39:27 +00002856/// visitLog - Lower a log intrinsic. Handles the special sequences for
2857/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002858void
2859SelectionDAGLowering::visitLog(CallInst &I) {
2860 SDValue result;
Bill Wendling39150252008-09-09 20:39:27 +00002861
2862 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
2863 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2864 SDValue Op = getValue(I.getOperand(1));
2865 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
2866
2867 // Scale the exponent by log(2) [0.69314718f].
2868 SDValue Exp = GetExponent(DAG, Op1);
2869 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002870 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00002871
2872 // Get the significand and build it into a floating-point number with
2873 // exponent of 1.
2874 SDValue X = GetSignificand(DAG, Op1);
2875
2876 if (LimitFloatPrecision <= 6) {
2877 // For floating-point precision of 6:
2878 //
2879 // LogofMantissa =
2880 // -1.1609546f +
2881 // (1.4034025f - 0.23903021f * x) * x;
2882 //
2883 // error 0.0034276066, which is better than 8 bits
2884 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002885 getF32Constant(DAG, 0xbe74c456));
Bill Wendling39150252008-09-09 20:39:27 +00002886 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002887 getF32Constant(DAG, 0x3fb3a2b1));
Bill Wendling39150252008-09-09 20:39:27 +00002888 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2889 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002890 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00002891
2892 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
2893 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2894 // For floating-point precision of 12:
2895 //
2896 // LogOfMantissa =
2897 // -1.7417939f +
2898 // (2.8212026f +
2899 // (-1.4699568f +
2900 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
2901 //
2902 // error 0.000061011436, which is 14 bits
2903 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002904 getF32Constant(DAG, 0xbd67b6d6));
Bill Wendling39150252008-09-09 20:39:27 +00002905 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002906 getF32Constant(DAG, 0x3ee4f4b8));
Bill Wendling39150252008-09-09 20:39:27 +00002907 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2908 SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002909 getF32Constant(DAG, 0x3fbc278b));
Bill Wendling39150252008-09-09 20:39:27 +00002910 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2911 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002912 getF32Constant(DAG, 0x40348e95));
Bill Wendling39150252008-09-09 20:39:27 +00002913 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2914 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002915 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00002916
2917 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
2918 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2919 // For floating-point precision of 18:
2920 //
2921 // LogOfMantissa =
2922 // -2.1072184f +
2923 // (4.2372794f +
2924 // (-3.7029485f +
2925 // (2.2781945f +
2926 // (-0.87823314f +
2927 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
2928 //
2929 // error 0.0000023660568, which is better than 18 bits
2930 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002931 getF32Constant(DAG, 0xbc91e5ac));
Bill Wendling39150252008-09-09 20:39:27 +00002932 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002933 getF32Constant(DAG, 0x3e4350aa));
Bill Wendling39150252008-09-09 20:39:27 +00002934 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2935 SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002936 getF32Constant(DAG, 0x3f60d3e3));
Bill Wendling39150252008-09-09 20:39:27 +00002937 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
2938 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002939 getF32Constant(DAG, 0x4011cdf0));
Bill Wendling39150252008-09-09 20:39:27 +00002940 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
2941 SDValue t7 = DAG.getNode(ISD::FSUB, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002942 getF32Constant(DAG, 0x406cfd1c));
Bill Wendling39150252008-09-09 20:39:27 +00002943 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
2944 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002945 getF32Constant(DAG, 0x408797cb));
Bill Wendling39150252008-09-09 20:39:27 +00002946 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
2947 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002948 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00002949
2950 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, LogOfMantissa);
2951 }
2952 } else {
2953 // No special expansion.
2954 result = DAG.getNode(ISD::FLOG,
2955 getValue(I.getOperand(1)).getValueType(),
2956 getValue(I.getOperand(1)));
2957 }
2958
Dale Johannesen59e577f2008-09-05 18:38:42 +00002959 setValue(&I, result);
2960}
2961
Bill Wendling3eb59402008-09-09 00:28:24 +00002962/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
2963/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002964void
2965SelectionDAGLowering::visitLog2(CallInst &I) {
2966 SDValue result;
Bill Wendling3eb59402008-09-09 00:28:24 +00002967
Dale Johannesen853244f2008-09-05 23:49:37 +00002968 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00002969 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2970 SDValue Op = getValue(I.getOperand(1));
2971 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
2972
Bill Wendling39150252008-09-09 20:39:27 +00002973 // Get the exponent.
2974 SDValue LogOfExponent = GetExponent(DAG, Op1);
Bill Wendling3eb59402008-09-09 00:28:24 +00002975
2976 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00002977 // exponent of 1.
2978 SDValue X = GetSignificand(DAG, Op1);
Bill Wendling3eb59402008-09-09 00:28:24 +00002979
2980 // Different possible minimax approximations of significand in
2981 // floating-point for various degrees of accuracy over [1,2].
2982 if (LimitFloatPrecision <= 6) {
2983 // For floating-point precision of 6:
2984 //
2985 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
2986 //
2987 // error 0.0049451742, which is more than 7 bits
Bill Wendling39150252008-09-09 20:39:27 +00002988 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002989 getF32Constant(DAG, 0xbeb08fe0));
Bill Wendling39150252008-09-09 20:39:27 +00002990 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002991 getF32Constant(DAG, 0x40019463));
Bill Wendling39150252008-09-09 20:39:27 +00002992 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
2993 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002994 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00002995
2996 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
2997 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2998 // For floating-point precision of 12:
2999 //
3000 // Log2ofMantissa =
3001 // -2.51285454f +
3002 // (4.07009056f +
3003 // (-2.12067489f +
3004 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
3005 //
3006 // error 0.0000876136000, which is better than 13 bits
Bill Wendling39150252008-09-09 20:39:27 +00003007 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003008 getF32Constant(DAG, 0xbda7262e));
Bill Wendling39150252008-09-09 20:39:27 +00003009 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003010 getF32Constant(DAG, 0x3f25280b));
Bill Wendling39150252008-09-09 20:39:27 +00003011 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3012 SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003013 getF32Constant(DAG, 0x4007b923));
Bill Wendling39150252008-09-09 20:39:27 +00003014 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3015 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003016 getF32Constant(DAG, 0x40823e2f));
Bill Wendling39150252008-09-09 20:39:27 +00003017 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3018 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003019 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003020
3021 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
3022 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3023 // For floating-point precision of 18:
3024 //
3025 // Log2ofMantissa =
3026 // -3.0400495f +
3027 // (6.1129976f +
3028 // (-5.3420409f +
3029 // (3.2865683f +
3030 // (-1.2669343f +
3031 // (0.27515199f -
3032 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3033 //
3034 // error 0.0000018516, which is better than 18 bits
Bill Wendling39150252008-09-09 20:39:27 +00003035 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003036 getF32Constant(DAG, 0xbcd2769e));
Bill Wendling39150252008-09-09 20:39:27 +00003037 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003038 getF32Constant(DAG, 0x3e8ce0b9));
Bill Wendling39150252008-09-09 20:39:27 +00003039 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3040 SDValue t3 = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003041 getF32Constant(DAG, 0x3fa22ae7));
Bill Wendling39150252008-09-09 20:39:27 +00003042 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3043 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003044 getF32Constant(DAG, 0x40525723));
Bill Wendling39150252008-09-09 20:39:27 +00003045 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3046 SDValue t7 = DAG.getNode(ISD::FSUB, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003047 getF32Constant(DAG, 0x40aaf200));
Bill Wendling39150252008-09-09 20:39:27 +00003048 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3049 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003050 getF32Constant(DAG, 0x40c39dad));
Bill Wendling3eb59402008-09-09 00:28:24 +00003051 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
Bill Wendling39150252008-09-09 20:39:27 +00003052 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003053 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003054
3055 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log2ofMantissa);
3056 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003057 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003058 // No special expansion.
Dale Johannesen853244f2008-09-05 23:49:37 +00003059 result = DAG.getNode(ISD::FLOG2,
3060 getValue(I.getOperand(1)).getValueType(),
3061 getValue(I.getOperand(1)));
3062 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003063
Dale Johannesen59e577f2008-09-05 18:38:42 +00003064 setValue(&I, result);
3065}
3066
Bill Wendling3eb59402008-09-09 00:28:24 +00003067/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3068/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003069void
3070SelectionDAGLowering::visitLog10(CallInst &I) {
3071 SDValue result;
Dale Johannesen852680a2008-09-05 21:27:19 +00003072 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003073 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3074 SDValue Op = getValue(I.getOperand(1));
3075 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, Op);
3076
Bill Wendling39150252008-09-09 20:39:27 +00003077 // Scale the exponent by log10(2) [0.30102999f].
3078 SDValue Exp = GetExponent(DAG, Op1);
3079 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003080 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003081
3082 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003083 // exponent of 1.
3084 SDValue X = GetSignificand(DAG, Op1);
Bill Wendling3eb59402008-09-09 00:28:24 +00003085
3086 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003087 // For floating-point precision of 6:
3088 //
3089 // Log10ofMantissa =
3090 // -0.50419619f +
3091 // (0.60948995f - 0.10380950f * x) * x;
3092 //
3093 // error 0.0014886165, which is 6 bits
Bill Wendling39150252008-09-09 20:39:27 +00003094 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003095 getF32Constant(DAG, 0xbdd49a13));
Bill Wendling39150252008-09-09 20:39:27 +00003096 SDValue t1 = DAG.getNode(ISD::FADD, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003097 getF32Constant(DAG, 0x3f1c0789));
Bill Wendling39150252008-09-09 20:39:27 +00003098 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3099 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003100 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003101
3102 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003103 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3104 // For floating-point precision of 12:
3105 //
3106 // Log10ofMantissa =
3107 // -0.64831180f +
3108 // (0.91751397f +
3109 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3110 //
3111 // error 0.00019228036, which is better than 12 bits
Bill Wendling39150252008-09-09 20:39:27 +00003112 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003113 getF32Constant(DAG, 0x3d431f31));
Bill Wendling39150252008-09-09 20:39:27 +00003114 SDValue t1 = DAG.getNode(ISD::FSUB, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003115 getF32Constant(DAG, 0x3ea21fb2));
Bill Wendling39150252008-09-09 20:39:27 +00003116 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3117 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003118 getF32Constant(DAG, 0x3f6ae232));
Bill Wendling39150252008-09-09 20:39:27 +00003119 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3120 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003121 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003122
3123 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
3124 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003125 // For floating-point precision of 18:
3126 //
3127 // Log10ofMantissa =
3128 // -0.84299375f +
3129 // (1.5327582f +
3130 // (-1.0688956f +
3131 // (0.49102474f +
3132 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3133 //
3134 // error 0.0000037995730, which is better than 18 bits
Bill Wendling39150252008-09-09 20:39:27 +00003135 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003136 getF32Constant(DAG, 0x3c5d51ce));
Bill Wendling39150252008-09-09 20:39:27 +00003137 SDValue t1 = DAG.getNode(ISD::FSUB, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003138 getF32Constant(DAG, 0x3e00685a));
Bill Wendling39150252008-09-09 20:39:27 +00003139 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, t1, X);
3140 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003141 getF32Constant(DAG, 0x3efb6798));
Bill Wendling39150252008-09-09 20:39:27 +00003142 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3143 SDValue t5 = DAG.getNode(ISD::FSUB, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003144 getF32Constant(DAG, 0x3f88d192));
Bill Wendling39150252008-09-09 20:39:27 +00003145 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3146 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003147 getF32Constant(DAG, 0x3fc4316c));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003148 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
Bill Wendling39150252008-09-09 20:39:27 +00003149 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003150 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003151
3152 result = DAG.getNode(ISD::FADD, MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003153 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003154 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003155 // No special expansion.
Dale Johannesen852680a2008-09-05 21:27:19 +00003156 result = DAG.getNode(ISD::FLOG10,
3157 getValue(I.getOperand(1)).getValueType(),
3158 getValue(I.getOperand(1)));
3159 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003160
Dale Johannesen59e577f2008-09-05 18:38:42 +00003161 setValue(&I, result);
3162}
3163
Bill Wendlinge10c8142008-09-09 22:39:21 +00003164/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3165/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003166void
3167SelectionDAGLowering::visitExp2(CallInst &I) {
3168 SDValue result;
Bill Wendlinge10c8142008-09-09 22:39:21 +00003169
Dale Johannesen601d3c02008-09-05 01:48:15 +00003170 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003171 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3172 SDValue Op = getValue(I.getOperand(1));
3173
3174 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, Op);
3175
3176 // FractionalPartOfX = x - (float)IntegerPartOfX;
3177 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
3178 SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, Op, t1);
3179
3180 // IntegerPartOfX <<= 23;
3181 IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
3182 DAG.getConstant(23, MVT::i32));
3183
3184 if (LimitFloatPrecision <= 6) {
3185 // For floating-point precision of 6:
3186 //
3187 // TwoToFractionalPartOfX =
3188 // 0.997535578f +
3189 // (0.735607626f + 0.252464424f * x) * x;
3190 //
3191 // error 0.0144103317, which is 6 bits
3192 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003193 getF32Constant(DAG, 0x3e814304));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003194 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003195 getF32Constant(DAG, 0x3f3c50c8));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003196 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3197 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003198 getF32Constant(DAG, 0x3f7f5e7e));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003199 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
3200 SDValue TwoToFractionalPartOfX =
3201 DAG.getNode(ISD::ADD, MVT::i32, t6, IntegerPartOfX);
3202
3203 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3204 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3205 // For floating-point precision of 12:
3206 //
3207 // TwoToFractionalPartOfX =
3208 // 0.999892986f +
3209 // (0.696457318f +
3210 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3211 //
3212 // error 0.000107046256, which is 13 to 14 bits
3213 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003214 getF32Constant(DAG, 0x3da235e3));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003215 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003216 getF32Constant(DAG, 0x3e65b8f3));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003217 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3218 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003219 getF32Constant(DAG, 0x3f324b07));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003220 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3221 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003222 getF32Constant(DAG, 0x3f7ff8fd));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003223 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
3224 SDValue TwoToFractionalPartOfX =
3225 DAG.getNode(ISD::ADD, MVT::i32, t8, IntegerPartOfX);
3226
3227 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3228 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3229 // For floating-point precision of 18:
3230 //
3231 // TwoToFractionalPartOfX =
3232 // 0.999999982f +
3233 // (0.693148872f +
3234 // (0.240227044f +
3235 // (0.554906021e-1f +
3236 // (0.961591928e-2f +
3237 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3238 // error 2.47208000*10^(-7), which is better than 18 bits
3239 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003240 getF32Constant(DAG, 0x3924b03e));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003241 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003242 getF32Constant(DAG, 0x3ab24b87));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003243 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3244 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245 getF32Constant(DAG, 0x3c1d8c17));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003246 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3247 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003248 getF32Constant(DAG, 0x3d634a1d));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003249 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3250 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003251 getF32Constant(DAG, 0x3e75fe14));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003252 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3253 SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003254 getF32Constant(DAG, 0x3f317234));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003255 SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
3256 SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003257 getF32Constant(DAG, 0x3f800000));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003258 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
3259 SDValue TwoToFractionalPartOfX =
3260 DAG.getNode(ISD::ADD, MVT::i32, t14, IntegerPartOfX);
3261
3262 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3263 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003264 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003265 // No special expansion.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003266 result = DAG.getNode(ISD::FEXP2,
3267 getValue(I.getOperand(1)).getValueType(),
3268 getValue(I.getOperand(1)));
3269 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003270
Dale Johannesen601d3c02008-09-05 01:48:15 +00003271 setValue(&I, result);
3272}
3273
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003274/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3275/// limited-precision mode with x == 10.0f.
3276void
3277SelectionDAGLowering::visitPow(CallInst &I) {
3278 SDValue result;
3279 Value *Val = I.getOperand(1);
3280 bool IsExp10 = false;
3281
3282 if (getValue(Val).getValueType() == MVT::f32 &&
Bill Wendling277fc242008-09-10 00:24:59 +00003283 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003284 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3285 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3286 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3287 APFloat Ten(10.0f);
3288 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3289 }
3290 }
3291 }
3292
3293 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3294 SDValue Op = getValue(I.getOperand(2));
3295
3296 // Put the exponent in the right bit position for later addition to the
3297 // final result:
3298 //
3299 // #define LOG2OF10 3.3219281f
3300 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
3301 SDValue t0 = DAG.getNode(ISD::FMUL, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003302 getF32Constant(DAG, 0x40549a78));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003303 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, MVT::i32, t0);
3304
3305 // FractionalPartOfX = x - (float)IntegerPartOfX;
3306 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, MVT::f32, IntegerPartOfX);
3307 SDValue X = DAG.getNode(ISD::FSUB, MVT::f32, t0, t1);
3308
3309 // IntegerPartOfX <<= 23;
3310 IntegerPartOfX = DAG.getNode(ISD::SHL, MVT::i32, IntegerPartOfX,
3311 DAG.getConstant(23, MVT::i32));
3312
3313 if (LimitFloatPrecision <= 6) {
3314 // For floating-point precision of 6:
3315 //
3316 // twoToFractionalPartOfX =
3317 // 0.997535578f +
3318 // (0.735607626f + 0.252464424f * x) * x;
3319 //
3320 // error 0.0144103317, which is 6 bits
3321 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003322 getF32Constant(DAG, 0x3e814304));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003323 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003324 getF32Constant(DAG, 0x3f3c50c8));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003325 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3326 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003327 getF32Constant(DAG, 0x3f7f5e7e));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003328 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t5);
3329 SDValue TwoToFractionalPartOfX =
3330 DAG.getNode(ISD::ADD, MVT::i32, t6, IntegerPartOfX);
3331
3332 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3333 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3334 // For floating-point precision of 12:
3335 //
3336 // TwoToFractionalPartOfX =
3337 // 0.999892986f +
3338 // (0.696457318f +
3339 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3340 //
3341 // error 0.000107046256, which is 13 to 14 bits
3342 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003343 getF32Constant(DAG, 0x3da235e3));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003344 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003345 getF32Constant(DAG, 0x3e65b8f3));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003346 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3347 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003348 getF32Constant(DAG, 0x3f324b07));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003349 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3350 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003351 getF32Constant(DAG, 0x3f7ff8fd));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003352 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t7);
3353 SDValue TwoToFractionalPartOfX =
3354 DAG.getNode(ISD::ADD, MVT::i32, t8, IntegerPartOfX);
3355
3356 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3357 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3358 // For floating-point precision of 18:
3359 //
3360 // TwoToFractionalPartOfX =
3361 // 0.999999982f +
3362 // (0.693148872f +
3363 // (0.240227044f +
3364 // (0.554906021e-1f +
3365 // (0.961591928e-2f +
3366 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3367 // error 2.47208000*10^(-7), which is better than 18 bits
3368 SDValue t2 = DAG.getNode(ISD::FMUL, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003369 getF32Constant(DAG, 0x3924b03e));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003370 SDValue t3 = DAG.getNode(ISD::FADD, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003371 getF32Constant(DAG, 0x3ab24b87));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003372 SDValue t4 = DAG.getNode(ISD::FMUL, MVT::f32, t3, X);
3373 SDValue t5 = DAG.getNode(ISD::FADD, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003374 getF32Constant(DAG, 0x3c1d8c17));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003375 SDValue t6 = DAG.getNode(ISD::FMUL, MVT::f32, t5, X);
3376 SDValue t7 = DAG.getNode(ISD::FADD, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003377 getF32Constant(DAG, 0x3d634a1d));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003378 SDValue t8 = DAG.getNode(ISD::FMUL, MVT::f32, t7, X);
3379 SDValue t9 = DAG.getNode(ISD::FADD, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003380 getF32Constant(DAG, 0x3e75fe14));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003381 SDValue t10 = DAG.getNode(ISD::FMUL, MVT::f32, t9, X);
3382 SDValue t11 = DAG.getNode(ISD::FADD, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003383 getF32Constant(DAG, 0x3f317234));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003384 SDValue t12 = DAG.getNode(ISD::FMUL, MVT::f32, t11, X);
3385 SDValue t13 = DAG.getNode(ISD::FADD, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003386 getF32Constant(DAG, 0x3f800000));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003387 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, MVT::i32, t13);
3388 SDValue TwoToFractionalPartOfX =
3389 DAG.getNode(ISD::ADD, MVT::i32, t14, IntegerPartOfX);
3390
3391 result = DAG.getNode(ISD::BIT_CONVERT, MVT::f32, TwoToFractionalPartOfX);
3392 }
3393 } else {
3394 // No special expansion.
3395 result = DAG.getNode(ISD::FPOW,
3396 getValue(I.getOperand(1)).getValueType(),
3397 getValue(I.getOperand(1)),
3398 getValue(I.getOperand(2)));
3399 }
3400
3401 setValue(&I, result);
3402}
3403
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003404/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3405/// we want to emit this as a call to a named external function, return the name
3406/// otherwise lower it and return null.
3407const char *
3408SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
3409 switch (Intrinsic) {
3410 default:
3411 // By default, turn this into a target intrinsic node.
3412 visitTargetIntrinsic(I, Intrinsic);
3413 return 0;
3414 case Intrinsic::vastart: visitVAStart(I); return 0;
3415 case Intrinsic::vaend: visitVAEnd(I); return 0;
3416 case Intrinsic::vacopy: visitVACopy(I); return 0;
3417 case Intrinsic::returnaddress:
3418 setValue(&I, DAG.getNode(ISD::RETURNADDR, TLI.getPointerTy(),
3419 getValue(I.getOperand(1))));
3420 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003421 case Intrinsic::frameaddress:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003422 setValue(&I, DAG.getNode(ISD::FRAMEADDR, TLI.getPointerTy(),
3423 getValue(I.getOperand(1))));
3424 return 0;
3425 case Intrinsic::setjmp:
3426 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3427 break;
3428 case Intrinsic::longjmp:
3429 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3430 break;
3431 case Intrinsic::memcpy_i32:
3432 case Intrinsic::memcpy_i64: {
3433 SDValue Op1 = getValue(I.getOperand(1));
3434 SDValue Op2 = getValue(I.getOperand(2));
3435 SDValue Op3 = getValue(I.getOperand(3));
3436 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3437 DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
3438 I.getOperand(1), 0, I.getOperand(2), 0));
3439 return 0;
3440 }
3441 case Intrinsic::memset_i32:
3442 case Intrinsic::memset_i64: {
3443 SDValue Op1 = getValue(I.getOperand(1));
3444 SDValue Op2 = getValue(I.getOperand(2));
3445 SDValue Op3 = getValue(I.getOperand(3));
3446 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3447 DAG.setRoot(DAG.getMemset(getRoot(), Op1, Op2, Op3, Align,
3448 I.getOperand(1), 0));
3449 return 0;
3450 }
3451 case Intrinsic::memmove_i32:
3452 case Intrinsic::memmove_i64: {
3453 SDValue Op1 = getValue(I.getOperand(1));
3454 SDValue Op2 = getValue(I.getOperand(2));
3455 SDValue Op3 = getValue(I.getOperand(3));
3456 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3457
3458 // If the source and destination are known to not be aliases, we can
3459 // lower memmove as memcpy.
3460 uint64_t Size = -1ULL;
3461 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003462 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003463 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3464 AliasAnalysis::NoAlias) {
3465 DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
3466 I.getOperand(1), 0, I.getOperand(2), 0));
3467 return 0;
3468 }
3469
3470 DAG.setRoot(DAG.getMemmove(getRoot(), Op1, Op2, Op3, Align,
3471 I.getOperand(1), 0, I.getOperand(2), 0));
3472 return 0;
3473 }
3474 case Intrinsic::dbg_stoppoint: {
3475 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3476 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
3477 if (MMI && SPI.getContext() && MMI->Verify(SPI.getContext())) {
3478 DebugInfoDesc *DD = MMI->getDescFor(SPI.getContext());
3479 assert(DD && "Not a debug information descriptor");
3480 DAG.setRoot(DAG.getDbgStopPoint(getRoot(),
3481 SPI.getLine(),
3482 SPI.getColumn(),
3483 cast<CompileUnitDesc>(DD)));
3484 }
3485
3486 return 0;
3487 }
3488 case Intrinsic::dbg_region_start: {
3489 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3490 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
3491 if (MMI && RSI.getContext() && MMI->Verify(RSI.getContext())) {
3492 unsigned LabelID = MMI->RecordRegionStart(RSI.getContext());
3493 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
3494 }
3495
3496 return 0;
3497 }
3498 case Intrinsic::dbg_region_end: {
3499 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3500 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
3501 if (MMI && REI.getContext() && MMI->Verify(REI.getContext())) {
3502 unsigned LabelID = MMI->RecordRegionEnd(REI.getContext());
3503 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
3504 }
3505
3506 return 0;
3507 }
3508 case Intrinsic::dbg_func_start: {
3509 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3510 if (!MMI) return 0;
3511 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
3512 Value *SP = FSI.getSubprogram();
3513 if (SP && MMI->Verify(SP)) {
3514 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
3515 // what (most?) gdb expects.
3516 DebugInfoDesc *DD = MMI->getDescFor(SP);
3517 assert(DD && "Not a debug information descriptor");
3518 SubprogramDesc *Subprogram = cast<SubprogramDesc>(DD);
3519 const CompileUnitDesc *CompileUnit = Subprogram->getFile();
3520 unsigned SrcFile = MMI->RecordSource(CompileUnit);
3521 // Record the source line but does create a label. It will be emitted
3522 // at asm emission time.
3523 MMI->RecordSourceLine(Subprogram->getLine(), 0, SrcFile);
3524 }
3525
3526 return 0;
3527 }
3528 case Intrinsic::dbg_declare: {
3529 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3530 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3531 Value *Variable = DI.getVariable();
3532 if (MMI && Variable && MMI->Verify(Variable))
3533 DAG.setRoot(DAG.getNode(ISD::DECLARE, MVT::Other, getRoot(),
3534 getValue(DI.getAddress()), getValue(Variable)));
3535 return 0;
3536 }
3537
3538 case Intrinsic::eh_exception: {
3539 if (!CurMBB->isLandingPad()) {
3540 // FIXME: Mark exception register as live in. Hack for PR1508.
3541 unsigned Reg = TLI.getExceptionAddressRegister();
3542 if (Reg) CurMBB->addLiveIn(Reg);
3543 }
3544 // Insert the EXCEPTIONADDR instruction.
3545 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3546 SDValue Ops[1];
3547 Ops[0] = DAG.getRoot();
3548 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, VTs, Ops, 1);
3549 setValue(&I, Op);
3550 DAG.setRoot(Op.getValue(1));
3551 return 0;
3552 }
3553
3554 case Intrinsic::eh_selector_i32:
3555 case Intrinsic::eh_selector_i64: {
3556 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3557 MVT VT = (Intrinsic == Intrinsic::eh_selector_i32 ?
3558 MVT::i32 : MVT::i64);
3559
3560 if (MMI) {
3561 if (CurMBB->isLandingPad())
3562 AddCatchInfo(I, MMI, CurMBB);
3563 else {
3564#ifndef NDEBUG
3565 FuncInfo.CatchInfoLost.insert(&I);
3566#endif
3567 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3568 unsigned Reg = TLI.getExceptionSelectorRegister();
3569 if (Reg) CurMBB->addLiveIn(Reg);
3570 }
3571
3572 // Insert the EHSELECTION instruction.
3573 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
3574 SDValue Ops[2];
3575 Ops[0] = getValue(I.getOperand(1));
3576 Ops[1] = getRoot();
3577 SDValue Op = DAG.getNode(ISD::EHSELECTION, VTs, Ops, 2);
3578 setValue(&I, Op);
3579 DAG.setRoot(Op.getValue(1));
3580 } else {
3581 setValue(&I, DAG.getConstant(0, VT));
3582 }
3583
3584 return 0;
3585 }
3586
3587 case Intrinsic::eh_typeid_for_i32:
3588 case Intrinsic::eh_typeid_for_i64: {
3589 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3590 MVT VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ?
3591 MVT::i32 : MVT::i64);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003592
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003593 if (MMI) {
3594 // Find the type id for the given typeinfo.
3595 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3596
3597 unsigned TypeID = MMI->getTypeIDFor(GV);
3598 setValue(&I, DAG.getConstant(TypeID, VT));
3599 } else {
3600 // Return something different to eh_selector.
3601 setValue(&I, DAG.getConstant(1, VT));
3602 }
3603
3604 return 0;
3605 }
3606
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003607 case Intrinsic::eh_return_i32:
3608 case Intrinsic::eh_return_i64:
3609 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003610 MMI->setCallsEHReturn(true);
3611 DAG.setRoot(DAG.getNode(ISD::EH_RETURN,
3612 MVT::Other,
3613 getControlRoot(),
3614 getValue(I.getOperand(1)),
3615 getValue(I.getOperand(2))));
3616 } else {
3617 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
3618 }
3619
3620 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003621 case Intrinsic::eh_unwind_init:
3622 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3623 MMI->setCallsUnwindInit(true);
3624 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003625
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003626 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003627
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003628 case Intrinsic::eh_dwarf_cfa: {
3629 MVT VT = getValue(I.getOperand(1)).getValueType();
3630 SDValue CfaArg;
3631 if (VT.bitsGT(TLI.getPointerTy()))
3632 CfaArg = DAG.getNode(ISD::TRUNCATE,
3633 TLI.getPointerTy(), getValue(I.getOperand(1)));
3634 else
3635 CfaArg = DAG.getNode(ISD::SIGN_EXTEND,
3636 TLI.getPointerTy(), getValue(I.getOperand(1)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003637
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003638 SDValue Offset = DAG.getNode(ISD::ADD,
3639 TLI.getPointerTy(),
3640 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET,
3641 TLI.getPointerTy()),
3642 CfaArg);
3643 setValue(&I, DAG.getNode(ISD::ADD,
3644 TLI.getPointerTy(),
3645 DAG.getNode(ISD::FRAMEADDR,
3646 TLI.getPointerTy(),
3647 DAG.getConstant(0,
3648 TLI.getPointerTy())),
3649 Offset));
3650 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003651 }
3652
3653 case Intrinsic::sqrt:
3654 setValue(&I, DAG.getNode(ISD::FSQRT,
3655 getValue(I.getOperand(1)).getValueType(),
3656 getValue(I.getOperand(1))));
3657 return 0;
3658 case Intrinsic::powi:
3659 setValue(&I, DAG.getNode(ISD::FPOWI,
3660 getValue(I.getOperand(1)).getValueType(),
3661 getValue(I.getOperand(1)),
3662 getValue(I.getOperand(2))));
3663 return 0;
3664 case Intrinsic::sin:
3665 setValue(&I, DAG.getNode(ISD::FSIN,
3666 getValue(I.getOperand(1)).getValueType(),
3667 getValue(I.getOperand(1))));
3668 return 0;
3669 case Intrinsic::cos:
3670 setValue(&I, DAG.getNode(ISD::FCOS,
3671 getValue(I.getOperand(1)).getValueType(),
3672 getValue(I.getOperand(1))));
3673 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003674 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003675 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003676 return 0;
3677 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003678 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003679 return 0;
3680 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003681 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003682 return 0;
3683 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003684 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003685 return 0;
3686 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003687 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003688 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003689 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003690 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003691 return 0;
3692 case Intrinsic::pcmarker: {
3693 SDValue Tmp = getValue(I.getOperand(1));
3694 DAG.setRoot(DAG.getNode(ISD::PCMARKER, MVT::Other, getRoot(), Tmp));
3695 return 0;
3696 }
3697 case Intrinsic::readcyclecounter: {
3698 SDValue Op = getRoot();
3699 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER,
3700 DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2,
3701 &Op, 1);
3702 setValue(&I, Tmp);
3703 DAG.setRoot(Tmp.getValue(1));
3704 return 0;
3705 }
3706 case Intrinsic::part_select: {
3707 // Currently not implemented: just abort
3708 assert(0 && "part_select intrinsic not implemented");
3709 abort();
3710 }
3711 case Intrinsic::part_set: {
3712 // Currently not implemented: just abort
3713 assert(0 && "part_set intrinsic not implemented");
3714 abort();
3715 }
3716 case Intrinsic::bswap:
3717 setValue(&I, DAG.getNode(ISD::BSWAP,
3718 getValue(I.getOperand(1)).getValueType(),
3719 getValue(I.getOperand(1))));
3720 return 0;
3721 case Intrinsic::cttz: {
3722 SDValue Arg = getValue(I.getOperand(1));
3723 MVT Ty = Arg.getValueType();
3724 SDValue result = DAG.getNode(ISD::CTTZ, Ty, Arg);
3725 setValue(&I, result);
3726 return 0;
3727 }
3728 case Intrinsic::ctlz: {
3729 SDValue Arg = getValue(I.getOperand(1));
3730 MVT Ty = Arg.getValueType();
3731 SDValue result = DAG.getNode(ISD::CTLZ, Ty, Arg);
3732 setValue(&I, result);
3733 return 0;
3734 }
3735 case Intrinsic::ctpop: {
3736 SDValue Arg = getValue(I.getOperand(1));
3737 MVT Ty = Arg.getValueType();
3738 SDValue result = DAG.getNode(ISD::CTPOP, Ty, Arg);
3739 setValue(&I, result);
3740 return 0;
3741 }
3742 case Intrinsic::stacksave: {
3743 SDValue Op = getRoot();
3744 SDValue Tmp = DAG.getNode(ISD::STACKSAVE,
3745 DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1);
3746 setValue(&I, Tmp);
3747 DAG.setRoot(Tmp.getValue(1));
3748 return 0;
3749 }
3750 case Intrinsic::stackrestore: {
3751 SDValue Tmp = getValue(I.getOperand(1));
3752 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, MVT::Other, getRoot(), Tmp));
3753 return 0;
3754 }
3755 case Intrinsic::var_annotation:
3756 // Discard annotate attributes
3757 return 0;
3758
3759 case Intrinsic::init_trampoline: {
3760 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
3761
3762 SDValue Ops[6];
3763 Ops[0] = getRoot();
3764 Ops[1] = getValue(I.getOperand(1));
3765 Ops[2] = getValue(I.getOperand(2));
3766 Ops[3] = getValue(I.getOperand(3));
3767 Ops[4] = DAG.getSrcValue(I.getOperand(1));
3768 Ops[5] = DAG.getSrcValue(F);
3769
3770 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE,
3771 DAG.getNodeValueTypes(TLI.getPointerTy(),
3772 MVT::Other), 2,
3773 Ops, 6);
3774
3775 setValue(&I, Tmp);
3776 DAG.setRoot(Tmp.getValue(1));
3777 return 0;
3778 }
3779
3780 case Intrinsic::gcroot:
3781 if (GFI) {
3782 Value *Alloca = I.getOperand(1);
3783 Constant *TypeMap = cast<Constant>(I.getOperand(2));
3784
3785 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
3786 GFI->addStackRoot(FI->getIndex(), TypeMap);
3787 }
3788 return 0;
3789
3790 case Intrinsic::gcread:
3791 case Intrinsic::gcwrite:
3792 assert(0 && "GC failed to lower gcread/gcwrite intrinsics!");
3793 return 0;
3794
3795 case Intrinsic::flt_rounds: {
3796 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, MVT::i32));
3797 return 0;
3798 }
3799
3800 case Intrinsic::trap: {
3801 DAG.setRoot(DAG.getNode(ISD::TRAP, MVT::Other, getRoot()));
3802 return 0;
3803 }
3804 case Intrinsic::prefetch: {
3805 SDValue Ops[4];
3806 Ops[0] = getRoot();
3807 Ops[1] = getValue(I.getOperand(1));
3808 Ops[2] = getValue(I.getOperand(2));
3809 Ops[3] = getValue(I.getOperand(3));
3810 DAG.setRoot(DAG.getNode(ISD::PREFETCH, MVT::Other, &Ops[0], 4));
3811 return 0;
3812 }
3813
3814 case Intrinsic::memory_barrier: {
3815 SDValue Ops[6];
3816 Ops[0] = getRoot();
3817 for (int x = 1; x < 6; ++x)
3818 Ops[x] = getValue(I.getOperand(x));
3819
3820 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, MVT::Other, &Ops[0], 6));
3821 return 0;
3822 }
3823 case Intrinsic::atomic_cmp_swap: {
3824 SDValue Root = getRoot();
3825 SDValue L;
3826 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3827 case MVT::i8:
3828 L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_8, Root,
3829 getValue(I.getOperand(1)),
3830 getValue(I.getOperand(2)),
3831 getValue(I.getOperand(3)),
3832 I.getOperand(1));
3833 break;
3834 case MVT::i16:
3835 L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_16, Root,
3836 getValue(I.getOperand(1)),
3837 getValue(I.getOperand(2)),
3838 getValue(I.getOperand(3)),
3839 I.getOperand(1));
3840 break;
3841 case MVT::i32:
3842 L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_32, Root,
3843 getValue(I.getOperand(1)),
3844 getValue(I.getOperand(2)),
3845 getValue(I.getOperand(3)),
3846 I.getOperand(1));
3847 break;
3848 case MVT::i64:
3849 L = DAG.getAtomic(ISD::ATOMIC_CMP_SWAP_64, Root,
3850 getValue(I.getOperand(1)),
3851 getValue(I.getOperand(2)),
3852 getValue(I.getOperand(3)),
3853 I.getOperand(1));
3854 break;
3855 default:
3856 assert(0 && "Invalid atomic type");
3857 abort();
3858 }
3859 setValue(&I, L);
3860 DAG.setRoot(L.getValue(1));
3861 return 0;
3862 }
3863 case Intrinsic::atomic_load_add:
3864 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3865 case MVT::i8:
3866 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_8);
3867 case MVT::i16:
3868 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_16);
3869 case MVT::i32:
3870 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_32);
3871 case MVT::i64:
3872 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD_64);
3873 default:
3874 assert(0 && "Invalid atomic type");
3875 abort();
3876 }
3877 case Intrinsic::atomic_load_sub:
3878 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3879 case MVT::i8:
3880 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_8);
3881 case MVT::i16:
3882 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_16);
3883 case MVT::i32:
3884 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_32);
3885 case MVT::i64:
3886 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB_64);
3887 default:
3888 assert(0 && "Invalid atomic type");
3889 abort();
3890 }
3891 case Intrinsic::atomic_load_or:
3892 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3893 case MVT::i8:
3894 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_8);
3895 case MVT::i16:
3896 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_16);
3897 case MVT::i32:
3898 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_32);
3899 case MVT::i64:
3900 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR_64);
3901 default:
3902 assert(0 && "Invalid atomic type");
3903 abort();
3904 }
3905 case Intrinsic::atomic_load_xor:
3906 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3907 case MVT::i8:
3908 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_8);
3909 case MVT::i16:
3910 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_16);
3911 case MVT::i32:
3912 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_32);
3913 case MVT::i64:
3914 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR_64);
3915 default:
3916 assert(0 && "Invalid atomic type");
3917 abort();
3918 }
3919 case Intrinsic::atomic_load_and:
3920 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3921 case MVT::i8:
3922 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_8);
3923 case MVT::i16:
3924 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_16);
3925 case MVT::i32:
3926 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_32);
3927 case MVT::i64:
3928 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND_64);
3929 default:
3930 assert(0 && "Invalid atomic type");
3931 abort();
3932 }
3933 case Intrinsic::atomic_load_nand:
3934 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3935 case MVT::i8:
3936 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_8);
3937 case MVT::i16:
3938 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_16);
3939 case MVT::i32:
3940 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_32);
3941 case MVT::i64:
3942 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND_64);
3943 default:
3944 assert(0 && "Invalid atomic type");
3945 abort();
3946 }
3947 case Intrinsic::atomic_load_max:
3948 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3949 case MVT::i8:
3950 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_8);
3951 case MVT::i16:
3952 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_16);
3953 case MVT::i32:
3954 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_32);
3955 case MVT::i64:
3956 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX_64);
3957 default:
3958 assert(0 && "Invalid atomic type");
3959 abort();
3960 }
3961 case Intrinsic::atomic_load_min:
3962 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3963 case MVT::i8:
3964 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_8);
3965 case MVT::i16:
3966 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_16);
3967 case MVT::i32:
3968 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_32);
3969 case MVT::i64:
3970 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN_64);
3971 default:
3972 assert(0 && "Invalid atomic type");
3973 abort();
3974 }
3975 case Intrinsic::atomic_load_umin:
3976 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3977 case MVT::i8:
3978 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_8);
3979 case MVT::i16:
3980 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_16);
3981 case MVT::i32:
3982 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_32);
3983 case MVT::i64:
3984 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN_64);
3985 default:
3986 assert(0 && "Invalid atomic type");
3987 abort();
3988 }
3989 case Intrinsic::atomic_load_umax:
3990 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
3991 case MVT::i8:
3992 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_8);
3993 case MVT::i16:
3994 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_16);
3995 case MVT::i32:
3996 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_32);
3997 case MVT::i64:
3998 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX_64);
3999 default:
4000 assert(0 && "Invalid atomic type");
4001 abort();
4002 }
4003 case Intrinsic::atomic_swap:
4004 switch (getValue(I.getOperand(2)).getValueType().getSimpleVT()) {
4005 case MVT::i8:
4006 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_8);
4007 case MVT::i16:
4008 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_16);
4009 case MVT::i32:
4010 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_32);
4011 case MVT::i64:
4012 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP_64);
4013 default:
4014 assert(0 && "Invalid atomic type");
4015 abort();
4016 }
4017 }
4018}
4019
4020
4021void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee,
4022 bool IsTailCall,
4023 MachineBasicBlock *LandingPad) {
4024 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4025 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
4026 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4027 unsigned BeginLabel = 0, EndLabel = 0;
4028
4029 TargetLowering::ArgListTy Args;
4030 TargetLowering::ArgListEntry Entry;
4031 Args.reserve(CS.arg_size());
4032 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
4033 i != e; ++i) {
4034 SDValue ArgNode = getValue(*i);
4035 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4036
4037 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004038 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4039 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4040 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4041 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4042 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4043 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004044 Entry.Alignment = CS.getParamAlignment(attrInd);
4045 Args.push_back(Entry);
4046 }
4047
4048 if (LandingPad && MMI) {
4049 // Insert a label before the invoke call to mark the try range. This can be
4050 // used to detect deletion of the invoke via the MachineModuleInfo.
4051 BeginLabel = MMI->NextLabelID();
4052 // Both PendingLoads and PendingExports must be flushed here;
4053 // this call might not return.
4054 (void)getRoot();
4055 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getControlRoot(), BeginLabel));
4056 }
4057
4058 std::pair<SDValue,SDValue> Result =
4059 TLI.LowerCallTo(getRoot(), CS.getType(),
Devang Patel05988662008-09-25 21:00:45 +00004060 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004061 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
4062 CS.paramHasAttr(0, Attribute::InReg),
4063 CS.getCallingConv(),
Dan Gohman1937e2f2008-09-16 01:42:28 +00004064 IsTailCall && PerformTailCallOpt,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004065 Callee, Args, DAG);
4066 if (CS.getType() != Type::VoidTy)
4067 setValue(CS.getInstruction(), Result.first);
4068 DAG.setRoot(Result.second);
4069
4070 if (LandingPad && MMI) {
4071 // Insert a label at the end of the invoke call to mark the try range. This
4072 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4073 EndLabel = MMI->NextLabelID();
4074 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getRoot(), EndLabel));
4075
4076 // Inform MachineModuleInfo of range.
4077 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4078 }
4079}
4080
4081
4082void SelectionDAGLowering::visitCall(CallInst &I) {
4083 const char *RenameFn = 0;
4084 if (Function *F = I.getCalledFunction()) {
4085 if (F->isDeclaration()) {
4086 if (unsigned IID = F->getIntrinsicID()) {
4087 RenameFn = visitIntrinsicCall(I, IID);
4088 if (!RenameFn)
4089 return;
4090 }
4091 }
4092
4093 // Check for well-known libc/libm calls. If the function is internal, it
4094 // can't be a library call.
4095 unsigned NameLen = F->getNameLen();
4096 if (!F->hasInternalLinkage() && NameLen) {
4097 const char *NameStr = F->getNameStart();
4098 if (NameStr[0] == 'c' &&
4099 ((NameLen == 8 && !strcmp(NameStr, "copysign")) ||
4100 (NameLen == 9 && !strcmp(NameStr, "copysignf")))) {
4101 if (I.getNumOperands() == 3 && // Basic sanity checks.
4102 I.getOperand(1)->getType()->isFloatingPoint() &&
4103 I.getType() == I.getOperand(1)->getType() &&
4104 I.getType() == I.getOperand(2)->getType()) {
4105 SDValue LHS = getValue(I.getOperand(1));
4106 SDValue RHS = getValue(I.getOperand(2));
4107 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, LHS.getValueType(),
4108 LHS, RHS));
4109 return;
4110 }
4111 } else if (NameStr[0] == 'f' &&
4112 ((NameLen == 4 && !strcmp(NameStr, "fabs")) ||
4113 (NameLen == 5 && !strcmp(NameStr, "fabsf")) ||
4114 (NameLen == 5 && !strcmp(NameStr, "fabsl")))) {
4115 if (I.getNumOperands() == 2 && // Basic sanity checks.
4116 I.getOperand(1)->getType()->isFloatingPoint() &&
4117 I.getType() == I.getOperand(1)->getType()) {
4118 SDValue Tmp = getValue(I.getOperand(1));
4119 setValue(&I, DAG.getNode(ISD::FABS, Tmp.getValueType(), Tmp));
4120 return;
4121 }
4122 } else if (NameStr[0] == 's' &&
4123 ((NameLen == 3 && !strcmp(NameStr, "sin")) ||
4124 (NameLen == 4 && !strcmp(NameStr, "sinf")) ||
4125 (NameLen == 4 && !strcmp(NameStr, "sinl")))) {
4126 if (I.getNumOperands() == 2 && // Basic sanity checks.
4127 I.getOperand(1)->getType()->isFloatingPoint() &&
4128 I.getType() == I.getOperand(1)->getType()) {
4129 SDValue Tmp = getValue(I.getOperand(1));
4130 setValue(&I, DAG.getNode(ISD::FSIN, Tmp.getValueType(), Tmp));
4131 return;
4132 }
4133 } else if (NameStr[0] == 'c' &&
4134 ((NameLen == 3 && !strcmp(NameStr, "cos")) ||
4135 (NameLen == 4 && !strcmp(NameStr, "cosf")) ||
4136 (NameLen == 4 && !strcmp(NameStr, "cosl")))) {
4137 if (I.getNumOperands() == 2 && // Basic sanity checks.
4138 I.getOperand(1)->getType()->isFloatingPoint() &&
4139 I.getType() == I.getOperand(1)->getType()) {
4140 SDValue Tmp = getValue(I.getOperand(1));
4141 setValue(&I, DAG.getNode(ISD::FCOS, Tmp.getValueType(), Tmp));
4142 return;
4143 }
4144 }
4145 }
4146 } else if (isa<InlineAsm>(I.getOperand(0))) {
4147 visitInlineAsm(&I);
4148 return;
4149 }
4150
4151 SDValue Callee;
4152 if (!RenameFn)
4153 Callee = getValue(I.getOperand(0));
4154 else
Bill Wendling056292f2008-09-16 21:48:12 +00004155 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004156
4157 LowerCallTo(&I, Callee, I.isTailCall());
4158}
4159
4160
4161/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
4162/// this value and returns the result as a ValueVT value. This uses
4163/// Chain/Flag as the input and updates them for the output Chain/Flag.
4164/// If the Flag pointer is NULL, no flag is used.
4165SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG,
4166 SDValue &Chain,
4167 SDValue *Flag) const {
4168 // Assemble the legal parts into the final values.
4169 SmallVector<SDValue, 4> Values(ValueVTs.size());
4170 SmallVector<SDValue, 8> Parts;
4171 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4172 // Copy the legal parts from the registers.
4173 MVT ValueVT = ValueVTs[Value];
4174 unsigned NumRegs = TLI->getNumRegisters(ValueVT);
4175 MVT RegisterVT = RegVTs[Value];
4176
4177 Parts.resize(NumRegs);
4178 for (unsigned i = 0; i != NumRegs; ++i) {
4179 SDValue P;
4180 if (Flag == 0)
4181 P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT);
4182 else {
4183 P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT, *Flag);
4184 *Flag = P.getValue(2);
4185 }
4186 Chain = P.getValue(1);
4187
4188 // If the source register was virtual and if we know something about it,
4189 // add an assert node.
4190 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4191 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4192 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4193 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4194 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4195 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
4196
4197 unsigned RegSize = RegisterVT.getSizeInBits();
4198 unsigned NumSignBits = LOI.NumSignBits;
4199 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
4200
4201 // FIXME: We capture more information than the dag can represent. For
4202 // now, just use the tightest assertzext/assertsext possible.
4203 bool isSExt = true;
4204 MVT FromVT(MVT::Other);
4205 if (NumSignBits == RegSize)
4206 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
4207 else if (NumZeroBits >= RegSize-1)
4208 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
4209 else if (NumSignBits > RegSize-8)
4210 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
4211 else if (NumZeroBits >= RegSize-9)
4212 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
4213 else if (NumSignBits > RegSize-16)
4214 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
4215 else if (NumZeroBits >= RegSize-17)
4216 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
4217 else if (NumSignBits > RegSize-32)
4218 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
4219 else if (NumZeroBits >= RegSize-33)
4220 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
4221
4222 if (FromVT != MVT::Other) {
4223 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext,
4224 RegisterVT, P, DAG.getValueType(FromVT));
4225
4226 }
4227 }
4228 }
4229
4230 Parts[i] = P;
4231 }
4232
4233 Values[Value] = getCopyFromParts(DAG, Parts.begin(), NumRegs, RegisterVT,
4234 ValueVT);
4235 Part += NumRegs;
4236 Parts.clear();
4237 }
4238
4239 return DAG.getMergeValues(DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4240 &Values[0], ValueVTs.size());
4241}
4242
4243/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
4244/// specified value into the registers specified by this object. This uses
4245/// Chain/Flag as the input and updates them for the output Chain/Flag.
4246/// If the Flag pointer is NULL, no flag is used.
4247void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG,
4248 SDValue &Chain, SDValue *Flag) const {
4249 // Get the list of the values's legal parts.
4250 unsigned NumRegs = Regs.size();
4251 SmallVector<SDValue, 8> Parts(NumRegs);
4252 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4253 MVT ValueVT = ValueVTs[Value];
4254 unsigned NumParts = TLI->getNumRegisters(ValueVT);
4255 MVT RegisterVT = RegVTs[Value];
4256
4257 getCopyToParts(DAG, Val.getValue(Val.getResNo() + Value),
4258 &Parts[Part], NumParts, RegisterVT);
4259 Part += NumParts;
4260 }
4261
4262 // Copy the parts into the registers.
4263 SmallVector<SDValue, 8> Chains(NumRegs);
4264 for (unsigned i = 0; i != NumRegs; ++i) {
4265 SDValue Part;
4266 if (Flag == 0)
4267 Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i]);
4268 else {
4269 Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i], *Flag);
4270 *Flag = Part.getValue(1);
4271 }
4272 Chains[i] = Part.getValue(0);
4273 }
4274
4275 if (NumRegs == 1 || Flag)
4276 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
4277 // flagged to it. That is the CopyToReg nodes and the user are considered
4278 // a single scheduling unit. If we create a TokenFactor and return it as
4279 // chain, then the TokenFactor is both a predecessor (operand) of the
4280 // user as well as a successor (the TF operands are flagged to the user).
4281 // c1, f1 = CopyToReg
4282 // c2, f2 = CopyToReg
4283 // c3 = TokenFactor c1, c2
4284 // ...
4285 // = op c3, ..., f2
4286 Chain = Chains[NumRegs-1];
4287 else
4288 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Chains[0], NumRegs);
4289}
4290
4291/// AddInlineAsmOperands - Add this value to the specified inlineasm node
4292/// operand list. This adds the code marker and includes the number of
4293/// values added into it.
4294void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
4295 std::vector<SDValue> &Ops) const {
4296 MVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
4297 Ops.push_back(DAG.getTargetConstant(Code | (Regs.size() << 3), IntPtrTy));
4298 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
4299 unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]);
4300 MVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004301 for (unsigned i = 0; i != NumRegs; ++i) {
4302 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004303 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004304 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004305 }
4306}
4307
4308/// isAllocatableRegister - If the specified register is safe to allocate,
4309/// i.e. it isn't a stack pointer or some other special register, return the
4310/// register class for the register. Otherwise, return null.
4311static const TargetRegisterClass *
4312isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4313 const TargetLowering &TLI,
4314 const TargetRegisterInfo *TRI) {
4315 MVT FoundVT = MVT::Other;
4316 const TargetRegisterClass *FoundRC = 0;
4317 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4318 E = TRI->regclass_end(); RCI != E; ++RCI) {
4319 MVT ThisVT = MVT::Other;
4320
4321 const TargetRegisterClass *RC = *RCI;
4322 // If none of the the value types for this register class are valid, we
4323 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4324 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4325 I != E; ++I) {
4326 if (TLI.isTypeLegal(*I)) {
4327 // If we have already found this register in a different register class,
4328 // choose the one with the largest VT specified. For example, on
4329 // PowerPC, we favor f64 register classes over f32.
4330 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
4331 ThisVT = *I;
4332 break;
4333 }
4334 }
4335 }
4336
4337 if (ThisVT == MVT::Other) continue;
4338
4339 // NOTE: This isn't ideal. In particular, this might allocate the
4340 // frame pointer in functions that need it (due to them not being taken
4341 // out of allocation, because a variable sized allocation hasn't been seen
4342 // yet). This is a slight code pessimization, but should still work.
4343 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4344 E = RC->allocation_order_end(MF); I != E; ++I)
4345 if (*I == Reg) {
4346 // We found a matching register class. Keep looking at others in case
4347 // we find one with larger registers that this physreg is also in.
4348 FoundRC = RC;
4349 FoundVT = ThisVT;
4350 break;
4351 }
4352 }
4353 return FoundRC;
4354}
4355
4356
4357namespace llvm {
4358/// AsmOperandInfo - This contains information for each constraint that we are
4359/// lowering.
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004360struct VISIBILITY_HIDDEN SDISelAsmOperandInfo :
4361 public TargetLowering::AsmOperandInfo {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004362 /// CallOperand - If this is the result output operand or a clobber
4363 /// this is null, otherwise it is the incoming operand to the CallInst.
4364 /// This gets modified as the asm is processed.
4365 SDValue CallOperand;
4366
4367 /// AssignedRegs - If this is a register or register class operand, this
4368 /// contains the set of register corresponding to the operand.
4369 RegsForValue AssignedRegs;
4370
4371 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4372 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4373 }
4374
4375 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4376 /// busy in OutputRegs/InputRegs.
4377 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
4378 std::set<unsigned> &OutputRegs,
4379 std::set<unsigned> &InputRegs,
4380 const TargetRegisterInfo &TRI) const {
4381 if (isOutReg) {
4382 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4383 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4384 }
4385 if (isInReg) {
4386 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4387 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4388 }
4389 }
Chris Lattner81249c92008-10-17 17:05:25 +00004390
4391 /// getCallOperandValMVT - Return the MVT of the Value* that this operand
4392 /// corresponds to. If there is no Value* for this operand, it returns
4393 /// MVT::Other.
4394 MVT getCallOperandValMVT(const TargetLowering &TLI,
4395 const TargetData *TD) const {
4396 if (CallOperandVal == 0) return MVT::Other;
4397
4398 if (isa<BasicBlock>(CallOperandVal))
4399 return TLI.getPointerTy();
4400
4401 const llvm::Type *OpTy = CallOperandVal->getType();
4402
4403 // If this is an indirect operand, the operand is a pointer to the
4404 // accessed type.
4405 if (isIndirect)
4406 OpTy = cast<PointerType>(OpTy)->getElementType();
4407
4408 // If OpTy is not a single value, it may be a struct/union that we
4409 // can tile with integers.
4410 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4411 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4412 switch (BitSize) {
4413 default: break;
4414 case 1:
4415 case 8:
4416 case 16:
4417 case 32:
4418 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004419 case 128:
Chris Lattner81249c92008-10-17 17:05:25 +00004420 OpTy = IntegerType::get(BitSize);
4421 break;
4422 }
4423 }
4424
4425 return TLI.getValueType(OpTy, true);
4426 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004427
4428private:
4429 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4430 /// specified set.
4431 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
4432 const TargetRegisterInfo &TRI) {
4433 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4434 Regs.insert(Reg);
4435 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4436 for (; *Aliases; ++Aliases)
4437 Regs.insert(*Aliases);
4438 }
4439};
4440} // end llvm namespace.
4441
4442
4443/// GetRegistersForValue - Assign registers (virtual or physical) for the
4444/// specified operand. We prefer to assign virtual registers, to allow the
4445/// register allocator handle the assignment process. However, if the asm uses
4446/// features that we can't model on machineinstrs, we have SDISel do the
4447/// allocation. This produces generally horrible, but correct, code.
4448///
4449/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004450/// Input and OutputRegs are the set of already allocated physical registers.
4451///
4452void SelectionDAGLowering::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004453GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004454 std::set<unsigned> &OutputRegs,
4455 std::set<unsigned> &InputRegs) {
4456 // Compute whether this value requires an input register, an output register,
4457 // or both.
4458 bool isOutReg = false;
4459 bool isInReg = false;
4460 switch (OpInfo.Type) {
4461 case InlineAsm::isOutput:
4462 isOutReg = true;
4463
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004464 // If there is an input constraint that matches this, we need to reserve
4465 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004466 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004467 break;
4468 case InlineAsm::isInput:
4469 isInReg = true;
4470 isOutReg = false;
4471 break;
4472 case InlineAsm::isClobber:
4473 isOutReg = true;
4474 isInReg = true;
4475 break;
4476 }
4477
4478
4479 MachineFunction &MF = DAG.getMachineFunction();
4480 SmallVector<unsigned, 4> Regs;
4481
4482 // If this is a constraint for a single physreg, or a constraint for a
4483 // register class, find it.
4484 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
4485 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4486 OpInfo.ConstraintVT);
4487
4488 unsigned NumRegs = 1;
4489 if (OpInfo.ConstraintVT != MVT::Other)
4490 NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT);
4491 MVT RegVT;
4492 MVT ValueVT = OpInfo.ConstraintVT;
4493
4494
4495 // If this is a constraint for a specific physical register, like {r17},
4496 // assign it now.
4497 if (PhysReg.first) {
4498 if (OpInfo.ConstraintVT == MVT::Other)
4499 ValueVT = *PhysReg.second->vt_begin();
4500
4501 // Get the actual register value type. This is important, because the user
4502 // may have asked for (e.g.) the AX register in i32 type. We need to
4503 // remember that AX is actually i16 to get the right extension.
4504 RegVT = *PhysReg.second->vt_begin();
4505
4506 // This is a explicit reference to a physical register.
4507 Regs.push_back(PhysReg.first);
4508
4509 // If this is an expanded reference, add the rest of the regs to Regs.
4510 if (NumRegs != 1) {
4511 TargetRegisterClass::iterator I = PhysReg.second->begin();
4512 for (; *I != PhysReg.first; ++I)
4513 assert(I != PhysReg.second->end() && "Didn't find reg!");
4514
4515 // Already added the first reg.
4516 --NumRegs; ++I;
4517 for (; NumRegs; --NumRegs, ++I) {
4518 assert(I != PhysReg.second->end() && "Ran out of registers to allocate!");
4519 Regs.push_back(*I);
4520 }
4521 }
4522 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4523 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4524 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4525 return;
4526 }
4527
4528 // Otherwise, if this was a reference to an LLVM register class, create vregs
4529 // for this reference.
4530 std::vector<unsigned> RegClassRegs;
4531 const TargetRegisterClass *RC = PhysReg.second;
4532 if (RC) {
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004533 // If this is a tied register, our regalloc doesn't know how to maintain
Chris Lattner58f15c42008-10-17 16:21:11 +00004534 // the constraint, so we have to pick a register to pin the input/output to.
4535 // If it isn't a matched constraint, go ahead and create vreg and let the
4536 // regalloc do its thing.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004537 if (!OpInfo.hasMatchingInput()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004538 RegVT = *PhysReg.second->vt_begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004539 if (OpInfo.ConstraintVT == MVT::Other)
4540 ValueVT = RegVT;
4541
4542 // Create the appropriate number of virtual registers.
4543 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4544 for (; NumRegs; --NumRegs)
4545 Regs.push_back(RegInfo.createVirtualRegister(PhysReg.second));
4546
4547 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4548 return;
4549 }
4550
4551 // Otherwise, we can't allocate it. Let the code below figure out how to
4552 // maintain these constraints.
4553 RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end());
4554
4555 } else {
4556 // This is a reference to a register class that doesn't directly correspond
4557 // to an LLVM register class. Allocate NumRegs consecutive, available,
4558 // registers from the class.
4559 RegClassRegs = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
4560 OpInfo.ConstraintVT);
4561 }
4562
4563 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4564 unsigned NumAllocated = 0;
4565 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
4566 unsigned Reg = RegClassRegs[i];
4567 // See if this register is available.
4568 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
4569 (isInReg && InputRegs.count(Reg))) { // Already used.
4570 // Make sure we find consecutive registers.
4571 NumAllocated = 0;
4572 continue;
4573 }
4574
4575 // Check to see if this register is allocatable (i.e. don't give out the
4576 // stack pointer).
4577 if (RC == 0) {
4578 RC = isAllocatableRegister(Reg, MF, TLI, TRI);
4579 if (!RC) { // Couldn't allocate this register.
4580 // Reset NumAllocated to make sure we return consecutive registers.
4581 NumAllocated = 0;
4582 continue;
4583 }
4584 }
4585
4586 // Okay, this register is good, we can use it.
4587 ++NumAllocated;
4588
4589 // If we allocated enough consecutive registers, succeed.
4590 if (NumAllocated == NumRegs) {
4591 unsigned RegStart = (i-NumAllocated)+1;
4592 unsigned RegEnd = i+1;
4593 // Mark all of the allocated registers used.
4594 for (unsigned i = RegStart; i != RegEnd; ++i)
4595 Regs.push_back(RegClassRegs[i]);
4596
4597 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
4598 OpInfo.ConstraintVT);
4599 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4600 return;
4601 }
4602 }
4603
4604 // Otherwise, we couldn't allocate enough registers for this.
4605}
4606
Evan Chengda43bcf2008-09-24 00:05:32 +00004607/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
4608/// processed uses a memory 'm' constraint.
4609static bool
4610hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
4611 TargetLowering &TLI) {
4612 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
4613 InlineAsm::ConstraintInfo &CI = CInfos[i];
4614 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
4615 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
4616 if (CType == TargetLowering::C_Memory)
4617 return true;
4618 }
4619 }
4620
4621 return false;
4622}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004623
4624/// visitInlineAsm - Handle a call to an InlineAsm object.
4625///
4626void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
4627 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
4628
4629 /// ConstraintOperands - Information about all of the constraints.
4630 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
4631
4632 SDValue Chain = getRoot();
4633 SDValue Flag;
4634
4635 std::set<unsigned> OutputRegs, InputRegs;
4636
4637 // Do a prepass over the constraints, canonicalizing them, and building up the
4638 // ConstraintOperands list.
4639 std::vector<InlineAsm::ConstraintInfo>
4640 ConstraintInfos = IA->ParseConstraints();
4641
Evan Chengda43bcf2008-09-24 00:05:32 +00004642 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004643
4644 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
4645 unsigned ResNo = 0; // ResNo - The result number of the next output.
4646 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4647 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
4648 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
4649
4650 MVT OpVT = MVT::Other;
4651
4652 // Compute the value type for each operand.
4653 switch (OpInfo.Type) {
4654 case InlineAsm::isOutput:
4655 // Indirect outputs just consume an argument.
4656 if (OpInfo.isIndirect) {
4657 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4658 break;
4659 }
Chris Lattner0c526442008-10-17 17:52:49 +00004660
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004661 // The return value of the call is this value. As such, there is no
4662 // corresponding argument.
4663 assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
4664 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
4665 OpVT = TLI.getValueType(STy->getElementType(ResNo));
4666 } else {
4667 assert(ResNo == 0 && "Asm only has one result!");
4668 OpVT = TLI.getValueType(CS.getType());
4669 }
4670 ++ResNo;
4671 break;
4672 case InlineAsm::isInput:
4673 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4674 break;
4675 case InlineAsm::isClobber:
4676 // Nothing to do.
4677 break;
4678 }
4679
4680 // If this is an input or an indirect output, process the call argument.
4681 // BasicBlocks are labels, currently appearing only in asm's.
4682 if (OpInfo.CallOperandVal) {
Chris Lattner81249c92008-10-17 17:05:25 +00004683 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004684 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00004685 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004686 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004687 }
Chris Lattner81249c92008-10-17 17:05:25 +00004688
4689 OpVT = OpInfo.getCallOperandValMVT(TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004690 }
4691
4692 OpInfo.ConstraintVT = OpVT;
Chris Lattner0c526442008-10-17 17:52:49 +00004693 }
4694
4695 // Second pass over the constraints: compute which constraint option to use
4696 // and assign registers to constraints that want a specific physreg.
4697 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4698 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
4699
4700 // If this is an output operand with a matching input operand, look up the
4701 // matching input. It might have a different type (e.g. the output might be
4702 // i32 and the input i64) and we need to pick the larger width to ensure we
4703 // reserve the right number of registers.
4704 if (OpInfo.hasMatchingInput()) {
4705 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
4706 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
4707 assert(OpInfo.ConstraintVT.isInteger() &&
4708 Input.ConstraintVT.isInteger() &&
4709 "Asm constraints must be the same or different sized integers");
4710 if (OpInfo.ConstraintVT.getSizeInBits() <
4711 Input.ConstraintVT.getSizeInBits())
4712 OpInfo.ConstraintVT = Input.ConstraintVT;
4713 else
4714 Input.ConstraintVT = OpInfo.ConstraintVT;
4715 }
4716 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004717
4718 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00004719 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004720
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004721 // If this is a memory input, and if the operand is not indirect, do what we
4722 // need to to provide an address for the memory input.
4723 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
4724 !OpInfo.isIndirect) {
4725 assert(OpInfo.Type == InlineAsm::isInput &&
4726 "Can only indirectify direct input operands!");
4727
4728 // Memory operands really want the address of the value. If we don't have
4729 // an indirect input, put it in the constpool if we can, otherwise spill
4730 // it to a stack slot.
4731
4732 // If the operand is a float, integer, or vector constant, spill to a
4733 // constant pool entry to get its address.
4734 Value *OpVal = OpInfo.CallOperandVal;
4735 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
4736 isa<ConstantVector>(OpVal)) {
4737 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
4738 TLI.getPointerTy());
4739 } else {
4740 // Otherwise, create a stack slot and emit a store to it before the
4741 // asm.
4742 const Type *Ty = OpVal->getType();
4743 uint64_t TySize = TLI.getTargetData()->getABITypeSize(Ty);
4744 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
4745 MachineFunction &MF = DAG.getMachineFunction();
4746 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
4747 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4748 Chain = DAG.getStore(Chain, OpInfo.CallOperand, StackSlot, NULL, 0);
4749 OpInfo.CallOperand = StackSlot;
4750 }
4751
4752 // There is no longer a Value* corresponding to this operand.
4753 OpInfo.CallOperandVal = 0;
4754 // It is now an indirect operand.
4755 OpInfo.isIndirect = true;
4756 }
4757
4758 // If this constraint is for a specific register, allocate it before
4759 // anything else.
4760 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004761 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004762 }
4763 ConstraintInfos.clear();
4764
4765
4766 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00004767 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
4769 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
4770
4771 // C_Register operands have already been allocated, Other/Memory don't need
4772 // to be.
4773 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004774 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004775 }
4776
4777 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
4778 std::vector<SDValue> AsmNodeOperands;
4779 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
4780 AsmNodeOperands.push_back(
Bill Wendling056292f2008-09-16 21:48:12 +00004781 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004782
4783
4784 // Loop over all of the inputs, copying the operand values into the
4785 // appropriate registers and processing the output regs.
4786 RegsForValue RetValRegs;
4787
4788 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
4789 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
4790
4791 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
4792 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
4793
4794 switch (OpInfo.Type) {
4795 case InlineAsm::isOutput: {
4796 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
4797 OpInfo.ConstraintType != TargetLowering::C_Register) {
4798 // Memory output, or 'other' output (e.g. 'X' constraint).
4799 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
4800
4801 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00004802 unsigned ResOpType = 4/*MEM*/ | (1<<3);
4803 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004804 TLI.getPointerTy()));
4805 AsmNodeOperands.push_back(OpInfo.CallOperand);
4806 break;
4807 }
4808
4809 // Otherwise, this is a register or register class output.
4810
4811 // Copy the output from the appropriate register. Find a register that
4812 // we can use.
4813 if (OpInfo.AssignedRegs.Regs.empty()) {
4814 cerr << "Couldn't allocate output reg for constraint '"
4815 << OpInfo.ConstraintCode << "'!\n";
4816 exit(1);
4817 }
4818
4819 // If this is an indirect operand, store through the pointer after the
4820 // asm.
4821 if (OpInfo.isIndirect) {
4822 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
4823 OpInfo.CallOperandVal));
4824 } else {
4825 // This is the result value of the call.
4826 assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
4827 // Concatenate this output onto the outputs list.
4828 RetValRegs.append(OpInfo.AssignedRegs);
4829 }
4830
4831 // Add information to the INLINEASM node to know that this register is
4832 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00004833 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
4834 6 /* EARLYCLOBBER REGDEF */ :
4835 2 /* REGDEF */ ,
4836 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004837 break;
4838 }
4839 case InlineAsm::isInput: {
4840 SDValue InOperandVal = OpInfo.CallOperand;
4841
Chris Lattner6bdcda32008-10-17 16:47:46 +00004842 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004843 // If this is required to match an output register we have already set,
4844 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00004845 unsigned OperandNo = OpInfo.getMatchedOperand();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004846
4847 // Scan until we find the definition we already emitted of this operand.
4848 // When we find it, create a RegsForValue operand.
4849 unsigned CurOp = 2; // The first operand.
4850 for (; OperandNo; --OperandNo) {
4851 // Advance to the next operand.
4852 unsigned NumOps =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004853 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854 assert(((NumOps & 7) == 2 /*REGDEF*/ ||
Dale Johannesen913d3df2008-09-12 17:49:03 +00004855 (NumOps & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
Dale Johannesen86b49f82008-09-24 01:07:17 +00004856 (NumOps & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004857 "Skipped past definitions?");
4858 CurOp += (NumOps>>3)+1;
4859 }
4860
4861 unsigned NumOps =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004862 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Dale Johannesen913d3df2008-09-12 17:49:03 +00004863 if ((NumOps & 7) == 2 /*REGDEF*/
4864 || (NumOps & 7) == 6 /* EARLYCLOBBER REGDEF */) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004865 // Add NumOps>>3 registers to MatchedRegs.
4866 RegsForValue MatchedRegs;
4867 MatchedRegs.TLI = &TLI;
4868 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
4869 MatchedRegs.RegVTs.push_back(AsmNodeOperands[CurOp+1].getValueType());
4870 for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
4871 unsigned Reg =
4872 cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
4873 MatchedRegs.Regs.push_back(Reg);
4874 }
4875
4876 // Use the produced MatchedRegs object to
4877 MatchedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag);
Dale Johannesen86b49f82008-09-24 01:07:17 +00004878 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004879 break;
4880 } else {
Dale Johannesen86b49f82008-09-24 01:07:17 +00004881 assert(((NumOps & 7) == 4) && "Unknown matching constraint!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004882 assert((NumOps >> 3) == 1 && "Unexpected number of operands");
4883 // Add information to the INLINEASM node to know about this input.
Dale Johannesen91aac102008-09-17 21:13:11 +00004884 AsmNodeOperands.push_back(DAG.getTargetConstant(NumOps,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004885 TLI.getPointerTy()));
4886 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
4887 break;
4888 }
4889 }
4890
4891 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
4892 assert(!OpInfo.isIndirect &&
4893 "Don't know how to handle indirect other inputs yet!");
4894
4895 std::vector<SDValue> Ops;
4896 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00004897 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004898 if (Ops.empty()) {
4899 cerr << "Invalid operand for inline asm constraint '"
4900 << OpInfo.ConstraintCode << "'!\n";
4901 exit(1);
4902 }
4903
4904 // Add information to the INLINEASM node to know about this input.
4905 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
4906 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
4907 TLI.getPointerTy()));
4908 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
4909 break;
4910 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
4911 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
4912 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
4913 "Memory operands expect pointer values");
4914
4915 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00004916 unsigned ResOpType = 4/*MEM*/ | (1<<3);
4917 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004918 TLI.getPointerTy()));
4919 AsmNodeOperands.push_back(InOperandVal);
4920 break;
4921 }
4922
4923 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
4924 OpInfo.ConstraintType == TargetLowering::C_Register) &&
4925 "Unknown constraint type!");
4926 assert(!OpInfo.isIndirect &&
4927 "Don't know how to handle indirect register inputs yet!");
4928
4929 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00004930 if (OpInfo.AssignedRegs.Regs.empty()) {
4931 cerr << "Couldn't allocate output reg for constraint '"
4932 << OpInfo.ConstraintCode << "'!\n";
4933 exit(1);
4934 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935
4936 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, Chain, &Flag);
4937
Dale Johannesen86b49f82008-09-24 01:07:17 +00004938 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/,
4939 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004940 break;
4941 }
4942 case InlineAsm::isClobber: {
4943 // Add the clobbered value to the operand list, so that the register
4944 // allocator is aware that the physreg got clobbered.
4945 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00004946 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
4947 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004948 break;
4949 }
4950 }
4951 }
4952
4953 // Finish up input operands.
4954 AsmNodeOperands[0] = Chain;
4955 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
4956
4957 Chain = DAG.getNode(ISD::INLINEASM,
4958 DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2,
4959 &AsmNodeOperands[0], AsmNodeOperands.size());
4960 Flag = Chain.getValue(1);
4961
4962 // If this asm returns a register value, copy the result from that register
4963 // and set it as the value of the call.
4964 if (!RetValRegs.Regs.empty()) {
4965 SDValue Val = RetValRegs.getCopyFromRegs(DAG, Chain, &Flag);
Chris Lattner0c526442008-10-17 17:52:49 +00004966 MVT ResultType = TLI.getValueType(CS.getType());
4967
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004968 // If any of the results of the inline asm is a vector, it may have the
4969 // wrong width/num elts. This can happen for register classes that can
4970 // contain multiple different value types. The preg or vreg allocated may
Chris Lattner0c526442008-10-17 17:52:49 +00004971 // not have the same VT as was expected. Convert it to the right type
4972 // with bit_convert.
4973 // FIXME: Is this sufficient for inline asms with MRVs?
4974 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
4975 Val = DAG.getNode(ISD::BIT_CONVERT, ResultType, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004976
Chris Lattner0c526442008-10-17 17:52:49 +00004977 } else if (ResultType != Val.getValueType() &&
4978 ResultType.isInteger() && Val.getValueType().isInteger()) {
4979 // If a result value was tied to an input value, the computed result may
4980 // have a wider width than the expected result. Extract the relevant
4981 // portion.
4982 Val = DAG.getNode(ISD::TRUNCATE, ResultType, Val);
4983 }
4984
4985 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004986 setValue(CS.getInstruction(), Val);
4987 }
4988
4989 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
4990
4991 // Process indirect outputs, first output all of the flagged copies out of
4992 // physregs.
4993 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
4994 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
4995 Value *Ptr = IndirectStoresToEmit[i].second;
4996 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, Chain, &Flag);
4997 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
4998 }
4999
5000 // Emit the non-flagged stores from the physregs.
5001 SmallVector<SDValue, 8> OutChains;
5002 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
5003 OutChains.push_back(DAG.getStore(Chain, StoresToEmit[i].first,
5004 getValue(StoresToEmit[i].second),
5005 StoresToEmit[i].second, 0));
5006 if (!OutChains.empty())
5007 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
5008 &OutChains[0], OutChains.size());
5009 DAG.setRoot(Chain);
5010}
5011
5012
5013void SelectionDAGLowering::visitMalloc(MallocInst &I) {
5014 SDValue Src = getValue(I.getOperand(0));
5015
5016 MVT IntPtr = TLI.getPointerTy();
5017
5018 if (IntPtr.bitsLT(Src.getValueType()))
5019 Src = DAG.getNode(ISD::TRUNCATE, IntPtr, Src);
5020 else if (IntPtr.bitsGT(Src.getValueType()))
5021 Src = DAG.getNode(ISD::ZERO_EXTEND, IntPtr, Src);
5022
5023 // Scale the source by the type size.
5024 uint64_t ElementSize = TD->getABITypeSize(I.getType()->getElementType());
5025 Src = DAG.getNode(ISD::MUL, Src.getValueType(),
5026 Src, DAG.getIntPtrConstant(ElementSize));
5027
5028 TargetLowering::ArgListTy Args;
5029 TargetLowering::ArgListEntry Entry;
5030 Entry.Node = Src;
5031 Entry.Ty = TLI.getTargetData()->getIntPtrType();
5032 Args.push_back(Entry);
5033
5034 std::pair<SDValue,SDValue> Result =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005035 TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, false,
5036 CallingConv::C, PerformTailCallOpt,
5037 DAG.getExternalSymbol("malloc", IntPtr),
Dan Gohman1937e2f2008-09-16 01:42:28 +00005038 Args, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005039 setValue(&I, Result.first); // Pointers always fit in registers
5040 DAG.setRoot(Result.second);
5041}
5042
5043void SelectionDAGLowering::visitFree(FreeInst &I) {
5044 TargetLowering::ArgListTy Args;
5045 TargetLowering::ArgListEntry Entry;
5046 Entry.Node = getValue(I.getOperand(0));
5047 Entry.Ty = TLI.getTargetData()->getIntPtrType();
5048 Args.push_back(Entry);
5049 MVT IntPtr = TLI.getPointerTy();
5050 std::pair<SDValue,SDValue> Result =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005051 TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false, false,
Dan Gohman1937e2f2008-09-16 01:42:28 +00005052 CallingConv::C, PerformTailCallOpt,
Bill Wendling056292f2008-09-16 21:48:12 +00005053 DAG.getExternalSymbol("free", IntPtr), Args, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005054 DAG.setRoot(Result.second);
5055}
5056
5057void SelectionDAGLowering::visitVAStart(CallInst &I) {
5058 DAG.setRoot(DAG.getNode(ISD::VASTART, MVT::Other, getRoot(),
5059 getValue(I.getOperand(1)),
5060 DAG.getSrcValue(I.getOperand(1))));
5061}
5062
5063void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
5064 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
5065 getValue(I.getOperand(0)),
5066 DAG.getSrcValue(I.getOperand(0)));
5067 setValue(&I, V);
5068 DAG.setRoot(V.getValue(1));
5069}
5070
5071void SelectionDAGLowering::visitVAEnd(CallInst &I) {
5072 DAG.setRoot(DAG.getNode(ISD::VAEND, MVT::Other, getRoot(),
5073 getValue(I.getOperand(1)),
5074 DAG.getSrcValue(I.getOperand(1))));
5075}
5076
5077void SelectionDAGLowering::visitVACopy(CallInst &I) {
5078 DAG.setRoot(DAG.getNode(ISD::VACOPY, MVT::Other, getRoot(),
5079 getValue(I.getOperand(1)),
5080 getValue(I.getOperand(2)),
5081 DAG.getSrcValue(I.getOperand(1)),
5082 DAG.getSrcValue(I.getOperand(2))));
5083}
5084
5085/// TargetLowering::LowerArguments - This is the default LowerArguments
5086/// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all
5087/// targets are migrated to using FORMAL_ARGUMENTS, this hook should be
5088/// integrated into SDISel.
5089void TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG,
5090 SmallVectorImpl<SDValue> &ArgValues) {
5091 // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node.
5092 SmallVector<SDValue, 3+16> Ops;
5093 Ops.push_back(DAG.getRoot());
5094 Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy()));
5095 Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy()));
5096
5097 // Add one result value for each formal argument.
5098 SmallVector<MVT, 16> RetVals;
5099 unsigned j = 1;
5100 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5101 I != E; ++I, ++j) {
5102 SmallVector<MVT, 4> ValueVTs;
5103 ComputeValueVTs(*this, I->getType(), ValueVTs);
5104 for (unsigned Value = 0, NumValues = ValueVTs.size();
5105 Value != NumValues; ++Value) {
5106 MVT VT = ValueVTs[Value];
5107 const Type *ArgTy = VT.getTypeForMVT();
5108 ISD::ArgFlagsTy Flags;
5109 unsigned OriginalAlignment =
5110 getTargetData()->getABITypeAlignment(ArgTy);
5111
Devang Patel05988662008-09-25 21:00:45 +00005112 if (F.paramHasAttr(j, Attribute::ZExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005113 Flags.setZExt();
Devang Patel05988662008-09-25 21:00:45 +00005114 if (F.paramHasAttr(j, Attribute::SExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005115 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00005116 if (F.paramHasAttr(j, Attribute::InReg))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005117 Flags.setInReg();
Devang Patel05988662008-09-25 21:00:45 +00005118 if (F.paramHasAttr(j, Attribute::StructRet))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005119 Flags.setSRet();
Devang Patel05988662008-09-25 21:00:45 +00005120 if (F.paramHasAttr(j, Attribute::ByVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005121 Flags.setByVal();
5122 const PointerType *Ty = cast<PointerType>(I->getType());
5123 const Type *ElementTy = Ty->getElementType();
5124 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
5125 unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy);
5126 // For ByVal, alignment should be passed from FE. BE will guess if
5127 // this info is not there but there are cases it cannot get right.
5128 if (F.getParamAlignment(j))
5129 FrameAlign = F.getParamAlignment(j);
5130 Flags.setByValAlign(FrameAlign);
5131 Flags.setByValSize(FrameSize);
5132 }
Devang Patel05988662008-09-25 21:00:45 +00005133 if (F.paramHasAttr(j, Attribute::Nest))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005134 Flags.setNest();
5135 Flags.setOrigAlign(OriginalAlignment);
5136
5137 MVT RegisterVT = getRegisterType(VT);
5138 unsigned NumRegs = getNumRegisters(VT);
5139 for (unsigned i = 0; i != NumRegs; ++i) {
5140 RetVals.push_back(RegisterVT);
5141 ISD::ArgFlagsTy MyFlags = Flags;
5142 if (NumRegs > 1 && i == 0)
5143 MyFlags.setSplit();
5144 // if it isn't first piece, alignment must be 1
5145 else if (i > 0)
5146 MyFlags.setOrigAlign(1);
5147 Ops.push_back(DAG.getArgFlags(MyFlags));
5148 }
5149 }
5150 }
5151
5152 RetVals.push_back(MVT::Other);
5153
5154 // Create the node.
5155 SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS,
5156 DAG.getVTList(&RetVals[0], RetVals.size()),
5157 &Ops[0], Ops.size()).getNode();
5158
5159 // Prelower FORMAL_ARGUMENTS. This isn't required for functionality, but
5160 // allows exposing the loads that may be part of the argument access to the
5161 // first DAGCombiner pass.
5162 SDValue TmpRes = LowerOperation(SDValue(Result, 0), DAG);
5163
5164 // The number of results should match up, except that the lowered one may have
5165 // an extra flag result.
5166 assert((Result->getNumValues() == TmpRes.getNode()->getNumValues() ||
5167 (Result->getNumValues()+1 == TmpRes.getNode()->getNumValues() &&
5168 TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag))
5169 && "Lowering produced unexpected number of results!");
5170
5171 // The FORMAL_ARGUMENTS node itself is likely no longer needed.
5172 if (Result != TmpRes.getNode() && Result->use_empty()) {
5173 HandleSDNode Dummy(DAG.getRoot());
5174 DAG.RemoveDeadNode(Result);
5175 }
5176
5177 Result = TmpRes.getNode();
5178
5179 unsigned NumArgRegs = Result->getNumValues() - 1;
5180 DAG.setRoot(SDValue(Result, NumArgRegs));
5181
5182 // Set up the return result vector.
5183 unsigned i = 0;
5184 unsigned Idx = 1;
5185 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5186 ++I, ++Idx) {
5187 SmallVector<MVT, 4> ValueVTs;
5188 ComputeValueVTs(*this, I->getType(), ValueVTs);
5189 for (unsigned Value = 0, NumValues = ValueVTs.size();
5190 Value != NumValues; ++Value) {
5191 MVT VT = ValueVTs[Value];
5192 MVT PartVT = getRegisterType(VT);
5193
5194 unsigned NumParts = getNumRegisters(VT);
5195 SmallVector<SDValue, 4> Parts(NumParts);
5196 for (unsigned j = 0; j != NumParts; ++j)
5197 Parts[j] = SDValue(Result, i++);
5198
5199 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Devang Patel05988662008-09-25 21:00:45 +00005200 if (F.paramHasAttr(Idx, Attribute::SExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005201 AssertOp = ISD::AssertSext;
Devang Patel05988662008-09-25 21:00:45 +00005202 else if (F.paramHasAttr(Idx, Attribute::ZExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005203 AssertOp = ISD::AssertZext;
5204
5205 ArgValues.push_back(getCopyFromParts(DAG, &Parts[0], NumParts, PartVT, VT,
5206 AssertOp));
5207 }
5208 }
5209 assert(i == NumArgRegs && "Argument register count mismatch!");
5210}
5211
5212
5213/// TargetLowering::LowerCallTo - This is the default LowerCallTo
5214/// implementation, which just inserts an ISD::CALL node, which is later custom
5215/// lowered by the target to something concrete. FIXME: When all targets are
5216/// migrated to using ISD::CALL, this hook should be integrated into SDISel.
5217std::pair<SDValue, SDValue>
5218TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5219 bool RetSExt, bool RetZExt, bool isVarArg,
Dale Johannesen86098bd2008-09-26 19:31:26 +00005220 bool isInreg,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005221 unsigned CallingConv, bool isTailCall,
5222 SDValue Callee,
5223 ArgListTy &Args, SelectionDAG &DAG) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00005224 assert((!isTailCall || PerformTailCallOpt) &&
5225 "isTailCall set when tail-call optimizations are disabled!");
5226
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005227 SmallVector<SDValue, 32> Ops;
5228 Ops.push_back(Chain); // Op#0 - Chain
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005229 Ops.push_back(Callee);
5230
5231 // Handle all of the outgoing arguments.
5232 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
5233 SmallVector<MVT, 4> ValueVTs;
5234 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5235 for (unsigned Value = 0, NumValues = ValueVTs.size();
5236 Value != NumValues; ++Value) {
5237 MVT VT = ValueVTs[Value];
5238 const Type *ArgTy = VT.getTypeForMVT();
Chris Lattner0c526442008-10-17 17:52:49 +00005239 SDValue Op = SDValue(Args[i].Node.getNode(),
5240 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005241 ISD::ArgFlagsTy Flags;
5242 unsigned OriginalAlignment =
5243 getTargetData()->getABITypeAlignment(ArgTy);
5244
5245 if (Args[i].isZExt)
5246 Flags.setZExt();
5247 if (Args[i].isSExt)
5248 Flags.setSExt();
5249 if (Args[i].isInReg)
5250 Flags.setInReg();
5251 if (Args[i].isSRet)
5252 Flags.setSRet();
5253 if (Args[i].isByVal) {
5254 Flags.setByVal();
5255 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5256 const Type *ElementTy = Ty->getElementType();
5257 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
5258 unsigned FrameSize = getTargetData()->getABITypeSize(ElementTy);
5259 // For ByVal, alignment should come from FE. BE will guess if this
5260 // info is not there but there are cases it cannot get right.
5261 if (Args[i].Alignment)
5262 FrameAlign = Args[i].Alignment;
5263 Flags.setByValAlign(FrameAlign);
5264 Flags.setByValSize(FrameSize);
5265 }
5266 if (Args[i].isNest)
5267 Flags.setNest();
5268 Flags.setOrigAlign(OriginalAlignment);
5269
5270 MVT PartVT = getRegisterType(VT);
5271 unsigned NumParts = getNumRegisters(VT);
5272 SmallVector<SDValue, 4> Parts(NumParts);
5273 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5274
5275 if (Args[i].isSExt)
5276 ExtendKind = ISD::SIGN_EXTEND;
5277 else if (Args[i].isZExt)
5278 ExtendKind = ISD::ZERO_EXTEND;
5279
5280 getCopyToParts(DAG, Op, &Parts[0], NumParts, PartVT, ExtendKind);
5281
5282 for (unsigned i = 0; i != NumParts; ++i) {
5283 // if it isn't first piece, alignment must be 1
5284 ISD::ArgFlagsTy MyFlags = Flags;
5285 if (NumParts > 1 && i == 0)
5286 MyFlags.setSplit();
5287 else if (i != 0)
5288 MyFlags.setOrigAlign(1);
5289
5290 Ops.push_back(Parts[i]);
5291 Ops.push_back(DAG.getArgFlags(MyFlags));
5292 }
5293 }
5294 }
5295
5296 // Figure out the result value types. We start by making a list of
5297 // the potentially illegal return value types.
5298 SmallVector<MVT, 4> LoweredRetTys;
5299 SmallVector<MVT, 4> RetTys;
5300 ComputeValueVTs(*this, RetTy, RetTys);
5301
5302 // Then we translate that to a list of legal types.
5303 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5304 MVT VT = RetTys[I];
5305 MVT RegisterVT = getRegisterType(VT);
5306 unsigned NumRegs = getNumRegisters(VT);
5307 for (unsigned i = 0; i != NumRegs; ++i)
5308 LoweredRetTys.push_back(RegisterVT);
5309 }
5310
5311 LoweredRetTys.push_back(MVT::Other); // Always has a chain.
5312
5313 // Create the CALL node.
Dale Johannesen86098bd2008-09-26 19:31:26 +00005314 SDValue Res = DAG.getCall(CallingConv, isVarArg, isTailCall, isInreg,
Dan Gohman095cc292008-09-13 01:54:27 +00005315 DAG.getVTList(&LoweredRetTys[0],
5316 LoweredRetTys.size()),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005317 &Ops[0], Ops.size()
5318 );
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005319 Chain = Res.getValue(LoweredRetTys.size() - 1);
5320
5321 // Gather up the call result into a single value.
Dan Gohmanb5cc34d2008-10-07 00:12:37 +00005322 if (RetTy != Type::VoidTy && !RetTys.empty()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005323 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5324
5325 if (RetSExt)
5326 AssertOp = ISD::AssertSext;
5327 else if (RetZExt)
5328 AssertOp = ISD::AssertZext;
5329
5330 SmallVector<SDValue, 4> ReturnValues;
5331 unsigned RegNo = 0;
5332 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5333 MVT VT = RetTys[I];
5334 MVT RegisterVT = getRegisterType(VT);
5335 unsigned NumRegs = getNumRegisters(VT);
5336 unsigned RegNoEnd = NumRegs + RegNo;
5337 SmallVector<SDValue, 4> Results;
5338 for (; RegNo != RegNoEnd; ++RegNo)
5339 Results.push_back(Res.getValue(RegNo));
5340 SDValue ReturnValue =
5341 getCopyFromParts(DAG, &Results[0], NumRegs, RegisterVT, VT,
5342 AssertOp);
5343 ReturnValues.push_back(ReturnValue);
5344 }
5345 Res = DAG.getMergeValues(DAG.getVTList(&RetTys[0], RetTys.size()),
5346 &ReturnValues[0], ReturnValues.size());
5347 }
5348
5349 return std::make_pair(Res, Chain);
5350}
5351
5352SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
5353 assert(0 && "LowerOperation not implemented for this target!");
5354 abort();
5355 return SDValue();
5356}
5357
5358
5359void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
5360 SDValue Op = getValue(V);
5361 assert((Op.getOpcode() != ISD::CopyFromReg ||
5362 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5363 "Copy from a reg to the same reg!");
5364 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5365
5366 RegsForValue RFV(TLI, Reg, V->getType());
5367 SDValue Chain = DAG.getEntryNode();
5368 RFV.getCopyToRegs(Op, DAG, Chain, 0);
5369 PendingExports.push_back(Chain);
5370}
5371
5372#include "llvm/CodeGen/SelectionDAGISel.h"
5373
5374void SelectionDAGISel::
5375LowerArguments(BasicBlock *LLVMBB) {
5376 // If this is the entry block, emit arguments.
5377 Function &F = *LLVMBB->getParent();
5378 SDValue OldRoot = SDL->DAG.getRoot();
5379 SmallVector<SDValue, 16> Args;
5380 TLI.LowerArguments(F, SDL->DAG, Args);
5381
5382 unsigned a = 0;
5383 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
5384 AI != E; ++AI) {
5385 SmallVector<MVT, 4> ValueVTs;
5386 ComputeValueVTs(TLI, AI->getType(), ValueVTs);
5387 unsigned NumValues = ValueVTs.size();
5388 if (!AI->use_empty()) {
5389 SDL->setValue(AI, SDL->DAG.getMergeValues(&Args[a], NumValues));
5390 // If this argument is live outside of the entry block, insert a copy from
5391 // whereever we got it to the vreg that other BB's will reference it as.
5392 DenseMap<const Value*, unsigned>::iterator VMI=FuncInfo->ValueMap.find(AI);
5393 if (VMI != FuncInfo->ValueMap.end()) {
5394 SDL->CopyValueToVirtualRegister(AI, VMI->second);
5395 }
5396 }
5397 a += NumValues;
5398 }
5399
5400 // Finally, if the target has anything special to do, allow it to do so.
5401 // FIXME: this should insert code into the DAG!
5402 EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction());
5403}
5404
5405/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5406/// ensure constants are generated when needed. Remember the virtual registers
5407/// that need to be added to the Machine PHI nodes as input. We cannot just
5408/// directly add them, because expansion might result in multiple MBB's for one
5409/// BB. As such, the start of the BB might correspond to a different MBB than
5410/// the end.
5411///
5412void
5413SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5414 TerminatorInst *TI = LLVMBB->getTerminator();
5415
5416 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5417
5418 // Check successor nodes' PHI nodes that expect a constant to be available
5419 // from this block.
5420 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5421 BasicBlock *SuccBB = TI->getSuccessor(succ);
5422 if (!isa<PHINode>(SuccBB->begin())) continue;
5423 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
5424
5425 // If this terminator has multiple identical successors (common for
5426 // switches), only handle each succ once.
5427 if (!SuccsHandled.insert(SuccMBB)) continue;
5428
5429 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5430 PHINode *PN;
5431
5432 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5433 // nodes and Machine PHI nodes, but the incoming operands have not been
5434 // emitted yet.
5435 for (BasicBlock::iterator I = SuccBB->begin();
5436 (PN = dyn_cast<PHINode>(I)); ++I) {
5437 // Ignore dead phi's.
5438 if (PN->use_empty()) continue;
5439
5440 unsigned Reg;
5441 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5442
5443 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
5444 unsigned &RegOut = SDL->ConstantsOut[C];
5445 if (RegOut == 0) {
5446 RegOut = FuncInfo->CreateRegForValue(C);
5447 SDL->CopyValueToVirtualRegister(C, RegOut);
5448 }
5449 Reg = RegOut;
5450 } else {
5451 Reg = FuncInfo->ValueMap[PHIOp];
5452 if (Reg == 0) {
5453 assert(isa<AllocaInst>(PHIOp) &&
5454 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5455 "Didn't codegen value into a register!??");
5456 Reg = FuncInfo->CreateRegForValue(PHIOp);
5457 SDL->CopyValueToVirtualRegister(PHIOp, Reg);
5458 }
5459 }
5460
5461 // Remember that this register needs to added to the machine PHI node as
5462 // the input for this MBB.
5463 SmallVector<MVT, 4> ValueVTs;
5464 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5465 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
5466 MVT VT = ValueVTs[vti];
5467 unsigned NumRegisters = TLI.getNumRegisters(VT);
5468 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
5469 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
5470 Reg += NumRegisters;
5471 }
5472 }
5473 }
5474 SDL->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005475}
5476
Dan Gohman3df24e62008-09-03 23:12:08 +00005477/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5478/// supports legal types, and it emits MachineInstrs directly instead of
5479/// creating SelectionDAG nodes.
5480///
5481bool
5482SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5483 FastISel *F) {
5484 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005485
Dan Gohman3df24e62008-09-03 23:12:08 +00005486 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5487 unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size();
5488
5489 // Check successor nodes' PHI nodes that expect a constant to be available
5490 // from this block.
5491 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5492 BasicBlock *SuccBB = TI->getSuccessor(succ);
5493 if (!isa<PHINode>(SuccBB->begin())) continue;
5494 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
5495
5496 // If this terminator has multiple identical successors (common for
5497 // switches), only handle each succ once.
5498 if (!SuccsHandled.insert(SuccMBB)) continue;
5499
5500 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5501 PHINode *PN;
5502
5503 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5504 // nodes and Machine PHI nodes, but the incoming operands have not been
5505 // emitted yet.
5506 for (BasicBlock::iterator I = SuccBB->begin();
5507 (PN = dyn_cast<PHINode>(I)); ++I) {
5508 // Ignore dead phi's.
5509 if (PN->use_empty()) continue;
5510
5511 // Only handle legal types. Two interesting things to note here. First,
5512 // by bailing out early, we may leave behind some dead instructions,
5513 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
5514 // own moves. Second, this check is necessary becuase FastISel doesn't
5515 // use CreateRegForValue to create registers, so it always creates
5516 // exactly one register for each non-void instruction.
5517 MVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
5518 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Dan Gohman74321ab2008-09-10 21:01:31 +00005519 // Promote MVT::i1.
5520 if (VT == MVT::i1)
5521 VT = TLI.getTypeToTransformTo(VT);
5522 else {
5523 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5524 return false;
5525 }
Dan Gohman3df24e62008-09-03 23:12:08 +00005526 }
5527
5528 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5529
5530 unsigned Reg = F->getRegForValue(PHIOp);
5531 if (Reg == 0) {
5532 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5533 return false;
5534 }
5535 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
5536 }
5537 }
5538
5539 return true;
5540}