blob: 024cb4b92f4e20062d342dd724e0e6caa84c1c2d [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"
Bill Wendlingb2a42982008-11-06 02:29:10 +000028#include "llvm/Module.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000029#include "llvm/CodeGen/FastISel.h"
30#include "llvm/CodeGen/GCStrategy.h"
31#include "llvm/CodeGen/GCMetadata.h"
32#include "llvm/CodeGen/MachineFunction.h"
33#include "llvm/CodeGen/MachineFrameInfo.h"
34#include "llvm/CodeGen/MachineInstrBuilder.h"
35#include "llvm/CodeGen/MachineJumpTableInfo.h"
36#include "llvm/CodeGen/MachineModuleInfo.h"
37#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000038#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000039#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000040#include "llvm/CodeGen/DwarfWriter.h"
41#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000042#include "llvm/Target/TargetRegisterInfo.h"
43#include "llvm/Target/TargetData.h"
44#include "llvm/Target/TargetFrameInfo.h"
45#include "llvm/Target/TargetInstrInfo.h"
46#include "llvm/Target/TargetLowering.h"
47#include "llvm/Target/TargetMachine.h"
48#include "llvm/Target/TargetOptions.h"
49#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000050#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051#include "llvm/Support/Debug.h"
52#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000053#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000054#include <algorithm>
55using namespace llvm;
56
Dale Johannesen601d3c02008-09-05 01:48:15 +000057/// LimitFloatPrecision - Generate low-precision inline sequences for
58/// some float libcalls (6, 8 or 12 bits).
59static unsigned LimitFloatPrecision;
60
61static cl::opt<unsigned, true>
62LimitFPPrecision("limit-float-precision",
63 cl::desc("Generate low-precision inline sequences "
64 "for some float libcalls"),
65 cl::location(LimitFloatPrecision),
66 cl::init(0));
67
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000068/// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
Dan Gohman2c91d102009-01-06 22:53:52 +000069/// of insertvalue or extractvalue indices that identify a member, return
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000070/// the linearized index of the start of the member.
71///
72static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty,
73 const unsigned *Indices,
74 const unsigned *IndicesEnd,
75 unsigned CurIndex = 0) {
76 // Base case: We're done.
77 if (Indices && Indices == IndicesEnd)
78 return CurIndex;
79
80 // Given a struct type, recursively traverse the elements.
81 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
82 for (StructType::element_iterator EB = STy->element_begin(),
83 EI = EB,
84 EE = STy->element_end();
85 EI != EE; ++EI) {
86 if (Indices && *Indices == unsigned(EI - EB))
87 return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex);
88 CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex);
89 }
Dan Gohman2c91d102009-01-06 22:53:52 +000090 return CurIndex;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000091 }
92 // Given an array type, recursively traverse the elements.
93 else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
94 const Type *EltTy = ATy->getElementType();
95 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
96 if (Indices && *Indices == i)
97 return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex);
98 CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex);
99 }
Dan Gohman2c91d102009-01-06 22:53:52 +0000100 return CurIndex;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000101 }
102 // We haven't found the type we're looking for, so keep searching.
103 return CurIndex + 1;
104}
105
106/// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
107/// MVTs that represent all the individual underlying
108/// non-aggregate types that comprise it.
109///
110/// If Offsets is non-null, it points to a vector to be filled in
111/// with the in-memory offsets of each of the individual values.
112///
113static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty,
114 SmallVectorImpl<MVT> &ValueVTs,
115 SmallVectorImpl<uint64_t> *Offsets = 0,
116 uint64_t StartingOffset = 0) {
117 // Given a struct type, recursively traverse the elements.
118 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
119 const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy);
120 for (StructType::element_iterator EB = STy->element_begin(),
121 EI = EB,
122 EE = STy->element_end();
123 EI != EE; ++EI)
124 ComputeValueVTs(TLI, *EI, ValueVTs, Offsets,
125 StartingOffset + SL->getElementOffset(EI - EB));
126 return;
127 }
128 // Given an array type, recursively traverse the elements.
129 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
130 const Type *EltTy = ATy->getElementType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000131 uint64_t EltSize = TLI.getTargetData()->getTypePaddedSize(EltTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000132 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
133 ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets,
134 StartingOffset + i * EltSize);
135 return;
136 }
137 // Base case: we can get an MVT for this LLVM IR type.
138 ValueVTs.push_back(TLI.getValueType(Ty));
139 if (Offsets)
140 Offsets->push_back(StartingOffset);
141}
142
Dan Gohman2a7c6712008-09-03 23:18:39 +0000143namespace llvm {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000144 /// RegsForValue - This struct represents the registers (physical or virtual)
145 /// that a particular set of values is assigned, and the type information about
146 /// the value. The most common situation is to represent one value at a time,
147 /// but struct or array values are handled element-wise as multiple values.
148 /// The splitting of aggregates is performed recursively, so that we never
149 /// have aggregate-typed registers. The values at this point do not necessarily
150 /// have legal types, so each value may require one or more registers of some
151 /// legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000152 ///
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000153 struct VISIBILITY_HIDDEN RegsForValue {
154 /// TLI - The TargetLowering object.
155 ///
156 const TargetLowering *TLI;
157
158 /// ValueVTs - The value types of the values, which may not be legal, and
159 /// may need be promoted or synthesized from one or more registers.
160 ///
161 SmallVector<MVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000162
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000163 /// RegVTs - The value types of the registers. This is the same size as
164 /// ValueVTs and it records, for each value, what the type of the assigned
165 /// register or registers are. (Individual values are never synthesized
166 /// from more than one type of register.)
167 ///
168 /// With virtual registers, the contents of RegVTs is redundant with TLI's
169 /// getRegisterType member function, however when with physical registers
170 /// it is necessary to have a separate record of the types.
171 ///
172 SmallVector<MVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000174 /// Regs - This list holds the registers assigned to the values.
175 /// Each legal or promoted value requires one register, and each
176 /// expanded value requires multiple registers.
177 ///
178 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000180 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000181
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000182 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000183 const SmallVector<unsigned, 4> &regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000184 MVT regvt, MVT valuevt)
185 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
186 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000187 const SmallVector<unsigned, 4> &regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000188 const SmallVector<MVT, 4> &regvts,
189 const SmallVector<MVT, 4> &valuevts)
190 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
191 RegsForValue(const TargetLowering &tli,
192 unsigned Reg, const Type *Ty) : TLI(&tli) {
193 ComputeValueVTs(tli, Ty, ValueVTs);
194
195 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
196 MVT ValueVT = ValueVTs[Value];
197 unsigned NumRegs = TLI->getNumRegisters(ValueVT);
198 MVT RegisterVT = TLI->getRegisterType(ValueVT);
199 for (unsigned i = 0; i != NumRegs; ++i)
200 Regs.push_back(Reg + i);
201 RegVTs.push_back(RegisterVT);
202 Reg += NumRegs;
203 }
204 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000205
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000206 /// append - Add the specified values to this one.
207 void append(const RegsForValue &RHS) {
208 TLI = RHS.TLI;
209 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
210 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
211 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
212 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000213
214
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000215 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000216 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000217 /// Chain/Flag as the input and updates them for the output Chain/Flag.
218 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000219 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000220 SDValue &Chain, SDValue *Flag) const;
221
222 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000223 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000224 /// Chain/Flag as the input and updates them for the output Chain/Flag.
225 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000226 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000227 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000228
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000229 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000230 /// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000231 /// values added into it.
232 void AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
233 std::vector<SDValue> &Ops) const;
234 };
235}
236
237/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000238/// PHI nodes or outside of the basic block that defines it, or used by a
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000239/// switch or atomic instruction, which may expand to multiple basic blocks.
240static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
241 if (isa<PHINode>(I)) return true;
242 BasicBlock *BB = I->getParent();
243 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
244 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI) ||
245 // FIXME: Remove switchinst special case.
246 isa<SwitchInst>(*UI))
247 return true;
248 return false;
249}
250
251/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
252/// entry block, return true. This includes arguments used by switches, since
253/// the switch may expand into multiple basic blocks.
254static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) {
255 // With FastISel active, we may be splitting blocks, so force creation
256 // of virtual registers for all non-dead arguments.
Dan Gohman33134c42008-09-25 17:05:24 +0000257 // Don't force virtual registers for byval arguments though, because
258 // fast-isel can't handle those in all cases.
259 if (EnableFastISel && !A->hasByValAttr())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000260 return A->use_empty();
261
262 BasicBlock *Entry = A->getParent()->begin();
263 for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
264 if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
265 return false; // Use not in entry block.
266 return true;
267}
268
269FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli)
270 : TLI(tli) {
271}
272
273void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf,
274 bool EnableFastISel) {
275 Fn = &fn;
276 MF = &mf;
277 RegInfo = &MF->getRegInfo();
278
279 // Create a vreg for each argument register that is not dead and is used
280 // outside of the entry block for the function.
281 for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
282 AI != E; ++AI)
283 if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
284 InitializeRegForValue(AI);
285
286 // Initialize the mapping of values to registers. This is only set up for
287 // instruction values that are used outside of the block that defines
288 // them.
289 Function::iterator BB = Fn->begin(), EB = Fn->end();
290 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
291 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
292 if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
293 const Type *Ty = AI->getAllocatedType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +0000294 uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000295 unsigned Align =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000296 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
297 AI->getAlignment());
298
299 TySize *= CUI->getZExtValue(); // Get total allocated size.
300 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
301 StaticAllocaMap[AI] =
302 MF->getFrameInfo()->CreateStackObject(TySize, Align);
303 }
304
305 for (; BB != EB; ++BB)
306 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
307 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
308 if (!isa<AllocaInst>(I) ||
309 !StaticAllocaMap.count(cast<AllocaInst>(I)))
310 InitializeRegForValue(I);
311
312 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
313 // also creates the initial PHI MachineInstrs, though none of the input
314 // operands are populated.
315 for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) {
316 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
317 MBBMap[BB] = MBB;
318 MF->push_back(MBB);
319
320 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
321 // appropriate.
322 PHINode *PN;
323 for (BasicBlock::iterator I = BB->begin();(PN = dyn_cast<PHINode>(I)); ++I){
324 if (PN->use_empty()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000326 unsigned PHIReg = ValueMap[PN];
327 assert(PHIReg && "PHI node does not have an assigned virtual register!");
328
329 SmallVector<MVT, 4> ValueVTs;
330 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
331 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
332 MVT VT = ValueVTs[vti];
333 unsigned NumRegisters = TLI.getNumRegisters(VT);
Dan Gohman6448d912008-09-04 15:39:15 +0000334 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335 for (unsigned i = 0; i != NumRegisters; ++i)
336 BuildMI(MBB, TII->get(TargetInstrInfo::PHI), PHIReg+i);
337 PHIReg += NumRegisters;
338 }
339 }
340 }
341}
342
343unsigned FunctionLoweringInfo::MakeReg(MVT VT) {
344 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
345}
346
347/// CreateRegForValue - Allocate the appropriate number of virtual registers of
348/// the correctly promoted or expanded types. Assign these registers
349/// consecutive vreg numbers and return the first assigned number.
350///
351/// In the case that the given value has struct or array type, this function
352/// will assign registers for each member or element.
353///
354unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
355 SmallVector<MVT, 4> ValueVTs;
356 ComputeValueVTs(TLI, V->getType(), ValueVTs);
357
358 unsigned FirstReg = 0;
359 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
360 MVT ValueVT = ValueVTs[Value];
361 MVT RegisterVT = TLI.getRegisterType(ValueVT);
362
363 unsigned NumRegs = TLI.getNumRegisters(ValueVT);
364 for (unsigned i = 0; i != NumRegs; ++i) {
365 unsigned R = MakeReg(RegisterVT);
366 if (!FirstReg) FirstReg = R;
367 }
368 }
369 return FirstReg;
370}
371
372/// getCopyFromParts - Create a value that contains the specified legal parts
373/// combined into the value they represent. If the parts combine to a type
374/// larger then ValueVT then AssertOp can be used to specify whether the extra
375/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
376/// (ISD::AssertSext).
Dale Johannesen66978ee2009-01-31 02:22:37 +0000377static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
378 const SDValue *Parts,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000379 unsigned NumParts, MVT PartVT, MVT ValueVT,
380 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000381 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000382 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000383 SDValue Val = Parts[0];
384
385 if (NumParts > 1) {
386 // Assemble the value from multiple parts.
387 if (!ValueVT.isVector()) {
388 unsigned PartBits = PartVT.getSizeInBits();
389 unsigned ValueBits = ValueVT.getSizeInBits();
390
391 // Assemble the power of 2 part.
392 unsigned RoundParts = NumParts & (NumParts - 1) ?
393 1 << Log2_32(NumParts) : NumParts;
394 unsigned RoundBits = PartBits * RoundParts;
395 MVT RoundVT = RoundBits == ValueBits ?
396 ValueVT : MVT::getIntegerVT(RoundBits);
397 SDValue Lo, Hi;
398
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000399 MVT HalfVT = ValueVT.isInteger() ?
400 MVT::getIntegerVT(RoundBits/2) :
401 MVT::getFloatingPointVT(RoundBits/2);
402
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000403 if (RoundParts > 2) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000404 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT);
405 Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000406 PartVT, HalfVT);
407 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000408 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
409 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000410 }
411 if (TLI.isBigEndian())
412 std::swap(Lo, Hi);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000413 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000414
415 if (RoundParts < NumParts) {
416 // Assemble the trailing non-power-of-2 part.
417 unsigned OddParts = NumParts - RoundParts;
418 MVT OddVT = MVT::getIntegerVT(OddParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000419 Hi = getCopyFromParts(DAG, dl,
420 Parts+RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000421
422 // Combine the round and odd parts.
423 Lo = Val;
424 if (TLI.isBigEndian())
425 std::swap(Lo, Hi);
426 MVT TotalVT = MVT::getIntegerVT(NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000427 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
428 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000429 DAG.getConstant(Lo.getValueType().getSizeInBits(),
430 TLI.getShiftAmountTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000431 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
432 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000433 }
434 } else {
435 // Handle a multi-element vector.
436 MVT IntermediateVT, RegisterVT;
437 unsigned NumIntermediates;
438 unsigned NumRegs =
439 TLI.getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates,
440 RegisterVT);
441 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
442 NumParts = NumRegs; // Silence a compiler warning.
443 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
444 assert(RegisterVT == Parts[0].getValueType() &&
445 "Part type doesn't match part!");
446
447 // Assemble the parts into intermediate operands.
448 SmallVector<SDValue, 8> Ops(NumIntermediates);
449 if (NumIntermediates == NumParts) {
450 // If the register was not expanded, truncate or copy the value,
451 // as appropriate.
452 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000453 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000454 PartVT, IntermediateVT);
455 } else if (NumParts > 0) {
456 // If the intermediate type was expanded, build the intermediate operands
457 // from the parts.
458 assert(NumParts % NumIntermediates == 0 &&
459 "Must expand into a divisible number of parts!");
460 unsigned Factor = NumParts / NumIntermediates;
461 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000462 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000463 PartVT, IntermediateVT);
464 }
465
466 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
467 // operands.
468 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000469 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000470 ValueVT, &Ops[0], NumIntermediates);
471 }
472 }
473
474 // There is now one part, held in Val. Correct it to match ValueVT.
475 PartVT = Val.getValueType();
476
477 if (PartVT == ValueVT)
478 return Val;
479
480 if (PartVT.isVector()) {
481 assert(ValueVT.isVector() && "Unknown vector conversion!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000482 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000483 }
484
485 if (ValueVT.isVector()) {
486 assert(ValueVT.getVectorElementType() == PartVT &&
487 ValueVT.getVectorNumElements() == 1 &&
488 "Only trivial scalar-to-vector conversions should get here!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000489 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000490 }
491
492 if (PartVT.isInteger() &&
493 ValueVT.isInteger()) {
494 if (ValueVT.bitsLT(PartVT)) {
495 // For a truncate, see if we have any information to
496 // indicate whether the truncated bits will always be
497 // zero or sign-extension.
498 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000499 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000500 DAG.getValueType(ValueVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000501 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000502 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000503 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000504 }
505 }
506
507 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
508 if (ValueVT.bitsLT(Val.getValueType()))
509 // FP_ROUND's are always exact here.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000510 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000511 DAG.getIntPtrConstant(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000512 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000513 }
514
515 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000516 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000517
518 assert(0 && "Unknown mismatch!");
519 return SDValue();
520}
521
522/// getCopyToParts - Create a series of nodes that contain the specified value
523/// split into legal parts. If the parts contain more bits than Val, then, for
524/// integers, ExtendKind can be used to specify how to generate the extra bits.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000525static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val,
Chris Lattner01426e12008-10-21 00:45:36 +0000526 SDValue *Parts, unsigned NumParts, MVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000527 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000528 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000529 MVT PtrVT = TLI.getPointerTy();
530 MVT ValueVT = Val.getValueType();
531 unsigned PartBits = PartVT.getSizeInBits();
532 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
533
534 if (!NumParts)
535 return;
536
537 if (!ValueVT.isVector()) {
538 if (PartVT == ValueVT) {
539 assert(NumParts == 1 && "No-op copy with multiple parts!");
540 Parts[0] = Val;
541 return;
542 }
543
544 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
545 // If the parts cover more bits than the value has, promote the value.
546 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
547 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000548 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000549 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
550 ValueVT = MVT::getIntegerVT(NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000551 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000552 } else {
553 assert(0 && "Unknown mismatch!");
554 }
555 } else if (PartBits == ValueVT.getSizeInBits()) {
556 // Different types of the same size.
557 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000558 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000559 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
560 // If the parts cover less bits than value has, truncate the value.
561 if (PartVT.isInteger() && ValueVT.isInteger()) {
562 ValueVT = MVT::getIntegerVT(NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000563 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000564 } else {
565 assert(0 && "Unknown mismatch!");
566 }
567 }
568
569 // The value may have changed - recompute ValueVT.
570 ValueVT = Val.getValueType();
571 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
572 "Failed to tile the value with PartVT!");
573
574 if (NumParts == 1) {
575 assert(PartVT == ValueVT && "Type conversion failed!");
576 Parts[0] = Val;
577 return;
578 }
579
580 // Expand the value into multiple parts.
581 if (NumParts & (NumParts - 1)) {
582 // The number of parts is not a power of 2. Split off and copy the tail.
583 assert(PartVT.isInteger() && ValueVT.isInteger() &&
584 "Do not know what to expand to!");
585 unsigned RoundParts = 1 << Log2_32(NumParts);
586 unsigned RoundBits = RoundParts * PartBits;
587 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000588 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000589 DAG.getConstant(RoundBits,
590 TLI.getShiftAmountTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000591 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000592 if (TLI.isBigEndian())
593 // The odd parts were reversed by getCopyToParts - unreverse them.
594 std::reverse(Parts + RoundParts, Parts + NumParts);
595 NumParts = RoundParts;
596 ValueVT = MVT::getIntegerVT(NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000597 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000598 }
599
600 // The number of parts is a power of 2. Repeatedly bisect the value using
601 // EXTRACT_ELEMENT.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000602 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000603 MVT::getIntegerVT(ValueVT.getSizeInBits()),
604 Val);
605 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
606 for (unsigned i = 0; i < NumParts; i += StepSize) {
607 unsigned ThisBits = StepSize * PartBits / 2;
608 MVT ThisVT = MVT::getIntegerVT (ThisBits);
609 SDValue &Part0 = Parts[i];
610 SDValue &Part1 = Parts[i+StepSize/2];
611
Dale Johannesen66978ee2009-01-31 02:22:37 +0000612 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000613 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000614 DAG.getConstant(1, PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000615 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000616 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000617 DAG.getConstant(0, PtrVT));
618
619 if (ThisBits == PartBits && ThisVT != PartVT) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000620 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000621 PartVT, Part0);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000622 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000623 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000624 }
625 }
626 }
627
628 if (TLI.isBigEndian())
629 std::reverse(Parts, Parts + NumParts);
630
631 return;
632 }
633
634 // Vector ValueVT.
635 if (NumParts == 1) {
636 if (PartVT != ValueVT) {
637 if (PartVT.isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000638 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000639 } else {
640 assert(ValueVT.getVectorElementType() == PartVT &&
641 ValueVT.getVectorNumElements() == 1 &&
642 "Only trivial vector-to-scalar conversions should get here!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000643 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000644 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000645 DAG.getConstant(0, PtrVT));
646 }
647 }
648
649 Parts[0] = Val;
650 return;
651 }
652
653 // Handle a multi-element vector.
654 MVT IntermediateVT, RegisterVT;
655 unsigned NumIntermediates;
Dan Gohmane9530ec2009-01-15 16:58:17 +0000656 unsigned NumRegs = TLI
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000657 .getVectorTypeBreakdown(ValueVT, IntermediateVT, NumIntermediates,
658 RegisterVT);
659 unsigned NumElements = ValueVT.getVectorNumElements();
660
661 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
662 NumParts = NumRegs; // Silence a compiler warning.
663 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
664
665 // Split the vector into intermediate operands.
666 SmallVector<SDValue, 8> Ops(NumIntermediates);
667 for (unsigned i = 0; i != NumIntermediates; ++i)
668 if (IntermediateVT.isVector())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000669 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000670 IntermediateVT, Val,
671 DAG.getConstant(i * (NumElements / NumIntermediates),
672 PtrVT));
673 else
Dale Johannesen66978ee2009-01-31 02:22:37 +0000674 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000675 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000676 DAG.getConstant(i, PtrVT));
677
678 // Split the intermediate operands into legal parts.
679 if (NumParts == NumIntermediates) {
680 // If the register was not expanded, promote or copy the value,
681 // as appropriate.
682 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000683 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000684 } else if (NumParts > 0) {
685 // If the intermediate type was expanded, split each the value into
686 // legal parts.
687 assert(NumParts % NumIntermediates == 0 &&
688 "Must expand into a divisible number of parts!");
689 unsigned Factor = NumParts / NumIntermediates;
690 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000691 getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000692 }
693}
694
695
696void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
697 AA = &aa;
698 GFI = gfi;
699 TD = DAG.getTarget().getTargetData();
700}
701
702/// clear - Clear out the curret SelectionDAG and the associated
703/// state and prepare this SelectionDAGLowering object to be used
704/// for a new block. This doesn't clear out information about
705/// additional blocks that are needed to complete switch lowering
706/// or PHI node updating; that information is cleared out as it is
707/// consumed.
708void SelectionDAGLowering::clear() {
709 NodeMap.clear();
710 PendingLoads.clear();
711 PendingExports.clear();
712 DAG.clear();
713}
714
715/// getRoot - Return the current virtual root of the Selection DAG,
716/// flushing any PendingLoad items. This must be done before emitting
717/// a store or any other node that may need to be ordered after any
718/// prior load instructions.
719///
720SDValue SelectionDAGLowering::getRoot() {
721 if (PendingLoads.empty())
722 return DAG.getRoot();
723
724 if (PendingLoads.size() == 1) {
725 SDValue Root = PendingLoads[0];
726 DAG.setRoot(Root);
727 PendingLoads.clear();
728 return Root;
729 }
730
731 // Otherwise, we have to make a token factor node.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000732 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000733 &PendingLoads[0], PendingLoads.size());
734 PendingLoads.clear();
735 DAG.setRoot(Root);
736 return Root;
737}
738
739/// getControlRoot - Similar to getRoot, but instead of flushing all the
740/// PendingLoad items, flush all the PendingExports items. It is necessary
741/// to do this before emitting a terminator instruction.
742///
743SDValue SelectionDAGLowering::getControlRoot() {
744 SDValue Root = DAG.getRoot();
745
746 if (PendingExports.empty())
747 return Root;
748
749 // Turn all of the CopyToReg chains into one factored node.
750 if (Root.getOpcode() != ISD::EntryToken) {
751 unsigned i = 0, e = PendingExports.size();
752 for (; i != e; ++i) {
753 assert(PendingExports[i].getNode()->getNumOperands() > 1);
754 if (PendingExports[i].getNode()->getOperand(0) == Root)
755 break; // Don't add the root if we already indirectly depend on it.
756 }
757
758 if (i == e)
759 PendingExports.push_back(Root);
760 }
761
Dale Johannesen66978ee2009-01-31 02:22:37 +0000762 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000763 &PendingExports[0],
764 PendingExports.size());
765 PendingExports.clear();
766 DAG.setRoot(Root);
767 return Root;
768}
769
770void SelectionDAGLowering::visit(Instruction &I) {
771 visit(I.getOpcode(), I);
772}
773
774void SelectionDAGLowering::visit(unsigned Opcode, User &I) {
775 // Note: this doesn't use InstVisitor, because it has to work with
776 // ConstantExpr's in addition to instructions.
777 switch (Opcode) {
778 default: assert(0 && "Unknown instruction type encountered!");
779 abort();
780 // Build the switch statement using the Instruction.def file.
781#define HANDLE_INST(NUM, OPCODE, CLASS) \
782 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
783#include "llvm/Instruction.def"
784 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000785}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000786
787void SelectionDAGLowering::visitAdd(User &I) {
788 if (I.getType()->isFPOrFPVector())
789 visitBinary(I, ISD::FADD);
790 else
791 visitBinary(I, ISD::ADD);
792}
793
794void SelectionDAGLowering::visitMul(User &I) {
795 if (I.getType()->isFPOrFPVector())
796 visitBinary(I, ISD::FMUL);
797 else
798 visitBinary(I, ISD::MUL);
799}
800
801SDValue SelectionDAGLowering::getValue(const Value *V) {
802 SDValue &N = NodeMap[V];
803 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000804
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000805 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
806 MVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000808 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000809 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000810
811 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
812 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000814 if (isa<ConstantPointerNull>(C))
815 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000816
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000817 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000818 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000820 if (isa<UndefValue>(C) && !isa<VectorType>(V->getType()) &&
821 !V->getType()->isAggregateType())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000822 return N = DAG.getNode(ISD::UNDEF, getCurDebugLoc(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000823
824 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
825 visit(CE->getOpcode(), *CE);
826 SDValue N1 = NodeMap[V];
827 assert(N1.getNode() && "visit didn't populate the ValueMap!");
828 return N1;
829 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000830
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000831 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
832 SmallVector<SDValue, 4> Constants;
833 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
834 OI != OE; ++OI) {
835 SDNode *Val = getValue(*OI).getNode();
836 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
837 Constants.push_back(SDValue(Val, i));
838 }
839 return DAG.getMergeValues(&Constants[0], Constants.size());
840 }
841
842 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
843 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
844 "Unknown struct or array constant!");
845
846 SmallVector<MVT, 4> ValueVTs;
847 ComputeValueVTs(TLI, C->getType(), ValueVTs);
848 unsigned NumElts = ValueVTs.size();
849 if (NumElts == 0)
850 return SDValue(); // empty struct
851 SmallVector<SDValue, 4> Constants(NumElts);
852 for (unsigned i = 0; i != NumElts; ++i) {
853 MVT EltVT = ValueVTs[i];
854 if (isa<UndefValue>(C))
Dale Johannesen66978ee2009-01-31 02:22:37 +0000855 Constants[i] = DAG.getNode(ISD::UNDEF, getCurDebugLoc(), EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000856 else if (EltVT.isFloatingPoint())
857 Constants[i] = DAG.getConstantFP(0, EltVT);
858 else
859 Constants[i] = DAG.getConstant(0, EltVT);
860 }
861 return DAG.getMergeValues(&Constants[0], NumElts);
862 }
863
864 const VectorType *VecTy = cast<VectorType>(V->getType());
865 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000866
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000867 // Now that we know the number and type of the elements, get that number of
868 // elements into the Ops array based on what kind of constant it is.
869 SmallVector<SDValue, 16> Ops;
870 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
871 for (unsigned i = 0; i != NumElements; ++i)
872 Ops.push_back(getValue(CP->getOperand(i)));
873 } else {
874 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
875 "Unknown vector constant!");
876 MVT EltVT = TLI.getValueType(VecTy->getElementType());
877
878 SDValue Op;
879 if (isa<UndefValue>(C))
Dale Johannesen66978ee2009-01-31 02:22:37 +0000880 Op = DAG.getNode(ISD::UNDEF, getCurDebugLoc(), EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000881 else if (EltVT.isFloatingPoint())
882 Op = DAG.getConstantFP(0, EltVT);
883 else
884 Op = DAG.getConstant(0, EltVT);
885 Ops.assign(NumElements, Op);
886 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000887
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000888 // Create a BUILD_VECTOR node.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000889 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000890 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000891 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000892
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000893 // If this is a static alloca, generate it as the frameindex instead of
894 // computation.
895 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
896 DenseMap<const AllocaInst*, int>::iterator SI =
897 FuncInfo.StaticAllocaMap.find(AI);
898 if (SI != FuncInfo.StaticAllocaMap.end())
899 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
900 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000901
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000902 unsigned InReg = FuncInfo.ValueMap[V];
903 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000905 RegsForValue RFV(TLI, InReg, V->getType());
906 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +0000907 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000908}
909
910
911void SelectionDAGLowering::visitRet(ReturnInst &I) {
912 if (I.getNumOperands() == 0) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000913 DAG.setRoot(DAG.getNode(ISD::RET, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000914 MVT::Other, getControlRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915 return;
916 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000917
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000918 SmallVector<SDValue, 8> NewValues;
919 NewValues.push_back(getControlRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000920 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000921 SmallVector<MVT, 4> ValueVTs;
922 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000923 unsigned NumValues = ValueVTs.size();
924 if (NumValues == 0) continue;
925
926 SDValue RetOp = getValue(I.getOperand(i));
927 for (unsigned j = 0, f = NumValues; j != f; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000928 MVT VT = ValueVTs[j];
929
930 // FIXME: C calling convention requires the return type to be promoted to
Dale Johannesenc9c6da62008-09-25 20:47:45 +0000931 // at least 32-bit. But this is not necessary for non-C calling
932 // conventions.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000933 if (VT.isInteger()) {
934 MVT MinVT = TLI.getRegisterType(MVT::i32);
935 if (VT.bitsLT(MinVT))
936 VT = MinVT;
937 }
938
939 unsigned NumParts = TLI.getNumRegisters(VT);
940 MVT PartVT = TLI.getRegisterType(VT);
941 SmallVector<SDValue, 4> Parts(NumParts);
942 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000943
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000944 const Function *F = I.getParent()->getParent();
Devang Patel05988662008-09-25 21:00:45 +0000945 if (F->paramHasAttr(0, Attribute::SExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000946 ExtendKind = ISD::SIGN_EXTEND;
Devang Patel05988662008-09-25 21:00:45 +0000947 else if (F->paramHasAttr(0, Attribute::ZExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000948 ExtendKind = ISD::ZERO_EXTEND;
949
Dale Johannesen66978ee2009-01-31 02:22:37 +0000950 getCopyToParts(DAG, getCurDebugLoc(),
951 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000952 &Parts[0], NumParts, PartVT, ExtendKind);
953
Dale Johannesenc9c6da62008-09-25 20:47:45 +0000954 // 'inreg' on function refers to return value
955 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Devang Patel05988662008-09-25 21:00:45 +0000956 if (F->paramHasAttr(0, Attribute::InReg))
Dale Johannesenc9c6da62008-09-25 20:47:45 +0000957 Flags.setInReg();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000958 for (unsigned i = 0; i < NumParts; ++i) {
959 NewValues.push_back(Parts[i]);
Dale Johannesenc9c6da62008-09-25 20:47:45 +0000960 NewValues.push_back(DAG.getArgFlags(Flags));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000961 }
962 }
963 }
Dale Johannesen66978ee2009-01-31 02:22:37 +0000964 DAG.setRoot(DAG.getNode(ISD::RET, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000965 &NewValues[0], NewValues.size()));
966}
967
968/// ExportFromCurrentBlock - If this condition isn't known to be exported from
969/// the current basic block, add it to ValueMap now so that we'll get a
970/// CopyTo/FromReg.
971void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
972 // No need to export constants.
973 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000974
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000975 // Already exported?
976 if (FuncInfo.isExportedInst(V)) return;
977
978 unsigned Reg = FuncInfo.InitializeRegForValue(V);
979 CopyValueToVirtualRegister(V, Reg);
980}
981
982bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
983 const BasicBlock *FromBB) {
984 // The operands of the setcc have to be in this block. We don't know
985 // how to export them from some other block.
986 if (Instruction *VI = dyn_cast<Instruction>(V)) {
987 // Can export from current BB.
988 if (VI->getParent() == FromBB)
989 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000990
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000991 // Is already exported, noop.
992 return FuncInfo.isExportedInst(V);
993 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000994
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000995 // If this is an argument, we can export it if the BB is the entry block or
996 // if it is already exported.
997 if (isa<Argument>(V)) {
998 if (FromBB == &FromBB->getParent()->getEntryBlock())
999 return true;
1000
1001 // Otherwise, can only export this if it is already exported.
1002 return FuncInfo.isExportedInst(V);
1003 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001005 // Otherwise, constants can always be exported.
1006 return true;
1007}
1008
1009static bool InBlock(const Value *V, const BasicBlock *BB) {
1010 if (const Instruction *I = dyn_cast<Instruction>(V))
1011 return I->getParent() == BB;
1012 return true;
1013}
1014
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001015/// getFCmpCondCode - Return the ISD condition code corresponding to
1016/// the given LLVM IR floating-point condition code. This includes
1017/// consideration of global floating-point math flags.
1018///
1019static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1020 ISD::CondCode FPC, FOC;
1021 switch (Pred) {
1022 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1023 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1024 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1025 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1026 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1027 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1028 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1029 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1030 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1031 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1032 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1033 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1034 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1035 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1036 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1037 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1038 default:
1039 assert(0 && "Invalid FCmp predicate opcode!");
1040 FOC = FPC = ISD::SETFALSE;
1041 break;
1042 }
1043 if (FiniteOnlyFPMath())
1044 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001045 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001046 return FPC;
1047}
1048
1049/// getICmpCondCode - Return the ISD condition code corresponding to
1050/// the given LLVM IR integer condition code.
1051///
1052static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1053 switch (Pred) {
1054 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1055 case ICmpInst::ICMP_NE: return ISD::SETNE;
1056 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1057 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1058 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1059 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1060 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1061 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1062 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1063 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1064 default:
1065 assert(0 && "Invalid ICmp predicate opcode!");
1066 return ISD::SETNE;
1067 }
1068}
1069
Dan Gohmanc2277342008-10-17 21:16:08 +00001070/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1071/// This function emits a branch and is used at the leaves of an OR or an
1072/// AND operator tree.
1073///
1074void
1075SelectionDAGLowering::EmitBranchForMergedCondition(Value *Cond,
1076 MachineBasicBlock *TBB,
1077 MachineBasicBlock *FBB,
1078 MachineBasicBlock *CurBB) {
1079 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001080
Dan Gohmanc2277342008-10-17 21:16:08 +00001081 // If the leaf of the tree is a comparison, merge the condition into
1082 // the caseblock.
1083 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1084 // The operands of the cmp have to be in this block. We don't know
1085 // how to export them from some other block. If this is the first block
1086 // of the sequence, no exporting is needed.
1087 if (CurBB == CurMBB ||
1088 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1089 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001090 ISD::CondCode Condition;
1091 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001092 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001093 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001094 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001095 } else {
1096 Condition = ISD::SETEQ; // silence warning.
1097 assert(0 && "Unknown compare instruction");
1098 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001099
1100 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001101 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1102 SwitchCases.push_back(CB);
1103 return;
1104 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001105 }
1106
1107 // Create a CaseBlock record representing this branch.
1108 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(),
1109 NULL, TBB, FBB, CurBB);
1110 SwitchCases.push_back(CB);
1111}
1112
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001113/// FindMergedConditions - If Cond is an expression like
Dan Gohmanc2277342008-10-17 21:16:08 +00001114void SelectionDAGLowering::FindMergedConditions(Value *Cond,
1115 MachineBasicBlock *TBB,
1116 MachineBasicBlock *FBB,
1117 MachineBasicBlock *CurBB,
1118 unsigned Opc) {
1119 // If this node is not part of the or/and tree, emit it as a branch.
1120 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001121 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001122 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1123 BOp->getParent() != CurBB->getBasicBlock() ||
1124 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1125 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1126 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001127 return;
1128 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001130 // Create TmpBB after CurBB.
1131 MachineFunction::iterator BBI = CurBB;
1132 MachineFunction &MF = DAG.getMachineFunction();
1133 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1134 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001135
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001136 if (Opc == Instruction::Or) {
1137 // Codegen X | Y as:
1138 // jmp_if_X TBB
1139 // jmp TmpBB
1140 // TmpBB:
1141 // jmp_if_Y TBB
1142 // jmp FBB
1143 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001145 // Emit the LHS condition.
1146 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001147
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001148 // Emit the RHS condition into TmpBB.
1149 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1150 } else {
1151 assert(Opc == Instruction::And && "Unknown merge op!");
1152 // Codegen X & Y as:
1153 // jmp_if_X TmpBB
1154 // jmp FBB
1155 // TmpBB:
1156 // jmp_if_Y TBB
1157 // jmp FBB
1158 //
1159 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001160
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001161 // Emit the LHS condition.
1162 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001163
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001164 // Emit the RHS condition into TmpBB.
1165 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1166 }
1167}
1168
1169/// If the set of cases should be emitted as a series of branches, return true.
1170/// If we should emit this as a bunch of and/or'd together conditions, return
1171/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001172bool
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001173SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
1174 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001175
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001176 // If this is two comparisons of the same values or'd or and'd together, they
1177 // will get folded into a single comparison, so don't emit two blocks.
1178 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1179 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1180 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1181 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1182 return false;
1183 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001185 return true;
1186}
1187
1188void SelectionDAGLowering::visitBr(BranchInst &I) {
1189 // Update machine-CFG edges.
1190 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1191
1192 // Figure out which block is immediately after the current one.
1193 MachineBasicBlock *NextBlock = 0;
1194 MachineFunction::iterator BBI = CurMBB;
1195 if (++BBI != CurMBB->getParent()->end())
1196 NextBlock = BBI;
1197
1198 if (I.isUnconditional()) {
1199 // Update machine-CFG edges.
1200 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001201
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001202 // If this is not a fall-through branch, emit the branch.
1203 if (Succ0MBB != NextBlock)
Dale Johannesen66978ee2009-01-31 02:22:37 +00001204 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001205 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001206 DAG.getBasicBlock(Succ0MBB)));
1207 return;
1208 }
1209
1210 // If this condition is one of the special cases we handle, do special stuff
1211 // now.
1212 Value *CondVal = I.getCondition();
1213 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1214
1215 // If this is a series of conditions that are or'd or and'd together, emit
1216 // this as a sequence of branches instead of setcc's with and/or operations.
1217 // For example, instead of something like:
1218 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001219 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001220 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001221 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001222 // or C, F
1223 // jnz foo
1224 // Emit:
1225 // cmp A, B
1226 // je foo
1227 // cmp D, E
1228 // jle foo
1229 //
1230 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001231 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001232 (BOp->getOpcode() == Instruction::And ||
1233 BOp->getOpcode() == Instruction::Or)) {
1234 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1235 // If the compares in later blocks need to use values not currently
1236 // exported from this block, export them now. This block should always
1237 // be the first entry.
1238 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001240 // Allow some cases to be rejected.
1241 if (ShouldEmitAsBranches(SwitchCases)) {
1242 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1243 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1244 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1245 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001246
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001247 // Emit the branch for this block.
1248 visitSwitchCase(SwitchCases[0]);
1249 SwitchCases.erase(SwitchCases.begin());
1250 return;
1251 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001252
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001253 // Okay, we decided not to do this, remove any inserted MBB's and clear
1254 // SwitchCases.
1255 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
1256 CurMBB->getParent()->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001258 SwitchCases.clear();
1259 }
1260 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001262 // Create a CaseBlock record representing this branch.
1263 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(),
1264 NULL, Succ0MBB, Succ1MBB, CurMBB);
1265 // Use visitSwitchCase to actually insert the fast branch sequence for this
1266 // cond branch.
1267 visitSwitchCase(CB);
1268}
1269
1270/// visitSwitchCase - Emits the necessary code to represent a single node in
1271/// the binary search tree resulting from lowering a switch instruction.
1272void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) {
1273 SDValue Cond;
1274 SDValue CondLHS = getValue(CB.CmpLHS);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001275
1276 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001277 if (CB.CmpMHS == NULL) {
1278 // Fold "(X == true)" to X and "(X == false)" to !X to
1279 // handle common cases produced by branch lowering.
1280 if (CB.CmpRHS == ConstantInt::getTrue() && CB.CC == ISD::SETEQ)
1281 Cond = CondLHS;
1282 else if (CB.CmpRHS == ConstantInt::getFalse() && CB.CC == ISD::SETEQ) {
1283 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00001284 Cond = DAG.getNode(ISD::XOR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001285 CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001286 } else
1287 Cond = DAG.getSetCC(MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
1288 } else {
1289 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1290
Anton Korobeynikov23218582008-12-23 22:25:27 +00001291 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1292 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001293
1294 SDValue CmpOp = getValue(CB.CmpMHS);
1295 MVT VT = CmpOp.getValueType();
1296
1297 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
1298 Cond = DAG.getSetCC(MVT::i1, CmpOp, DAG.getConstant(High, VT), ISD::SETLE);
1299 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00001300 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001301 VT, CmpOp, DAG.getConstant(Low, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001302 Cond = DAG.getSetCC(MVT::i1, SUB,
1303 DAG.getConstant(High-Low, VT), ISD::SETULE);
1304 }
1305 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001307 // Update successor info
1308 CurMBB->addSuccessor(CB.TrueBB);
1309 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001310
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001311 // Set NextBlock to be the MBB immediately after the current one, if any.
1312 // This is used to avoid emitting unnecessary branches to the next block.
1313 MachineBasicBlock *NextBlock = 0;
1314 MachineFunction::iterator BBI = CurMBB;
1315 if (++BBI != CurMBB->getParent()->end())
1316 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001317
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001318 // If the lhs block is the next block, invert the condition so that we can
1319 // fall through to the lhs instead of the rhs block.
1320 if (CB.TrueBB == NextBlock) {
1321 std::swap(CB.TrueBB, CB.FalseBB);
1322 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00001323 Cond = DAG.getNode(ISD::XOR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001324 Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001325 }
Dale Johannesen66978ee2009-01-31 02:22:37 +00001326 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001327 MVT::Other, getControlRoot(), Cond,
1328 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330 // If the branch was constant folded, fix up the CFG.
1331 if (BrCond.getOpcode() == ISD::BR) {
1332 CurMBB->removeSuccessor(CB.FalseBB);
1333 DAG.setRoot(BrCond);
1334 } else {
1335 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001336 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001337 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001338
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001339 if (CB.FalseBB == NextBlock)
1340 DAG.setRoot(BrCond);
1341 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00001342 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001343 DAG.getBasicBlock(CB.FalseBB)));
1344 }
1345}
1346
1347/// visitJumpTable - Emit JumpTable node in the current MBB
1348void SelectionDAGLowering::visitJumpTable(JumpTable &JT) {
1349 // Emit the code for the jump table
1350 assert(JT.Reg != -1U && "Should lower JT Header first!");
1351 MVT PTy = TLI.getPointerTy();
1352 SDValue Index = DAG.getCopyFromReg(getControlRoot(), JT.Reg, PTy);
1353 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00001354 DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001355 MVT::Other, Index.getValue(1),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001356 Table, Index));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001357}
1358
1359/// visitJumpTableHeader - This function emits necessary code to produce index
1360/// in the JumpTable from switch case.
1361void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT,
1362 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001363 // Subtract the lowest switch case value from the value being switched on and
1364 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 // difference between smallest and largest cases.
1366 SDValue SwitchOp = getValue(JTH.SValue);
1367 MVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001368 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001369 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001370
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001371 // The SDNode we just created, which holds the value being switched on minus
1372 // the the smallest case value, needs to be copied to a virtual register so it
1373 // can be used as an index into the jump table in a subsequent basic block.
1374 // This value may be smaller or larger than the target's pointer type, and
1375 // therefore require extension or truncating.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001376 if (VT.bitsGT(TLI.getPointerTy()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00001377 SwitchOp = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001378 TLI.getPointerTy(), SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001379 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00001380 SwitchOp = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001381 TLI.getPointerTy(), SUB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001382
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001383 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
1384 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), JumpTableReg, SwitchOp);
1385 JT.Reg = JumpTableReg;
1386
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001387 // Emit the range check for the jump table, and branch to the default block
1388 // for the switch statement if the value being switched on exceeds the largest
1389 // case in the switch.
Duncan Sands5480c042009-01-01 15:52:00 +00001390 SDValue CMP = DAG.getSetCC(TLI.getSetCCResultType(SUB.getValueType()), SUB,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001391 DAG.getConstant(JTH.Last-JTH.First,VT),
1392 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001393
1394 // Set NextBlock to be the MBB immediately after the current one, if any.
1395 // This is used to avoid emitting unnecessary branches to the next block.
1396 MachineBasicBlock *NextBlock = 0;
1397 MachineFunction::iterator BBI = CurMBB;
1398 if (++BBI != CurMBB->getParent()->end())
1399 NextBlock = BBI;
1400
Dale Johannesen66978ee2009-01-31 02:22:37 +00001401 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001402 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001403 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001404
1405 if (JT.MBB == NextBlock)
1406 DAG.setRoot(BrCond);
1407 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00001408 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409 DAG.getBasicBlock(JT.MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001410}
1411
1412/// visitBitTestHeader - This function emits necessary code to produce value
1413/// suitable for "bit tests"
1414void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) {
1415 // Subtract the minimum value
1416 SDValue SwitchOp = getValue(B.SValue);
1417 MVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001418 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001419 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001420
1421 // Check range
Duncan Sands5480c042009-01-01 15:52:00 +00001422 SDValue RangeCmp = DAG.getSetCC(TLI.getSetCCResultType(SUB.getValueType()), SUB,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001423 DAG.getConstant(B.Range, VT),
1424 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001425
1426 SDValue ShiftOp;
1427 if (VT.bitsGT(TLI.getShiftAmountTy()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00001428 ShiftOp = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001429 TLI.getShiftAmountTy(), SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00001431 ShiftOp = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001432 TLI.getShiftAmountTy(), SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001433
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001434 B.Reg = FuncInfo.MakeReg(TLI.getShiftAmountTy());
1435 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436
1437 // Set NextBlock to be the MBB immediately after the current one, if any.
1438 // This is used to avoid emitting unnecessary branches to the next block.
1439 MachineBasicBlock *NextBlock = 0;
1440 MachineFunction::iterator BBI = CurMBB;
1441 if (++BBI != CurMBB->getParent()->end())
1442 NextBlock = BBI;
1443
1444 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1445
1446 CurMBB->addSuccessor(B.Default);
1447 CurMBB->addSuccessor(MBB);
1448
Dale Johannesen66978ee2009-01-31 02:22:37 +00001449 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001450 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001451 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001452
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001453 if (MBB == NextBlock)
1454 DAG.setRoot(BrRange);
1455 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00001456 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457 DAG.getBasicBlock(MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458}
1459
1460/// visitBitTestCase - this function produces one "bit test"
1461void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB,
1462 unsigned Reg,
1463 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001464 // Make desired shift
1465 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), Reg,
1466 TLI.getShiftAmountTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00001467 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001468 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001469 DAG.getConstant(1, TLI.getPointerTy()),
1470 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001471
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001472 // Emit bit tests and jumps
Dale Johannesen66978ee2009-01-31 02:22:37 +00001473 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001474 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001475 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Duncan Sands5480c042009-01-01 15:52:00 +00001476 SDValue AndCmp = DAG.getSetCC(TLI.getSetCCResultType(AndOp.getValueType()),
1477 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001478 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001479
1480 CurMBB->addSuccessor(B.TargetBB);
1481 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001482
Dale Johannesen66978ee2009-01-31 02:22:37 +00001483 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001484 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001485 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486
1487 // Set NextBlock to be the MBB immediately after the current one, if any.
1488 // This is used to avoid emitting unnecessary branches to the next block.
1489 MachineBasicBlock *NextBlock = 0;
1490 MachineFunction::iterator BBI = CurMBB;
1491 if (++BBI != CurMBB->getParent()->end())
1492 NextBlock = BBI;
1493
1494 if (NextMBB == NextBlock)
1495 DAG.setRoot(BrAnd);
1496 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00001497 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001498 DAG.getBasicBlock(NextMBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001499}
1500
1501void SelectionDAGLowering::visitInvoke(InvokeInst &I) {
1502 // Retrieve successors.
1503 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1504 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1505
Gabor Greifb67e6b32009-01-15 11:10:44 +00001506 const Value *Callee(I.getCalledValue());
1507 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001508 visitInlineAsm(&I);
1509 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001510 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001511
1512 // If the value of the invoke is used outside of its defining block, make it
1513 // available as a virtual register.
1514 if (!I.use_empty()) {
1515 DenseMap<const Value*, unsigned>::iterator VMI = FuncInfo.ValueMap.find(&I);
1516 if (VMI != FuncInfo.ValueMap.end())
1517 CopyValueToVirtualRegister(&I, VMI->second);
1518 }
1519
1520 // Update successor info
1521 CurMBB->addSuccessor(Return);
1522 CurMBB->addSuccessor(LandingPad);
1523
1524 // Drop into normal successor.
Dale Johannesen66978ee2009-01-31 02:22:37 +00001525 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001526 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001527 DAG.getBasicBlock(Return)));
1528}
1529
1530void SelectionDAGLowering::visitUnwind(UnwindInst &I) {
1531}
1532
1533/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1534/// small case ranges).
1535bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR,
1536 CaseRecVector& WorkList,
1537 Value* SV,
1538 MachineBasicBlock* Default) {
1539 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001541 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001542 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001543 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001544 return false;
1545
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 // Get the MachineFunction which holds the current MBB. This is used when
1547 // inserting any additional MBBs necessary to represent the switch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001548 MachineFunction *CurMF = CurMBB->getParent();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001549
1550 // Figure out which block is immediately after the current one.
1551 MachineBasicBlock *NextBlock = 0;
1552 MachineFunction::iterator BBI = CR.CaseBB;
1553
1554 if (++BBI != CurMBB->getParent()->end())
1555 NextBlock = BBI;
1556
1557 // TODO: If any two of the cases has the same destination, and if one value
1558 // is the same as the other, but has one bit unset that the other has set,
1559 // use bit manipulation to do two compares at once. For example:
1560 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001561
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562 // Rearrange the case blocks so that the last one falls through if possible.
1563 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1564 // The last case block won't fall through into 'NextBlock' if we emit the
1565 // branches in this order. See if rearranging a case value would help.
1566 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1567 if (I->BB == NextBlock) {
1568 std::swap(*I, BackCase);
1569 break;
1570 }
1571 }
1572 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001573
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001574 // Create a CaseBlock record representing a conditional branch to
1575 // the Case's target mbb if the value being switched on SV is equal
1576 // to C.
1577 MachineBasicBlock *CurBlock = CR.CaseBB;
1578 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1579 MachineBasicBlock *FallThrough;
1580 if (I != E-1) {
1581 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1582 CurMF->insert(BBI, FallThrough);
1583 } else {
1584 // If the last case doesn't match, go to the default block.
1585 FallThrough = Default;
1586 }
1587
1588 Value *RHS, *LHS, *MHS;
1589 ISD::CondCode CC;
1590 if (I->High == I->Low) {
1591 // This is just small small case range :) containing exactly 1 case
1592 CC = ISD::SETEQ;
1593 LHS = SV; RHS = I->High; MHS = NULL;
1594 } else {
1595 CC = ISD::SETLE;
1596 LHS = I->Low; MHS = SV; RHS = I->High;
1597 }
1598 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001599
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001600 // If emitting the first comparison, just call visitSwitchCase to emit the
1601 // code into the current block. Otherwise, push the CaseBlock onto the
1602 // vector to be later processed by SDISel, and insert the node's MBB
1603 // before the next MBB.
1604 if (CurBlock == CurMBB)
1605 visitSwitchCase(CB);
1606 else
1607 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001608
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609 CurBlock = FallThrough;
1610 }
1611
1612 return true;
1613}
1614
1615static inline bool areJTsAllowed(const TargetLowering &TLI) {
1616 return !DisableJumpTables &&
Dan Gohmanf560ffa2009-01-28 17:46:25 +00001617 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1618 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001620
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001621static APInt ComputeRange(const APInt &First, const APInt &Last) {
1622 APInt LastExt(Last), FirstExt(First);
1623 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1624 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1625 return (LastExt - FirstExt + 1ULL);
1626}
1627
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001628/// handleJTSwitchCase - Emit jumptable for current switch case range
1629bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR,
1630 CaseRecVector& WorkList,
1631 Value* SV,
1632 MachineBasicBlock* Default) {
1633 Case& FrontCase = *CR.Range.first;
1634 Case& BackCase = *(CR.Range.second-1);
1635
Anton Korobeynikov23218582008-12-23 22:25:27 +00001636 const APInt& First = cast<ConstantInt>(FrontCase.Low)->getValue();
1637 const APInt& Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638
Anton Korobeynikov23218582008-12-23 22:25:27 +00001639 size_t TSize = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1641 I!=E; ++I)
1642 TSize += I->size();
1643
1644 if (!areJTsAllowed(TLI) || TSize <= 3)
1645 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001646
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001647 APInt Range = ComputeRange(First, Last);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001648 double Density = (double)TSize / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001649 if (Density < 0.4)
1650 return false;
1651
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001652 DEBUG(errs() << "Lowering jump table\n"
1653 << "First entry: " << First << ". Last entry: " << Last << '\n'
1654 << "Range: " << Range
1655 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656
1657 // Get the MachineFunction which holds the current MBB. This is used when
1658 // inserting any additional MBBs necessary to represent the switch.
1659 MachineFunction *CurMF = CurMBB->getParent();
1660
1661 // Figure out which block is immediately after the current one.
1662 MachineBasicBlock *NextBlock = 0;
1663 MachineFunction::iterator BBI = CR.CaseBB;
1664
1665 if (++BBI != CurMBB->getParent()->end())
1666 NextBlock = BBI;
1667
1668 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1669
1670 // Create a new basic block to hold the code for loading the address
1671 // of the jump table, and jumping to it. Update successor information;
1672 // we will either branch to the default case for the switch, or the jump
1673 // table.
1674 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1675 CurMF->insert(BBI, JumpTableBB);
1676 CR.CaseBB->addSuccessor(Default);
1677 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001679 // Build a vector of destination BBs, corresponding to each target
1680 // of the jump table. If the value of the jump table slot corresponds to
1681 // a case statement, push the case's BB onto the vector, otherwise, push
1682 // the default BB.
1683 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001684 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001685 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001686 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1687 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1688
1689 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001690 DestBBs.push_back(I->BB);
1691 if (TEI==High)
1692 ++I;
1693 } else {
1694 DestBBs.push_back(Default);
1695 }
1696 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001697
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001698 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001699 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1700 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001701 E = DestBBs.end(); I != E; ++I) {
1702 if (!SuccsHandled[(*I)->getNumber()]) {
1703 SuccsHandled[(*I)->getNumber()] = true;
1704 JumpTableBB->addSuccessor(*I);
1705 }
1706 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001707
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001708 // Create a jump table index for this jump table, or return an existing
1709 // one.
1710 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001711
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001712 // Set the jump table information so that we can codegen it as a second
1713 // MachineBasicBlock
1714 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1715 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1716 if (CR.CaseBB == CurMBB)
1717 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001718
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719 JTCases.push_back(JumpTableBlock(JTH, JT));
1720
1721 return true;
1722}
1723
1724/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1725/// 2 subtrees.
1726bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR,
1727 CaseRecVector& WorkList,
1728 Value* SV,
1729 MachineBasicBlock* Default) {
1730 // Get the MachineFunction which holds the current MBB. This is used when
1731 // inserting any additional MBBs necessary to represent the switch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001732 MachineFunction *CurMF = CurMBB->getParent();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001733
1734 // Figure out which block is immediately after the current one.
1735 MachineBasicBlock *NextBlock = 0;
1736 MachineFunction::iterator BBI = CR.CaseBB;
1737
1738 if (++BBI != CurMBB->getParent()->end())
1739 NextBlock = BBI;
1740
1741 Case& FrontCase = *CR.Range.first;
1742 Case& BackCase = *(CR.Range.second-1);
1743 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1744
1745 // Size is the number of Cases represented by this range.
1746 unsigned Size = CR.Range.second - CR.Range.first;
1747
Anton Korobeynikov23218582008-12-23 22:25:27 +00001748 const APInt& First = cast<ConstantInt>(FrontCase.Low)->getValue();
1749 const APInt& Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001750 double FMetric = 0;
1751 CaseItr Pivot = CR.Range.first + Size/2;
1752
1753 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1754 // (heuristically) allow us to emit JumpTable's later.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001755 size_t TSize = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001756 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1757 I!=E; ++I)
1758 TSize += I->size();
1759
Anton Korobeynikov23218582008-12-23 22:25:27 +00001760 size_t LSize = FrontCase.size();
1761 size_t RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001762 DEBUG(errs() << "Selecting best pivot: \n"
1763 << "First: " << First << ", Last: " << Last <<'\n'
1764 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001765 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1766 J!=E; ++I, ++J) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001767 const APInt& LEnd = cast<ConstantInt>(I->High)->getValue();
1768 const APInt& RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001769 APInt Range = ComputeRange(LEnd, RBegin);
1770 assert((Range - 2ULL).isNonNegative() &&
1771 "Invalid case distance");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001772 double LDensity = (double)LSize / (LEnd - First + 1ULL).roundToDouble();
1773 double RDensity = (double)RSize / (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001774 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001775 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001776 DEBUG(errs() <<"=>Step\n"
1777 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1778 << "LDensity: " << LDensity
1779 << ", RDensity: " << RDensity << '\n'
1780 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781 if (FMetric < Metric) {
1782 Pivot = J;
1783 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001784 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001785 }
1786
1787 LSize += J->size();
1788 RSize -= J->size();
1789 }
1790 if (areJTsAllowed(TLI)) {
1791 // If our case is dense we *really* should handle it earlier!
1792 assert((FMetric > 0) && "Should handle dense range earlier!");
1793 } else {
1794 Pivot = CR.Range.first + Size/2;
1795 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001796
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001797 CaseRange LHSR(CR.Range.first, Pivot);
1798 CaseRange RHSR(Pivot, CR.Range.second);
1799 Constant *C = Pivot->Low;
1800 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001802 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001803 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001804 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001805 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001806 // Pivot's Value, then we can branch directly to the LHS's Target,
1807 // rather than creating a leaf node for it.
1808 if ((LHSR.second - LHSR.first) == 1 &&
1809 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001810 cast<ConstantInt>(C)->getValue() ==
1811 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001812 TrueBB = LHSR.first->BB;
1813 } else {
1814 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1815 CurMF->insert(BBI, TrueBB);
1816 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
1817 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001818
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001819 // Similar to the optimization above, if the Value being switched on is
1820 // known to be less than the Constant CR.LT, and the current Case Value
1821 // is CR.LT - 1, then we can branch directly to the target block for
1822 // the current Case Value, rather than emitting a RHS leaf node for it.
1823 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001824 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1825 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 FalseBB = RHSR.first->BB;
1827 } else {
1828 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1829 CurMF->insert(BBI, FalseBB);
1830 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
1831 }
1832
1833 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001834 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001835 // Otherwise, branch to LHS.
1836 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1837
1838 if (CR.CaseBB == CurMBB)
1839 visitSwitchCase(CB);
1840 else
1841 SwitchCases.push_back(CB);
1842
1843 return true;
1844}
1845
1846/// handleBitTestsSwitchCase - if current case range has few destination and
1847/// range span less, than machine word bitwidth, encode case range into series
1848/// of masks and emit bit tests with these masks.
1849bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1850 CaseRecVector& WorkList,
1851 Value* SV,
1852 MachineBasicBlock* Default){
1853 unsigned IntPtrBits = TLI.getPointerTy().getSizeInBits();
1854
1855 Case& FrontCase = *CR.Range.first;
1856 Case& BackCase = *(CR.Range.second-1);
1857
1858 // Get the MachineFunction which holds the current MBB. This is used when
1859 // inserting any additional MBBs necessary to represent the switch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001860 MachineFunction *CurMF = CurMBB->getParent();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001861
Anton Korobeynikov23218582008-12-23 22:25:27 +00001862 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1864 I!=E; ++I) {
1865 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001866 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001867 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001868
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001869 // Count unique destinations
1870 SmallSet<MachineBasicBlock*, 4> Dests;
1871 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1872 Dests.insert(I->BB);
1873 if (Dests.size() > 3)
1874 // Don't bother the code below, if there are too much unique destinations
1875 return false;
1876 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001877 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1878 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001879
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001880 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001881 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1882 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001883 APInt cmpRange = maxValue - minValue;
1884
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001885 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1886 << "Low bound: " << minValue << '\n'
1887 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001888
1889 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001890 (!(Dests.size() == 1 && numCmps >= 3) &&
1891 !(Dests.size() == 2 && numCmps >= 5) &&
1892 !(Dests.size() >= 3 && numCmps >= 6)))
1893 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001894
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001895 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001896 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001898 // Optimize the case where all the case values fit in a
1899 // word without having to subtract minValue. In this case,
1900 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001901 if (minValue.isNonNegative() &&
1902 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1903 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001904 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001905 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001906 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001907
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001908 CaseBitsVector CasesBits;
1909 unsigned i, count = 0;
1910
1911 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1912 MachineBasicBlock* Dest = I->BB;
1913 for (i = 0; i < count; ++i)
1914 if (Dest == CasesBits[i].BB)
1915 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001916
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001917 if (i == count) {
1918 assert((count < 3) && "Too much destinations to test!");
1919 CasesBits.push_back(CaseBits(0, Dest, 0));
1920 count++;
1921 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001922
1923 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1924 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1925
1926 uint64_t lo = (lowValue - lowBound).getZExtValue();
1927 uint64_t hi = (highValue - lowBound).getZExtValue();
1928
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929 for (uint64_t j = lo; j <= hi; j++) {
1930 CasesBits[i].Mask |= 1ULL << j;
1931 CasesBits[i].Bits++;
1932 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934 }
1935 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001936
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001937 BitTestInfo BTC;
1938
1939 // Figure out which block is immediately after the current one.
1940 MachineFunction::iterator BBI = CR.CaseBB;
1941 ++BBI;
1942
1943 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1944
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001945 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001947 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
1948 << ", Bits: " << CasesBits[i].Bits
1949 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001950
1951 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1952 CurMF->insert(BBI, CaseBB);
1953 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1954 CaseBB,
1955 CasesBits[i].BB));
1956 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001957
1958 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001959 -1U, (CR.CaseBB == CurMBB),
1960 CR.CaseBB, Default, BTC);
1961
1962 if (CR.CaseBB == CurMBB)
1963 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001965 BitTestCases.push_back(BTB);
1966
1967 return true;
1968}
1969
1970
1971/// Clusterify - Transform simple list of Cases into list of CaseRange's
Anton Korobeynikov23218582008-12-23 22:25:27 +00001972size_t SelectionDAGLowering::Clusterify(CaseVector& Cases,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001973 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001974 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001975
1976 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001977 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001978 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1979 Cases.push_back(Case(SI.getSuccessorValue(i),
1980 SI.getSuccessorValue(i),
1981 SMBB));
1982 }
1983 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1984
1985 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001986 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001987 // Must recompute end() each iteration because it may be
1988 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001989 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1990 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1991 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992 MachineBasicBlock* nextBB = J->BB;
1993 MachineBasicBlock* currentBB = I->BB;
1994
1995 // If the two neighboring cases go to the same destination, merge them
1996 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001997 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001998 I->High = J->High;
1999 J = Cases.erase(J);
2000 } else {
2001 I = J++;
2002 }
2003 }
2004
2005 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2006 if (I->Low != I->High)
2007 // A range counts double, since it requires two compares.
2008 ++numCmps;
2009 }
2010
2011 return numCmps;
2012}
2013
Anton Korobeynikov23218582008-12-23 22:25:27 +00002014void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002015 // Figure out which block is immediately after the current one.
2016 MachineBasicBlock *NextBlock = 0;
2017 MachineFunction::iterator BBI = CurMBB;
2018
2019 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2020
2021 // If there is only the default destination, branch to it if it is not the
2022 // next basic block. Otherwise, just fall through.
2023 if (SI.getNumOperands() == 2) {
2024 // Update machine-CFG edges.
2025
2026 // If this is not a fall-through branch, emit the branch.
2027 CurMBB->addSuccessor(Default);
2028 if (Default != NextBlock)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002029 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002030 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002031 DAG.getBasicBlock(Default)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002032 return;
2033 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002035 // If there are any non-default case statements, create a vector of Cases
2036 // representing each one, and sort the vector so that we can efficiently
2037 // create a binary search tree from them.
2038 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002039 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002040 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2041 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002042 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002043
2044 // Get the Value to be switched on and default basic blocks, which will be
2045 // inserted into CaseBlock records, representing basic blocks in the binary
2046 // search tree.
2047 Value *SV = SI.getOperand(0);
2048
2049 // Push the initial CaseRec onto the worklist
2050 CaseRecVector WorkList;
2051 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2052
2053 while (!WorkList.empty()) {
2054 // Grab a record representing a case range to process off the worklist
2055 CaseRec CR = WorkList.back();
2056 WorkList.pop_back();
2057
2058 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2059 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002060
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002061 // If the range has few cases (two or less) emit a series of specific
2062 // tests.
2063 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2064 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002065
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002066 // If the switch has more than 5 blocks, and at least 40% dense, and the
2067 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002068 // lowering the switch to a binary tree of conditional branches.
2069 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2070 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002071
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2073 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2074 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2075 }
2076}
2077
2078
2079void SelectionDAGLowering::visitSub(User &I) {
2080 // -0.0 - X --> fneg
2081 const Type *Ty = I.getType();
2082 if (isa<VectorType>(Ty)) {
2083 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2084 const VectorType *DestTy = cast<VectorType>(I.getType());
2085 const Type *ElTy = DestTy->getElementType();
2086 if (ElTy->isFloatingPoint()) {
2087 unsigned VL = DestTy->getNumElements();
2088 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
2089 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
2090 if (CV == CNZ) {
2091 SDValue Op2 = getValue(I.getOperand(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002092 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002093 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002094 return;
2095 }
2096 }
2097 }
2098 }
2099 if (Ty->isFloatingPoint()) {
2100 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
2101 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
2102 SDValue Op2 = getValue(I.getOperand(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002103 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002104 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002105 return;
2106 }
2107 }
2108
2109 visitBinary(I, Ty->isFPOrFPVector() ? ISD::FSUB : ISD::SUB);
2110}
2111
2112void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
2113 SDValue Op1 = getValue(I.getOperand(0));
2114 SDValue Op2 = getValue(I.getOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002115
Dale Johannesen66978ee2009-01-31 02:22:37 +00002116 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002117 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002118}
2119
2120void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2121 SDValue Op1 = getValue(I.getOperand(0));
2122 SDValue Op2 = getValue(I.getOperand(1));
2123 if (!isa<VectorType>(I.getType())) {
2124 if (TLI.getShiftAmountTy().bitsLT(Op2.getValueType()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00002125 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002126 TLI.getShiftAmountTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127 else if (TLI.getShiftAmountTy().bitsGT(Op2.getValueType()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00002128 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002129 TLI.getShiftAmountTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002130 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002131
Dale Johannesen66978ee2009-01-31 02:22:37 +00002132 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002133 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002134}
2135
2136void SelectionDAGLowering::visitICmp(User &I) {
2137 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2138 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2139 predicate = IC->getPredicate();
2140 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2141 predicate = ICmpInst::Predicate(IC->getPredicate());
2142 SDValue Op1 = getValue(I.getOperand(0));
2143 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002144 ISD::CondCode Opcode = getICmpCondCode(predicate);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002145 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Opcode));
2146}
2147
2148void SelectionDAGLowering::visitFCmp(User &I) {
2149 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2150 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2151 predicate = FC->getPredicate();
2152 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2153 predicate = FCmpInst::Predicate(FC->getPredicate());
2154 SDValue Op1 = getValue(I.getOperand(0));
2155 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002156 ISD::CondCode Condition = getFCmpCondCode(predicate);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002157 setValue(&I, DAG.getSetCC(MVT::i1, Op1, Op2, Condition));
2158}
2159
2160void SelectionDAGLowering::visitVICmp(User &I) {
2161 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2162 if (VICmpInst *IC = dyn_cast<VICmpInst>(&I))
2163 predicate = IC->getPredicate();
2164 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2165 predicate = ICmpInst::Predicate(IC->getPredicate());
2166 SDValue Op1 = getValue(I.getOperand(0));
2167 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002168 ISD::CondCode Opcode = getICmpCondCode(predicate);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002169 setValue(&I, DAG.getVSetCC(Op1.getValueType(), Op1, Op2, Opcode));
2170}
2171
2172void SelectionDAGLowering::visitVFCmp(User &I) {
2173 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2174 if (VFCmpInst *FC = dyn_cast<VFCmpInst>(&I))
2175 predicate = FC->getPredicate();
2176 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2177 predicate = FCmpInst::Predicate(FC->getPredicate());
2178 SDValue Op1 = getValue(I.getOperand(0));
2179 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002180 ISD::CondCode Condition = getFCmpCondCode(predicate);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002181 MVT DestVT = TLI.getValueType(I.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002182
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002183 setValue(&I, DAG.getVSetCC(DestVT, Op1, Op2, Condition));
2184}
2185
2186void SelectionDAGLowering::visitSelect(User &I) {
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002187 SmallVector<MVT, 4> ValueVTs;
2188 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2189 unsigned NumValues = ValueVTs.size();
2190 if (NumValues != 0) {
2191 SmallVector<SDValue, 4> Values(NumValues);
2192 SDValue Cond = getValue(I.getOperand(0));
2193 SDValue TrueVal = getValue(I.getOperand(1));
2194 SDValue FalseVal = getValue(I.getOperand(2));
2195
2196 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002197 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002198 TrueVal.getValueType(), Cond,
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002199 SDValue(TrueVal.getNode(), TrueVal.getResNo() + i),
2200 SDValue(FalseVal.getNode(), FalseVal.getResNo() + i));
2201
Dale Johannesen66978ee2009-01-31 02:22:37 +00002202 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002203 DAG.getVTList(&ValueVTs[0], NumValues),
2204 &Values[0], NumValues));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002205 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002206}
2207
2208
2209void SelectionDAGLowering::visitTrunc(User &I) {
2210 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2211 SDValue N = getValue(I.getOperand(0));
2212 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002213 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002214}
2215
2216void SelectionDAGLowering::visitZExt(User &I) {
2217 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2218 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2219 SDValue N = getValue(I.getOperand(0));
2220 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002221 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222}
2223
2224void SelectionDAGLowering::visitSExt(User &I) {
2225 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2226 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2227 SDValue N = getValue(I.getOperand(0));
2228 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002229 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002230}
2231
2232void SelectionDAGLowering::visitFPTrunc(User &I) {
2233 // FPTrunc is never a no-op cast, no need to check
2234 SDValue N = getValue(I.getOperand(0));
2235 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002236 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002237 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002238}
2239
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002240void SelectionDAGLowering::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241 // FPTrunc is never a no-op cast, no need to check
2242 SDValue N = getValue(I.getOperand(0));
2243 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002244 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002245}
2246
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002247void SelectionDAGLowering::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002248 // FPToUI is never a no-op cast, no need to check
2249 SDValue N = getValue(I.getOperand(0));
2250 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002251 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252}
2253
2254void SelectionDAGLowering::visitFPToSI(User &I) {
2255 // FPToSI is never a no-op cast, no need to check
2256 SDValue N = getValue(I.getOperand(0));
2257 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002258 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002259}
2260
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002261void SelectionDAGLowering::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002262 // UIToFP is never a no-op cast, no need to check
2263 SDValue N = getValue(I.getOperand(0));
2264 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002265 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002266}
2267
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002268void SelectionDAGLowering::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002269 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002270 SDValue N = getValue(I.getOperand(0));
2271 MVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002272 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273}
2274
2275void SelectionDAGLowering::visitPtrToInt(User &I) {
2276 // What to do depends on the size of the integer and the size of the pointer.
2277 // We can either truncate, zero extend, or no-op, accordingly.
2278 SDValue N = getValue(I.getOperand(0));
2279 MVT SrcVT = N.getValueType();
2280 MVT DestVT = TLI.getValueType(I.getType());
2281 SDValue Result;
2282 if (DestVT.bitsLT(SrcVT))
Dale Johannesen66978ee2009-01-31 02:22:37 +00002283 Result = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002284 else
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002285 // Note: ZERO_EXTEND can handle cases where the sizes are equal too
Dale Johannesen66978ee2009-01-31 02:22:37 +00002286 Result = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002287 setValue(&I, Result);
2288}
2289
2290void SelectionDAGLowering::visitIntToPtr(User &I) {
2291 // What to do depends on the size of the integer and the size of the pointer.
2292 // We can either truncate, zero extend, or no-op, accordingly.
2293 SDValue N = getValue(I.getOperand(0));
2294 MVT SrcVT = N.getValueType();
2295 MVT DestVT = TLI.getValueType(I.getType());
2296 if (DestVT.bitsLT(SrcVT))
Dale Johannesen66978ee2009-01-31 02:22:37 +00002297 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002298 else
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002299 // Note: ZERO_EXTEND can handle cases where the sizes are equal too
Dale Johannesen66978ee2009-01-31 02:22:37 +00002300 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002301 DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002302}
2303
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002304void SelectionDAGLowering::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002305 SDValue N = getValue(I.getOperand(0));
2306 MVT DestVT = TLI.getValueType(I.getType());
2307
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002308 // BitCast assures us that source and destination are the same size so this
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002309 // is either a BIT_CONVERT or a no-op.
2310 if (DestVT != N.getValueType())
Dale Johannesen66978ee2009-01-31 02:22:37 +00002311 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002312 DestVT, N)); // convert types
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002313 else
2314 setValue(&I, N); // noop cast.
2315}
2316
2317void SelectionDAGLowering::visitInsertElement(User &I) {
2318 SDValue InVec = getValue(I.getOperand(0));
2319 SDValue InVal = getValue(I.getOperand(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002320 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002321 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002322 getValue(I.getOperand(2)));
2323
Dale Johannesen66978ee2009-01-31 02:22:37 +00002324 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002325 TLI.getValueType(I.getType()),
2326 InVec, InVal, InIdx));
2327}
2328
2329void SelectionDAGLowering::visitExtractElement(User &I) {
2330 SDValue InVec = getValue(I.getOperand(0));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002331 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002332 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002333 getValue(I.getOperand(1)));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002334 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002335 TLI.getValueType(I.getType()), InVec, InIdx));
2336}
2337
Mon P Wangaeb06d22008-11-10 04:46:22 +00002338
2339// Utility for visitShuffleVector - Returns true if the mask is mask starting
2340// from SIndx and increasing to the element length (undefs are allowed).
2341static bool SequentialMask(SDValue Mask, unsigned SIndx) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002342 unsigned MaskNumElts = Mask.getNumOperands();
2343 for (unsigned i = 0; i != MaskNumElts; ++i) {
Mon P Wangaeb06d22008-11-10 04:46:22 +00002344 if (Mask.getOperand(i).getOpcode() != ISD::UNDEF) {
2345 unsigned Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue();
2346 if (Idx != i + SIndx)
2347 return false;
2348 }
2349 }
2350 return true;
2351}
2352
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002353void SelectionDAGLowering::visitShuffleVector(User &I) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002354 SDValue Src1 = getValue(I.getOperand(0));
2355 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002356 SDValue Mask = getValue(I.getOperand(2));
2357
Mon P Wangaeb06d22008-11-10 04:46:22 +00002358 MVT VT = TLI.getValueType(I.getType());
Mon P Wang230e4fa2008-11-21 04:25:21 +00002359 MVT SrcVT = Src1.getValueType();
Mon P Wangc7849c22008-11-16 05:06:27 +00002360 int MaskNumElts = Mask.getNumOperands();
2361 int SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002362
Mon P Wangc7849c22008-11-16 05:06:27 +00002363 if (SrcNumElts == MaskNumElts) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002364 setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002365 VT, Src1, Src2, Mask));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002366 return;
2367 }
2368
2369 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002370 MVT MaskEltVT = Mask.getValueType().getVectorElementType();
2371
2372 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2373 // Mask is longer than the source vectors and is a multiple of the source
2374 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002375 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002376 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2377 // The shuffle is concatenating two vectors together.
Dale Johannesen66978ee2009-01-31 02:22:37 +00002378 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002379 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002380 return;
2381 }
2382
Mon P Wangc7849c22008-11-16 05:06:27 +00002383 // Pad both vectors with undefs to make them the same length as the mask.
2384 unsigned NumConcat = MaskNumElts / SrcNumElts;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002385 SDValue UndefVal = DAG.getNode(ISD::UNDEF, getCurDebugLoc(), SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002386
Mon P Wang230e4fa2008-11-21 04:25:21 +00002387 SDValue* MOps1 = new SDValue[NumConcat];
2388 SDValue* MOps2 = new SDValue[NumConcat];
2389 MOps1[0] = Src1;
2390 MOps2[0] = Src2;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002391 for (unsigned i = 1; i != NumConcat; ++i) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002392 MOps1[i] = UndefVal;
2393 MOps2[i] = UndefVal;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002394 }
Dale Johannesen66978ee2009-01-31 02:22:37 +00002395 Src1 = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002396 VT, MOps1, NumConcat);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002397 Src2 = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002398 VT, MOps2, NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002399
2400 delete [] MOps1;
2401 delete [] MOps2;
2402
Mon P Wangaeb06d22008-11-10 04:46:22 +00002403 // Readjust mask for new input vector length.
2404 SmallVector<SDValue, 8> MappedOps;
Mon P Wangc7849c22008-11-16 05:06:27 +00002405 for (int i = 0; i != MaskNumElts; ++i) {
Mon P Wangaeb06d22008-11-10 04:46:22 +00002406 if (Mask.getOperand(i).getOpcode() == ISD::UNDEF) {
2407 MappedOps.push_back(Mask.getOperand(i));
2408 } else {
Mon P Wangc7849c22008-11-16 05:06:27 +00002409 int Idx = cast<ConstantSDNode>(Mask.getOperand(i))->getZExtValue();
2410 if (Idx < SrcNumElts)
2411 MappedOps.push_back(DAG.getConstant(Idx, MaskEltVT));
2412 else
2413 MappedOps.push_back(DAG.getConstant(Idx + MaskNumElts - SrcNumElts,
2414 MaskEltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002415 }
2416 }
Dale Johannesen66978ee2009-01-31 02:22:37 +00002417 Mask = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002418 Mask.getValueType(),
Mon P Wangaeb06d22008-11-10 04:46:22 +00002419 &MappedOps[0], MappedOps.size());
2420
Dale Johannesen66978ee2009-01-31 02:22:37 +00002421 setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002422 VT, Src1, Src2, Mask));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002423 return;
2424 }
2425
Mon P Wangc7849c22008-11-16 05:06:27 +00002426 if (SrcNumElts > MaskNumElts) {
Mon P Wangaeb06d22008-11-10 04:46:22 +00002427 // Resulting vector is shorter than the incoming vector.
Mon P Wangc7849c22008-11-16 05:06:27 +00002428 if (SrcNumElts == MaskNumElts && SequentialMask(Mask,0)) {
Mon P Wangaeb06d22008-11-10 04:46:22 +00002429 // Shuffle extracts 1st vector.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002430 setValue(&I, Src1);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002431 return;
2432 }
2433
Mon P Wangc7849c22008-11-16 05:06:27 +00002434 if (SrcNumElts == MaskNumElts && SequentialMask(Mask,MaskNumElts)) {
Mon P Wangaeb06d22008-11-10 04:46:22 +00002435 // Shuffle extracts 2nd vector.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002436 setValue(&I, Src2);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002437 return;
2438 }
2439
Mon P Wangc7849c22008-11-16 05:06:27 +00002440 // Analyze the access pattern of the vector to see if we can extract
2441 // two subvectors and do the shuffle. The analysis is done by calculating
2442 // the range of elements the mask access on both vectors.
2443 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2444 int MaxRange[2] = {-1, -1};
2445
2446 for (int i = 0; i != MaskNumElts; ++i) {
Mon P Wangaeb06d22008-11-10 04:46:22 +00002447 SDValue Arg = Mask.getOperand(i);
2448 if (Arg.getOpcode() != ISD::UNDEF) {
2449 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Mon P Wangc7849c22008-11-16 05:06:27 +00002450 int Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
2451 int Input = 0;
2452 if (Idx >= SrcNumElts) {
2453 Input = 1;
2454 Idx -= SrcNumElts;
2455 }
2456 if (Idx > MaxRange[Input])
2457 MaxRange[Input] = Idx;
2458 if (Idx < MinRange[Input])
2459 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002460 }
2461 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002462
Mon P Wangc7849c22008-11-16 05:06:27 +00002463 // Check if the access is smaller than the vector size and can we find
2464 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002465 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002466 int StartIdx[2]; // StartIdx to extract from
2467 for (int Input=0; Input < 2; ++Input) {
2468 if (MinRange[Input] == SrcNumElts+1 && MaxRange[Input] == -1) {
2469 RangeUse[Input] = 0; // Unused
2470 StartIdx[Input] = 0;
2471 } else if (MaxRange[Input] - MinRange[Input] < MaskNumElts) {
2472 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002473 // start index that is a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002474 if (MaxRange[Input] < MaskNumElts) {
2475 RangeUse[Input] = 1; // Extract from beginning of the vector
2476 StartIdx[Input] = 0;
2477 } else {
2478 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Mon P Wang6cce3da2008-11-23 04:35:05 +00002479 if (MaxRange[Input] - StartIdx[Input] < MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002480 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002481 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002482 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002483 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002484 }
2485
2486 if (RangeUse[0] == 0 && RangeUse[0] == 0) {
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002487 setValue(&I, DAG.getNode(ISD::UNDEF,
Dale Johannesen66978ee2009-01-31 02:22:37 +00002488 getCurDebugLoc(), VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002489 return;
2490 }
2491 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2492 // Extract appropriate subvector and generate a vector shuffle
2493 for (int Input=0; Input < 2; ++Input) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002494 SDValue& Src = Input == 0 ? Src1 : Src2;
Mon P Wangc7849c22008-11-16 05:06:27 +00002495 if (RangeUse[Input] == 0) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002496 Src = DAG.getNode(ISD::UNDEF, getCurDebugLoc(), VT);
Mon P Wangc7849c22008-11-16 05:06:27 +00002497 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002498 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002499 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002500 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002501 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002502 // Calculate new mask.
2503 SmallVector<SDValue, 8> MappedOps;
2504 for (int i = 0; i != MaskNumElts; ++i) {
2505 SDValue Arg = Mask.getOperand(i);
2506 if (Arg.getOpcode() == ISD::UNDEF) {
2507 MappedOps.push_back(Arg);
2508 } else {
2509 int Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
2510 if (Idx < SrcNumElts)
2511 MappedOps.push_back(DAG.getConstant(Idx - StartIdx[0], MaskEltVT));
2512 else {
2513 Idx = Idx - SrcNumElts - StartIdx[1] + MaskNumElts;
2514 MappedOps.push_back(DAG.getConstant(Idx, MaskEltVT));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002515 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002516 }
2517 }
Dale Johannesen66978ee2009-01-31 02:22:37 +00002518 Mask = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002519 Mask.getValueType(),
Mon P Wangc7849c22008-11-16 05:06:27 +00002520 &MappedOps[0], MappedOps.size());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002521 setValue(&I, DAG.getNode(ISD::VECTOR_SHUFFLE, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002522 VT, Src1, Src2, Mask));
Mon P Wangc7849c22008-11-16 05:06:27 +00002523 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002524 }
2525 }
2526
Mon P Wangc7849c22008-11-16 05:06:27 +00002527 // We can't use either concat vectors or extract subvectors so fall back to
2528 // replacing the shuffle with extract and build vector.
2529 // to insert and build vector.
Mon P Wangaeb06d22008-11-10 04:46:22 +00002530 MVT EltVT = VT.getVectorElementType();
2531 MVT PtrVT = TLI.getPointerTy();
2532 SmallVector<SDValue,8> Ops;
Mon P Wangc7849c22008-11-16 05:06:27 +00002533 for (int i = 0; i != MaskNumElts; ++i) {
Mon P Wangaeb06d22008-11-10 04:46:22 +00002534 SDValue Arg = Mask.getOperand(i);
2535 if (Arg.getOpcode() == ISD::UNDEF) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002536 Ops.push_back(DAG.getNode(ISD::UNDEF, getCurDebugLoc(), EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002537 } else {
2538 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Mon P Wangc7849c22008-11-16 05:06:27 +00002539 int Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
2540 if (Idx < SrcNumElts)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002541 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002542 EltVT, Src1, DAG.getConstant(Idx, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002543 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002544 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002545 EltVT, Src2,
Mon P Wangc7849c22008-11-16 05:06:27 +00002546 DAG.getConstant(Idx - SrcNumElts, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002547 }
2548 }
Dale Johannesen66978ee2009-01-31 02:22:37 +00002549 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002550 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002551}
2552
2553void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) {
2554 const Value *Op0 = I.getOperand(0);
2555 const Value *Op1 = I.getOperand(1);
2556 const Type *AggTy = I.getType();
2557 const Type *ValTy = Op1->getType();
2558 bool IntoUndef = isa<UndefValue>(Op0);
2559 bool FromUndef = isa<UndefValue>(Op1);
2560
2561 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2562 I.idx_begin(), I.idx_end());
2563
2564 SmallVector<MVT, 4> AggValueVTs;
2565 ComputeValueVTs(TLI, AggTy, AggValueVTs);
2566 SmallVector<MVT, 4> ValValueVTs;
2567 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2568
2569 unsigned NumAggValues = AggValueVTs.size();
2570 unsigned NumValValues = ValValueVTs.size();
2571 SmallVector<SDValue, 4> Values(NumAggValues);
2572
2573 SDValue Agg = getValue(Op0);
2574 SDValue Val = getValue(Op1);
2575 unsigned i = 0;
2576 // Copy the beginning value(s) from the original aggregate.
2577 for (; i != LinearIndex; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002578 Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002579 AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002580 SDValue(Agg.getNode(), Agg.getResNo() + i);
2581 // Copy values from the inserted value(s).
2582 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002583 Values[i] = FromUndef ? DAG.getNode(ISD::UNDEF, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002584 AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002585 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2586 // Copy remaining value(s) from the original aggregate.
2587 for (; i != NumAggValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002588 Values[i] = IntoUndef ? DAG.getNode(ISD::UNDEF, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002589 AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002590 SDValue(Agg.getNode(), Agg.getResNo() + i);
2591
Dale Johannesen66978ee2009-01-31 02:22:37 +00002592 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002593 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2594 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002595}
2596
2597void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) {
2598 const Value *Op0 = I.getOperand(0);
2599 const Type *AggTy = Op0->getType();
2600 const Type *ValTy = I.getType();
2601 bool OutOfUndef = isa<UndefValue>(Op0);
2602
2603 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2604 I.idx_begin(), I.idx_end());
2605
2606 SmallVector<MVT, 4> ValValueVTs;
2607 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2608
2609 unsigned NumValValues = ValValueVTs.size();
2610 SmallVector<SDValue, 4> Values(NumValValues);
2611
2612 SDValue Agg = getValue(Op0);
2613 // Copy out the selected value(s).
2614 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2615 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002616 OutOfUndef ?
Dale Johannesen66978ee2009-01-31 02:22:37 +00002617 DAG.getNode(ISD::UNDEF, getCurDebugLoc(),
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002618 Agg.getNode()->getValueType(Agg.getResNo() + i)) :
2619 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002620
Dale Johannesen66978ee2009-01-31 02:22:37 +00002621 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002622 DAG.getVTList(&ValValueVTs[0], NumValValues),
2623 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002624}
2625
2626
2627void SelectionDAGLowering::visitGetElementPtr(User &I) {
2628 SDValue N = getValue(I.getOperand(0));
2629 const Type *Ty = I.getOperand(0)->getType();
2630
2631 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2632 OI != E; ++OI) {
2633 Value *Idx = *OI;
2634 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2635 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2636 if (Field) {
2637 // N = N + Offset
2638 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002639 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002640 DAG.getIntPtrConstant(Offset));
2641 }
2642 Ty = StTy->getElementType(Field);
2643 } else {
2644 Ty = cast<SequentialType>(Ty)->getElementType();
2645
2646 // If this is a constant subscript, handle it quickly.
2647 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2648 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002649 uint64_t Offs =
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002650 TD->getTypePaddedSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Dale Johannesen66978ee2009-01-31 02:22:37 +00002651 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002652 DAG.getIntPtrConstant(Offs));
2653 continue;
2654 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002655
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002656 // N = N + Idx * ElementSize;
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002657 uint64_t ElementSize = TD->getTypePaddedSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002658 SDValue IdxN = getValue(Idx);
2659
2660 // If the index is smaller or larger than intptr_t, truncate or extend
2661 // it.
2662 if (IdxN.getValueType().bitsLT(N.getValueType()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00002663 IdxN = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002664 N.getValueType(), IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002665 else if (IdxN.getValueType().bitsGT(N.getValueType()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00002666 IdxN = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002667 N.getValueType(), IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002668
2669 // If this is a multiply by a power of two, turn it into a shl
2670 // immediately. This is a very common case.
2671 if (ElementSize != 1) {
2672 if (isPowerOf2_64(ElementSize)) {
2673 unsigned Amt = Log2_64(ElementSize);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002674 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002675 N.getValueType(), IdxN,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002676 DAG.getConstant(Amt, TLI.getShiftAmountTy()));
2677 } else {
2678 SDValue Scale = DAG.getIntPtrConstant(ElementSize);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002679 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002680 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002681 }
2682 }
2683
Dale Johannesen66978ee2009-01-31 02:22:37 +00002684 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002685 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002686 }
2687 }
2688 setValue(&I, N);
2689}
2690
2691void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2692 // If this is a fixed sized alloca in the entry block of the function,
2693 // allocate it statically on the stack.
2694 if (FuncInfo.StaticAllocaMap.count(&I))
2695 return; // getValue will auto-populate this.
2696
2697 const Type *Ty = I.getAllocatedType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00002698 uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002699 unsigned Align =
2700 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2701 I.getAlignment());
2702
2703 SDValue AllocSize = getValue(I.getArraySize());
2704 MVT IntPtr = TLI.getPointerTy();
2705 if (IntPtr.bitsLT(AllocSize.getValueType()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00002706 AllocSize = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002707 IntPtr, AllocSize);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002708 else if (IntPtr.bitsGT(AllocSize.getValueType()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00002709 AllocSize = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002710 IntPtr, AllocSize);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711
Dale Johannesen66978ee2009-01-31 02:22:37 +00002712 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), IntPtr, AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002713 DAG.getIntPtrConstant(TySize));
2714
2715 // Handle alignment. If the requested alignment is less than or equal to
2716 // the stack alignment, ignore it. If the size is greater than or equal to
2717 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2718 unsigned StackAlign =
2719 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2720 if (Align <= StackAlign)
2721 Align = 0;
2722
2723 // Round the size of the allocation up to the stack alignment size
2724 // by add SA-1 to the size.
Dale Johannesen66978ee2009-01-31 02:22:37 +00002725 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002726 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002727 DAG.getIntPtrConstant(StackAlign-1));
2728 // Mask out the low bits for alignment purposes.
Dale Johannesen66978ee2009-01-31 02:22:37 +00002729 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002730 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002731 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2732
2733 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
2734 const MVT *VTs = DAG.getNodeValueTypes(AllocSize.getValueType(),
2735 MVT::Other);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002736 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002737 VTs, 2, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002738 setValue(&I, DSA);
2739 DAG.setRoot(DSA.getValue(1));
2740
2741 // Inform the Frame Information that we have just allocated a variable-sized
2742 // object.
2743 CurMBB->getParent()->getFrameInfo()->CreateVariableSizedObject();
2744}
2745
2746void SelectionDAGLowering::visitLoad(LoadInst &I) {
2747 const Value *SV = I.getOperand(0);
2748 SDValue Ptr = getValue(SV);
2749
2750 const Type *Ty = I.getType();
2751 bool isVolatile = I.isVolatile();
2752 unsigned Alignment = I.getAlignment();
2753
2754 SmallVector<MVT, 4> ValueVTs;
2755 SmallVector<uint64_t, 4> Offsets;
2756 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2757 unsigned NumValues = ValueVTs.size();
2758 if (NumValues == 0)
2759 return;
2760
2761 SDValue Root;
2762 bool ConstantMemory = false;
2763 if (I.isVolatile())
2764 // Serialize volatile loads with other side effects.
2765 Root = getRoot();
2766 else if (AA->pointsToConstantMemory(SV)) {
2767 // Do not serialize (non-volatile) loads of constant memory with anything.
2768 Root = DAG.getEntryNode();
2769 ConstantMemory = true;
2770 } else {
2771 // Do not serialize non-volatile loads against each other.
2772 Root = DAG.getRoot();
2773 }
2774
2775 SmallVector<SDValue, 4> Values(NumValues);
2776 SmallVector<SDValue, 4> Chains(NumValues);
2777 MVT PtrVT = Ptr.getValueType();
2778 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002779 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
2780 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002781 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002782 DAG.getConstant(Offsets[i], PtrVT)),
2783 SV, Offsets[i],
2784 isVolatile, Alignment);
2785 Values[i] = L;
2786 Chains[i] = L.getValue(1);
2787 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002788
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002789 if (!ConstantMemory) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002790 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002791 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792 &Chains[0], NumValues);
2793 if (isVolatile)
2794 DAG.setRoot(Chain);
2795 else
2796 PendingLoads.push_back(Chain);
2797 }
2798
Dale Johannesen66978ee2009-01-31 02:22:37 +00002799 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002800 DAG.getVTList(&ValueVTs[0], NumValues),
2801 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002802}
2803
2804
2805void SelectionDAGLowering::visitStore(StoreInst &I) {
2806 Value *SrcV = I.getOperand(0);
2807 Value *PtrV = I.getOperand(1);
2808
2809 SmallVector<MVT, 4> ValueVTs;
2810 SmallVector<uint64_t, 4> Offsets;
2811 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2812 unsigned NumValues = ValueVTs.size();
2813 if (NumValues == 0)
2814 return;
2815
2816 // Get the lowered operands. Note that we do this after
2817 // checking if NumResults is zero, because with zero results
2818 // the operands won't have values in the map.
2819 SDValue Src = getValue(SrcV);
2820 SDValue Ptr = getValue(PtrV);
2821
2822 SDValue Root = getRoot();
2823 SmallVector<SDValue, 4> Chains(NumValues);
2824 MVT PtrVT = Ptr.getValueType();
2825 bool isVolatile = I.isVolatile();
2826 unsigned Alignment = I.getAlignment();
2827 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002828 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002829 SDValue(Src.getNode(), Src.getResNo() + i),
Dale Johannesen66978ee2009-01-31 02:22:37 +00002830 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002831 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 DAG.getConstant(Offsets[i], PtrVT)),
2833 PtrV, Offsets[i],
2834 isVolatile, Alignment);
2835
Dale Johannesen66978ee2009-01-31 02:22:37 +00002836 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002837 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002838}
2839
2840/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2841/// node.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002842void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843 unsigned Intrinsic) {
2844 bool HasChain = !I.doesNotAccessMemory();
2845 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2846
2847 // Build the operand list.
2848 SmallVector<SDValue, 8> Ops;
2849 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2850 if (OnlyLoad) {
2851 // We don't need to serialize loads against other loads.
2852 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002853 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854 Ops.push_back(getRoot());
2855 }
2856 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002857
2858 // Info is set by getTgtMemInstrinsic
2859 TargetLowering::IntrinsicInfo Info;
2860 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2861
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002862 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002863 if (!IsTgtIntrinsic)
2864 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002865
2866 // Add all operands of the call to the operand list.
2867 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2868 SDValue Op = getValue(I.getOperand(i));
2869 assert(TLI.isTypeLegal(Op.getValueType()) &&
2870 "Intrinsic uses a non-legal type?");
2871 Ops.push_back(Op);
2872 }
2873
2874 std::vector<MVT> VTs;
2875 if (I.getType() != Type::VoidTy) {
2876 MVT VT = TLI.getValueType(I.getType());
2877 if (VT.isVector()) {
2878 const VectorType *DestTy = cast<VectorType>(I.getType());
2879 MVT EltVT = TLI.getValueType(DestTy->getElementType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002880
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881 VT = MVT::getVectorVT(EltVT, DestTy->getNumElements());
2882 assert(VT != MVT::Other && "Intrinsic uses a non-legal type?");
2883 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002885 assert(TLI.isTypeLegal(VT) && "Intrinsic uses a non-legal type?");
2886 VTs.push_back(VT);
2887 }
2888 if (HasChain)
2889 VTs.push_back(MVT::Other);
2890
2891 const MVT *VTList = DAG.getNodeValueTypes(VTs);
2892
2893 // Create the node.
2894 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002895 if (IsTgtIntrinsic) {
2896 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002897 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002898 VTList, VTs.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002899 &Ops[0], Ops.size(),
2900 Info.memVT, Info.ptrVal, Info.offset,
2901 Info.align, Info.vol,
2902 Info.readMem, Info.writeMem);
2903 }
2904 else if (!HasChain)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002905 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002906 VTList, VTs.size(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002907 &Ops[0], Ops.size());
2908 else if (I.getType() != Type::VoidTy)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002909 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002910 VTList, VTs.size(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002911 &Ops[0], Ops.size());
2912 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002913 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002914 VTList, VTs.size(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002915 &Ops[0], Ops.size());
2916
2917 if (HasChain) {
2918 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2919 if (OnlyLoad)
2920 PendingLoads.push_back(Chain);
2921 else
2922 DAG.setRoot(Chain);
2923 }
2924 if (I.getType() != Type::VoidTy) {
2925 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
2926 MVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002927 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002928 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002929 setValue(&I, Result);
2930 }
2931}
2932
2933/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
2934static GlobalVariable *ExtractTypeInfo(Value *V) {
2935 V = V->stripPointerCasts();
2936 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
2937 assert ((GV || isa<ConstantPointerNull>(V)) &&
2938 "TypeInfo must be a global variable or NULL");
2939 return GV;
2940}
2941
2942namespace llvm {
2943
2944/// AddCatchInfo - Extract the personality and type infos from an eh.selector
2945/// call, and add them to the specified machine basic block.
2946void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
2947 MachineBasicBlock *MBB) {
2948 // Inform the MachineModuleInfo of the personality for this landing pad.
2949 ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
2950 assert(CE->getOpcode() == Instruction::BitCast &&
2951 isa<Function>(CE->getOperand(0)) &&
2952 "Personality should be a function");
2953 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
2954
2955 // Gather all the type infos for this landing pad and pass them along to
2956 // MachineModuleInfo.
2957 std::vector<GlobalVariable *> TyInfo;
2958 unsigned N = I.getNumOperands();
2959
2960 for (unsigned i = N - 1; i > 2; --i) {
2961 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
2962 unsigned FilterLength = CI->getZExtValue();
2963 unsigned FirstCatch = i + FilterLength + !FilterLength;
2964 assert (FirstCatch <= N && "Invalid filter length");
2965
2966 if (FirstCatch < N) {
2967 TyInfo.reserve(N - FirstCatch);
2968 for (unsigned j = FirstCatch; j < N; ++j)
2969 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2970 MMI->addCatchTypeInfo(MBB, TyInfo);
2971 TyInfo.clear();
2972 }
2973
2974 if (!FilterLength) {
2975 // Cleanup.
2976 MMI->addCleanup(MBB);
2977 } else {
2978 // Filter.
2979 TyInfo.reserve(FilterLength - 1);
2980 for (unsigned j = i + 1; j < FirstCatch; ++j)
2981 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2982 MMI->addFilterTypeInfo(MBB, TyInfo);
2983 TyInfo.clear();
2984 }
2985
2986 N = i;
2987 }
2988 }
2989
2990 if (N > 3) {
2991 TyInfo.reserve(N - 3);
2992 for (unsigned j = 3; j < N; ++j)
2993 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
2994 MMI->addCatchTypeInfo(MBB, TyInfo);
2995 }
2996}
2997
2998}
2999
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003000/// GetSignificand - Get the significand and build it into a floating-point
3001/// number with exponent of 1:
3002///
3003/// Op = (Op & 0x007fffff) | 0x3f800000;
3004///
3005/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003006static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003007GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
3008 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
Bill Wendlinge9a72862009-01-20 21:17:57 +00003009 DAG.getConstant(0x007fffff, MVT::i32));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003010 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
Bill Wendlinge9a72862009-01-20 21:17:57 +00003011 DAG.getConstant(0x3f800000, MVT::i32));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003012 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003013}
3014
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003015/// GetExponent - Get the exponent:
3016///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003017/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003018///
3019/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003020static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003021GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
3022 DebugLoc dl) {
3023 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
Bill Wendlinge9a72862009-01-20 21:17:57 +00003024 DAG.getConstant(0x7f800000, MVT::i32));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003025 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Bill Wendlinge9a72862009-01-20 21:17:57 +00003026 DAG.getConstant(23, TLI.getShiftAmountTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003027 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
Bill Wendlinge9a72862009-01-20 21:17:57 +00003028 DAG.getConstant(127, MVT::i32));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003029 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003030}
3031
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003032/// getF32Constant - Get 32-bit floating point constant.
3033static SDValue
3034getF32Constant(SelectionDAG &DAG, unsigned Flt) {
3035 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
3036}
3037
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003038/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003039/// visitIntrinsicCall: I is a call instruction
3040/// Op is the associated NodeType for I
3041const char *
3042SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003043 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003044 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003045 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003046 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003047 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003048 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003049 getValue(I.getOperand(2)),
3050 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003051 setValue(&I, L);
3052 DAG.setRoot(L.getValue(1));
3053 return 0;
3054}
3055
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003056// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003057const char *
3058SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003059 SDValue Op1 = getValue(I.getOperand(1));
3060 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003061
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003062 MVT ValueVTs[] = { Op1.getValueType(), MVT::i1 };
3063 SDValue Ops[] = { Op1, Op2 };
Bill Wendling74c37652008-12-09 22:08:41 +00003064
Dale Johannesen66978ee2009-01-31 02:22:37 +00003065 SDValue Result = DAG.getNode(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003066 DAG.getVTList(&ValueVTs[0], 2), &Ops[0], 2);
Bill Wendling74c37652008-12-09 22:08:41 +00003067
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003068 setValue(&I, Result);
3069 return 0;
3070}
Bill Wendling74c37652008-12-09 22:08:41 +00003071
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003072/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3073/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003074void
3075SelectionDAGLowering::visitExp(CallInst &I) {
3076 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003077 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003078
3079 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3080 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3081 SDValue Op = getValue(I.getOperand(1));
3082
3083 // Put the exponent in the right bit position for later addition to the
3084 // final result:
3085 //
3086 // #define LOG2OFe 1.4426950f
3087 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003088 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003089 getF32Constant(DAG, 0x3fb8aa3b));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003090 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003091
3092 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003093 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3094 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003095
3096 // IntegerPartOfX <<= 23;
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003097 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Bill Wendling6c533342009-01-20 06:10:42 +00003098 DAG.getConstant(23, TLI.getShiftAmountTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003099
3100 if (LimitFloatPrecision <= 6) {
3101 // For floating-point precision of 6:
3102 //
3103 // TwoToFractionalPartOfX =
3104 // 0.997535578f +
3105 // (0.735607626f + 0.252464424f * x) * x;
3106 //
3107 // error 0.0144103317, which is 6 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003108 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003109 getF32Constant(DAG, 0x3e814304));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003110 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003111 getF32Constant(DAG, 0x3f3c50c8));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003112 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3113 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003114 getF32Constant(DAG, 0x3f7f5e7e));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003115 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003116
3117 // Add the exponent into the result in integer domain.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003118 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003119 TwoToFracPartOfX, IntegerPartOfX);
3120
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003121 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003122 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3123 // For floating-point precision of 12:
3124 //
3125 // TwoToFractionalPartOfX =
3126 // 0.999892986f +
3127 // (0.696457318f +
3128 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3129 //
3130 // 0.000107046256 error, which is 13 to 14 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003131 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003132 getF32Constant(DAG, 0x3da235e3));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003133 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003134 getF32Constant(DAG, 0x3e65b8f3));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003135 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3136 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003137 getF32Constant(DAG, 0x3f324b07));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003138 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3139 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003140 getF32Constant(DAG, 0x3f7ff8fd));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003141 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003142
3143 // Add the exponent into the result in integer domain.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003144 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003145 TwoToFracPartOfX, IntegerPartOfX);
3146
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003147 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003148 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3149 // For floating-point precision of 18:
3150 //
3151 // TwoToFractionalPartOfX =
3152 // 0.999999982f +
3153 // (0.693148872f +
3154 // (0.240227044f +
3155 // (0.554906021e-1f +
3156 // (0.961591928e-2f +
3157 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3158 //
3159 // error 2.47208000*10^(-7), which is better than 18 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003160 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003161 getF32Constant(DAG, 0x3924b03e));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003162 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003163 getF32Constant(DAG, 0x3ab24b87));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003164 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3165 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003166 getF32Constant(DAG, 0x3c1d8c17));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003167 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3168 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003169 getF32Constant(DAG, 0x3d634a1d));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003170 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3171 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003172 getF32Constant(DAG, 0x3e75fe14));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003173 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3174 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003175 getF32Constant(DAG, 0x3f317234));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003176 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3177 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003178 getF32Constant(DAG, 0x3f800000));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003179 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
3180 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003181
3182 // Add the exponent into the result in integer domain.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003183 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003184 TwoToFracPartOfX, IntegerPartOfX);
3185
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003186 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003187 }
3188 } else {
3189 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003190 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003191 getValue(I.getOperand(1)).getValueType(),
3192 getValue(I.getOperand(1)));
3193 }
3194
Dale Johannesen59e577f2008-09-05 18:38:42 +00003195 setValue(&I, result);
3196}
3197
Bill Wendling39150252008-09-09 20:39:27 +00003198/// visitLog - Lower a log intrinsic. Handles the special sequences for
3199/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003200void
3201SelectionDAGLowering::visitLog(CallInst &I) {
3202 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003203 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003204
3205 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
3206 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3207 SDValue Op = getValue(I.getOperand(1));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003208 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003209
3210 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003211 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003212 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003213 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003214
3215 // Get the significand and build it into a floating-point number with
3216 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003217 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003218
3219 if (LimitFloatPrecision <= 6) {
3220 // For floating-point precision of 6:
3221 //
3222 // LogofMantissa =
3223 // -1.1609546f +
3224 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003225 //
Bill Wendling39150252008-09-09 20:39:27 +00003226 // error 0.0034276066, which is better than 8 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003227 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003228 getF32Constant(DAG, 0xbe74c456));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003229 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003230 getF32Constant(DAG, 0x3fb3a2b1));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003231 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3232 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003233 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003234
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003235 result = DAG.getNode(ISD::FADD, dl,
3236 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003237 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3238 // For floating-point precision of 12:
3239 //
3240 // LogOfMantissa =
3241 // -1.7417939f +
3242 // (2.8212026f +
3243 // (-1.4699568f +
3244 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3245 //
3246 // error 0.000061011436, which is 14 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003247 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003248 getF32Constant(DAG, 0xbd67b6d6));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003249 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003250 getF32Constant(DAG, 0x3ee4f4b8));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003251 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3252 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003253 getF32Constant(DAG, 0x3fbc278b));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003254 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3255 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003256 getF32Constant(DAG, 0x40348e95));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003257 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3258 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003259 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003260
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003261 result = DAG.getNode(ISD::FADD, dl,
3262 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003263 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3264 // For floating-point precision of 18:
3265 //
3266 // LogOfMantissa =
3267 // -2.1072184f +
3268 // (4.2372794f +
3269 // (-3.7029485f +
3270 // (2.2781945f +
3271 // (-0.87823314f +
3272 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3273 //
3274 // error 0.0000023660568, which is better than 18 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003275 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003276 getF32Constant(DAG, 0xbc91e5ac));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003277 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003278 getF32Constant(DAG, 0x3e4350aa));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003279 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3280 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003281 getF32Constant(DAG, 0x3f60d3e3));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003282 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3283 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003284 getF32Constant(DAG, 0x4011cdf0));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003285 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3286 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003287 getF32Constant(DAG, 0x406cfd1c));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003288 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3289 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003290 getF32Constant(DAG, 0x408797cb));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003291 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3292 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003293 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003294
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003295 result = DAG.getNode(ISD::FADD, dl,
3296 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003297 }
3298 } else {
3299 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003300 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003301 getValue(I.getOperand(1)).getValueType(),
3302 getValue(I.getOperand(1)));
3303 }
3304
Dale Johannesen59e577f2008-09-05 18:38:42 +00003305 setValue(&I, result);
3306}
3307
Bill Wendling3eb59402008-09-09 00:28:24 +00003308/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3309/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003310void
3311SelectionDAGLowering::visitLog2(CallInst &I) {
3312 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003313 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003314
Dale Johannesen853244f2008-09-05 23:49:37 +00003315 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003316 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3317 SDValue Op = getValue(I.getOperand(1));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003318 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003319
Bill Wendling39150252008-09-09 20:39:27 +00003320 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003321 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003322
3323 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003324 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003325 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003326
Bill Wendling3eb59402008-09-09 00:28:24 +00003327 // Different possible minimax approximations of significand in
3328 // floating-point for various degrees of accuracy over [1,2].
3329 if (LimitFloatPrecision <= 6) {
3330 // For floating-point precision of 6:
3331 //
3332 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3333 //
3334 // error 0.0049451742, which is more than 7 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003335 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003336 getF32Constant(DAG, 0xbeb08fe0));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003337 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003338 getF32Constant(DAG, 0x40019463));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003339 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3340 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003341 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003342
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003343 result = DAG.getNode(ISD::FADD, dl,
3344 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003345 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3346 // For floating-point precision of 12:
3347 //
3348 // Log2ofMantissa =
3349 // -2.51285454f +
3350 // (4.07009056f +
3351 // (-2.12067489f +
3352 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003353 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003354 // error 0.0000876136000, which is better than 13 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003355 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003356 getF32Constant(DAG, 0xbda7262e));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003357 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003358 getF32Constant(DAG, 0x3f25280b));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003359 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3360 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003361 getF32Constant(DAG, 0x4007b923));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003362 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3363 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003364 getF32Constant(DAG, 0x40823e2f));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003365 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3366 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003367 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003368
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003369 result = DAG.getNode(ISD::FADD, dl,
3370 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003371 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3372 // For floating-point precision of 18:
3373 //
3374 // Log2ofMantissa =
3375 // -3.0400495f +
3376 // (6.1129976f +
3377 // (-5.3420409f +
3378 // (3.2865683f +
3379 // (-1.2669343f +
3380 // (0.27515199f -
3381 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3382 //
3383 // error 0.0000018516, which is better than 18 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003384 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003385 getF32Constant(DAG, 0xbcd2769e));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003386 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003387 getF32Constant(DAG, 0x3e8ce0b9));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003388 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3389 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003390 getF32Constant(DAG, 0x3fa22ae7));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003391 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3392 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003393 getF32Constant(DAG, 0x40525723));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003394 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3395 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003396 getF32Constant(DAG, 0x40aaf200));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003397 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3398 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003399 getF32Constant(DAG, 0x40c39dad));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003400 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3401 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003402 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003403
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003404 result = DAG.getNode(ISD::FADD, dl,
3405 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003406 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003407 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003408 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003409 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003410 getValue(I.getOperand(1)).getValueType(),
3411 getValue(I.getOperand(1)));
3412 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003413
Dale Johannesen59e577f2008-09-05 18:38:42 +00003414 setValue(&I, result);
3415}
3416
Bill Wendling3eb59402008-09-09 00:28:24 +00003417/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3418/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003419void
3420SelectionDAGLowering::visitLog10(CallInst &I) {
3421 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003422 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003423
Dale Johannesen852680a2008-09-05 21:27:19 +00003424 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003425 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3426 SDValue Op = getValue(I.getOperand(1));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003427 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003428
Bill Wendling39150252008-09-09 20:39:27 +00003429 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003430 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003431 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003432 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003433
3434 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003435 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003436 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003437
3438 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003439 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003440 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003441 // Log10ofMantissa =
3442 // -0.50419619f +
3443 // (0.60948995f - 0.10380950f * x) * x;
3444 //
3445 // error 0.0014886165, which is 6 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003446 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003447 getF32Constant(DAG, 0xbdd49a13));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003448 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003449 getF32Constant(DAG, 0x3f1c0789));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003450 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3451 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003453
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003454 result = DAG.getNode(ISD::FADD, dl,
3455 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003456 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3457 // For floating-point precision of 12:
3458 //
3459 // Log10ofMantissa =
3460 // -0.64831180f +
3461 // (0.91751397f +
3462 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3463 //
3464 // error 0.00019228036, which is better than 12 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003465 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003466 getF32Constant(DAG, 0x3d431f31));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003467 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003468 getF32Constant(DAG, 0x3ea21fb2));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003469 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3470 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003471 getF32Constant(DAG, 0x3f6ae232));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003472 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3473 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003474 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003475
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003476 result = DAG.getNode(ISD::FADD, dl,
3477 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003478 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003479 // For floating-point precision of 18:
3480 //
3481 // Log10ofMantissa =
3482 // -0.84299375f +
3483 // (1.5327582f +
3484 // (-1.0688956f +
3485 // (0.49102474f +
3486 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3487 //
3488 // error 0.0000037995730, which is better than 18 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003489 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003490 getF32Constant(DAG, 0x3c5d51ce));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003491 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003492 getF32Constant(DAG, 0x3e00685a));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003493 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3494 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003495 getF32Constant(DAG, 0x3efb6798));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003496 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3497 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003498 getF32Constant(DAG, 0x3f88d192));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003499 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3500 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003501 getF32Constant(DAG, 0x3fc4316c));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003502 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3503 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003504 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003505
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003506 result = DAG.getNode(ISD::FADD, dl,
3507 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003508 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003509 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003510 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003511 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003512 getValue(I.getOperand(1)).getValueType(),
3513 getValue(I.getOperand(1)));
3514 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003515
Dale Johannesen59e577f2008-09-05 18:38:42 +00003516 setValue(&I, result);
3517}
3518
Bill Wendlinge10c8142008-09-09 22:39:21 +00003519/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3520/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003521void
3522SelectionDAGLowering::visitExp2(CallInst &I) {
3523 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003524 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003525
Dale Johannesen601d3c02008-09-05 01:48:15 +00003526 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003527 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3528 SDValue Op = getValue(I.getOperand(1));
3529
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003530 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003531
3532 // FractionalPartOfX = x - (float)IntegerPartOfX;
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003533 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3534 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003535
3536 // IntegerPartOfX <<= 23;
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003537 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Bill Wendling6c533342009-01-20 06:10:42 +00003538 DAG.getConstant(23, TLI.getShiftAmountTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003539
3540 if (LimitFloatPrecision <= 6) {
3541 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003542 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003543 // TwoToFractionalPartOfX =
3544 // 0.997535578f +
3545 // (0.735607626f + 0.252464424f * x) * x;
3546 //
3547 // error 0.0144103317, which is 6 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003548 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003549 getF32Constant(DAG, 0x3e814304));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003550 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003551 getF32Constant(DAG, 0x3f3c50c8));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003552 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3553 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003554 getF32Constant(DAG, 0x3f7f5e7e));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003555 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003556 SDValue TwoToFractionalPartOfX =
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003557 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003558
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003559 result = DAG.getNode(ISD::BIT_CONVERT, dl,
3560 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003561 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3562 // For floating-point precision of 12:
3563 //
3564 // TwoToFractionalPartOfX =
3565 // 0.999892986f +
3566 // (0.696457318f +
3567 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3568 //
3569 // error 0.000107046256, which is 13 to 14 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003570 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003571 getF32Constant(DAG, 0x3da235e3));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003572 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003573 getF32Constant(DAG, 0x3e65b8f3));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003574 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3575 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003576 getF32Constant(DAG, 0x3f324b07));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003577 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3578 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003579 getF32Constant(DAG, 0x3f7ff8fd));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003580 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003581 SDValue TwoToFractionalPartOfX =
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003582 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003583
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003584 result = DAG.getNode(ISD::BIT_CONVERT, dl,
3585 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003586 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3587 // For floating-point precision of 18:
3588 //
3589 // TwoToFractionalPartOfX =
3590 // 0.999999982f +
3591 // (0.693148872f +
3592 // (0.240227044f +
3593 // (0.554906021e-1f +
3594 // (0.961591928e-2f +
3595 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3596 // error 2.47208000*10^(-7), which is better than 18 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003597 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003598 getF32Constant(DAG, 0x3924b03e));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003599 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003600 getF32Constant(DAG, 0x3ab24b87));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003601 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3602 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003603 getF32Constant(DAG, 0x3c1d8c17));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003604 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3605 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003606 getF32Constant(DAG, 0x3d634a1d));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003607 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3608 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003609 getF32Constant(DAG, 0x3e75fe14));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003610 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3611 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003612 getF32Constant(DAG, 0x3f317234));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003613 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3614 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003615 getF32Constant(DAG, 0x3f800000));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003616 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003617 SDValue TwoToFractionalPartOfX =
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003618 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003619
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003620 result = DAG.getNode(ISD::BIT_CONVERT, dl,
3621 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003622 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003623 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003624 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003625 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003626 getValue(I.getOperand(1)).getValueType(),
3627 getValue(I.getOperand(1)));
3628 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003629
Dale Johannesen601d3c02008-09-05 01:48:15 +00003630 setValue(&I, result);
3631}
3632
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003633/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3634/// limited-precision mode with x == 10.0f.
3635void
3636SelectionDAGLowering::visitPow(CallInst &I) {
3637 SDValue result;
3638 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003639 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003640 bool IsExp10 = false;
3641
3642 if (getValue(Val).getValueType() == MVT::f32 &&
Bill Wendling277fc242008-09-10 00:24:59 +00003643 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003644 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3645 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3646 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3647 APFloat Ten(10.0f);
3648 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3649 }
3650 }
3651 }
3652
3653 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3654 SDValue Op = getValue(I.getOperand(2));
3655
3656 // Put the exponent in the right bit position for later addition to the
3657 // final result:
3658 //
3659 // #define LOG2OF10 3.3219281f
3660 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003661 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003662 getF32Constant(DAG, 0x40549a78));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003663 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003664
3665 // FractionalPartOfX = x - (float)IntegerPartOfX;
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003666 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3667 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003668
3669 // IntegerPartOfX <<= 23;
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003670 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Bill Wendling6c533342009-01-20 06:10:42 +00003671 DAG.getConstant(23, TLI.getShiftAmountTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003672
3673 if (LimitFloatPrecision <= 6) {
3674 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003675 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003676 // twoToFractionalPartOfX =
3677 // 0.997535578f +
3678 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003679 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003680 // error 0.0144103317, which is 6 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003681 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003682 getF32Constant(DAG, 0x3e814304));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003683 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003684 getF32Constant(DAG, 0x3f3c50c8));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003685 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3686 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003687 getF32Constant(DAG, 0x3f7f5e7e));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003688 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003689 SDValue TwoToFractionalPartOfX =
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003690 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003691
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003692 result = DAG.getNode(ISD::BIT_CONVERT, dl,
3693 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003694 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3695 // For floating-point precision of 12:
3696 //
3697 // TwoToFractionalPartOfX =
3698 // 0.999892986f +
3699 // (0.696457318f +
3700 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3701 //
3702 // error 0.000107046256, which is 13 to 14 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003703 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003704 getF32Constant(DAG, 0x3da235e3));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003705 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003706 getF32Constant(DAG, 0x3e65b8f3));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003707 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3708 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003709 getF32Constant(DAG, 0x3f324b07));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003710 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3711 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003712 getF32Constant(DAG, 0x3f7ff8fd));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003713 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003714 SDValue TwoToFractionalPartOfX =
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003715 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003716
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003717 result = DAG.getNode(ISD::BIT_CONVERT, dl,
3718 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003719 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3720 // For floating-point precision of 18:
3721 //
3722 // TwoToFractionalPartOfX =
3723 // 0.999999982f +
3724 // (0.693148872f +
3725 // (0.240227044f +
3726 // (0.554906021e-1f +
3727 // (0.961591928e-2f +
3728 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3729 // error 2.47208000*10^(-7), which is better than 18 bits
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003730 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003731 getF32Constant(DAG, 0x3924b03e));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003732 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003733 getF32Constant(DAG, 0x3ab24b87));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003734 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3735 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003736 getF32Constant(DAG, 0x3c1d8c17));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003737 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3738 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003739 getF32Constant(DAG, 0x3d634a1d));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003740 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3741 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003742 getF32Constant(DAG, 0x3e75fe14));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003743 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3744 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003745 getF32Constant(DAG, 0x3f317234));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003746 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3747 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003748 getF32Constant(DAG, 0x3f800000));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003749 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003750 SDValue TwoToFractionalPartOfX =
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003751 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003752
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003753 result = DAG.getNode(ISD::BIT_CONVERT, dl,
3754 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003755 }
3756 } else {
3757 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003758 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003759 getValue(I.getOperand(1)).getValueType(),
3760 getValue(I.getOperand(1)),
3761 getValue(I.getOperand(2)));
3762 }
3763
3764 setValue(&I, result);
3765}
3766
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003767/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3768/// we want to emit this as a call to a named external function, return the name
3769/// otherwise lower it and return null.
3770const char *
3771SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003772 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003773 switch (Intrinsic) {
3774 default:
3775 // By default, turn this into a target intrinsic node.
3776 visitTargetIntrinsic(I, Intrinsic);
3777 return 0;
3778 case Intrinsic::vastart: visitVAStart(I); return 0;
3779 case Intrinsic::vaend: visitVAEnd(I); return 0;
3780 case Intrinsic::vacopy: visitVACopy(I); return 0;
3781 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003782 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003783 getValue(I.getOperand(1))));
3784 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003785 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003786 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003787 getValue(I.getOperand(1))));
3788 return 0;
3789 case Intrinsic::setjmp:
3790 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3791 break;
3792 case Intrinsic::longjmp:
3793 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3794 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003795 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003796 SDValue Op1 = getValue(I.getOperand(1));
3797 SDValue Op2 = getValue(I.getOperand(2));
3798 SDValue Op3 = getValue(I.getOperand(3));
3799 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3800 DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
3801 I.getOperand(1), 0, I.getOperand(2), 0));
3802 return 0;
3803 }
Chris Lattner824b9582008-11-21 16:42:48 +00003804 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003805 SDValue Op1 = getValue(I.getOperand(1));
3806 SDValue Op2 = getValue(I.getOperand(2));
3807 SDValue Op3 = getValue(I.getOperand(3));
3808 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3809 DAG.setRoot(DAG.getMemset(getRoot(), Op1, Op2, Op3, Align,
3810 I.getOperand(1), 0));
3811 return 0;
3812 }
Chris Lattner824b9582008-11-21 16:42:48 +00003813 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003814 SDValue Op1 = getValue(I.getOperand(1));
3815 SDValue Op2 = getValue(I.getOperand(2));
3816 SDValue Op3 = getValue(I.getOperand(3));
3817 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3818
3819 // If the source and destination are known to not be aliases, we can
3820 // lower memmove as memcpy.
3821 uint64_t Size = -1ULL;
3822 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003823 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003824 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3825 AliasAnalysis::NoAlias) {
3826 DAG.setRoot(DAG.getMemcpy(getRoot(), Op1, Op2, Op3, Align, false,
3827 I.getOperand(1), 0, I.getOperand(2), 0));
3828 return 0;
3829 }
3830
3831 DAG.setRoot(DAG.getMemmove(getRoot(), Op1, Op2, Op3, Align,
3832 I.getOperand(1), 0, I.getOperand(2), 0));
3833 return 0;
3834 }
3835 case Intrinsic::dbg_stoppoint: {
Devang Patel83489bb2009-01-13 00:35:13 +00003836 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003837 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003838 if (DW && DW->ValidDebugInfo(SPI.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003839 DAG.setRoot(DAG.getDbgStopPoint(getRoot(),
3840 SPI.getLine(),
3841 SPI.getColumn(),
Devang Patel83489bb2009-01-13 00:35:13 +00003842 SPI.getContext()));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003843 DICompileUnit CU(cast<GlobalVariable>(SPI.getContext()));
3844 unsigned SrcFile = DW->RecordSource(CU.getDirectory(), CU.getFilename());
3845 unsigned idx = DAG.getMachineFunction().
3846 getOrCreateDebugLocID(SrcFile,
3847 SPI.getLine(),
3848 SPI.getColumn());
Dale Johannesen66978ee2009-01-31 02:22:37 +00003849 setCurDebugLoc(DebugLoc::get(idx));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003850 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003851 return 0;
3852 }
3853 case Intrinsic::dbg_region_start: {
Devang Patel83489bb2009-01-13 00:35:13 +00003854 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003855 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
Devang Patelb79b5352009-01-19 23:21:49 +00003856 if (DW && DW->ValidDebugInfo(RSI.getContext())) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003857 unsigned LabelID =
Devang Patel83489bb2009-01-13 00:35:13 +00003858 DW->RecordRegionStart(cast<GlobalVariable>(RSI.getContext()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003859 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
3860 }
3861
3862 return 0;
3863 }
3864 case Intrinsic::dbg_region_end: {
Devang Patel83489bb2009-01-13 00:35:13 +00003865 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003866 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
Devang Patelb79b5352009-01-19 23:21:49 +00003867 if (DW && DW->ValidDebugInfo(REI.getContext())) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003868 unsigned LabelID =
Devang Patel83489bb2009-01-13 00:35:13 +00003869 DW->RecordRegionEnd(cast<GlobalVariable>(REI.getContext()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003870 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
3871 }
3872
3873 return 0;
3874 }
3875 case Intrinsic::dbg_func_start: {
Devang Patel83489bb2009-01-13 00:35:13 +00003876 DwarfWriter *DW = DAG.getDwarfWriter();
3877 if (!DW) return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003878 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
3879 Value *SP = FSI.getSubprogram();
Devang Patelcf3a4482009-01-15 23:41:32 +00003880 if (SP && DW->ValidDebugInfo(SP)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003881 // llvm.dbg.func.start implicitly defines a dbg_stoppoint which is
3882 // what (most?) gdb expects.
Devang Patel83489bb2009-01-13 00:35:13 +00003883 DISubprogram Subprogram(cast<GlobalVariable>(SP));
3884 DICompileUnit CompileUnit = Subprogram.getCompileUnit();
3885 unsigned SrcFile = DW->RecordSource(CompileUnit.getDirectory(),
3886 CompileUnit.getFilename());
Devang Patel20dd0462008-11-06 00:30:09 +00003887 // Record the source line but does not create a label for the normal
3888 // function start. It will be emitted at asm emission time. However,
3889 // create a label if this is a beginning of inlined function.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003890 unsigned Line = Subprogram.getLineNumber();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003891 unsigned LabelID =
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003892 DW->RecordSourceLine(Line, 0, SrcFile);
Devang Patel83489bb2009-01-13 00:35:13 +00003893 if (DW->getRecordSourceLineCount() != 1)
Devang Patel20dd0462008-11-06 00:30:09 +00003894 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getRoot(), LabelID));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003895 setCurDebugLoc(DebugLoc::get(DAG.getMachineFunction().
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003896 getOrCreateDebugLocID(SrcFile, Line, 0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003897 }
3898
3899 return 0;
3900 }
3901 case Intrinsic::dbg_declare: {
Devang Patel83489bb2009-01-13 00:35:13 +00003902 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003903 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3904 Value *Variable = DI.getVariable();
Devang Patelb79b5352009-01-19 23:21:49 +00003905 if (DW && DW->ValidDebugInfo(Variable))
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003906 DAG.setRoot(DAG.getNode(ISD::DECLARE, dl, MVT::Other, getRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003907 getValue(DI.getAddress()), getValue(Variable)));
3908 return 0;
3909 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003910
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003911 case Intrinsic::eh_exception: {
3912 if (!CurMBB->isLandingPad()) {
3913 // FIXME: Mark exception register as live in. Hack for PR1508.
3914 unsigned Reg = TLI.getExceptionAddressRegister();
3915 if (Reg) CurMBB->addLiveIn(Reg);
3916 }
3917 // Insert the EXCEPTIONADDR instruction.
3918 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3919 SDValue Ops[1];
3920 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003921 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003922 setValue(&I, Op);
3923 DAG.setRoot(Op.getValue(1));
3924 return 0;
3925 }
3926
3927 case Intrinsic::eh_selector_i32:
3928 case Intrinsic::eh_selector_i64: {
3929 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3930 MVT VT = (Intrinsic == Intrinsic::eh_selector_i32 ?
3931 MVT::i32 : MVT::i64);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003932
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003933 if (MMI) {
3934 if (CurMBB->isLandingPad())
3935 AddCatchInfo(I, MMI, CurMBB);
3936 else {
3937#ifndef NDEBUG
3938 FuncInfo.CatchInfoLost.insert(&I);
3939#endif
3940 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3941 unsigned Reg = TLI.getExceptionSelectorRegister();
3942 if (Reg) CurMBB->addLiveIn(Reg);
3943 }
3944
3945 // Insert the EHSELECTION instruction.
3946 SDVTList VTs = DAG.getVTList(VT, MVT::Other);
3947 SDValue Ops[2];
3948 Ops[0] = getValue(I.getOperand(1));
3949 Ops[1] = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003950 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003951 setValue(&I, Op);
3952 DAG.setRoot(Op.getValue(1));
3953 } else {
3954 setValue(&I, DAG.getConstant(0, VT));
3955 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003956
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003957 return 0;
3958 }
3959
3960 case Intrinsic::eh_typeid_for_i32:
3961 case Intrinsic::eh_typeid_for_i64: {
3962 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
3963 MVT VT = (Intrinsic == Intrinsic::eh_typeid_for_i32 ?
3964 MVT::i32 : MVT::i64);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003965
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003966 if (MMI) {
3967 // Find the type id for the given typeinfo.
3968 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3969
3970 unsigned TypeID = MMI->getTypeIDFor(GV);
3971 setValue(&I, DAG.getConstant(TypeID, VT));
3972 } else {
3973 // Return something different to eh_selector.
3974 setValue(&I, DAG.getConstant(1, VT));
3975 }
3976
3977 return 0;
3978 }
3979
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003980 case Intrinsic::eh_return_i32:
3981 case Intrinsic::eh_return_i64:
3982 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003983 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003984 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003985 MVT::Other,
3986 getControlRoot(),
3987 getValue(I.getOperand(1)),
3988 getValue(I.getOperand(2))));
3989 } else {
3990 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
3991 }
3992
3993 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003994 case Intrinsic::eh_unwind_init:
3995 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3996 MMI->setCallsUnwindInit(true);
3997 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003998
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003999 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004000
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004001 case Intrinsic::eh_dwarf_cfa: {
4002 MVT VT = getValue(I.getOperand(1)).getValueType();
4003 SDValue CfaArg;
4004 if (VT.bitsGT(TLI.getPointerTy()))
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004005 CfaArg = DAG.getNode(ISD::TRUNCATE, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004006 TLI.getPointerTy(), getValue(I.getOperand(1)));
4007 else
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004008 CfaArg = DAG.getNode(ISD::SIGN_EXTEND, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004009 TLI.getPointerTy(), getValue(I.getOperand(1)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004010
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004011 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004012 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004013 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004014 TLI.getPointerTy()),
4015 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004016 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004017 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004018 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004019 TLI.getPointerTy(),
4020 DAG.getConstant(0,
4021 TLI.getPointerTy())),
4022 Offset));
4023 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004024 }
4025
Mon P Wang77cdf302008-11-10 20:54:11 +00004026 case Intrinsic::convertff:
4027 case Intrinsic::convertfsi:
4028 case Intrinsic::convertfui:
4029 case Intrinsic::convertsif:
4030 case Intrinsic::convertuif:
4031 case Intrinsic::convertss:
4032 case Intrinsic::convertsu:
4033 case Intrinsic::convertus:
4034 case Intrinsic::convertuu: {
4035 ISD::CvtCode Code = ISD::CVT_INVALID;
4036 switch (Intrinsic) {
4037 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4038 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4039 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4040 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4041 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4042 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4043 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4044 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4045 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4046 }
4047 MVT DestVT = TLI.getValueType(I.getType());
4048 Value* Op1 = I.getOperand(1);
4049 setValue(&I, DAG.getConvertRndSat(DestVT, getValue(Op1),
4050 DAG.getValueType(DestVT),
4051 DAG.getValueType(getValue(Op1).getValueType()),
4052 getValue(I.getOperand(2)),
4053 getValue(I.getOperand(3)),
4054 Code));
4055 return 0;
4056 }
4057
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004058 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004059 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004060 getValue(I.getOperand(1)).getValueType(),
4061 getValue(I.getOperand(1))));
4062 return 0;
4063 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004064 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004065 getValue(I.getOperand(1)).getValueType(),
4066 getValue(I.getOperand(1)),
4067 getValue(I.getOperand(2))));
4068 return 0;
4069 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004070 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004071 getValue(I.getOperand(1)).getValueType(),
4072 getValue(I.getOperand(1))));
4073 return 0;
4074 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004075 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004076 getValue(I.getOperand(1)).getValueType(),
4077 getValue(I.getOperand(1))));
4078 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004079 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004080 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004081 return 0;
4082 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004083 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004084 return 0;
4085 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004086 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004087 return 0;
4088 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004089 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004090 return 0;
4091 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004092 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004093 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004094 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004095 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004096 return 0;
4097 case Intrinsic::pcmarker: {
4098 SDValue Tmp = getValue(I.getOperand(1));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004099 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004100 return 0;
4101 }
4102 case Intrinsic::readcyclecounter: {
4103 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004104 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004105 DAG.getNodeValueTypes(MVT::i64, MVT::Other), 2,
4106 &Op, 1);
4107 setValue(&I, Tmp);
4108 DAG.setRoot(Tmp.getValue(1));
4109 return 0;
4110 }
4111 case Intrinsic::part_select: {
4112 // Currently not implemented: just abort
4113 assert(0 && "part_select intrinsic not implemented");
4114 abort();
4115 }
4116 case Intrinsic::part_set: {
4117 // Currently not implemented: just abort
4118 assert(0 && "part_set intrinsic not implemented");
4119 abort();
4120 }
4121 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004122 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004123 getValue(I.getOperand(1)).getValueType(),
4124 getValue(I.getOperand(1))));
4125 return 0;
4126 case Intrinsic::cttz: {
4127 SDValue Arg = getValue(I.getOperand(1));
4128 MVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004129 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004130 setValue(&I, result);
4131 return 0;
4132 }
4133 case Intrinsic::ctlz: {
4134 SDValue Arg = getValue(I.getOperand(1));
4135 MVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004136 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004137 setValue(&I, result);
4138 return 0;
4139 }
4140 case Intrinsic::ctpop: {
4141 SDValue Arg = getValue(I.getOperand(1));
4142 MVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004143 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004144 setValue(&I, result);
4145 return 0;
4146 }
4147 case Intrinsic::stacksave: {
4148 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004149 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004150 DAG.getNodeValueTypes(TLI.getPointerTy(), MVT::Other), 2, &Op, 1);
4151 setValue(&I, Tmp);
4152 DAG.setRoot(Tmp.getValue(1));
4153 return 0;
4154 }
4155 case Intrinsic::stackrestore: {
4156 SDValue Tmp = getValue(I.getOperand(1));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004157 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004158 return 0;
4159 }
Bill Wendling57344502008-11-18 11:01:33 +00004160 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004161 // Emit code into the DAG to store the stack guard onto the stack.
4162 MachineFunction &MF = DAG.getMachineFunction();
4163 MachineFrameInfo *MFI = MF.getFrameInfo();
4164 MVT PtrTy = TLI.getPointerTy();
4165
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004166 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4167 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004168
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004169 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004170 MFI->setStackProtectorIndex(FI);
4171
4172 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4173
4174 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004175 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Bill Wendlingb2a42982008-11-06 02:29:10 +00004176 PseudoSourceValue::getFixedStack(FI),
4177 0, true);
4178 setValue(&I, Result);
4179 DAG.setRoot(Result);
4180 return 0;
4181 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004182 case Intrinsic::var_annotation:
4183 // Discard annotate attributes
4184 return 0;
4185
4186 case Intrinsic::init_trampoline: {
4187 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4188
4189 SDValue Ops[6];
4190 Ops[0] = getRoot();
4191 Ops[1] = getValue(I.getOperand(1));
4192 Ops[2] = getValue(I.getOperand(2));
4193 Ops[3] = getValue(I.getOperand(3));
4194 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4195 Ops[5] = DAG.getSrcValue(F);
4196
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004197 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004198 DAG.getNodeValueTypes(TLI.getPointerTy(),
4199 MVT::Other), 2,
4200 Ops, 6);
4201
4202 setValue(&I, Tmp);
4203 DAG.setRoot(Tmp.getValue(1));
4204 return 0;
4205 }
4206
4207 case Intrinsic::gcroot:
4208 if (GFI) {
4209 Value *Alloca = I.getOperand(1);
4210 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004211
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004212 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4213 GFI->addStackRoot(FI->getIndex(), TypeMap);
4214 }
4215 return 0;
4216
4217 case Intrinsic::gcread:
4218 case Intrinsic::gcwrite:
4219 assert(0 && "GC failed to lower gcread/gcwrite intrinsics!");
4220 return 0;
4221
4222 case Intrinsic::flt_rounds: {
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004223 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004224 return 0;
4225 }
4226
4227 case Intrinsic::trap: {
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004228 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004229 return 0;
4230 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004231
Bill Wendlingef375462008-11-21 02:38:44 +00004232 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004233 return implVisitAluOverflow(I, ISD::UADDO);
4234 case Intrinsic::sadd_with_overflow:
4235 return implVisitAluOverflow(I, ISD::SADDO);
4236 case Intrinsic::usub_with_overflow:
4237 return implVisitAluOverflow(I, ISD::USUBO);
4238 case Intrinsic::ssub_with_overflow:
4239 return implVisitAluOverflow(I, ISD::SSUBO);
4240 case Intrinsic::umul_with_overflow:
4241 return implVisitAluOverflow(I, ISD::UMULO);
4242 case Intrinsic::smul_with_overflow:
4243 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004244
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004245 case Intrinsic::prefetch: {
4246 SDValue Ops[4];
4247 Ops[0] = getRoot();
4248 Ops[1] = getValue(I.getOperand(1));
4249 Ops[2] = getValue(I.getOperand(2));
4250 Ops[3] = getValue(I.getOperand(3));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004251 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004252 return 0;
4253 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004254
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004255 case Intrinsic::memory_barrier: {
4256 SDValue Ops[6];
4257 Ops[0] = getRoot();
4258 for (int x = 1; x < 6; ++x)
4259 Ops[x] = getValue(I.getOperand(x));
4260
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004261 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004262 return 0;
4263 }
4264 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004265 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004266 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004267 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004268 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4269 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004270 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004271 getValue(I.getOperand(2)),
4272 getValue(I.getOperand(3)),
4273 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004274 setValue(&I, L);
4275 DAG.setRoot(L.getValue(1));
4276 return 0;
4277 }
4278 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004279 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004280 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004281 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004282 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004283 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004284 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004285 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004286 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004287 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004288 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004289 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004290 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004291 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004292 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004293 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004294 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004295 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004296 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004297 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004298 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004299 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004300 }
4301}
4302
4303
4304void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee,
4305 bool IsTailCall,
4306 MachineBasicBlock *LandingPad) {
4307 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4308 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
4309 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4310 unsigned BeginLabel = 0, EndLabel = 0;
4311
4312 TargetLowering::ArgListTy Args;
4313 TargetLowering::ArgListEntry Entry;
4314 Args.reserve(CS.arg_size());
4315 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
4316 i != e; ++i) {
4317 SDValue ArgNode = getValue(*i);
4318 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4319
4320 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004321 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4322 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4323 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4324 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4325 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4326 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004327 Entry.Alignment = CS.getParamAlignment(attrInd);
4328 Args.push_back(Entry);
4329 }
4330
4331 if (LandingPad && MMI) {
4332 // Insert a label before the invoke call to mark the try range. This can be
4333 // used to detect deletion of the invoke via the MachineModuleInfo.
4334 BeginLabel = MMI->NextLabelID();
4335 // Both PendingLoads and PendingExports must be flushed here;
4336 // this call might not return.
4337 (void)getRoot();
4338 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getControlRoot(), BeginLabel));
4339 }
4340
4341 std::pair<SDValue,SDValue> Result =
4342 TLI.LowerCallTo(getRoot(), CS.getType(),
Devang Patel05988662008-09-25 21:00:45 +00004343 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004344 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
4345 CS.paramHasAttr(0, Attribute::InReg),
4346 CS.getCallingConv(),
Dan Gohman1937e2f2008-09-16 01:42:28 +00004347 IsTailCall && PerformTailCallOpt,
Dale Johannesen66978ee2009-01-31 02:22:37 +00004348 Callee, Args, DAG, getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004349 if (CS.getType() != Type::VoidTy)
4350 setValue(CS.getInstruction(), Result.first);
4351 DAG.setRoot(Result.second);
4352
4353 if (LandingPad && MMI) {
4354 // Insert a label at the end of the invoke call to mark the try range. This
4355 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4356 EndLabel = MMI->NextLabelID();
4357 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getRoot(), EndLabel));
4358
4359 // Inform MachineModuleInfo of range.
4360 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4361 }
4362}
4363
4364
4365void SelectionDAGLowering::visitCall(CallInst &I) {
4366 const char *RenameFn = 0;
4367 if (Function *F = I.getCalledFunction()) {
4368 if (F->isDeclaration()) {
4369 if (unsigned IID = F->getIntrinsicID()) {
4370 RenameFn = visitIntrinsicCall(I, IID);
4371 if (!RenameFn)
4372 return;
4373 }
4374 }
4375
4376 // Check for well-known libc/libm calls. If the function is internal, it
4377 // can't be a library call.
4378 unsigned NameLen = F->getNameLen();
Rafael Espindolabb46f522009-01-15 20:18:42 +00004379 if (!F->hasLocalLinkage() && NameLen) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004380 const char *NameStr = F->getNameStart();
4381 if (NameStr[0] == 'c' &&
4382 ((NameLen == 8 && !strcmp(NameStr, "copysign")) ||
4383 (NameLen == 9 && !strcmp(NameStr, "copysignf")))) {
4384 if (I.getNumOperands() == 3 && // Basic sanity checks.
4385 I.getOperand(1)->getType()->isFloatingPoint() &&
4386 I.getType() == I.getOperand(1)->getType() &&
4387 I.getType() == I.getOperand(2)->getType()) {
4388 SDValue LHS = getValue(I.getOperand(1));
4389 SDValue RHS = getValue(I.getOperand(2));
Dale Johannesen66978ee2009-01-31 02:22:37 +00004390 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004391 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392 return;
4393 }
4394 } else if (NameStr[0] == 'f' &&
4395 ((NameLen == 4 && !strcmp(NameStr, "fabs")) ||
4396 (NameLen == 5 && !strcmp(NameStr, "fabsf")) ||
4397 (NameLen == 5 && !strcmp(NameStr, "fabsl")))) {
4398 if (I.getNumOperands() == 2 && // Basic sanity checks.
4399 I.getOperand(1)->getType()->isFloatingPoint() &&
4400 I.getType() == I.getOperand(1)->getType()) {
4401 SDValue Tmp = getValue(I.getOperand(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +00004402 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004403 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004404 return;
4405 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004406 } else if (NameStr[0] == 's' &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004407 ((NameLen == 3 && !strcmp(NameStr, "sin")) ||
4408 (NameLen == 4 && !strcmp(NameStr, "sinf")) ||
4409 (NameLen == 4 && !strcmp(NameStr, "sinl")))) {
4410 if (I.getNumOperands() == 2 && // Basic sanity checks.
4411 I.getOperand(1)->getType()->isFloatingPoint() &&
4412 I.getType() == I.getOperand(1)->getType()) {
4413 SDValue Tmp = getValue(I.getOperand(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +00004414 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004415 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004416 return;
4417 }
4418 } else if (NameStr[0] == 'c' &&
4419 ((NameLen == 3 && !strcmp(NameStr, "cos")) ||
4420 (NameLen == 4 && !strcmp(NameStr, "cosf")) ||
4421 (NameLen == 4 && !strcmp(NameStr, "cosl")))) {
4422 if (I.getNumOperands() == 2 && // Basic sanity checks.
4423 I.getOperand(1)->getType()->isFloatingPoint() &&
4424 I.getType() == I.getOperand(1)->getType()) {
4425 SDValue Tmp = getValue(I.getOperand(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +00004426 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004427 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004428 return;
4429 }
4430 }
4431 }
4432 } else if (isa<InlineAsm>(I.getOperand(0))) {
4433 visitInlineAsm(&I);
4434 return;
4435 }
4436
4437 SDValue Callee;
4438 if (!RenameFn)
4439 Callee = getValue(I.getOperand(0));
4440 else
Bill Wendling056292f2008-09-16 21:48:12 +00004441 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004442
4443 LowerCallTo(&I, Callee, I.isTailCall());
4444}
4445
4446
4447/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004448/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004449/// Chain/Flag as the input and updates them for the output Chain/Flag.
4450/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004451SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004452 SDValue &Chain,
4453 SDValue *Flag) const {
4454 // Assemble the legal parts into the final values.
4455 SmallVector<SDValue, 4> Values(ValueVTs.size());
4456 SmallVector<SDValue, 8> Parts;
4457 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4458 // Copy the legal parts from the registers.
4459 MVT ValueVT = ValueVTs[Value];
4460 unsigned NumRegs = TLI->getNumRegisters(ValueVT);
4461 MVT RegisterVT = RegVTs[Value];
4462
4463 Parts.resize(NumRegs);
4464 for (unsigned i = 0; i != NumRegs; ++i) {
4465 SDValue P;
4466 if (Flag == 0)
4467 P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT);
4468 else {
4469 P = DAG.getCopyFromReg(Chain, Regs[Part+i], RegisterVT, *Flag);
4470 *Flag = P.getValue(2);
4471 }
4472 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004474 // If the source register was virtual and if we know something about it,
4475 // add an assert node.
4476 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4477 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4478 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4479 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4480 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4481 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004482
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004483 unsigned RegSize = RegisterVT.getSizeInBits();
4484 unsigned NumSignBits = LOI.NumSignBits;
4485 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004486
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004487 // FIXME: We capture more information than the dag can represent. For
4488 // now, just use the tightest assertzext/assertsext possible.
4489 bool isSExt = true;
4490 MVT FromVT(MVT::Other);
4491 if (NumSignBits == RegSize)
4492 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
4493 else if (NumZeroBits >= RegSize-1)
4494 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
4495 else if (NumSignBits > RegSize-8)
4496 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
4497 else if (NumZeroBits >= RegSize-9)
4498 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
4499 else if (NumSignBits > RegSize-16)
Bill Wendling181b6272008-10-19 20:34:04 +00004500 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004501 else if (NumZeroBits >= RegSize-17)
Bill Wendling181b6272008-10-19 20:34:04 +00004502 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004503 else if (NumSignBits > RegSize-32)
Bill Wendling181b6272008-10-19 20:34:04 +00004504 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004505 else if (NumZeroBits >= RegSize-33)
Bill Wendling181b6272008-10-19 20:34:04 +00004506 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004507
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004508 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004509 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004510 RegisterVT, P, DAG.getValueType(FromVT));
4511
4512 }
4513 }
4514 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004515
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004516 Parts[i] = P;
4517 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004518
Dale Johannesen66978ee2009-01-31 02:22:37 +00004519 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
4520 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004521 Part += NumRegs;
4522 Parts.clear();
4523 }
4524
Dale Johannesen66978ee2009-01-31 02:22:37 +00004525 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004526 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4527 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004528}
4529
4530/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004531/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004532/// Chain/Flag as the input and updates them for the output Chain/Flag.
4533/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004534void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004535 SDValue &Chain, SDValue *Flag) const {
4536 // Get the list of the values's legal parts.
4537 unsigned NumRegs = Regs.size();
4538 SmallVector<SDValue, 8> Parts(NumRegs);
4539 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4540 MVT ValueVT = ValueVTs[Value];
4541 unsigned NumParts = TLI->getNumRegisters(ValueVT);
4542 MVT RegisterVT = RegVTs[Value];
4543
Dale Johannesen66978ee2009-01-31 02:22:37 +00004544 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004545 &Parts[Part], NumParts, RegisterVT);
4546 Part += NumParts;
4547 }
4548
4549 // Copy the parts into the registers.
4550 SmallVector<SDValue, 8> Chains(NumRegs);
4551 for (unsigned i = 0; i != NumRegs; ++i) {
4552 SDValue Part;
4553 if (Flag == 0)
4554 Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i]);
4555 else {
4556 Part = DAG.getCopyToReg(Chain, Regs[i], Parts[i], *Flag);
4557 *Flag = Part.getValue(1);
4558 }
4559 Chains[i] = Part.getValue(0);
4560 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004561
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004562 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004563 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004564 // flagged to it. That is the CopyToReg nodes and the user are considered
4565 // a single scheduling unit. If we create a TokenFactor and return it as
4566 // chain, then the TokenFactor is both a predecessor (operand) of the
4567 // user as well as a successor (the TF operands are flagged to the user).
4568 // c1, f1 = CopyToReg
4569 // c2, f2 = CopyToReg
4570 // c3 = TokenFactor c1, c2
4571 // ...
4572 // = op c3, ..., f2
4573 Chain = Chains[NumRegs-1];
4574 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00004575 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004576}
4577
4578/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004579/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004580/// values added into it.
4581void RegsForValue::AddInlineAsmOperands(unsigned Code, SelectionDAG &DAG,
4582 std::vector<SDValue> &Ops) const {
4583 MVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
4584 Ops.push_back(DAG.getTargetConstant(Code | (Regs.size() << 3), IntPtrTy));
4585 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
4586 unsigned NumRegs = TLI->getNumRegisters(ValueVTs[Value]);
4587 MVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004588 for (unsigned i = 0; i != NumRegs; ++i) {
4589 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004590 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004591 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004592 }
4593}
4594
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004595/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596/// i.e. it isn't a stack pointer or some other special register, return the
4597/// register class for the register. Otherwise, return null.
4598static const TargetRegisterClass *
4599isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4600 const TargetLowering &TLI,
4601 const TargetRegisterInfo *TRI) {
4602 MVT FoundVT = MVT::Other;
4603 const TargetRegisterClass *FoundRC = 0;
4604 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4605 E = TRI->regclass_end(); RCI != E; ++RCI) {
4606 MVT ThisVT = MVT::Other;
4607
4608 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004609 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004610 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4611 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4612 I != E; ++I) {
4613 if (TLI.isTypeLegal(*I)) {
4614 // If we have already found this register in a different register class,
4615 // choose the one with the largest VT specified. For example, on
4616 // PowerPC, we favor f64 register classes over f32.
4617 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
4618 ThisVT = *I;
4619 break;
4620 }
4621 }
4622 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004625
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004626 // NOTE: This isn't ideal. In particular, this might allocate the
4627 // frame pointer in functions that need it (due to them not being taken
4628 // out of allocation, because a variable sized allocation hasn't been seen
4629 // yet). This is a slight code pessimization, but should still work.
4630 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4631 E = RC->allocation_order_end(MF); I != E; ++I)
4632 if (*I == Reg) {
4633 // We found a matching register class. Keep looking at others in case
4634 // we find one with larger registers that this physreg is also in.
4635 FoundRC = RC;
4636 FoundVT = ThisVT;
4637 break;
4638 }
4639 }
4640 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004641}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004642
4643
4644namespace llvm {
4645/// AsmOperandInfo - This contains information for each constraint that we are
4646/// lowering.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004647struct VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004648 public TargetLowering::AsmOperandInfo {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004649 /// CallOperand - If this is the result output operand or a clobber
4650 /// this is null, otherwise it is the incoming operand to the CallInst.
4651 /// This gets modified as the asm is processed.
4652 SDValue CallOperand;
4653
4654 /// AssignedRegs - If this is a register or register class operand, this
4655 /// contains the set of register corresponding to the operand.
4656 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004657
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004658 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4659 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4660 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004661
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004662 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4663 /// busy in OutputRegs/InputRegs.
4664 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004665 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004666 std::set<unsigned> &InputRegs,
4667 const TargetRegisterInfo &TRI) const {
4668 if (isOutReg) {
4669 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4670 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4671 }
4672 if (isInReg) {
4673 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4674 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4675 }
4676 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004677
Chris Lattner81249c92008-10-17 17:05:25 +00004678 /// getCallOperandValMVT - Return the MVT of the Value* that this operand
4679 /// corresponds to. If there is no Value* for this operand, it returns
4680 /// MVT::Other.
4681 MVT getCallOperandValMVT(const TargetLowering &TLI,
4682 const TargetData *TD) const {
4683 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004684
Chris Lattner81249c92008-10-17 17:05:25 +00004685 if (isa<BasicBlock>(CallOperandVal))
4686 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004687
Chris Lattner81249c92008-10-17 17:05:25 +00004688 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004689
Chris Lattner81249c92008-10-17 17:05:25 +00004690 // If this is an indirect operand, the operand is a pointer to the
4691 // accessed type.
4692 if (isIndirect)
4693 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004694
Chris Lattner81249c92008-10-17 17:05:25 +00004695 // If OpTy is not a single value, it may be a struct/union that we
4696 // can tile with integers.
4697 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4698 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4699 switch (BitSize) {
4700 default: break;
4701 case 1:
4702 case 8:
4703 case 16:
4704 case 32:
4705 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004706 case 128:
Chris Lattner81249c92008-10-17 17:05:25 +00004707 OpTy = IntegerType::get(BitSize);
4708 break;
4709 }
4710 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004711
Chris Lattner81249c92008-10-17 17:05:25 +00004712 return TLI.getValueType(OpTy, true);
4713 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004714
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004715private:
4716 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4717 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004718 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004719 const TargetRegisterInfo &TRI) {
4720 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4721 Regs.insert(Reg);
4722 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4723 for (; *Aliases; ++Aliases)
4724 Regs.insert(*Aliases);
4725 }
4726};
4727} // end llvm namespace.
4728
4729
4730/// GetRegistersForValue - Assign registers (virtual or physical) for the
4731/// specified operand. We prefer to assign virtual registers, to allow the
4732/// register allocator handle the assignment process. However, if the asm uses
4733/// features that we can't model on machineinstrs, we have SDISel do the
4734/// allocation. This produces generally horrible, but correct, code.
4735///
4736/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004737/// Input and OutputRegs are the set of already allocated physical registers.
4738///
4739void SelectionDAGLowering::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004740GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004741 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004742 std::set<unsigned> &InputRegs) {
4743 // Compute whether this value requires an input register, an output register,
4744 // or both.
4745 bool isOutReg = false;
4746 bool isInReg = false;
4747 switch (OpInfo.Type) {
4748 case InlineAsm::isOutput:
4749 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004750
4751 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004752 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004753 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004754 break;
4755 case InlineAsm::isInput:
4756 isInReg = true;
4757 isOutReg = false;
4758 break;
4759 case InlineAsm::isClobber:
4760 isOutReg = true;
4761 isInReg = true;
4762 break;
4763 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004764
4765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004766 MachineFunction &MF = DAG.getMachineFunction();
4767 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004768
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004769 // If this is a constraint for a single physreg, or a constraint for a
4770 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004771 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004772 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4773 OpInfo.ConstraintVT);
4774
4775 unsigned NumRegs = 1;
Chris Lattner01426e12008-10-21 00:45:36 +00004776 if (OpInfo.ConstraintVT != MVT::Other) {
4777 // If this is a FP input in an integer register (or visa versa) insert a bit
4778 // cast of the input value. More generally, handle any case where the input
4779 // value disagrees with the register class we plan to stick this in.
4780 if (OpInfo.Type == InlineAsm::isInput &&
4781 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
4782 // Try to convert to the first MVT that the reg class contains. If the
4783 // types are identical size, use a bitcast to convert (e.g. two differing
4784 // vector types).
4785 MVT RegVT = *PhysReg.second->vt_begin();
4786 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004787 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004788 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004789 OpInfo.ConstraintVT = RegVT;
4790 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4791 // If the input is a FP value and we want it in FP registers, do a
4792 // bitcast to the corresponding integer type. This turns an f64 value
4793 // into i64, which can be passed with two i32 values on a 32-bit
4794 // machine.
4795 RegVT = MVT::getIntegerVT(OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004796 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004797 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004798 OpInfo.ConstraintVT = RegVT;
4799 }
4800 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802 NumRegs = TLI.getNumRegisters(OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004803 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004804
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004805 MVT RegVT;
4806 MVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004807
4808 // If this is a constraint for a specific physical register, like {r17},
4809 // assign it now.
4810 if (PhysReg.first) {
4811 if (OpInfo.ConstraintVT == MVT::Other)
4812 ValueVT = *PhysReg.second->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004814 // Get the actual register value type. This is important, because the user
4815 // may have asked for (e.g.) the AX register in i32 type. We need to
4816 // remember that AX is actually i16 to get the right extension.
4817 RegVT = *PhysReg.second->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004818
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004819 // This is a explicit reference to a physical register.
4820 Regs.push_back(PhysReg.first);
4821
4822 // If this is an expanded reference, add the rest of the regs to Regs.
4823 if (NumRegs != 1) {
4824 TargetRegisterClass::iterator I = PhysReg.second->begin();
4825 for (; *I != PhysReg.first; ++I)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004826 assert(I != PhysReg.second->end() && "Didn't find reg!");
4827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 // Already added the first reg.
4829 --NumRegs; ++I;
4830 for (; NumRegs; --NumRegs, ++I) {
4831 assert(I != PhysReg.second->end() && "Ran out of registers to allocate!");
4832 Regs.push_back(*I);
4833 }
4834 }
4835 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4836 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4837 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4838 return;
4839 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004840
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004841 // Otherwise, if this was a reference to an LLVM register class, create vregs
4842 // for this reference.
4843 std::vector<unsigned> RegClassRegs;
4844 const TargetRegisterClass *RC = PhysReg.second;
4845 if (RC) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004846 // If this is a tied register, our regalloc doesn't know how to maintain
Chris Lattner58f15c42008-10-17 16:21:11 +00004847 // the constraint, so we have to pick a register to pin the input/output to.
4848 // If it isn't a matched constraint, go ahead and create vreg and let the
4849 // regalloc do its thing.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004850 if (!OpInfo.hasMatchingInput()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004851 RegVT = *PhysReg.second->vt_begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004852 if (OpInfo.ConstraintVT == MVT::Other)
4853 ValueVT = RegVT;
4854
4855 // Create the appropriate number of virtual registers.
4856 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4857 for (; NumRegs; --NumRegs)
4858 Regs.push_back(RegInfo.createVirtualRegister(PhysReg.second));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004859
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004860 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4861 return;
4862 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004863
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004864 // Otherwise, we can't allocate it. Let the code below figure out how to
4865 // maintain these constraints.
4866 RegClassRegs.assign(PhysReg.second->begin(), PhysReg.second->end());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004867
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004868 } else {
4869 // This is a reference to a register class that doesn't directly correspond
4870 // to an LLVM register class. Allocate NumRegs consecutive, available,
4871 // registers from the class.
4872 RegClassRegs = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
4873 OpInfo.ConstraintVT);
4874 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004875
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004876 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4877 unsigned NumAllocated = 0;
4878 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
4879 unsigned Reg = RegClassRegs[i];
4880 // See if this register is available.
4881 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
4882 (isInReg && InputRegs.count(Reg))) { // Already used.
4883 // Make sure we find consecutive registers.
4884 NumAllocated = 0;
4885 continue;
4886 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004887
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004888 // Check to see if this register is allocatable (i.e. don't give out the
4889 // stack pointer).
4890 if (RC == 0) {
4891 RC = isAllocatableRegister(Reg, MF, TLI, TRI);
4892 if (!RC) { // Couldn't allocate this register.
4893 // Reset NumAllocated to make sure we return consecutive registers.
4894 NumAllocated = 0;
4895 continue;
4896 }
4897 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004898
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004899 // Okay, this register is good, we can use it.
4900 ++NumAllocated;
4901
4902 // If we allocated enough consecutive registers, succeed.
4903 if (NumAllocated == NumRegs) {
4904 unsigned RegStart = (i-NumAllocated)+1;
4905 unsigned RegEnd = i+1;
4906 // Mark all of the allocated registers used.
4907 for (unsigned i = RegStart; i != RegEnd; ++i)
4908 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004909
4910 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004911 OpInfo.ConstraintVT);
4912 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4913 return;
4914 }
4915 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004916
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004917 // Otherwise, we couldn't allocate enough registers for this.
4918}
4919
Evan Chengda43bcf2008-09-24 00:05:32 +00004920/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
4921/// processed uses a memory 'm' constraint.
4922static bool
4923hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00004924 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00004925 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
4926 InlineAsm::ConstraintInfo &CI = CInfos[i];
4927 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
4928 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
4929 if (CType == TargetLowering::C_Memory)
4930 return true;
4931 }
4932 }
4933
4934 return false;
4935}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004936
4937/// visitInlineAsm - Handle a call to an InlineAsm object.
4938///
4939void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
4940 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
4941
4942 /// ConstraintOperands - Information about all of the constraints.
4943 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004945 SDValue Chain = getRoot();
4946 SDValue Flag;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004947
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004948 std::set<unsigned> OutputRegs, InputRegs;
4949
4950 // Do a prepass over the constraints, canonicalizing them, and building up the
4951 // ConstraintOperands list.
4952 std::vector<InlineAsm::ConstraintInfo>
4953 ConstraintInfos = IA->ParseConstraints();
4954
Evan Chengda43bcf2008-09-24 00:05:32 +00004955 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004956
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004957 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
4958 unsigned ResNo = 0; // ResNo - The result number of the next output.
4959 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4960 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
4961 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004962
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004963 MVT OpVT = MVT::Other;
4964
4965 // Compute the value type for each operand.
4966 switch (OpInfo.Type) {
4967 case InlineAsm::isOutput:
4968 // Indirect outputs just consume an argument.
4969 if (OpInfo.isIndirect) {
4970 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4971 break;
4972 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004973
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004974 // The return value of the call is this value. As such, there is no
4975 // corresponding argument.
4976 assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
4977 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
4978 OpVT = TLI.getValueType(STy->getElementType(ResNo));
4979 } else {
4980 assert(ResNo == 0 && "Asm only has one result!");
4981 OpVT = TLI.getValueType(CS.getType());
4982 }
4983 ++ResNo;
4984 break;
4985 case InlineAsm::isInput:
4986 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4987 break;
4988 case InlineAsm::isClobber:
4989 // Nothing to do.
4990 break;
4991 }
4992
4993 // If this is an input or an indirect output, process the call argument.
4994 // BasicBlocks are labels, currently appearing only in asm's.
4995 if (OpInfo.CallOperandVal) {
Chris Lattner81249c92008-10-17 17:05:25 +00004996 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004997 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00004998 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004999 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005000 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005001
Chris Lattner81249c92008-10-17 17:05:25 +00005002 OpVT = OpInfo.getCallOperandValMVT(TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005003 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005005 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005006 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005007
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005008 // Second pass over the constraints: compute which constraint option to use
5009 // and assign registers to constraints that want a specific physreg.
5010 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5011 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005012
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005013 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005014 // matching input. If their types mismatch, e.g. one is an integer, the
5015 // other is floating point, or their sizes are different, flag it as an
5016 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005017 if (OpInfo.hasMatchingInput()) {
5018 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5019 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005020 if ((OpInfo.ConstraintVT.isInteger() !=
5021 Input.ConstraintVT.isInteger()) ||
5022 (OpInfo.ConstraintVT.getSizeInBits() !=
5023 Input.ConstraintVT.getSizeInBits())) {
5024 cerr << "Unsupported asm: input constraint with a matching output "
5025 << "constraint of incompatible type!\n";
5026 exit(1);
5027 }
5028 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005029 }
5030 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005031
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005032 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005033 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005035 // If this is a memory input, and if the operand is not indirect, do what we
5036 // need to to provide an address for the memory input.
5037 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5038 !OpInfo.isIndirect) {
5039 assert(OpInfo.Type == InlineAsm::isInput &&
5040 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005041
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005042 // Memory operands really want the address of the value. If we don't have
5043 // an indirect input, put it in the constpool if we can, otherwise spill
5044 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005046 // If the operand is a float, integer, or vector constant, spill to a
5047 // constant pool entry to get its address.
5048 Value *OpVal = OpInfo.CallOperandVal;
5049 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5050 isa<ConstantVector>(OpVal)) {
5051 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5052 TLI.getPointerTy());
5053 } else {
5054 // Otherwise, create a stack slot and emit a store to it before the
5055 // asm.
5056 const Type *Ty = OpVal->getType();
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00005057 uint64_t TySize = TLI.getTargetData()->getTypePaddedSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005058 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5059 MachineFunction &MF = DAG.getMachineFunction();
5060 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
5061 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005062 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005063 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005064 OpInfo.CallOperand = StackSlot;
5065 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067 // There is no longer a Value* corresponding to this operand.
5068 OpInfo.CallOperandVal = 0;
5069 // It is now an indirect operand.
5070 OpInfo.isIndirect = true;
5071 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005072
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005073 // If this constraint is for a specific register, allocate it before
5074 // anything else.
5075 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005076 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005077 }
5078 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005079
5080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005082 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005083 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5084 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005086 // C_Register operands have already been allocated, Other/Memory don't need
5087 // to be.
5088 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005089 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005090 }
5091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005092 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5093 std::vector<SDValue> AsmNodeOperands;
5094 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5095 AsmNodeOperands.push_back(
Bill Wendling056292f2008-09-16 21:48:12 +00005096 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005097
5098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005099 // Loop over all of the inputs, copying the operand values into the
5100 // appropriate registers and processing the output regs.
5101 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005103 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5104 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005105
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005106 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5107 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5108
5109 switch (OpInfo.Type) {
5110 case InlineAsm::isOutput: {
5111 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5112 OpInfo.ConstraintType != TargetLowering::C_Register) {
5113 // Memory output, or 'other' output (e.g. 'X' constraint).
5114 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5115
5116 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005117 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5118 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005119 TLI.getPointerTy()));
5120 AsmNodeOperands.push_back(OpInfo.CallOperand);
5121 break;
5122 }
5123
5124 // Otherwise, this is a register or register class output.
5125
5126 // Copy the output from the appropriate register. Find a register that
5127 // we can use.
5128 if (OpInfo.AssignedRegs.Regs.empty()) {
5129 cerr << "Couldn't allocate output reg for constraint '"
5130 << OpInfo.ConstraintCode << "'!\n";
5131 exit(1);
5132 }
5133
5134 // If this is an indirect operand, store through the pointer after the
5135 // asm.
5136 if (OpInfo.isIndirect) {
5137 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5138 OpInfo.CallOperandVal));
5139 } else {
5140 // This is the result value of the call.
5141 assert(CS.getType() != Type::VoidTy && "Bad inline asm!");
5142 // Concatenate this output onto the outputs list.
5143 RetValRegs.append(OpInfo.AssignedRegs);
5144 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005146 // Add information to the INLINEASM node to know that this register is
5147 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005148 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5149 6 /* EARLYCLOBBER REGDEF */ :
5150 2 /* REGDEF */ ,
5151 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005152 break;
5153 }
5154 case InlineAsm::isInput: {
5155 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005156
Chris Lattner6bdcda32008-10-17 16:47:46 +00005157 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005158 // If this is required to match an output register we have already set,
5159 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005160 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005161
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005162 // Scan until we find the definition we already emitted of this operand.
5163 // When we find it, create a RegsForValue operand.
5164 unsigned CurOp = 2; // The first operand.
5165 for (; OperandNo; --OperandNo) {
5166 // Advance to the next operand.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005167 unsigned NumOps =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005168 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005169 assert(((NumOps & 7) == 2 /*REGDEF*/ ||
Dale Johannesen913d3df2008-09-12 17:49:03 +00005170 (NumOps & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
Dale Johannesen86b49f82008-09-24 01:07:17 +00005171 (NumOps & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005172 "Skipped past definitions?");
5173 CurOp += (NumOps>>3)+1;
5174 }
5175
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005176 unsigned NumOps =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005177 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005178 if ((NumOps & 7) == 2 /*REGDEF*/
Dale Johannesen913d3df2008-09-12 17:49:03 +00005179 || (NumOps & 7) == 6 /* EARLYCLOBBER REGDEF */) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005180 // Add NumOps>>3 registers to MatchedRegs.
5181 RegsForValue MatchedRegs;
5182 MatchedRegs.TLI = &TLI;
5183 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
5184 MatchedRegs.RegVTs.push_back(AsmNodeOperands[CurOp+1].getValueType());
5185 for (unsigned i = 0, e = NumOps>>3; i != e; ++i) {
5186 unsigned Reg =
5187 cast<RegisterSDNode>(AsmNodeOperands[++CurOp])->getReg();
5188 MatchedRegs.Regs.push_back(Reg);
5189 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005190
5191 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005192 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5193 Chain, &Flag);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005194 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/, DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005195 break;
5196 } else {
Dale Johannesen86b49f82008-09-24 01:07:17 +00005197 assert(((NumOps & 7) == 4) && "Unknown matching constraint!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005198 assert((NumOps >> 3) == 1 && "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005199 // Add information to the INLINEASM node to know about this input.
Dale Johannesen91aac102008-09-17 21:13:11 +00005200 AsmNodeOperands.push_back(DAG.getTargetConstant(NumOps,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005201 TLI.getPointerTy()));
5202 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5203 break;
5204 }
5205 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005206
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005207 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005208 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005209 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005211 std::vector<SDValue> Ops;
5212 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005213 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005214 if (Ops.empty()) {
5215 cerr << "Invalid operand for inline asm constraint '"
5216 << OpInfo.ConstraintCode << "'!\n";
5217 exit(1);
5218 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 // Add information to the INLINEASM node to know about this input.
5221 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005222 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005223 TLI.getPointerTy()));
5224 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5225 break;
5226 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5227 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5228 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5229 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005230
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005231 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005232 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5233 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005234 TLI.getPointerTy()));
5235 AsmNodeOperands.push_back(InOperandVal);
5236 break;
5237 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005238
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005239 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5240 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5241 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005242 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005243 "Don't know how to handle indirect register inputs yet!");
5244
5245 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005246 if (OpInfo.AssignedRegs.Regs.empty()) {
5247 cerr << "Couldn't allocate output reg for constraint '"
5248 << OpInfo.ConstraintCode << "'!\n";
5249 exit(1);
5250 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005251
Dale Johannesen66978ee2009-01-31 02:22:37 +00005252 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5253 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005254
Dale Johannesen86b49f82008-09-24 01:07:17 +00005255 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/,
5256 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005257 break;
5258 }
5259 case InlineAsm::isClobber: {
5260 // Add the clobbered value to the operand list, so that the register
5261 // allocator is aware that the physreg got clobbered.
5262 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005263 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
5264 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005265 break;
5266 }
5267 }
5268 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005270 // Finish up input operands.
5271 AsmNodeOperands[0] = Chain;
5272 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005273
Dale Johannesen66978ee2009-01-31 02:22:37 +00005274 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005275 DAG.getNodeValueTypes(MVT::Other, MVT::Flag), 2,
5276 &AsmNodeOperands[0], AsmNodeOperands.size());
5277 Flag = Chain.getValue(1);
5278
5279 // If this asm returns a register value, copy the result from that register
5280 // and set it as the value of the call.
5281 if (!RetValRegs.Regs.empty()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005282 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5283 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005284
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005285 // FIXME: Why don't we do this for inline asms with MRVs?
5286 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
5287 MVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005288
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005289 // If any of the results of the inline asm is a vector, it may have the
5290 // wrong width/num elts. This can happen for register classes that can
5291 // contain multiple different value types. The preg or vreg allocated may
5292 // not have the same VT as was expected. Convert it to the right type
5293 // with bit_convert.
5294 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005295 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005296 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005297
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005298 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005299 ResultType.isInteger() && Val.getValueType().isInteger()) {
5300 // If a result value was tied to an input value, the computed result may
5301 // have a wider width than the expected result. Extract the relevant
5302 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005303 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005304 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005305
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005306 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005307 }
Dan Gohman95915732008-10-18 01:03:45 +00005308
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005309 setValue(CS.getInstruction(), Val);
5310 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005311
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005313
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005314 // Process indirect outputs, first output all of the flagged copies out of
5315 // physregs.
5316 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5317 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5318 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005319 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5320 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005321 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5322 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005323
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005324 // Emit the non-flagged stores from the physregs.
5325 SmallVector<SDValue, 8> OutChains;
5326 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005327 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005328 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005329 getValue(StoresToEmit[i].second),
5330 StoresToEmit[i].second, 0));
5331 if (!OutChains.empty())
Dale Johannesen66978ee2009-01-31 02:22:37 +00005332 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005333 &OutChains[0], OutChains.size());
5334 DAG.setRoot(Chain);
5335}
5336
5337
5338void SelectionDAGLowering::visitMalloc(MallocInst &I) {
5339 SDValue Src = getValue(I.getOperand(0));
5340
5341 MVT IntPtr = TLI.getPointerTy();
5342
5343 if (IntPtr.bitsLT(Src.getValueType()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00005344 Src = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), IntPtr, Src);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005345 else if (IntPtr.bitsGT(Src.getValueType()))
Dale Johannesen66978ee2009-01-31 02:22:37 +00005346 Src = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), IntPtr, Src);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005347
5348 // Scale the source by the type size.
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00005349 uint64_t ElementSize = TD->getTypePaddedSize(I.getType()->getElementType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005350 Src = DAG.getNode(ISD::MUL, getCurDebugLoc(), Src.getValueType(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005351 Src, DAG.getIntPtrConstant(ElementSize));
5352
5353 TargetLowering::ArgListTy Args;
5354 TargetLowering::ArgListEntry Entry;
5355 Entry.Node = Src;
5356 Entry.Ty = TLI.getTargetData()->getIntPtrType();
5357 Args.push_back(Entry);
5358
5359 std::pair<SDValue,SDValue> Result =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005360 TLI.LowerCallTo(getRoot(), I.getType(), false, false, false, false,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005361 CallingConv::C, PerformTailCallOpt,
Dale Johannesen86098bd2008-09-26 19:31:26 +00005362 DAG.getExternalSymbol("malloc", IntPtr),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005363 Args, DAG, getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005364 setValue(&I, Result.first); // Pointers always fit in registers
5365 DAG.setRoot(Result.second);
5366}
5367
5368void SelectionDAGLowering::visitFree(FreeInst &I) {
5369 TargetLowering::ArgListTy Args;
5370 TargetLowering::ArgListEntry Entry;
5371 Entry.Node = getValue(I.getOperand(0));
5372 Entry.Ty = TLI.getTargetData()->getIntPtrType();
5373 Args.push_back(Entry);
5374 MVT IntPtr = TLI.getPointerTy();
5375 std::pair<SDValue,SDValue> Result =
Dale Johannesen86098bd2008-09-26 19:31:26 +00005376 TLI.LowerCallTo(getRoot(), Type::VoidTy, false, false, false, false,
Dan Gohman1937e2f2008-09-16 01:42:28 +00005377 CallingConv::C, PerformTailCallOpt,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005378 DAG.getExternalSymbol("free", IntPtr), Args, DAG,
Dale Johannesen66978ee2009-01-31 02:22:37 +00005379 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005380 DAG.setRoot(Result.second);
5381}
5382
5383void SelectionDAGLowering::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005384 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005385 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005386 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005387 DAG.getSrcValue(I.getOperand(1))));
5388}
5389
5390void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
5391 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getRoot(),
5392 getValue(I.getOperand(0)),
5393 DAG.getSrcValue(I.getOperand(0)));
5394 setValue(&I, V);
5395 DAG.setRoot(V.getValue(1));
5396}
5397
5398void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005399 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005400 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005401 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005402 DAG.getSrcValue(I.getOperand(1))));
5403}
5404
5405void SelectionDAGLowering::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005406 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005407 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005408 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005409 getValue(I.getOperand(2)),
5410 DAG.getSrcValue(I.getOperand(1)),
5411 DAG.getSrcValue(I.getOperand(2))));
5412}
5413
5414/// TargetLowering::LowerArguments - This is the default LowerArguments
5415/// implementation, which just inserts a FORMAL_ARGUMENTS node. FIXME: When all
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005416/// targets are migrated to using FORMAL_ARGUMENTS, this hook should be
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417/// integrated into SDISel.
5418void TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005419 SmallVectorImpl<SDValue> &ArgValues,
5420 DebugLoc dl) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005421 // Add CC# and isVararg as operands to the FORMAL_ARGUMENTS node.
5422 SmallVector<SDValue, 3+16> Ops;
5423 Ops.push_back(DAG.getRoot());
5424 Ops.push_back(DAG.getConstant(F.getCallingConv(), getPointerTy()));
5425 Ops.push_back(DAG.getConstant(F.isVarArg(), getPointerTy()));
5426
5427 // Add one result value for each formal argument.
5428 SmallVector<MVT, 16> RetVals;
5429 unsigned j = 1;
5430 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5431 I != E; ++I, ++j) {
5432 SmallVector<MVT, 4> ValueVTs;
5433 ComputeValueVTs(*this, I->getType(), ValueVTs);
5434 for (unsigned Value = 0, NumValues = ValueVTs.size();
5435 Value != NumValues; ++Value) {
5436 MVT VT = ValueVTs[Value];
5437 const Type *ArgTy = VT.getTypeForMVT();
5438 ISD::ArgFlagsTy Flags;
5439 unsigned OriginalAlignment =
5440 getTargetData()->getABITypeAlignment(ArgTy);
5441
Devang Patel05988662008-09-25 21:00:45 +00005442 if (F.paramHasAttr(j, Attribute::ZExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005443 Flags.setZExt();
Devang Patel05988662008-09-25 21:00:45 +00005444 if (F.paramHasAttr(j, Attribute::SExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005445 Flags.setSExt();
Devang Patel05988662008-09-25 21:00:45 +00005446 if (F.paramHasAttr(j, Attribute::InReg))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005447 Flags.setInReg();
Devang Patel05988662008-09-25 21:00:45 +00005448 if (F.paramHasAttr(j, Attribute::StructRet))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005449 Flags.setSRet();
Devang Patel05988662008-09-25 21:00:45 +00005450 if (F.paramHasAttr(j, Attribute::ByVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005451 Flags.setByVal();
5452 const PointerType *Ty = cast<PointerType>(I->getType());
5453 const Type *ElementTy = Ty->getElementType();
5454 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00005455 unsigned FrameSize = getTargetData()->getTypePaddedSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005456 // For ByVal, alignment should be passed from FE. BE will guess if
5457 // this info is not there but there are cases it cannot get right.
5458 if (F.getParamAlignment(j))
5459 FrameAlign = F.getParamAlignment(j);
5460 Flags.setByValAlign(FrameAlign);
5461 Flags.setByValSize(FrameSize);
5462 }
Devang Patel05988662008-09-25 21:00:45 +00005463 if (F.paramHasAttr(j, Attribute::Nest))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005464 Flags.setNest();
5465 Flags.setOrigAlign(OriginalAlignment);
5466
5467 MVT RegisterVT = getRegisterType(VT);
5468 unsigned NumRegs = getNumRegisters(VT);
5469 for (unsigned i = 0; i != NumRegs; ++i) {
5470 RetVals.push_back(RegisterVT);
5471 ISD::ArgFlagsTy MyFlags = Flags;
5472 if (NumRegs > 1 && i == 0)
5473 MyFlags.setSplit();
5474 // if it isn't first piece, alignment must be 1
5475 else if (i > 0)
5476 MyFlags.setOrigAlign(1);
5477 Ops.push_back(DAG.getArgFlags(MyFlags));
5478 }
5479 }
5480 }
5481
5482 RetVals.push_back(MVT::Other);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005483
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005484 // Create the node.
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005485 SDNode *Result = DAG.getNode(ISD::FORMAL_ARGUMENTS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005486 DAG.getVTList(&RetVals[0], RetVals.size()),
5487 &Ops[0], Ops.size()).getNode();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005488
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005489 // Prelower FORMAL_ARGUMENTS. This isn't required for functionality, but
5490 // allows exposing the loads that may be part of the argument access to the
5491 // first DAGCombiner pass.
5492 SDValue TmpRes = LowerOperation(SDValue(Result, 0), DAG);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005493
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005494 // The number of results should match up, except that the lowered one may have
5495 // an extra flag result.
5496 assert((Result->getNumValues() == TmpRes.getNode()->getNumValues() ||
5497 (Result->getNumValues()+1 == TmpRes.getNode()->getNumValues() &&
5498 TmpRes.getValue(Result->getNumValues()).getValueType() == MVT::Flag))
5499 && "Lowering produced unexpected number of results!");
5500
5501 // The FORMAL_ARGUMENTS node itself is likely no longer needed.
5502 if (Result != TmpRes.getNode() && Result->use_empty()) {
5503 HandleSDNode Dummy(DAG.getRoot());
5504 DAG.RemoveDeadNode(Result);
5505 }
5506
5507 Result = TmpRes.getNode();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005508
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005509 unsigned NumArgRegs = Result->getNumValues() - 1;
5510 DAG.setRoot(SDValue(Result, NumArgRegs));
5511
5512 // Set up the return result vector.
5513 unsigned i = 0;
5514 unsigned Idx = 1;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005515 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005516 ++I, ++Idx) {
5517 SmallVector<MVT, 4> ValueVTs;
5518 ComputeValueVTs(*this, I->getType(), ValueVTs);
5519 for (unsigned Value = 0, NumValues = ValueVTs.size();
5520 Value != NumValues; ++Value) {
5521 MVT VT = ValueVTs[Value];
5522 MVT PartVT = getRegisterType(VT);
5523
5524 unsigned NumParts = getNumRegisters(VT);
5525 SmallVector<SDValue, 4> Parts(NumParts);
5526 for (unsigned j = 0; j != NumParts; ++j)
5527 Parts[j] = SDValue(Result, i++);
5528
5529 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Devang Patel05988662008-09-25 21:00:45 +00005530 if (F.paramHasAttr(Idx, Attribute::SExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005531 AssertOp = ISD::AssertSext;
Devang Patel05988662008-09-25 21:00:45 +00005532 else if (F.paramHasAttr(Idx, Attribute::ZExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005533 AssertOp = ISD::AssertZext;
5534
Dale Johannesen66978ee2009-01-31 02:22:37 +00005535 ArgValues.push_back(getCopyFromParts(DAG, dl, &Parts[0], NumParts,
5536 PartVT, VT, AssertOp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005537 }
5538 }
5539 assert(i == NumArgRegs && "Argument register count mismatch!");
5540}
5541
5542
5543/// TargetLowering::LowerCallTo - This is the default LowerCallTo
5544/// implementation, which just inserts an ISD::CALL node, which is later custom
5545/// lowered by the target to something concrete. FIXME: When all targets are
5546/// migrated to using ISD::CALL, this hook should be integrated into SDISel.
5547std::pair<SDValue, SDValue>
5548TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5549 bool RetSExt, bool RetZExt, bool isVarArg,
Dale Johannesen86098bd2008-09-26 19:31:26 +00005550 bool isInreg,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005551 unsigned CallingConv, bool isTailCall,
5552 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005553 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00005554 assert((!isTailCall || PerformTailCallOpt) &&
5555 "isTailCall set when tail-call optimizations are disabled!");
5556
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005557 SmallVector<SDValue, 32> Ops;
5558 Ops.push_back(Chain); // Op#0 - Chain
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005559 Ops.push_back(Callee);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005560
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005561 // Handle all of the outgoing arguments.
5562 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
5563 SmallVector<MVT, 4> ValueVTs;
5564 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5565 for (unsigned Value = 0, NumValues = ValueVTs.size();
5566 Value != NumValues; ++Value) {
5567 MVT VT = ValueVTs[Value];
5568 const Type *ArgTy = VT.getTypeForMVT();
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005569 SDValue Op = SDValue(Args[i].Node.getNode(),
5570 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005571 ISD::ArgFlagsTy Flags;
5572 unsigned OriginalAlignment =
5573 getTargetData()->getABITypeAlignment(ArgTy);
5574
5575 if (Args[i].isZExt)
5576 Flags.setZExt();
5577 if (Args[i].isSExt)
5578 Flags.setSExt();
5579 if (Args[i].isInReg)
5580 Flags.setInReg();
5581 if (Args[i].isSRet)
5582 Flags.setSRet();
5583 if (Args[i].isByVal) {
5584 Flags.setByVal();
5585 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5586 const Type *ElementTy = Ty->getElementType();
5587 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sandsceb4d1a2009-01-12 20:38:59 +00005588 unsigned FrameSize = getTargetData()->getTypePaddedSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005589 // For ByVal, alignment should come from FE. BE will guess if this
5590 // info is not there but there are cases it cannot get right.
5591 if (Args[i].Alignment)
5592 FrameAlign = Args[i].Alignment;
5593 Flags.setByValAlign(FrameAlign);
5594 Flags.setByValSize(FrameSize);
5595 }
5596 if (Args[i].isNest)
5597 Flags.setNest();
5598 Flags.setOrigAlign(OriginalAlignment);
5599
5600 MVT PartVT = getRegisterType(VT);
5601 unsigned NumParts = getNumRegisters(VT);
5602 SmallVector<SDValue, 4> Parts(NumParts);
5603 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5604
5605 if (Args[i].isSExt)
5606 ExtendKind = ISD::SIGN_EXTEND;
5607 else if (Args[i].isZExt)
5608 ExtendKind = ISD::ZERO_EXTEND;
5609
Dale Johannesen66978ee2009-01-31 02:22:37 +00005610 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005611
5612 for (unsigned i = 0; i != NumParts; ++i) {
5613 // if it isn't first piece, alignment must be 1
5614 ISD::ArgFlagsTy MyFlags = Flags;
5615 if (NumParts > 1 && i == 0)
5616 MyFlags.setSplit();
5617 else if (i != 0)
5618 MyFlags.setOrigAlign(1);
5619
5620 Ops.push_back(Parts[i]);
5621 Ops.push_back(DAG.getArgFlags(MyFlags));
5622 }
5623 }
5624 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005625
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626 // Figure out the result value types. We start by making a list of
5627 // the potentially illegal return value types.
5628 SmallVector<MVT, 4> LoweredRetTys;
5629 SmallVector<MVT, 4> RetTys;
5630 ComputeValueVTs(*this, RetTy, RetTys);
5631
5632 // Then we translate that to a list of legal types.
5633 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5634 MVT VT = RetTys[I];
5635 MVT RegisterVT = getRegisterType(VT);
5636 unsigned NumRegs = getNumRegisters(VT);
5637 for (unsigned i = 0; i != NumRegs; ++i)
5638 LoweredRetTys.push_back(RegisterVT);
5639 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005640
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005641 LoweredRetTys.push_back(MVT::Other); // Always has a chain.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005642
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005643 // Create the CALL node.
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005644 SDValue Res = DAG.getCall(CallingConv, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005645 isVarArg, isTailCall, isInreg,
Dan Gohman095cc292008-09-13 01:54:27 +00005646 DAG.getVTList(&LoweredRetTys[0],
5647 LoweredRetTys.size()),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005648 &Ops[0], Ops.size()
5649 );
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005650 Chain = Res.getValue(LoweredRetTys.size() - 1);
5651
5652 // Gather up the call result into a single value.
Dan Gohmanb5cc34d2008-10-07 00:12:37 +00005653 if (RetTy != Type::VoidTy && !RetTys.empty()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5655
5656 if (RetSExt)
5657 AssertOp = ISD::AssertSext;
5658 else if (RetZExt)
5659 AssertOp = ISD::AssertZext;
5660
5661 SmallVector<SDValue, 4> ReturnValues;
5662 unsigned RegNo = 0;
5663 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5664 MVT VT = RetTys[I];
5665 MVT RegisterVT = getRegisterType(VT);
5666 unsigned NumRegs = getNumRegisters(VT);
5667 unsigned RegNoEnd = NumRegs + RegNo;
5668 SmallVector<SDValue, 4> Results;
5669 for (; RegNo != RegNoEnd; ++RegNo)
5670 Results.push_back(Res.getValue(RegNo));
5671 SDValue ReturnValue =
Dale Johannesen66978ee2009-01-31 02:22:37 +00005672 getCopyFromParts(DAG, dl, &Results[0], NumRegs, RegisterVT, VT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005673 AssertOp);
5674 ReturnValues.push_back(ReturnValue);
5675 }
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005676 Res = DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00005677 DAG.getVTList(&RetTys[0], RetTys.size()),
5678 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005679 }
5680
5681 return std::make_pair(Res, Chain);
5682}
5683
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005684void TargetLowering::LowerOperationWrapper(SDNode *N,
5685 SmallVectorImpl<SDValue> &Results,
5686 SelectionDAG &DAG) {
5687 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005688 if (Res.getNode())
5689 Results.push_back(Res);
5690}
5691
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005692SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
5693 assert(0 && "LowerOperation not implemented for this target!");
5694 abort();
5695 return SDValue();
5696}
5697
5698
5699void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
5700 SDValue Op = getValue(V);
5701 assert((Op.getOpcode() != ISD::CopyFromReg ||
5702 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5703 "Copy from a reg to the same reg!");
5704 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5705
5706 RegsForValue RFV(TLI, Reg, V->getType());
5707 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005708 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709 PendingExports.push_back(Chain);
5710}
5711
5712#include "llvm/CodeGen/SelectionDAGISel.h"
5713
5714void SelectionDAGISel::
5715LowerArguments(BasicBlock *LLVMBB) {
5716 // If this is the entry block, emit arguments.
5717 Function &F = *LLVMBB->getParent();
5718 SDValue OldRoot = SDL->DAG.getRoot();
5719 SmallVector<SDValue, 16> Args;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005720 TLI.LowerArguments(F, SDL->DAG, Args, SDL->getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005721
5722 unsigned a = 0;
5723 for (Function::arg_iterator AI = F.arg_begin(), E = F.arg_end();
5724 AI != E; ++AI) {
5725 SmallVector<MVT, 4> ValueVTs;
5726 ComputeValueVTs(TLI, AI->getType(), ValueVTs);
5727 unsigned NumValues = ValueVTs.size();
5728 if (!AI->use_empty()) {
5729 SDL->setValue(AI, SDL->DAG.getMergeValues(&Args[a], NumValues));
5730 // If this argument is live outside of the entry block, insert a copy from
5731 // whereever we got it to the vreg that other BB's will reference it as.
5732 DenseMap<const Value*, unsigned>::iterator VMI=FuncInfo->ValueMap.find(AI);
5733 if (VMI != FuncInfo->ValueMap.end()) {
5734 SDL->CopyValueToVirtualRegister(AI, VMI->second);
5735 }
5736 }
5737 a += NumValues;
5738 }
5739
5740 // Finally, if the target has anything special to do, allow it to do so.
5741 // FIXME: this should insert code into the DAG!
5742 EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction());
5743}
5744
5745/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5746/// ensure constants are generated when needed. Remember the virtual registers
5747/// that need to be added to the Machine PHI nodes as input. We cannot just
5748/// directly add them, because expansion might result in multiple MBB's for one
5749/// BB. As such, the start of the BB might correspond to a different MBB than
5750/// the end.
5751///
5752void
5753SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5754 TerminatorInst *TI = LLVMBB->getTerminator();
5755
5756 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5757
5758 // Check successor nodes' PHI nodes that expect a constant to be available
5759 // from this block.
5760 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5761 BasicBlock *SuccBB = TI->getSuccessor(succ);
5762 if (!isa<PHINode>(SuccBB->begin())) continue;
5763 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005764
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005765 // If this terminator has multiple identical successors (common for
5766 // switches), only handle each succ once.
5767 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005768
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005769 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5770 PHINode *PN;
5771
5772 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5773 // nodes and Machine PHI nodes, but the incoming operands have not been
5774 // emitted yet.
5775 for (BasicBlock::iterator I = SuccBB->begin();
5776 (PN = dyn_cast<PHINode>(I)); ++I) {
5777 // Ignore dead phi's.
5778 if (PN->use_empty()) continue;
5779
5780 unsigned Reg;
5781 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5782
5783 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
5784 unsigned &RegOut = SDL->ConstantsOut[C];
5785 if (RegOut == 0) {
5786 RegOut = FuncInfo->CreateRegForValue(C);
5787 SDL->CopyValueToVirtualRegister(C, RegOut);
5788 }
5789 Reg = RegOut;
5790 } else {
5791 Reg = FuncInfo->ValueMap[PHIOp];
5792 if (Reg == 0) {
5793 assert(isa<AllocaInst>(PHIOp) &&
5794 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5795 "Didn't codegen value into a register!??");
5796 Reg = FuncInfo->CreateRegForValue(PHIOp);
5797 SDL->CopyValueToVirtualRegister(PHIOp, Reg);
5798 }
5799 }
5800
5801 // Remember that this register needs to added to the machine PHI node as
5802 // the input for this MBB.
5803 SmallVector<MVT, 4> ValueVTs;
5804 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5805 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
5806 MVT VT = ValueVTs[vti];
5807 unsigned NumRegisters = TLI.getNumRegisters(VT);
5808 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
5809 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
5810 Reg += NumRegisters;
5811 }
5812 }
5813 }
5814 SDL->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005815}
5816
Dan Gohman3df24e62008-09-03 23:12:08 +00005817/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5818/// supports legal types, and it emits MachineInstrs directly instead of
5819/// creating SelectionDAG nodes.
5820///
5821bool
5822SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5823 FastISel *F) {
5824 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005825
Dan Gohman3df24e62008-09-03 23:12:08 +00005826 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5827 unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size();
5828
5829 // Check successor nodes' PHI nodes that expect a constant to be available
5830 // from this block.
5831 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5832 BasicBlock *SuccBB = TI->getSuccessor(succ);
5833 if (!isa<PHINode>(SuccBB->begin())) continue;
5834 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005835
Dan Gohman3df24e62008-09-03 23:12:08 +00005836 // If this terminator has multiple identical successors (common for
5837 // switches), only handle each succ once.
5838 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005839
Dan Gohman3df24e62008-09-03 23:12:08 +00005840 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5841 PHINode *PN;
5842
5843 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5844 // nodes and Machine PHI nodes, but the incoming operands have not been
5845 // emitted yet.
5846 for (BasicBlock::iterator I = SuccBB->begin();
5847 (PN = dyn_cast<PHINode>(I)); ++I) {
5848 // Ignore dead phi's.
5849 if (PN->use_empty()) continue;
5850
5851 // Only handle legal types. Two interesting things to note here. First,
5852 // by bailing out early, we may leave behind some dead instructions,
5853 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
5854 // own moves. Second, this check is necessary becuase FastISel doesn't
5855 // use CreateRegForValue to create registers, so it always creates
5856 // exactly one register for each non-void instruction.
5857 MVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
5858 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
Dan Gohman74321ab2008-09-10 21:01:31 +00005859 // Promote MVT::i1.
5860 if (VT == MVT::i1)
5861 VT = TLI.getTypeToTransformTo(VT);
5862 else {
5863 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5864 return false;
5865 }
Dan Gohman3df24e62008-09-03 23:12:08 +00005866 }
5867
5868 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5869
5870 unsigned Reg = F->getRegForValue(PHIOp);
5871 if (Reg == 0) {
5872 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5873 return false;
5874 }
5875 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
5876 }
5877 }
5878
5879 return true;
5880}