blob: 1ff1cd30f241eae51d355a458d818660372fb865 [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"
Dan Gohman98ca4f22009-08-05 01:29:28 +000020#include "llvm/Constants.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000021#include "llvm/CallingConv.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/Function.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/InlineAsm.h"
26#include "llvm/Instructions.h"
27#include "llvm/Intrinsics.h"
28#include "llvm/IntrinsicInst.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000029#include "llvm/Module.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000030#include "llvm/CodeGen/FastISel.h"
31#include "llvm/CodeGen/GCStrategy.h"
32#include "llvm/CodeGen/GCMetadata.h"
33#include "llvm/CodeGen/MachineFunction.h"
34#include "llvm/CodeGen/MachineFrameInfo.h"
35#include "llvm/CodeGen/MachineInstrBuilder.h"
36#include "llvm/CodeGen/MachineJumpTableInfo.h"
37#include "llvm/CodeGen/MachineModuleInfo.h"
38#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000039#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000040#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000041#include "llvm/CodeGen/DwarfWriter.h"
42#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000043#include "llvm/Target/TargetRegisterInfo.h"
44#include "llvm/Target/TargetData.h"
45#include "llvm/Target/TargetFrameInfo.h"
46#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000047#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000048#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000049#include "llvm/Target/TargetOptions.h"
50#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000051#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000052#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000053#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000054#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000055#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000056#include <algorithm>
57using namespace llvm;
58
Dale Johannesen601d3c02008-09-05 01:48:15 +000059/// LimitFloatPrecision - Generate low-precision inline sequences for
60/// some float libcalls (6, 8 or 12 bits).
61static unsigned LimitFloatPrecision;
62
63static cl::opt<unsigned, true>
64LimitFPPrecision("limit-float-precision",
65 cl::desc("Generate low-precision inline sequences "
66 "for some float libcalls"),
67 cl::location(LimitFloatPrecision),
68 cl::init(0));
69
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000070/// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
Dan Gohman2c91d102009-01-06 22:53:52 +000071/// of insertvalue or extractvalue indices that identify a member, return
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000072/// the linearized index of the start of the member.
73///
74static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty,
75 const unsigned *Indices,
76 const unsigned *IndicesEnd,
77 unsigned CurIndex = 0) {
78 // Base case: We're done.
79 if (Indices && Indices == IndicesEnd)
80 return CurIndex;
81
82 // Given a struct type, recursively traverse the elements.
83 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
84 for (StructType::element_iterator EB = STy->element_begin(),
85 EI = EB,
86 EE = STy->element_end();
87 EI != EE; ++EI) {
88 if (Indices && *Indices == unsigned(EI - EB))
89 return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex);
90 CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex);
91 }
Dan Gohman2c91d102009-01-06 22:53:52 +000092 return CurIndex;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000093 }
94 // Given an array type, recursively traverse the elements.
95 else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
96 const Type *EltTy = ATy->getElementType();
97 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
98 if (Indices && *Indices == i)
99 return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex);
100 CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex);
101 }
Dan Gohman2c91d102009-01-06 22:53:52 +0000102 return CurIndex;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000103 }
104 // We haven't found the type we're looking for, so keep searching.
105 return CurIndex + 1;
106}
107
108/// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
Owen Andersone50ed302009-08-10 22:56:29 +0000109/// EVTs that represent all the individual underlying
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110/// non-aggregate types that comprise it.
111///
112/// If Offsets is non-null, it points to a vector to be filled in
113/// with the in-memory offsets of each of the individual values.
114///
115static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty,
Owen Andersone50ed302009-08-10 22:56:29 +0000116 SmallVectorImpl<EVT> &ValueVTs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000117 SmallVectorImpl<uint64_t> *Offsets = 0,
118 uint64_t StartingOffset = 0) {
119 // Given a struct type, recursively traverse the elements.
120 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
121 const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy);
122 for (StructType::element_iterator EB = STy->element_begin(),
123 EI = EB,
124 EE = STy->element_end();
125 EI != EE; ++EI)
126 ComputeValueVTs(TLI, *EI, ValueVTs, Offsets,
127 StartingOffset + SL->getElementOffset(EI - EB));
128 return;
129 }
130 // Given an array type, recursively traverse the elements.
131 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
132 const Type *EltTy = ATy->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000133 uint64_t EltSize = TLI.getTargetData()->getTypeAllocSize(EltTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
135 ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets,
136 StartingOffset + i * EltSize);
137 return;
138 }
Dan Gohman5e5558b2009-04-23 22:50:03 +0000139 // Interpret void as zero return values.
Owen Anderson1d0be152009-08-13 21:58:54 +0000140 if (Ty == Type::getVoidTy(Ty->getContext()))
Dan Gohman5e5558b2009-04-23 22:50:03 +0000141 return;
Owen Andersone50ed302009-08-10 22:56:29 +0000142 // Base case: we can get an EVT for this LLVM IR type.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000143 ValueVTs.push_back(TLI.getValueType(Ty));
144 if (Offsets)
145 Offsets->push_back(StartingOffset);
146}
147
Dan Gohman2a7c6712008-09-03 23:18:39 +0000148namespace llvm {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000149 /// RegsForValue - This struct represents the registers (physical or virtual)
150 /// that a particular set of values is assigned, and the type information about
151 /// the value. The most common situation is to represent one value at a time,
152 /// but struct or array values are handled element-wise as multiple values.
153 /// The splitting of aggregates is performed recursively, so that we never
154 /// have aggregate-typed registers. The values at this point do not necessarily
155 /// have legal types, so each value may require one or more registers of some
156 /// legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000157 ///
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000158 struct VISIBILITY_HIDDEN RegsForValue {
159 /// TLI - The TargetLowering object.
160 ///
161 const TargetLowering *TLI;
162
163 /// ValueVTs - The value types of the values, which may not be legal, and
164 /// may need be promoted or synthesized from one or more registers.
165 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000166 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000167
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000168 /// RegVTs - The value types of the registers. This is the same size as
169 /// ValueVTs and it records, for each value, what the type of the assigned
170 /// register or registers are. (Individual values are never synthesized
171 /// from more than one type of register.)
172 ///
173 /// With virtual registers, the contents of RegVTs is redundant with TLI's
174 /// getRegisterType member function, however when with physical registers
175 /// it is necessary to have a separate record of the types.
176 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000177 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000178
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000179 /// Regs - This list holds the registers assigned to the values.
180 /// Each legal or promoted value requires one register, and each
181 /// expanded value requires multiple registers.
182 ///
183 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000185 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000186
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000187 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000188 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000189 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000190 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
191 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000192 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000193 const SmallVector<EVT, 4> &regvts,
194 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000196 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000197 unsigned Reg, const Type *Ty) : TLI(&tli) {
198 ComputeValueVTs(tli, Ty, ValueVTs);
199
200 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000201 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000202 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
203 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000204 for (unsigned i = 0; i != NumRegs; ++i)
205 Regs.push_back(Reg + i);
206 RegVTs.push_back(RegisterVT);
207 Reg += NumRegs;
208 }
209 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000211 /// append - Add the specified values to this one.
212 void append(const RegsForValue &RHS) {
213 TLI = RHS.TLI;
214 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
215 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
216 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
217 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000218
219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000220 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000221 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000222 /// Chain/Flag as the input and updates them for the output Chain/Flag.
223 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000224 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000225 SDValue &Chain, SDValue *Flag) const;
226
227 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000228 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000229 /// Chain/Flag as the input and updates them for the output Chain/Flag.
230 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000231 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000232 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000233
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000234 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000235 /// operand list. This adds the code marker, matching input operand index
236 /// (if applicable), and includes the number of values added into it.
237 void AddInlineAsmOperands(unsigned Code,
238 bool HasMatching, unsigned MatchingIdx,
239 SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000240 };
241}
242
243/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000244/// PHI nodes or outside of the basic block that defines it, or used by a
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000245/// switch or atomic instruction, which may expand to multiple basic blocks.
246static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
247 if (isa<PHINode>(I)) return true;
248 BasicBlock *BB = I->getParent();
249 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
Dan Gohman8e5c0da2009-04-09 02:33:36 +0000250 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000251 return true;
252 return false;
253}
254
255/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
256/// entry block, return true. This includes arguments used by switches, since
257/// the switch may expand into multiple basic blocks.
258static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) {
259 // With FastISel active, we may be splitting blocks, so force creation
260 // of virtual registers for all non-dead arguments.
Dan Gohman33134c42008-09-25 17:05:24 +0000261 // Don't force virtual registers for byval arguments though, because
262 // fast-isel can't handle those in all cases.
263 if (EnableFastISel && !A->hasByValAttr())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000264 return A->use_empty();
265
266 BasicBlock *Entry = A->getParent()->begin();
267 for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
268 if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
269 return false; // Use not in entry block.
270 return true;
271}
272
273FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli)
274 : TLI(tli) {
275}
276
277void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf,
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000278 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000279 bool EnableFastISel) {
280 Fn = &fn;
281 MF = &mf;
282 RegInfo = &MF->getRegInfo();
283
284 // Create a vreg for each argument register that is not dead and is used
285 // outside of the entry block for the function.
286 for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
287 AI != E; ++AI)
288 if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
289 InitializeRegForValue(AI);
290
291 // Initialize the mapping of values to registers. This is only set up for
292 // instruction values that are used outside of the block that defines
293 // them.
294 Function::iterator BB = Fn->begin(), EB = Fn->end();
295 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
296 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
297 if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
298 const Type *Ty = AI->getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +0000299 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000300 unsigned Align =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000301 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
302 AI->getAlignment());
303
304 TySize *= CUI->getZExtValue(); // Get total allocated size.
305 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
306 StaticAllocaMap[AI] =
307 MF->getFrameInfo()->CreateStackObject(TySize, Align);
308 }
309
310 for (; BB != EB; ++BB)
311 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
312 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
313 if (!isa<AllocaInst>(I) ||
314 !StaticAllocaMap.count(cast<AllocaInst>(I)))
315 InitializeRegForValue(I);
316
317 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
318 // also creates the initial PHI MachineInstrs, though none of the input
319 // operands are populated.
320 for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) {
321 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
322 MBBMap[BB] = MBB;
323 MF->push_back(MBB);
324
Dan Gohman8c2b5252009-10-30 01:27:03 +0000325 // Transfer the address-taken flag. This is necessary because there could
326 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
327 // the first one should be marked.
328 if (BB->hasAddressTaken())
329 MBB->setHasAddressTaken();
330
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000331 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
332 // appropriate.
333 PHINode *PN;
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000334 DebugLoc DL;
335 for (BasicBlock::iterator
336 I = BB->begin(), E = BB->end(); I != E; ++I) {
337 if (CallInst *CI = dyn_cast<CallInst>(I)) {
338 if (Function *F = CI->getCalledFunction()) {
339 switch (F->getIntrinsicID()) {
340 default: break;
341 case Intrinsic::dbg_stoppoint: {
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000342 DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000343 if (isValidDebugInfoIntrinsic(*SPI, CodeGenOpt::Default))
344 DL = ExtractDebugLocation(*SPI, MF->getDebugLocInfo());
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000345 break;
346 }
347 case Intrinsic::dbg_func_start: {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +0000348 DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000349 if (isValidDebugInfoIntrinsic(*FSI, CodeGenOpt::Default))
350 DL = ExtractDebugLocation(*FSI, MF->getDebugLocInfo());
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000351 break;
352 }
353 }
354 }
355 }
356
357 PN = dyn_cast<PHINode>(I);
358 if (!PN || PN->use_empty()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000359
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000360 unsigned PHIReg = ValueMap[PN];
361 assert(PHIReg && "PHI node does not have an assigned virtual register!");
362
Owen Andersone50ed302009-08-10 22:56:29 +0000363 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000364 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
365 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +0000366 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +0000367 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohman6448d912008-09-04 15:39:15 +0000368 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000369 for (unsigned i = 0; i != NumRegisters; ++i)
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000370 BuildMI(MBB, DL, TII->get(TargetInstrInfo::PHI), PHIReg + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000371 PHIReg += NumRegisters;
372 }
373 }
374 }
375}
376
Owen Andersone50ed302009-08-10 22:56:29 +0000377unsigned FunctionLoweringInfo::MakeReg(EVT VT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000378 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
379}
380
381/// CreateRegForValue - Allocate the appropriate number of virtual registers of
382/// the correctly promoted or expanded types. Assign these registers
383/// consecutive vreg numbers and return the first assigned number.
384///
385/// In the case that the given value has struct or array type, this function
386/// will assign registers for each member or element.
387///
388unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000389 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000390 ComputeValueVTs(TLI, V->getType(), ValueVTs);
391
392 unsigned FirstReg = 0;
393 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000394 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000395 EVT RegisterVT = TLI.getRegisterType(V->getContext(), ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000396
Owen Anderson23b9b192009-08-12 00:36:31 +0000397 unsigned NumRegs = TLI.getNumRegisters(V->getContext(), ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000398 for (unsigned i = 0; i != NumRegs; ++i) {
399 unsigned R = MakeReg(RegisterVT);
400 if (!FirstReg) FirstReg = R;
401 }
402 }
403 return FirstReg;
404}
405
406/// getCopyFromParts - Create a value that contains the specified legal parts
407/// combined into the value they represent. If the parts combine to a type
408/// larger then ValueVT then AssertOp can be used to specify whether the extra
409/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
410/// (ISD::AssertSext).
Dale Johannesen66978ee2009-01-31 02:22:37 +0000411static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
412 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000413 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000414 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000415 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000416 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000417 SDValue Val = Parts[0];
418
419 if (NumParts > 1) {
420 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000421 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000422 unsigned PartBits = PartVT.getSizeInBits();
423 unsigned ValueBits = ValueVT.getSizeInBits();
424
425 // Assemble the power of 2 part.
426 unsigned RoundParts = NumParts & (NumParts - 1) ?
427 1 << Log2_32(NumParts) : NumParts;
428 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000429 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000430 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000431 SDValue Lo, Hi;
432
Owen Anderson23b9b192009-08-12 00:36:31 +0000433 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000434
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000435 if (RoundParts > 2) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000436 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT);
437 Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000438 PartVT, HalfVT);
439 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000440 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
441 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000442 }
443 if (TLI.isBigEndian())
444 std::swap(Lo, Hi);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000445 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000446
447 if (RoundParts < NumParts) {
448 // Assemble the trailing non-power-of-2 part.
449 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000450 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Scott Michelfdc40a02009-02-17 22:15:04 +0000451 Hi = getCopyFromParts(DAG, dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000452 Parts+RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000453
454 // Combine the round and odd parts.
455 Lo = Val;
456 if (TLI.isBigEndian())
457 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000458 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000459 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
460 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000461 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000462 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000463 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
464 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000465 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000466 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000467 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000468 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000469 unsigned NumIntermediates;
470 unsigned NumRegs =
Owen Anderson23b9b192009-08-12 00:36:31 +0000471 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
472 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000473 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
474 NumParts = NumRegs; // Silence a compiler warning.
475 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
476 assert(RegisterVT == Parts[0].getValueType() &&
477 "Part type doesn't match part!");
478
479 // Assemble the parts into intermediate operands.
480 SmallVector<SDValue, 8> Ops(NumIntermediates);
481 if (NumIntermediates == NumParts) {
482 // If the register was not expanded, truncate or copy the value,
483 // as appropriate.
484 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000485 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000486 PartVT, IntermediateVT);
487 } else if (NumParts > 0) {
488 // If the intermediate type was expanded, build the intermediate operands
489 // from the parts.
490 assert(NumParts % NumIntermediates == 0 &&
491 "Must expand into a divisible number of parts!");
492 unsigned Factor = NumParts / NumIntermediates;
493 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000494 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000495 PartVT, IntermediateVT);
496 }
497
498 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
499 // operands.
500 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000501 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000502 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000503 } else if (PartVT.isFloatingPoint()) {
504 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000505 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000506 "Unexpected split");
507 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000508 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
509 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000510 if (TLI.isBigEndian())
511 std::swap(Lo, Hi);
512 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
513 } else {
514 // FP split into integer parts (soft fp)
515 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
516 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000517 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Eli Friedman2ac8b322009-05-20 06:02:09 +0000518 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000519 }
520 }
521
522 // There is now one part, held in Val. Correct it to match ValueVT.
523 PartVT = Val.getValueType();
524
525 if (PartVT == ValueVT)
526 return Val;
527
528 if (PartVT.isVector()) {
529 assert(ValueVT.isVector() && "Unknown vector conversion!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000530 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000531 }
532
533 if (ValueVT.isVector()) {
534 assert(ValueVT.getVectorElementType() == PartVT &&
535 ValueVT.getVectorNumElements() == 1 &&
536 "Only trivial scalar-to-vector conversions should get here!");
Evan Chenga87008d2009-02-25 22:49:59 +0000537 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000538 }
539
540 if (PartVT.isInteger() &&
541 ValueVT.isInteger()) {
542 if (ValueVT.bitsLT(PartVT)) {
543 // For a truncate, see if we have any information to
544 // indicate whether the truncated bits will always be
545 // zero or sign-extension.
546 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000547 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000548 DAG.getValueType(ValueVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000549 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000550 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000551 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000552 }
553 }
554
555 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
556 if (ValueVT.bitsLT(Val.getValueType()))
557 // FP_ROUND's are always exact here.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000558 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000559 DAG.getIntPtrConstant(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000560 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000561 }
562
563 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000564 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000565
Torok Edwinc23197a2009-07-14 16:55:14 +0000566 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000567 return SDValue();
568}
569
570/// getCopyToParts - Create a series of nodes that contain the specified value
571/// split into legal parts. If the parts contain more bits than Val, then, for
572/// integers, ExtendKind can be used to specify how to generate the extra bits.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000573static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val,
Owen Andersone50ed302009-08-10 22:56:29 +0000574 SDValue *Parts, unsigned NumParts, EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000575 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000576 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000577 EVT PtrVT = TLI.getPointerTy();
578 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000579 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000580 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000581 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
582
583 if (!NumParts)
584 return;
585
586 if (!ValueVT.isVector()) {
587 if (PartVT == ValueVT) {
588 assert(NumParts == 1 && "No-op copy with multiple parts!");
589 Parts[0] = Val;
590 return;
591 }
592
593 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
594 // If the parts cover more bits than the value has, promote the value.
595 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
596 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000597 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000598 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000599 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000600 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000601 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000602 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000603 }
604 } else if (PartBits == ValueVT.getSizeInBits()) {
605 // Different types of the same size.
606 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000607 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000608 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
609 // If the parts cover less bits than value has, truncate the value.
610 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000611 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000612 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000613 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000614 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000615 }
616 }
617
618 // The value may have changed - recompute ValueVT.
619 ValueVT = Val.getValueType();
620 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
621 "Failed to tile the value with PartVT!");
622
623 if (NumParts == 1) {
624 assert(PartVT == ValueVT && "Type conversion failed!");
625 Parts[0] = Val;
626 return;
627 }
628
629 // Expand the value into multiple parts.
630 if (NumParts & (NumParts - 1)) {
631 // The number of parts is not a power of 2. Split off and copy the tail.
632 assert(PartVT.isInteger() && ValueVT.isInteger() &&
633 "Do not know what to expand to!");
634 unsigned RoundParts = 1 << Log2_32(NumParts);
635 unsigned RoundBits = RoundParts * PartBits;
636 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000637 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000638 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000639 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000640 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000641 if (TLI.isBigEndian())
642 // The odd parts were reversed by getCopyToParts - unreverse them.
643 std::reverse(Parts + RoundParts, Parts + NumParts);
644 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000645 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000646 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000647 }
648
649 // The number of parts is a power of 2. Repeatedly bisect the value using
650 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000651 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson23b9b192009-08-12 00:36:31 +0000652 EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000653 Val);
654 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
655 for (unsigned i = 0; i < NumParts; i += StepSize) {
656 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000657 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000658 SDValue &Part0 = Parts[i];
659 SDValue &Part1 = Parts[i+StepSize/2];
660
Scott Michelfdc40a02009-02-17 22:15:04 +0000661 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000662 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000663 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000664 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000665 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000666 DAG.getConstant(0, PtrVT));
667
668 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000669 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000670 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000671 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000672 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000673 }
674 }
675 }
676
677 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000678 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000679
680 return;
681 }
682
683 // Vector ValueVT.
684 if (NumParts == 1) {
685 if (PartVT != ValueVT) {
686 if (PartVT.isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000687 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000688 } else {
689 assert(ValueVT.getVectorElementType() == PartVT &&
690 ValueVT.getVectorNumElements() == 1 &&
691 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000692 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000693 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000694 DAG.getConstant(0, PtrVT));
695 }
696 }
697
698 Parts[0] = Val;
699 return;
700 }
701
702 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000703 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000704 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000705 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
706 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000707 unsigned NumElements = ValueVT.getVectorNumElements();
708
709 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
710 NumParts = NumRegs; // Silence a compiler warning.
711 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
712
713 // Split the vector into intermediate operands.
714 SmallVector<SDValue, 8> Ops(NumIntermediates);
715 for (unsigned i = 0; i != NumIntermediates; ++i)
716 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000717 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000718 IntermediateVT, Val,
719 DAG.getConstant(i * (NumElements / NumIntermediates),
720 PtrVT));
721 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000722 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000723 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000724 DAG.getConstant(i, PtrVT));
725
726 // Split the intermediate operands into legal parts.
727 if (NumParts == NumIntermediates) {
728 // If the register was not expanded, promote or copy the value,
729 // as appropriate.
730 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000731 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000732 } else if (NumParts > 0) {
733 // If the intermediate type was expanded, split each the value into
734 // legal parts.
735 assert(NumParts % NumIntermediates == 0 &&
736 "Must expand into a divisible number of parts!");
737 unsigned Factor = NumParts / NumIntermediates;
738 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000739 getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000740 }
741}
742
743
744void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
745 AA = &aa;
746 GFI = gfi;
747 TD = DAG.getTarget().getTargetData();
748}
749
750/// clear - Clear out the curret SelectionDAG and the associated
751/// state and prepare this SelectionDAGLowering object to be used
752/// for a new block. This doesn't clear out information about
753/// additional blocks that are needed to complete switch lowering
754/// or PHI node updating; that information is cleared out as it is
755/// consumed.
756void SelectionDAGLowering::clear() {
757 NodeMap.clear();
758 PendingLoads.clear();
759 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000760 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000761 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000762 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000763 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000764}
765
766/// getRoot - Return the current virtual root of the Selection DAG,
767/// flushing any PendingLoad items. This must be done before emitting
768/// a store or any other node that may need to be ordered after any
769/// prior load instructions.
770///
771SDValue SelectionDAGLowering::getRoot() {
772 if (PendingLoads.empty())
773 return DAG.getRoot();
774
775 if (PendingLoads.size() == 1) {
776 SDValue Root = PendingLoads[0];
777 DAG.setRoot(Root);
778 PendingLoads.clear();
779 return Root;
780 }
781
782 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000783 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000784 &PendingLoads[0], PendingLoads.size());
785 PendingLoads.clear();
786 DAG.setRoot(Root);
787 return Root;
788}
789
790/// getControlRoot - Similar to getRoot, but instead of flushing all the
791/// PendingLoad items, flush all the PendingExports items. It is necessary
792/// to do this before emitting a terminator instruction.
793///
794SDValue SelectionDAGLowering::getControlRoot() {
795 SDValue Root = DAG.getRoot();
796
797 if (PendingExports.empty())
798 return Root;
799
800 // Turn all of the CopyToReg chains into one factored node.
801 if (Root.getOpcode() != ISD::EntryToken) {
802 unsigned i = 0, e = PendingExports.size();
803 for (; i != e; ++i) {
804 assert(PendingExports[i].getNode()->getNumOperands() > 1);
805 if (PendingExports[i].getNode()->getOperand(0) == Root)
806 break; // Don't add the root if we already indirectly depend on it.
807 }
808
809 if (i == e)
810 PendingExports.push_back(Root);
811 }
812
Owen Anderson825b72b2009-08-11 20:47:22 +0000813 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000814 &PendingExports[0],
815 PendingExports.size());
816 PendingExports.clear();
817 DAG.setRoot(Root);
818 return Root;
819}
820
821void SelectionDAGLowering::visit(Instruction &I) {
822 visit(I.getOpcode(), I);
823}
824
825void SelectionDAGLowering::visit(unsigned Opcode, User &I) {
826 // Note: this doesn't use InstVisitor, because it has to work with
827 // ConstantExpr's in addition to instructions.
828 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000829 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000830 // Build the switch statement using the Instruction.def file.
831#define HANDLE_INST(NUM, OPCODE, CLASS) \
832 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
833#include "llvm/Instruction.def"
834 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000835}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000836
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000837SDValue SelectionDAGLowering::getValue(const Value *V) {
838 SDValue &N = NodeMap[V];
839 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000840
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000841 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000842 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000843
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000844 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000845 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000846
847 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
848 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000849
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000850 if (isa<ConstantPointerNull>(C))
851 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000852
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000853 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000854 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000855
Nate Begeman9008ca62009-04-27 18:41:29 +0000856 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000857 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000858
859 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
860 visit(CE->getOpcode(), *CE);
861 SDValue N1 = NodeMap[V];
862 assert(N1.getNode() && "visit didn't populate the ValueMap!");
863 return N1;
864 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000865
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000866 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
867 SmallVector<SDValue, 4> Constants;
868 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
869 OI != OE; ++OI) {
870 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000871 // If the operand is an empty aggregate, there are no values.
872 if (!Val) continue;
873 // Add each leaf value from the operand to the Constants list
874 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000875 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
876 Constants.push_back(SDValue(Val, i));
877 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000878 return DAG.getMergeValues(&Constants[0], Constants.size(),
879 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000880 }
881
882 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
883 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
884 "Unknown struct or array constant!");
885
Owen Andersone50ed302009-08-10 22:56:29 +0000886 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000887 ComputeValueVTs(TLI, C->getType(), ValueVTs);
888 unsigned NumElts = ValueVTs.size();
889 if (NumElts == 0)
890 return SDValue(); // empty struct
891 SmallVector<SDValue, 4> Constants(NumElts);
892 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000893 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000894 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000895 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000896 else if (EltVT.isFloatingPoint())
897 Constants[i] = DAG.getConstantFP(0, EltVT);
898 else
899 Constants[i] = DAG.getConstant(0, EltVT);
900 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000901 return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000902 }
903
Dan Gohman8c2b5252009-10-30 01:27:03 +0000904 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
905 return DAG.getBlockAddress(BA, getCurDebugLoc());
906
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000907 const VectorType *VecTy = cast<VectorType>(V->getType());
908 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000910 // Now that we know the number and type of the elements, get that number of
911 // elements into the Ops array based on what kind of constant it is.
912 SmallVector<SDValue, 16> Ops;
913 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
914 for (unsigned i = 0; i != NumElements; ++i)
915 Ops.push_back(getValue(CP->getOperand(i)));
916 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000917 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000918 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000919
920 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000921 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000922 Op = DAG.getConstantFP(0, EltVT);
923 else
924 Op = DAG.getConstant(0, EltVT);
925 Ops.assign(NumElements, Op);
926 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000927
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000928 // Create a BUILD_VECTOR node.
Evan Chenga87008d2009-02-25 22:49:59 +0000929 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
930 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000931 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000932
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000933 // If this is a static alloca, generate it as the frameindex instead of
934 // computation.
935 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
936 DenseMap<const AllocaInst*, int>::iterator SI =
937 FuncInfo.StaticAllocaMap.find(AI);
938 if (SI != FuncInfo.StaticAllocaMap.end())
939 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
940 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000941
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000942 unsigned InReg = FuncInfo.ValueMap[V];
943 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000944
Owen Anderson23b9b192009-08-12 00:36:31 +0000945 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000946 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +0000947 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000948}
949
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000950/// Get the EVTs and ArgFlags collections that represent the return type
951/// of the given function. This does not require a DAG or a return value, and
952/// is suitable for use before any DAGs for the function are constructed.
953static void getReturnInfo(const Function* F, SmallVectorImpl<EVT> &OutVTs,
954 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
955 TargetLowering &TLI) {
956 const Type* ReturnType = F->getReturnType();
957
958 SmallVector<EVT, 4> ValueVTs;
959 ComputeValueVTs(TLI, ReturnType, ValueVTs);
960 unsigned NumValues = ValueVTs.size();
961 if ( NumValues == 0 ) return;
962
963 for (unsigned j = 0, f = NumValues; j != f; ++j) {
964 EVT VT = ValueVTs[j];
965 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
966
967 if (F->paramHasAttr(0, Attribute::SExt))
968 ExtendKind = ISD::SIGN_EXTEND;
969 else if (F->paramHasAttr(0, Attribute::ZExt))
970 ExtendKind = ISD::ZERO_EXTEND;
971
972 // FIXME: C calling convention requires the return type to be promoted to
973 // at least 32-bit. But this is not necessary for non-C calling
974 // conventions. The frontend should mark functions whose return values
975 // require promoting with signext or zeroext attributes.
976 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
977 EVT MinVT = TLI.getRegisterType(F->getContext(), MVT::i32);
978 if (VT.bitsLT(MinVT))
979 VT = MinVT;
980 }
981
982 unsigned NumParts = TLI.getNumRegisters(F->getContext(), VT);
983 EVT PartVT = TLI.getRegisterType(F->getContext(), VT);
984 // 'inreg' on function refers to return value
985 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
986 if (F->paramHasAttr(0, Attribute::InReg))
987 Flags.setInReg();
988
989 // Propagate extension type if any
990 if (F->paramHasAttr(0, Attribute::SExt))
991 Flags.setSExt();
992 else if (F->paramHasAttr(0, Attribute::ZExt))
993 Flags.setZExt();
994
995 for (unsigned i = 0; i < NumParts; ++i)
996 {
997 OutVTs.push_back(PartVT);
998 OutFlags.push_back(Flags);
999 }
1000 }
1001}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001002
1003void SelectionDAGLowering::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001004 SDValue Chain = getControlRoot();
1005 SmallVector<ISD::OutputArg, 8> Outs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001006 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001007 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001008 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001009 unsigned NumValues = ValueVTs.size();
1010 if (NumValues == 0) continue;
1011
1012 SDValue RetOp = getValue(I.getOperand(i));
1013 for (unsigned j = 0, f = NumValues; j != f; ++j) {
Owen Andersone50ed302009-08-10 22:56:29 +00001014 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001015
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001016 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001017
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001018 const Function *F = I.getParent()->getParent();
Devang Patel05988662008-09-25 21:00:45 +00001019 if (F->paramHasAttr(0, Attribute::SExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001020 ExtendKind = ISD::SIGN_EXTEND;
Devang Patel05988662008-09-25 21:00:45 +00001021 else if (F->paramHasAttr(0, Attribute::ZExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001022 ExtendKind = ISD::ZERO_EXTEND;
1023
Evan Cheng3927f432009-03-25 20:20:11 +00001024 // FIXME: C calling convention requires the return type to be promoted to
1025 // at least 32-bit. But this is not necessary for non-C calling
1026 // conventions. The frontend should mark functions whose return values
1027 // require promoting with signext or zeroext attributes.
1028 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001029 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
Evan Cheng3927f432009-03-25 20:20:11 +00001030 if (VT.bitsLT(MinVT))
1031 VT = MinVT;
1032 }
1033
Owen Anderson23b9b192009-08-12 00:36:31 +00001034 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
1035 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
Evan Cheng3927f432009-03-25 20:20:11 +00001036 SmallVector<SDValue, 4> Parts(NumParts);
Dale Johannesen66978ee2009-01-31 02:22:37 +00001037 getCopyToParts(DAG, getCurDebugLoc(),
1038 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001039 &Parts[0], NumParts, PartVT, ExtendKind);
1040
Dale Johannesenc9c6da62008-09-25 20:47:45 +00001041 // 'inreg' on function refers to return value
1042 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Devang Patel05988662008-09-25 21:00:45 +00001043 if (F->paramHasAttr(0, Attribute::InReg))
Dale Johannesenc9c6da62008-09-25 20:47:45 +00001044 Flags.setInReg();
Anton Korobeynikov0692fab2009-07-16 13:35:48 +00001045
1046 // Propagate extension type if any
1047 if (F->paramHasAttr(0, Attribute::SExt))
1048 Flags.setSExt();
1049 else if (F->paramHasAttr(0, Attribute::ZExt))
1050 Flags.setZExt();
1051
Dan Gohman98ca4f22009-08-05 01:29:28 +00001052 for (unsigned i = 0; i < NumParts; ++i)
1053 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001054 }
1055 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001056
1057 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001058 CallingConv::ID CallConv =
1059 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001060 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
1061 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001062
1063 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001064 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001065 "LowerReturn didn't return a valid chain!");
1066
1067 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001068 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001069}
1070
Dan Gohmanad62f532009-04-23 23:13:24 +00001071/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1072/// created for it, emit nodes to copy the value into the virtual
1073/// registers.
1074void SelectionDAGLowering::CopyToExportRegsIfNeeded(Value *V) {
1075 if (!V->use_empty()) {
1076 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1077 if (VMI != FuncInfo.ValueMap.end())
1078 CopyValueToVirtualRegister(V, VMI->second);
1079 }
1080}
1081
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001082/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1083/// the current basic block, add it to ValueMap now so that we'll get a
1084/// CopyTo/FromReg.
1085void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
1086 // No need to export constants.
1087 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001088
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001089 // Already exported?
1090 if (FuncInfo.isExportedInst(V)) return;
1091
1092 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1093 CopyValueToVirtualRegister(V, Reg);
1094}
1095
1096bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
1097 const BasicBlock *FromBB) {
1098 // The operands of the setcc have to be in this block. We don't know
1099 // how to export them from some other block.
1100 if (Instruction *VI = dyn_cast<Instruction>(V)) {
1101 // Can export from current BB.
1102 if (VI->getParent() == FromBB)
1103 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001104
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001105 // Is already exported, noop.
1106 return FuncInfo.isExportedInst(V);
1107 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001109 // If this is an argument, we can export it if the BB is the entry block or
1110 // if it is already exported.
1111 if (isa<Argument>(V)) {
1112 if (FromBB == &FromBB->getParent()->getEntryBlock())
1113 return true;
1114
1115 // Otherwise, can only export this if it is already exported.
1116 return FuncInfo.isExportedInst(V);
1117 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001118
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001119 // Otherwise, constants can always be exported.
1120 return true;
1121}
1122
1123static bool InBlock(const Value *V, const BasicBlock *BB) {
1124 if (const Instruction *I = dyn_cast<Instruction>(V))
1125 return I->getParent() == BB;
1126 return true;
1127}
1128
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001129/// getFCmpCondCode - Return the ISD condition code corresponding to
1130/// the given LLVM IR floating-point condition code. This includes
1131/// consideration of global floating-point math flags.
1132///
1133static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1134 ISD::CondCode FPC, FOC;
1135 switch (Pred) {
1136 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1137 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1138 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1139 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1140 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1141 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1142 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1143 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1144 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1145 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1146 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1147 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1148 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1149 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1150 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1151 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1152 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001153 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001154 FOC = FPC = ISD::SETFALSE;
1155 break;
1156 }
1157 if (FiniteOnlyFPMath())
1158 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001159 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001160 return FPC;
1161}
1162
1163/// getICmpCondCode - Return the ISD condition code corresponding to
1164/// the given LLVM IR integer condition code.
1165///
1166static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1167 switch (Pred) {
1168 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1169 case ICmpInst::ICMP_NE: return ISD::SETNE;
1170 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1171 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1172 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1173 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1174 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1175 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1176 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1177 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1178 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001179 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001180 return ISD::SETNE;
1181 }
1182}
1183
Dan Gohmanc2277342008-10-17 21:16:08 +00001184/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1185/// This function emits a branch and is used at the leaves of an OR or an
1186/// AND operator tree.
1187///
1188void
1189SelectionDAGLowering::EmitBranchForMergedCondition(Value *Cond,
1190 MachineBasicBlock *TBB,
1191 MachineBasicBlock *FBB,
1192 MachineBasicBlock *CurBB) {
1193 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001194
Dan Gohmanc2277342008-10-17 21:16:08 +00001195 // If the leaf of the tree is a comparison, merge the condition into
1196 // the caseblock.
1197 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1198 // The operands of the cmp have to be in this block. We don't know
1199 // how to export them from some other block. If this is the first block
1200 // of the sequence, no exporting is needed.
1201 if (CurBB == CurMBB ||
1202 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1203 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001204 ISD::CondCode Condition;
1205 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001206 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001208 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001209 } else {
1210 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001211 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001213
1214 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001215 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1216 SwitchCases.push_back(CB);
1217 return;
1218 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001219 }
1220
1221 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001222 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001223 NULL, TBB, FBB, CurBB);
1224 SwitchCases.push_back(CB);
1225}
1226
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001227/// FindMergedConditions - If Cond is an expression like
Dan Gohmanc2277342008-10-17 21:16:08 +00001228void SelectionDAGLowering::FindMergedConditions(Value *Cond,
1229 MachineBasicBlock *TBB,
1230 MachineBasicBlock *FBB,
1231 MachineBasicBlock *CurBB,
1232 unsigned Opc) {
1233 // If this node is not part of the or/and tree, emit it as a branch.
1234 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001235 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001236 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1237 BOp->getParent() != CurBB->getBasicBlock() ||
1238 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1239 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1240 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001241 return;
1242 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001243
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001244 // Create TmpBB after CurBB.
1245 MachineFunction::iterator BBI = CurBB;
1246 MachineFunction &MF = DAG.getMachineFunction();
1247 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1248 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001249
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001250 if (Opc == Instruction::Or) {
1251 // Codegen X | Y as:
1252 // jmp_if_X TBB
1253 // jmp TmpBB
1254 // TmpBB:
1255 // jmp_if_Y TBB
1256 // jmp FBB
1257 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001259 // Emit the LHS condition.
1260 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001262 // Emit the RHS condition into TmpBB.
1263 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1264 } else {
1265 assert(Opc == Instruction::And && "Unknown merge op!");
1266 // Codegen X & Y as:
1267 // jmp_if_X TmpBB
1268 // jmp FBB
1269 // TmpBB:
1270 // jmp_if_Y TBB
1271 // jmp FBB
1272 //
1273 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001274
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275 // Emit the LHS condition.
1276 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 // Emit the RHS condition into TmpBB.
1279 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1280 }
1281}
1282
1283/// If the set of cases should be emitted as a series of branches, return true.
1284/// If we should emit this as a bunch of and/or'd together conditions, return
1285/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001286bool
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001287SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
1288 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001289
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001290 // If this is two comparisons of the same values or'd or and'd together, they
1291 // will get folded into a single comparison, so don't emit two blocks.
1292 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1293 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1294 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1295 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1296 return false;
1297 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001298
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001299 return true;
1300}
1301
1302void SelectionDAGLowering::visitBr(BranchInst &I) {
1303 // Update machine-CFG edges.
1304 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1305
1306 // Figure out which block is immediately after the current one.
1307 MachineBasicBlock *NextBlock = 0;
1308 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001309 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001310 NextBlock = BBI;
1311
1312 if (I.isUnconditional()) {
1313 // Update machine-CFG edges.
1314 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001315
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001316 // If this is not a fall-through branch, emit the branch.
1317 if (Succ0MBB != NextBlock)
Scott Michelfdc40a02009-02-17 22:15:04 +00001318 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320 DAG.getBasicBlock(Succ0MBB)));
1321 return;
1322 }
1323
1324 // If this condition is one of the special cases we handle, do special stuff
1325 // now.
1326 Value *CondVal = I.getCondition();
1327 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1328
1329 // If this is a series of conditions that are or'd or and'd together, emit
1330 // this as a sequence of branches instead of setcc's with and/or operations.
1331 // For example, instead of something like:
1332 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001333 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001335 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001336 // or C, F
1337 // jnz foo
1338 // Emit:
1339 // cmp A, B
1340 // je foo
1341 // cmp D, E
1342 // jle foo
1343 //
1344 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001345 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001346 (BOp->getOpcode() == Instruction::And ||
1347 BOp->getOpcode() == Instruction::Or)) {
1348 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1349 // If the compares in later blocks need to use values not currently
1350 // exported from this block, export them now. This block should always
1351 // be the first entry.
1352 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001353
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001354 // Allow some cases to be rejected.
1355 if (ShouldEmitAsBranches(SwitchCases)) {
1356 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1357 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1358 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1359 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001360
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001361 // Emit the branch for this block.
1362 visitSwitchCase(SwitchCases[0]);
1363 SwitchCases.erase(SwitchCases.begin());
1364 return;
1365 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001366
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001367 // Okay, we decided not to do this, remove any inserted MBB's and clear
1368 // SwitchCases.
1369 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001370 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001371
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001372 SwitchCases.clear();
1373 }
1374 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001375
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001376 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001377 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001378 NULL, Succ0MBB, Succ1MBB, CurMBB);
1379 // Use visitSwitchCase to actually insert the fast branch sequence for this
1380 // cond branch.
1381 visitSwitchCase(CB);
1382}
1383
1384/// visitSwitchCase - Emits the necessary code to represent a single node in
1385/// the binary search tree resulting from lowering a switch instruction.
1386void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) {
1387 SDValue Cond;
1388 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001389 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001390
1391 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001392 if (CB.CmpMHS == NULL) {
1393 // Fold "(X == true)" to X and "(X == false)" to !X to
1394 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001395 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001396 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001397 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001398 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001399 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001400 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001401 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001402 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001403 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001404 } else {
1405 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1406
Anton Korobeynikov23218582008-12-23 22:25:27 +00001407 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1408 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409
1410 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001411 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001412
1413 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001414 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001415 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001416 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001417 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001418 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001419 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001420 DAG.getConstant(High-Low, VT), ISD::SETULE);
1421 }
1422 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001423
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001424 // Update successor info
1425 CurMBB->addSuccessor(CB.TrueBB);
1426 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001427
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001428 // Set NextBlock to be the MBB immediately after the current one, if any.
1429 // This is used to avoid emitting unnecessary branches to the next block.
1430 MachineBasicBlock *NextBlock = 0;
1431 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001432 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001433 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001434
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001435 // If the lhs block is the next block, invert the condition so that we can
1436 // fall through to the lhs instead of the rhs block.
1437 if (CB.TrueBB == NextBlock) {
1438 std::swap(CB.TrueBB, CB.FalseBB);
1439 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001440 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001441 }
Dale Johannesenf5d97892009-02-04 01:48:28 +00001442 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001443 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001444 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001445
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001446 // If the branch was constant folded, fix up the CFG.
1447 if (BrCond.getOpcode() == ISD::BR) {
1448 CurMBB->removeSuccessor(CB.FalseBB);
1449 DAG.setRoot(BrCond);
1450 } else {
1451 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001452 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001453 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001454
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001455 if (CB.FalseBB == NextBlock)
1456 DAG.setRoot(BrCond);
1457 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001458 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001459 DAG.getBasicBlock(CB.FalseBB)));
1460 }
1461}
1462
1463/// visitJumpTable - Emit JumpTable node in the current MBB
1464void SelectionDAGLowering::visitJumpTable(JumpTable &JT) {
1465 // Emit the code for the jump table
1466 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001467 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001468 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1469 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Scott Michelfdc40a02009-02-17 22:15:04 +00001471 DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001472 MVT::Other, Index.getValue(1),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 Table, Index));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001474}
1475
1476/// visitJumpTableHeader - This function emits necessary code to produce index
1477/// in the JumpTable from switch case.
1478void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT,
1479 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001480 // Subtract the lowest switch case value from the value being switched on and
1481 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001482 // difference between smallest and largest cases.
1483 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001484 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001485 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001486 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001487
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001488 // The SDNode we just created, which holds the value being switched on minus
1489 // the the smallest case value, needs to be copied to a virtual register so it
1490 // can be used as an index into the jump table in a subsequent basic block.
1491 // This value may be smaller or larger than the target's pointer type, and
1492 // therefore require extension or truncating.
Duncan Sands3a66a682009-10-13 21:04:12 +00001493 SwitchOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001494
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001495 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001496 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1497 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001498 JT.Reg = JumpTableReg;
1499
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001500 // Emit the range check for the jump table, and branch to the default block
1501 // for the switch statement if the value being switched on exceeds the largest
1502 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001503 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
1504 TLI.getSetCCResultType(SUB.getValueType()), SUB,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001505 DAG.getConstant(JTH.Last-JTH.First,VT),
1506 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001507
1508 // Set NextBlock to be the MBB immediately after the current one, if any.
1509 // This is used to avoid emitting unnecessary branches to the next block.
1510 MachineBasicBlock *NextBlock = 0;
1511 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001512 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001513 NextBlock = BBI;
1514
Dale Johannesen66978ee2009-01-31 02:22:37 +00001515 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001516 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001517 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001518
1519 if (JT.MBB == NextBlock)
1520 DAG.setRoot(BrCond);
1521 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001522 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001523 DAG.getBasicBlock(JT.MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001524}
1525
1526/// visitBitTestHeader - This function emits necessary code to produce value
1527/// suitable for "bit tests"
1528void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) {
1529 // Subtract the minimum value
1530 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001531 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001532 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001533 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001534
1535 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001536 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
1537 TLI.getSetCCResultType(SUB.getValueType()),
1538 SUB, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001539 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001540
Duncan Sands3a66a682009-10-13 21:04:12 +00001541 SDValue ShiftOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542
Duncan Sands92abc622009-01-31 15:50:11 +00001543 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001544 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1545 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546
1547 // Set NextBlock to be the MBB immediately after the current one, if any.
1548 // This is used to avoid emitting unnecessary branches to the next block.
1549 MachineBasicBlock *NextBlock = 0;
1550 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001551 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552 NextBlock = BBI;
1553
1554 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1555
1556 CurMBB->addSuccessor(B.Default);
1557 CurMBB->addSuccessor(MBB);
1558
Dale Johannesen66978ee2009-01-31 02:22:37 +00001559 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001560 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001561 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001562
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001563 if (MBB == NextBlock)
1564 DAG.setRoot(BrRange);
1565 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001566 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567 DAG.getBasicBlock(MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001568}
1569
1570/// visitBitTestCase - this function produces one "bit test"
1571void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB,
1572 unsigned Reg,
1573 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001574 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001575 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001576 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001577 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001578 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001579 DAG.getConstant(1, TLI.getPointerTy()),
1580 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001581
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001582 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001583 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001584 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001585 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001586 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1587 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001588 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001589 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001590
1591 CurMBB->addSuccessor(B.TargetBB);
1592 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001593
Dale Johannesen66978ee2009-01-31 02:22:37 +00001594 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001595 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001596 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001597
1598 // Set NextBlock to be the MBB immediately after the current one, if any.
1599 // This is used to avoid emitting unnecessary branches to the next block.
1600 MachineBasicBlock *NextBlock = 0;
1601 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001602 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001603 NextBlock = BBI;
1604
1605 if (NextMBB == NextBlock)
1606 DAG.setRoot(BrAnd);
1607 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001608 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609 DAG.getBasicBlock(NextMBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610}
1611
1612void SelectionDAGLowering::visitInvoke(InvokeInst &I) {
1613 // Retrieve successors.
1614 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1615 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1616
Gabor Greifb67e6b32009-01-15 11:10:44 +00001617 const Value *Callee(I.getCalledValue());
1618 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 visitInlineAsm(&I);
1620 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001621 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622
1623 // If the value of the invoke is used outside of its defining block, make it
1624 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001625 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001626
1627 // Update successor info
1628 CurMBB->addSuccessor(Return);
1629 CurMBB->addSuccessor(LandingPad);
1630
1631 // Drop into normal successor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001632 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001633 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001634 DAG.getBasicBlock(Return)));
1635}
1636
1637void SelectionDAGLowering::visitUnwind(UnwindInst &I) {
1638}
1639
1640/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1641/// small case ranges).
1642bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR,
1643 CaseRecVector& WorkList,
1644 Value* SV,
1645 MachineBasicBlock* Default) {
1646 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001647
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001648 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001649 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001650 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001651 return false;
1652
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653 // Get the MachineFunction which holds the current MBB. This is used when
1654 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001655 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656
1657 // Figure out which block is immediately after the current one.
1658 MachineBasicBlock *NextBlock = 0;
1659 MachineFunction::iterator BBI = CR.CaseBB;
1660
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001661 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001662 NextBlock = BBI;
1663
1664 // TODO: If any two of the cases has the same destination, and if one value
1665 // is the same as the other, but has one bit unset that the other has set,
1666 // use bit manipulation to do two compares at once. For example:
1667 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001668
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001669 // Rearrange the case blocks so that the last one falls through if possible.
1670 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1671 // The last case block won't fall through into 'NextBlock' if we emit the
1672 // branches in this order. See if rearranging a case value would help.
1673 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1674 if (I->BB == NextBlock) {
1675 std::swap(*I, BackCase);
1676 break;
1677 }
1678 }
1679 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681 // Create a CaseBlock record representing a conditional branch to
1682 // the Case's target mbb if the value being switched on SV is equal
1683 // to C.
1684 MachineBasicBlock *CurBlock = CR.CaseBB;
1685 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1686 MachineBasicBlock *FallThrough;
1687 if (I != E-1) {
1688 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1689 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001690
1691 // Put SV in a virtual register to make it available from the new blocks.
1692 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001693 } else {
1694 // If the last case doesn't match, go to the default block.
1695 FallThrough = Default;
1696 }
1697
1698 Value *RHS, *LHS, *MHS;
1699 ISD::CondCode CC;
1700 if (I->High == I->Low) {
1701 // This is just small small case range :) containing exactly 1 case
1702 CC = ISD::SETEQ;
1703 LHS = SV; RHS = I->High; MHS = NULL;
1704 } else {
1705 CC = ISD::SETLE;
1706 LHS = I->Low; MHS = SV; RHS = I->High;
1707 }
1708 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001709
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001710 // If emitting the first comparison, just call visitSwitchCase to emit the
1711 // code into the current block. Otherwise, push the CaseBlock onto the
1712 // vector to be later processed by SDISel, and insert the node's MBB
1713 // before the next MBB.
1714 if (CurBlock == CurMBB)
1715 visitSwitchCase(CB);
1716 else
1717 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001718
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719 CurBlock = FallThrough;
1720 }
1721
1722 return true;
1723}
1724
1725static inline bool areJTsAllowed(const TargetLowering &TLI) {
1726 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001727 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1728 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001729}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001730
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001731static APInt ComputeRange(const APInt &First, const APInt &Last) {
1732 APInt LastExt(Last), FirstExt(First);
1733 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1734 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1735 return (LastExt - FirstExt + 1ULL);
1736}
1737
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001738/// handleJTSwitchCase - Emit jumptable for current switch case range
1739bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR,
1740 CaseRecVector& WorkList,
1741 Value* SV,
1742 MachineBasicBlock* Default) {
1743 Case& FrontCase = *CR.Range.first;
1744 Case& BackCase = *(CR.Range.second-1);
1745
Anton Korobeynikov23218582008-12-23 22:25:27 +00001746 const APInt& First = cast<ConstantInt>(FrontCase.Low)->getValue();
1747 const APInt& Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748
Anton Korobeynikov23218582008-12-23 22:25:27 +00001749 size_t TSize = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001750 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1751 I!=E; ++I)
1752 TSize += I->size();
1753
1754 if (!areJTsAllowed(TLI) || TSize <= 3)
1755 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001756
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001757 APInt Range = ComputeRange(First, Last);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001758 double Density = (double)TSize / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001759 if (Density < 0.4)
1760 return false;
1761
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001762 DEBUG(errs() << "Lowering jump table\n"
1763 << "First entry: " << First << ". Last entry: " << Last << '\n'
1764 << "Range: " << Range
1765 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001766
1767 // Get the MachineFunction which holds the current MBB. This is used when
1768 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001769 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001770
1771 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001772 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001773 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001774
1775 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1776
1777 // Create a new basic block to hold the code for loading the address
1778 // of the jump table, and jumping to it. Update successor information;
1779 // we will either branch to the default case for the switch, or the jump
1780 // table.
1781 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1782 CurMF->insert(BBI, JumpTableBB);
1783 CR.CaseBB->addSuccessor(Default);
1784 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001785
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001786 // Build a vector of destination BBs, corresponding to each target
1787 // of the jump table. If the value of the jump table slot corresponds to
1788 // a case statement, push the case's BB onto the vector, otherwise, push
1789 // the default BB.
1790 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001791 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001792 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001793 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1794 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1795
1796 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001797 DestBBs.push_back(I->BB);
1798 if (TEI==High)
1799 ++I;
1800 } else {
1801 DestBBs.push_back(Default);
1802 }
1803 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001804
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001805 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001806 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1807 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 E = DestBBs.end(); I != E; ++I) {
1809 if (!SuccsHandled[(*I)->getNumber()]) {
1810 SuccsHandled[(*I)->getNumber()] = true;
1811 JumpTableBB->addSuccessor(*I);
1812 }
1813 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001814
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001815 // Create a jump table index for this jump table, or return an existing
1816 // one.
1817 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001818
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001819 // Set the jump table information so that we can codegen it as a second
1820 // MachineBasicBlock
1821 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1822 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1823 if (CR.CaseBB == CurMBB)
1824 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001825
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 JTCases.push_back(JumpTableBlock(JTH, JT));
1827
1828 return true;
1829}
1830
1831/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1832/// 2 subtrees.
1833bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR,
1834 CaseRecVector& WorkList,
1835 Value* SV,
1836 MachineBasicBlock* Default) {
1837 // Get the MachineFunction which holds the current MBB. This is used when
1838 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001839 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001840
1841 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001842 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001843 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001844
1845 Case& FrontCase = *CR.Range.first;
1846 Case& BackCase = *(CR.Range.second-1);
1847 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1848
1849 // Size is the number of Cases represented by this range.
1850 unsigned Size = CR.Range.second - CR.Range.first;
1851
Anton Korobeynikov23218582008-12-23 22:25:27 +00001852 const APInt& First = cast<ConstantInt>(FrontCase.Low)->getValue();
1853 const APInt& Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001854 double FMetric = 0;
1855 CaseItr Pivot = CR.Range.first + Size/2;
1856
1857 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1858 // (heuristically) allow us to emit JumpTable's later.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001859 size_t TSize = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001860 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1861 I!=E; ++I)
1862 TSize += I->size();
1863
Anton Korobeynikov23218582008-12-23 22:25:27 +00001864 size_t LSize = FrontCase.size();
1865 size_t RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001866 DEBUG(errs() << "Selecting best pivot: \n"
1867 << "First: " << First << ", Last: " << Last <<'\n'
1868 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001869 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1870 J!=E; ++I, ++J) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001871 const APInt& LEnd = cast<ConstantInt>(I->High)->getValue();
1872 const APInt& RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001873 APInt Range = ComputeRange(LEnd, RBegin);
1874 assert((Range - 2ULL).isNonNegative() &&
1875 "Invalid case distance");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001876 double LDensity = (double)LSize / (LEnd - First + 1ULL).roundToDouble();
1877 double RDensity = (double)RSize / (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001878 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001879 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001880 DEBUG(errs() <<"=>Step\n"
1881 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1882 << "LDensity: " << LDensity
1883 << ", RDensity: " << RDensity << '\n'
1884 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001885 if (FMetric < Metric) {
1886 Pivot = J;
1887 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001888 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001889 }
1890
1891 LSize += J->size();
1892 RSize -= J->size();
1893 }
1894 if (areJTsAllowed(TLI)) {
1895 // If our case is dense we *really* should handle it earlier!
1896 assert((FMetric > 0) && "Should handle dense range earlier!");
1897 } else {
1898 Pivot = CR.Range.first + Size/2;
1899 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001900
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001901 CaseRange LHSR(CR.Range.first, Pivot);
1902 CaseRange RHSR(Pivot, CR.Range.second);
1903 Constant *C = Pivot->Low;
1904 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001905
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001906 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001907 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001908 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001909 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001910 // Pivot's Value, then we can branch directly to the LHS's Target,
1911 // rather than creating a leaf node for it.
1912 if ((LHSR.second - LHSR.first) == 1 &&
1913 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001914 cast<ConstantInt>(C)->getValue() ==
1915 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001916 TrueBB = LHSR.first->BB;
1917 } else {
1918 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1919 CurMF->insert(BBI, TrueBB);
1920 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001921
1922 // Put SV in a virtual register to make it available from the new blocks.
1923 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001924 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001925
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001926 // Similar to the optimization above, if the Value being switched on is
1927 // known to be less than the Constant CR.LT, and the current Case Value
1928 // is CR.LT - 1, then we can branch directly to the target block for
1929 // the current Case Value, rather than emitting a RHS leaf node for it.
1930 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001931 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1932 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001933 FalseBB = RHSR.first->BB;
1934 } else {
1935 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1936 CurMF->insert(BBI, FalseBB);
1937 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001938
1939 // Put SV in a virtual register to make it available from the new blocks.
1940 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941 }
1942
1943 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001944 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945 // Otherwise, branch to LHS.
1946 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1947
1948 if (CR.CaseBB == CurMBB)
1949 visitSwitchCase(CB);
1950 else
1951 SwitchCases.push_back(CB);
1952
1953 return true;
1954}
1955
1956/// handleBitTestsSwitchCase - if current case range has few destination and
1957/// range span less, than machine word bitwidth, encode case range into series
1958/// of masks and emit bit tests with these masks.
1959bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1960 CaseRecVector& WorkList,
1961 Value* SV,
1962 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001963 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001964 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001965
1966 Case& FrontCase = *CR.Range.first;
1967 Case& BackCase = *(CR.Range.second-1);
1968
1969 // Get the MachineFunction which holds the current MBB. This is used when
1970 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001971 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001972
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001973 // If target does not have legal shift left, do not emit bit tests at all.
1974 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1975 return false;
1976
Anton Korobeynikov23218582008-12-23 22:25:27 +00001977 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001978 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1979 I!=E; ++I) {
1980 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001981 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001982 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001983
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001984 // Count unique destinations
1985 SmallSet<MachineBasicBlock*, 4> Dests;
1986 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1987 Dests.insert(I->BB);
1988 if (Dests.size() > 3)
1989 // Don't bother the code below, if there are too much unique destinations
1990 return false;
1991 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001992 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1993 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001994
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001995 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001996 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1997 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001998 APInt cmpRange = maxValue - minValue;
1999
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002000 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
2001 << "Low bound: " << minValue << '\n'
2002 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002003
2004 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002005 (!(Dests.size() == 1 && numCmps >= 3) &&
2006 !(Dests.size() == 2 && numCmps >= 5) &&
2007 !(Dests.size() >= 3 && numCmps >= 6)))
2008 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002009
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002010 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002011 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2012
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002013 // Optimize the case where all the case values fit in a
2014 // word without having to subtract minValue. In this case,
2015 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002016 if (minValue.isNonNegative() &&
2017 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
2018 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002019 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002020 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002022
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002023 CaseBitsVector CasesBits;
2024 unsigned i, count = 0;
2025
2026 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2027 MachineBasicBlock* Dest = I->BB;
2028 for (i = 0; i < count; ++i)
2029 if (Dest == CasesBits[i].BB)
2030 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002031
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002032 if (i == count) {
2033 assert((count < 3) && "Too much destinations to test!");
2034 CasesBits.push_back(CaseBits(0, Dest, 0));
2035 count++;
2036 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002037
2038 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2039 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2040
2041 uint64_t lo = (lowValue - lowBound).getZExtValue();
2042 uint64_t hi = (highValue - lowBound).getZExtValue();
2043
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002044 for (uint64_t j = lo; j <= hi; j++) {
2045 CasesBits[i].Mask |= 1ULL << j;
2046 CasesBits[i].Bits++;
2047 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002048
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002049 }
2050 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002052 BitTestInfo BTC;
2053
2054 // Figure out which block is immediately after the current one.
2055 MachineFunction::iterator BBI = CR.CaseBB;
2056 ++BBI;
2057
2058 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2059
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002060 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002061 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002062 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
2063 << ", Bits: " << CasesBits[i].Bits
2064 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002065
2066 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2067 CurMF->insert(BBI, CaseBB);
2068 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2069 CaseBB,
2070 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002071
2072 // Put SV in a virtual register to make it available from the new blocks.
2073 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002074 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002075
2076 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002077 -1U, (CR.CaseBB == CurMBB),
2078 CR.CaseBB, Default, BTC);
2079
2080 if (CR.CaseBB == CurMBB)
2081 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002083 BitTestCases.push_back(BTB);
2084
2085 return true;
2086}
2087
2088
2089/// Clusterify - Transform simple list of Cases into list of CaseRange's
Anton Korobeynikov23218582008-12-23 22:25:27 +00002090size_t SelectionDAGLowering::Clusterify(CaseVector& Cases,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002091 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002092 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093
2094 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002095 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002096 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2097 Cases.push_back(Case(SI.getSuccessorValue(i),
2098 SI.getSuccessorValue(i),
2099 SMBB));
2100 }
2101 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2102
2103 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002104 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002105 // Must recompute end() each iteration because it may be
2106 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002107 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2108 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2109 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002110 MachineBasicBlock* nextBB = J->BB;
2111 MachineBasicBlock* currentBB = I->BB;
2112
2113 // If the two neighboring cases go to the same destination, merge them
2114 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002115 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002116 I->High = J->High;
2117 J = Cases.erase(J);
2118 } else {
2119 I = J++;
2120 }
2121 }
2122
2123 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2124 if (I->Low != I->High)
2125 // A range counts double, since it requires two compares.
2126 ++numCmps;
2127 }
2128
2129 return numCmps;
2130}
2131
Anton Korobeynikov23218582008-12-23 22:25:27 +00002132void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133 // Figure out which block is immediately after the current one.
2134 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002135
2136 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2137
2138 // If there is only the default destination, branch to it if it is not the
2139 // next basic block. Otherwise, just fall through.
2140 if (SI.getNumOperands() == 2) {
2141 // Update machine-CFG edges.
2142
2143 // If this is not a fall-through branch, emit the branch.
2144 CurMBB->addSuccessor(Default);
2145 if (Default != NextBlock)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002146 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002147 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002148 DAG.getBasicBlock(Default)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002149 return;
2150 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002151
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002152 // If there are any non-default case statements, create a vector of Cases
2153 // representing each one, and sort the vector so that we can efficiently
2154 // create a binary search tree from them.
2155 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002156 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002157 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2158 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002159 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002160
2161 // Get the Value to be switched on and default basic blocks, which will be
2162 // inserted into CaseBlock records, representing basic blocks in the binary
2163 // search tree.
2164 Value *SV = SI.getOperand(0);
2165
2166 // Push the initial CaseRec onto the worklist
2167 CaseRecVector WorkList;
2168 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2169
2170 while (!WorkList.empty()) {
2171 // Grab a record representing a case range to process off the worklist
2172 CaseRec CR = WorkList.back();
2173 WorkList.pop_back();
2174
2175 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2176 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002178 // If the range has few cases (two or less) emit a series of specific
2179 // tests.
2180 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2181 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002182
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002183 // If the switch has more than 5 blocks, and at least 40% dense, and the
2184 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002185 // lowering the switch to a binary tree of conditional branches.
2186 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2187 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002189 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2190 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2191 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2192 }
2193}
2194
Chris Lattnerab21db72009-10-28 00:19:10 +00002195void SelectionDAGLowering::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002196 // Update machine-CFG edges.
2197 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2198 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2199
Dan Gohman64825152009-10-27 21:56:26 +00002200 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2201 MVT::Other, getControlRoot(),
2202 getValue(I.getAddress())));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002203}
2204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002206void SelectionDAGLowering::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207 // -0.0 - X --> fneg
2208 const Type *Ty = I.getType();
2209 if (isa<VectorType>(Ty)) {
2210 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2211 const VectorType *DestTy = cast<VectorType>(I.getType());
2212 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002213 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002214 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002215 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002216 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002217 SDValue Op2 = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002218 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002219 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002220 return;
2221 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002222 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002224 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002225 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002226 SDValue Op2 = getValue(I.getOperand(1));
2227 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2228 Op2.getValueType(), Op2));
2229 return;
2230 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002231
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002232 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002233}
2234
2235void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
2236 SDValue Op1 = getValue(I.getOperand(0));
2237 SDValue Op2 = getValue(I.getOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002238
Scott Michelfdc40a02009-02-17 22:15:04 +00002239 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002240 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241}
2242
2243void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2244 SDValue Op1 = getValue(I.getOperand(0));
2245 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002246 if (!isa<VectorType>(I.getType()) &&
2247 Op2.getValueType() != TLI.getShiftAmountTy()) {
2248 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002249 EVT PTy = TLI.getPointerTy();
2250 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002251 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002252 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2253 TLI.getShiftAmountTy(), Op2);
2254 // If the operand is larger than the shift count type but the shift
2255 // count type has enough bits to represent any shift value, truncate
2256 // it now. This is a common case and it exposes the truncate to
2257 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002258 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002259 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2260 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2261 TLI.getShiftAmountTy(), Op2);
2262 // Otherwise we'll need to temporarily settle for some other
2263 // convenient type; type legalization will make adjustments as
2264 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002265 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002266 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002267 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002268 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002269 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002270 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002271 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002272
Scott Michelfdc40a02009-02-17 22:15:04 +00002273 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002274 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002275}
2276
2277void SelectionDAGLowering::visitICmp(User &I) {
2278 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2279 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2280 predicate = IC->getPredicate();
2281 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2282 predicate = ICmpInst::Predicate(IC->getPredicate());
2283 SDValue Op1 = getValue(I.getOperand(0));
2284 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002285 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002286
Owen Andersone50ed302009-08-10 22:56:29 +00002287 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002288 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002289}
2290
2291void SelectionDAGLowering::visitFCmp(User &I) {
2292 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2293 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2294 predicate = FC->getPredicate();
2295 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2296 predicate = FCmpInst::Predicate(FC->getPredicate());
2297 SDValue Op1 = getValue(I.getOperand(0));
2298 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002299 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002300 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002301 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002302}
2303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002304void SelectionDAGLowering::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002305 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002306 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2307 unsigned NumValues = ValueVTs.size();
2308 if (NumValues != 0) {
2309 SmallVector<SDValue, 4> Values(NumValues);
2310 SDValue Cond = getValue(I.getOperand(0));
2311 SDValue TrueVal = getValue(I.getOperand(1));
2312 SDValue FalseVal = getValue(I.getOperand(2));
2313
2314 for (unsigned i = 0; i != NumValues; ++i)
Scott Michelfdc40a02009-02-17 22:15:04 +00002315 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002316 TrueVal.getValueType(), Cond,
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002317 SDValue(TrueVal.getNode(), TrueVal.getResNo() + i),
2318 SDValue(FalseVal.getNode(), FalseVal.getResNo() + i));
2319
Scott Michelfdc40a02009-02-17 22:15:04 +00002320 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002321 DAG.getVTList(&ValueVTs[0], NumValues),
2322 &Values[0], NumValues));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002323 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002324}
2325
2326
2327void SelectionDAGLowering::visitTrunc(User &I) {
2328 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2329 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002330 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002331 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002332}
2333
2334void SelectionDAGLowering::visitZExt(User &I) {
2335 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2336 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2337 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002338 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002339 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002340}
2341
2342void SelectionDAGLowering::visitSExt(User &I) {
2343 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2344 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2345 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002346 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002347 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002348}
2349
2350void SelectionDAGLowering::visitFPTrunc(User &I) {
2351 // FPTrunc is never a no-op cast, no need to check
2352 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002353 EVT DestVT = TLI.getValueType(I.getType());
Scott Michelfdc40a02009-02-17 22:15:04 +00002354 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002355 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002356}
2357
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002358void SelectionDAGLowering::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002359 // FPTrunc is never a no-op cast, no need to check
2360 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002361 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002362 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002363}
2364
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002365void SelectionDAGLowering::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002366 // FPToUI is never a no-op cast, no need to check
2367 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002368 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002369 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002370}
2371
2372void SelectionDAGLowering::visitFPToSI(User &I) {
2373 // FPToSI is never a no-op cast, no need to check
2374 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002375 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002376 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002377}
2378
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002379void SelectionDAGLowering::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002380 // UIToFP is never a no-op cast, no need to check
2381 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002382 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002383 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002384}
2385
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002386void SelectionDAGLowering::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002387 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002388 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002389 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002390 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002391}
2392
2393void SelectionDAGLowering::visitPtrToInt(User &I) {
2394 // What to do depends on the size of the integer and the size of the pointer.
2395 // We can either truncate, zero extend, or no-op, accordingly.
2396 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002397 EVT SrcVT = N.getValueType();
2398 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002399 SDValue Result = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002400 setValue(&I, Result);
2401}
2402
2403void SelectionDAGLowering::visitIntToPtr(User &I) {
2404 // What to do depends on the size of the integer and the size of the pointer.
2405 // We can either truncate, zero extend, or no-op, accordingly.
2406 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002407 EVT SrcVT = N.getValueType();
2408 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002409 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002410}
2411
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002412void SelectionDAGLowering::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002413 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002414 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002415
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002416 // BitCast assures us that source and destination are the same size so this
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002417 // is either a BIT_CONVERT or a no-op.
2418 if (DestVT != N.getValueType())
Scott Michelfdc40a02009-02-17 22:15:04 +00002419 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002420 DestVT, N)); // convert types
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002421 else
2422 setValue(&I, N); // noop cast.
2423}
2424
2425void SelectionDAGLowering::visitInsertElement(User &I) {
2426 SDValue InVec = getValue(I.getOperand(0));
2427 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002428 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002429 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002430 getValue(I.getOperand(2)));
2431
Scott Michelfdc40a02009-02-17 22:15:04 +00002432 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002433 TLI.getValueType(I.getType()),
2434 InVec, InVal, InIdx));
2435}
2436
2437void SelectionDAGLowering::visitExtractElement(User &I) {
2438 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002439 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002440 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002441 getValue(I.getOperand(1)));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002442 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002443 TLI.getValueType(I.getType()), InVec, InIdx));
2444}
2445
Mon P Wangaeb06d22008-11-10 04:46:22 +00002446
2447// Utility for visitShuffleVector - Returns true if the mask is mask starting
2448// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002449static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2450 unsigned MaskNumElts = Mask.size();
2451 for (unsigned i = 0; i != MaskNumElts; ++i)
2452 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002453 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002454 return true;
2455}
2456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002457void SelectionDAGLowering::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002458 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002459 SDValue Src1 = getValue(I.getOperand(0));
2460 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002461
Nate Begeman9008ca62009-04-27 18:41:29 +00002462 // Convert the ConstantVector mask operand into an array of ints, with -1
2463 // representing undef values.
2464 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002465 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2466 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002467 unsigned MaskNumElts = MaskElts.size();
2468 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002469 if (isa<UndefValue>(MaskElts[i]))
2470 Mask.push_back(-1);
2471 else
2472 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2473 }
2474
Owen Andersone50ed302009-08-10 22:56:29 +00002475 EVT VT = TLI.getValueType(I.getType());
2476 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002477 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002478
Mon P Wangc7849c22008-11-16 05:06:27 +00002479 if (SrcNumElts == MaskNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002480 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2481 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002482 return;
2483 }
2484
2485 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002486 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2487 // Mask is longer than the source vectors and is a multiple of the source
2488 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002489 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002490 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2491 // The shuffle is concatenating two vectors together.
Scott Michelfdc40a02009-02-17 22:15:04 +00002492 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002493 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002494 return;
2495 }
2496
Mon P Wangc7849c22008-11-16 05:06:27 +00002497 // Pad both vectors with undefs to make them the same length as the mask.
2498 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002499 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2500 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002501 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002502
Nate Begeman9008ca62009-04-27 18:41:29 +00002503 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2504 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002505 MOps1[0] = Src1;
2506 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002507
2508 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2509 getCurDebugLoc(), VT,
2510 &MOps1[0], NumConcat);
2511 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2512 getCurDebugLoc(), VT,
2513 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002514
Mon P Wangaeb06d22008-11-10 04:46:22 +00002515 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002516 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002517 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002518 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002519 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002520 MappedOps.push_back(Idx);
2521 else
2522 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002523 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002524 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2525 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002526 return;
2527 }
2528
Mon P Wangc7849c22008-11-16 05:06:27 +00002529 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002530 // Analyze the access pattern of the vector to see if we can extract
2531 // two subvectors and do the shuffle. The analysis is done by calculating
2532 // the range of elements the mask access on both vectors.
2533 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2534 int MaxRange[2] = {-1, -1};
2535
Nate Begeman5a5ca152009-04-29 05:20:52 +00002536 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002537 int Idx = Mask[i];
2538 int Input = 0;
2539 if (Idx < 0)
2540 continue;
2541
Nate Begeman5a5ca152009-04-29 05:20:52 +00002542 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002543 Input = 1;
2544 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002545 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002546 if (Idx > MaxRange[Input])
2547 MaxRange[Input] = Idx;
2548 if (Idx < MinRange[Input])
2549 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002550 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002551
Mon P Wangc7849c22008-11-16 05:06:27 +00002552 // Check if the access is smaller than the vector size and can we find
2553 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002554 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002555 int StartIdx[2]; // StartIdx to extract from
2556 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002557 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002558 RangeUse[Input] = 0; // Unused
2559 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002560 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002561 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002562 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002563 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002564 RangeUse[Input] = 1; // Extract from beginning of the vector
2565 StartIdx[Input] = 0;
2566 } else {
2567 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002568 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002569 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002570 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002571 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002572 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002573 }
2574
Bill Wendling636e2582009-08-21 18:16:06 +00002575 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002576 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002577 return;
2578 }
2579 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2580 // Extract appropriate subvector and generate a vector shuffle
2581 for (int Input=0; Input < 2; ++Input) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002582 SDValue& Src = Input == 0 ? Src1 : Src2;
Mon P Wangc7849c22008-11-16 05:06:27 +00002583 if (RangeUse[Input] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002584 Src = DAG.getUNDEF(VT);
Mon P Wangc7849c22008-11-16 05:06:27 +00002585 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002586 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002587 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002588 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002589 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002590 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002591 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002592 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002593 int Idx = Mask[i];
2594 if (Idx < 0)
2595 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002596 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002597 MappedOps.push_back(Idx - StartIdx[0]);
2598 else
2599 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002600 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002601 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2602 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002603 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002604 }
2605 }
2606
Mon P Wangc7849c22008-11-16 05:06:27 +00002607 // We can't use either concat vectors or extract subvectors so fall back to
2608 // replacing the shuffle with extract and build vector.
2609 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002610 EVT EltVT = VT.getVectorElementType();
2611 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002612 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002613 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002614 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002615 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002616 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002617 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002618 if (Idx < (int)SrcNumElts)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002619 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002620 EltVT, Src1, DAG.getConstant(Idx, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002621 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002622 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Scott Michelfdc40a02009-02-17 22:15:04 +00002623 EltVT, Src2,
Mon P Wangc7849c22008-11-16 05:06:27 +00002624 DAG.getConstant(Idx - SrcNumElts, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002625 }
2626 }
Evan Chenga87008d2009-02-25 22:49:59 +00002627 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2628 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002629}
2630
2631void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) {
2632 const Value *Op0 = I.getOperand(0);
2633 const Value *Op1 = I.getOperand(1);
2634 const Type *AggTy = I.getType();
2635 const Type *ValTy = Op1->getType();
2636 bool IntoUndef = isa<UndefValue>(Op0);
2637 bool FromUndef = isa<UndefValue>(Op1);
2638
2639 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2640 I.idx_begin(), I.idx_end());
2641
Owen Andersone50ed302009-08-10 22:56:29 +00002642 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002643 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002644 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002645 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2646
2647 unsigned NumAggValues = AggValueVTs.size();
2648 unsigned NumValValues = ValValueVTs.size();
2649 SmallVector<SDValue, 4> Values(NumAggValues);
2650
2651 SDValue Agg = getValue(Op0);
2652 SDValue Val = getValue(Op1);
2653 unsigned i = 0;
2654 // Copy the beginning value(s) from the original aggregate.
2655 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002656 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002657 SDValue(Agg.getNode(), Agg.getResNo() + i);
2658 // Copy values from the inserted value(s).
2659 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002660 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002661 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2662 // Copy remaining value(s) from the original aggregate.
2663 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002664 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002665 SDValue(Agg.getNode(), Agg.getResNo() + i);
2666
Scott Michelfdc40a02009-02-17 22:15:04 +00002667 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002668 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2669 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002670}
2671
2672void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) {
2673 const Value *Op0 = I.getOperand(0);
2674 const Type *AggTy = Op0->getType();
2675 const Type *ValTy = I.getType();
2676 bool OutOfUndef = isa<UndefValue>(Op0);
2677
2678 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2679 I.idx_begin(), I.idx_end());
2680
Owen Andersone50ed302009-08-10 22:56:29 +00002681 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002682 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2683
2684 unsigned NumValValues = ValValueVTs.size();
2685 SmallVector<SDValue, 4> Values(NumValValues);
2686
2687 SDValue Agg = getValue(Op0);
2688 // Copy out the selected value(s).
2689 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2690 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002691 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002692 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002693 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002694
Scott Michelfdc40a02009-02-17 22:15:04 +00002695 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002696 DAG.getVTList(&ValValueVTs[0], NumValValues),
2697 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002698}
2699
2700
2701void SelectionDAGLowering::visitGetElementPtr(User &I) {
2702 SDValue N = getValue(I.getOperand(0));
2703 const Type *Ty = I.getOperand(0)->getType();
2704
2705 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2706 OI != E; ++OI) {
2707 Value *Idx = *OI;
2708 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2709 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2710 if (Field) {
2711 // N = N + Offset
2712 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002713 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002714 DAG.getIntPtrConstant(Offset));
2715 }
2716 Ty = StTy->getElementType(Field);
2717 } else {
2718 Ty = cast<SequentialType>(Ty)->getElementType();
2719
2720 // If this is a constant subscript, handle it quickly.
2721 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2722 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002723 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002724 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002725 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002726 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002727 unsigned PtrBits = PTy.getSizeInBits();
Evan Cheng65b52df2009-02-09 21:01:06 +00002728 if (PtrBits < 64) {
2729 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2730 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002731 DAG.getConstant(Offs, MVT::i64));
Evan Cheng65b52df2009-02-09 21:01:06 +00002732 } else
Evan Chengb1032a82009-02-09 20:54:38 +00002733 OffsVal = DAG.getIntPtrConstant(Offs);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002734 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002735 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002736 continue;
2737 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002738
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002739 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002740 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2741 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002742 SDValue IdxN = getValue(Idx);
2743
2744 // If the index is smaller or larger than intptr_t, truncate or extend
2745 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002746 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002747
2748 // If this is a multiply by a power of two, turn it into a shl
2749 // immediately. This is a very common case.
2750 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002751 if (ElementSize.isPowerOf2()) {
2752 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002753 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002754 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002755 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002756 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002757 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002758 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002759 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002760 }
2761 }
2762
Scott Michelfdc40a02009-02-17 22:15:04 +00002763 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002764 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002765 }
2766 }
2767 setValue(&I, N);
2768}
2769
2770void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2771 // If this is a fixed sized alloca in the entry block of the function,
2772 // allocate it statically on the stack.
2773 if (FuncInfo.StaticAllocaMap.count(&I))
2774 return; // getValue will auto-populate this.
2775
2776 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002777 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002778 unsigned Align =
2779 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2780 I.getAlignment());
2781
2782 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002783
2784 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2785 AllocSize,
2786 DAG.getConstant(TySize, AllocSize.getValueType()));
2787
2788
2789
Owen Andersone50ed302009-08-10 22:56:29 +00002790 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002791 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002793 // Handle alignment. If the requested alignment is less than or equal to
2794 // the stack alignment, ignore it. If the size is greater than or equal to
2795 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2796 unsigned StackAlign =
2797 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2798 if (Align <= StackAlign)
2799 Align = 0;
2800
2801 // Round the size of the allocation up to the stack alignment size
2802 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002803 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002804 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002805 DAG.getIntPtrConstant(StackAlign-1));
2806 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002807 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002808 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002809 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2810
2811 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002812 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002813 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002814 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002815 setValue(&I, DSA);
2816 DAG.setRoot(DSA.getValue(1));
2817
2818 // Inform the Frame Information that we have just allocated a variable-sized
2819 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002820 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002821}
2822
2823void SelectionDAGLowering::visitLoad(LoadInst &I) {
2824 const Value *SV = I.getOperand(0);
2825 SDValue Ptr = getValue(SV);
2826
2827 const Type *Ty = I.getType();
2828 bool isVolatile = I.isVolatile();
2829 unsigned Alignment = I.getAlignment();
2830
Owen Andersone50ed302009-08-10 22:56:29 +00002831 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 SmallVector<uint64_t, 4> Offsets;
2833 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2834 unsigned NumValues = ValueVTs.size();
2835 if (NumValues == 0)
2836 return;
2837
2838 SDValue Root;
2839 bool ConstantMemory = false;
2840 if (I.isVolatile())
2841 // Serialize volatile loads with other side effects.
2842 Root = getRoot();
2843 else if (AA->pointsToConstantMemory(SV)) {
2844 // Do not serialize (non-volatile) loads of constant memory with anything.
2845 Root = DAG.getEntryNode();
2846 ConstantMemory = true;
2847 } else {
2848 // Do not serialize non-volatile loads against each other.
2849 Root = DAG.getRoot();
2850 }
2851
2852 SmallVector<SDValue, 4> Values(NumValues);
2853 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002854 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002855 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002856 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Nate Begemane6798372009-09-15 00:13:12 +00002857 DAG.getNode(ISD::ADD, getCurDebugLoc(),
2858 PtrVT, Ptr,
2859 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002860 SV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002861 Values[i] = L;
2862 Chains[i] = L.getValue(1);
2863 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002864
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002865 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002866 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002867 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002868 &Chains[0], NumValues);
2869 if (isVolatile)
2870 DAG.setRoot(Chain);
2871 else
2872 PendingLoads.push_back(Chain);
2873 }
2874
Scott Michelfdc40a02009-02-17 22:15:04 +00002875 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002876 DAG.getVTList(&ValueVTs[0], NumValues),
2877 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002878}
2879
2880
2881void SelectionDAGLowering::visitStore(StoreInst &I) {
2882 Value *SrcV = I.getOperand(0);
2883 Value *PtrV = I.getOperand(1);
2884
Owen Andersone50ed302009-08-10 22:56:29 +00002885 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002886 SmallVector<uint64_t, 4> Offsets;
2887 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2888 unsigned NumValues = ValueVTs.size();
2889 if (NumValues == 0)
2890 return;
2891
2892 // Get the lowered operands. Note that we do this after
2893 // checking if NumResults is zero, because with zero results
2894 // the operands won't have values in the map.
2895 SDValue Src = getValue(SrcV);
2896 SDValue Ptr = getValue(PtrV);
2897
2898 SDValue Root = getRoot();
2899 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002900 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002901 bool isVolatile = I.isVolatile();
2902 unsigned Alignment = I.getAlignment();
2903 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002904 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002905 SDValue(Src.getNode(), Src.getResNo() + i),
Scott Michelfdc40a02009-02-17 22:15:04 +00002906 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002907 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002908 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002909 PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002910
Scott Michelfdc40a02009-02-17 22:15:04 +00002911 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002912 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002913}
2914
2915/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2916/// node.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002917void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002918 unsigned Intrinsic) {
2919 bool HasChain = !I.doesNotAccessMemory();
2920 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2921
2922 // Build the operand list.
2923 SmallVector<SDValue, 8> Ops;
2924 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2925 if (OnlyLoad) {
2926 // We don't need to serialize loads against other loads.
2927 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002928 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002929 Ops.push_back(getRoot());
2930 }
2931 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002932
2933 // Info is set by getTgtMemInstrinsic
2934 TargetLowering::IntrinsicInfo Info;
2935 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2936
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002937 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002938 if (!IsTgtIntrinsic)
2939 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002940
2941 // Add all operands of the call to the operand list.
2942 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2943 SDValue Op = getValue(I.getOperand(i));
2944 assert(TLI.isTypeLegal(Op.getValueType()) &&
2945 "Intrinsic uses a non-legal type?");
2946 Ops.push_back(Op);
2947 }
2948
Owen Andersone50ed302009-08-10 22:56:29 +00002949 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002950 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2951#ifndef NDEBUG
2952 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2953 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2954 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002955 }
Bob Wilson8d919552009-07-31 22:41:21 +00002956#endif // NDEBUG
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002957 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002959
Bob Wilson8d919552009-07-31 22:41:21 +00002960 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002961
2962 // Create the node.
2963 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002964 if (IsTgtIntrinsic) {
2965 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002966 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002967 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002968 Info.memVT, Info.ptrVal, Info.offset,
2969 Info.align, Info.vol,
2970 Info.readMem, Info.writeMem);
2971 }
2972 else if (!HasChain)
Scott Michelfdc40a02009-02-17 22:15:04 +00002973 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002974 VTs, &Ops[0], Ops.size());
Owen Anderson1d0be152009-08-13 21:58:54 +00002975 else if (I.getType() != Type::getVoidTy(*DAG.getContext()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002976 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002977 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002978 else
Scott Michelfdc40a02009-02-17 22:15:04 +00002979 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002980 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002981
2982 if (HasChain) {
2983 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2984 if (OnlyLoad)
2985 PendingLoads.push_back(Chain);
2986 else
2987 DAG.setRoot(Chain);
2988 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002989 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002990 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002991 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002992 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002993 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002994 setValue(&I, Result);
2995 }
2996}
2997
2998/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
2999static GlobalVariable *ExtractTypeInfo(Value *V) {
3000 V = V->stripPointerCasts();
3001 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
3002 assert ((GV || isa<ConstantPointerNull>(V)) &&
3003 "TypeInfo must be a global variable or NULL");
3004 return GV;
3005}
3006
3007namespace llvm {
3008
3009/// AddCatchInfo - Extract the personality and type infos from an eh.selector
3010/// call, and add them to the specified machine basic block.
3011void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
3012 MachineBasicBlock *MBB) {
3013 // Inform the MachineModuleInfo of the personality for this landing pad.
3014 ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
3015 assert(CE->getOpcode() == Instruction::BitCast &&
3016 isa<Function>(CE->getOperand(0)) &&
3017 "Personality should be a function");
3018 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
3019
3020 // Gather all the type infos for this landing pad and pass them along to
3021 // MachineModuleInfo.
3022 std::vector<GlobalVariable *> TyInfo;
3023 unsigned N = I.getNumOperands();
3024
3025 for (unsigned i = N - 1; i > 2; --i) {
3026 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
3027 unsigned FilterLength = CI->getZExtValue();
3028 unsigned FirstCatch = i + FilterLength + !FilterLength;
3029 assert (FirstCatch <= N && "Invalid filter length");
3030
3031 if (FirstCatch < N) {
3032 TyInfo.reserve(N - FirstCatch);
3033 for (unsigned j = FirstCatch; j < N; ++j)
3034 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3035 MMI->addCatchTypeInfo(MBB, TyInfo);
3036 TyInfo.clear();
3037 }
3038
3039 if (!FilterLength) {
3040 // Cleanup.
3041 MMI->addCleanup(MBB);
3042 } else {
3043 // Filter.
3044 TyInfo.reserve(FilterLength - 1);
3045 for (unsigned j = i + 1; j < FirstCatch; ++j)
3046 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3047 MMI->addFilterTypeInfo(MBB, TyInfo);
3048 TyInfo.clear();
3049 }
3050
3051 N = i;
3052 }
3053 }
3054
3055 if (N > 3) {
3056 TyInfo.reserve(N - 3);
3057 for (unsigned j = 3; j < N; ++j)
3058 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3059 MMI->addCatchTypeInfo(MBB, TyInfo);
3060 }
3061}
3062
3063}
3064
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003065/// GetSignificand - Get the significand and build it into a floating-point
3066/// number with exponent of 1:
3067///
3068/// Op = (Op & 0x007fffff) | 0x3f800000;
3069///
3070/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003071static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003072GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003073 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3074 DAG.getConstant(0x007fffff, MVT::i32));
3075 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3076 DAG.getConstant(0x3f800000, MVT::i32));
3077 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003078}
3079
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003080/// GetExponent - Get the exponent:
3081///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003082/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003083///
3084/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003085static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003086GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
3087 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003088 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3089 DAG.getConstant(0x7f800000, MVT::i32));
3090 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003091 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003092 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3093 DAG.getConstant(127, MVT::i32));
3094 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003095}
3096
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003097/// getF32Constant - Get 32-bit floating point constant.
3098static SDValue
3099getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003100 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003101}
3102
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003103/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003104/// visitIntrinsicCall: I is a call instruction
3105/// Op is the associated NodeType for I
3106const char *
3107SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003108 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003109 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003110 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003111 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003112 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003113 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003114 getValue(I.getOperand(2)),
3115 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003116 setValue(&I, L);
3117 DAG.setRoot(L.getValue(1));
3118 return 0;
3119}
3120
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003121// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003122const char *
3123SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003124 SDValue Op1 = getValue(I.getOperand(1));
3125 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003126
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003128 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003129
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003130 setValue(&I, Result);
3131 return 0;
3132}
Bill Wendling74c37652008-12-09 22:08:41 +00003133
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003134/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3135/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003136void
3137SelectionDAGLowering::visitExp(CallInst &I) {
3138 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003139 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003140
Owen Anderson825b72b2009-08-11 20:47:22 +00003141 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003142 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3143 SDValue Op = getValue(I.getOperand(1));
3144
3145 // Put the exponent in the right bit position for later addition to the
3146 // final result:
3147 //
3148 // #define LOG2OFe 1.4426950f
3149 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003151 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003153
3154 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3156 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003157
3158 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003159 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003160 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003161
3162 if (LimitFloatPrecision <= 6) {
3163 // For floating-point precision of 6:
3164 //
3165 // TwoToFractionalPartOfX =
3166 // 0.997535578f +
3167 // (0.735607626f + 0.252464424f * x) * x;
3168 //
3169 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003171 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003172 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003173 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003174 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3175 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003176 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003177 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003178
3179 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003180 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003181 TwoToFracPartOfX, IntegerPartOfX);
3182
Owen Anderson825b72b2009-08-11 20:47:22 +00003183 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003184 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3185 // For floating-point precision of 12:
3186 //
3187 // TwoToFractionalPartOfX =
3188 // 0.999892986f +
3189 // (0.696457318f +
3190 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3191 //
3192 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003193 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003194 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003196 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003197 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3198 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003199 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003200 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3201 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003202 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003204
3205 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003206 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003207 TwoToFracPartOfX, IntegerPartOfX);
3208
Owen Anderson825b72b2009-08-11 20:47:22 +00003209 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003210 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3211 // For floating-point precision of 18:
3212 //
3213 // TwoToFractionalPartOfX =
3214 // 0.999999982f +
3215 // (0.693148872f +
3216 // (0.240227044f +
3217 // (0.554906021e-1f +
3218 // (0.961591928e-2f +
3219 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3220 //
3221 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003223 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003224 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003225 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3227 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003228 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3230 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003231 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3233 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003234 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003235 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3236 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003237 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003238 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3239 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003240 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003241 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003242 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003243
3244 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003246 TwoToFracPartOfX, IntegerPartOfX);
3247
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003249 }
3250 } else {
3251 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003252 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003253 getValue(I.getOperand(1)).getValueType(),
3254 getValue(I.getOperand(1)));
3255 }
3256
Dale Johannesen59e577f2008-09-05 18:38:42 +00003257 setValue(&I, result);
3258}
3259
Bill Wendling39150252008-09-09 20:39:27 +00003260/// visitLog - Lower a log intrinsic. Handles the special sequences for
3261/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003262void
3263SelectionDAGLowering::visitLog(CallInst &I) {
3264 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003265 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003266
Owen Anderson825b72b2009-08-11 20:47:22 +00003267 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003268 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3269 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003270 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003271
3272 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003273 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003274 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003275 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003276
3277 // Get the significand and build it into a floating-point number with
3278 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003279 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003280
3281 if (LimitFloatPrecision <= 6) {
3282 // For floating-point precision of 6:
3283 //
3284 // LogofMantissa =
3285 // -1.1609546f +
3286 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003287 //
Bill Wendling39150252008-09-09 20:39:27 +00003288 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003290 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003292 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3294 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003295 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003296
Scott Michelfdc40a02009-02-17 22:15:04 +00003297 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003299 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3300 // For floating-point precision of 12:
3301 //
3302 // LogOfMantissa =
3303 // -1.7417939f +
3304 // (2.8212026f +
3305 // (-1.4699568f +
3306 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3307 //
3308 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003309 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003310 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003311 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003312 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3314 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003315 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3317 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003318 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3320 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003321 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003322
Scott Michelfdc40a02009-02-17 22:15:04 +00003323 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003324 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003325 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3326 // For floating-point precision of 18:
3327 //
3328 // LogOfMantissa =
3329 // -2.1072184f +
3330 // (4.2372794f +
3331 // (-3.7029485f +
3332 // (2.2781945f +
3333 // (-0.87823314f +
3334 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3335 //
3336 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003338 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003340 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3342 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003343 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3345 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003346 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3348 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3351 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003352 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3354 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003355 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003356
Scott Michelfdc40a02009-02-17 22:15:04 +00003357 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003359 }
3360 } else {
3361 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003362 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003363 getValue(I.getOperand(1)).getValueType(),
3364 getValue(I.getOperand(1)));
3365 }
3366
Dale Johannesen59e577f2008-09-05 18:38:42 +00003367 setValue(&I, result);
3368}
3369
Bill Wendling3eb59402008-09-09 00:28:24 +00003370/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3371/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003372void
3373SelectionDAGLowering::visitLog2(CallInst &I) {
3374 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003375 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003376
Owen Anderson825b72b2009-08-11 20:47:22 +00003377 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003378 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3379 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003380 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003381
Bill Wendling39150252008-09-09 20:39:27 +00003382 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003383 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003384
3385 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003386 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003387 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003388
Bill Wendling3eb59402008-09-09 00:28:24 +00003389 // Different possible minimax approximations of significand in
3390 // floating-point for various degrees of accuracy over [1,2].
3391 if (LimitFloatPrecision <= 6) {
3392 // For floating-point precision of 6:
3393 //
3394 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3395 //
3396 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003398 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003400 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3402 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003403 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003404
Scott Michelfdc40a02009-02-17 22:15:04 +00003405 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003407 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3408 // For floating-point precision of 12:
3409 //
3410 // Log2ofMantissa =
3411 // -2.51285454f +
3412 // (4.07009056f +
3413 // (-2.12067489f +
3414 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003415 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003416 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003417 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003418 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003420 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3422 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003423 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3425 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003426 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3428 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003429 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003430
Scott Michelfdc40a02009-02-17 22:15:04 +00003431 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003433 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3434 // For floating-point precision of 18:
3435 //
3436 // Log2ofMantissa =
3437 // -3.0400495f +
3438 // (6.1129976f +
3439 // (-5.3420409f +
3440 // (3.2865683f +
3441 // (-1.2669343f +
3442 // (0.27515199f -
3443 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3444 //
3445 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003446 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003447 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003449 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3451 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3454 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003455 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3457 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003458 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3460 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003461 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3463 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003464 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003465
Scott Michelfdc40a02009-02-17 22:15:04 +00003466 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003468 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003469 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003470 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003471 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003472 getValue(I.getOperand(1)).getValueType(),
3473 getValue(I.getOperand(1)));
3474 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003475
Dale Johannesen59e577f2008-09-05 18:38:42 +00003476 setValue(&I, result);
3477}
3478
Bill Wendling3eb59402008-09-09 00:28:24 +00003479/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3480/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003481void
3482SelectionDAGLowering::visitLog10(CallInst &I) {
3483 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003484 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003485
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003487 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3488 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003489 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003490
Bill Wendling39150252008-09-09 20:39:27 +00003491 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003492 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003494 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003495
3496 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003497 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003498 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003499
3500 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003501 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003502 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003503 // Log10ofMantissa =
3504 // -0.50419619f +
3505 // (0.60948995f - 0.10380950f * x) * x;
3506 //
3507 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003509 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003510 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003511 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3513 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003514 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003515
Scott Michelfdc40a02009-02-17 22:15:04 +00003516 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003517 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003518 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3519 // For floating-point precision of 12:
3520 //
3521 // Log10ofMantissa =
3522 // -0.64831180f +
3523 // (0.91751397f +
3524 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3525 //
3526 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003528 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003530 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3532 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003533 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3535 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003536 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003537
Scott Michelfdc40a02009-02-17 22:15:04 +00003538 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003540 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003541 // For floating-point precision of 18:
3542 //
3543 // Log10ofMantissa =
3544 // -0.84299375f +
3545 // (1.5327582f +
3546 // (-1.0688956f +
3547 // (0.49102474f +
3548 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3549 //
3550 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003552 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003554 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3556 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003557 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3559 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003560 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3562 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3565 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003567
Scott Michelfdc40a02009-02-17 22:15:04 +00003568 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003570 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003571 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003572 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003573 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003574 getValue(I.getOperand(1)).getValueType(),
3575 getValue(I.getOperand(1)));
3576 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003577
Dale Johannesen59e577f2008-09-05 18:38:42 +00003578 setValue(&I, result);
3579}
3580
Bill Wendlinge10c8142008-09-09 22:39:21 +00003581/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3582/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003583void
3584SelectionDAGLowering::visitExp2(CallInst &I) {
3585 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003586 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003587
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003589 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3590 SDValue Op = getValue(I.getOperand(1));
3591
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003593
3594 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3596 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003597
3598 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003599 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003600 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003601
3602 if (LimitFloatPrecision <= 6) {
3603 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003604 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003605 // TwoToFractionalPartOfX =
3606 // 0.997535578f +
3607 // (0.735607626f + 0.252464424f * x) * x;
3608 //
3609 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003611 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003613 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3615 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003616 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003617 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003618 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003619 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003620
Scott Michelfdc40a02009-02-17 22:15:04 +00003621 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003622 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003623 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3624 // For floating-point precision of 12:
3625 //
3626 // TwoToFractionalPartOfX =
3627 // 0.999892986f +
3628 // (0.696457318f +
3629 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3630 //
3631 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003632 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003633 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003635 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003636 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3637 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003638 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003639 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3640 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003641 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003642 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003643 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003644 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003645
Scott Michelfdc40a02009-02-17 22:15:04 +00003646 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003648 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3649 // For floating-point precision of 18:
3650 //
3651 // TwoToFractionalPartOfX =
3652 // 0.999999982f +
3653 // (0.693148872f +
3654 // (0.240227044f +
3655 // (0.554906021e-1f +
3656 // (0.961591928e-2f +
3657 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3658 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003659 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003660 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003662 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003663 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3664 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003665 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003666 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3667 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003668 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003669 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3670 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003671 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003672 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3673 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003674 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003675 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3676 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003677 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003678 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003679 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003681
Scott Michelfdc40a02009-02-17 22:15:04 +00003682 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003684 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003685 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003686 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003687 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003688 getValue(I.getOperand(1)).getValueType(),
3689 getValue(I.getOperand(1)));
3690 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003691
Dale Johannesen601d3c02008-09-05 01:48:15 +00003692 setValue(&I, result);
3693}
3694
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003695/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3696/// limited-precision mode with x == 10.0f.
3697void
3698SelectionDAGLowering::visitPow(CallInst &I) {
3699 SDValue result;
3700 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003701 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003702 bool IsExp10 = false;
3703
Owen Anderson825b72b2009-08-11 20:47:22 +00003704 if (getValue(Val).getValueType() == MVT::f32 &&
3705 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003706 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3707 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3708 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3709 APFloat Ten(10.0f);
3710 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3711 }
3712 }
3713 }
3714
3715 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3716 SDValue Op = getValue(I.getOperand(2));
3717
3718 // Put the exponent in the right bit position for later addition to the
3719 // final result:
3720 //
3721 // #define LOG2OF10 3.3219281f
3722 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003723 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003724 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003725 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003726
3727 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003728 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3729 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003730
3731 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003733 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003734
3735 if (LimitFloatPrecision <= 6) {
3736 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003737 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003738 // twoToFractionalPartOfX =
3739 // 0.997535578f +
3740 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003741 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003742 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003743 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003744 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003745 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003746 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003747 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3748 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003749 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003750 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003751 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003752 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003753
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003754 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003755 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003756 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3757 // For floating-point precision of 12:
3758 //
3759 // TwoToFractionalPartOfX =
3760 // 0.999892986f +
3761 // (0.696457318f +
3762 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3763 //
3764 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003765 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003766 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003768 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003769 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3770 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003771 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003772 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3773 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003774 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003775 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003776 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003777 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003778
Scott Michelfdc40a02009-02-17 22:15:04 +00003779 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003780 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003781 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3782 // For floating-point precision of 18:
3783 //
3784 // TwoToFractionalPartOfX =
3785 // 0.999999982f +
3786 // (0.693148872f +
3787 // (0.240227044f +
3788 // (0.554906021e-1f +
3789 // (0.961591928e-2f +
3790 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3791 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003792 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003793 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003795 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3797 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003798 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3800 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003801 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3803 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003804 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3806 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003807 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003808 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3809 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003810 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003812 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003814
Scott Michelfdc40a02009-02-17 22:15:04 +00003815 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003817 }
3818 } else {
3819 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003820 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003821 getValue(I.getOperand(1)).getValueType(),
3822 getValue(I.getOperand(1)),
3823 getValue(I.getOperand(2)));
3824 }
3825
3826 setValue(&I, result);
3827}
3828
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003829/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3830/// we want to emit this as a call to a named external function, return the name
3831/// otherwise lower it and return null.
3832const char *
3833SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003834 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003835 switch (Intrinsic) {
3836 default:
3837 // By default, turn this into a target intrinsic node.
3838 visitTargetIntrinsic(I, Intrinsic);
3839 return 0;
3840 case Intrinsic::vastart: visitVAStart(I); return 0;
3841 case Intrinsic::vaend: visitVAEnd(I); return 0;
3842 case Intrinsic::vacopy: visitVACopy(I); return 0;
3843 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003844 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003845 getValue(I.getOperand(1))));
3846 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003847 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003848 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003849 getValue(I.getOperand(1))));
3850 return 0;
3851 case Intrinsic::setjmp:
3852 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3853 break;
3854 case Intrinsic::longjmp:
3855 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3856 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003857 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003858 SDValue Op1 = getValue(I.getOperand(1));
3859 SDValue Op2 = getValue(I.getOperand(2));
3860 SDValue Op3 = getValue(I.getOperand(3));
3861 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003862 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003863 I.getOperand(1), 0, I.getOperand(2), 0));
3864 return 0;
3865 }
Chris Lattner824b9582008-11-21 16:42:48 +00003866 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003867 SDValue Op1 = getValue(I.getOperand(1));
3868 SDValue Op2 = getValue(I.getOperand(2));
3869 SDValue Op3 = getValue(I.getOperand(3));
3870 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003871 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003872 I.getOperand(1), 0));
3873 return 0;
3874 }
Chris Lattner824b9582008-11-21 16:42:48 +00003875 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003876 SDValue Op1 = getValue(I.getOperand(1));
3877 SDValue Op2 = getValue(I.getOperand(2));
3878 SDValue Op3 = getValue(I.getOperand(3));
3879 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3880
3881 // If the source and destination are known to not be aliases, we can
3882 // lower memmove as memcpy.
3883 uint64_t Size = -1ULL;
3884 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003885 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003886 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3887 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00003888 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003889 I.getOperand(1), 0, I.getOperand(2), 0));
3890 return 0;
3891 }
3892
Dale Johannesena04b7572009-02-03 23:04:43 +00003893 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003894 I.getOperand(1), 0, I.getOperand(2), 0));
3895 return 0;
3896 }
3897 case Intrinsic::dbg_stoppoint: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003898 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003899 if (isValidDebugInfoIntrinsic(SPI, CodeGenOpt::Default)) {
Evan Chenge3d42322009-02-25 07:04:34 +00003900 MachineFunction &MF = DAG.getMachineFunction();
Devang Patel7e1e31f2009-07-02 22:43:26 +00003901 DebugLoc Loc = ExtractDebugLocation(SPI, MF.getDebugLocInfo());
Chris Lattneraf29a522009-05-04 22:10:05 +00003902 setCurDebugLoc(Loc);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003903
Bill Wendling98a366d2009-04-29 23:29:43 +00003904 if (OptLevel == CodeGenOpt::None)
Chris Lattneraf29a522009-05-04 22:10:05 +00003905 DAG.setRoot(DAG.getDbgStopPoint(Loc, getRoot(),
Dale Johannesenbeaec4c2009-03-25 17:36:08 +00003906 SPI.getLine(),
3907 SPI.getColumn(),
3908 SPI.getContext()));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003909 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003910 return 0;
3911 }
3912 case Intrinsic::dbg_region_start: {
Devang Patel83489bb2009-01-13 00:35:13 +00003913 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003914 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003915 if (isValidDebugInfoIntrinsic(RSI, OptLevel) && DW
3916 && DW->ShouldEmitDwarfDebug()) {
Bill Wendlingdf7d5d32009-05-21 00:04:55 +00003917 unsigned LabelID =
Devang Patele4b27562009-08-28 23:24:31 +00003918 DW->RecordRegionStart(RSI.getContext());
Devang Patel48c7fa22009-04-13 18:13:16 +00003919 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3920 getRoot(), LabelID));
Bill Wendling92c1e122009-02-13 02:16:35 +00003921 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003922 return 0;
3923 }
3924 case Intrinsic::dbg_region_end: {
Devang Patel83489bb2009-01-13 00:35:13 +00003925 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003926 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
Devang Patel0f7fef32009-04-13 17:02:03 +00003927
Devang Patel7e1e31f2009-07-02 22:43:26 +00003928 if (!isValidDebugInfoIntrinsic(REI, OptLevel) || !DW
3929 || !DW->ShouldEmitDwarfDebug())
3930 return 0;
Bill Wendling6c4311d2009-05-08 21:14:49 +00003931
Devang Patel7e1e31f2009-07-02 22:43:26 +00003932 MachineFunction &MF = DAG.getMachineFunction();
Devang Patele4b27562009-08-28 23:24:31 +00003933 DISubprogram Subprogram(REI.getContext());
Devang Patel7e1e31f2009-07-02 22:43:26 +00003934
3935 if (isInlinedFnEnd(REI, MF.getFunction())) {
3936 // This is end of inlined function. Debugging information for inlined
3937 // function is not handled yet (only supported by FastISel).
3938 if (OptLevel == CodeGenOpt::None) {
3939 unsigned ID = DW->RecordInlinedFnEnd(Subprogram);
3940 if (ID != 0)
3941 // Returned ID is 0 if this is unbalanced "end of inlined
3942 // scope". This could happen if optimizer eats dbg intrinsics or
3943 // "beginning of inlined scope" is not recoginized due to missing
3944 // location info. In such cases, do ignore this region.end.
3945 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3946 getRoot(), ID));
Devang Patel0f7fef32009-04-13 17:02:03 +00003947 }
Devang Patel7e1e31f2009-07-02 22:43:26 +00003948 return 0;
3949 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003950
Devang Patel7e1e31f2009-07-02 22:43:26 +00003951 unsigned LabelID =
Devang Patele4b27562009-08-28 23:24:31 +00003952 DW->RecordRegionEnd(REI.getContext());
Devang Patel7e1e31f2009-07-02 22:43:26 +00003953 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3954 getRoot(), LabelID));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003955 return 0;
3956 }
3957 case Intrinsic::dbg_func_start: {
Devang Patel83489bb2009-01-13 00:35:13 +00003958 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003959 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
Jeffrey Yasskin32360a72009-07-16 21:07:26 +00003960 if (!isValidDebugInfoIntrinsic(FSI, CodeGenOpt::None))
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00003961 return 0;
Devang Patel16f2ffd2009-04-16 02:33:41 +00003962
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00003963 MachineFunction &MF = DAG.getMachineFunction();
Devang Patel7e1e31f2009-07-02 22:43:26 +00003964 // This is a beginning of an inlined function.
3965 if (isInlinedFnStart(FSI, MF.getFunction())) {
3966 if (OptLevel != CodeGenOpt::None)
3967 // FIXME: Debugging informaation for inlined function is only
3968 // supported at CodeGenOpt::Node.
3969 return 0;
3970
Bill Wendlingc677fe52009-05-10 00:10:50 +00003971 DebugLoc PrevLoc = CurDebugLoc;
Devang Patel07b0ec02009-07-02 00:08:09 +00003972 // If llvm.dbg.func.start is seen in a new block before any
3973 // llvm.dbg.stoppoint intrinsic then the location info is unknown.
3974 // FIXME : Why DebugLoc is reset at the beginning of each block ?
3975 if (PrevLoc.isUnknown())
3976 return 0;
Devang Patel07b0ec02009-07-02 00:08:09 +00003977
Devang Patel7e1e31f2009-07-02 22:43:26 +00003978 // Record the source line.
3979 setCurDebugLoc(ExtractDebugLocation(FSI, MF.getDebugLocInfo()));
3980
Jeffrey Yasskin32360a72009-07-16 21:07:26 +00003981 if (!DW || !DW->ShouldEmitDwarfDebug())
3982 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003983 DebugLocTuple PrevLocTpl = MF.getDebugLocTuple(PrevLoc);
Devang Patele4b27562009-08-28 23:24:31 +00003984 DISubprogram SP(FSI.getSubprogram());
Devang Patel1619dc32009-10-13 23:28:53 +00003985 DICompileUnit CU(PrevLocTpl.Scope);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003986 unsigned LabelID = DW->RecordInlinedFnStart(SP, CU,
3987 PrevLocTpl.Line,
3988 PrevLocTpl.Col);
3989 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3990 getRoot(), LabelID));
Devang Patel07b0ec02009-07-02 00:08:09 +00003991 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003992 }
3993
Devang Patel07b0ec02009-07-02 00:08:09 +00003994 // This is a beginning of a new function.
Devang Patel7e1e31f2009-07-02 22:43:26 +00003995 MF.setDefaultDebugLoc(ExtractDebugLocation(FSI, MF.getDebugLocInfo()));
Jeffrey Yasskin32360a72009-07-16 21:07:26 +00003996
3997 if (!DW || !DW->ShouldEmitDwarfDebug())
3998 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003999 // llvm.dbg.func_start also defines beginning of function scope.
Devang Patele4b27562009-08-28 23:24:31 +00004000 DW->RecordRegionStart(FSI.getSubprogram());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004001 return 0;
4002 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004003 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00004004 if (OptLevel != CodeGenOpt::None)
4005 // FIXME: Variable debug info is not supported here.
4006 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004007 DwarfWriter *DW = DAG.getDwarfWriter();
4008 if (!DW)
4009 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004010 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
4011 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
4012 return 0;
4013
Devang Patelac1ceb32009-10-09 22:42:28 +00004014 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004015 Value *Address = DI.getAddress();
4016 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4017 Address = BCI->getOperand(0);
4018 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4019 // Don't handle byval struct arguments or VLAs, for example.
4020 if (!AI)
4021 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004022 DenseMap<const AllocaInst*, int>::iterator SI =
4023 FuncInfo.StaticAllocaMap.find(AI);
4024 if (SI == FuncInfo.StaticAllocaMap.end())
4025 return 0; // VLAs.
4026 int FI = SI->second;
Devang Patelac1ceb32009-10-09 22:42:28 +00004027#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
4028 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4029 if (MMI)
4030 MMI->setVariableDbgInfo(Variable, FI);
4031#else
4032 DW->RecordVariable(Variable, FI);
4033#endif
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004034 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004035 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004036 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004037 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004038 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004040 SDValue Ops[1];
4041 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004042 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004043 setValue(&I, Op);
4044 DAG.setRoot(Op.getValue(1));
4045 return 0;
4046 }
4047
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004048 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004049 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004050
Chris Lattner3a5815f2009-09-17 23:54:54 +00004051 if (CurMBB->isLandingPad())
4052 AddCatchInfo(I, MMI, CurMBB);
4053 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004054#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004055 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004056#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004057 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4058 unsigned Reg = TLI.getExceptionSelectorRegister();
4059 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004060 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004061
Chris Lattner3a5815f2009-09-17 23:54:54 +00004062 // Insert the EHSELECTION instruction.
4063 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4064 SDValue Ops[2];
4065 Ops[0] = getValue(I.getOperand(1));
4066 Ops[1] = getRoot();
4067 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4068
4069 DAG.setRoot(Op.getValue(1));
4070
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004071 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004072 return 0;
4073 }
4074
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004075 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004076 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004077
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004078 if (MMI) {
4079 // Find the type id for the given typeinfo.
4080 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
4081
4082 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004083 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004084 } else {
4085 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004086 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004087 }
4088
4089 return 0;
4090 }
4091
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004092 case Intrinsic::eh_return_i32:
4093 case Intrinsic::eh_return_i64:
4094 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004095 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004096 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004097 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004098 getControlRoot(),
4099 getValue(I.getOperand(1)),
4100 getValue(I.getOperand(2))));
4101 } else {
4102 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4103 }
4104
4105 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004106 case Intrinsic::eh_unwind_init:
4107 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4108 MMI->setCallsUnwindInit(true);
4109 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004110
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004111 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004112
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004113 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004114 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004115 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4116 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004117
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004118 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004119 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004120 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004121 TLI.getPointerTy()),
4122 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004123 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004124 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004125 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004126 TLI.getPointerTy(),
4127 DAG.getConstant(0,
4128 TLI.getPointerTy())),
4129 Offset));
4130 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004131 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004132 case Intrinsic::convertff:
4133 case Intrinsic::convertfsi:
4134 case Intrinsic::convertfui:
4135 case Intrinsic::convertsif:
4136 case Intrinsic::convertuif:
4137 case Intrinsic::convertss:
4138 case Intrinsic::convertsu:
4139 case Intrinsic::convertus:
4140 case Intrinsic::convertuu: {
4141 ISD::CvtCode Code = ISD::CVT_INVALID;
4142 switch (Intrinsic) {
4143 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4144 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4145 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4146 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4147 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4148 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4149 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4150 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4151 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4152 }
Owen Andersone50ed302009-08-10 22:56:29 +00004153 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00004154 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00004155 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00004156 DAG.getValueType(DestVT),
4157 DAG.getValueType(getValue(Op1).getValueType()),
4158 getValue(I.getOperand(2)),
4159 getValue(I.getOperand(3)),
4160 Code));
4161 return 0;
4162 }
4163
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004164 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004165 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004166 getValue(I.getOperand(1)).getValueType(),
4167 getValue(I.getOperand(1))));
4168 return 0;
4169 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004170 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004171 getValue(I.getOperand(1)).getValueType(),
4172 getValue(I.getOperand(1)),
4173 getValue(I.getOperand(2))));
4174 return 0;
4175 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004176 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004177 getValue(I.getOperand(1)).getValueType(),
4178 getValue(I.getOperand(1))));
4179 return 0;
4180 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004181 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004182 getValue(I.getOperand(1)).getValueType(),
4183 getValue(I.getOperand(1))));
4184 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004185 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004186 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004187 return 0;
4188 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004189 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004190 return 0;
4191 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004192 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004193 return 0;
4194 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004195 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004196 return 0;
4197 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004198 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004199 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004200 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004201 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004202 return 0;
4203 case Intrinsic::pcmarker: {
4204 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004205 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004206 return 0;
4207 }
4208 case Intrinsic::readcyclecounter: {
4209 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004210 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004211 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004212 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004213 setValue(&I, Tmp);
4214 DAG.setRoot(Tmp.getValue(1));
4215 return 0;
4216 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004217 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004218 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004219 getValue(I.getOperand(1)).getValueType(),
4220 getValue(I.getOperand(1))));
4221 return 0;
4222 case Intrinsic::cttz: {
4223 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004224 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004225 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004226 setValue(&I, result);
4227 return 0;
4228 }
4229 case Intrinsic::ctlz: {
4230 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004231 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004232 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004233 setValue(&I, result);
4234 return 0;
4235 }
4236 case Intrinsic::ctpop: {
4237 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004238 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004239 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004240 setValue(&I, result);
4241 return 0;
4242 }
4243 case Intrinsic::stacksave: {
4244 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004245 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004246 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004247 setValue(&I, Tmp);
4248 DAG.setRoot(Tmp.getValue(1));
4249 return 0;
4250 }
4251 case Intrinsic::stackrestore: {
4252 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004253 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004254 return 0;
4255 }
Bill Wendling57344502008-11-18 11:01:33 +00004256 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004257 // Emit code into the DAG to store the stack guard onto the stack.
4258 MachineFunction &MF = DAG.getMachineFunction();
4259 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004260 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004261
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004262 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4263 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004264
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004265 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004266 MFI->setStackProtectorIndex(FI);
4267
4268 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4269
4270 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004271 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00004272 PseudoSourceValue::getFixedStack(FI),
4273 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004274 setValue(&I, Result);
4275 DAG.setRoot(Result);
4276 return 0;
4277 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004278 case Intrinsic::objectsize: {
4279 // If we don't know by now, we're never going to know.
4280 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4281
4282 assert(CI && "Non-constant type in __builtin_object_size?");
4283
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004284 SDValue Arg = getValue(I.getOperand(0));
4285 EVT Ty = Arg.getValueType();
4286
Eric Christopher7b5e6172009-10-27 00:52:25 +00004287 if (CI->getZExtValue() < 2)
Eric Christophercdfa6662009-10-31 09:24:35 +00004288 setValue(&I, DAG.getConstant(-1U, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004289 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004290 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004291 return 0;
4292 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004293 case Intrinsic::var_annotation:
4294 // Discard annotate attributes
4295 return 0;
4296
4297 case Intrinsic::init_trampoline: {
4298 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4299
4300 SDValue Ops[6];
4301 Ops[0] = getRoot();
4302 Ops[1] = getValue(I.getOperand(1));
4303 Ops[2] = getValue(I.getOperand(2));
4304 Ops[3] = getValue(I.getOperand(3));
4305 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4306 Ops[5] = DAG.getSrcValue(F);
4307
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004308 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004309 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004310 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004311
4312 setValue(&I, Tmp);
4313 DAG.setRoot(Tmp.getValue(1));
4314 return 0;
4315 }
4316
4317 case Intrinsic::gcroot:
4318 if (GFI) {
4319 Value *Alloca = I.getOperand(1);
4320 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004321
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004322 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4323 GFI->addStackRoot(FI->getIndex(), TypeMap);
4324 }
4325 return 0;
4326
4327 case Intrinsic::gcread:
4328 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004329 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004330 return 0;
4331
4332 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004333 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004334 return 0;
4335 }
4336
4337 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004338 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004339 return 0;
4340 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004341
Bill Wendlingef375462008-11-21 02:38:44 +00004342 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004343 return implVisitAluOverflow(I, ISD::UADDO);
4344 case Intrinsic::sadd_with_overflow:
4345 return implVisitAluOverflow(I, ISD::SADDO);
4346 case Intrinsic::usub_with_overflow:
4347 return implVisitAluOverflow(I, ISD::USUBO);
4348 case Intrinsic::ssub_with_overflow:
4349 return implVisitAluOverflow(I, ISD::SSUBO);
4350 case Intrinsic::umul_with_overflow:
4351 return implVisitAluOverflow(I, ISD::UMULO);
4352 case Intrinsic::smul_with_overflow:
4353 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004354
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004355 case Intrinsic::prefetch: {
4356 SDValue Ops[4];
4357 Ops[0] = getRoot();
4358 Ops[1] = getValue(I.getOperand(1));
4359 Ops[2] = getValue(I.getOperand(2));
4360 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004361 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004362 return 0;
4363 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004364
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004365 case Intrinsic::memory_barrier: {
4366 SDValue Ops[6];
4367 Ops[0] = getRoot();
4368 for (int x = 1; x < 6; ++x)
4369 Ops[x] = getValue(I.getOperand(x));
4370
Owen Anderson825b72b2009-08-11 20:47:22 +00004371 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004372 return 0;
4373 }
4374 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004375 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004376 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004377 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004378 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4379 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004380 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004381 getValue(I.getOperand(2)),
4382 getValue(I.getOperand(3)),
4383 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004384 setValue(&I, L);
4385 DAG.setRoot(L.getValue(1));
4386 return 0;
4387 }
4388 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004389 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004390 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004391 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004393 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004394 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004395 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004396 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004397 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004398 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004399 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004400 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004401 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004402 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004403 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004404 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004405 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004407 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004408 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004409 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004410 }
4411}
4412
Dan Gohman98ca4f22009-08-05 01:29:28 +00004413/// Test if the given instruction is in a position to be optimized
4414/// with a tail-call. This roughly means that it's in a block with
4415/// a return and there's nothing that needs to be scheduled
4416/// between it and the return.
4417///
4418/// This function only tests target-independent requirements.
4419/// For target-dependent requirements, a target should override
4420/// TargetLowering::IsEligibleForTailCallOptimization.
4421///
4422static bool
4423isInTailCallPosition(const Instruction *I, Attributes RetAttr,
4424 const TargetLowering &TLI) {
4425 const BasicBlock *ExitBB = I->getParent();
4426 const TerminatorInst *Term = ExitBB->getTerminator();
4427 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4428 const Function *F = ExitBB->getParent();
4429
4430 // The block must end in a return statement or an unreachable.
4431 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4432
4433 // If I will have a chain, make sure no other instruction that will have a
4434 // chain interposes between I and the return.
4435 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4436 !I->isSafeToSpeculativelyExecute())
4437 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4438 --BBI) {
4439 if (&*BBI == I)
4440 break;
4441 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4442 !BBI->isSafeToSpeculativelyExecute())
4443 return false;
4444 }
4445
4446 // If the block ends with a void return or unreachable, it doesn't matter
4447 // what the call's return type is.
4448 if (!Ret || Ret->getNumOperands() == 0) return true;
4449
4450 // Conservatively require the attributes of the call to match those of
4451 // the return.
4452 if (F->getAttributes().getRetAttributes() != RetAttr)
4453 return false;
4454
4455 // Otherwise, make sure the unmodified return value of I is the return value.
4456 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4457 U = dyn_cast<Instruction>(U->getOperand(0))) {
4458 if (!U)
4459 return false;
4460 if (!U->hasOneUse())
4461 return false;
4462 if (U == I)
4463 break;
4464 // Check for a truly no-op truncate.
4465 if (isa<TruncInst>(U) &&
4466 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4467 continue;
4468 // Check for a truly no-op bitcast.
4469 if (isa<BitCastInst>(U) &&
4470 (U->getOperand(0)->getType() == U->getType() ||
4471 (isa<PointerType>(U->getOperand(0)->getType()) &&
4472 isa<PointerType>(U->getType()))))
4473 continue;
4474 // Otherwise it's not a true no-op.
4475 return false;
4476 }
4477
4478 return true;
4479}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004480
4481void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004482 bool isTailCall,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004483 MachineBasicBlock *LandingPad) {
4484 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4485 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
4486 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4487 unsigned BeginLabel = 0, EndLabel = 0;
4488
4489 TargetLowering::ArgListTy Args;
4490 TargetLowering::ArgListEntry Entry;
4491 Args.reserve(CS.arg_size());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004492 unsigned j = 1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004493 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00004494 i != e; ++i, ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004495 SDValue ArgNode = getValue(*i);
4496 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4497
4498 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004499 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4500 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4501 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4502 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4503 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4504 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004505 Entry.Alignment = CS.getParamAlignment(attrInd);
4506 Args.push_back(Entry);
4507 }
4508
4509 if (LandingPad && MMI) {
4510 // Insert a label before the invoke call to mark the try range. This can be
4511 // used to detect deletion of the invoke via the MachineModuleInfo.
4512 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004513
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004514 // Both PendingLoads and PendingExports must be flushed here;
4515 // this call might not return.
4516 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004517 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4518 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004519 }
4520
Dan Gohman98ca4f22009-08-05 01:29:28 +00004521 // Check if target-independent constraints permit a tail call here.
4522 // Target-dependent constraints are checked within TLI.LowerCallTo.
4523 if (isTailCall &&
4524 !isInTailCallPosition(CS.getInstruction(),
4525 CS.getAttributes().getRetAttributes(),
4526 TLI))
4527 isTailCall = false;
4528
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004529 std::pair<SDValue,SDValue> Result =
4530 TLI.LowerCallTo(getRoot(), CS.getType(),
Devang Patel05988662008-09-25 21:00:45 +00004531 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004532 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004533 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004534 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004535 isTailCall,
4536 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004537 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004538 assert((isTailCall || Result.second.getNode()) &&
4539 "Non-null chain expected with non-tail call!");
4540 assert((Result.second.getNode() || !Result.first.getNode()) &&
4541 "Null value expected with tail call!");
4542 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004543 setValue(CS.getInstruction(), Result.first);
Dan Gohman98ca4f22009-08-05 01:29:28 +00004544 // As a special case, a null chain means that a tail call has
4545 // been emitted and the DAG root is already updated.
4546 if (Result.second.getNode())
4547 DAG.setRoot(Result.second);
4548 else
4549 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004550
4551 if (LandingPad && MMI) {
4552 // Insert a label at the end of the invoke call to mark the try range. This
4553 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4554 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004555 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4556 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004557
4558 // Inform MachineModuleInfo of range.
4559 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4560 }
4561}
4562
4563
4564void SelectionDAGLowering::visitCall(CallInst &I) {
4565 const char *RenameFn = 0;
4566 if (Function *F = I.getCalledFunction()) {
4567 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004568 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4569 if (II) {
4570 if (unsigned IID = II->getIntrinsicID(F)) {
4571 RenameFn = visitIntrinsicCall(I, IID);
4572 if (!RenameFn)
4573 return;
4574 }
4575 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004576 if (unsigned IID = F->getIntrinsicID()) {
4577 RenameFn = visitIntrinsicCall(I, IID);
4578 if (!RenameFn)
4579 return;
4580 }
4581 }
4582
4583 // Check for well-known libc/libm calls. If the function is internal, it
4584 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004585 if (!F->hasLocalLinkage() && F->hasName()) {
4586 StringRef Name = F->getName();
4587 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 if (I.getNumOperands() == 3 && // Basic sanity checks.
4589 I.getOperand(1)->getType()->isFloatingPoint() &&
4590 I.getType() == I.getOperand(1)->getType() &&
4591 I.getType() == I.getOperand(2)->getType()) {
4592 SDValue LHS = getValue(I.getOperand(1));
4593 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004594 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004595 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596 return;
4597 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004598 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004599 if (I.getNumOperands() == 2 && // Basic sanity checks.
4600 I.getOperand(1)->getType()->isFloatingPoint() &&
4601 I.getType() == I.getOperand(1)->getType()) {
4602 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004603 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004604 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004605 return;
4606 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004607 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004608 if (I.getNumOperands() == 2 && // Basic sanity checks.
4609 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004610 I.getType() == I.getOperand(1)->getType() &&
4611 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004612 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004613 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004614 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004615 return;
4616 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004617 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004618 if (I.getNumOperands() == 2 && // Basic sanity checks.
4619 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004620 I.getType() == I.getOperand(1)->getType() &&
4621 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004622 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004623 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004624 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004625 return;
4626 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004627 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4628 if (I.getNumOperands() == 2 && // Basic sanity checks.
4629 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004630 I.getType() == I.getOperand(1)->getType() &&
4631 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004632 SDValue Tmp = getValue(I.getOperand(1));
4633 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4634 Tmp.getValueType(), Tmp));
4635 return;
4636 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004637 }
4638 }
4639 } else if (isa<InlineAsm>(I.getOperand(0))) {
4640 visitInlineAsm(&I);
4641 return;
4642 }
4643
4644 SDValue Callee;
4645 if (!RenameFn)
4646 Callee = getValue(I.getOperand(0));
4647 else
Bill Wendling056292f2008-09-16 21:48:12 +00004648 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004649
Dan Gohman98ca4f22009-08-05 01:29:28 +00004650 // Check if we can potentially perform a tail call. More detailed
4651 // checking is be done within LowerCallTo, after more information
4652 // about the call is known.
4653 bool isTailCall = PerformTailCallOpt && I.isTailCall();
4654
4655 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656}
4657
4658
4659/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004660/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004661/// Chain/Flag as the input and updates them for the output Chain/Flag.
4662/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004663SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004664 SDValue &Chain,
4665 SDValue *Flag) const {
4666 // Assemble the legal parts into the final values.
4667 SmallVector<SDValue, 4> Values(ValueVTs.size());
4668 SmallVector<SDValue, 8> Parts;
4669 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4670 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004671 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004672 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004673 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004674
4675 Parts.resize(NumRegs);
4676 for (unsigned i = 0; i != NumRegs; ++i) {
4677 SDValue P;
4678 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004679 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004680 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004681 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004682 *Flag = P.getValue(2);
4683 }
4684 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004686 // If the source register was virtual and if we know something about it,
4687 // add an assert node.
4688 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4689 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4690 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4691 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4692 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4693 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004694
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004695 unsigned RegSize = RegisterVT.getSizeInBits();
4696 unsigned NumSignBits = LOI.NumSignBits;
4697 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004698
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004699 // FIXME: We capture more information than the dag can represent. For
4700 // now, just use the tightest assertzext/assertsext possible.
4701 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004702 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004703 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004705 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004706 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004707 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004709 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004710 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004711 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004713 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004714 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004715 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004716 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004717 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004718 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004719
Owen Anderson825b72b2009-08-11 20:47:22 +00004720 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004721 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004722 RegisterVT, P, DAG.getValueType(FromVT));
4723
4724 }
4725 }
4726 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004727
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004728 Parts[i] = P;
4729 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004730
Scott Michelfdc40a02009-02-17 22:15:04 +00004731 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004732 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004733 Part += NumRegs;
4734 Parts.clear();
4735 }
4736
Dale Johannesen66978ee2009-01-31 02:22:37 +00004737 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004738 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4739 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004740}
4741
4742/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004743/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004744/// Chain/Flag as the input and updates them for the output Chain/Flag.
4745/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004746void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004747 SDValue &Chain, SDValue *Flag) const {
4748 // Get the list of the values's legal parts.
4749 unsigned NumRegs = Regs.size();
4750 SmallVector<SDValue, 8> Parts(NumRegs);
4751 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004752 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004753 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004754 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004755
Dale Johannesen66978ee2009-01-31 02:22:37 +00004756 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757 &Parts[Part], NumParts, RegisterVT);
4758 Part += NumParts;
4759 }
4760
4761 // Copy the parts into the registers.
4762 SmallVector<SDValue, 8> Chains(NumRegs);
4763 for (unsigned i = 0; i != NumRegs; ++i) {
4764 SDValue Part;
4765 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004766 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004767 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004768 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004769 *Flag = Part.getValue(1);
4770 }
4771 Chains[i] = Part.getValue(0);
4772 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004773
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004774 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004775 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004776 // flagged to it. That is the CopyToReg nodes and the user are considered
4777 // a single scheduling unit. If we create a TokenFactor and return it as
4778 // chain, then the TokenFactor is both a predecessor (operand) of the
4779 // user as well as a successor (the TF operands are flagged to the user).
4780 // c1, f1 = CopyToReg
4781 // c2, f2 = CopyToReg
4782 // c3 = TokenFactor c1, c2
4783 // ...
4784 // = op c3, ..., f2
4785 Chain = Chains[NumRegs-1];
4786 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004787 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004788}
4789
4790/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004791/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004792/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00004793void RegsForValue::AddInlineAsmOperands(unsigned Code,
4794 bool HasMatching,unsigned MatchingIdx,
4795 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004796 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004797 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00004798 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
4799 unsigned Flag = Code | (Regs.size() << 3);
4800 if (HasMatching)
4801 Flag |= 0x80000000 | (MatchingIdx << 16);
4802 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004803 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004804 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004805 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004806 for (unsigned i = 0; i != NumRegs; ++i) {
4807 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004808 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004809 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004810 }
4811}
4812
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004813/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004814/// i.e. it isn't a stack pointer or some other special register, return the
4815/// register class for the register. Otherwise, return null.
4816static const TargetRegisterClass *
4817isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4818 const TargetLowering &TLI,
4819 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004820 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004821 const TargetRegisterClass *FoundRC = 0;
4822 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4823 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004824 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004825
4826 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004827 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4829 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4830 I != E; ++I) {
4831 if (TLI.isTypeLegal(*I)) {
4832 // If we have already found this register in a different register class,
4833 // choose the one with the largest VT specified. For example, on
4834 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004835 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004836 ThisVT = *I;
4837 break;
4838 }
4839 }
4840 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004841
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004843
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004844 // NOTE: This isn't ideal. In particular, this might allocate the
4845 // frame pointer in functions that need it (due to them not being taken
4846 // out of allocation, because a variable sized allocation hasn't been seen
4847 // yet). This is a slight code pessimization, but should still work.
4848 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4849 E = RC->allocation_order_end(MF); I != E; ++I)
4850 if (*I == Reg) {
4851 // We found a matching register class. Keep looking at others in case
4852 // we find one with larger registers that this physreg is also in.
4853 FoundRC = RC;
4854 FoundVT = ThisVT;
4855 break;
4856 }
4857 }
4858 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004859}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004860
4861
4862namespace llvm {
4863/// AsmOperandInfo - This contains information for each constraint that we are
4864/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004865class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004866 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004867public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004868 /// CallOperand - If this is the result output operand or a clobber
4869 /// this is null, otherwise it is the incoming operand to the CallInst.
4870 /// This gets modified as the asm is processed.
4871 SDValue CallOperand;
4872
4873 /// AssignedRegs - If this is a register or register class operand, this
4874 /// contains the set of register corresponding to the operand.
4875 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004877 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4878 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4879 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004880
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004881 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4882 /// busy in OutputRegs/InputRegs.
4883 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004884 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004885 std::set<unsigned> &InputRegs,
4886 const TargetRegisterInfo &TRI) const {
4887 if (isOutReg) {
4888 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4889 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4890 }
4891 if (isInReg) {
4892 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4893 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4894 }
4895 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004896
Owen Andersone50ed302009-08-10 22:56:29 +00004897 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004898 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004899 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00004900 EVT getCallOperandValEVT(LLVMContext &Context,
4901 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004902 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004903 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004904
Chris Lattner81249c92008-10-17 17:05:25 +00004905 if (isa<BasicBlock>(CallOperandVal))
4906 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004907
Chris Lattner81249c92008-10-17 17:05:25 +00004908 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004909
Chris Lattner81249c92008-10-17 17:05:25 +00004910 // If this is an indirect operand, the operand is a pointer to the
4911 // accessed type.
4912 if (isIndirect)
4913 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004914
Chris Lattner81249c92008-10-17 17:05:25 +00004915 // If OpTy is not a single value, it may be a struct/union that we
4916 // can tile with integers.
4917 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4918 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4919 switch (BitSize) {
4920 default: break;
4921 case 1:
4922 case 8:
4923 case 16:
4924 case 32:
4925 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004926 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004927 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004928 break;
4929 }
4930 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004931
Chris Lattner81249c92008-10-17 17:05:25 +00004932 return TLI.getValueType(OpTy, true);
4933 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004934
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935private:
4936 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4937 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004938 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004939 const TargetRegisterInfo &TRI) {
4940 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4941 Regs.insert(Reg);
4942 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4943 for (; *Aliases; ++Aliases)
4944 Regs.insert(*Aliases);
4945 }
4946};
4947} // end llvm namespace.
4948
4949
4950/// GetRegistersForValue - Assign registers (virtual or physical) for the
4951/// specified operand. We prefer to assign virtual registers, to allow the
4952/// register allocator handle the assignment process. However, if the asm uses
4953/// features that we can't model on machineinstrs, we have SDISel do the
4954/// allocation. This produces generally horrible, but correct, code.
4955///
4956/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004957/// Input and OutputRegs are the set of already allocated physical registers.
4958///
4959void SelectionDAGLowering::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004960GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004961 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004962 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004963 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004965 // Compute whether this value requires an input register, an output register,
4966 // or both.
4967 bool isOutReg = false;
4968 bool isInReg = false;
4969 switch (OpInfo.Type) {
4970 case InlineAsm::isOutput:
4971 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004972
4973 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004974 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004975 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004976 break;
4977 case InlineAsm::isInput:
4978 isInReg = true;
4979 isOutReg = false;
4980 break;
4981 case InlineAsm::isClobber:
4982 isOutReg = true;
4983 isInReg = true;
4984 break;
4985 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004986
4987
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004988 MachineFunction &MF = DAG.getMachineFunction();
4989 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004990
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004991 // If this is a constraint for a single physreg, or a constraint for a
4992 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004993 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004994 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4995 OpInfo.ConstraintVT);
4996
4997 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004998 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004999 // If this is a FP input in an integer register (or visa versa) insert a bit
5000 // cast of the input value. More generally, handle any case where the input
5001 // value disagrees with the register class we plan to stick this in.
5002 if (OpInfo.Type == InlineAsm::isInput &&
5003 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005004 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005005 // types are identical size, use a bitcast to convert (e.g. two differing
5006 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005007 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005008 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005009 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005010 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005011 OpInfo.ConstraintVT = RegVT;
5012 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5013 // If the input is a FP value and we want it in FP registers, do a
5014 // bitcast to the corresponding integer type. This turns an f64 value
5015 // into i64, which can be passed with two i32 values on a 32-bit
5016 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005017 RegVT = EVT::getIntegerVT(Context,
5018 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005019 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005020 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005021 OpInfo.ConstraintVT = RegVT;
5022 }
5023 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005024
Owen Anderson23b9b192009-08-12 00:36:31 +00005025 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005026 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005027
Owen Andersone50ed302009-08-10 22:56:29 +00005028 EVT RegVT;
5029 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005030
5031 // If this is a constraint for a specific physical register, like {r17},
5032 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005033 if (unsigned AssignedReg = PhysReg.first) {
5034 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005035 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005036 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005037
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005038 // Get the actual register value type. This is important, because the user
5039 // may have asked for (e.g.) the AX register in i32 type. We need to
5040 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005041 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005042
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005043 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005044 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005045
5046 // If this is an expanded reference, add the rest of the regs to Regs.
5047 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005048 TargetRegisterClass::iterator I = RC->begin();
5049 for (; *I != AssignedReg; ++I)
5050 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005052 // Already added the first reg.
5053 --NumRegs; ++I;
5054 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005055 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005056 Regs.push_back(*I);
5057 }
5058 }
5059 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5060 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5061 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5062 return;
5063 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005064
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005065 // Otherwise, if this was a reference to an LLVM register class, create vregs
5066 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005067 if (const TargetRegisterClass *RC = PhysReg.second) {
5068 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005069 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005070 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005071
Evan Chengfb112882009-03-23 08:01:15 +00005072 // Create the appropriate number of virtual registers.
5073 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5074 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005075 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005076
Evan Chengfb112882009-03-23 08:01:15 +00005077 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5078 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005079 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005080
5081 // This is a reference to a register class that doesn't directly correspond
5082 // to an LLVM register class. Allocate NumRegs consecutive, available,
5083 // registers from the class.
5084 std::vector<unsigned> RegClassRegs
5085 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5086 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005087
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005088 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5089 unsigned NumAllocated = 0;
5090 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5091 unsigned Reg = RegClassRegs[i];
5092 // See if this register is available.
5093 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5094 (isInReg && InputRegs.count(Reg))) { // Already used.
5095 // Make sure we find consecutive registers.
5096 NumAllocated = 0;
5097 continue;
5098 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005099
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005100 // Check to see if this register is allocatable (i.e. don't give out the
5101 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005102 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5103 if (!RC) { // Couldn't allocate this register.
5104 // Reset NumAllocated to make sure we return consecutive registers.
5105 NumAllocated = 0;
5106 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005107 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005109 // Okay, this register is good, we can use it.
5110 ++NumAllocated;
5111
5112 // If we allocated enough consecutive registers, succeed.
5113 if (NumAllocated == NumRegs) {
5114 unsigned RegStart = (i-NumAllocated)+1;
5115 unsigned RegEnd = i+1;
5116 // Mark all of the allocated registers used.
5117 for (unsigned i = RegStart; i != RegEnd; ++i)
5118 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005119
5120 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005121 OpInfo.ConstraintVT);
5122 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5123 return;
5124 }
5125 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005126
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005127 // Otherwise, we couldn't allocate enough registers for this.
5128}
5129
Evan Chengda43bcf2008-09-24 00:05:32 +00005130/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5131/// processed uses a memory 'm' constraint.
5132static bool
5133hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005134 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005135 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5136 InlineAsm::ConstraintInfo &CI = CInfos[i];
5137 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5138 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5139 if (CType == TargetLowering::C_Memory)
5140 return true;
5141 }
Chris Lattner6c147292009-04-30 00:48:50 +00005142
5143 // Indirect operand accesses access memory.
5144 if (CI.isIndirect)
5145 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005146 }
5147
5148 return false;
5149}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005150
5151/// visitInlineAsm - Handle a call to an InlineAsm object.
5152///
5153void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
5154 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5155
5156 /// ConstraintOperands - Information about all of the constraints.
5157 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005158
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005159 std::set<unsigned> OutputRegs, InputRegs;
5160
5161 // Do a prepass over the constraints, canonicalizing them, and building up the
5162 // ConstraintOperands list.
5163 std::vector<InlineAsm::ConstraintInfo>
5164 ConstraintInfos = IA->ParseConstraints();
5165
Evan Chengda43bcf2008-09-24 00:05:32 +00005166 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005167
5168 SDValue Chain, Flag;
5169
5170 // We won't need to flush pending loads if this asm doesn't touch
5171 // memory and is nonvolatile.
5172 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005173 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005174 else
5175 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005176
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005177 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5178 unsigned ResNo = 0; // ResNo - The result number of the next output.
5179 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5180 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5181 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005182
Owen Anderson825b72b2009-08-11 20:47:22 +00005183 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005184
5185 // Compute the value type for each operand.
5186 switch (OpInfo.Type) {
5187 case InlineAsm::isOutput:
5188 // Indirect outputs just consume an argument.
5189 if (OpInfo.isIndirect) {
5190 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5191 break;
5192 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005193
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005194 // The return value of the call is this value. As such, there is no
5195 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005196 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5197 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005198 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5199 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5200 } else {
5201 assert(ResNo == 0 && "Asm only has one result!");
5202 OpVT = TLI.getValueType(CS.getType());
5203 }
5204 ++ResNo;
5205 break;
5206 case InlineAsm::isInput:
5207 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5208 break;
5209 case InlineAsm::isClobber:
5210 // Nothing to do.
5211 break;
5212 }
5213
5214 // If this is an input or an indirect output, process the call argument.
5215 // BasicBlocks are labels, currently appearing only in asm's.
5216 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005217 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005218 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5219
Chris Lattner81249c92008-10-17 17:05:25 +00005220 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005221 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005222 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005223 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005224 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005225
Owen Anderson1d0be152009-08-13 21:58:54 +00005226 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005227 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005228
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005229 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005230 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005231
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005232 // Second pass over the constraints: compute which constraint option to use
5233 // and assign registers to constraints that want a specific physreg.
5234 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5235 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005236
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005237 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005238 // matching input. If their types mismatch, e.g. one is an integer, the
5239 // other is floating point, or their sizes are different, flag it as an
5240 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005241 if (OpInfo.hasMatchingInput()) {
5242 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5243 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005244 if ((OpInfo.ConstraintVT.isInteger() !=
5245 Input.ConstraintVT.isInteger()) ||
5246 (OpInfo.ConstraintVT.getSizeInBits() !=
5247 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005248 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005249 " with a matching output constraint of incompatible"
5250 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005251 }
5252 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005253 }
5254 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005255
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005256 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005257 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005259 // If this is a memory input, and if the operand is not indirect, do what we
5260 // need to to provide an address for the memory input.
5261 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5262 !OpInfo.isIndirect) {
5263 assert(OpInfo.Type == InlineAsm::isInput &&
5264 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005266 // Memory operands really want the address of the value. If we don't have
5267 // an indirect input, put it in the constpool if we can, otherwise spill
5268 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005270 // If the operand is a float, integer, or vector constant, spill to a
5271 // constant pool entry to get its address.
5272 Value *OpVal = OpInfo.CallOperandVal;
5273 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5274 isa<ConstantVector>(OpVal)) {
5275 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5276 TLI.getPointerTy());
5277 } else {
5278 // Otherwise, create a stack slot and emit a store to it before the
5279 // asm.
5280 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005281 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005282 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5283 MachineFunction &MF = DAG.getMachineFunction();
5284 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
5285 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005286 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005287 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005288 OpInfo.CallOperand = StackSlot;
5289 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005291 // There is no longer a Value* corresponding to this operand.
5292 OpInfo.CallOperandVal = 0;
5293 // It is now an indirect operand.
5294 OpInfo.isIndirect = true;
5295 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005297 // If this constraint is for a specific register, allocate it before
5298 // anything else.
5299 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005300 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005301 }
5302 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005303
5304
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005306 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005307 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5308 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005309
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005310 // C_Register operands have already been allocated, Other/Memory don't need
5311 // to be.
5312 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005313 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005314 }
5315
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005316 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5317 std::vector<SDValue> AsmNodeOperands;
5318 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5319 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005320 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005321
5322
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005323 // Loop over all of the inputs, copying the operand values into the
5324 // appropriate registers and processing the output regs.
5325 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005326
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005327 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5328 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005330 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5331 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5332
5333 switch (OpInfo.Type) {
5334 case InlineAsm::isOutput: {
5335 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5336 OpInfo.ConstraintType != TargetLowering::C_Register) {
5337 // Memory output, or 'other' output (e.g. 'X' constraint).
5338 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5339
5340 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005341 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5342 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005343 TLI.getPointerTy()));
5344 AsmNodeOperands.push_back(OpInfo.CallOperand);
5345 break;
5346 }
5347
5348 // Otherwise, this is a register or register class output.
5349
5350 // Copy the output from the appropriate register. Find a register that
5351 // we can use.
5352 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005353 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005354 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005355 }
5356
5357 // If this is an indirect operand, store through the pointer after the
5358 // asm.
5359 if (OpInfo.isIndirect) {
5360 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5361 OpInfo.CallOperandVal));
5362 } else {
5363 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005364 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5365 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005366 // Concatenate this output onto the outputs list.
5367 RetValRegs.append(OpInfo.AssignedRegs);
5368 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005369
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005370 // Add information to the INLINEASM node to know that this register is
5371 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005372 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5373 6 /* EARLYCLOBBER REGDEF */ :
5374 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005375 false,
5376 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005377 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005378 break;
5379 }
5380 case InlineAsm::isInput: {
5381 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005382
Chris Lattner6bdcda32008-10-17 16:47:46 +00005383 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005384 // If this is required to match an output register we have already set,
5385 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005386 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005387
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005388 // Scan until we find the definition we already emitted of this operand.
5389 // When we find it, create a RegsForValue operand.
5390 unsigned CurOp = 2; // The first operand.
5391 for (; OperandNo; --OperandNo) {
5392 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005393 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005394 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005395 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5396 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5397 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005398 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005399 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400 }
5401
Evan Cheng697cbbf2009-03-20 18:03:34 +00005402 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005403 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005404 if ((OpFlag & 7) == 2 /*REGDEF*/
5405 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5406 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005407 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005408 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005409 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005410 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005411 RegsForValue MatchedRegs;
5412 MatchedRegs.TLI = &TLI;
5413 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005414 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005415 MatchedRegs.RegVTs.push_back(RegVT);
5416 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005417 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005418 i != e; ++i)
5419 MatchedRegs.Regs.
5420 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005421
5422 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005423 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5424 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005425 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5426 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005427 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005428 break;
5429 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005430 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5431 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5432 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005433 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005434 // See InlineAsm.h isUseOperandTiedToDef.
5435 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005436 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005437 TLI.getPointerTy()));
5438 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5439 break;
5440 }
5441 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005442
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005443 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005444 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005445 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005447 std::vector<SDValue> Ops;
5448 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005449 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005450 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005451 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005452 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005453 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005454
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005455 // Add information to the INLINEASM node to know about this input.
5456 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005457 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005458 TLI.getPointerTy()));
5459 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5460 break;
5461 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5462 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5463 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5464 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005465
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005466 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005467 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5468 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005469 TLI.getPointerTy()));
5470 AsmNodeOperands.push_back(InOperandVal);
5471 break;
5472 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5475 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5476 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005477 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005478 "Don't know how to handle indirect register inputs yet!");
5479
5480 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005481 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005482 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005483 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005484 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005485
Dale Johannesen66978ee2009-01-31 02:22:37 +00005486 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5487 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005488
Evan Cheng697cbbf2009-03-20 18:03:34 +00005489 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005490 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005491 break;
5492 }
5493 case InlineAsm::isClobber: {
5494 // Add the clobbered value to the operand list, so that the register
5495 // allocator is aware that the physreg got clobbered.
5496 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005497 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005498 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005499 break;
5500 }
5501 }
5502 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005503
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005504 // Finish up input operands.
5505 AsmNodeOperands[0] = Chain;
5506 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005507
Dale Johannesen66978ee2009-01-31 02:22:37 +00005508 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005509 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005510 &AsmNodeOperands[0], AsmNodeOperands.size());
5511 Flag = Chain.getValue(1);
5512
5513 // If this asm returns a register value, copy the result from that register
5514 // and set it as the value of the call.
5515 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005516 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005517 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005518
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005519 // FIXME: Why don't we do this for inline asms with MRVs?
5520 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005521 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005522
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005523 // If any of the results of the inline asm is a vector, it may have the
5524 // wrong width/num elts. This can happen for register classes that can
5525 // contain multiple different value types. The preg or vreg allocated may
5526 // not have the same VT as was expected. Convert it to the right type
5527 // with bit_convert.
5528 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005529 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005530 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005531
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005532 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005533 ResultType.isInteger() && Val.getValueType().isInteger()) {
5534 // If a result value was tied to an input value, the computed result may
5535 // have a wider width than the expected result. Extract the relevant
5536 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005537 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005538 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005539
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005540 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005541 }
Dan Gohman95915732008-10-18 01:03:45 +00005542
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005543 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005544 // Don't need to use this as a chain in this case.
5545 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5546 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005547 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005548
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005549 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005550
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005551 // Process indirect outputs, first output all of the flagged copies out of
5552 // physregs.
5553 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5554 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5555 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005556 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5557 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005558 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005559
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005560 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005561
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005562 // Emit the non-flagged stores from the physregs.
5563 SmallVector<SDValue, 8> OutChains;
5564 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005565 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005566 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005567 getValue(StoresToEmit[i].second),
5568 StoresToEmit[i].second, 0));
5569 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005570 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005571 &OutChains[0], OutChains.size());
5572 DAG.setRoot(Chain);
5573}
5574
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005575void SelectionDAGLowering::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005576 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005577 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005578 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005579 DAG.getSrcValue(I.getOperand(1))));
5580}
5581
5582void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005583 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5584 getRoot(), getValue(I.getOperand(0)),
5585 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005586 setValue(&I, V);
5587 DAG.setRoot(V.getValue(1));
5588}
5589
5590void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005591 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005592 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005593 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005594 DAG.getSrcValue(I.getOperand(1))));
5595}
5596
5597void SelectionDAGLowering::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005598 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005599 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005600 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005601 getValue(I.getOperand(2)),
5602 DAG.getSrcValue(I.getOperand(1)),
5603 DAG.getSrcValue(I.getOperand(2))));
5604}
5605
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005606/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005607/// implementation, which just calls LowerCall.
5608/// FIXME: When all targets are
5609/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005610std::pair<SDValue, SDValue>
5611TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5612 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005613 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005614 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005615 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005616 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005617 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005618
Dan Gohman1937e2f2008-09-16 01:42:28 +00005619 assert((!isTailCall || PerformTailCallOpt) &&
5620 "isTailCall set when tail-call optimizations are disabled!");
5621
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005622 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005623 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005624 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005625 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5627 for (unsigned Value = 0, NumValues = ValueVTs.size();
5628 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005629 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005630 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005631 SDValue Op = SDValue(Args[i].Node.getNode(),
5632 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005633 ISD::ArgFlagsTy Flags;
5634 unsigned OriginalAlignment =
5635 getTargetData()->getABITypeAlignment(ArgTy);
5636
5637 if (Args[i].isZExt)
5638 Flags.setZExt();
5639 if (Args[i].isSExt)
5640 Flags.setSExt();
5641 if (Args[i].isInReg)
5642 Flags.setInReg();
5643 if (Args[i].isSRet)
5644 Flags.setSRet();
5645 if (Args[i].isByVal) {
5646 Flags.setByVal();
5647 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5648 const Type *ElementTy = Ty->getElementType();
5649 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005650 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005651 // For ByVal, alignment should come from FE. BE will guess if this
5652 // info is not there but there are cases it cannot get right.
5653 if (Args[i].Alignment)
5654 FrameAlign = Args[i].Alignment;
5655 Flags.setByValAlign(FrameAlign);
5656 Flags.setByValSize(FrameSize);
5657 }
5658 if (Args[i].isNest)
5659 Flags.setNest();
5660 Flags.setOrigAlign(OriginalAlignment);
5661
Owen Anderson23b9b192009-08-12 00:36:31 +00005662 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5663 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005664 SmallVector<SDValue, 4> Parts(NumParts);
5665 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5666
5667 if (Args[i].isSExt)
5668 ExtendKind = ISD::SIGN_EXTEND;
5669 else if (Args[i].isZExt)
5670 ExtendKind = ISD::ZERO_EXTEND;
5671
Dale Johannesen66978ee2009-01-31 02:22:37 +00005672 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005673
Dan Gohman98ca4f22009-08-05 01:29:28 +00005674 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005675 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005676 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5677 if (NumParts > 1 && j == 0)
5678 MyFlags.Flags.setSplit();
5679 else if (j != 0)
5680 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005681
Dan Gohman98ca4f22009-08-05 01:29:28 +00005682 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005683 }
5684 }
5685 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005686
Dan Gohman98ca4f22009-08-05 01:29:28 +00005687 // Handle the incoming return values from the call.
5688 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005689 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005690 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005691 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005692 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005693 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5694 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005695 for (unsigned i = 0; i != NumRegs; ++i) {
5696 ISD::InputArg MyFlags;
5697 MyFlags.VT = RegisterVT;
5698 MyFlags.Used = isReturnValueUsed;
5699 if (RetSExt)
5700 MyFlags.Flags.setSExt();
5701 if (RetZExt)
5702 MyFlags.Flags.setZExt();
5703 if (isInreg)
5704 MyFlags.Flags.setInReg();
5705 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005706 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005707 }
5708
Dan Gohman98ca4f22009-08-05 01:29:28 +00005709 // Check if target-dependent constraints permit a tail call here.
5710 // Target-independent constraints should be checked by the caller.
5711 if (isTailCall &&
5712 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
5713 isTailCall = false;
5714
5715 SmallVector<SDValue, 4> InVals;
5716 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
5717 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005718
5719 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005720 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005721 "LowerCall didn't return a valid chain!");
5722 assert((!isTailCall || InVals.empty()) &&
5723 "LowerCall emitted a return value for a tail call!");
5724 assert((isTailCall || InVals.size() == Ins.size()) &&
5725 "LowerCall didn't emit the correct number of values!");
5726 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5727 assert(InVals[i].getNode() &&
5728 "LowerCall emitted a null value!");
5729 assert(Ins[i].VT == InVals[i].getValueType() &&
5730 "LowerCall emitted a value with the wrong type!");
5731 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00005732
5733 // For a tail call, the return value is merely live-out and there aren't
5734 // any nodes in the DAG representing it. Return a special value to
5735 // indicate that a tail call has been emitted and no more Instructions
5736 // should be processed in the current block.
5737 if (isTailCall) {
5738 DAG.setRoot(Chain);
5739 return std::make_pair(SDValue(), SDValue());
5740 }
5741
5742 // Collect the legal value parts into potentially illegal values
5743 // that correspond to the original function's return values.
5744 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5745 if (RetSExt)
5746 AssertOp = ISD::AssertSext;
5747 else if (RetZExt)
5748 AssertOp = ISD::AssertZext;
5749 SmallVector<SDValue, 4> ReturnValues;
5750 unsigned CurReg = 0;
5751 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005752 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005753 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5754 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005755
5756 SDValue ReturnValue =
5757 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
5758 AssertOp);
5759 ReturnValues.push_back(ReturnValue);
5760 CurReg += NumRegs;
5761 }
5762
5763 // For a function returning void, there is no return value. We can't create
5764 // such a node, so we just return a null return value in that case. In
5765 // that case, nothing will actualy look at the value.
5766 if (ReturnValues.empty())
5767 return std::make_pair(SDValue(), Chain);
5768
5769 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5770 DAG.getVTList(&RetTys[0], RetTys.size()),
5771 &ReturnValues[0], ReturnValues.size());
5772
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005773 return std::make_pair(Res, Chain);
5774}
5775
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005776void TargetLowering::LowerOperationWrapper(SDNode *N,
5777 SmallVectorImpl<SDValue> &Results,
5778 SelectionDAG &DAG) {
5779 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005780 if (Res.getNode())
5781 Results.push_back(Res);
5782}
5783
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005784SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005785 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005786 return SDValue();
5787}
5788
5789
5790void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
5791 SDValue Op = getValue(V);
5792 assert((Op.getOpcode() != ISD::CopyFromReg ||
5793 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5794 "Copy from a reg to the same reg!");
5795 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5796
Owen Anderson23b9b192009-08-12 00:36:31 +00005797 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005798 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005799 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005800 PendingExports.push_back(Chain);
5801}
5802
5803#include "llvm/CodeGen/SelectionDAGISel.h"
5804
Dan Gohman8c2b5252009-10-30 01:27:03 +00005805void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005806 // If this is the entry block, emit arguments.
5807 Function &F = *LLVMBB->getParent();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005808 SelectionDAG &DAG = SDL->DAG;
5809 SDValue OldRoot = DAG.getRoot();
5810 DebugLoc dl = SDL->getCurDebugLoc();
5811 const TargetData *TD = TLI.getTargetData();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005812
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005813 // Check whether the function can return without sret-demotion.
5814 SmallVector<EVT, 4> OutVTs;
5815 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
5816 getReturnInfo(&F, OutVTs, OutsFlags, TLI);
5817 // For now, assert and bail out if it can't.
5818 assert(TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), OutVTs, OutsFlags,
5819 DAG) && "Cannot fit return value in registers!");
5820
Dan Gohman98ca4f22009-08-05 01:29:28 +00005821 // Set up the incoming argument description vector.
5822 SmallVector<ISD::InputArg, 16> Ins;
5823 unsigned Idx = 1;
5824 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5825 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005826 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005827 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5828 bool isArgValueUsed = !I->use_empty();
5829 for (unsigned Value = 0, NumValues = ValueVTs.size();
5830 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005831 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005832 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005833 ISD::ArgFlagsTy Flags;
5834 unsigned OriginalAlignment =
5835 TD->getABITypeAlignment(ArgTy);
5836
5837 if (F.paramHasAttr(Idx, Attribute::ZExt))
5838 Flags.setZExt();
5839 if (F.paramHasAttr(Idx, Attribute::SExt))
5840 Flags.setSExt();
5841 if (F.paramHasAttr(Idx, Attribute::InReg))
5842 Flags.setInReg();
5843 if (F.paramHasAttr(Idx, Attribute::StructRet))
5844 Flags.setSRet();
5845 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5846 Flags.setByVal();
5847 const PointerType *Ty = cast<PointerType>(I->getType());
5848 const Type *ElementTy = Ty->getElementType();
5849 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5850 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5851 // For ByVal, alignment should be passed from FE. BE will guess if
5852 // this info is not there but there are cases it cannot get right.
5853 if (F.getParamAlignment(Idx))
5854 FrameAlign = F.getParamAlignment(Idx);
5855 Flags.setByValAlign(FrameAlign);
5856 Flags.setByValSize(FrameSize);
5857 }
5858 if (F.paramHasAttr(Idx, Attribute::Nest))
5859 Flags.setNest();
5860 Flags.setOrigAlign(OriginalAlignment);
5861
Owen Anderson23b9b192009-08-12 00:36:31 +00005862 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5863 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005864 for (unsigned i = 0; i != NumRegs; ++i) {
5865 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5866 if (NumRegs > 1 && i == 0)
5867 MyFlags.Flags.setSplit();
5868 // if it isn't first piece, alignment must be 1
5869 else if (i > 0)
5870 MyFlags.Flags.setOrigAlign(1);
5871 Ins.push_back(MyFlags);
5872 }
5873 }
5874 }
5875
5876 // Call the target to set up the argument values.
5877 SmallVector<SDValue, 8> InVals;
5878 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5879 F.isVarArg(), Ins,
5880 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005881
5882 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005883 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005884 "LowerFormalArguments didn't return a valid chain!");
5885 assert(InVals.size() == Ins.size() &&
5886 "LowerFormalArguments didn't emit the correct number of values!");
5887 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5888 assert(InVals[i].getNode() &&
5889 "LowerFormalArguments emitted a null value!");
5890 assert(Ins[i].VT == InVals[i].getValueType() &&
5891 "LowerFormalArguments emitted a value with the wrong type!");
5892 });
5893
5894 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005895 DAG.setRoot(NewRoot);
5896
5897 // Set up the argument values.
5898 unsigned i = 0;
5899 Idx = 1;
5900 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5901 ++I, ++Idx) {
5902 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005903 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005904 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005905 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005906 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005907 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005908 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5909 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005910
5911 if (!I->use_empty()) {
5912 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5913 if (F.paramHasAttr(Idx, Attribute::SExt))
5914 AssertOp = ISD::AssertSext;
5915 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5916 AssertOp = ISD::AssertZext;
5917
5918 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
5919 PartVT, VT, AssertOp));
5920 }
5921 i += NumParts;
5922 }
5923 if (!I->use_empty()) {
5924 SDL->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
5925 SDL->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005926 // If this argument is live outside of the entry block, insert a copy from
5927 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005928 SDL->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005929 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005930 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005931 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005932
5933 // Finally, if the target has anything special to do, allow it to do so.
5934 // FIXME: this should insert code into the DAG!
5935 EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction());
5936}
5937
5938/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5939/// ensure constants are generated when needed. Remember the virtual registers
5940/// that need to be added to the Machine PHI nodes as input. We cannot just
5941/// directly add them, because expansion might result in multiple MBB's for one
5942/// BB. As such, the start of the BB might correspond to a different MBB than
5943/// the end.
5944///
5945void
5946SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5947 TerminatorInst *TI = LLVMBB->getTerminator();
5948
5949 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5950
5951 // Check successor nodes' PHI nodes that expect a constant to be available
5952 // from this block.
5953 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5954 BasicBlock *SuccBB = TI->getSuccessor(succ);
5955 if (!isa<PHINode>(SuccBB->begin())) continue;
5956 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005957
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005958 // If this terminator has multiple identical successors (common for
5959 // switches), only handle each succ once.
5960 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005961
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005962 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5963 PHINode *PN;
5964
5965 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5966 // nodes and Machine PHI nodes, but the incoming operands have not been
5967 // emitted yet.
5968 for (BasicBlock::iterator I = SuccBB->begin();
5969 (PN = dyn_cast<PHINode>(I)); ++I) {
5970 // Ignore dead phi's.
5971 if (PN->use_empty()) continue;
5972
5973 unsigned Reg;
5974 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5975
5976 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
5977 unsigned &RegOut = SDL->ConstantsOut[C];
5978 if (RegOut == 0) {
5979 RegOut = FuncInfo->CreateRegForValue(C);
5980 SDL->CopyValueToVirtualRegister(C, RegOut);
5981 }
5982 Reg = RegOut;
5983 } else {
5984 Reg = FuncInfo->ValueMap[PHIOp];
5985 if (Reg == 0) {
5986 assert(isa<AllocaInst>(PHIOp) &&
5987 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5988 "Didn't codegen value into a register!??");
5989 Reg = FuncInfo->CreateRegForValue(PHIOp);
5990 SDL->CopyValueToVirtualRegister(PHIOp, Reg);
5991 }
5992 }
5993
5994 // Remember that this register needs to added to the machine PHI node as
5995 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00005996 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005997 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5998 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00005999 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006000 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006001 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
6002 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
6003 Reg += NumRegisters;
6004 }
6005 }
6006 }
6007 SDL->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006008}
6009
Dan Gohman3df24e62008-09-03 23:12:08 +00006010/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6011/// supports legal types, and it emits MachineInstrs directly instead of
6012/// creating SelectionDAG nodes.
6013///
6014bool
6015SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6016 FastISel *F) {
6017 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006018
Dan Gohman3df24e62008-09-03 23:12:08 +00006019 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6020 unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size();
6021
6022 // Check successor nodes' PHI nodes that expect a constant to be available
6023 // from this block.
6024 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6025 BasicBlock *SuccBB = TI->getSuccessor(succ);
6026 if (!isa<PHINode>(SuccBB->begin())) continue;
6027 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006028
Dan Gohman3df24e62008-09-03 23:12:08 +00006029 // If this terminator has multiple identical successors (common for
6030 // switches), only handle each succ once.
6031 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006032
Dan Gohman3df24e62008-09-03 23:12:08 +00006033 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6034 PHINode *PN;
6035
6036 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6037 // nodes and Machine PHI nodes, but the incoming operands have not been
6038 // emitted yet.
6039 for (BasicBlock::iterator I = SuccBB->begin();
6040 (PN = dyn_cast<PHINode>(I)); ++I) {
6041 // Ignore dead phi's.
6042 if (PN->use_empty()) continue;
6043
6044 // Only handle legal types. Two interesting things to note here. First,
6045 // by bailing out early, we may leave behind some dead instructions,
6046 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6047 // own moves. Second, this check is necessary becuase FastISel doesn't
6048 // use CreateRegForValue to create registers, so it always creates
6049 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006050 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006051 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6052 // Promote MVT::i1.
6053 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006054 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006055 else {
6056 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
6057 return false;
6058 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006059 }
6060
6061 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6062
6063 unsigned Reg = F->getRegForValue(PHIOp);
6064 if (Reg == 0) {
6065 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
6066 return false;
6067 }
6068 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
6069 }
6070 }
6071
6072 return true;
6073}