blob: ee8bf791715e96ff1172f97cd225d5222538762e [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
Chris Lattnere880efe2009-11-07 07:50:34 +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
Chris Lattnere880efe2009-11-07 07:50:34 +00001749 APInt TSize(First.getBitWidth(), 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
Chris Lattnere880efe2009-11-07 07:50:34 +00001754 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001755 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001756
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001757 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001758 double Density = TSize.roundToDouble() / 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
Chris Lattnere880efe2009-11-07 07:50:34 +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.
Chris Lattnere880efe2009-11-07 07:50:34 +00001859 APInt TSize(First.getBitWidth(), 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
Chris Lattnere880efe2009-11-07 07:50:34 +00001864 APInt LSize = FrontCase.size();
1865 APInt 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) {
Chris Lattnere880efe2009-11-07 07:50:34 +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");
Chris Lattnere880efe2009-11-07 07:50:34 +00001876 double LDensity = (double)LSize.roundToDouble() /
1877 (LEnd - First + 1ULL).roundToDouble();
1878 double RDensity = (double)RSize.roundToDouble() /
1879 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001880 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001881 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001882 DEBUG(errs() <<"=>Step\n"
1883 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1884 << "LDensity: " << LDensity
1885 << ", RDensity: " << RDensity << '\n'
1886 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001887 if (FMetric < Metric) {
1888 Pivot = J;
1889 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001890 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001891 }
1892
1893 LSize += J->size();
1894 RSize -= J->size();
1895 }
1896 if (areJTsAllowed(TLI)) {
1897 // If our case is dense we *really* should handle it earlier!
1898 assert((FMetric > 0) && "Should handle dense range earlier!");
1899 } else {
1900 Pivot = CR.Range.first + Size/2;
1901 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001902
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903 CaseRange LHSR(CR.Range.first, Pivot);
1904 CaseRange RHSR(Pivot, CR.Range.second);
1905 Constant *C = Pivot->Low;
1906 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001907
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001908 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001909 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001910 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001911 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001912 // Pivot's Value, then we can branch directly to the LHS's Target,
1913 // rather than creating a leaf node for it.
1914 if ((LHSR.second - LHSR.first) == 1 &&
1915 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001916 cast<ConstantInt>(C)->getValue() ==
1917 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001918 TrueBB = LHSR.first->BB;
1919 } else {
1920 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1921 CurMF->insert(BBI, TrueBB);
1922 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001923
1924 // Put SV in a virtual register to make it available from the new blocks.
1925 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001926 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001927
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001928 // Similar to the optimization above, if the Value being switched on is
1929 // known to be less than the Constant CR.LT, and the current Case Value
1930 // is CR.LT - 1, then we can branch directly to the target block for
1931 // the current Case Value, rather than emitting a RHS leaf node for it.
1932 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001933 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1934 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001935 FalseBB = RHSR.first->BB;
1936 } else {
1937 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1938 CurMF->insert(BBI, FalseBB);
1939 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001940
1941 // Put SV in a virtual register to make it available from the new blocks.
1942 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001943 }
1944
1945 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001946 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001947 // Otherwise, branch to LHS.
1948 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1949
1950 if (CR.CaseBB == CurMBB)
1951 visitSwitchCase(CB);
1952 else
1953 SwitchCases.push_back(CB);
1954
1955 return true;
1956}
1957
1958/// handleBitTestsSwitchCase - if current case range has few destination and
1959/// range span less, than machine word bitwidth, encode case range into series
1960/// of masks and emit bit tests with these masks.
1961bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1962 CaseRecVector& WorkList,
1963 Value* SV,
1964 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001965 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001966 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001967
1968 Case& FrontCase = *CR.Range.first;
1969 Case& BackCase = *(CR.Range.second-1);
1970
1971 // Get the MachineFunction which holds the current MBB. This is used when
1972 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001973 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001974
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001975 // If target does not have legal shift left, do not emit bit tests at all.
1976 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1977 return false;
1978
Anton Korobeynikov23218582008-12-23 22:25:27 +00001979 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001980 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1981 I!=E; ++I) {
1982 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001983 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001984 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001985
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001986 // Count unique destinations
1987 SmallSet<MachineBasicBlock*, 4> Dests;
1988 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1989 Dests.insert(I->BB);
1990 if (Dests.size() > 3)
1991 // Don't bother the code below, if there are too much unique destinations
1992 return false;
1993 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001994 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1995 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001996
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001997 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001998 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1999 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002000 APInt cmpRange = maxValue - minValue;
2001
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002002 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
2003 << "Low bound: " << minValue << '\n'
2004 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002005
2006 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002007 (!(Dests.size() == 1 && numCmps >= 3) &&
2008 !(Dests.size() == 2 && numCmps >= 5) &&
2009 !(Dests.size() >= 3 && numCmps >= 6)))
2010 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002011
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002012 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002013 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002015 // Optimize the case where all the case values fit in a
2016 // word without having to subtract minValue. In this case,
2017 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002018 if (minValue.isNonNegative() &&
2019 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
2020 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002022 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002023 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002024
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 CaseBitsVector CasesBits;
2026 unsigned i, count = 0;
2027
2028 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2029 MachineBasicBlock* Dest = I->BB;
2030 for (i = 0; i < count; ++i)
2031 if (Dest == CasesBits[i].BB)
2032 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002033
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 if (i == count) {
2035 assert((count < 3) && "Too much destinations to test!");
2036 CasesBits.push_back(CaseBits(0, Dest, 0));
2037 count++;
2038 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002039
2040 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2041 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2042
2043 uint64_t lo = (lowValue - lowBound).getZExtValue();
2044 uint64_t hi = (highValue - lowBound).getZExtValue();
2045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002046 for (uint64_t j = lo; j <= hi; j++) {
2047 CasesBits[i].Mask |= 1ULL << j;
2048 CasesBits[i].Bits++;
2049 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002051 }
2052 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002054 BitTestInfo BTC;
2055
2056 // Figure out which block is immediately after the current one.
2057 MachineFunction::iterator BBI = CR.CaseBB;
2058 ++BBI;
2059
2060 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2061
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002062 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002063 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002064 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
2065 << ", Bits: " << CasesBits[i].Bits
2066 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002067
2068 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2069 CurMF->insert(BBI, CaseBB);
2070 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2071 CaseBB,
2072 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002073
2074 // Put SV in a virtual register to make it available from the new blocks.
2075 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002076 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002077
2078 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002079 -1U, (CR.CaseBB == CurMBB),
2080 CR.CaseBB, Default, BTC);
2081
2082 if (CR.CaseBB == CurMBB)
2083 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002084
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002085 BitTestCases.push_back(BTB);
2086
2087 return true;
2088}
2089
2090
2091/// Clusterify - Transform simple list of Cases into list of CaseRange's
Anton Korobeynikov23218582008-12-23 22:25:27 +00002092size_t SelectionDAGLowering::Clusterify(CaseVector& Cases,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002094 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002095
2096 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002097 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002098 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2099 Cases.push_back(Case(SI.getSuccessorValue(i),
2100 SI.getSuccessorValue(i),
2101 SMBB));
2102 }
2103 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2104
2105 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002106 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002107 // Must recompute end() each iteration because it may be
2108 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002109 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2110 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2111 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002112 MachineBasicBlock* nextBB = J->BB;
2113 MachineBasicBlock* currentBB = I->BB;
2114
2115 // If the two neighboring cases go to the same destination, merge them
2116 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002117 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002118 I->High = J->High;
2119 J = Cases.erase(J);
2120 } else {
2121 I = J++;
2122 }
2123 }
2124
2125 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2126 if (I->Low != I->High)
2127 // A range counts double, since it requires two compares.
2128 ++numCmps;
2129 }
2130
2131 return numCmps;
2132}
2133
Anton Korobeynikov23218582008-12-23 22:25:27 +00002134void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002135 // Figure out which block is immediately after the current one.
2136 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002137
2138 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2139
2140 // If there is only the default destination, branch to it if it is not the
2141 // next basic block. Otherwise, just fall through.
2142 if (SI.getNumOperands() == 2) {
2143 // Update machine-CFG edges.
2144
2145 // If this is not a fall-through branch, emit the branch.
2146 CurMBB->addSuccessor(Default);
2147 if (Default != NextBlock)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002148 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002149 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002150 DAG.getBasicBlock(Default)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151 return;
2152 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002154 // If there are any non-default case statements, create a vector of Cases
2155 // representing each one, and sort the vector so that we can efficiently
2156 // create a binary search tree from them.
2157 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002158 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002159 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2160 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002161 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002162
2163 // Get the Value to be switched on and default basic blocks, which will be
2164 // inserted into CaseBlock records, representing basic blocks in the binary
2165 // search tree.
2166 Value *SV = SI.getOperand(0);
2167
2168 // Push the initial CaseRec onto the worklist
2169 CaseRecVector WorkList;
2170 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2171
2172 while (!WorkList.empty()) {
2173 // Grab a record representing a case range to process off the worklist
2174 CaseRec CR = WorkList.back();
2175 WorkList.pop_back();
2176
2177 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2178 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002180 // If the range has few cases (two or less) emit a series of specific
2181 // tests.
2182 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2183 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002184
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002185 // If the switch has more than 5 blocks, and at least 40% dense, and the
2186 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187 // lowering the switch to a binary tree of conditional branches.
2188 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2189 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002190
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002191 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2192 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2193 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2194 }
2195}
2196
Chris Lattnerab21db72009-10-28 00:19:10 +00002197void SelectionDAGLowering::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002198 // Update machine-CFG edges.
2199 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2200 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2201
Dan Gohman64825152009-10-27 21:56:26 +00002202 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2203 MVT::Other, getControlRoot(),
2204 getValue(I.getAddress())));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002205}
2206
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002208void SelectionDAGLowering::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002209 // -0.0 - X --> fneg
2210 const Type *Ty = I.getType();
2211 if (isa<VectorType>(Ty)) {
2212 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2213 const VectorType *DestTy = cast<VectorType>(I.getType());
2214 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002215 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002216 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002217 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002218 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002219 SDValue Op2 = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002220 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002221 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222 return;
2223 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002224 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002226 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002227 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002228 SDValue Op2 = getValue(I.getOperand(1));
2229 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2230 Op2.getValueType(), Op2));
2231 return;
2232 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002233
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002234 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235}
2236
2237void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
2238 SDValue Op1 = getValue(I.getOperand(0));
2239 SDValue Op2 = getValue(I.getOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002240
Scott Michelfdc40a02009-02-17 22:15:04 +00002241 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002242 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002243}
2244
2245void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2246 SDValue Op1 = getValue(I.getOperand(0));
2247 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002248 if (!isa<VectorType>(I.getType()) &&
2249 Op2.getValueType() != TLI.getShiftAmountTy()) {
2250 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002251 EVT PTy = TLI.getPointerTy();
2252 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002253 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002254 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2255 TLI.getShiftAmountTy(), Op2);
2256 // If the operand is larger than the shift count type but the shift
2257 // count type has enough bits to represent any shift value, truncate
2258 // it now. This is a common case and it exposes the truncate to
2259 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002260 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002261 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2262 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2263 TLI.getShiftAmountTy(), Op2);
2264 // Otherwise we'll need to temporarily settle for some other
2265 // convenient type; type legalization will make adjustments as
2266 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002267 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002268 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002269 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002270 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002271 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002272 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002274
Scott Michelfdc40a02009-02-17 22:15:04 +00002275 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002276 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002277}
2278
2279void SelectionDAGLowering::visitICmp(User &I) {
2280 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2281 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2282 predicate = IC->getPredicate();
2283 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2284 predicate = ICmpInst::Predicate(IC->getPredicate());
2285 SDValue Op1 = getValue(I.getOperand(0));
2286 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002287 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002288
Owen Andersone50ed302009-08-10 22:56:29 +00002289 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002290 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002291}
2292
2293void SelectionDAGLowering::visitFCmp(User &I) {
2294 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2295 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2296 predicate = FC->getPredicate();
2297 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2298 predicate = FCmpInst::Predicate(FC->getPredicate());
2299 SDValue Op1 = getValue(I.getOperand(0));
2300 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002301 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002302 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002303 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002304}
2305
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002306void SelectionDAGLowering::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002307 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002308 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2309 unsigned NumValues = ValueVTs.size();
2310 if (NumValues != 0) {
2311 SmallVector<SDValue, 4> Values(NumValues);
2312 SDValue Cond = getValue(I.getOperand(0));
2313 SDValue TrueVal = getValue(I.getOperand(1));
2314 SDValue FalseVal = getValue(I.getOperand(2));
2315
2316 for (unsigned i = 0; i != NumValues; ++i)
Scott Michelfdc40a02009-02-17 22:15:04 +00002317 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002318 TrueVal.getValueType(), Cond,
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002319 SDValue(TrueVal.getNode(), TrueVal.getResNo() + i),
2320 SDValue(FalseVal.getNode(), FalseVal.getResNo() + i));
2321
Scott Michelfdc40a02009-02-17 22:15:04 +00002322 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002323 DAG.getVTList(&ValueVTs[0], NumValues),
2324 &Values[0], NumValues));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002325 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002326}
2327
2328
2329void SelectionDAGLowering::visitTrunc(User &I) {
2330 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2331 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002332 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002333 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002334}
2335
2336void SelectionDAGLowering::visitZExt(User &I) {
2337 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2338 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2339 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002340 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002341 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002342}
2343
2344void SelectionDAGLowering::visitSExt(User &I) {
2345 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2346 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2347 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002348 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002349 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002350}
2351
2352void SelectionDAGLowering::visitFPTrunc(User &I) {
2353 // FPTrunc is never a no-op cast, no need to check
2354 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002355 EVT DestVT = TLI.getValueType(I.getType());
Scott Michelfdc40a02009-02-17 22:15:04 +00002356 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002357 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002358}
2359
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002360void SelectionDAGLowering::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002361 // FPTrunc is never a no-op cast, no need to check
2362 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002363 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002364 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002365}
2366
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002367void SelectionDAGLowering::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002368 // FPToUI is never a no-op cast, no need to check
2369 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002370 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002371 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002372}
2373
2374void SelectionDAGLowering::visitFPToSI(User &I) {
2375 // FPToSI is never a no-op cast, no need to check
2376 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002377 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002378 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002379}
2380
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002381void SelectionDAGLowering::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002382 // UIToFP is never a no-op cast, no need to check
2383 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002384 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002385 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002386}
2387
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002388void SelectionDAGLowering::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002389 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002390 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002391 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002392 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002393}
2394
2395void SelectionDAGLowering::visitPtrToInt(User &I) {
2396 // What to do depends on the size of the integer and the size of the pointer.
2397 // We can either truncate, zero extend, or no-op, accordingly.
2398 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT SrcVT = N.getValueType();
2400 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002401 SDValue Result = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002402 setValue(&I, Result);
2403}
2404
2405void SelectionDAGLowering::visitIntToPtr(User &I) {
2406 // What to do depends on the size of the integer and the size of the pointer.
2407 // We can either truncate, zero extend, or no-op, accordingly.
2408 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002409 EVT SrcVT = N.getValueType();
2410 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002411 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002412}
2413
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002414void SelectionDAGLowering::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002415 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002416 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002417
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002418 // BitCast assures us that source and destination are the same size so this
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002419 // is either a BIT_CONVERT or a no-op.
2420 if (DestVT != N.getValueType())
Scott Michelfdc40a02009-02-17 22:15:04 +00002421 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002422 DestVT, N)); // convert types
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002423 else
2424 setValue(&I, N); // noop cast.
2425}
2426
2427void SelectionDAGLowering::visitInsertElement(User &I) {
2428 SDValue InVec = getValue(I.getOperand(0));
2429 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002430 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002431 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002432 getValue(I.getOperand(2)));
2433
Scott Michelfdc40a02009-02-17 22:15:04 +00002434 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002435 TLI.getValueType(I.getType()),
2436 InVec, InVal, InIdx));
2437}
2438
2439void SelectionDAGLowering::visitExtractElement(User &I) {
2440 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002441 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002442 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002443 getValue(I.getOperand(1)));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002444 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002445 TLI.getValueType(I.getType()), InVec, InIdx));
2446}
2447
Mon P Wangaeb06d22008-11-10 04:46:22 +00002448
2449// Utility for visitShuffleVector - Returns true if the mask is mask starting
2450// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002451static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2452 unsigned MaskNumElts = Mask.size();
2453 for (unsigned i = 0; i != MaskNumElts; ++i)
2454 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002455 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002456 return true;
2457}
2458
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002459void SelectionDAGLowering::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002460 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002461 SDValue Src1 = getValue(I.getOperand(0));
2462 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002463
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 // Convert the ConstantVector mask operand into an array of ints, with -1
2465 // representing undef values.
2466 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002467 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2468 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002469 unsigned MaskNumElts = MaskElts.size();
2470 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002471 if (isa<UndefValue>(MaskElts[i]))
2472 Mask.push_back(-1);
2473 else
2474 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2475 }
2476
Owen Andersone50ed302009-08-10 22:56:29 +00002477 EVT VT = TLI.getValueType(I.getType());
2478 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002479 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002480
Mon P Wangc7849c22008-11-16 05:06:27 +00002481 if (SrcNumElts == MaskNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002482 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2483 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002484 return;
2485 }
2486
2487 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002488 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2489 // Mask is longer than the source vectors and is a multiple of the source
2490 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002491 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002492 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2493 // The shuffle is concatenating two vectors together.
Scott Michelfdc40a02009-02-17 22:15:04 +00002494 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002495 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002496 return;
2497 }
2498
Mon P Wangc7849c22008-11-16 05:06:27 +00002499 // Pad both vectors with undefs to make them the same length as the mask.
2500 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002501 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2502 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002503 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002504
Nate Begeman9008ca62009-04-27 18:41:29 +00002505 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2506 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002507 MOps1[0] = Src1;
2508 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002509
2510 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2511 getCurDebugLoc(), VT,
2512 &MOps1[0], NumConcat);
2513 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2514 getCurDebugLoc(), VT,
2515 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002516
Mon P Wangaeb06d22008-11-10 04:46:22 +00002517 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002518 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002519 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002520 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002521 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002522 MappedOps.push_back(Idx);
2523 else
2524 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002525 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002526 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2527 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002528 return;
2529 }
2530
Mon P Wangc7849c22008-11-16 05:06:27 +00002531 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002532 // Analyze the access pattern of the vector to see if we can extract
2533 // two subvectors and do the shuffle. The analysis is done by calculating
2534 // the range of elements the mask access on both vectors.
2535 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2536 int MaxRange[2] = {-1, -1};
2537
Nate Begeman5a5ca152009-04-29 05:20:52 +00002538 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002539 int Idx = Mask[i];
2540 int Input = 0;
2541 if (Idx < 0)
2542 continue;
2543
Nate Begeman5a5ca152009-04-29 05:20:52 +00002544 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002545 Input = 1;
2546 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002547 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002548 if (Idx > MaxRange[Input])
2549 MaxRange[Input] = Idx;
2550 if (Idx < MinRange[Input])
2551 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002552 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002553
Mon P Wangc7849c22008-11-16 05:06:27 +00002554 // Check if the access is smaller than the vector size and can we find
2555 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002556 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002557 int StartIdx[2]; // StartIdx to extract from
2558 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002559 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002560 RangeUse[Input] = 0; // Unused
2561 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002562 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002563 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002564 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002565 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002566 RangeUse[Input] = 1; // Extract from beginning of the vector
2567 StartIdx[Input] = 0;
2568 } else {
2569 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002570 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002571 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002572 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002573 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002574 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002575 }
2576
Bill Wendling636e2582009-08-21 18:16:06 +00002577 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002578 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002579 return;
2580 }
2581 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2582 // Extract appropriate subvector and generate a vector shuffle
2583 for (int Input=0; Input < 2; ++Input) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002584 SDValue& Src = Input == 0 ? Src1 : Src2;
Mon P Wangc7849c22008-11-16 05:06:27 +00002585 if (RangeUse[Input] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002586 Src = DAG.getUNDEF(VT);
Mon P Wangc7849c22008-11-16 05:06:27 +00002587 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002588 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002589 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002590 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002591 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002592 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002593 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002594 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002595 int Idx = Mask[i];
2596 if (Idx < 0)
2597 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002598 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002599 MappedOps.push_back(Idx - StartIdx[0]);
2600 else
2601 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002602 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002603 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2604 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002605 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002606 }
2607 }
2608
Mon P Wangc7849c22008-11-16 05:06:27 +00002609 // We can't use either concat vectors or extract subvectors so fall back to
2610 // replacing the shuffle with extract and build vector.
2611 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002612 EVT EltVT = VT.getVectorElementType();
2613 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002614 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002615 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002616 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002617 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002618 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002619 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002620 if (Idx < (int)SrcNumElts)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002621 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002622 EltVT, Src1, DAG.getConstant(Idx, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002623 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002624 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Scott Michelfdc40a02009-02-17 22:15:04 +00002625 EltVT, Src2,
Mon P Wangc7849c22008-11-16 05:06:27 +00002626 DAG.getConstant(Idx - SrcNumElts, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002627 }
2628 }
Evan Chenga87008d2009-02-25 22:49:59 +00002629 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2630 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002631}
2632
2633void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) {
2634 const Value *Op0 = I.getOperand(0);
2635 const Value *Op1 = I.getOperand(1);
2636 const Type *AggTy = I.getType();
2637 const Type *ValTy = Op1->getType();
2638 bool IntoUndef = isa<UndefValue>(Op0);
2639 bool FromUndef = isa<UndefValue>(Op1);
2640
2641 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2642 I.idx_begin(), I.idx_end());
2643
Owen Andersone50ed302009-08-10 22:56:29 +00002644 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002645 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002646 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002647 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2648
2649 unsigned NumAggValues = AggValueVTs.size();
2650 unsigned NumValValues = ValValueVTs.size();
2651 SmallVector<SDValue, 4> Values(NumAggValues);
2652
2653 SDValue Agg = getValue(Op0);
2654 SDValue Val = getValue(Op1);
2655 unsigned i = 0;
2656 // Copy the beginning value(s) from the original aggregate.
2657 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002658 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002659 SDValue(Agg.getNode(), Agg.getResNo() + i);
2660 // Copy values from the inserted value(s).
2661 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002662 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002663 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2664 // Copy remaining value(s) from the original aggregate.
2665 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002666 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002667 SDValue(Agg.getNode(), Agg.getResNo() + i);
2668
Scott Michelfdc40a02009-02-17 22:15:04 +00002669 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002670 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2671 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002672}
2673
2674void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) {
2675 const Value *Op0 = I.getOperand(0);
2676 const Type *AggTy = Op0->getType();
2677 const Type *ValTy = I.getType();
2678 bool OutOfUndef = isa<UndefValue>(Op0);
2679
2680 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2681 I.idx_begin(), I.idx_end());
2682
Owen Andersone50ed302009-08-10 22:56:29 +00002683 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002684 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2685
2686 unsigned NumValValues = ValValueVTs.size();
2687 SmallVector<SDValue, 4> Values(NumValValues);
2688
2689 SDValue Agg = getValue(Op0);
2690 // Copy out the selected value(s).
2691 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2692 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002693 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002694 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002695 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002696
Scott Michelfdc40a02009-02-17 22:15:04 +00002697 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002698 DAG.getVTList(&ValValueVTs[0], NumValValues),
2699 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002700}
2701
2702
2703void SelectionDAGLowering::visitGetElementPtr(User &I) {
2704 SDValue N = getValue(I.getOperand(0));
2705 const Type *Ty = I.getOperand(0)->getType();
2706
2707 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2708 OI != E; ++OI) {
2709 Value *Idx = *OI;
2710 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2711 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2712 if (Field) {
2713 // N = N + Offset
2714 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002715 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002716 DAG.getIntPtrConstant(Offset));
2717 }
2718 Ty = StTy->getElementType(Field);
2719 } else {
2720 Ty = cast<SequentialType>(Ty)->getElementType();
2721
2722 // If this is a constant subscript, handle it quickly.
2723 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2724 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002725 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002726 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002727 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002728 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002729 unsigned PtrBits = PTy.getSizeInBits();
Evan Cheng65b52df2009-02-09 21:01:06 +00002730 if (PtrBits < 64) {
2731 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2732 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002733 DAG.getConstant(Offs, MVT::i64));
Evan Cheng65b52df2009-02-09 21:01:06 +00002734 } else
Evan Chengb1032a82009-02-09 20:54:38 +00002735 OffsVal = DAG.getIntPtrConstant(Offs);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002736 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002737 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002738 continue;
2739 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002740
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002741 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002742 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2743 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002744 SDValue IdxN = getValue(Idx);
2745
2746 // If the index is smaller or larger than intptr_t, truncate or extend
2747 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002748 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002749
2750 // If this is a multiply by a power of two, turn it into a shl
2751 // immediately. This is a very common case.
2752 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002753 if (ElementSize.isPowerOf2()) {
2754 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002755 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002756 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002757 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002758 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002759 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002760 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002761 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002762 }
2763 }
2764
Scott Michelfdc40a02009-02-17 22:15:04 +00002765 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002766 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002767 }
2768 }
2769 setValue(&I, N);
2770}
2771
2772void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2773 // If this is a fixed sized alloca in the entry block of the function,
2774 // allocate it statically on the stack.
2775 if (FuncInfo.StaticAllocaMap.count(&I))
2776 return; // getValue will auto-populate this.
2777
2778 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002779 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002780 unsigned Align =
2781 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2782 I.getAlignment());
2783
2784 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002785
2786 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2787 AllocSize,
2788 DAG.getConstant(TySize, AllocSize.getValueType()));
2789
2790
2791
Owen Andersone50ed302009-08-10 22:56:29 +00002792 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002793 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002794
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002795 // Handle alignment. If the requested alignment is less than or equal to
2796 // the stack alignment, ignore it. If the size is greater than or equal to
2797 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2798 unsigned StackAlign =
2799 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2800 if (Align <= StackAlign)
2801 Align = 0;
2802
2803 // Round the size of the allocation up to the stack alignment size
2804 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002805 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002806 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807 DAG.getIntPtrConstant(StackAlign-1));
2808 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002809 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002810 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002811 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2812
2813 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002814 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002815 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002816 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002817 setValue(&I, DSA);
2818 DAG.setRoot(DSA.getValue(1));
2819
2820 // Inform the Frame Information that we have just allocated a variable-sized
2821 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002822 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002823}
2824
2825void SelectionDAGLowering::visitLoad(LoadInst &I) {
2826 const Value *SV = I.getOperand(0);
2827 SDValue Ptr = getValue(SV);
2828
2829 const Type *Ty = I.getType();
2830 bool isVolatile = I.isVolatile();
2831 unsigned Alignment = I.getAlignment();
2832
Owen Andersone50ed302009-08-10 22:56:29 +00002833 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002834 SmallVector<uint64_t, 4> Offsets;
2835 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2836 unsigned NumValues = ValueVTs.size();
2837 if (NumValues == 0)
2838 return;
2839
2840 SDValue Root;
2841 bool ConstantMemory = false;
2842 if (I.isVolatile())
2843 // Serialize volatile loads with other side effects.
2844 Root = getRoot();
2845 else if (AA->pointsToConstantMemory(SV)) {
2846 // Do not serialize (non-volatile) loads of constant memory with anything.
2847 Root = DAG.getEntryNode();
2848 ConstantMemory = true;
2849 } else {
2850 // Do not serialize non-volatile loads against each other.
2851 Root = DAG.getRoot();
2852 }
2853
2854 SmallVector<SDValue, 4> Values(NumValues);
2855 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002856 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002857 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002858 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Nate Begemane6798372009-09-15 00:13:12 +00002859 DAG.getNode(ISD::ADD, getCurDebugLoc(),
2860 PtrVT, Ptr,
2861 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002862 SV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002863 Values[i] = L;
2864 Chains[i] = L.getValue(1);
2865 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002866
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002867 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002868 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002869 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002870 &Chains[0], NumValues);
2871 if (isVolatile)
2872 DAG.setRoot(Chain);
2873 else
2874 PendingLoads.push_back(Chain);
2875 }
2876
Scott Michelfdc40a02009-02-17 22:15:04 +00002877 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002878 DAG.getVTList(&ValueVTs[0], NumValues),
2879 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002880}
2881
2882
2883void SelectionDAGLowering::visitStore(StoreInst &I) {
2884 Value *SrcV = I.getOperand(0);
2885 Value *PtrV = I.getOperand(1);
2886
Owen Andersone50ed302009-08-10 22:56:29 +00002887 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002888 SmallVector<uint64_t, 4> Offsets;
2889 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2890 unsigned NumValues = ValueVTs.size();
2891 if (NumValues == 0)
2892 return;
2893
2894 // Get the lowered operands. Note that we do this after
2895 // checking if NumResults is zero, because with zero results
2896 // the operands won't have values in the map.
2897 SDValue Src = getValue(SrcV);
2898 SDValue Ptr = getValue(PtrV);
2899
2900 SDValue Root = getRoot();
2901 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002902 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002903 bool isVolatile = I.isVolatile();
2904 unsigned Alignment = I.getAlignment();
2905 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002906 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002907 SDValue(Src.getNode(), Src.getResNo() + i),
Scott Michelfdc40a02009-02-17 22:15:04 +00002908 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002909 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002910 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002911 PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002912
Scott Michelfdc40a02009-02-17 22:15:04 +00002913 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002914 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002915}
2916
2917/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2918/// node.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002919void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002920 unsigned Intrinsic) {
2921 bool HasChain = !I.doesNotAccessMemory();
2922 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2923
2924 // Build the operand list.
2925 SmallVector<SDValue, 8> Ops;
2926 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2927 if (OnlyLoad) {
2928 // We don't need to serialize loads against other loads.
2929 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002930 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002931 Ops.push_back(getRoot());
2932 }
2933 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002934
2935 // Info is set by getTgtMemInstrinsic
2936 TargetLowering::IntrinsicInfo Info;
2937 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2938
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002939 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002940 if (!IsTgtIntrinsic)
2941 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002942
2943 // Add all operands of the call to the operand list.
2944 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2945 SDValue Op = getValue(I.getOperand(i));
2946 assert(TLI.isTypeLegal(Op.getValueType()) &&
2947 "Intrinsic uses a non-legal type?");
2948 Ops.push_back(Op);
2949 }
2950
Owen Andersone50ed302009-08-10 22:56:29 +00002951 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002952 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2953#ifndef NDEBUG
2954 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2955 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2956 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002957 }
Bob Wilson8d919552009-07-31 22:41:21 +00002958#endif // NDEBUG
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002959 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002961
Bob Wilson8d919552009-07-31 22:41:21 +00002962 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002963
2964 // Create the node.
2965 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002966 if (IsTgtIntrinsic) {
2967 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002968 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002969 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002970 Info.memVT, Info.ptrVal, Info.offset,
2971 Info.align, Info.vol,
2972 Info.readMem, Info.writeMem);
2973 }
2974 else if (!HasChain)
Scott Michelfdc40a02009-02-17 22:15:04 +00002975 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002976 VTs, &Ops[0], Ops.size());
Owen Anderson1d0be152009-08-13 21:58:54 +00002977 else if (I.getType() != Type::getVoidTy(*DAG.getContext()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002978 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002979 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002980 else
Scott Michelfdc40a02009-02-17 22:15:04 +00002981 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002982 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002983
2984 if (HasChain) {
2985 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2986 if (OnlyLoad)
2987 PendingLoads.push_back(Chain);
2988 else
2989 DAG.setRoot(Chain);
2990 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002991 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002992 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002993 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002994 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002995 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002996 setValue(&I, Result);
2997 }
2998}
2999
3000/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
3001static GlobalVariable *ExtractTypeInfo(Value *V) {
3002 V = V->stripPointerCasts();
3003 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
3004 assert ((GV || isa<ConstantPointerNull>(V)) &&
3005 "TypeInfo must be a global variable or NULL");
3006 return GV;
3007}
3008
3009namespace llvm {
3010
3011/// AddCatchInfo - Extract the personality and type infos from an eh.selector
3012/// call, and add them to the specified machine basic block.
3013void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
3014 MachineBasicBlock *MBB) {
3015 // Inform the MachineModuleInfo of the personality for this landing pad.
3016 ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
3017 assert(CE->getOpcode() == Instruction::BitCast &&
3018 isa<Function>(CE->getOperand(0)) &&
3019 "Personality should be a function");
3020 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
3021
3022 // Gather all the type infos for this landing pad and pass them along to
3023 // MachineModuleInfo.
3024 std::vector<GlobalVariable *> TyInfo;
3025 unsigned N = I.getNumOperands();
3026
3027 for (unsigned i = N - 1; i > 2; --i) {
3028 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
3029 unsigned FilterLength = CI->getZExtValue();
3030 unsigned FirstCatch = i + FilterLength + !FilterLength;
3031 assert (FirstCatch <= N && "Invalid filter length");
3032
3033 if (FirstCatch < N) {
3034 TyInfo.reserve(N - FirstCatch);
3035 for (unsigned j = FirstCatch; j < N; ++j)
3036 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3037 MMI->addCatchTypeInfo(MBB, TyInfo);
3038 TyInfo.clear();
3039 }
3040
3041 if (!FilterLength) {
3042 // Cleanup.
3043 MMI->addCleanup(MBB);
3044 } else {
3045 // Filter.
3046 TyInfo.reserve(FilterLength - 1);
3047 for (unsigned j = i + 1; j < FirstCatch; ++j)
3048 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3049 MMI->addFilterTypeInfo(MBB, TyInfo);
3050 TyInfo.clear();
3051 }
3052
3053 N = i;
3054 }
3055 }
3056
3057 if (N > 3) {
3058 TyInfo.reserve(N - 3);
3059 for (unsigned j = 3; j < N; ++j)
3060 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3061 MMI->addCatchTypeInfo(MBB, TyInfo);
3062 }
3063}
3064
3065}
3066
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003067/// GetSignificand - Get the significand and build it into a floating-point
3068/// number with exponent of 1:
3069///
3070/// Op = (Op & 0x007fffff) | 0x3f800000;
3071///
3072/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003073static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003074GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3076 DAG.getConstant(0x007fffff, MVT::i32));
3077 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3078 DAG.getConstant(0x3f800000, MVT::i32));
3079 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003080}
3081
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003082/// GetExponent - Get the exponent:
3083///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003084/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003085///
3086/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003087static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003088GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
3089 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003090 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3091 DAG.getConstant(0x7f800000, MVT::i32));
3092 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003093 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003094 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3095 DAG.getConstant(127, MVT::i32));
3096 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003097}
3098
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003099/// getF32Constant - Get 32-bit floating point constant.
3100static SDValue
3101getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003102 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003103}
3104
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003105/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003106/// visitIntrinsicCall: I is a call instruction
3107/// Op is the associated NodeType for I
3108const char *
3109SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003110 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003111 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003112 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003113 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003114 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003115 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003116 getValue(I.getOperand(2)),
3117 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003118 setValue(&I, L);
3119 DAG.setRoot(L.getValue(1));
3120 return 0;
3121}
3122
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003123// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003124const char *
3125SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003126 SDValue Op1 = getValue(I.getOperand(1));
3127 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003128
Owen Anderson825b72b2009-08-11 20:47:22 +00003129 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003130 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003131
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003132 setValue(&I, Result);
3133 return 0;
3134}
Bill Wendling74c37652008-12-09 22:08:41 +00003135
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003136/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3137/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003138void
3139SelectionDAGLowering::visitExp(CallInst &I) {
3140 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003141 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003142
Owen Anderson825b72b2009-08-11 20:47:22 +00003143 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003144 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3145 SDValue Op = getValue(I.getOperand(1));
3146
3147 // Put the exponent in the right bit position for later addition to the
3148 // final result:
3149 //
3150 // #define LOG2OFe 1.4426950f
3151 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003153 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003154 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003155
3156 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003157 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3158 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003159
3160 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003161 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003162 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003163
3164 if (LimitFloatPrecision <= 6) {
3165 // For floating-point precision of 6:
3166 //
3167 // TwoToFractionalPartOfX =
3168 // 0.997535578f +
3169 // (0.735607626f + 0.252464424f * x) * x;
3170 //
3171 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003172 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003173 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003174 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003175 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003176 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3177 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003178 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003179 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003180
3181 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003182 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003183 TwoToFracPartOfX, IntegerPartOfX);
3184
Owen Anderson825b72b2009-08-11 20:47:22 +00003185 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003186 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3187 // For floating-point precision of 12:
3188 //
3189 // TwoToFractionalPartOfX =
3190 // 0.999892986f +
3191 // (0.696457318f +
3192 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3193 //
3194 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003196 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003197 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003198 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3200 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003201 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003202 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3203 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003204 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003205 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003206
3207 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003208 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003209 TwoToFracPartOfX, IntegerPartOfX);
3210
Owen Anderson825b72b2009-08-11 20:47:22 +00003211 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003212 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3213 // For floating-point precision of 18:
3214 //
3215 // TwoToFractionalPartOfX =
3216 // 0.999999982f +
3217 // (0.693148872f +
3218 // (0.240227044f +
3219 // (0.554906021e-1f +
3220 // (0.961591928e-2f +
3221 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3222 //
3223 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003224 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003225 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003227 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3229 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003230 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3232 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003233 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003234 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3235 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003236 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003237 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3238 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003239 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003240 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3241 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003242 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003243 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003245
3246 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003247 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003248 TwoToFracPartOfX, IntegerPartOfX);
3249
Owen Anderson825b72b2009-08-11 20:47:22 +00003250 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003251 }
3252 } else {
3253 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003254 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003255 getValue(I.getOperand(1)).getValueType(),
3256 getValue(I.getOperand(1)));
3257 }
3258
Dale Johannesen59e577f2008-09-05 18:38:42 +00003259 setValue(&I, result);
3260}
3261
Bill Wendling39150252008-09-09 20:39:27 +00003262/// visitLog - Lower a log intrinsic. Handles the special sequences for
3263/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003264void
3265SelectionDAGLowering::visitLog(CallInst &I) {
3266 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003267 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003268
Owen Anderson825b72b2009-08-11 20:47:22 +00003269 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003270 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3271 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003272 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003273
3274 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003275 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003276 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003277 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003278
3279 // Get the significand and build it into a floating-point number with
3280 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003281 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003282
3283 if (LimitFloatPrecision <= 6) {
3284 // For floating-point precision of 6:
3285 //
3286 // LogofMantissa =
3287 // -1.1609546f +
3288 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003289 //
Bill Wendling39150252008-09-09 20:39:27 +00003290 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003292 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003294 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003295 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3296 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003297 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003298
Scott Michelfdc40a02009-02-17 22:15:04 +00003299 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003301 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3302 // For floating-point precision of 12:
3303 //
3304 // LogOfMantissa =
3305 // -1.7417939f +
3306 // (2.8212026f +
3307 // (-1.4699568f +
3308 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3309 //
3310 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003311 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003312 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003314 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003315 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3316 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003317 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003318 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3319 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003320 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003321 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3322 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003323 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003324
Scott Michelfdc40a02009-02-17 22:15:04 +00003325 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003326 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003327 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3328 // For floating-point precision of 18:
3329 //
3330 // LogOfMantissa =
3331 // -2.1072184f +
3332 // (4.2372794f +
3333 // (-3.7029485f +
3334 // (2.2781945f +
3335 // (-0.87823314f +
3336 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3337 //
3338 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003340 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003342 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003343 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3344 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003345 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3347 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003348 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3350 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003351 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3353 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003354 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3356 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003357 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003358
Scott Michelfdc40a02009-02-17 22:15:04 +00003359 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003361 }
3362 } else {
3363 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003364 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003365 getValue(I.getOperand(1)).getValueType(),
3366 getValue(I.getOperand(1)));
3367 }
3368
Dale Johannesen59e577f2008-09-05 18:38:42 +00003369 setValue(&I, result);
3370}
3371
Bill Wendling3eb59402008-09-09 00:28:24 +00003372/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3373/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003374void
3375SelectionDAGLowering::visitLog2(CallInst &I) {
3376 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003377 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003378
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003380 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3381 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003382 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003383
Bill Wendling39150252008-09-09 20:39:27 +00003384 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003385 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003386
3387 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003388 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003389 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003390
Bill Wendling3eb59402008-09-09 00:28:24 +00003391 // Different possible minimax approximations of significand in
3392 // floating-point for various degrees of accuracy over [1,2].
3393 if (LimitFloatPrecision <= 6) {
3394 // For floating-point precision of 6:
3395 //
3396 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3397 //
3398 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003400 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003402 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3404 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003405 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003406
Scott Michelfdc40a02009-02-17 22:15:04 +00003407 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003409 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3410 // For floating-point precision of 12:
3411 //
3412 // Log2ofMantissa =
3413 // -2.51285454f +
3414 // (4.07009056f +
3415 // (-2.12067489f +
3416 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003417 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003418 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003420 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003422 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3424 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003425 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003426 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3427 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003428 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003429 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3430 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003431 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003432
Scott Michelfdc40a02009-02-17 22:15:04 +00003433 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003435 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3436 // For floating-point precision of 18:
3437 //
3438 // Log2ofMantissa =
3439 // -3.0400495f +
3440 // (6.1129976f +
3441 // (-5.3420409f +
3442 // (3.2865683f +
3443 // (-1.2669343f +
3444 // (0.27515199f -
3445 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3446 //
3447 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003448 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003449 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003451 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003452 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3453 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003454 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3456 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003457 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3459 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003460 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003461 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3462 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003463 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3465 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003466 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003467
Scott Michelfdc40a02009-02-17 22:15:04 +00003468 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003470 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003471 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003472 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003473 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003474 getValue(I.getOperand(1)).getValueType(),
3475 getValue(I.getOperand(1)));
3476 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003477
Dale Johannesen59e577f2008-09-05 18:38:42 +00003478 setValue(&I, result);
3479}
3480
Bill Wendling3eb59402008-09-09 00:28:24 +00003481/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3482/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003483void
3484SelectionDAGLowering::visitLog10(CallInst &I) {
3485 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003486 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003487
Owen Anderson825b72b2009-08-11 20:47:22 +00003488 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003489 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3490 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003492
Bill Wendling39150252008-09-09 20:39:27 +00003493 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003494 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003496 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003497
3498 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003499 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003500 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003501
3502 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003503 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003504 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003505 // Log10ofMantissa =
3506 // -0.50419619f +
3507 // (0.60948995f - 0.10380950f * x) * x;
3508 //
3509 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003510 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003511 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003513 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3515 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003516 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003517
Scott Michelfdc40a02009-02-17 22:15:04 +00003518 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003519 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003520 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3521 // For floating-point precision of 12:
3522 //
3523 // Log10ofMantissa =
3524 // -0.64831180f +
3525 // (0.91751397f +
3526 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3527 //
3528 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003530 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003532 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3534 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003535 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3537 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003538 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003539
Scott Michelfdc40a02009-02-17 22:15:04 +00003540 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003542 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003543 // For floating-point precision of 18:
3544 //
3545 // Log10ofMantissa =
3546 // -0.84299375f +
3547 // (1.5327582f +
3548 // (-1.0688956f +
3549 // (0.49102474f +
3550 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3551 //
3552 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003554 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003556 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003557 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3558 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003559 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3561 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003562 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3564 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003565 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003566 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3567 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003568 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003569
Scott Michelfdc40a02009-02-17 22:15:04 +00003570 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003572 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003573 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003574 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003575 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003576 getValue(I.getOperand(1)).getValueType(),
3577 getValue(I.getOperand(1)));
3578 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003579
Dale Johannesen59e577f2008-09-05 18:38:42 +00003580 setValue(&I, result);
3581}
3582
Bill Wendlinge10c8142008-09-09 22:39:21 +00003583/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3584/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003585void
3586SelectionDAGLowering::visitExp2(CallInst &I) {
3587 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003588 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003589
Owen Anderson825b72b2009-08-11 20:47:22 +00003590 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003591 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3592 SDValue Op = getValue(I.getOperand(1));
3593
Owen Anderson825b72b2009-08-11 20:47:22 +00003594 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003595
3596 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3598 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003599
3600 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003602 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003603
3604 if (LimitFloatPrecision <= 6) {
3605 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003606 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003607 // TwoToFractionalPartOfX =
3608 // 0.997535578f +
3609 // (0.735607626f + 0.252464424f * x) * x;
3610 //
3611 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003613 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003615 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003616 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3617 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003618 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003619 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003620 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003621 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003622
Scott Michelfdc40a02009-02-17 22:15:04 +00003623 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003624 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003625 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3626 // For floating-point precision of 12:
3627 //
3628 // TwoToFractionalPartOfX =
3629 // 0.999892986f +
3630 // (0.696457318f +
3631 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3632 //
3633 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003635 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003636 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003637 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003638 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3639 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003640 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003641 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3642 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003643 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003644 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003645 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003646 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003647
Scott Michelfdc40a02009-02-17 22:15:04 +00003648 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003649 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003650 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3651 // For floating-point precision of 18:
3652 //
3653 // TwoToFractionalPartOfX =
3654 // 0.999999982f +
3655 // (0.693148872f +
3656 // (0.240227044f +
3657 // (0.554906021e-1f +
3658 // (0.961591928e-2f +
3659 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3660 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003662 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003663 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003664 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003665 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3666 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003667 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3669 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003670 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003671 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3672 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003673 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003674 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3675 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003676 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003677 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3678 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003679 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003681 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003682 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003683
Scott Michelfdc40a02009-02-17 22:15:04 +00003684 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003685 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003686 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003687 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003688 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003689 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003690 getValue(I.getOperand(1)).getValueType(),
3691 getValue(I.getOperand(1)));
3692 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003693
Dale Johannesen601d3c02008-09-05 01:48:15 +00003694 setValue(&I, result);
3695}
3696
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003697/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3698/// limited-precision mode with x == 10.0f.
3699void
3700SelectionDAGLowering::visitPow(CallInst &I) {
3701 SDValue result;
3702 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003703 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003704 bool IsExp10 = false;
3705
Owen Anderson825b72b2009-08-11 20:47:22 +00003706 if (getValue(Val).getValueType() == MVT::f32 &&
3707 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003708 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3709 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3710 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3711 APFloat Ten(10.0f);
3712 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3713 }
3714 }
3715 }
3716
3717 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3718 SDValue Op = getValue(I.getOperand(2));
3719
3720 // Put the exponent in the right bit position for later addition to the
3721 // final result:
3722 //
3723 // #define LOG2OF10 3.3219281f
3724 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003725 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003726 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003728
3729 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003730 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3731 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003732
3733 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003735 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003736
3737 if (LimitFloatPrecision <= 6) {
3738 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003739 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003740 // twoToFractionalPartOfX =
3741 // 0.997535578f +
3742 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003743 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003744 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003745 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003746 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003747 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003748 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003749 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3750 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003751 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003752 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003753 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003754 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003755
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003756 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003757 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003758 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3759 // For floating-point precision of 12:
3760 //
3761 // TwoToFractionalPartOfX =
3762 // 0.999892986f +
3763 // (0.696457318f +
3764 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3765 //
3766 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003768 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003769 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003770 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003771 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3772 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003773 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003774 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3775 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003776 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003777 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003778 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003779 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003780
Scott Michelfdc40a02009-02-17 22:15:04 +00003781 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003782 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003783 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3784 // For floating-point precision of 18:
3785 //
3786 // TwoToFractionalPartOfX =
3787 // 0.999999982f +
3788 // (0.693148872f +
3789 // (0.240227044f +
3790 // (0.554906021e-1f +
3791 // (0.961591928e-2f +
3792 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3793 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003795 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003797 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3799 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003800 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003801 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3802 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003803 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003804 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3805 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003806 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003807 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3808 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003809 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003810 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3811 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003812 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003814 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003815 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003816
Scott Michelfdc40a02009-02-17 22:15:04 +00003817 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003818 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003819 }
3820 } else {
3821 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003822 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003823 getValue(I.getOperand(1)).getValueType(),
3824 getValue(I.getOperand(1)),
3825 getValue(I.getOperand(2)));
3826 }
3827
3828 setValue(&I, result);
3829}
3830
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003831/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3832/// we want to emit this as a call to a named external function, return the name
3833/// otherwise lower it and return null.
3834const char *
3835SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003836 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003837 switch (Intrinsic) {
3838 default:
3839 // By default, turn this into a target intrinsic node.
3840 visitTargetIntrinsic(I, Intrinsic);
3841 return 0;
3842 case Intrinsic::vastart: visitVAStart(I); return 0;
3843 case Intrinsic::vaend: visitVAEnd(I); return 0;
3844 case Intrinsic::vacopy: visitVACopy(I); return 0;
3845 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003846 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003847 getValue(I.getOperand(1))));
3848 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003849 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003850 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003851 getValue(I.getOperand(1))));
3852 return 0;
3853 case Intrinsic::setjmp:
3854 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3855 break;
3856 case Intrinsic::longjmp:
3857 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3858 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003859 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003860 SDValue Op1 = getValue(I.getOperand(1));
3861 SDValue Op2 = getValue(I.getOperand(2));
3862 SDValue Op3 = getValue(I.getOperand(3));
3863 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003864 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003865 I.getOperand(1), 0, I.getOperand(2), 0));
3866 return 0;
3867 }
Chris Lattner824b9582008-11-21 16:42:48 +00003868 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003869 SDValue Op1 = getValue(I.getOperand(1));
3870 SDValue Op2 = getValue(I.getOperand(2));
3871 SDValue Op3 = getValue(I.getOperand(3));
3872 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003873 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003874 I.getOperand(1), 0));
3875 return 0;
3876 }
Chris Lattner824b9582008-11-21 16:42:48 +00003877 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003878 SDValue Op1 = getValue(I.getOperand(1));
3879 SDValue Op2 = getValue(I.getOperand(2));
3880 SDValue Op3 = getValue(I.getOperand(3));
3881 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3882
3883 // If the source and destination are known to not be aliases, we can
3884 // lower memmove as memcpy.
3885 uint64_t Size = -1ULL;
3886 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003887 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003888 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3889 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00003890 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003891 I.getOperand(1), 0, I.getOperand(2), 0));
3892 return 0;
3893 }
3894
Dale Johannesena04b7572009-02-03 23:04:43 +00003895 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003896 I.getOperand(1), 0, I.getOperand(2), 0));
3897 return 0;
3898 }
3899 case Intrinsic::dbg_stoppoint: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003900 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003901 if (isValidDebugInfoIntrinsic(SPI, CodeGenOpt::Default)) {
Evan Chenge3d42322009-02-25 07:04:34 +00003902 MachineFunction &MF = DAG.getMachineFunction();
Devang Patel7e1e31f2009-07-02 22:43:26 +00003903 DebugLoc Loc = ExtractDebugLocation(SPI, MF.getDebugLocInfo());
Chris Lattneraf29a522009-05-04 22:10:05 +00003904 setCurDebugLoc(Loc);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003905
Bill Wendling98a366d2009-04-29 23:29:43 +00003906 if (OptLevel == CodeGenOpt::None)
Chris Lattneraf29a522009-05-04 22:10:05 +00003907 DAG.setRoot(DAG.getDbgStopPoint(Loc, getRoot(),
Dale Johannesenbeaec4c2009-03-25 17:36:08 +00003908 SPI.getLine(),
3909 SPI.getColumn(),
3910 SPI.getContext()));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003911 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003912 return 0;
3913 }
3914 case Intrinsic::dbg_region_start: {
Devang Patel83489bb2009-01-13 00:35:13 +00003915 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003916 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003917 if (isValidDebugInfoIntrinsic(RSI, OptLevel) && DW
3918 && DW->ShouldEmitDwarfDebug()) {
Bill Wendlingdf7d5d32009-05-21 00:04:55 +00003919 unsigned LabelID =
Devang Patele4b27562009-08-28 23:24:31 +00003920 DW->RecordRegionStart(RSI.getContext());
Devang Patel48c7fa22009-04-13 18:13:16 +00003921 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3922 getRoot(), LabelID));
Bill Wendling92c1e122009-02-13 02:16:35 +00003923 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003924 return 0;
3925 }
3926 case Intrinsic::dbg_region_end: {
Devang Patel83489bb2009-01-13 00:35:13 +00003927 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003928 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
Devang Patel0f7fef32009-04-13 17:02:03 +00003929
Devang Patel7e1e31f2009-07-02 22:43:26 +00003930 if (!isValidDebugInfoIntrinsic(REI, OptLevel) || !DW
3931 || !DW->ShouldEmitDwarfDebug())
3932 return 0;
Bill Wendling6c4311d2009-05-08 21:14:49 +00003933
Devang Patel7e1e31f2009-07-02 22:43:26 +00003934 MachineFunction &MF = DAG.getMachineFunction();
Devang Patele4b27562009-08-28 23:24:31 +00003935 DISubprogram Subprogram(REI.getContext());
Devang Patel7e1e31f2009-07-02 22:43:26 +00003936
3937 if (isInlinedFnEnd(REI, MF.getFunction())) {
3938 // This is end of inlined function. Debugging information for inlined
3939 // function is not handled yet (only supported by FastISel).
3940 if (OptLevel == CodeGenOpt::None) {
3941 unsigned ID = DW->RecordInlinedFnEnd(Subprogram);
3942 if (ID != 0)
3943 // Returned ID is 0 if this is unbalanced "end of inlined
3944 // scope". This could happen if optimizer eats dbg intrinsics or
3945 // "beginning of inlined scope" is not recoginized due to missing
3946 // location info. In such cases, do ignore this region.end.
3947 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3948 getRoot(), ID));
Devang Patel0f7fef32009-04-13 17:02:03 +00003949 }
Devang Patel7e1e31f2009-07-02 22:43:26 +00003950 return 0;
3951 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003952
Devang Patel7e1e31f2009-07-02 22:43:26 +00003953 unsigned LabelID =
Devang Patele4b27562009-08-28 23:24:31 +00003954 DW->RecordRegionEnd(REI.getContext());
Devang Patel7e1e31f2009-07-02 22:43:26 +00003955 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3956 getRoot(), LabelID));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003957 return 0;
3958 }
3959 case Intrinsic::dbg_func_start: {
Devang Patel83489bb2009-01-13 00:35:13 +00003960 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003961 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
Jeffrey Yasskin32360a72009-07-16 21:07:26 +00003962 if (!isValidDebugInfoIntrinsic(FSI, CodeGenOpt::None))
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00003963 return 0;
Devang Patel16f2ffd2009-04-16 02:33:41 +00003964
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00003965 MachineFunction &MF = DAG.getMachineFunction();
Devang Patel7e1e31f2009-07-02 22:43:26 +00003966 // This is a beginning of an inlined function.
3967 if (isInlinedFnStart(FSI, MF.getFunction())) {
3968 if (OptLevel != CodeGenOpt::None)
3969 // FIXME: Debugging informaation for inlined function is only
3970 // supported at CodeGenOpt::Node.
3971 return 0;
3972
Bill Wendlingc677fe52009-05-10 00:10:50 +00003973 DebugLoc PrevLoc = CurDebugLoc;
Devang Patel07b0ec02009-07-02 00:08:09 +00003974 // If llvm.dbg.func.start is seen in a new block before any
3975 // llvm.dbg.stoppoint intrinsic then the location info is unknown.
3976 // FIXME : Why DebugLoc is reset at the beginning of each block ?
3977 if (PrevLoc.isUnknown())
3978 return 0;
Devang Patel07b0ec02009-07-02 00:08:09 +00003979
Devang Patel7e1e31f2009-07-02 22:43:26 +00003980 // Record the source line.
3981 setCurDebugLoc(ExtractDebugLocation(FSI, MF.getDebugLocInfo()));
3982
Jeffrey Yasskin32360a72009-07-16 21:07:26 +00003983 if (!DW || !DW->ShouldEmitDwarfDebug())
3984 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003985 DebugLocTuple PrevLocTpl = MF.getDebugLocTuple(PrevLoc);
Devang Patele4b27562009-08-28 23:24:31 +00003986 DISubprogram SP(FSI.getSubprogram());
Devang Patel1619dc32009-10-13 23:28:53 +00003987 DICompileUnit CU(PrevLocTpl.Scope);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003988 unsigned LabelID = DW->RecordInlinedFnStart(SP, CU,
3989 PrevLocTpl.Line,
3990 PrevLocTpl.Col);
3991 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3992 getRoot(), LabelID));
Devang Patel07b0ec02009-07-02 00:08:09 +00003993 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003994 }
3995
Devang Patel07b0ec02009-07-02 00:08:09 +00003996 // This is a beginning of a new function.
Devang Patel7e1e31f2009-07-02 22:43:26 +00003997 MF.setDefaultDebugLoc(ExtractDebugLocation(FSI, MF.getDebugLocInfo()));
Jeffrey Yasskin32360a72009-07-16 21:07:26 +00003998
3999 if (!DW || !DW->ShouldEmitDwarfDebug())
4000 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004001 // llvm.dbg.func_start also defines beginning of function scope.
Devang Patele4b27562009-08-28 23:24:31 +00004002 DW->RecordRegionStart(FSI.getSubprogram());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004003 return 0;
4004 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004005 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00004006 if (OptLevel != CodeGenOpt::None)
4007 // FIXME: Variable debug info is not supported here.
4008 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004009 DwarfWriter *DW = DAG.getDwarfWriter();
4010 if (!DW)
4011 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004012 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
4013 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
4014 return 0;
4015
Devang Patelac1ceb32009-10-09 22:42:28 +00004016 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004017 Value *Address = DI.getAddress();
4018 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4019 Address = BCI->getOperand(0);
4020 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4021 // Don't handle byval struct arguments or VLAs, for example.
4022 if (!AI)
4023 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004024 DenseMap<const AllocaInst*, int>::iterator SI =
4025 FuncInfo.StaticAllocaMap.find(AI);
4026 if (SI == FuncInfo.StaticAllocaMap.end())
4027 return 0; // VLAs.
4028 int FI = SI->second;
Devang Patelac1ceb32009-10-09 22:42:28 +00004029#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
4030 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4031 if (MMI)
4032 MMI->setVariableDbgInfo(Variable, FI);
4033#else
4034 DW->RecordVariable(Variable, FI);
4035#endif
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004036 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004037 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004038 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004039 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004040 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004041 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004042 SDValue Ops[1];
4043 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004044 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004045 setValue(&I, Op);
4046 DAG.setRoot(Op.getValue(1));
4047 return 0;
4048 }
4049
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004050 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004051 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004052
Chris Lattner3a5815f2009-09-17 23:54:54 +00004053 if (CurMBB->isLandingPad())
4054 AddCatchInfo(I, MMI, CurMBB);
4055 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004056#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004057 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004058#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004059 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4060 unsigned Reg = TLI.getExceptionSelectorRegister();
4061 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004062 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004063
Chris Lattner3a5815f2009-09-17 23:54:54 +00004064 // Insert the EHSELECTION instruction.
4065 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4066 SDValue Ops[2];
4067 Ops[0] = getValue(I.getOperand(1));
4068 Ops[1] = getRoot();
4069 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4070
4071 DAG.setRoot(Op.getValue(1));
4072
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004073 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004074 return 0;
4075 }
4076
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004077 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004078 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004079
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004080 if (MMI) {
4081 // Find the type id for the given typeinfo.
4082 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
4083
4084 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004085 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004086 } else {
4087 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004088 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004089 }
4090
4091 return 0;
4092 }
4093
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004094 case Intrinsic::eh_return_i32:
4095 case Intrinsic::eh_return_i64:
4096 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004097 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004098 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004099 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004100 getControlRoot(),
4101 getValue(I.getOperand(1)),
4102 getValue(I.getOperand(2))));
4103 } else {
4104 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4105 }
4106
4107 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004108 case Intrinsic::eh_unwind_init:
4109 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4110 MMI->setCallsUnwindInit(true);
4111 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004112
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004113 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004114
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004115 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004116 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004117 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4118 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004119
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004120 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004121 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004122 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004123 TLI.getPointerTy()),
4124 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004125 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004126 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004127 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004128 TLI.getPointerTy(),
4129 DAG.getConstant(0,
4130 TLI.getPointerTy())),
4131 Offset));
4132 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004133 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004134 case Intrinsic::convertff:
4135 case Intrinsic::convertfsi:
4136 case Intrinsic::convertfui:
4137 case Intrinsic::convertsif:
4138 case Intrinsic::convertuif:
4139 case Intrinsic::convertss:
4140 case Intrinsic::convertsu:
4141 case Intrinsic::convertus:
4142 case Intrinsic::convertuu: {
4143 ISD::CvtCode Code = ISD::CVT_INVALID;
4144 switch (Intrinsic) {
4145 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4146 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4147 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4148 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4149 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4150 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4151 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4152 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4153 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4154 }
Owen Andersone50ed302009-08-10 22:56:29 +00004155 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00004156 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00004157 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00004158 DAG.getValueType(DestVT),
4159 DAG.getValueType(getValue(Op1).getValueType()),
4160 getValue(I.getOperand(2)),
4161 getValue(I.getOperand(3)),
4162 Code));
4163 return 0;
4164 }
4165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004166 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004167 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004168 getValue(I.getOperand(1)).getValueType(),
4169 getValue(I.getOperand(1))));
4170 return 0;
4171 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004172 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004173 getValue(I.getOperand(1)).getValueType(),
4174 getValue(I.getOperand(1)),
4175 getValue(I.getOperand(2))));
4176 return 0;
4177 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004178 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004179 getValue(I.getOperand(1)).getValueType(),
4180 getValue(I.getOperand(1))));
4181 return 0;
4182 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004183 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004184 getValue(I.getOperand(1)).getValueType(),
4185 getValue(I.getOperand(1))));
4186 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004187 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004188 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004189 return 0;
4190 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004191 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004192 return 0;
4193 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004194 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004195 return 0;
4196 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004197 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004198 return 0;
4199 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004200 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004201 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004202 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004203 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004204 return 0;
4205 case Intrinsic::pcmarker: {
4206 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004207 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004208 return 0;
4209 }
4210 case Intrinsic::readcyclecounter: {
4211 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004212 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004214 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004215 setValue(&I, Tmp);
4216 DAG.setRoot(Tmp.getValue(1));
4217 return 0;
4218 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004219 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004220 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004221 getValue(I.getOperand(1)).getValueType(),
4222 getValue(I.getOperand(1))));
4223 return 0;
4224 case Intrinsic::cttz: {
4225 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004226 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004227 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004228 setValue(&I, result);
4229 return 0;
4230 }
4231 case Intrinsic::ctlz: {
4232 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004233 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004234 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004235 setValue(&I, result);
4236 return 0;
4237 }
4238 case Intrinsic::ctpop: {
4239 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004240 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004241 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004242 setValue(&I, result);
4243 return 0;
4244 }
4245 case Intrinsic::stacksave: {
4246 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004247 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004248 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004249 setValue(&I, Tmp);
4250 DAG.setRoot(Tmp.getValue(1));
4251 return 0;
4252 }
4253 case Intrinsic::stackrestore: {
4254 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004255 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004256 return 0;
4257 }
Bill Wendling57344502008-11-18 11:01:33 +00004258 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004259 // Emit code into the DAG to store the stack guard onto the stack.
4260 MachineFunction &MF = DAG.getMachineFunction();
4261 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004262 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004263
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004264 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4265 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004266
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004267 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004268 MFI->setStackProtectorIndex(FI);
4269
4270 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4271
4272 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004273 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00004274 PseudoSourceValue::getFixedStack(FI),
4275 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004276 setValue(&I, Result);
4277 DAG.setRoot(Result);
4278 return 0;
4279 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004280 case Intrinsic::objectsize: {
4281 // If we don't know by now, we're never going to know.
4282 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4283
4284 assert(CI && "Non-constant type in __builtin_object_size?");
4285
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004286 SDValue Arg = getValue(I.getOperand(0));
4287 EVT Ty = Arg.getValueType();
4288
Eric Christopher7b5e6172009-10-27 00:52:25 +00004289 if (CI->getZExtValue() < 2)
Mike Stump70e5e682009-11-09 22:28:21 +00004290 setValue(&I, DAG.getConstant(-1ULL, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004291 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004292 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004293 return 0;
4294 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004295 case Intrinsic::var_annotation:
4296 // Discard annotate attributes
4297 return 0;
4298
4299 case Intrinsic::init_trampoline: {
4300 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4301
4302 SDValue Ops[6];
4303 Ops[0] = getRoot();
4304 Ops[1] = getValue(I.getOperand(1));
4305 Ops[2] = getValue(I.getOperand(2));
4306 Ops[3] = getValue(I.getOperand(3));
4307 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4308 Ops[5] = DAG.getSrcValue(F);
4309
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004310 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004311 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004312 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004313
4314 setValue(&I, Tmp);
4315 DAG.setRoot(Tmp.getValue(1));
4316 return 0;
4317 }
4318
4319 case Intrinsic::gcroot:
4320 if (GFI) {
4321 Value *Alloca = I.getOperand(1);
4322 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004323
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004324 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4325 GFI->addStackRoot(FI->getIndex(), TypeMap);
4326 }
4327 return 0;
4328
4329 case Intrinsic::gcread:
4330 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004331 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004332 return 0;
4333
4334 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004335 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004336 return 0;
4337 }
4338
4339 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004340 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004341 return 0;
4342 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004343
Bill Wendlingef375462008-11-21 02:38:44 +00004344 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004345 return implVisitAluOverflow(I, ISD::UADDO);
4346 case Intrinsic::sadd_with_overflow:
4347 return implVisitAluOverflow(I, ISD::SADDO);
4348 case Intrinsic::usub_with_overflow:
4349 return implVisitAluOverflow(I, ISD::USUBO);
4350 case Intrinsic::ssub_with_overflow:
4351 return implVisitAluOverflow(I, ISD::SSUBO);
4352 case Intrinsic::umul_with_overflow:
4353 return implVisitAluOverflow(I, ISD::UMULO);
4354 case Intrinsic::smul_with_overflow:
4355 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004356
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004357 case Intrinsic::prefetch: {
4358 SDValue Ops[4];
4359 Ops[0] = getRoot();
4360 Ops[1] = getValue(I.getOperand(1));
4361 Ops[2] = getValue(I.getOperand(2));
4362 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004363 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004364 return 0;
4365 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004366
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004367 case Intrinsic::memory_barrier: {
4368 SDValue Ops[6];
4369 Ops[0] = getRoot();
4370 for (int x = 1; x < 6; ++x)
4371 Ops[x] = getValue(I.getOperand(x));
4372
Owen Anderson825b72b2009-08-11 20:47:22 +00004373 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004374 return 0;
4375 }
4376 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004377 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004378 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004379 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004380 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4381 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004382 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004383 getValue(I.getOperand(2)),
4384 getValue(I.getOperand(3)),
4385 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004386 setValue(&I, L);
4387 DAG.setRoot(L.getValue(1));
4388 return 0;
4389 }
4390 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004391 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004393 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004394 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004395 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004396 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004397 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004398 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004399 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004400 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004401 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004402 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004403 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004404 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004405 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004407 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004408 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004409 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004410 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004411 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004412 }
4413}
4414
Dan Gohman98ca4f22009-08-05 01:29:28 +00004415/// Test if the given instruction is in a position to be optimized
4416/// with a tail-call. This roughly means that it's in a block with
4417/// a return and there's nothing that needs to be scheduled
4418/// between it and the return.
4419///
4420/// This function only tests target-independent requirements.
4421/// For target-dependent requirements, a target should override
4422/// TargetLowering::IsEligibleForTailCallOptimization.
4423///
4424static bool
4425isInTailCallPosition(const Instruction *I, Attributes RetAttr,
4426 const TargetLowering &TLI) {
4427 const BasicBlock *ExitBB = I->getParent();
4428 const TerminatorInst *Term = ExitBB->getTerminator();
4429 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4430 const Function *F = ExitBB->getParent();
4431
4432 // The block must end in a return statement or an unreachable.
4433 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4434
4435 // If I will have a chain, make sure no other instruction that will have a
4436 // chain interposes between I and the return.
4437 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4438 !I->isSafeToSpeculativelyExecute())
4439 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4440 --BBI) {
4441 if (&*BBI == I)
4442 break;
4443 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4444 !BBI->isSafeToSpeculativelyExecute())
4445 return false;
4446 }
4447
4448 // If the block ends with a void return or unreachable, it doesn't matter
4449 // what the call's return type is.
4450 if (!Ret || Ret->getNumOperands() == 0) return true;
4451
4452 // Conservatively require the attributes of the call to match those of
4453 // the return.
4454 if (F->getAttributes().getRetAttributes() != RetAttr)
4455 return false;
4456
4457 // Otherwise, make sure the unmodified return value of I is the return value.
4458 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4459 U = dyn_cast<Instruction>(U->getOperand(0))) {
4460 if (!U)
4461 return false;
4462 if (!U->hasOneUse())
4463 return false;
4464 if (U == I)
4465 break;
4466 // Check for a truly no-op truncate.
4467 if (isa<TruncInst>(U) &&
4468 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4469 continue;
4470 // Check for a truly no-op bitcast.
4471 if (isa<BitCastInst>(U) &&
4472 (U->getOperand(0)->getType() == U->getType() ||
4473 (isa<PointerType>(U->getOperand(0)->getType()) &&
4474 isa<PointerType>(U->getType()))))
4475 continue;
4476 // Otherwise it's not a true no-op.
4477 return false;
4478 }
4479
4480 return true;
4481}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004482
4483void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004484 bool isTailCall,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004485 MachineBasicBlock *LandingPad) {
4486 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4487 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
4488 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4489 unsigned BeginLabel = 0, EndLabel = 0;
4490
4491 TargetLowering::ArgListTy Args;
4492 TargetLowering::ArgListEntry Entry;
4493 Args.reserve(CS.arg_size());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004494 unsigned j = 1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004495 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00004496 i != e; ++i, ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004497 SDValue ArgNode = getValue(*i);
4498 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4499
4500 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004501 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4502 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4503 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4504 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4505 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4506 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004507 Entry.Alignment = CS.getParamAlignment(attrInd);
4508 Args.push_back(Entry);
4509 }
4510
4511 if (LandingPad && MMI) {
4512 // Insert a label before the invoke call to mark the try range. This can be
4513 // used to detect deletion of the invoke via the MachineModuleInfo.
4514 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004515
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004516 // Both PendingLoads and PendingExports must be flushed here;
4517 // this call might not return.
4518 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004519 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4520 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004521 }
4522
Dan Gohman98ca4f22009-08-05 01:29:28 +00004523 // Check if target-independent constraints permit a tail call here.
4524 // Target-dependent constraints are checked within TLI.LowerCallTo.
4525 if (isTailCall &&
4526 !isInTailCallPosition(CS.getInstruction(),
4527 CS.getAttributes().getRetAttributes(),
4528 TLI))
4529 isTailCall = false;
4530
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004531 std::pair<SDValue,SDValue> Result =
4532 TLI.LowerCallTo(getRoot(), CS.getType(),
Devang Patel05988662008-09-25 21:00:45 +00004533 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004534 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004535 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004536 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004537 isTailCall,
4538 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004539 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004540 assert((isTailCall || Result.second.getNode()) &&
4541 "Non-null chain expected with non-tail call!");
4542 assert((Result.second.getNode() || !Result.first.getNode()) &&
4543 "Null value expected with tail call!");
4544 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004545 setValue(CS.getInstruction(), Result.first);
Dan Gohman98ca4f22009-08-05 01:29:28 +00004546 // As a special case, a null chain means that a tail call has
4547 // been emitted and the DAG root is already updated.
4548 if (Result.second.getNode())
4549 DAG.setRoot(Result.second);
4550 else
4551 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004552
4553 if (LandingPad && MMI) {
4554 // Insert a label at the end of the invoke call to mark the try range. This
4555 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4556 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004557 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4558 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004559
4560 // Inform MachineModuleInfo of range.
4561 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4562 }
4563}
4564
4565
4566void SelectionDAGLowering::visitCall(CallInst &I) {
4567 const char *RenameFn = 0;
4568 if (Function *F = I.getCalledFunction()) {
4569 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004570 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4571 if (II) {
4572 if (unsigned IID = II->getIntrinsicID(F)) {
4573 RenameFn = visitIntrinsicCall(I, IID);
4574 if (!RenameFn)
4575 return;
4576 }
4577 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004578 if (unsigned IID = F->getIntrinsicID()) {
4579 RenameFn = visitIntrinsicCall(I, IID);
4580 if (!RenameFn)
4581 return;
4582 }
4583 }
4584
4585 // Check for well-known libc/libm calls. If the function is internal, it
4586 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004587 if (!F->hasLocalLinkage() && F->hasName()) {
4588 StringRef Name = F->getName();
4589 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004590 if (I.getNumOperands() == 3 && // Basic sanity checks.
4591 I.getOperand(1)->getType()->isFloatingPoint() &&
4592 I.getType() == I.getOperand(1)->getType() &&
4593 I.getType() == I.getOperand(2)->getType()) {
4594 SDValue LHS = getValue(I.getOperand(1));
4595 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004596 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004597 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004598 return;
4599 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004600 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004601 if (I.getNumOperands() == 2 && // Basic sanity checks.
4602 I.getOperand(1)->getType()->isFloatingPoint() &&
4603 I.getType() == I.getOperand(1)->getType()) {
4604 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004605 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004606 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004607 return;
4608 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004609 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004610 if (I.getNumOperands() == 2 && // Basic sanity checks.
4611 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004612 I.getType() == I.getOperand(1)->getType() &&
4613 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004614 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004615 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004616 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004617 return;
4618 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004619 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004620 if (I.getNumOperands() == 2 && // Basic sanity checks.
4621 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004622 I.getType() == I.getOperand(1)->getType() &&
4623 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004625 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004626 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004627 return;
4628 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004629 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4630 if (I.getNumOperands() == 2 && // Basic sanity checks.
4631 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004632 I.getType() == I.getOperand(1)->getType() &&
4633 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004634 SDValue Tmp = getValue(I.getOperand(1));
4635 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4636 Tmp.getValueType(), Tmp));
4637 return;
4638 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004639 }
4640 }
4641 } else if (isa<InlineAsm>(I.getOperand(0))) {
4642 visitInlineAsm(&I);
4643 return;
4644 }
4645
4646 SDValue Callee;
4647 if (!RenameFn)
4648 Callee = getValue(I.getOperand(0));
4649 else
Bill Wendling056292f2008-09-16 21:48:12 +00004650 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004651
Dan Gohman98ca4f22009-08-05 01:29:28 +00004652 // Check if we can potentially perform a tail call. More detailed
4653 // checking is be done within LowerCallTo, after more information
4654 // about the call is known.
4655 bool isTailCall = PerformTailCallOpt && I.isTailCall();
4656
4657 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004658}
4659
4660
4661/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004662/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004663/// Chain/Flag as the input and updates them for the output Chain/Flag.
4664/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004665SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004666 SDValue &Chain,
4667 SDValue *Flag) const {
4668 // Assemble the legal parts into the final values.
4669 SmallVector<SDValue, 4> Values(ValueVTs.size());
4670 SmallVector<SDValue, 8> Parts;
4671 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4672 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004673 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004674 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004675 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676
4677 Parts.resize(NumRegs);
4678 for (unsigned i = 0; i != NumRegs; ++i) {
4679 SDValue P;
4680 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004681 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004682 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004683 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004684 *Flag = P.getValue(2);
4685 }
4686 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004687
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004688 // If the source register was virtual and if we know something about it,
4689 // add an assert node.
4690 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4691 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4692 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4693 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4694 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4695 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004696
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004697 unsigned RegSize = RegisterVT.getSizeInBits();
4698 unsigned NumSignBits = LOI.NumSignBits;
4699 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004700
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004701 // FIXME: We capture more information than the dag can represent. For
4702 // now, just use the tightest assertzext/assertsext possible.
4703 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004705 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004706 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004707 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004709 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004710 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004711 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004713 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004714 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004715 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004716 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004717 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004718 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004719 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004720 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004721
Owen Anderson825b72b2009-08-11 20:47:22 +00004722 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004723 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004724 RegisterVT, P, DAG.getValueType(FromVT));
4725
4726 }
4727 }
4728 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004729
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004730 Parts[i] = P;
4731 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004732
Scott Michelfdc40a02009-02-17 22:15:04 +00004733 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004734 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004735 Part += NumRegs;
4736 Parts.clear();
4737 }
4738
Dale Johannesen66978ee2009-01-31 02:22:37 +00004739 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004740 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4741 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004742}
4743
4744/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004745/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004746/// Chain/Flag as the input and updates them for the output Chain/Flag.
4747/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004748void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004749 SDValue &Chain, SDValue *Flag) const {
4750 // Get the list of the values's legal parts.
4751 unsigned NumRegs = Regs.size();
4752 SmallVector<SDValue, 8> Parts(NumRegs);
4753 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004754 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004755 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004756 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757
Dale Johannesen66978ee2009-01-31 02:22:37 +00004758 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004759 &Parts[Part], NumParts, RegisterVT);
4760 Part += NumParts;
4761 }
4762
4763 // Copy the parts into the registers.
4764 SmallVector<SDValue, 8> Chains(NumRegs);
4765 for (unsigned i = 0; i != NumRegs; ++i) {
4766 SDValue Part;
4767 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004768 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004769 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004770 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004771 *Flag = Part.getValue(1);
4772 }
4773 Chains[i] = Part.getValue(0);
4774 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004775
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004776 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004777 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004778 // flagged to it. That is the CopyToReg nodes and the user are considered
4779 // a single scheduling unit. If we create a TokenFactor and return it as
4780 // chain, then the TokenFactor is both a predecessor (operand) of the
4781 // user as well as a successor (the TF operands are flagged to the user).
4782 // c1, f1 = CopyToReg
4783 // c2, f2 = CopyToReg
4784 // c3 = TokenFactor c1, c2
4785 // ...
4786 // = op c3, ..., f2
4787 Chain = Chains[NumRegs-1];
4788 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004789 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790}
4791
4792/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004793/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004794/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00004795void RegsForValue::AddInlineAsmOperands(unsigned Code,
4796 bool HasMatching,unsigned MatchingIdx,
4797 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004798 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004799 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00004800 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
4801 unsigned Flag = Code | (Regs.size() << 3);
4802 if (HasMatching)
4803 Flag |= 0x80000000 | (MatchingIdx << 16);
4804 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004805 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004806 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004807 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004808 for (unsigned i = 0; i != NumRegs; ++i) {
4809 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004810 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004811 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004812 }
4813}
4814
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004815/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004816/// i.e. it isn't a stack pointer or some other special register, return the
4817/// register class for the register. Otherwise, return null.
4818static const TargetRegisterClass *
4819isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4820 const TargetLowering &TLI,
4821 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004822 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004823 const TargetRegisterClass *FoundRC = 0;
4824 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4825 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004826 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004827
4828 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004829 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004830 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4831 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4832 I != E; ++I) {
4833 if (TLI.isTypeLegal(*I)) {
4834 // If we have already found this register in a different register class,
4835 // choose the one with the largest VT specified. For example, on
4836 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004837 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004838 ThisVT = *I;
4839 break;
4840 }
4841 }
4842 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004843
Owen Anderson825b72b2009-08-11 20:47:22 +00004844 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004845
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004846 // NOTE: This isn't ideal. In particular, this might allocate the
4847 // frame pointer in functions that need it (due to them not being taken
4848 // out of allocation, because a variable sized allocation hasn't been seen
4849 // yet). This is a slight code pessimization, but should still work.
4850 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4851 E = RC->allocation_order_end(MF); I != E; ++I)
4852 if (*I == Reg) {
4853 // We found a matching register class. Keep looking at others in case
4854 // we find one with larger registers that this physreg is also in.
4855 FoundRC = RC;
4856 FoundVT = ThisVT;
4857 break;
4858 }
4859 }
4860 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004861}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004862
4863
4864namespace llvm {
4865/// AsmOperandInfo - This contains information for each constraint that we are
4866/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004867class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004868 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004869public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004870 /// CallOperand - If this is the result output operand or a clobber
4871 /// this is null, otherwise it is the incoming operand to the CallInst.
4872 /// This gets modified as the asm is processed.
4873 SDValue CallOperand;
4874
4875 /// AssignedRegs - If this is a register or register class operand, this
4876 /// contains the set of register corresponding to the operand.
4877 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004878
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004879 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4880 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4881 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004882
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004883 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4884 /// busy in OutputRegs/InputRegs.
4885 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004886 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004887 std::set<unsigned> &InputRegs,
4888 const TargetRegisterInfo &TRI) const {
4889 if (isOutReg) {
4890 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4891 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4892 }
4893 if (isInReg) {
4894 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4895 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4896 }
4897 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004898
Owen Andersone50ed302009-08-10 22:56:29 +00004899 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004900 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004901 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00004902 EVT getCallOperandValEVT(LLVMContext &Context,
4903 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004904 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004905 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004906
Chris Lattner81249c92008-10-17 17:05:25 +00004907 if (isa<BasicBlock>(CallOperandVal))
4908 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004909
Chris Lattner81249c92008-10-17 17:05:25 +00004910 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004911
Chris Lattner81249c92008-10-17 17:05:25 +00004912 // If this is an indirect operand, the operand is a pointer to the
4913 // accessed type.
4914 if (isIndirect)
4915 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004916
Chris Lattner81249c92008-10-17 17:05:25 +00004917 // If OpTy is not a single value, it may be a struct/union that we
4918 // can tile with integers.
4919 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4920 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4921 switch (BitSize) {
4922 default: break;
4923 case 1:
4924 case 8:
4925 case 16:
4926 case 32:
4927 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004928 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004929 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004930 break;
4931 }
4932 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004933
Chris Lattner81249c92008-10-17 17:05:25 +00004934 return TLI.getValueType(OpTy, true);
4935 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004936
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004937private:
4938 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4939 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004940 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004941 const TargetRegisterInfo &TRI) {
4942 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4943 Regs.insert(Reg);
4944 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4945 for (; *Aliases; ++Aliases)
4946 Regs.insert(*Aliases);
4947 }
4948};
4949} // end llvm namespace.
4950
4951
4952/// GetRegistersForValue - Assign registers (virtual or physical) for the
4953/// specified operand. We prefer to assign virtual registers, to allow the
4954/// register allocator handle the assignment process. However, if the asm uses
4955/// features that we can't model on machineinstrs, we have SDISel do the
4956/// allocation. This produces generally horrible, but correct, code.
4957///
4958/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004959/// Input and OutputRegs are the set of already allocated physical registers.
4960///
4961void SelectionDAGLowering::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004962GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004963 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004964 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004965 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004966
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004967 // Compute whether this value requires an input register, an output register,
4968 // or both.
4969 bool isOutReg = false;
4970 bool isInReg = false;
4971 switch (OpInfo.Type) {
4972 case InlineAsm::isOutput:
4973 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004974
4975 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004976 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004977 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004978 break;
4979 case InlineAsm::isInput:
4980 isInReg = true;
4981 isOutReg = false;
4982 break;
4983 case InlineAsm::isClobber:
4984 isOutReg = true;
4985 isInReg = true;
4986 break;
4987 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004988
4989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004990 MachineFunction &MF = DAG.getMachineFunction();
4991 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004992
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004993 // If this is a constraint for a single physreg, or a constraint for a
4994 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004995 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004996 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4997 OpInfo.ConstraintVT);
4998
4999 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005000 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005001 // If this is a FP input in an integer register (or visa versa) insert a bit
5002 // cast of the input value. More generally, handle any case where the input
5003 // value disagrees with the register class we plan to stick this in.
5004 if (OpInfo.Type == InlineAsm::isInput &&
5005 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005006 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005007 // types are identical size, use a bitcast to convert (e.g. two differing
5008 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005009 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005010 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005011 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005012 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005013 OpInfo.ConstraintVT = RegVT;
5014 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5015 // If the input is a FP value and we want it in FP registers, do a
5016 // bitcast to the corresponding integer type. This turns an f64 value
5017 // into i64, which can be passed with two i32 values on a 32-bit
5018 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005019 RegVT = EVT::getIntegerVT(Context,
5020 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005021 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005022 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005023 OpInfo.ConstraintVT = RegVT;
5024 }
5025 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005026
Owen Anderson23b9b192009-08-12 00:36:31 +00005027 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005028 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005029
Owen Andersone50ed302009-08-10 22:56:29 +00005030 EVT RegVT;
5031 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005032
5033 // If this is a constraint for a specific physical register, like {r17},
5034 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005035 if (unsigned AssignedReg = PhysReg.first) {
5036 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005037 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005038 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005039
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005040 // Get the actual register value type. This is important, because the user
5041 // may have asked for (e.g.) the AX register in i32 type. We need to
5042 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005043 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005044
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005045 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005046 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005047
5048 // If this is an expanded reference, add the rest of the regs to Regs.
5049 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005050 TargetRegisterClass::iterator I = RC->begin();
5051 for (; *I != AssignedReg; ++I)
5052 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005054 // Already added the first reg.
5055 --NumRegs; ++I;
5056 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005057 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005058 Regs.push_back(*I);
5059 }
5060 }
5061 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5062 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5063 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5064 return;
5065 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067 // Otherwise, if this was a reference to an LLVM register class, create vregs
5068 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005069 if (const TargetRegisterClass *RC = PhysReg.second) {
5070 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005071 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005072 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005073
Evan Chengfb112882009-03-23 08:01:15 +00005074 // Create the appropriate number of virtual registers.
5075 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5076 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005077 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005078
Evan Chengfb112882009-03-23 08:01:15 +00005079 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5080 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005082
5083 // This is a reference to a register class that doesn't directly correspond
5084 // to an LLVM register class. Allocate NumRegs consecutive, available,
5085 // registers from the class.
5086 std::vector<unsigned> RegClassRegs
5087 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5088 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005089
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005090 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5091 unsigned NumAllocated = 0;
5092 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5093 unsigned Reg = RegClassRegs[i];
5094 // See if this register is available.
5095 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5096 (isInReg && InputRegs.count(Reg))) { // Already used.
5097 // Make sure we find consecutive registers.
5098 NumAllocated = 0;
5099 continue;
5100 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005102 // Check to see if this register is allocatable (i.e. don't give out the
5103 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005104 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5105 if (!RC) { // Couldn't allocate this register.
5106 // Reset NumAllocated to make sure we return consecutive registers.
5107 NumAllocated = 0;
5108 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005109 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005110
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005111 // Okay, this register is good, we can use it.
5112 ++NumAllocated;
5113
5114 // If we allocated enough consecutive registers, succeed.
5115 if (NumAllocated == NumRegs) {
5116 unsigned RegStart = (i-NumAllocated)+1;
5117 unsigned RegEnd = i+1;
5118 // Mark all of the allocated registers used.
5119 for (unsigned i = RegStart; i != RegEnd; ++i)
5120 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005121
5122 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005123 OpInfo.ConstraintVT);
5124 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5125 return;
5126 }
5127 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005129 // Otherwise, we couldn't allocate enough registers for this.
5130}
5131
Evan Chengda43bcf2008-09-24 00:05:32 +00005132/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5133/// processed uses a memory 'm' constraint.
5134static bool
5135hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005136 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005137 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5138 InlineAsm::ConstraintInfo &CI = CInfos[i];
5139 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5140 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5141 if (CType == TargetLowering::C_Memory)
5142 return true;
5143 }
Chris Lattner6c147292009-04-30 00:48:50 +00005144
5145 // Indirect operand accesses access memory.
5146 if (CI.isIndirect)
5147 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005148 }
5149
5150 return false;
5151}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005152
5153/// visitInlineAsm - Handle a call to an InlineAsm object.
5154///
5155void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
5156 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5157
5158 /// ConstraintOperands - Information about all of the constraints.
5159 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005160
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005161 std::set<unsigned> OutputRegs, InputRegs;
5162
5163 // Do a prepass over the constraints, canonicalizing them, and building up the
5164 // ConstraintOperands list.
5165 std::vector<InlineAsm::ConstraintInfo>
5166 ConstraintInfos = IA->ParseConstraints();
5167
Evan Chengda43bcf2008-09-24 00:05:32 +00005168 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005169
5170 SDValue Chain, Flag;
5171
5172 // We won't need to flush pending loads if this asm doesn't touch
5173 // memory and is nonvolatile.
5174 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005175 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005176 else
5177 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005178
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005179 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5180 unsigned ResNo = 0; // ResNo - The result number of the next output.
5181 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5182 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5183 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005184
Owen Anderson825b72b2009-08-11 20:47:22 +00005185 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005186
5187 // Compute the value type for each operand.
5188 switch (OpInfo.Type) {
5189 case InlineAsm::isOutput:
5190 // Indirect outputs just consume an argument.
5191 if (OpInfo.isIndirect) {
5192 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5193 break;
5194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005196 // The return value of the call is this value. As such, there is no
5197 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005198 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5199 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005200 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5201 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5202 } else {
5203 assert(ResNo == 0 && "Asm only has one result!");
5204 OpVT = TLI.getValueType(CS.getType());
5205 }
5206 ++ResNo;
5207 break;
5208 case InlineAsm::isInput:
5209 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5210 break;
5211 case InlineAsm::isClobber:
5212 // Nothing to do.
5213 break;
5214 }
5215
5216 // If this is an input or an indirect output, process the call argument.
5217 // BasicBlocks are labels, currently appearing only in asm's.
5218 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005219 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005220 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5221
Chris Lattner81249c92008-10-17 17:05:25 +00005222 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005223 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005224 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005225 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005226 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005227
Owen Anderson1d0be152009-08-13 21:58:54 +00005228 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005229 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005230
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005231 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005232 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005233
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005234 // Second pass over the constraints: compute which constraint option to use
5235 // and assign registers to constraints that want a specific physreg.
5236 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5237 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005238
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005239 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005240 // matching input. If their types mismatch, e.g. one is an integer, the
5241 // other is floating point, or their sizes are different, flag it as an
5242 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005243 if (OpInfo.hasMatchingInput()) {
5244 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5245 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005246 if ((OpInfo.ConstraintVT.isInteger() !=
5247 Input.ConstraintVT.isInteger()) ||
5248 (OpInfo.ConstraintVT.getSizeInBits() !=
5249 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005250 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005251 " with a matching output constraint of incompatible"
5252 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005253 }
5254 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005255 }
5256 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005259 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261 // If this is a memory input, and if the operand is not indirect, do what we
5262 // need to to provide an address for the memory input.
5263 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5264 !OpInfo.isIndirect) {
5265 assert(OpInfo.Type == InlineAsm::isInput &&
5266 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005267
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005268 // Memory operands really want the address of the value. If we don't have
5269 // an indirect input, put it in the constpool if we can, otherwise spill
5270 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005271
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005272 // If the operand is a float, integer, or vector constant, spill to a
5273 // constant pool entry to get its address.
5274 Value *OpVal = OpInfo.CallOperandVal;
5275 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5276 isa<ConstantVector>(OpVal)) {
5277 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5278 TLI.getPointerTy());
5279 } else {
5280 // Otherwise, create a stack slot and emit a store to it before the
5281 // asm.
5282 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005283 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005284 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5285 MachineFunction &MF = DAG.getMachineFunction();
5286 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
5287 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005288 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005289 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005290 OpInfo.CallOperand = StackSlot;
5291 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005292
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005293 // There is no longer a Value* corresponding to this operand.
5294 OpInfo.CallOperandVal = 0;
5295 // It is now an indirect operand.
5296 OpInfo.isIndirect = true;
5297 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005298
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005299 // If this constraint is for a specific register, allocate it before
5300 // anything else.
5301 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005302 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005303 }
5304 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005305
5306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005307 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005308 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005309 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5310 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005311
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 // C_Register operands have already been allocated, Other/Memory don't need
5313 // to be.
5314 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005315 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005316 }
5317
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005318 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5319 std::vector<SDValue> AsmNodeOperands;
5320 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5321 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005322 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005323
5324
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325 // Loop over all of the inputs, copying the operand values into the
5326 // appropriate registers and processing the output regs.
5327 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005328
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005329 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5330 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005331
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005332 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5333 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5334
5335 switch (OpInfo.Type) {
5336 case InlineAsm::isOutput: {
5337 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5338 OpInfo.ConstraintType != TargetLowering::C_Register) {
5339 // Memory output, or 'other' output (e.g. 'X' constraint).
5340 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5341
5342 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005343 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5344 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005345 TLI.getPointerTy()));
5346 AsmNodeOperands.push_back(OpInfo.CallOperand);
5347 break;
5348 }
5349
5350 // Otherwise, this is a register or register class output.
5351
5352 // Copy the output from the appropriate register. Find a register that
5353 // we can use.
5354 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005355 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005356 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005357 }
5358
5359 // If this is an indirect operand, store through the pointer after the
5360 // asm.
5361 if (OpInfo.isIndirect) {
5362 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5363 OpInfo.CallOperandVal));
5364 } else {
5365 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005366 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5367 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005368 // Concatenate this output onto the outputs list.
5369 RetValRegs.append(OpInfo.AssignedRegs);
5370 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005371
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005372 // Add information to the INLINEASM node to know that this register is
5373 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005374 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5375 6 /* EARLYCLOBBER REGDEF */ :
5376 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005377 false,
5378 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005379 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005380 break;
5381 }
5382 case InlineAsm::isInput: {
5383 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005384
Chris Lattner6bdcda32008-10-17 16:47:46 +00005385 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005386 // If this is required to match an output register we have already set,
5387 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005388 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005389
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005390 // Scan until we find the definition we already emitted of this operand.
5391 // When we find it, create a RegsForValue operand.
5392 unsigned CurOp = 2; // The first operand.
5393 for (; OperandNo; --OperandNo) {
5394 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005395 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005396 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005397 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5398 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5399 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005401 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005402 }
5403
Evan Cheng697cbbf2009-03-20 18:03:34 +00005404 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005405 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005406 if ((OpFlag & 7) == 2 /*REGDEF*/
5407 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5408 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005409 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005410 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005411 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005412 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005413 RegsForValue MatchedRegs;
5414 MatchedRegs.TLI = &TLI;
5415 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005416 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005417 MatchedRegs.RegVTs.push_back(RegVT);
5418 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005419 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005420 i != e; ++i)
5421 MatchedRegs.Regs.
5422 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005423
5424 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005425 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5426 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005427 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5428 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005429 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005430 break;
5431 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005432 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5433 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5434 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005435 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005436 // See InlineAsm.h isUseOperandTiedToDef.
5437 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005438 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005439 TLI.getPointerTy()));
5440 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5441 break;
5442 }
5443 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005444
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005445 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005446 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005447 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005448
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005449 std::vector<SDValue> Ops;
5450 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005451 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005452 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005453 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005454 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005455 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005456
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005457 // Add information to the INLINEASM node to know about this input.
5458 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005459 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005460 TLI.getPointerTy()));
5461 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5462 break;
5463 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5464 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5465 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5466 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005467
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005469 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5470 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005471 TLI.getPointerTy()));
5472 AsmNodeOperands.push_back(InOperandVal);
5473 break;
5474 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005475
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005476 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5477 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5478 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005479 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005480 "Don't know how to handle indirect register inputs yet!");
5481
5482 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005483 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005484 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005485 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005486 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005487
Dale Johannesen66978ee2009-01-31 02:22:37 +00005488 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5489 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005490
Evan Cheng697cbbf2009-03-20 18:03:34 +00005491 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005492 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005493 break;
5494 }
5495 case InlineAsm::isClobber: {
5496 // Add the clobbered value to the operand list, so that the register
5497 // allocator is aware that the physreg got clobbered.
5498 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005499 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005500 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005501 break;
5502 }
5503 }
5504 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005506 // Finish up input operands.
5507 AsmNodeOperands[0] = Chain;
5508 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005509
Dale Johannesen66978ee2009-01-31 02:22:37 +00005510 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005511 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005512 &AsmNodeOperands[0], AsmNodeOperands.size());
5513 Flag = Chain.getValue(1);
5514
5515 // If this asm returns a register value, copy the result from that register
5516 // and set it as the value of the call.
5517 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005518 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005519 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005520
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005521 // FIXME: Why don't we do this for inline asms with MRVs?
5522 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005523 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005524
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005525 // If any of the results of the inline asm is a vector, it may have the
5526 // wrong width/num elts. This can happen for register classes that can
5527 // contain multiple different value types. The preg or vreg allocated may
5528 // not have the same VT as was expected. Convert it to the right type
5529 // with bit_convert.
5530 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005531 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005532 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005533
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005534 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005535 ResultType.isInteger() && Val.getValueType().isInteger()) {
5536 // If a result value was tied to an input value, the computed result may
5537 // have a wider width than the expected result. Extract the relevant
5538 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005539 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005540 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005541
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005542 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005543 }
Dan Gohman95915732008-10-18 01:03:45 +00005544
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005545 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005546 // Don't need to use this as a chain in this case.
5547 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5548 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005549 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005550
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005551 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005552
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005553 // Process indirect outputs, first output all of the flagged copies out of
5554 // physregs.
5555 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5556 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5557 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005558 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5559 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005560 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005561
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005562 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005563
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005564 // Emit the non-flagged stores from the physregs.
5565 SmallVector<SDValue, 8> OutChains;
5566 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005567 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005568 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005569 getValue(StoresToEmit[i].second),
5570 StoresToEmit[i].second, 0));
5571 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005573 &OutChains[0], OutChains.size());
5574 DAG.setRoot(Chain);
5575}
5576
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005577void SelectionDAGLowering::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005578 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005579 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005580 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005581 DAG.getSrcValue(I.getOperand(1))));
5582}
5583
5584void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005585 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5586 getRoot(), getValue(I.getOperand(0)),
5587 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005588 setValue(&I, V);
5589 DAG.setRoot(V.getValue(1));
5590}
5591
5592void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005593 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005594 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005595 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005596 DAG.getSrcValue(I.getOperand(1))));
5597}
5598
5599void SelectionDAGLowering::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005600 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005601 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005602 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005603 getValue(I.getOperand(2)),
5604 DAG.getSrcValue(I.getOperand(1)),
5605 DAG.getSrcValue(I.getOperand(2))));
5606}
5607
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005608/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005609/// implementation, which just calls LowerCall.
5610/// FIXME: When all targets are
5611/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005612std::pair<SDValue, SDValue>
5613TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5614 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005615 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005616 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005617 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005618 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005619 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005620
Dan Gohman1937e2f2008-09-16 01:42:28 +00005621 assert((!isTailCall || PerformTailCallOpt) &&
5622 "isTailCall set when tail-call optimizations are disabled!");
5623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005624 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005625 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005627 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005628 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5629 for (unsigned Value = 0, NumValues = ValueVTs.size();
5630 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005631 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005632 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005633 SDValue Op = SDValue(Args[i].Node.getNode(),
5634 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005635 ISD::ArgFlagsTy Flags;
5636 unsigned OriginalAlignment =
5637 getTargetData()->getABITypeAlignment(ArgTy);
5638
5639 if (Args[i].isZExt)
5640 Flags.setZExt();
5641 if (Args[i].isSExt)
5642 Flags.setSExt();
5643 if (Args[i].isInReg)
5644 Flags.setInReg();
5645 if (Args[i].isSRet)
5646 Flags.setSRet();
5647 if (Args[i].isByVal) {
5648 Flags.setByVal();
5649 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5650 const Type *ElementTy = Ty->getElementType();
5651 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005652 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005653 // For ByVal, alignment should come from FE. BE will guess if this
5654 // info is not there but there are cases it cannot get right.
5655 if (Args[i].Alignment)
5656 FrameAlign = Args[i].Alignment;
5657 Flags.setByValAlign(FrameAlign);
5658 Flags.setByValSize(FrameSize);
5659 }
5660 if (Args[i].isNest)
5661 Flags.setNest();
5662 Flags.setOrigAlign(OriginalAlignment);
5663
Owen Anderson23b9b192009-08-12 00:36:31 +00005664 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5665 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005666 SmallVector<SDValue, 4> Parts(NumParts);
5667 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5668
5669 if (Args[i].isSExt)
5670 ExtendKind = ISD::SIGN_EXTEND;
5671 else if (Args[i].isZExt)
5672 ExtendKind = ISD::ZERO_EXTEND;
5673
Dale Johannesen66978ee2009-01-31 02:22:37 +00005674 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005675
Dan Gohman98ca4f22009-08-05 01:29:28 +00005676 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005678 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5679 if (NumParts > 1 && j == 0)
5680 MyFlags.Flags.setSplit();
5681 else if (j != 0)
5682 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005683
Dan Gohman98ca4f22009-08-05 01:29:28 +00005684 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005685 }
5686 }
5687 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005688
Dan Gohman98ca4f22009-08-05 01:29:28 +00005689 // Handle the incoming return values from the call.
5690 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005691 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005692 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005693 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005694 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005695 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5696 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005697 for (unsigned i = 0; i != NumRegs; ++i) {
5698 ISD::InputArg MyFlags;
5699 MyFlags.VT = RegisterVT;
5700 MyFlags.Used = isReturnValueUsed;
5701 if (RetSExt)
5702 MyFlags.Flags.setSExt();
5703 if (RetZExt)
5704 MyFlags.Flags.setZExt();
5705 if (isInreg)
5706 MyFlags.Flags.setInReg();
5707 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005708 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709 }
5710
Dan Gohman98ca4f22009-08-05 01:29:28 +00005711 // Check if target-dependent constraints permit a tail call here.
5712 // Target-independent constraints should be checked by the caller.
5713 if (isTailCall &&
5714 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
5715 isTailCall = false;
5716
5717 SmallVector<SDValue, 4> InVals;
5718 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
5719 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005720
5721 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005722 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005723 "LowerCall didn't return a valid chain!");
5724 assert((!isTailCall || InVals.empty()) &&
5725 "LowerCall emitted a return value for a tail call!");
5726 assert((isTailCall || InVals.size() == Ins.size()) &&
5727 "LowerCall didn't emit the correct number of values!");
5728 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5729 assert(InVals[i].getNode() &&
5730 "LowerCall emitted a null value!");
5731 assert(Ins[i].VT == InVals[i].getValueType() &&
5732 "LowerCall emitted a value with the wrong type!");
5733 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00005734
5735 // For a tail call, the return value is merely live-out and there aren't
5736 // any nodes in the DAG representing it. Return a special value to
5737 // indicate that a tail call has been emitted and no more Instructions
5738 // should be processed in the current block.
5739 if (isTailCall) {
5740 DAG.setRoot(Chain);
5741 return std::make_pair(SDValue(), SDValue());
5742 }
5743
5744 // Collect the legal value parts into potentially illegal values
5745 // that correspond to the original function's return values.
5746 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5747 if (RetSExt)
5748 AssertOp = ISD::AssertSext;
5749 else if (RetZExt)
5750 AssertOp = ISD::AssertZext;
5751 SmallVector<SDValue, 4> ReturnValues;
5752 unsigned CurReg = 0;
5753 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005754 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005755 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5756 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005757
5758 SDValue ReturnValue =
5759 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
5760 AssertOp);
5761 ReturnValues.push_back(ReturnValue);
5762 CurReg += NumRegs;
5763 }
5764
5765 // For a function returning void, there is no return value. We can't create
5766 // such a node, so we just return a null return value in that case. In
5767 // that case, nothing will actualy look at the value.
5768 if (ReturnValues.empty())
5769 return std::make_pair(SDValue(), Chain);
5770
5771 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5772 DAG.getVTList(&RetTys[0], RetTys.size()),
5773 &ReturnValues[0], ReturnValues.size());
5774
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005775 return std::make_pair(Res, Chain);
5776}
5777
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005778void TargetLowering::LowerOperationWrapper(SDNode *N,
5779 SmallVectorImpl<SDValue> &Results,
5780 SelectionDAG &DAG) {
5781 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005782 if (Res.getNode())
5783 Results.push_back(Res);
5784}
5785
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005786SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005787 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005788 return SDValue();
5789}
5790
5791
5792void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
5793 SDValue Op = getValue(V);
5794 assert((Op.getOpcode() != ISD::CopyFromReg ||
5795 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5796 "Copy from a reg to the same reg!");
5797 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5798
Owen Anderson23b9b192009-08-12 00:36:31 +00005799 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005800 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005801 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 PendingExports.push_back(Chain);
5803}
5804
5805#include "llvm/CodeGen/SelectionDAGISel.h"
5806
Dan Gohman8c2b5252009-10-30 01:27:03 +00005807void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808 // If this is the entry block, emit arguments.
5809 Function &F = *LLVMBB->getParent();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005810 SelectionDAG &DAG = SDL->DAG;
5811 SDValue OldRoot = DAG.getRoot();
5812 DebugLoc dl = SDL->getCurDebugLoc();
5813 const TargetData *TD = TLI.getTargetData();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005814
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005815 // Check whether the function can return without sret-demotion.
5816 SmallVector<EVT, 4> OutVTs;
5817 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
5818 getReturnInfo(&F, OutVTs, OutsFlags, TLI);
5819 // For now, assert and bail out if it can't.
5820 assert(TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), OutVTs, OutsFlags,
5821 DAG) && "Cannot fit return value in registers!");
5822
Dan Gohman98ca4f22009-08-05 01:29:28 +00005823 // Set up the incoming argument description vector.
5824 SmallVector<ISD::InputArg, 16> Ins;
5825 unsigned Idx = 1;
5826 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5827 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005828 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005829 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5830 bool isArgValueUsed = !I->use_empty();
5831 for (unsigned Value = 0, NumValues = ValueVTs.size();
5832 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005833 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005834 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005835 ISD::ArgFlagsTy Flags;
5836 unsigned OriginalAlignment =
5837 TD->getABITypeAlignment(ArgTy);
5838
5839 if (F.paramHasAttr(Idx, Attribute::ZExt))
5840 Flags.setZExt();
5841 if (F.paramHasAttr(Idx, Attribute::SExt))
5842 Flags.setSExt();
5843 if (F.paramHasAttr(Idx, Attribute::InReg))
5844 Flags.setInReg();
5845 if (F.paramHasAttr(Idx, Attribute::StructRet))
5846 Flags.setSRet();
5847 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5848 Flags.setByVal();
5849 const PointerType *Ty = cast<PointerType>(I->getType());
5850 const Type *ElementTy = Ty->getElementType();
5851 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5852 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5853 // For ByVal, alignment should be passed from FE. BE will guess if
5854 // this info is not there but there are cases it cannot get right.
5855 if (F.getParamAlignment(Idx))
5856 FrameAlign = F.getParamAlignment(Idx);
5857 Flags.setByValAlign(FrameAlign);
5858 Flags.setByValSize(FrameSize);
5859 }
5860 if (F.paramHasAttr(Idx, Attribute::Nest))
5861 Flags.setNest();
5862 Flags.setOrigAlign(OriginalAlignment);
5863
Owen Anderson23b9b192009-08-12 00:36:31 +00005864 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5865 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005866 for (unsigned i = 0; i != NumRegs; ++i) {
5867 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5868 if (NumRegs > 1 && i == 0)
5869 MyFlags.Flags.setSplit();
5870 // if it isn't first piece, alignment must be 1
5871 else if (i > 0)
5872 MyFlags.Flags.setOrigAlign(1);
5873 Ins.push_back(MyFlags);
5874 }
5875 }
5876 }
5877
5878 // Call the target to set up the argument values.
5879 SmallVector<SDValue, 8> InVals;
5880 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5881 F.isVarArg(), Ins,
5882 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005883
5884 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005885 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005886 "LowerFormalArguments didn't return a valid chain!");
5887 assert(InVals.size() == Ins.size() &&
5888 "LowerFormalArguments didn't emit the correct number of values!");
5889 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5890 assert(InVals[i].getNode() &&
5891 "LowerFormalArguments emitted a null value!");
5892 assert(Ins[i].VT == InVals[i].getValueType() &&
5893 "LowerFormalArguments emitted a value with the wrong type!");
5894 });
5895
5896 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005897 DAG.setRoot(NewRoot);
5898
5899 // Set up the argument values.
5900 unsigned i = 0;
5901 Idx = 1;
5902 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5903 ++I, ++Idx) {
5904 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005905 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005906 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005907 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005908 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005909 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005910 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5911 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005912
5913 if (!I->use_empty()) {
5914 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5915 if (F.paramHasAttr(Idx, Attribute::SExt))
5916 AssertOp = ISD::AssertSext;
5917 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5918 AssertOp = ISD::AssertZext;
5919
5920 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
5921 PartVT, VT, AssertOp));
5922 }
5923 i += NumParts;
5924 }
5925 if (!I->use_empty()) {
5926 SDL->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
5927 SDL->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005928 // If this argument is live outside of the entry block, insert a copy from
5929 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005930 SDL->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005931 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005932 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005933 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934
5935 // Finally, if the target has anything special to do, allow it to do so.
5936 // FIXME: this should insert code into the DAG!
5937 EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction());
5938}
5939
5940/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5941/// ensure constants are generated when needed. Remember the virtual registers
5942/// that need to be added to the Machine PHI nodes as input. We cannot just
5943/// directly add them, because expansion might result in multiple MBB's for one
5944/// BB. As such, the start of the BB might correspond to a different MBB than
5945/// the end.
5946///
5947void
5948SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5949 TerminatorInst *TI = LLVMBB->getTerminator();
5950
5951 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5952
5953 // Check successor nodes' PHI nodes that expect a constant to be available
5954 // from this block.
5955 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5956 BasicBlock *SuccBB = TI->getSuccessor(succ);
5957 if (!isa<PHINode>(SuccBB->begin())) continue;
5958 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005959
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005960 // If this terminator has multiple identical successors (common for
5961 // switches), only handle each succ once.
5962 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005963
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005964 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5965 PHINode *PN;
5966
5967 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5968 // nodes and Machine PHI nodes, but the incoming operands have not been
5969 // emitted yet.
5970 for (BasicBlock::iterator I = SuccBB->begin();
5971 (PN = dyn_cast<PHINode>(I)); ++I) {
5972 // Ignore dead phi's.
5973 if (PN->use_empty()) continue;
5974
5975 unsigned Reg;
5976 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5977
5978 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
5979 unsigned &RegOut = SDL->ConstantsOut[C];
5980 if (RegOut == 0) {
5981 RegOut = FuncInfo->CreateRegForValue(C);
5982 SDL->CopyValueToVirtualRegister(C, RegOut);
5983 }
5984 Reg = RegOut;
5985 } else {
5986 Reg = FuncInfo->ValueMap[PHIOp];
5987 if (Reg == 0) {
5988 assert(isa<AllocaInst>(PHIOp) &&
5989 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5990 "Didn't codegen value into a register!??");
5991 Reg = FuncInfo->CreateRegForValue(PHIOp);
5992 SDL->CopyValueToVirtualRegister(PHIOp, Reg);
5993 }
5994 }
5995
5996 // Remember that this register needs to added to the machine PHI node as
5997 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00005998 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005999 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6000 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006001 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006002 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
6004 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
6005 Reg += NumRegisters;
6006 }
6007 }
6008 }
6009 SDL->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006010}
6011
Dan Gohman3df24e62008-09-03 23:12:08 +00006012/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6013/// supports legal types, and it emits MachineInstrs directly instead of
6014/// creating SelectionDAG nodes.
6015///
6016bool
6017SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6018 FastISel *F) {
6019 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006020
Dan Gohman3df24e62008-09-03 23:12:08 +00006021 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6022 unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size();
6023
6024 // Check successor nodes' PHI nodes that expect a constant to be available
6025 // from this block.
6026 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6027 BasicBlock *SuccBB = TI->getSuccessor(succ);
6028 if (!isa<PHINode>(SuccBB->begin())) continue;
6029 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006030
Dan Gohman3df24e62008-09-03 23:12:08 +00006031 // If this terminator has multiple identical successors (common for
6032 // switches), only handle each succ once.
6033 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006034
Dan Gohman3df24e62008-09-03 23:12:08 +00006035 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6036 PHINode *PN;
6037
6038 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6039 // nodes and Machine PHI nodes, but the incoming operands have not been
6040 // emitted yet.
6041 for (BasicBlock::iterator I = SuccBB->begin();
6042 (PN = dyn_cast<PHINode>(I)); ++I) {
6043 // Ignore dead phi's.
6044 if (PN->use_empty()) continue;
6045
6046 // Only handle legal types. Two interesting things to note here. First,
6047 // by bailing out early, we may leave behind some dead instructions,
6048 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6049 // own moves. Second, this check is necessary becuase FastISel doesn't
6050 // use CreateRegForValue to create registers, so it always creates
6051 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006052 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006053 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6054 // Promote MVT::i1.
6055 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006056 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006057 else {
6058 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
6059 return false;
6060 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006061 }
6062
6063 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6064
6065 unsigned Reg = F->getRegForValue(PHIOp);
6066 if (Reg == 0) {
6067 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
6068 return false;
6069 }
6070 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
6071 }
6072 }
6073
6074 return true;
6075}