blob: 3161bd090c75000e646b1f8c0dde32f40bcd5cf1 [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"
Devang Patel53bb5c92009-11-10 23:06:00 +000029#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000030#include "llvm/Module.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000031#include "llvm/CodeGen/FastISel.h"
32#include "llvm/CodeGen/GCStrategy.h"
33#include "llvm/CodeGen/GCMetadata.h"
34#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
37#include "llvm/CodeGen/MachineJumpTableInfo.h"
38#include "llvm/CodeGen/MachineModuleInfo.h"
39#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000040#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000041#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000042#include "llvm/CodeGen/DwarfWriter.h"
43#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044#include "llvm/Target/TargetRegisterInfo.h"
45#include "llvm/Target/TargetData.h"
46#include "llvm/Target/TargetFrameInfo.h"
47#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000048#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000049#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000050#include "llvm/Target/TargetOptions.h"
51#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000052#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000053#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000054#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000056#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057#include <algorithm>
58using namespace llvm;
59
Dale Johannesen601d3c02008-09-05 01:48:15 +000060/// LimitFloatPrecision - Generate low-precision inline sequences for
61/// some float libcalls (6, 8 or 12 bits).
62static unsigned LimitFloatPrecision;
63
64static cl::opt<unsigned, true>
65LimitFPPrecision("limit-float-precision",
66 cl::desc("Generate low-precision inline sequences "
67 "for some float libcalls"),
68 cl::location(LimitFloatPrecision),
69 cl::init(0));
70
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000071/// ComputeLinearIndex - Given an LLVM IR aggregate type and a sequence
Dan Gohman2c91d102009-01-06 22:53:52 +000072/// of insertvalue or extractvalue indices that identify a member, return
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000073/// the linearized index of the start of the member.
74///
75static unsigned ComputeLinearIndex(const TargetLowering &TLI, const Type *Ty,
76 const unsigned *Indices,
77 const unsigned *IndicesEnd,
78 unsigned CurIndex = 0) {
79 // Base case: We're done.
80 if (Indices && Indices == IndicesEnd)
81 return CurIndex;
82
83 // Given a struct type, recursively traverse the elements.
84 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
85 for (StructType::element_iterator EB = STy->element_begin(),
86 EI = EB,
87 EE = STy->element_end();
88 EI != EE; ++EI) {
89 if (Indices && *Indices == unsigned(EI - EB))
90 return ComputeLinearIndex(TLI, *EI, Indices+1, IndicesEnd, CurIndex);
91 CurIndex = ComputeLinearIndex(TLI, *EI, 0, 0, CurIndex);
92 }
Dan Gohman2c91d102009-01-06 22:53:52 +000093 return CurIndex;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000094 }
95 // Given an array type, recursively traverse the elements.
96 else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
97 const Type *EltTy = ATy->getElementType();
98 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i) {
99 if (Indices && *Indices == i)
100 return ComputeLinearIndex(TLI, EltTy, Indices+1, IndicesEnd, CurIndex);
101 CurIndex = ComputeLinearIndex(TLI, EltTy, 0, 0, CurIndex);
102 }
Dan Gohman2c91d102009-01-06 22:53:52 +0000103 return CurIndex;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000104 }
105 // We haven't found the type we're looking for, so keep searching.
106 return CurIndex + 1;
107}
108
109/// ComputeValueVTs - Given an LLVM IR type, compute a sequence of
Owen Andersone50ed302009-08-10 22:56:29 +0000110/// EVTs that represent all the individual underlying
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111/// non-aggregate types that comprise it.
112///
113/// If Offsets is non-null, it points to a vector to be filled in
114/// with the in-memory offsets of each of the individual values.
115///
116static void ComputeValueVTs(const TargetLowering &TLI, const Type *Ty,
Owen Andersone50ed302009-08-10 22:56:29 +0000117 SmallVectorImpl<EVT> &ValueVTs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000118 SmallVectorImpl<uint64_t> *Offsets = 0,
119 uint64_t StartingOffset = 0) {
120 // Given a struct type, recursively traverse the elements.
121 if (const StructType *STy = dyn_cast<StructType>(Ty)) {
122 const StructLayout *SL = TLI.getTargetData()->getStructLayout(STy);
123 for (StructType::element_iterator EB = STy->element_begin(),
124 EI = EB,
125 EE = STy->element_end();
126 EI != EE; ++EI)
127 ComputeValueVTs(TLI, *EI, ValueVTs, Offsets,
128 StartingOffset + SL->getElementOffset(EI - EB));
129 return;
130 }
131 // Given an array type, recursively traverse the elements.
132 if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
133 const Type *EltTy = ATy->getElementType();
Duncan Sands777d2302009-05-09 07:06:46 +0000134 uint64_t EltSize = TLI.getTargetData()->getTypeAllocSize(EltTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000135 for (unsigned i = 0, e = ATy->getNumElements(); i != e; ++i)
136 ComputeValueVTs(TLI, EltTy, ValueVTs, Offsets,
137 StartingOffset + i * EltSize);
138 return;
139 }
Dan Gohman5e5558b2009-04-23 22:50:03 +0000140 // Interpret void as zero return values.
Owen Anderson1d0be152009-08-13 21:58:54 +0000141 if (Ty == Type::getVoidTy(Ty->getContext()))
Dan Gohman5e5558b2009-04-23 22:50:03 +0000142 return;
Owen Andersone50ed302009-08-10 22:56:29 +0000143 // Base case: we can get an EVT for this LLVM IR type.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000144 ValueVTs.push_back(TLI.getValueType(Ty));
145 if (Offsets)
146 Offsets->push_back(StartingOffset);
147}
148
Dan Gohman2a7c6712008-09-03 23:18:39 +0000149namespace llvm {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000150 /// RegsForValue - This struct represents the registers (physical or virtual)
151 /// that a particular set of values is assigned, and the type information about
152 /// the value. The most common situation is to represent one value at a time,
153 /// but struct or array values are handled element-wise as multiple values.
154 /// The splitting of aggregates is performed recursively, so that we never
155 /// have aggregate-typed registers. The values at this point do not necessarily
156 /// have legal types, so each value may require one or more registers of some
157 /// legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000158 ///
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000159 struct VISIBILITY_HIDDEN RegsForValue {
160 /// TLI - The TargetLowering object.
161 ///
162 const TargetLowering *TLI;
163
164 /// ValueVTs - The value types of the values, which may not be legal, and
165 /// may need be promoted or synthesized from one or more registers.
166 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000167 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000169 /// RegVTs - The value types of the registers. This is the same size as
170 /// ValueVTs and it records, for each value, what the type of the assigned
171 /// register or registers are. (Individual values are never synthesized
172 /// from more than one type of register.)
173 ///
174 /// With virtual registers, the contents of RegVTs is redundant with TLI's
175 /// getRegisterType member function, however when with physical registers
176 /// it is necessary to have a separate record of the types.
177 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000178 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000180 /// Regs - This list holds the registers assigned to the values.
181 /// Each legal or promoted value requires one register, and each
182 /// expanded value requires multiple registers.
183 ///
184 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000186 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000187
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000188 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000189 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000190 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000191 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
192 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000193 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000194 const SmallVector<EVT, 4> &regvts,
195 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000196 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000197 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000198 unsigned Reg, const Type *Ty) : TLI(&tli) {
199 ComputeValueVTs(tli, Ty, ValueVTs);
200
201 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000202 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000203 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
204 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000205 for (unsigned i = 0; i != NumRegs; ++i)
206 Regs.push_back(Reg + i);
207 RegVTs.push_back(RegisterVT);
208 Reg += NumRegs;
209 }
210 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000211
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000212 /// append - Add the specified values to this one.
213 void append(const RegsForValue &RHS) {
214 TLI = RHS.TLI;
215 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
216 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
217 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
218 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000219
220
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000221 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000222 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000223 /// Chain/Flag as the input and updates them for the output Chain/Flag.
224 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000225 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000226 SDValue &Chain, SDValue *Flag) const;
227
228 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000229 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000230 /// Chain/Flag as the input and updates them for the output Chain/Flag.
231 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000232 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000233 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000234
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000235 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000236 /// operand list. This adds the code marker, matching input operand index
237 /// (if applicable), and includes the number of values added into it.
238 void AddInlineAsmOperands(unsigned Code,
239 bool HasMatching, unsigned MatchingIdx,
240 SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000241 };
242}
243
244/// isUsedOutsideOfDefiningBlock - Return true if this instruction is used by
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000245/// PHI nodes or outside of the basic block that defines it, or used by a
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000246/// switch or atomic instruction, which may expand to multiple basic blocks.
247static bool isUsedOutsideOfDefiningBlock(Instruction *I) {
248 if (isa<PHINode>(I)) return true;
249 BasicBlock *BB = I->getParent();
250 for (Value::use_iterator UI = I->use_begin(), E = I->use_end(); UI != E; ++UI)
Dan Gohman8e5c0da2009-04-09 02:33:36 +0000251 if (cast<Instruction>(*UI)->getParent() != BB || isa<PHINode>(*UI))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000252 return true;
253 return false;
254}
255
256/// isOnlyUsedInEntryBlock - If the specified argument is only used in the
257/// entry block, return true. This includes arguments used by switches, since
258/// the switch may expand into multiple basic blocks.
259static bool isOnlyUsedInEntryBlock(Argument *A, bool EnableFastISel) {
260 // With FastISel active, we may be splitting blocks, so force creation
261 // of virtual registers for all non-dead arguments.
Dan Gohman33134c42008-09-25 17:05:24 +0000262 // Don't force virtual registers for byval arguments though, because
263 // fast-isel can't handle those in all cases.
264 if (EnableFastISel && !A->hasByValAttr())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000265 return A->use_empty();
266
267 BasicBlock *Entry = A->getParent()->begin();
268 for (Value::use_iterator UI = A->use_begin(), E = A->use_end(); UI != E; ++UI)
269 if (cast<Instruction>(*UI)->getParent() != Entry || isa<SwitchInst>(*UI))
270 return false; // Use not in entry block.
271 return true;
272}
273
274FunctionLoweringInfo::FunctionLoweringInfo(TargetLowering &tli)
275 : TLI(tli) {
276}
277
278void FunctionLoweringInfo::set(Function &fn, MachineFunction &mf,
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000279 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000280 bool EnableFastISel) {
281 Fn = &fn;
282 MF = &mf;
283 RegInfo = &MF->getRegInfo();
284
285 // Create a vreg for each argument register that is not dead and is used
286 // outside of the entry block for the function.
287 for (Function::arg_iterator AI = Fn->arg_begin(), E = Fn->arg_end();
288 AI != E; ++AI)
289 if (!isOnlyUsedInEntryBlock(AI, EnableFastISel))
290 InitializeRegForValue(AI);
291
292 // Initialize the mapping of values to registers. This is only set up for
293 // instruction values that are used outside of the block that defines
294 // them.
295 Function::iterator BB = Fn->begin(), EB = Fn->end();
296 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
297 if (AllocaInst *AI = dyn_cast<AllocaInst>(I))
298 if (ConstantInt *CUI = dyn_cast<ConstantInt>(AI->getArraySize())) {
299 const Type *Ty = AI->getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +0000300 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000301 unsigned Align =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000302 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
303 AI->getAlignment());
304
305 TySize *= CUI->getZExtValue(); // Get total allocated size.
306 if (TySize == 0) TySize = 1; // Don't create zero-sized stack objects.
307 StaticAllocaMap[AI] =
308 MF->getFrameInfo()->CreateStackObject(TySize, Align);
309 }
310
311 for (; BB != EB; ++BB)
312 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
313 if (!I->use_empty() && isUsedOutsideOfDefiningBlock(I))
314 if (!isa<AllocaInst>(I) ||
315 !StaticAllocaMap.count(cast<AllocaInst>(I)))
316 InitializeRegForValue(I);
317
318 // Create an initial MachineBasicBlock for each LLVM BasicBlock in F. This
319 // also creates the initial PHI MachineInstrs, though none of the input
320 // operands are populated.
321 for (BB = Fn->begin(), EB = Fn->end(); BB != EB; ++BB) {
322 MachineBasicBlock *MBB = mf.CreateMachineBasicBlock(BB);
323 MBBMap[BB] = MBB;
324 MF->push_back(MBB);
325
Dan Gohman8c2b5252009-10-30 01:27:03 +0000326 // Transfer the address-taken flag. This is necessary because there could
327 // be multiple MachineBasicBlocks corresponding to one BasicBlock, and only
328 // the first one should be marked.
329 if (BB->hasAddressTaken())
330 MBB->setHasAddressTaken();
331
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000332 // Create Machine PHI nodes for LLVM PHI nodes, lowering them as
333 // appropriate.
334 PHINode *PN;
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000335 DebugLoc DL;
336 for (BasicBlock::iterator
337 I = BB->begin(), E = BB->end(); I != E; ++I) {
338 if (CallInst *CI = dyn_cast<CallInst>(I)) {
339 if (Function *F = CI->getCalledFunction()) {
340 switch (F->getIntrinsicID()) {
341 default: break;
342 case Intrinsic::dbg_stoppoint: {
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000343 DbgStopPointInst *SPI = cast<DbgStopPointInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000344 if (isValidDebugInfoIntrinsic(*SPI, CodeGenOpt::Default))
345 DL = ExtractDebugLocation(*SPI, MF->getDebugLocInfo());
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000346 break;
347 }
348 case Intrinsic::dbg_func_start: {
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +0000349 DbgFuncStartInst *FSI = cast<DbgFuncStartInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +0000350 if (isValidDebugInfoIntrinsic(*FSI, CodeGenOpt::Default))
351 DL = ExtractDebugLocation(*FSI, MF->getDebugLocInfo());
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000352 break;
353 }
354 }
355 }
356 }
357
358 PN = dyn_cast<PHINode>(I);
359 if (!PN || PN->use_empty()) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000360
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000361 unsigned PHIReg = ValueMap[PN];
362 assert(PHIReg && "PHI node does not have an assigned virtual register!");
363
Owen Andersone50ed302009-08-10 22:56:29 +0000364 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000365 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
366 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +0000367 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +0000368 unsigned NumRegisters = TLI.getNumRegisters(*DAG.getContext(), VT);
Dan Gohman6448d912008-09-04 15:39:15 +0000369 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000370 for (unsigned i = 0; i != NumRegisters; ++i)
Bill Wendling6a8a0d72009-02-03 02:20:52 +0000371 BuildMI(MBB, DL, TII->get(TargetInstrInfo::PHI), PHIReg + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000372 PHIReg += NumRegisters;
373 }
374 }
375 }
376}
377
Owen Andersone50ed302009-08-10 22:56:29 +0000378unsigned FunctionLoweringInfo::MakeReg(EVT VT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000379 return RegInfo->createVirtualRegister(TLI.getRegClassFor(VT));
380}
381
382/// CreateRegForValue - Allocate the appropriate number of virtual registers of
383/// the correctly promoted or expanded types. Assign these registers
384/// consecutive vreg numbers and return the first assigned number.
385///
386/// In the case that the given value has struct or array type, this function
387/// will assign registers for each member or element.
388///
389unsigned FunctionLoweringInfo::CreateRegForValue(const Value *V) {
Owen Andersone50ed302009-08-10 22:56:29 +0000390 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000391 ComputeValueVTs(TLI, V->getType(), ValueVTs);
392
393 unsigned FirstReg = 0;
394 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000395 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000396 EVT RegisterVT = TLI.getRegisterType(V->getContext(), ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000397
Owen Anderson23b9b192009-08-12 00:36:31 +0000398 unsigned NumRegs = TLI.getNumRegisters(V->getContext(), ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000399 for (unsigned i = 0; i != NumRegs; ++i) {
400 unsigned R = MakeReg(RegisterVT);
401 if (!FirstReg) FirstReg = R;
402 }
403 }
404 return FirstReg;
405}
406
407/// getCopyFromParts - Create a value that contains the specified legal parts
408/// combined into the value they represent. If the parts combine to a type
409/// larger then ValueVT then AssertOp can be used to specify whether the extra
410/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
411/// (ISD::AssertSext).
Dale Johannesen66978ee2009-01-31 02:22:37 +0000412static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
413 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000414 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000415 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000416 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000417 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000418 SDValue Val = Parts[0];
419
420 if (NumParts > 1) {
421 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000422 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000423 unsigned PartBits = PartVT.getSizeInBits();
424 unsigned ValueBits = ValueVT.getSizeInBits();
425
426 // Assemble the power of 2 part.
427 unsigned RoundParts = NumParts & (NumParts - 1) ?
428 1 << Log2_32(NumParts) : NumParts;
429 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000430 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000431 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000432 SDValue Lo, Hi;
433
Owen Anderson23b9b192009-08-12 00:36:31 +0000434 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000435
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000436 if (RoundParts > 2) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000437 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT);
438 Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000439 PartVT, HalfVT);
440 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000441 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
442 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000443 }
444 if (TLI.isBigEndian())
445 std::swap(Lo, Hi);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000446 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000447
448 if (RoundParts < NumParts) {
449 // Assemble the trailing non-power-of-2 part.
450 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000451 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Scott Michelfdc40a02009-02-17 22:15:04 +0000452 Hi = getCopyFromParts(DAG, dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000453 Parts+RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000454
455 // Combine the round and odd parts.
456 Lo = Val;
457 if (TLI.isBigEndian())
458 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000459 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000460 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
461 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000462 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000463 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000464 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
465 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000466 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000467 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000468 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000469 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000470 unsigned NumIntermediates;
471 unsigned NumRegs =
Owen Anderson23b9b192009-08-12 00:36:31 +0000472 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
473 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000474 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
475 NumParts = NumRegs; // Silence a compiler warning.
476 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
477 assert(RegisterVT == Parts[0].getValueType() &&
478 "Part type doesn't match part!");
479
480 // Assemble the parts into intermediate operands.
481 SmallVector<SDValue, 8> Ops(NumIntermediates);
482 if (NumIntermediates == NumParts) {
483 // If the register was not expanded, truncate or copy the value,
484 // as appropriate.
485 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000486 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000487 PartVT, IntermediateVT);
488 } else if (NumParts > 0) {
489 // If the intermediate type was expanded, build the intermediate operands
490 // from the parts.
491 assert(NumParts % NumIntermediates == 0 &&
492 "Must expand into a divisible number of parts!");
493 unsigned Factor = NumParts / NumIntermediates;
494 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000495 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000496 PartVT, IntermediateVT);
497 }
498
499 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
500 // operands.
501 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000502 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000503 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000504 } else if (PartVT.isFloatingPoint()) {
505 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000506 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000507 "Unexpected split");
508 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000509 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
510 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000511 if (TLI.isBigEndian())
512 std::swap(Lo, Hi);
513 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
514 } else {
515 // FP split into integer parts (soft fp)
516 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
517 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000518 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Eli Friedman2ac8b322009-05-20 06:02:09 +0000519 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000520 }
521 }
522
523 // There is now one part, held in Val. Correct it to match ValueVT.
524 PartVT = Val.getValueType();
525
526 if (PartVT == ValueVT)
527 return Val;
528
529 if (PartVT.isVector()) {
530 assert(ValueVT.isVector() && "Unknown vector conversion!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000531 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000532 }
533
534 if (ValueVT.isVector()) {
535 assert(ValueVT.getVectorElementType() == PartVT &&
536 ValueVT.getVectorNumElements() == 1 &&
537 "Only trivial scalar-to-vector conversions should get here!");
Evan Chenga87008d2009-02-25 22:49:59 +0000538 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000539 }
540
541 if (PartVT.isInteger() &&
542 ValueVT.isInteger()) {
543 if (ValueVT.bitsLT(PartVT)) {
544 // For a truncate, see if we have any information to
545 // indicate whether the truncated bits will always be
546 // zero or sign-extension.
547 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000548 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000549 DAG.getValueType(ValueVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000550 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000551 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000552 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000553 }
554 }
555
556 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
557 if (ValueVT.bitsLT(Val.getValueType()))
558 // FP_ROUND's are always exact here.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000559 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000560 DAG.getIntPtrConstant(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000561 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000562 }
563
564 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000565 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000566
Torok Edwinc23197a2009-07-14 16:55:14 +0000567 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000568 return SDValue();
569}
570
571/// getCopyToParts - Create a series of nodes that contain the specified value
572/// split into legal parts. If the parts contain more bits than Val, then, for
573/// integers, ExtendKind can be used to specify how to generate the extra bits.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000574static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val,
Owen Andersone50ed302009-08-10 22:56:29 +0000575 SDValue *Parts, unsigned NumParts, EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000576 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000577 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000578 EVT PtrVT = TLI.getPointerTy();
579 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000580 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000581 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000582 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
583
584 if (!NumParts)
585 return;
586
587 if (!ValueVT.isVector()) {
588 if (PartVT == ValueVT) {
589 assert(NumParts == 1 && "No-op copy with multiple parts!");
590 Parts[0] = Val;
591 return;
592 }
593
594 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
595 // If the parts cover more bits than the value has, promote the value.
596 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
597 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000598 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000599 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000600 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000601 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000602 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000603 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000604 }
605 } else if (PartBits == ValueVT.getSizeInBits()) {
606 // Different types of the same size.
607 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000608 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000609 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
610 // If the parts cover less bits than value has, truncate the value.
611 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000612 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000613 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000614 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000615 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000616 }
617 }
618
619 // The value may have changed - recompute ValueVT.
620 ValueVT = Val.getValueType();
621 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
622 "Failed to tile the value with PartVT!");
623
624 if (NumParts == 1) {
625 assert(PartVT == ValueVT && "Type conversion failed!");
626 Parts[0] = Val;
627 return;
628 }
629
630 // Expand the value into multiple parts.
631 if (NumParts & (NumParts - 1)) {
632 // The number of parts is not a power of 2. Split off and copy the tail.
633 assert(PartVT.isInteger() && ValueVT.isInteger() &&
634 "Do not know what to expand to!");
635 unsigned RoundParts = 1 << Log2_32(NumParts);
636 unsigned RoundBits = RoundParts * PartBits;
637 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000638 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000639 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000640 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000641 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000642 if (TLI.isBigEndian())
643 // The odd parts were reversed by getCopyToParts - unreverse them.
644 std::reverse(Parts + RoundParts, Parts + NumParts);
645 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000646 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000647 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000648 }
649
650 // The number of parts is a power of 2. Repeatedly bisect the value using
651 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000652 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson23b9b192009-08-12 00:36:31 +0000653 EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000654 Val);
655 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
656 for (unsigned i = 0; i < NumParts; i += StepSize) {
657 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000658 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000659 SDValue &Part0 = Parts[i];
660 SDValue &Part1 = Parts[i+StepSize/2];
661
Scott Michelfdc40a02009-02-17 22:15:04 +0000662 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000663 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000664 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000665 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000666 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000667 DAG.getConstant(0, PtrVT));
668
669 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000670 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000671 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000672 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000673 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000674 }
675 }
676 }
677
678 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000679 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000680
681 return;
682 }
683
684 // Vector ValueVT.
685 if (NumParts == 1) {
686 if (PartVT != ValueVT) {
687 if (PartVT.isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000688 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000689 } else {
690 assert(ValueVT.getVectorElementType() == PartVT &&
691 ValueVT.getVectorNumElements() == 1 &&
692 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000693 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000694 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000695 DAG.getConstant(0, PtrVT));
696 }
697 }
698
699 Parts[0] = Val;
700 return;
701 }
702
703 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000704 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000705 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000706 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
707 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000708 unsigned NumElements = ValueVT.getVectorNumElements();
709
710 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
711 NumParts = NumRegs; // Silence a compiler warning.
712 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
713
714 // Split the vector into intermediate operands.
715 SmallVector<SDValue, 8> Ops(NumIntermediates);
716 for (unsigned i = 0; i != NumIntermediates; ++i)
717 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000718 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000719 IntermediateVT, Val,
720 DAG.getConstant(i * (NumElements / NumIntermediates),
721 PtrVT));
722 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000723 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000724 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000725 DAG.getConstant(i, PtrVT));
726
727 // Split the intermediate operands into legal parts.
728 if (NumParts == NumIntermediates) {
729 // If the register was not expanded, promote or copy the value,
730 // as appropriate.
731 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000732 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000733 } else if (NumParts > 0) {
734 // If the intermediate type was expanded, split each the value into
735 // legal parts.
736 assert(NumParts % NumIntermediates == 0 &&
737 "Must expand into a divisible number of parts!");
738 unsigned Factor = NumParts / NumIntermediates;
739 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000740 getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000741 }
742}
743
744
745void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
746 AA = &aa;
747 GFI = gfi;
748 TD = DAG.getTarget().getTargetData();
749}
750
751/// clear - Clear out the curret SelectionDAG and the associated
752/// state and prepare this SelectionDAGLowering object to be used
753/// for a new block. This doesn't clear out information about
754/// additional blocks that are needed to complete switch lowering
755/// or PHI node updating; that information is cleared out as it is
756/// consumed.
757void SelectionDAGLowering::clear() {
758 NodeMap.clear();
759 PendingLoads.clear();
760 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000761 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000762 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000763 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000764 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000765}
766
767/// getRoot - Return the current virtual root of the Selection DAG,
768/// flushing any PendingLoad items. This must be done before emitting
769/// a store or any other node that may need to be ordered after any
770/// prior load instructions.
771///
772SDValue SelectionDAGLowering::getRoot() {
773 if (PendingLoads.empty())
774 return DAG.getRoot();
775
776 if (PendingLoads.size() == 1) {
777 SDValue Root = PendingLoads[0];
778 DAG.setRoot(Root);
779 PendingLoads.clear();
780 return Root;
781 }
782
783 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000784 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000785 &PendingLoads[0], PendingLoads.size());
786 PendingLoads.clear();
787 DAG.setRoot(Root);
788 return Root;
789}
790
791/// getControlRoot - Similar to getRoot, but instead of flushing all the
792/// PendingLoad items, flush all the PendingExports items. It is necessary
793/// to do this before emitting a terminator instruction.
794///
795SDValue SelectionDAGLowering::getControlRoot() {
796 SDValue Root = DAG.getRoot();
797
798 if (PendingExports.empty())
799 return Root;
800
801 // Turn all of the CopyToReg chains into one factored node.
802 if (Root.getOpcode() != ISD::EntryToken) {
803 unsigned i = 0, e = PendingExports.size();
804 for (; i != e; ++i) {
805 assert(PendingExports[i].getNode()->getNumOperands() > 1);
806 if (PendingExports[i].getNode()->getOperand(0) == Root)
807 break; // Don't add the root if we already indirectly depend on it.
808 }
809
810 if (i == e)
811 PendingExports.push_back(Root);
812 }
813
Owen Anderson825b72b2009-08-11 20:47:22 +0000814 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000815 &PendingExports[0],
816 PendingExports.size());
817 PendingExports.clear();
818 DAG.setRoot(Root);
819 return Root;
820}
821
822void SelectionDAGLowering::visit(Instruction &I) {
823 visit(I.getOpcode(), I);
824}
825
826void SelectionDAGLowering::visit(unsigned Opcode, User &I) {
827 // Note: this doesn't use InstVisitor, because it has to work with
828 // ConstantExpr's in addition to instructions.
829 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000830 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000831 // Build the switch statement using the Instruction.def file.
832#define HANDLE_INST(NUM, OPCODE, CLASS) \
833 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
834#include "llvm/Instruction.def"
835 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000836}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000837
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000838SDValue SelectionDAGLowering::getValue(const Value *V) {
839 SDValue &N = NodeMap[V];
840 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000841
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000842 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000843 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000844
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000845 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000846 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000847
848 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
849 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000850
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000851 if (isa<ConstantPointerNull>(C))
852 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000853
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000854 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000855 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000856
Nate Begeman9008ca62009-04-27 18:41:29 +0000857 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000858 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000859
860 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
861 visit(CE->getOpcode(), *CE);
862 SDValue N1 = NodeMap[V];
863 assert(N1.getNode() && "visit didn't populate the ValueMap!");
864 return N1;
865 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000866
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000867 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
868 SmallVector<SDValue, 4> Constants;
869 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
870 OI != OE; ++OI) {
871 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000872 // If the operand is an empty aggregate, there are no values.
873 if (!Val) continue;
874 // Add each leaf value from the operand to the Constants list
875 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000876 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
877 Constants.push_back(SDValue(Val, i));
878 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000879 return DAG.getMergeValues(&Constants[0], Constants.size(),
880 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000881 }
882
883 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
884 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
885 "Unknown struct or array constant!");
886
Owen Andersone50ed302009-08-10 22:56:29 +0000887 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000888 ComputeValueVTs(TLI, C->getType(), ValueVTs);
889 unsigned NumElts = ValueVTs.size();
890 if (NumElts == 0)
891 return SDValue(); // empty struct
892 SmallVector<SDValue, 4> Constants(NumElts);
893 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000894 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000895 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000896 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000897 else if (EltVT.isFloatingPoint())
898 Constants[i] = DAG.getConstantFP(0, EltVT);
899 else
900 Constants[i] = DAG.getConstant(0, EltVT);
901 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000902 return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000903 }
904
Dan Gohman8c2b5252009-10-30 01:27:03 +0000905 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
906 return DAG.getBlockAddress(BA, getCurDebugLoc());
907
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000908 const VectorType *VecTy = cast<VectorType>(V->getType());
909 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000910
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000911 // Now that we know the number and type of the elements, get that number of
912 // elements into the Ops array based on what kind of constant it is.
913 SmallVector<SDValue, 16> Ops;
914 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
915 for (unsigned i = 0; i != NumElements; ++i)
916 Ops.push_back(getValue(CP->getOperand(i)));
917 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000918 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000919 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000920
921 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000922 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000923 Op = DAG.getConstantFP(0, EltVT);
924 else
925 Op = DAG.getConstant(0, EltVT);
926 Ops.assign(NumElements, Op);
927 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000928
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000929 // Create a BUILD_VECTOR node.
Evan Chenga87008d2009-02-25 22:49:59 +0000930 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
931 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000932 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000934 // If this is a static alloca, generate it as the frameindex instead of
935 // computation.
936 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
937 DenseMap<const AllocaInst*, int>::iterator SI =
938 FuncInfo.StaticAllocaMap.find(AI);
939 if (SI != FuncInfo.StaticAllocaMap.end())
940 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
941 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000943 unsigned InReg = FuncInfo.ValueMap[V];
944 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000945
Owen Anderson23b9b192009-08-12 00:36:31 +0000946 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000947 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +0000948 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000949}
950
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000951/// Get the EVTs and ArgFlags collections that represent the return type
952/// of the given function. This does not require a DAG or a return value, and
953/// is suitable for use before any DAGs for the function are constructed.
954static void getReturnInfo(const Function* F, SmallVectorImpl<EVT> &OutVTs,
955 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
956 TargetLowering &TLI) {
957 const Type* ReturnType = F->getReturnType();
958
959 SmallVector<EVT, 4> ValueVTs;
960 ComputeValueVTs(TLI, ReturnType, ValueVTs);
961 unsigned NumValues = ValueVTs.size();
962 if ( NumValues == 0 ) return;
963
964 for (unsigned j = 0, f = NumValues; j != f; ++j) {
965 EVT VT = ValueVTs[j];
966 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
967
968 if (F->paramHasAttr(0, Attribute::SExt))
969 ExtendKind = ISD::SIGN_EXTEND;
970 else if (F->paramHasAttr(0, Attribute::ZExt))
971 ExtendKind = ISD::ZERO_EXTEND;
972
973 // FIXME: C calling convention requires the return type to be promoted to
974 // at least 32-bit. But this is not necessary for non-C calling
975 // conventions. The frontend should mark functions whose return values
976 // require promoting with signext or zeroext attributes.
977 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
978 EVT MinVT = TLI.getRegisterType(F->getContext(), MVT::i32);
979 if (VT.bitsLT(MinVT))
980 VT = MinVT;
981 }
982
983 unsigned NumParts = TLI.getNumRegisters(F->getContext(), VT);
984 EVT PartVT = TLI.getRegisterType(F->getContext(), VT);
985 // 'inreg' on function refers to return value
986 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
987 if (F->paramHasAttr(0, Attribute::InReg))
988 Flags.setInReg();
989
990 // Propagate extension type if any
991 if (F->paramHasAttr(0, Attribute::SExt))
992 Flags.setSExt();
993 else if (F->paramHasAttr(0, Attribute::ZExt))
994 Flags.setZExt();
995
996 for (unsigned i = 0; i < NumParts; ++i)
997 {
998 OutVTs.push_back(PartVT);
999 OutFlags.push_back(Flags);
1000 }
1001 }
1002}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001003
1004void SelectionDAGLowering::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001005 SDValue Chain = getControlRoot();
1006 SmallVector<ISD::OutputArg, 8> Outs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001007 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00001008 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001009 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00001010 unsigned NumValues = ValueVTs.size();
1011 if (NumValues == 0) continue;
1012
1013 SDValue RetOp = getValue(I.getOperand(i));
1014 for (unsigned j = 0, f = NumValues; j != f; ++j) {
Owen Andersone50ed302009-08-10 22:56:29 +00001015 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001017 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001018
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001019 const Function *F = I.getParent()->getParent();
Devang Patel05988662008-09-25 21:00:45 +00001020 if (F->paramHasAttr(0, Attribute::SExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001021 ExtendKind = ISD::SIGN_EXTEND;
Devang Patel05988662008-09-25 21:00:45 +00001022 else if (F->paramHasAttr(0, Attribute::ZExt))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001023 ExtendKind = ISD::ZERO_EXTEND;
1024
Evan Cheng3927f432009-03-25 20:20:11 +00001025 // FIXME: C calling convention requires the return type to be promoted to
1026 // at least 32-bit. But this is not necessary for non-C calling
1027 // conventions. The frontend should mark functions whose return values
1028 // require promoting with signext or zeroext attributes.
1029 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +00001030 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
Evan Cheng3927f432009-03-25 20:20:11 +00001031 if (VT.bitsLT(MinVT))
1032 VT = MinVT;
1033 }
1034
Owen Anderson23b9b192009-08-12 00:36:31 +00001035 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
1036 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
Evan Cheng3927f432009-03-25 20:20:11 +00001037 SmallVector<SDValue, 4> Parts(NumParts);
Dale Johannesen66978ee2009-01-31 02:22:37 +00001038 getCopyToParts(DAG, getCurDebugLoc(),
1039 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001040 &Parts[0], NumParts, PartVT, ExtendKind);
1041
Dale Johannesenc9c6da62008-09-25 20:47:45 +00001042 // 'inreg' on function refers to return value
1043 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Devang Patel05988662008-09-25 21:00:45 +00001044 if (F->paramHasAttr(0, Attribute::InReg))
Dale Johannesenc9c6da62008-09-25 20:47:45 +00001045 Flags.setInReg();
Anton Korobeynikov0692fab2009-07-16 13:35:48 +00001046
1047 // Propagate extension type if any
1048 if (F->paramHasAttr(0, Attribute::SExt))
1049 Flags.setSExt();
1050 else if (F->paramHasAttr(0, Attribute::ZExt))
1051 Flags.setZExt();
1052
Dan Gohman98ca4f22009-08-05 01:29:28 +00001053 for (unsigned i = 0; i < NumParts; ++i)
1054 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001055 }
1056 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001057
1058 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00001059 CallingConv::ID CallConv =
1060 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001061 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
1062 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +00001063
1064 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00001065 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00001066 "LowerReturn didn't return a valid chain!");
1067
1068 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001069 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001070}
1071
Dan Gohmanad62f532009-04-23 23:13:24 +00001072/// CopyToExportRegsIfNeeded - If the given value has virtual registers
1073/// created for it, emit nodes to copy the value into the virtual
1074/// registers.
1075void SelectionDAGLowering::CopyToExportRegsIfNeeded(Value *V) {
1076 if (!V->use_empty()) {
1077 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
1078 if (VMI != FuncInfo.ValueMap.end())
1079 CopyValueToVirtualRegister(V, VMI->second);
1080 }
1081}
1082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083/// ExportFromCurrentBlock - If this condition isn't known to be exported from
1084/// the current basic block, add it to ValueMap now so that we'll get a
1085/// CopyTo/FromReg.
1086void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
1087 // No need to export constants.
1088 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001089
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001090 // Already exported?
1091 if (FuncInfo.isExportedInst(V)) return;
1092
1093 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1094 CopyValueToVirtualRegister(V, Reg);
1095}
1096
1097bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
1098 const BasicBlock *FromBB) {
1099 // The operands of the setcc have to be in this block. We don't know
1100 // how to export them from some other block.
1101 if (Instruction *VI = dyn_cast<Instruction>(V)) {
1102 // Can export from current BB.
1103 if (VI->getParent() == FromBB)
1104 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001105
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001106 // Is already exported, noop.
1107 return FuncInfo.isExportedInst(V);
1108 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001110 // If this is an argument, we can export it if the BB is the entry block or
1111 // if it is already exported.
1112 if (isa<Argument>(V)) {
1113 if (FromBB == &FromBB->getParent()->getEntryBlock())
1114 return true;
1115
1116 // Otherwise, can only export this if it is already exported.
1117 return FuncInfo.isExportedInst(V);
1118 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001119
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001120 // Otherwise, constants can always be exported.
1121 return true;
1122}
1123
1124static bool InBlock(const Value *V, const BasicBlock *BB) {
1125 if (const Instruction *I = dyn_cast<Instruction>(V))
1126 return I->getParent() == BB;
1127 return true;
1128}
1129
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001130/// getFCmpCondCode - Return the ISD condition code corresponding to
1131/// the given LLVM IR floating-point condition code. This includes
1132/// consideration of global floating-point math flags.
1133///
1134static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1135 ISD::CondCode FPC, FOC;
1136 switch (Pred) {
1137 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1138 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1139 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1140 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1141 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1142 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1143 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1144 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1145 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1146 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1147 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1148 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1149 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1150 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1151 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1152 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1153 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001154 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001155 FOC = FPC = ISD::SETFALSE;
1156 break;
1157 }
1158 if (FiniteOnlyFPMath())
1159 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001160 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001161 return FPC;
1162}
1163
1164/// getICmpCondCode - Return the ISD condition code corresponding to
1165/// the given LLVM IR integer condition code.
1166///
1167static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1168 switch (Pred) {
1169 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1170 case ICmpInst::ICMP_NE: return ISD::SETNE;
1171 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1172 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1173 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1174 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1175 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1176 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1177 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1178 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1179 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001180 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001181 return ISD::SETNE;
1182 }
1183}
1184
Dan Gohmanc2277342008-10-17 21:16:08 +00001185/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1186/// This function emits a branch and is used at the leaves of an OR or an
1187/// AND operator tree.
1188///
1189void
1190SelectionDAGLowering::EmitBranchForMergedCondition(Value *Cond,
1191 MachineBasicBlock *TBB,
1192 MachineBasicBlock *FBB,
1193 MachineBasicBlock *CurBB) {
1194 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001195
Dan Gohmanc2277342008-10-17 21:16:08 +00001196 // If the leaf of the tree is a comparison, merge the condition into
1197 // the caseblock.
1198 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1199 // The operands of the cmp have to be in this block. We don't know
1200 // how to export them from some other block. If this is the first block
1201 // of the sequence, no exporting is needed.
1202 if (CurBB == CurMBB ||
1203 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1204 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205 ISD::CondCode Condition;
1206 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001207 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001208 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001209 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001210 } else {
1211 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001212 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001213 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001214
1215 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001216 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1217 SwitchCases.push_back(CB);
1218 return;
1219 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001220 }
1221
1222 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001223 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001224 NULL, TBB, FBB, CurBB);
1225 SwitchCases.push_back(CB);
1226}
1227
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001228/// FindMergedConditions - If Cond is an expression like
Dan Gohmanc2277342008-10-17 21:16:08 +00001229void SelectionDAGLowering::FindMergedConditions(Value *Cond,
1230 MachineBasicBlock *TBB,
1231 MachineBasicBlock *FBB,
1232 MachineBasicBlock *CurBB,
1233 unsigned Opc) {
1234 // If this node is not part of the or/and tree, emit it as a branch.
1235 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001236 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001237 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1238 BOp->getParent() != CurBB->getBasicBlock() ||
1239 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1240 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1241 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001242 return;
1243 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001244
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001245 // Create TmpBB after CurBB.
1246 MachineFunction::iterator BBI = CurBB;
1247 MachineFunction &MF = DAG.getMachineFunction();
1248 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1249 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001251 if (Opc == Instruction::Or) {
1252 // Codegen X | Y as:
1253 // jmp_if_X TBB
1254 // jmp TmpBB
1255 // TmpBB:
1256 // jmp_if_Y TBB
1257 // jmp FBB
1258 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001259
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001260 // Emit the LHS condition.
1261 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 // Emit the RHS condition into TmpBB.
1264 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1265 } else {
1266 assert(Opc == Instruction::And && "Unknown merge op!");
1267 // Codegen X & Y as:
1268 // jmp_if_X TmpBB
1269 // jmp FBB
1270 // TmpBB:
1271 // jmp_if_Y TBB
1272 // jmp FBB
1273 //
1274 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001275
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001276 // Emit the LHS condition.
1277 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001278
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001279 // Emit the RHS condition into TmpBB.
1280 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1281 }
1282}
1283
1284/// If the set of cases should be emitted as a series of branches, return true.
1285/// If we should emit this as a bunch of and/or'd together conditions, return
1286/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001287bool
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001288SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
1289 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291 // If this is two comparisons of the same values or'd or and'd together, they
1292 // will get folded into a single comparison, so don't emit two blocks.
1293 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1294 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1295 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1296 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1297 return false;
1298 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001299
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001300 return true;
1301}
1302
1303void SelectionDAGLowering::visitBr(BranchInst &I) {
1304 // Update machine-CFG edges.
1305 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1306
1307 // Figure out which block is immediately after the current one.
1308 MachineBasicBlock *NextBlock = 0;
1309 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001310 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001311 NextBlock = BBI;
1312
1313 if (I.isUnconditional()) {
1314 // Update machine-CFG edges.
1315 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001316
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001317 // If this is not a fall-through branch, emit the branch.
1318 if (Succ0MBB != NextBlock)
Scott Michelfdc40a02009-02-17 22:15:04 +00001319 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001320 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001321 DAG.getBasicBlock(Succ0MBB)));
1322 return;
1323 }
1324
1325 // If this condition is one of the special cases we handle, do special stuff
1326 // now.
1327 Value *CondVal = I.getCondition();
1328 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1329
1330 // If this is a series of conditions that are or'd or and'd together, emit
1331 // this as a sequence of branches instead of setcc's with and/or operations.
1332 // For example, instead of something like:
1333 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001334 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001335 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001336 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001337 // or C, F
1338 // jnz foo
1339 // Emit:
1340 // cmp A, B
1341 // je foo
1342 // cmp D, E
1343 // jle foo
1344 //
1345 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001346 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347 (BOp->getOpcode() == Instruction::And ||
1348 BOp->getOpcode() == Instruction::Or)) {
1349 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1350 // If the compares in later blocks need to use values not currently
1351 // exported from this block, export them now. This block should always
1352 // be the first entry.
1353 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001354
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001355 // Allow some cases to be rejected.
1356 if (ShouldEmitAsBranches(SwitchCases)) {
1357 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1358 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1359 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1360 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001361
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001362 // Emit the branch for this block.
1363 visitSwitchCase(SwitchCases[0]);
1364 SwitchCases.erase(SwitchCases.begin());
1365 return;
1366 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001367
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001368 // Okay, we decided not to do this, remove any inserted MBB's and clear
1369 // SwitchCases.
1370 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001371 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001372
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001373 SwitchCases.clear();
1374 }
1375 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001376
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001378 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001379 NULL, Succ0MBB, Succ1MBB, CurMBB);
1380 // Use visitSwitchCase to actually insert the fast branch sequence for this
1381 // cond branch.
1382 visitSwitchCase(CB);
1383}
1384
1385/// visitSwitchCase - Emits the necessary code to represent a single node in
1386/// the binary search tree resulting from lowering a switch instruction.
1387void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) {
1388 SDValue Cond;
1389 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001390 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001391
1392 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001393 if (CB.CmpMHS == NULL) {
1394 // Fold "(X == true)" to X and "(X == false)" to !X to
1395 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001396 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001397 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001398 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001399 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001400 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001401 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001402 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001403 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001404 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001405 } else {
1406 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1407
Anton Korobeynikov23218582008-12-23 22:25:27 +00001408 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1409 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001410
1411 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001412 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001413
1414 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001415 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001416 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001417 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001418 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001419 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001420 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421 DAG.getConstant(High-Low, VT), ISD::SETULE);
1422 }
1423 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001425 // Update successor info
1426 CurMBB->addSuccessor(CB.TrueBB);
1427 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001429 // Set NextBlock to be the MBB immediately after the current one, if any.
1430 // This is used to avoid emitting unnecessary branches to the next block.
1431 MachineBasicBlock *NextBlock = 0;
1432 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001433 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001434 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001435
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436 // If the lhs block is the next block, invert the condition so that we can
1437 // fall through to the lhs instead of the rhs block.
1438 if (CB.TrueBB == NextBlock) {
1439 std::swap(CB.TrueBB, CB.FalseBB);
1440 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001441 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442 }
Dale Johannesenf5d97892009-02-04 01:48:28 +00001443 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001444 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001445 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001447 // If the branch was constant folded, fix up the CFG.
1448 if (BrCond.getOpcode() == ISD::BR) {
1449 CurMBB->removeSuccessor(CB.FalseBB);
1450 DAG.setRoot(BrCond);
1451 } else {
1452 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001453 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001455
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001456 if (CB.FalseBB == NextBlock)
1457 DAG.setRoot(BrCond);
1458 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001459 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001460 DAG.getBasicBlock(CB.FalseBB)));
1461 }
1462}
1463
1464/// visitJumpTable - Emit JumpTable node in the current MBB
1465void SelectionDAGLowering::visitJumpTable(JumpTable &JT) {
1466 // Emit the code for the jump table
1467 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001468 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001469 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1470 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001471 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Scott Michelfdc40a02009-02-17 22:15:04 +00001472 DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001473 MVT::Other, Index.getValue(1),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001474 Table, Index));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001475}
1476
1477/// visitJumpTableHeader - This function emits necessary code to produce index
1478/// in the JumpTable from switch case.
1479void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT,
1480 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001481 // Subtract the lowest switch case value from the value being switched on and
1482 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001483 // difference between smallest and largest cases.
1484 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001485 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001486 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001487 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001488
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001489 // The SDNode we just created, which holds the value being switched on minus
1490 // the the smallest case value, needs to be copied to a virtual register so it
1491 // can be used as an index into the jump table in a subsequent basic block.
1492 // This value may be smaller or larger than the target's pointer type, and
1493 // therefore require extension or truncating.
Duncan Sands3a66a682009-10-13 21:04:12 +00001494 SwitchOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001495
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001496 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001497 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1498 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001499 JT.Reg = JumpTableReg;
1500
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001501 // Emit the range check for the jump table, and branch to the default block
1502 // for the switch statement if the value being switched on exceeds the largest
1503 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001504 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
1505 TLI.getSetCCResultType(SUB.getValueType()), SUB,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001506 DAG.getConstant(JTH.Last-JTH.First,VT),
1507 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001508
1509 // Set NextBlock to be the MBB immediately after the current one, if any.
1510 // This is used to avoid emitting unnecessary branches to the next block.
1511 MachineBasicBlock *NextBlock = 0;
1512 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001513 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001514 NextBlock = BBI;
1515
Dale Johannesen66978ee2009-01-31 02:22:37 +00001516 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001517 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001518 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001519
1520 if (JT.MBB == NextBlock)
1521 DAG.setRoot(BrCond);
1522 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001524 DAG.getBasicBlock(JT.MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001525}
1526
1527/// visitBitTestHeader - This function emits necessary code to produce value
1528/// suitable for "bit tests"
1529void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) {
1530 // Subtract the minimum value
1531 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001532 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001533 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001534 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001535
1536 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001537 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
1538 TLI.getSetCCResultType(SUB.getValueType()),
1539 SUB, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001540 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001541
Duncan Sands3a66a682009-10-13 21:04:12 +00001542 SDValue ShiftOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001543
Duncan Sands92abc622009-01-31 15:50:11 +00001544 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001545 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1546 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001547
1548 // Set NextBlock to be the MBB immediately after the current one, if any.
1549 // This is used to avoid emitting unnecessary branches to the next block.
1550 MachineBasicBlock *NextBlock = 0;
1551 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001552 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001553 NextBlock = BBI;
1554
1555 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1556
1557 CurMBB->addSuccessor(B.Default);
1558 CurMBB->addSuccessor(MBB);
1559
Dale Johannesen66978ee2009-01-31 02:22:37 +00001560 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001561 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001562 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001563
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001564 if (MBB == NextBlock)
1565 DAG.setRoot(BrRange);
1566 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001567 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001568 DAG.getBasicBlock(MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001569}
1570
1571/// visitBitTestCase - this function produces one "bit test"
1572void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB,
1573 unsigned Reg,
1574 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001575 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001576 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001577 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001578 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001579 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001580 DAG.getConstant(1, TLI.getPointerTy()),
1581 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001582
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001583 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001584 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001585 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001586 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001587 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1588 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001589 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001590 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001591
1592 CurMBB->addSuccessor(B.TargetBB);
1593 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001594
Dale Johannesen66978ee2009-01-31 02:22:37 +00001595 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001596 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001597 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001598
1599 // Set NextBlock to be the MBB immediately after the current one, if any.
1600 // This is used to avoid emitting unnecessary branches to the next block.
1601 MachineBasicBlock *NextBlock = 0;
1602 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001603 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001604 NextBlock = BBI;
1605
1606 if (NextMBB == NextBlock)
1607 DAG.setRoot(BrAnd);
1608 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001609 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610 DAG.getBasicBlock(NextMBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001611}
1612
1613void SelectionDAGLowering::visitInvoke(InvokeInst &I) {
1614 // Retrieve successors.
1615 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1616 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1617
Gabor Greifb67e6b32009-01-15 11:10:44 +00001618 const Value *Callee(I.getCalledValue());
1619 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 visitInlineAsm(&I);
1621 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001622 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001623
1624 // If the value of the invoke is used outside of its defining block, make it
1625 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001626 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001627
1628 // Update successor info
1629 CurMBB->addSuccessor(Return);
1630 CurMBB->addSuccessor(LandingPad);
1631
1632 // Drop into normal successor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001633 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001634 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001635 DAG.getBasicBlock(Return)));
1636}
1637
1638void SelectionDAGLowering::visitUnwind(UnwindInst &I) {
1639}
1640
1641/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1642/// small case ranges).
1643bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR,
1644 CaseRecVector& WorkList,
1645 Value* SV,
1646 MachineBasicBlock* Default) {
1647 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001648
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001649 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001650 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001651 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001652 return false;
1653
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001654 // Get the MachineFunction which holds the current MBB. This is used when
1655 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001656 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001657
1658 // Figure out which block is immediately after the current one.
1659 MachineBasicBlock *NextBlock = 0;
1660 MachineFunction::iterator BBI = CR.CaseBB;
1661
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001662 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001663 NextBlock = BBI;
1664
1665 // TODO: If any two of the cases has the same destination, and if one value
1666 // is the same as the other, but has one bit unset that the other has set,
1667 // use bit manipulation to do two compares at once. For example:
1668 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001669
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001670 // Rearrange the case blocks so that the last one falls through if possible.
1671 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1672 // The last case block won't fall through into 'NextBlock' if we emit the
1673 // branches in this order. See if rearranging a case value would help.
1674 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1675 if (I->BB == NextBlock) {
1676 std::swap(*I, BackCase);
1677 break;
1678 }
1679 }
1680 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001681
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001682 // Create a CaseBlock record representing a conditional branch to
1683 // the Case's target mbb if the value being switched on SV is equal
1684 // to C.
1685 MachineBasicBlock *CurBlock = CR.CaseBB;
1686 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1687 MachineBasicBlock *FallThrough;
1688 if (I != E-1) {
1689 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1690 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001691
1692 // Put SV in a virtual register to make it available from the new blocks.
1693 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001694 } else {
1695 // If the last case doesn't match, go to the default block.
1696 FallThrough = Default;
1697 }
1698
1699 Value *RHS, *LHS, *MHS;
1700 ISD::CondCode CC;
1701 if (I->High == I->Low) {
1702 // This is just small small case range :) containing exactly 1 case
1703 CC = ISD::SETEQ;
1704 LHS = SV; RHS = I->High; MHS = NULL;
1705 } else {
1706 CC = ISD::SETLE;
1707 LHS = I->Low; MHS = SV; RHS = I->High;
1708 }
1709 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001710
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001711 // If emitting the first comparison, just call visitSwitchCase to emit the
1712 // code into the current block. Otherwise, push the CaseBlock onto the
1713 // vector to be later processed by SDISel, and insert the node's MBB
1714 // before the next MBB.
1715 if (CurBlock == CurMBB)
1716 visitSwitchCase(CB);
1717 else
1718 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001719
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001720 CurBlock = FallThrough;
1721 }
1722
1723 return true;
1724}
1725
1726static inline bool areJTsAllowed(const TargetLowering &TLI) {
1727 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001728 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1729 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001730}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001731
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001732static APInt ComputeRange(const APInt &First, const APInt &Last) {
1733 APInt LastExt(Last), FirstExt(First);
1734 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1735 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1736 return (LastExt - FirstExt + 1ULL);
1737}
1738
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001739/// handleJTSwitchCase - Emit jumptable for current switch case range
1740bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR,
1741 CaseRecVector& WorkList,
1742 Value* SV,
1743 MachineBasicBlock* Default) {
1744 Case& FrontCase = *CR.Range.first;
1745 Case& BackCase = *(CR.Range.second-1);
1746
Chris Lattnere880efe2009-11-07 07:50:34 +00001747 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1748 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001749
Chris Lattnere880efe2009-11-07 07:50:34 +00001750 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001751 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1752 I!=E; ++I)
1753 TSize += I->size();
1754
Chris Lattnere880efe2009-11-07 07:50:34 +00001755 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001756 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001757
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001758 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001759 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001760 if (Density < 0.4)
1761 return false;
1762
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001763 DEBUG(errs() << "Lowering jump table\n"
1764 << "First entry: " << First << ". Last entry: " << Last << '\n'
1765 << "Range: " << Range
1766 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001767
1768 // Get the MachineFunction which holds the current MBB. This is used when
1769 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001770 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001771
1772 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001773 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001774 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001775
1776 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1777
1778 // Create a new basic block to hold the code for loading the address
1779 // of the jump table, and jumping to it. Update successor information;
1780 // we will either branch to the default case for the switch, or the jump
1781 // table.
1782 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1783 CurMF->insert(BBI, JumpTableBB);
1784 CR.CaseBB->addSuccessor(Default);
1785 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001786
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787 // Build a vector of destination BBs, corresponding to each target
1788 // of the jump table. If the value of the jump table slot corresponds to
1789 // a case statement, push the case's BB onto the vector, otherwise, push
1790 // the default BB.
1791 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001792 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001793 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001794 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1795 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1796
1797 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001798 DestBBs.push_back(I->BB);
1799 if (TEI==High)
1800 ++I;
1801 } else {
1802 DestBBs.push_back(Default);
1803 }
1804 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001805
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001806 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001807 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1808 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001809 E = DestBBs.end(); I != E; ++I) {
1810 if (!SuccsHandled[(*I)->getNumber()]) {
1811 SuccsHandled[(*I)->getNumber()] = true;
1812 JumpTableBB->addSuccessor(*I);
1813 }
1814 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001815
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816 // Create a jump table index for this jump table, or return an existing
1817 // one.
1818 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001820 // Set the jump table information so that we can codegen it as a second
1821 // MachineBasicBlock
1822 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1823 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1824 if (CR.CaseBB == CurMBB)
1825 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001826
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001827 JTCases.push_back(JumpTableBlock(JTH, JT));
1828
1829 return true;
1830}
1831
1832/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1833/// 2 subtrees.
1834bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR,
1835 CaseRecVector& WorkList,
1836 Value* SV,
1837 MachineBasicBlock* Default) {
1838 // Get the MachineFunction which holds the current MBB. This is used when
1839 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001840 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001841
1842 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001843 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001844 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001845
1846 Case& FrontCase = *CR.Range.first;
1847 Case& BackCase = *(CR.Range.second-1);
1848 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1849
1850 // Size is the number of Cases represented by this range.
1851 unsigned Size = CR.Range.second - CR.Range.first;
1852
Chris Lattnere880efe2009-11-07 07:50:34 +00001853 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1854 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 double FMetric = 0;
1856 CaseItr Pivot = CR.Range.first + Size/2;
1857
1858 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1859 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001860 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001861 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1862 I!=E; ++I)
1863 TSize += I->size();
1864
Chris Lattnere880efe2009-11-07 07:50:34 +00001865 APInt LSize = FrontCase.size();
1866 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001867 DEBUG(errs() << "Selecting best pivot: \n"
1868 << "First: " << First << ", Last: " << Last <<'\n'
1869 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001870 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1871 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001872 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1873 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001874 APInt Range = ComputeRange(LEnd, RBegin);
1875 assert((Range - 2ULL).isNonNegative() &&
1876 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001877 double LDensity = (double)LSize.roundToDouble() /
1878 (LEnd - First + 1ULL).roundToDouble();
1879 double RDensity = (double)RSize.roundToDouble() /
1880 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001881 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001883 DEBUG(errs() <<"=>Step\n"
1884 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1885 << "LDensity: " << LDensity
1886 << ", RDensity: " << RDensity << '\n'
1887 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001888 if (FMetric < Metric) {
1889 Pivot = J;
1890 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001891 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001892 }
1893
1894 LSize += J->size();
1895 RSize -= J->size();
1896 }
1897 if (areJTsAllowed(TLI)) {
1898 // If our case is dense we *really* should handle it earlier!
1899 assert((FMetric > 0) && "Should handle dense range earlier!");
1900 } else {
1901 Pivot = CR.Range.first + Size/2;
1902 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001903
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001904 CaseRange LHSR(CR.Range.first, Pivot);
1905 CaseRange RHSR(Pivot, CR.Range.second);
1906 Constant *C = Pivot->Low;
1907 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001908
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001909 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001910 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001911 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001912 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001913 // Pivot's Value, then we can branch directly to the LHS's Target,
1914 // rather than creating a leaf node for it.
1915 if ((LHSR.second - LHSR.first) == 1 &&
1916 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001917 cast<ConstantInt>(C)->getValue() ==
1918 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919 TrueBB = LHSR.first->BB;
1920 } else {
1921 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1922 CurMF->insert(BBI, TrueBB);
1923 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001924
1925 // Put SV in a virtual register to make it available from the new blocks.
1926 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001927 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001928
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929 // Similar to the optimization above, if the Value being switched on is
1930 // known to be less than the Constant CR.LT, and the current Case Value
1931 // is CR.LT - 1, then we can branch directly to the target block for
1932 // the current Case Value, rather than emitting a RHS leaf node for it.
1933 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001934 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1935 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001936 FalseBB = RHSR.first->BB;
1937 } else {
1938 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1939 CurMF->insert(BBI, FalseBB);
1940 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001941
1942 // Put SV in a virtual register to make it available from the new blocks.
1943 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001944 }
1945
1946 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001947 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001948 // Otherwise, branch to LHS.
1949 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1950
1951 if (CR.CaseBB == CurMBB)
1952 visitSwitchCase(CB);
1953 else
1954 SwitchCases.push_back(CB);
1955
1956 return true;
1957}
1958
1959/// handleBitTestsSwitchCase - if current case range has few destination and
1960/// range span less, than machine word bitwidth, encode case range into series
1961/// of masks and emit bit tests with these masks.
1962bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1963 CaseRecVector& WorkList,
1964 Value* SV,
1965 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001966 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001967 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001968
1969 Case& FrontCase = *CR.Range.first;
1970 Case& BackCase = *(CR.Range.second-1);
1971
1972 // Get the MachineFunction which holds the current MBB. This is used when
1973 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001974 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001975
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001976 // If target does not have legal shift left, do not emit bit tests at all.
1977 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1978 return false;
1979
Anton Korobeynikov23218582008-12-23 22:25:27 +00001980 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001981 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1982 I!=E; ++I) {
1983 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001984 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001985 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001986
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001987 // Count unique destinations
1988 SmallSet<MachineBasicBlock*, 4> Dests;
1989 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1990 Dests.insert(I->BB);
1991 if (Dests.size() > 3)
1992 // Don't bother the code below, if there are too much unique destinations
1993 return false;
1994 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001995 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1996 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001997
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001998 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001999 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
2000 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002001 APInt cmpRange = maxValue - minValue;
2002
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002003 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
2004 << "Low bound: " << minValue << '\n'
2005 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002006
2007 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002008 (!(Dests.size() == 1 && numCmps >= 3) &&
2009 !(Dests.size() == 2 && numCmps >= 5) &&
2010 !(Dests.size() >= 3 && numCmps >= 6)))
2011 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002012
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002013 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002014 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2015
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002016 // Optimize the case where all the case values fit in a
2017 // word without having to subtract minValue. In this case,
2018 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002019 if (minValue.isNonNegative() &&
2020 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
2021 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002022 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002023 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002024 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002025
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026 CaseBitsVector CasesBits;
2027 unsigned i, count = 0;
2028
2029 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2030 MachineBasicBlock* Dest = I->BB;
2031 for (i = 0; i < count; ++i)
2032 if (Dest == CasesBits[i].BB)
2033 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002035 if (i == count) {
2036 assert((count < 3) && "Too much destinations to test!");
2037 CasesBits.push_back(CaseBits(0, Dest, 0));
2038 count++;
2039 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002040
2041 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2042 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2043
2044 uint64_t lo = (lowValue - lowBound).getZExtValue();
2045 uint64_t hi = (highValue - lowBound).getZExtValue();
2046
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002047 for (uint64_t j = lo; j <= hi; j++) {
2048 CasesBits[i].Mask |= 1ULL << j;
2049 CasesBits[i].Bits++;
2050 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002052 }
2053 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002055 BitTestInfo BTC;
2056
2057 // Figure out which block is immediately after the current one.
2058 MachineFunction::iterator BBI = CR.CaseBB;
2059 ++BBI;
2060
2061 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2062
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002063 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002064 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002065 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
2066 << ", Bits: " << CasesBits[i].Bits
2067 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002068
2069 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2070 CurMF->insert(BBI, CaseBB);
2071 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2072 CaseBB,
2073 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002074
2075 // Put SV in a virtual register to make it available from the new blocks.
2076 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002077 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002078
2079 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002080 -1U, (CR.CaseBB == CurMBB),
2081 CR.CaseBB, Default, BTC);
2082
2083 if (CR.CaseBB == CurMBB)
2084 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002086 BitTestCases.push_back(BTB);
2087
2088 return true;
2089}
2090
2091
2092/// Clusterify - Transform simple list of Cases into list of CaseRange's
Anton Korobeynikov23218582008-12-23 22:25:27 +00002093size_t SelectionDAGLowering::Clusterify(CaseVector& Cases,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002094 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002095 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002096
2097 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002098 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002099 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2100 Cases.push_back(Case(SI.getSuccessorValue(i),
2101 SI.getSuccessorValue(i),
2102 SMBB));
2103 }
2104 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2105
2106 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002107 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002108 // Must recompute end() each iteration because it may be
2109 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002110 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2111 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2112 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002113 MachineBasicBlock* nextBB = J->BB;
2114 MachineBasicBlock* currentBB = I->BB;
2115
2116 // If the two neighboring cases go to the same destination, merge them
2117 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002118 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002119 I->High = J->High;
2120 J = Cases.erase(J);
2121 } else {
2122 I = J++;
2123 }
2124 }
2125
2126 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2127 if (I->Low != I->High)
2128 // A range counts double, since it requires two compares.
2129 ++numCmps;
2130 }
2131
2132 return numCmps;
2133}
2134
Anton Korobeynikov23218582008-12-23 22:25:27 +00002135void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002136 // Figure out which block is immediately after the current one.
2137 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002138
2139 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2140
2141 // If there is only the default destination, branch to it if it is not the
2142 // next basic block. Otherwise, just fall through.
2143 if (SI.getNumOperands() == 2) {
2144 // Update machine-CFG edges.
2145
2146 // If this is not a fall-through branch, emit the branch.
2147 CurMBB->addSuccessor(Default);
2148 if (Default != NextBlock)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002149 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002150 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151 DAG.getBasicBlock(Default)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002152 return;
2153 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002155 // If there are any non-default case statements, create a vector of Cases
2156 // representing each one, and sort the vector so that we can efficiently
2157 // create a binary search tree from them.
2158 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002159 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002160 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2161 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002162 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002163
2164 // Get the Value to be switched on and default basic blocks, which will be
2165 // inserted into CaseBlock records, representing basic blocks in the binary
2166 // search tree.
2167 Value *SV = SI.getOperand(0);
2168
2169 // Push the initial CaseRec onto the worklist
2170 CaseRecVector WorkList;
2171 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2172
2173 while (!WorkList.empty()) {
2174 // Grab a record representing a case range to process off the worklist
2175 CaseRec CR = WorkList.back();
2176 WorkList.pop_back();
2177
2178 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2179 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002180
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002181 // If the range has few cases (two or less) emit a series of specific
2182 // tests.
2183 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2184 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002185
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002186 // If the switch has more than 5 blocks, and at least 40% dense, and the
2187 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002188 // lowering the switch to a binary tree of conditional branches.
2189 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2190 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002191
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002192 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2193 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2194 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2195 }
2196}
2197
Chris Lattnerab21db72009-10-28 00:19:10 +00002198void SelectionDAGLowering::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002199 // Update machine-CFG edges.
2200 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2201 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2202
Dan Gohman64825152009-10-27 21:56:26 +00002203 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2204 MVT::Other, getControlRoot(),
2205 getValue(I.getAddress())));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002206}
2207
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002209void SelectionDAGLowering::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002210 // -0.0 - X --> fneg
2211 const Type *Ty = I.getType();
2212 if (isa<VectorType>(Ty)) {
2213 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2214 const VectorType *DestTy = cast<VectorType>(I.getType());
2215 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002216 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002217 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002218 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002219 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002220 SDValue Op2 = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002221 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002222 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223 return;
2224 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002225 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002227 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002228 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002229 SDValue Op2 = getValue(I.getOperand(1));
2230 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2231 Op2.getValueType(), Op2));
2232 return;
2233 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002234
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002235 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002236}
2237
2238void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
2239 SDValue Op1 = getValue(I.getOperand(0));
2240 SDValue Op2 = getValue(I.getOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002241
Scott Michelfdc40a02009-02-17 22:15:04 +00002242 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002243 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002244}
2245
2246void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2247 SDValue Op1 = getValue(I.getOperand(0));
2248 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002249 if (!isa<VectorType>(I.getType()) &&
2250 Op2.getValueType() != TLI.getShiftAmountTy()) {
2251 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002252 EVT PTy = TLI.getPointerTy();
2253 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002254 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002255 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2256 TLI.getShiftAmountTy(), Op2);
2257 // If the operand is larger than the shift count type but the shift
2258 // count type has enough bits to represent any shift value, truncate
2259 // it now. This is a common case and it exposes the truncate to
2260 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002261 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002262 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2263 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2264 TLI.getShiftAmountTy(), Op2);
2265 // Otherwise we'll need to temporarily settle for some other
2266 // convenient type; type legalization will make adjustments as
2267 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002268 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002269 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002270 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002271 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002272 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002273 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002275
Scott Michelfdc40a02009-02-17 22:15:04 +00002276 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002277 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002278}
2279
2280void SelectionDAGLowering::visitICmp(User &I) {
2281 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2282 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2283 predicate = IC->getPredicate();
2284 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2285 predicate = ICmpInst::Predicate(IC->getPredicate());
2286 SDValue Op1 = getValue(I.getOperand(0));
2287 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002288 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002289
Owen Andersone50ed302009-08-10 22:56:29 +00002290 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002291 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002292}
2293
2294void SelectionDAGLowering::visitFCmp(User &I) {
2295 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2296 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2297 predicate = FC->getPredicate();
2298 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2299 predicate = FCmpInst::Predicate(FC->getPredicate());
2300 SDValue Op1 = getValue(I.getOperand(0));
2301 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002302 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002303 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002304 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002305}
2306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307void SelectionDAGLowering::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002308 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002309 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2310 unsigned NumValues = ValueVTs.size();
2311 if (NumValues != 0) {
2312 SmallVector<SDValue, 4> Values(NumValues);
2313 SDValue Cond = getValue(I.getOperand(0));
2314 SDValue TrueVal = getValue(I.getOperand(1));
2315 SDValue FalseVal = getValue(I.getOperand(2));
2316
2317 for (unsigned i = 0; i != NumValues; ++i)
Scott Michelfdc40a02009-02-17 22:15:04 +00002318 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002319 TrueVal.getValueType(), Cond,
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002320 SDValue(TrueVal.getNode(), TrueVal.getResNo() + i),
2321 SDValue(FalseVal.getNode(), FalseVal.getResNo() + i));
2322
Scott Michelfdc40a02009-02-17 22:15:04 +00002323 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002324 DAG.getVTList(&ValueVTs[0], NumValues),
2325 &Values[0], NumValues));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002326 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002327}
2328
2329
2330void SelectionDAGLowering::visitTrunc(User &I) {
2331 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2332 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002333 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002334 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002335}
2336
2337void SelectionDAGLowering::visitZExt(User &I) {
2338 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2339 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2340 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002341 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002342 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002343}
2344
2345void SelectionDAGLowering::visitSExt(User &I) {
2346 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2347 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2348 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002349 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002350 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002351}
2352
2353void SelectionDAGLowering::visitFPTrunc(User &I) {
2354 // FPTrunc is never a no-op cast, no need to check
2355 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002356 EVT DestVT = TLI.getValueType(I.getType());
Scott Michelfdc40a02009-02-17 22:15:04 +00002357 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002358 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002359}
2360
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002361void SelectionDAGLowering::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002362 // FPTrunc is never a no-op cast, no need to check
2363 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002364 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002365 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002366}
2367
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002368void SelectionDAGLowering::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002369 // FPToUI is never a no-op cast, no need to check
2370 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002371 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002372 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002373}
2374
2375void SelectionDAGLowering::visitFPToSI(User &I) {
2376 // FPToSI is never a no-op cast, no need to check
2377 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002378 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002379 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002380}
2381
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002382void SelectionDAGLowering::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002383 // UIToFP is never a no-op cast, no need to check
2384 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002385 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002386 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002387}
2388
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002389void SelectionDAGLowering::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002390 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002391 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002392 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002393 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002394}
2395
2396void SelectionDAGLowering::visitPtrToInt(User &I) {
2397 // What to do depends on the size of the integer and the size of the pointer.
2398 // We can either truncate, zero extend, or no-op, accordingly.
2399 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002400 EVT SrcVT = N.getValueType();
2401 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002402 SDValue Result = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002403 setValue(&I, Result);
2404}
2405
2406void SelectionDAGLowering::visitIntToPtr(User &I) {
2407 // What to do depends on the size of the integer and the size of the pointer.
2408 // We can either truncate, zero extend, or no-op, accordingly.
2409 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002410 EVT SrcVT = N.getValueType();
2411 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002412 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002413}
2414
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002415void SelectionDAGLowering::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002416 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002417 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002418
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002419 // BitCast assures us that source and destination are the same size so this
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002420 // is either a BIT_CONVERT or a no-op.
2421 if (DestVT != N.getValueType())
Scott Michelfdc40a02009-02-17 22:15:04 +00002422 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002423 DestVT, N)); // convert types
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002424 else
2425 setValue(&I, N); // noop cast.
2426}
2427
2428void SelectionDAGLowering::visitInsertElement(User &I) {
2429 SDValue InVec = getValue(I.getOperand(0));
2430 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002431 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002432 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002433 getValue(I.getOperand(2)));
2434
Scott Michelfdc40a02009-02-17 22:15:04 +00002435 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002436 TLI.getValueType(I.getType()),
2437 InVec, InVal, InIdx));
2438}
2439
2440void SelectionDAGLowering::visitExtractElement(User &I) {
2441 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002442 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002443 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002444 getValue(I.getOperand(1)));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002445 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002446 TLI.getValueType(I.getType()), InVec, InIdx));
2447}
2448
Mon P Wangaeb06d22008-11-10 04:46:22 +00002449
2450// Utility for visitShuffleVector - Returns true if the mask is mask starting
2451// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002452static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2453 unsigned MaskNumElts = Mask.size();
2454 for (unsigned i = 0; i != MaskNumElts; ++i)
2455 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002456 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002457 return true;
2458}
2459
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002460void SelectionDAGLowering::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002461 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002462 SDValue Src1 = getValue(I.getOperand(0));
2463 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002464
Nate Begeman9008ca62009-04-27 18:41:29 +00002465 // Convert the ConstantVector mask operand into an array of ints, with -1
2466 // representing undef values.
2467 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002468 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2469 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002470 unsigned MaskNumElts = MaskElts.size();
2471 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002472 if (isa<UndefValue>(MaskElts[i]))
2473 Mask.push_back(-1);
2474 else
2475 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2476 }
2477
Owen Andersone50ed302009-08-10 22:56:29 +00002478 EVT VT = TLI.getValueType(I.getType());
2479 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002480 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002481
Mon P Wangc7849c22008-11-16 05:06:27 +00002482 if (SrcNumElts == MaskNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002483 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2484 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002485 return;
2486 }
2487
2488 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002489 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2490 // Mask is longer than the source vectors and is a multiple of the source
2491 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002492 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002493 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2494 // The shuffle is concatenating two vectors together.
Scott Michelfdc40a02009-02-17 22:15:04 +00002495 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002496 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002497 return;
2498 }
2499
Mon P Wangc7849c22008-11-16 05:06:27 +00002500 // Pad both vectors with undefs to make them the same length as the mask.
2501 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002502 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2503 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002504 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002505
Nate Begeman9008ca62009-04-27 18:41:29 +00002506 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2507 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002508 MOps1[0] = Src1;
2509 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002510
2511 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2512 getCurDebugLoc(), VT,
2513 &MOps1[0], NumConcat);
2514 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2515 getCurDebugLoc(), VT,
2516 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002517
Mon P Wangaeb06d22008-11-10 04:46:22 +00002518 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002519 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002520 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002521 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002522 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002523 MappedOps.push_back(Idx);
2524 else
2525 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002526 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002527 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2528 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002529 return;
2530 }
2531
Mon P Wangc7849c22008-11-16 05:06:27 +00002532 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002533 // Analyze the access pattern of the vector to see if we can extract
2534 // two subvectors and do the shuffle. The analysis is done by calculating
2535 // the range of elements the mask access on both vectors.
2536 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2537 int MaxRange[2] = {-1, -1};
2538
Nate Begeman5a5ca152009-04-29 05:20:52 +00002539 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002540 int Idx = Mask[i];
2541 int Input = 0;
2542 if (Idx < 0)
2543 continue;
2544
Nate Begeman5a5ca152009-04-29 05:20:52 +00002545 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002546 Input = 1;
2547 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002548 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002549 if (Idx > MaxRange[Input])
2550 MaxRange[Input] = Idx;
2551 if (Idx < MinRange[Input])
2552 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002553 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002554
Mon P Wangc7849c22008-11-16 05:06:27 +00002555 // Check if the access is smaller than the vector size and can we find
2556 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002557 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002558 int StartIdx[2]; // StartIdx to extract from
2559 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002560 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002561 RangeUse[Input] = 0; // Unused
2562 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002563 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002564 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002565 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002566 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002567 RangeUse[Input] = 1; // Extract from beginning of the vector
2568 StartIdx[Input] = 0;
2569 } else {
2570 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002571 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002572 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002573 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002574 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002575 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002576 }
2577
Bill Wendling636e2582009-08-21 18:16:06 +00002578 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002579 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002580 return;
2581 }
2582 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2583 // Extract appropriate subvector and generate a vector shuffle
2584 for (int Input=0; Input < 2; ++Input) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002585 SDValue& Src = Input == 0 ? Src1 : Src2;
Mon P Wangc7849c22008-11-16 05:06:27 +00002586 if (RangeUse[Input] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002587 Src = DAG.getUNDEF(VT);
Mon P Wangc7849c22008-11-16 05:06:27 +00002588 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002589 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002590 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002591 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002592 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002593 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002594 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002595 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002596 int Idx = Mask[i];
2597 if (Idx < 0)
2598 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002599 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002600 MappedOps.push_back(Idx - StartIdx[0]);
2601 else
2602 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002603 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002604 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2605 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002606 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002607 }
2608 }
2609
Mon P Wangc7849c22008-11-16 05:06:27 +00002610 // We can't use either concat vectors or extract subvectors so fall back to
2611 // replacing the shuffle with extract and build vector.
2612 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002613 EVT EltVT = VT.getVectorElementType();
2614 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002615 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002616 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002617 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002618 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002619 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002620 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002621 if (Idx < (int)SrcNumElts)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002622 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002623 EltVT, Src1, DAG.getConstant(Idx, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002624 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002625 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Scott Michelfdc40a02009-02-17 22:15:04 +00002626 EltVT, Src2,
Mon P Wangc7849c22008-11-16 05:06:27 +00002627 DAG.getConstant(Idx - SrcNumElts, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002628 }
2629 }
Evan Chenga87008d2009-02-25 22:49:59 +00002630 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2631 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002632}
2633
2634void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) {
2635 const Value *Op0 = I.getOperand(0);
2636 const Value *Op1 = I.getOperand(1);
2637 const Type *AggTy = I.getType();
2638 const Type *ValTy = Op1->getType();
2639 bool IntoUndef = isa<UndefValue>(Op0);
2640 bool FromUndef = isa<UndefValue>(Op1);
2641
2642 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2643 I.idx_begin(), I.idx_end());
2644
Owen Andersone50ed302009-08-10 22:56:29 +00002645 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002646 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002647 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002648 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2649
2650 unsigned NumAggValues = AggValueVTs.size();
2651 unsigned NumValValues = ValValueVTs.size();
2652 SmallVector<SDValue, 4> Values(NumAggValues);
2653
2654 SDValue Agg = getValue(Op0);
2655 SDValue Val = getValue(Op1);
2656 unsigned i = 0;
2657 // Copy the beginning value(s) from the original aggregate.
2658 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002659 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002660 SDValue(Agg.getNode(), Agg.getResNo() + i);
2661 // Copy values from the inserted value(s).
2662 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002663 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002664 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2665 // Copy remaining value(s) from the original aggregate.
2666 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002667 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002668 SDValue(Agg.getNode(), Agg.getResNo() + i);
2669
Scott Michelfdc40a02009-02-17 22:15:04 +00002670 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002671 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2672 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002673}
2674
2675void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) {
2676 const Value *Op0 = I.getOperand(0);
2677 const Type *AggTy = Op0->getType();
2678 const Type *ValTy = I.getType();
2679 bool OutOfUndef = isa<UndefValue>(Op0);
2680
2681 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2682 I.idx_begin(), I.idx_end());
2683
Owen Andersone50ed302009-08-10 22:56:29 +00002684 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002685 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2686
2687 unsigned NumValValues = ValValueVTs.size();
2688 SmallVector<SDValue, 4> Values(NumValValues);
2689
2690 SDValue Agg = getValue(Op0);
2691 // Copy out the selected value(s).
2692 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2693 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002694 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002695 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002696 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002697
Scott Michelfdc40a02009-02-17 22:15:04 +00002698 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002699 DAG.getVTList(&ValValueVTs[0], NumValValues),
2700 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002701}
2702
2703
2704void SelectionDAGLowering::visitGetElementPtr(User &I) {
2705 SDValue N = getValue(I.getOperand(0));
2706 const Type *Ty = I.getOperand(0)->getType();
2707
2708 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2709 OI != E; ++OI) {
2710 Value *Idx = *OI;
2711 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2712 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2713 if (Field) {
2714 // N = N + Offset
2715 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002716 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002717 DAG.getIntPtrConstant(Offset));
2718 }
2719 Ty = StTy->getElementType(Field);
2720 } else {
2721 Ty = cast<SequentialType>(Ty)->getElementType();
2722
2723 // If this is a constant subscript, handle it quickly.
2724 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2725 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002726 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002727 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002728 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002729 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002730 unsigned PtrBits = PTy.getSizeInBits();
Evan Cheng65b52df2009-02-09 21:01:06 +00002731 if (PtrBits < 64) {
2732 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2733 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002734 DAG.getConstant(Offs, MVT::i64));
Evan Cheng65b52df2009-02-09 21:01:06 +00002735 } else
Evan Chengb1032a82009-02-09 20:54:38 +00002736 OffsVal = DAG.getIntPtrConstant(Offs);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002737 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002738 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002739 continue;
2740 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002741
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002742 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002743 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2744 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002745 SDValue IdxN = getValue(Idx);
2746
2747 // If the index is smaller or larger than intptr_t, truncate or extend
2748 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002749 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002750
2751 // If this is a multiply by a power of two, turn it into a shl
2752 // immediately. This is a very common case.
2753 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002754 if (ElementSize.isPowerOf2()) {
2755 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002756 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002757 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002758 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002759 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002760 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002761 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002762 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002763 }
2764 }
2765
Scott Michelfdc40a02009-02-17 22:15:04 +00002766 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002767 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002768 }
2769 }
2770 setValue(&I, N);
2771}
2772
2773void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2774 // If this is a fixed sized alloca in the entry block of the function,
2775 // allocate it statically on the stack.
2776 if (FuncInfo.StaticAllocaMap.count(&I))
2777 return; // getValue will auto-populate this.
2778
2779 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002780 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002781 unsigned Align =
2782 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2783 I.getAlignment());
2784
2785 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002786
2787 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2788 AllocSize,
2789 DAG.getConstant(TySize, AllocSize.getValueType()));
2790
2791
2792
Owen Andersone50ed302009-08-10 22:56:29 +00002793 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002794 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002795
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796 // Handle alignment. If the requested alignment is less than or equal to
2797 // the stack alignment, ignore it. If the size is greater than or equal to
2798 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2799 unsigned StackAlign =
2800 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2801 if (Align <= StackAlign)
2802 Align = 0;
2803
2804 // Round the size of the allocation up to the stack alignment size
2805 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002806 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002807 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 DAG.getIntPtrConstant(StackAlign-1));
2809 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002810 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002811 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002812 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2813
2814 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002815 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002816 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002817 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002818 setValue(&I, DSA);
2819 DAG.setRoot(DSA.getValue(1));
2820
2821 // Inform the Frame Information that we have just allocated a variable-sized
2822 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002823 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002824}
2825
2826void SelectionDAGLowering::visitLoad(LoadInst &I) {
2827 const Value *SV = I.getOperand(0);
2828 SDValue Ptr = getValue(SV);
2829
2830 const Type *Ty = I.getType();
2831 bool isVolatile = I.isVolatile();
2832 unsigned Alignment = I.getAlignment();
2833
Owen Andersone50ed302009-08-10 22:56:29 +00002834 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002835 SmallVector<uint64_t, 4> Offsets;
2836 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2837 unsigned NumValues = ValueVTs.size();
2838 if (NumValues == 0)
2839 return;
2840
2841 SDValue Root;
2842 bool ConstantMemory = false;
2843 if (I.isVolatile())
2844 // Serialize volatile loads with other side effects.
2845 Root = getRoot();
2846 else if (AA->pointsToConstantMemory(SV)) {
2847 // Do not serialize (non-volatile) loads of constant memory with anything.
2848 Root = DAG.getEntryNode();
2849 ConstantMemory = true;
2850 } else {
2851 // Do not serialize non-volatile loads against each other.
2852 Root = DAG.getRoot();
2853 }
2854
2855 SmallVector<SDValue, 4> Values(NumValues);
2856 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002857 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002858 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002859 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Nate Begemane6798372009-09-15 00:13:12 +00002860 DAG.getNode(ISD::ADD, getCurDebugLoc(),
2861 PtrVT, Ptr,
2862 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002863 SV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002864 Values[i] = L;
2865 Chains[i] = L.getValue(1);
2866 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002867
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002868 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002869 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002870 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002871 &Chains[0], NumValues);
2872 if (isVolatile)
2873 DAG.setRoot(Chain);
2874 else
2875 PendingLoads.push_back(Chain);
2876 }
2877
Scott Michelfdc40a02009-02-17 22:15:04 +00002878 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002879 DAG.getVTList(&ValueVTs[0], NumValues),
2880 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881}
2882
2883
2884void SelectionDAGLowering::visitStore(StoreInst &I) {
2885 Value *SrcV = I.getOperand(0);
2886 Value *PtrV = I.getOperand(1);
2887
Owen Andersone50ed302009-08-10 22:56:29 +00002888 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002889 SmallVector<uint64_t, 4> Offsets;
2890 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2891 unsigned NumValues = ValueVTs.size();
2892 if (NumValues == 0)
2893 return;
2894
2895 // Get the lowered operands. Note that we do this after
2896 // checking if NumResults is zero, because with zero results
2897 // the operands won't have values in the map.
2898 SDValue Src = getValue(SrcV);
2899 SDValue Ptr = getValue(PtrV);
2900
2901 SDValue Root = getRoot();
2902 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002903 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002904 bool isVolatile = I.isVolatile();
2905 unsigned Alignment = I.getAlignment();
2906 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002907 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002908 SDValue(Src.getNode(), Src.getResNo() + i),
Scott Michelfdc40a02009-02-17 22:15:04 +00002909 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002910 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002911 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002912 PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002913
Scott Michelfdc40a02009-02-17 22:15:04 +00002914 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002915 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002916}
2917
2918/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2919/// node.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002920void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002921 unsigned Intrinsic) {
2922 bool HasChain = !I.doesNotAccessMemory();
2923 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2924
2925 // Build the operand list.
2926 SmallVector<SDValue, 8> Ops;
2927 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2928 if (OnlyLoad) {
2929 // We don't need to serialize loads against other loads.
2930 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002931 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002932 Ops.push_back(getRoot());
2933 }
2934 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002935
2936 // Info is set by getTgtMemInstrinsic
2937 TargetLowering::IntrinsicInfo Info;
2938 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2939
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002940 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002941 if (!IsTgtIntrinsic)
2942 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002943
2944 // Add all operands of the call to the operand list.
2945 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2946 SDValue Op = getValue(I.getOperand(i));
2947 assert(TLI.isTypeLegal(Op.getValueType()) &&
2948 "Intrinsic uses a non-legal type?");
2949 Ops.push_back(Op);
2950 }
2951
Owen Andersone50ed302009-08-10 22:56:29 +00002952 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002953 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2954#ifndef NDEBUG
2955 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2956 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2957 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002958 }
Bob Wilson8d919552009-07-31 22:41:21 +00002959#endif // NDEBUG
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002960 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002961 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002962
Bob Wilson8d919552009-07-31 22:41:21 +00002963 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002964
2965 // Create the node.
2966 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002967 if (IsTgtIntrinsic) {
2968 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002969 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002970 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002971 Info.memVT, Info.ptrVal, Info.offset,
2972 Info.align, Info.vol,
2973 Info.readMem, Info.writeMem);
2974 }
2975 else if (!HasChain)
Scott Michelfdc40a02009-02-17 22:15:04 +00002976 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002977 VTs, &Ops[0], Ops.size());
Owen Anderson1d0be152009-08-13 21:58:54 +00002978 else if (I.getType() != Type::getVoidTy(*DAG.getContext()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002979 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002980 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002981 else
Scott Michelfdc40a02009-02-17 22:15:04 +00002982 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002983 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002984
2985 if (HasChain) {
2986 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2987 if (OnlyLoad)
2988 PendingLoads.push_back(Chain);
2989 else
2990 DAG.setRoot(Chain);
2991 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002992 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002993 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002994 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002995 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002996 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002997 setValue(&I, Result);
2998 }
2999}
3000
3001/// ExtractTypeInfo - Returns the type info, possibly bitcast, encoded in V.
3002static GlobalVariable *ExtractTypeInfo(Value *V) {
3003 V = V->stripPointerCasts();
3004 GlobalVariable *GV = dyn_cast<GlobalVariable>(V);
3005 assert ((GV || isa<ConstantPointerNull>(V)) &&
3006 "TypeInfo must be a global variable or NULL");
3007 return GV;
3008}
3009
3010namespace llvm {
3011
3012/// AddCatchInfo - Extract the personality and type infos from an eh.selector
3013/// call, and add them to the specified machine basic block.
3014void AddCatchInfo(CallInst &I, MachineModuleInfo *MMI,
3015 MachineBasicBlock *MBB) {
3016 // Inform the MachineModuleInfo of the personality for this landing pad.
3017 ConstantExpr *CE = cast<ConstantExpr>(I.getOperand(2));
3018 assert(CE->getOpcode() == Instruction::BitCast &&
3019 isa<Function>(CE->getOperand(0)) &&
3020 "Personality should be a function");
3021 MMI->addPersonality(MBB, cast<Function>(CE->getOperand(0)));
3022
3023 // Gather all the type infos for this landing pad and pass them along to
3024 // MachineModuleInfo.
3025 std::vector<GlobalVariable *> TyInfo;
3026 unsigned N = I.getNumOperands();
3027
3028 for (unsigned i = N - 1; i > 2; --i) {
3029 if (ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(i))) {
3030 unsigned FilterLength = CI->getZExtValue();
3031 unsigned FirstCatch = i + FilterLength + !FilterLength;
3032 assert (FirstCatch <= N && "Invalid filter length");
3033
3034 if (FirstCatch < N) {
3035 TyInfo.reserve(N - FirstCatch);
3036 for (unsigned j = FirstCatch; j < N; ++j)
3037 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3038 MMI->addCatchTypeInfo(MBB, TyInfo);
3039 TyInfo.clear();
3040 }
3041
3042 if (!FilterLength) {
3043 // Cleanup.
3044 MMI->addCleanup(MBB);
3045 } else {
3046 // Filter.
3047 TyInfo.reserve(FilterLength - 1);
3048 for (unsigned j = i + 1; j < FirstCatch; ++j)
3049 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3050 MMI->addFilterTypeInfo(MBB, TyInfo);
3051 TyInfo.clear();
3052 }
3053
3054 N = i;
3055 }
3056 }
3057
3058 if (N > 3) {
3059 TyInfo.reserve(N - 3);
3060 for (unsigned j = 3; j < N; ++j)
3061 TyInfo.push_back(ExtractTypeInfo(I.getOperand(j)));
3062 MMI->addCatchTypeInfo(MBB, TyInfo);
3063 }
3064}
3065
3066}
3067
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003068/// GetSignificand - Get the significand and build it into a floating-point
3069/// number with exponent of 1:
3070///
3071/// Op = (Op & 0x007fffff) | 0x3f800000;
3072///
3073/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003074static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003075GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003076 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3077 DAG.getConstant(0x007fffff, MVT::i32));
3078 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3079 DAG.getConstant(0x3f800000, MVT::i32));
3080 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003081}
3082
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003083/// GetExponent - Get the exponent:
3084///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003085/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003086///
3087/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003088static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003089GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
3090 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003091 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3092 DAG.getConstant(0x7f800000, MVT::i32));
3093 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003094 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3096 DAG.getConstant(127, MVT::i32));
3097 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003098}
3099
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003100/// getF32Constant - Get 32-bit floating point constant.
3101static SDValue
3102getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003103 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003104}
3105
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003106/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003107/// visitIntrinsicCall: I is a call instruction
3108/// Op is the associated NodeType for I
3109const char *
3110SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003111 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003112 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003113 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003114 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003115 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003116 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003117 getValue(I.getOperand(2)),
3118 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003119 setValue(&I, L);
3120 DAG.setRoot(L.getValue(1));
3121 return 0;
3122}
3123
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003124// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003125const char *
3126SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003127 SDValue Op1 = getValue(I.getOperand(1));
3128 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003129
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003131 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003132
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003133 setValue(&I, Result);
3134 return 0;
3135}
Bill Wendling74c37652008-12-09 22:08:41 +00003136
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003137/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3138/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003139void
3140SelectionDAGLowering::visitExp(CallInst &I) {
3141 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003142 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003143
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003145 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3146 SDValue Op = getValue(I.getOperand(1));
3147
3148 // Put the exponent in the right bit position for later addition to the
3149 // final result:
3150 //
3151 // #define LOG2OFe 1.4426950f
3152 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003153 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003154 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003156
3157 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3159 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003160
3161 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003162 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003163 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003164
3165 if (LimitFloatPrecision <= 6) {
3166 // For floating-point precision of 6:
3167 //
3168 // TwoToFractionalPartOfX =
3169 // 0.997535578f +
3170 // (0.735607626f + 0.252464424f * x) * x;
3171 //
3172 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003173 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003174 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003175 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003176 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003177 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3178 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003179 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003180 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003181
3182 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003183 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003184 TwoToFracPartOfX, IntegerPartOfX);
3185
Owen Anderson825b72b2009-08-11 20:47:22 +00003186 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003187 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3188 // For floating-point precision of 12:
3189 //
3190 // TwoToFractionalPartOfX =
3191 // 0.999892986f +
3192 // (0.696457318f +
3193 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3194 //
3195 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003196 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003197 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003199 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003200 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3201 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003202 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3204 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003205 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003206 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003207
3208 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003209 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003210 TwoToFracPartOfX, IntegerPartOfX);
3211
Owen Anderson825b72b2009-08-11 20:47:22 +00003212 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003213 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3214 // For floating-point precision of 18:
3215 //
3216 // TwoToFractionalPartOfX =
3217 // 0.999999982f +
3218 // (0.693148872f +
3219 // (0.240227044f +
3220 // (0.554906021e-1f +
3221 // (0.961591928e-2f +
3222 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3223 //
3224 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003225 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003226 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003227 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003228 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3230 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003231 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3233 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003234 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003235 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3236 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003237 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003238 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3239 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003240 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003241 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3242 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003243 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003244 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003246
3247 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003249 TwoToFracPartOfX, IntegerPartOfX);
3250
Owen Anderson825b72b2009-08-11 20:47:22 +00003251 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003252 }
3253 } else {
3254 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003255 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003256 getValue(I.getOperand(1)).getValueType(),
3257 getValue(I.getOperand(1)));
3258 }
3259
Dale Johannesen59e577f2008-09-05 18:38:42 +00003260 setValue(&I, result);
3261}
3262
Bill Wendling39150252008-09-09 20:39:27 +00003263/// visitLog - Lower a log intrinsic. Handles the special sequences for
3264/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003265void
3266SelectionDAGLowering::visitLog(CallInst &I) {
3267 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003268 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003269
Owen Anderson825b72b2009-08-11 20:47:22 +00003270 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003271 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3272 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003273 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003274
3275 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003276 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003277 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003278 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003279
3280 // Get the significand and build it into a floating-point number with
3281 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003282 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003283
3284 if (LimitFloatPrecision <= 6) {
3285 // For floating-point precision of 6:
3286 //
3287 // LogofMantissa =
3288 // -1.1609546f +
3289 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003290 //
Bill Wendling39150252008-09-09 20:39:27 +00003291 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003293 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003295 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003296 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3297 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003298 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003299
Scott Michelfdc40a02009-02-17 22:15:04 +00003300 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003301 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003302 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3303 // For floating-point precision of 12:
3304 //
3305 // LogOfMantissa =
3306 // -1.7417939f +
3307 // (2.8212026f +
3308 // (-1.4699568f +
3309 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3310 //
3311 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003312 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003313 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003315 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3317 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003318 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3320 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003321 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003322 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3323 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003324 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003325
Scott Michelfdc40a02009-02-17 22:15:04 +00003326 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003328 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3329 // For floating-point precision of 18:
3330 //
3331 // LogOfMantissa =
3332 // -2.1072184f +
3333 // (4.2372794f +
3334 // (-3.7029485f +
3335 // (2.2781945f +
3336 // (-0.87823314f +
3337 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3338 //
3339 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003341 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003343 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3345 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003346 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3348 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3351 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003352 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3354 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003355 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3357 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003358 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003359
Scott Michelfdc40a02009-02-17 22:15:04 +00003360 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003362 }
3363 } else {
3364 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003365 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003366 getValue(I.getOperand(1)).getValueType(),
3367 getValue(I.getOperand(1)));
3368 }
3369
Dale Johannesen59e577f2008-09-05 18:38:42 +00003370 setValue(&I, result);
3371}
3372
Bill Wendling3eb59402008-09-09 00:28:24 +00003373/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3374/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003375void
3376SelectionDAGLowering::visitLog2(CallInst &I) {
3377 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003378 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003379
Owen Anderson825b72b2009-08-11 20:47:22 +00003380 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003381 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3382 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003383 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003384
Bill Wendling39150252008-09-09 20:39:27 +00003385 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003386 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003387
3388 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003389 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003390 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003391
Bill Wendling3eb59402008-09-09 00:28:24 +00003392 // Different possible minimax approximations of significand in
3393 // floating-point for various degrees of accuracy over [1,2].
3394 if (LimitFloatPrecision <= 6) {
3395 // For floating-point precision of 6:
3396 //
3397 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3398 //
3399 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003401 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003403 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003404 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3405 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003406 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003407
Scott Michelfdc40a02009-02-17 22:15:04 +00003408 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003409 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003410 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3411 // For floating-point precision of 12:
3412 //
3413 // Log2ofMantissa =
3414 // -2.51285454f +
3415 // (4.07009056f +
3416 // (-2.12067489f +
3417 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003418 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003419 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003420 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003421 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003423 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3425 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003426 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3428 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003429 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003430 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3431 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003432 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003433
Scott Michelfdc40a02009-02-17 22:15:04 +00003434 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003435 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003436 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3437 // For floating-point precision of 18:
3438 //
3439 // Log2ofMantissa =
3440 // -3.0400495f +
3441 // (6.1129976f +
3442 // (-5.3420409f +
3443 // (3.2865683f +
3444 // (-1.2669343f +
3445 // (0.27515199f -
3446 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3447 //
3448 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003449 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003450 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003451 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3454 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003455 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3457 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003458 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3460 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003461 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3463 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003464 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3466 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003467 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003468
Scott Michelfdc40a02009-02-17 22:15:04 +00003469 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003470 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003471 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003472 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003473 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003474 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003475 getValue(I.getOperand(1)).getValueType(),
3476 getValue(I.getOperand(1)));
3477 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003478
Dale Johannesen59e577f2008-09-05 18:38:42 +00003479 setValue(&I, result);
3480}
3481
Bill Wendling3eb59402008-09-09 00:28:24 +00003482/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3483/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003484void
3485SelectionDAGLowering::visitLog10(CallInst &I) {
3486 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003487 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003488
Owen Anderson825b72b2009-08-11 20:47:22 +00003489 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003490 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3491 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003492 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003493
Bill Wendling39150252008-09-09 20:39:27 +00003494 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003495 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003496 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003497 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003498
3499 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003500 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003501 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003502
3503 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003504 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003505 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003506 // Log10ofMantissa =
3507 // -0.50419619f +
3508 // (0.60948995f - 0.10380950f * x) * x;
3509 //
3510 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003512 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003513 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003514 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3516 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003517 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003518
Scott Michelfdc40a02009-02-17 22:15:04 +00003519 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003521 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3522 // For floating-point precision of 12:
3523 //
3524 // Log10ofMantissa =
3525 // -0.64831180f +
3526 // (0.91751397f +
3527 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3528 //
3529 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003531 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003533 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3535 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003536 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3538 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003539 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003540
Scott Michelfdc40a02009-02-17 22:15:04 +00003541 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003543 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003544 // For floating-point precision of 18:
3545 //
3546 // Log10ofMantissa =
3547 // -0.84299375f +
3548 // (1.5327582f +
3549 // (-1.0688956f +
3550 // (0.49102474f +
3551 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3552 //
3553 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003555 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003556 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003557 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3559 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003560 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3562 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3565 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3568 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003569 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003570
Scott Michelfdc40a02009-02-17 22:15:04 +00003571 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003573 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003574 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003575 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003576 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003577 getValue(I.getOperand(1)).getValueType(),
3578 getValue(I.getOperand(1)));
3579 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003580
Dale Johannesen59e577f2008-09-05 18:38:42 +00003581 setValue(&I, result);
3582}
3583
Bill Wendlinge10c8142008-09-09 22:39:21 +00003584/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3585/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003586void
3587SelectionDAGLowering::visitExp2(CallInst &I) {
3588 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003589 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003590
Owen Anderson825b72b2009-08-11 20:47:22 +00003591 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003592 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3593 SDValue Op = getValue(I.getOperand(1));
3594
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003596
3597 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003598 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3599 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003600
3601 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003603 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003604
3605 if (LimitFloatPrecision <= 6) {
3606 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003607 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003608 // TwoToFractionalPartOfX =
3609 // 0.997535578f +
3610 // (0.735607626f + 0.252464424f * x) * x;
3611 //
3612 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003614 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003615 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003616 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003617 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3618 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003619 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003620 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003621 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003622 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003623
Scott Michelfdc40a02009-02-17 22:15:04 +00003624 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003625 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003626 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3627 // For floating-point precision of 12:
3628 //
3629 // TwoToFractionalPartOfX =
3630 // 0.999892986f +
3631 // (0.696457318f +
3632 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3633 //
3634 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003635 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003636 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003637 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003638 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003639 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3640 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003641 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003642 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3643 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003644 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003645 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003646 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003648
Scott Michelfdc40a02009-02-17 22:15:04 +00003649 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003650 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003651 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3652 // For floating-point precision of 18:
3653 //
3654 // TwoToFractionalPartOfX =
3655 // 0.999999982f +
3656 // (0.693148872f +
3657 // (0.240227044f +
3658 // (0.554906021e-1f +
3659 // (0.961591928e-2f +
3660 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3661 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003662 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003663 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003664 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003665 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003666 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3667 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003668 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003669 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3670 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003671 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003672 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3673 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003674 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003675 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3676 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003677 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003678 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3679 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003680 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003681 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003682 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003684
Scott Michelfdc40a02009-02-17 22:15:04 +00003685 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003686 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003687 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003688 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003689 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003690 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003691 getValue(I.getOperand(1)).getValueType(),
3692 getValue(I.getOperand(1)));
3693 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003694
Dale Johannesen601d3c02008-09-05 01:48:15 +00003695 setValue(&I, result);
3696}
3697
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003698/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3699/// limited-precision mode with x == 10.0f.
3700void
3701SelectionDAGLowering::visitPow(CallInst &I) {
3702 SDValue result;
3703 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003704 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003705 bool IsExp10 = false;
3706
Owen Anderson825b72b2009-08-11 20:47:22 +00003707 if (getValue(Val).getValueType() == MVT::f32 &&
3708 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003709 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3710 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3711 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3712 APFloat Ten(10.0f);
3713 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3714 }
3715 }
3716 }
3717
3718 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3719 SDValue Op = getValue(I.getOperand(2));
3720
3721 // Put the exponent in the right bit position for later addition to the
3722 // final result:
3723 //
3724 // #define LOG2OF10 3.3219281f
3725 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003726 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003727 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003728 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003729
3730 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003731 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3732 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003733
3734 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003735 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003736 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003737
3738 if (LimitFloatPrecision <= 6) {
3739 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003740 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003741 // twoToFractionalPartOfX =
3742 // 0.997535578f +
3743 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003744 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003745 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003746 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003747 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003748 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003749 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003750 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3751 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003752 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003753 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003754 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003755 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003756
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003757 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003758 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003759 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3760 // For floating-point precision of 12:
3761 //
3762 // TwoToFractionalPartOfX =
3763 // 0.999892986f +
3764 // (0.696457318f +
3765 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3766 //
3767 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003768 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003769 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003770 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003771 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003772 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3773 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003774 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003775 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3776 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003777 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003778 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003779 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003780 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003781
Scott Michelfdc40a02009-02-17 22:15:04 +00003782 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003783 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003784 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3785 // For floating-point precision of 18:
3786 //
3787 // TwoToFractionalPartOfX =
3788 // 0.999999982f +
3789 // (0.693148872f +
3790 // (0.240227044f +
3791 // (0.554906021e-1f +
3792 // (0.961591928e-2f +
3793 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3794 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003795 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003796 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003797 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003798 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3800 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003801 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3803 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003804 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3806 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003807 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003808 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3809 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003810 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3812 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003813 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003815 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003816 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003817
Scott Michelfdc40a02009-02-17 22:15:04 +00003818 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003819 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003820 }
3821 } else {
3822 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003823 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003824 getValue(I.getOperand(1)).getValueType(),
3825 getValue(I.getOperand(1)),
3826 getValue(I.getOperand(2)));
3827 }
3828
3829 setValue(&I, result);
3830}
3831
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003832/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3833/// we want to emit this as a call to a named external function, return the name
3834/// otherwise lower it and return null.
3835const char *
3836SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003837 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003838 switch (Intrinsic) {
3839 default:
3840 // By default, turn this into a target intrinsic node.
3841 visitTargetIntrinsic(I, Intrinsic);
3842 return 0;
3843 case Intrinsic::vastart: visitVAStart(I); return 0;
3844 case Intrinsic::vaend: visitVAEnd(I); return 0;
3845 case Intrinsic::vacopy: visitVACopy(I); return 0;
3846 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003847 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003848 getValue(I.getOperand(1))));
3849 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003850 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003851 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003852 getValue(I.getOperand(1))));
3853 return 0;
3854 case Intrinsic::setjmp:
3855 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3856 break;
3857 case Intrinsic::longjmp:
3858 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3859 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003860 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003861 SDValue Op1 = getValue(I.getOperand(1));
3862 SDValue Op2 = getValue(I.getOperand(2));
3863 SDValue Op3 = getValue(I.getOperand(3));
3864 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003865 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003866 I.getOperand(1), 0, I.getOperand(2), 0));
3867 return 0;
3868 }
Chris Lattner824b9582008-11-21 16:42:48 +00003869 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003870 SDValue Op1 = getValue(I.getOperand(1));
3871 SDValue Op2 = getValue(I.getOperand(2));
3872 SDValue Op3 = getValue(I.getOperand(3));
3873 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003874 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003875 I.getOperand(1), 0));
3876 return 0;
3877 }
Chris Lattner824b9582008-11-21 16:42:48 +00003878 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003879 SDValue Op1 = getValue(I.getOperand(1));
3880 SDValue Op2 = getValue(I.getOperand(2));
3881 SDValue Op3 = getValue(I.getOperand(3));
3882 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3883
3884 // If the source and destination are known to not be aliases, we can
3885 // lower memmove as memcpy.
3886 uint64_t Size = -1ULL;
3887 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003888 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003889 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3890 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00003891 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003892 I.getOperand(1), 0, I.getOperand(2), 0));
3893 return 0;
3894 }
3895
Dale Johannesena04b7572009-02-03 23:04:43 +00003896 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003897 I.getOperand(1), 0, I.getOperand(2), 0));
3898 return 0;
3899 }
3900 case Intrinsic::dbg_stoppoint: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003901 DbgStopPointInst &SPI = cast<DbgStopPointInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003902 if (isValidDebugInfoIntrinsic(SPI, CodeGenOpt::Default)) {
Evan Chenge3d42322009-02-25 07:04:34 +00003903 MachineFunction &MF = DAG.getMachineFunction();
Devang Patel7e1e31f2009-07-02 22:43:26 +00003904 DebugLoc Loc = ExtractDebugLocation(SPI, MF.getDebugLocInfo());
Chris Lattneraf29a522009-05-04 22:10:05 +00003905 setCurDebugLoc(Loc);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003906
Bill Wendling98a366d2009-04-29 23:29:43 +00003907 if (OptLevel == CodeGenOpt::None)
Chris Lattneraf29a522009-05-04 22:10:05 +00003908 DAG.setRoot(DAG.getDbgStopPoint(Loc, getRoot(),
Dale Johannesenbeaec4c2009-03-25 17:36:08 +00003909 SPI.getLine(),
3910 SPI.getColumn(),
3911 SPI.getContext()));
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003912 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003913 return 0;
3914 }
3915 case Intrinsic::dbg_region_start: {
Devang Patel83489bb2009-01-13 00:35:13 +00003916 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003917 DbgRegionStartInst &RSI = cast<DbgRegionStartInst>(I);
Devang Patel7e1e31f2009-07-02 22:43:26 +00003918 if (isValidDebugInfoIntrinsic(RSI, OptLevel) && DW
3919 && DW->ShouldEmitDwarfDebug()) {
Bill Wendlingdf7d5d32009-05-21 00:04:55 +00003920 unsigned LabelID =
Devang Patele4b27562009-08-28 23:24:31 +00003921 DW->RecordRegionStart(RSI.getContext());
Devang Patel48c7fa22009-04-13 18:13:16 +00003922 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3923 getRoot(), LabelID));
Bill Wendling92c1e122009-02-13 02:16:35 +00003924 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003925 return 0;
3926 }
3927 case Intrinsic::dbg_region_end: {
Devang Patel83489bb2009-01-13 00:35:13 +00003928 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003929 DbgRegionEndInst &REI = cast<DbgRegionEndInst>(I);
Devang Patel0f7fef32009-04-13 17:02:03 +00003930
Devang Patel7e1e31f2009-07-02 22:43:26 +00003931 if (!isValidDebugInfoIntrinsic(REI, OptLevel) || !DW
3932 || !DW->ShouldEmitDwarfDebug())
3933 return 0;
Bill Wendling6c4311d2009-05-08 21:14:49 +00003934
Devang Patele4b27562009-08-28 23:24:31 +00003935 DISubprogram Subprogram(REI.getContext());
Devang Patel7e1e31f2009-07-02 22:43:26 +00003936
Devang Patel7e1e31f2009-07-02 22:43:26 +00003937 unsigned LabelID =
Devang Patele4b27562009-08-28 23:24:31 +00003938 DW->RecordRegionEnd(REI.getContext());
Devang Patel7e1e31f2009-07-02 22:43:26 +00003939 DAG.setRoot(DAG.getLabel(ISD::DBG_LABEL, getCurDebugLoc(),
3940 getRoot(), LabelID));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003941 return 0;
3942 }
3943 case Intrinsic::dbg_func_start: {
Devang Patel83489bb2009-01-13 00:35:13 +00003944 DwarfWriter *DW = DAG.getDwarfWriter();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003945 DbgFuncStartInst &FSI = cast<DbgFuncStartInst>(I);
Jeffrey Yasskin32360a72009-07-16 21:07:26 +00003946 if (!isValidDebugInfoIntrinsic(FSI, CodeGenOpt::None))
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00003947 return 0;
Devang Patel16f2ffd2009-04-16 02:33:41 +00003948
Argyrios Kyrtzidis77eaa682009-05-03 08:50:41 +00003949 MachineFunction &MF = DAG.getMachineFunction();
Devang Patel7e1e31f2009-07-02 22:43:26 +00003950 MF.setDefaultDebugLoc(ExtractDebugLocation(FSI, MF.getDebugLocInfo()));
Jeffrey Yasskin32360a72009-07-16 21:07:26 +00003951
3952 if (!DW || !DW->ShouldEmitDwarfDebug())
3953 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003954 // llvm.dbg.func_start also defines beginning of function scope.
Devang Patele4b27562009-08-28 23:24:31 +00003955 DW->RecordRegionStart(FSI.getSubprogram());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003956 return 0;
3957 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003958 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00003959 if (OptLevel != CodeGenOpt::None)
3960 // FIXME: Variable debug info is not supported here.
3961 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00003962 DwarfWriter *DW = DAG.getDwarfWriter();
3963 if (!DW)
3964 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003965 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3966 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
3967 return 0;
3968
Devang Patelac1ceb32009-10-09 22:42:28 +00003969 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00003970 Value *Address = DI.getAddress();
3971 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
3972 Address = BCI->getOperand(0);
3973 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
3974 // Don't handle byval struct arguments or VLAs, for example.
3975 if (!AI)
3976 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003977 DenseMap<const AllocaInst*, int>::iterator SI =
3978 FuncInfo.StaticAllocaMap.find(AI);
3979 if (SI == FuncInfo.StaticAllocaMap.end())
3980 return 0; // VLAs.
3981 int FI = SI->second;
Devang Patelac1ceb32009-10-09 22:42:28 +00003982#ifdef ATTACH_DEBUG_INFO_TO_AN_INSN
3983 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00003984 if (MMI) {
3985 MetadataContext &TheMetadata =
3986 DI.getParent()->getContext().getMetadata();
3987 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
3988 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
3989 MMI->setVariableDbgInfo(Variable, FI, Dbg);
3990 }
Devang Patelac1ceb32009-10-09 22:42:28 +00003991#else
3992 DW->RecordVariable(Variable, FI);
3993#endif
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003994 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003995 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003996 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003997 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00003998 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003999 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004000 SDValue Ops[1];
4001 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004002 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004003 setValue(&I, Op);
4004 DAG.setRoot(Op.getValue(1));
4005 return 0;
4006 }
4007
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004008 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004009 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004010
Chris Lattner3a5815f2009-09-17 23:54:54 +00004011 if (CurMBB->isLandingPad())
4012 AddCatchInfo(I, MMI, CurMBB);
4013 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004014#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004015 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004016#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004017 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4018 unsigned Reg = TLI.getExceptionSelectorRegister();
4019 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004020 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004021
Chris Lattner3a5815f2009-09-17 23:54:54 +00004022 // Insert the EHSELECTION instruction.
4023 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4024 SDValue Ops[2];
4025 Ops[0] = getValue(I.getOperand(1));
4026 Ops[1] = getRoot();
4027 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4028
4029 DAG.setRoot(Op.getValue(1));
4030
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004031 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004032 return 0;
4033 }
4034
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004035 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004036 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004037
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004038 if (MMI) {
4039 // Find the type id for the given typeinfo.
4040 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
4041
4042 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004043 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004044 } else {
4045 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004046 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004047 }
4048
4049 return 0;
4050 }
4051
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004052 case Intrinsic::eh_return_i32:
4053 case Intrinsic::eh_return_i64:
4054 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004055 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004056 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004057 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004058 getControlRoot(),
4059 getValue(I.getOperand(1)),
4060 getValue(I.getOperand(2))));
4061 } else {
4062 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4063 }
4064
4065 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004066 case Intrinsic::eh_unwind_init:
4067 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4068 MMI->setCallsUnwindInit(true);
4069 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004070
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004071 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004072
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004073 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004074 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004075 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4076 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004077
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004078 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004079 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004080 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004081 TLI.getPointerTy()),
4082 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004083 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004084 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004085 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004086 TLI.getPointerTy(),
4087 DAG.getConstant(0,
4088 TLI.getPointerTy())),
4089 Offset));
4090 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004091 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004092 case Intrinsic::convertff:
4093 case Intrinsic::convertfsi:
4094 case Intrinsic::convertfui:
4095 case Intrinsic::convertsif:
4096 case Intrinsic::convertuif:
4097 case Intrinsic::convertss:
4098 case Intrinsic::convertsu:
4099 case Intrinsic::convertus:
4100 case Intrinsic::convertuu: {
4101 ISD::CvtCode Code = ISD::CVT_INVALID;
4102 switch (Intrinsic) {
4103 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4104 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4105 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4106 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4107 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4108 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4109 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4110 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4111 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4112 }
Owen Andersone50ed302009-08-10 22:56:29 +00004113 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00004114 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00004115 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00004116 DAG.getValueType(DestVT),
4117 DAG.getValueType(getValue(Op1).getValueType()),
4118 getValue(I.getOperand(2)),
4119 getValue(I.getOperand(3)),
4120 Code));
4121 return 0;
4122 }
4123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004124 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004125 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004126 getValue(I.getOperand(1)).getValueType(),
4127 getValue(I.getOperand(1))));
4128 return 0;
4129 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004130 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004131 getValue(I.getOperand(1)).getValueType(),
4132 getValue(I.getOperand(1)),
4133 getValue(I.getOperand(2))));
4134 return 0;
4135 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004136 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004137 getValue(I.getOperand(1)).getValueType(),
4138 getValue(I.getOperand(1))));
4139 return 0;
4140 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004141 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004142 getValue(I.getOperand(1)).getValueType(),
4143 getValue(I.getOperand(1))));
4144 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004145 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004146 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004147 return 0;
4148 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004149 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004150 return 0;
4151 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004152 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004153 return 0;
4154 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004155 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004156 return 0;
4157 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004158 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004159 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004160 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004161 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004162 return 0;
4163 case Intrinsic::pcmarker: {
4164 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004166 return 0;
4167 }
4168 case Intrinsic::readcyclecounter: {
4169 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004170 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004171 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004172 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004173 setValue(&I, Tmp);
4174 DAG.setRoot(Tmp.getValue(1));
4175 return 0;
4176 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004177 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004178 setValue(&I, DAG.getNode(ISD::BSWAP, 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::cttz: {
4183 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004184 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004185 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004186 setValue(&I, result);
4187 return 0;
4188 }
4189 case Intrinsic::ctlz: {
4190 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004191 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004192 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004193 setValue(&I, result);
4194 return 0;
4195 }
4196 case Intrinsic::ctpop: {
4197 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004198 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004199 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004200 setValue(&I, result);
4201 return 0;
4202 }
4203 case Intrinsic::stacksave: {
4204 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004205 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004206 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004207 setValue(&I, Tmp);
4208 DAG.setRoot(Tmp.getValue(1));
4209 return 0;
4210 }
4211 case Intrinsic::stackrestore: {
4212 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004214 return 0;
4215 }
Bill Wendling57344502008-11-18 11:01:33 +00004216 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004217 // Emit code into the DAG to store the stack guard onto the stack.
4218 MachineFunction &MF = DAG.getMachineFunction();
4219 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004220 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004221
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004222 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4223 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004224
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004225 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004226 MFI->setStackProtectorIndex(FI);
4227
4228 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4229
4230 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004231 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00004232 PseudoSourceValue::getFixedStack(FI),
4233 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004234 setValue(&I, Result);
4235 DAG.setRoot(Result);
4236 return 0;
4237 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004238 case Intrinsic::objectsize: {
4239 // If we don't know by now, we're never going to know.
4240 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4241
4242 assert(CI && "Non-constant type in __builtin_object_size?");
4243
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004244 SDValue Arg = getValue(I.getOperand(0));
4245 EVT Ty = Arg.getValueType();
4246
Eric Christopher7b5e6172009-10-27 00:52:25 +00004247 if (CI->getZExtValue() < 2)
Mike Stump70e5e682009-11-09 22:28:21 +00004248 setValue(&I, DAG.getConstant(-1ULL, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004249 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004250 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004251 return 0;
4252 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004253 case Intrinsic::var_annotation:
4254 // Discard annotate attributes
4255 return 0;
4256
4257 case Intrinsic::init_trampoline: {
4258 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4259
4260 SDValue Ops[6];
4261 Ops[0] = getRoot();
4262 Ops[1] = getValue(I.getOperand(1));
4263 Ops[2] = getValue(I.getOperand(2));
4264 Ops[3] = getValue(I.getOperand(3));
4265 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4266 Ops[5] = DAG.getSrcValue(F);
4267
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004268 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004269 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004270 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004271
4272 setValue(&I, Tmp);
4273 DAG.setRoot(Tmp.getValue(1));
4274 return 0;
4275 }
4276
4277 case Intrinsic::gcroot:
4278 if (GFI) {
4279 Value *Alloca = I.getOperand(1);
4280 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004282 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4283 GFI->addStackRoot(FI->getIndex(), TypeMap);
4284 }
4285 return 0;
4286
4287 case Intrinsic::gcread:
4288 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004289 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004290 return 0;
4291
4292 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004293 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004294 return 0;
4295 }
4296
4297 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004298 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004299 return 0;
4300 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004301
Bill Wendlingef375462008-11-21 02:38:44 +00004302 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004303 return implVisitAluOverflow(I, ISD::UADDO);
4304 case Intrinsic::sadd_with_overflow:
4305 return implVisitAluOverflow(I, ISD::SADDO);
4306 case Intrinsic::usub_with_overflow:
4307 return implVisitAluOverflow(I, ISD::USUBO);
4308 case Intrinsic::ssub_with_overflow:
4309 return implVisitAluOverflow(I, ISD::SSUBO);
4310 case Intrinsic::umul_with_overflow:
4311 return implVisitAluOverflow(I, ISD::UMULO);
4312 case Intrinsic::smul_with_overflow:
4313 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004315 case Intrinsic::prefetch: {
4316 SDValue Ops[4];
4317 Ops[0] = getRoot();
4318 Ops[1] = getValue(I.getOperand(1));
4319 Ops[2] = getValue(I.getOperand(2));
4320 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004321 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004322 return 0;
4323 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004324
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004325 case Intrinsic::memory_barrier: {
4326 SDValue Ops[6];
4327 Ops[0] = getRoot();
4328 for (int x = 1; x < 6; ++x)
4329 Ops[x] = getValue(I.getOperand(x));
4330
Owen Anderson825b72b2009-08-11 20:47:22 +00004331 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004332 return 0;
4333 }
4334 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004335 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004336 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004337 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004338 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4339 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004340 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004341 getValue(I.getOperand(2)),
4342 getValue(I.getOperand(3)),
4343 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004344 setValue(&I, L);
4345 DAG.setRoot(L.getValue(1));
4346 return 0;
4347 }
4348 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004349 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004350 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004351 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004352 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004353 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004354 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004355 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004356 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004357 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004358 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004359 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004360 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004361 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004362 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004363 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004364 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004365 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004366 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004367 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004368 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004369 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004370
4371 case Intrinsic::invariant_start:
4372 case Intrinsic::lifetime_start:
4373 // Discard region information.
4374 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
4375 return 0;
4376 case Intrinsic::invariant_end:
4377 case Intrinsic::lifetime_end:
4378 // Discard region information.
4379 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004380 }
4381}
4382
Dan Gohman98ca4f22009-08-05 01:29:28 +00004383/// Test if the given instruction is in a position to be optimized
4384/// with a tail-call. This roughly means that it's in a block with
4385/// a return and there's nothing that needs to be scheduled
4386/// between it and the return.
4387///
4388/// This function only tests target-independent requirements.
4389/// For target-dependent requirements, a target should override
4390/// TargetLowering::IsEligibleForTailCallOptimization.
4391///
4392static bool
4393isInTailCallPosition(const Instruction *I, Attributes RetAttr,
4394 const TargetLowering &TLI) {
4395 const BasicBlock *ExitBB = I->getParent();
4396 const TerminatorInst *Term = ExitBB->getTerminator();
4397 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4398 const Function *F = ExitBB->getParent();
4399
4400 // The block must end in a return statement or an unreachable.
4401 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4402
4403 // If I will have a chain, make sure no other instruction that will have a
4404 // chain interposes between I and the return.
4405 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4406 !I->isSafeToSpeculativelyExecute())
4407 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4408 --BBI) {
4409 if (&*BBI == I)
4410 break;
4411 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4412 !BBI->isSafeToSpeculativelyExecute())
4413 return false;
4414 }
4415
4416 // If the block ends with a void return or unreachable, it doesn't matter
4417 // what the call's return type is.
4418 if (!Ret || Ret->getNumOperands() == 0) return true;
4419
4420 // Conservatively require the attributes of the call to match those of
4421 // the return.
4422 if (F->getAttributes().getRetAttributes() != RetAttr)
4423 return false;
4424
4425 // Otherwise, make sure the unmodified return value of I is the return value.
4426 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4427 U = dyn_cast<Instruction>(U->getOperand(0))) {
4428 if (!U)
4429 return false;
4430 if (!U->hasOneUse())
4431 return false;
4432 if (U == I)
4433 break;
4434 // Check for a truly no-op truncate.
4435 if (isa<TruncInst>(U) &&
4436 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4437 continue;
4438 // Check for a truly no-op bitcast.
4439 if (isa<BitCastInst>(U) &&
4440 (U->getOperand(0)->getType() == U->getType() ||
4441 (isa<PointerType>(U->getOperand(0)->getType()) &&
4442 isa<PointerType>(U->getType()))))
4443 continue;
4444 // Otherwise it's not a true no-op.
4445 return false;
4446 }
4447
4448 return true;
4449}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004450
4451void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004452 bool isTailCall,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004453 MachineBasicBlock *LandingPad) {
4454 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4455 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
4456 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4457 unsigned BeginLabel = 0, EndLabel = 0;
4458
4459 TargetLowering::ArgListTy Args;
4460 TargetLowering::ArgListEntry Entry;
4461 Args.reserve(CS.arg_size());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004462 unsigned j = 1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004463 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00004464 i != e; ++i, ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004465 SDValue ArgNode = getValue(*i);
4466 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4467
4468 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004469 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4470 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4471 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4472 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4473 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4474 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004475 Entry.Alignment = CS.getParamAlignment(attrInd);
4476 Args.push_back(Entry);
4477 }
4478
4479 if (LandingPad && MMI) {
4480 // Insert a label before the invoke call to mark the try range. This can be
4481 // used to detect deletion of the invoke via the MachineModuleInfo.
4482 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004483
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004484 // Both PendingLoads and PendingExports must be flushed here;
4485 // this call might not return.
4486 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004487 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4488 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004489 }
4490
Dan Gohman98ca4f22009-08-05 01:29:28 +00004491 // Check if target-independent constraints permit a tail call here.
4492 // Target-dependent constraints are checked within TLI.LowerCallTo.
4493 if (isTailCall &&
4494 !isInTailCallPosition(CS.getInstruction(),
4495 CS.getAttributes().getRetAttributes(),
4496 TLI))
4497 isTailCall = false;
4498
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004499 std::pair<SDValue,SDValue> Result =
4500 TLI.LowerCallTo(getRoot(), CS.getType(),
Devang Patel05988662008-09-25 21:00:45 +00004501 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004502 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004503 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004504 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004505 isTailCall,
4506 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004507 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004508 assert((isTailCall || Result.second.getNode()) &&
4509 "Non-null chain expected with non-tail call!");
4510 assert((Result.second.getNode() || !Result.first.getNode()) &&
4511 "Null value expected with tail call!");
4512 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004513 setValue(CS.getInstruction(), Result.first);
Dan Gohman98ca4f22009-08-05 01:29:28 +00004514 // As a special case, a null chain means that a tail call has
4515 // been emitted and the DAG root is already updated.
4516 if (Result.second.getNode())
4517 DAG.setRoot(Result.second);
4518 else
4519 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004520
4521 if (LandingPad && MMI) {
4522 // Insert a label at the end of the invoke call to mark the try range. This
4523 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4524 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004525 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4526 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004527
4528 // Inform MachineModuleInfo of range.
4529 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4530 }
4531}
4532
4533
4534void SelectionDAGLowering::visitCall(CallInst &I) {
4535 const char *RenameFn = 0;
4536 if (Function *F = I.getCalledFunction()) {
4537 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004538 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4539 if (II) {
4540 if (unsigned IID = II->getIntrinsicID(F)) {
4541 RenameFn = visitIntrinsicCall(I, IID);
4542 if (!RenameFn)
4543 return;
4544 }
4545 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004546 if (unsigned IID = F->getIntrinsicID()) {
4547 RenameFn = visitIntrinsicCall(I, IID);
4548 if (!RenameFn)
4549 return;
4550 }
4551 }
4552
4553 // Check for well-known libc/libm calls. If the function is internal, it
4554 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004555 if (!F->hasLocalLinkage() && F->hasName()) {
4556 StringRef Name = F->getName();
4557 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004558 if (I.getNumOperands() == 3 && // Basic sanity checks.
4559 I.getOperand(1)->getType()->isFloatingPoint() &&
4560 I.getType() == I.getOperand(1)->getType() &&
4561 I.getType() == I.getOperand(2)->getType()) {
4562 SDValue LHS = getValue(I.getOperand(1));
4563 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004564 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004565 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004566 return;
4567 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004568 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004569 if (I.getNumOperands() == 2 && // Basic sanity checks.
4570 I.getOperand(1)->getType()->isFloatingPoint() &&
4571 I.getType() == I.getOperand(1)->getType()) {
4572 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004573 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004574 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004575 return;
4576 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004577 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004578 if (I.getNumOperands() == 2 && // Basic sanity checks.
4579 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004580 I.getType() == I.getOperand(1)->getType() &&
4581 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004582 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004583 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004584 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004585 return;
4586 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004587 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 if (I.getNumOperands() == 2 && // Basic sanity checks.
4589 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004590 I.getType() == I.getOperand(1)->getType() &&
4591 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004592 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004593 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004594 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004595 return;
4596 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004597 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4598 if (I.getNumOperands() == 2 && // Basic sanity checks.
4599 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004600 I.getType() == I.getOperand(1)->getType() &&
4601 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004602 SDValue Tmp = getValue(I.getOperand(1));
4603 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4604 Tmp.getValueType(), Tmp));
4605 return;
4606 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004607 }
4608 }
4609 } else if (isa<InlineAsm>(I.getOperand(0))) {
4610 visitInlineAsm(&I);
4611 return;
4612 }
4613
4614 SDValue Callee;
4615 if (!RenameFn)
4616 Callee = getValue(I.getOperand(0));
4617 else
Bill Wendling056292f2008-09-16 21:48:12 +00004618 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004619
Dan Gohman98ca4f22009-08-05 01:29:28 +00004620 // Check if we can potentially perform a tail call. More detailed
4621 // checking is be done within LowerCallTo, after more information
4622 // about the call is known.
4623 bool isTailCall = PerformTailCallOpt && I.isTailCall();
4624
4625 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004626}
4627
4628
4629/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004630/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004631/// Chain/Flag as the input and updates them for the output Chain/Flag.
4632/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004633SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004634 SDValue &Chain,
4635 SDValue *Flag) const {
4636 // Assemble the legal parts into the final values.
4637 SmallVector<SDValue, 4> Values(ValueVTs.size());
4638 SmallVector<SDValue, 8> Parts;
4639 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4640 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004641 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004642 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004643 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004644
4645 Parts.resize(NumRegs);
4646 for (unsigned i = 0; i != NumRegs; ++i) {
4647 SDValue P;
4648 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004649 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004651 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004652 *Flag = P.getValue(2);
4653 }
4654 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004655
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 // If the source register was virtual and if we know something about it,
4657 // add an assert node.
4658 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4659 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4660 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4661 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4662 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4663 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004664
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004665 unsigned RegSize = RegisterVT.getSizeInBits();
4666 unsigned NumSignBits = LOI.NumSignBits;
4667 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004668
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004669 // FIXME: We capture more information than the dag can represent. For
4670 // now, just use the tightest assertzext/assertsext possible.
4671 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004672 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004673 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004674 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004675 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004676 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004677 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004678 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004679 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004680 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004681 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004682 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004683 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004684 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004685 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004686 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004687 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004688 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004689
Owen Anderson825b72b2009-08-11 20:47:22 +00004690 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004691 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004692 RegisterVT, P, DAG.getValueType(FromVT));
4693
4694 }
4695 }
4696 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004697
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004698 Parts[i] = P;
4699 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004700
Scott Michelfdc40a02009-02-17 22:15:04 +00004701 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004702 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004703 Part += NumRegs;
4704 Parts.clear();
4705 }
4706
Dale Johannesen66978ee2009-01-31 02:22:37 +00004707 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004708 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4709 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004710}
4711
4712/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004713/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004714/// Chain/Flag as the input and updates them for the output Chain/Flag.
4715/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004716void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004717 SDValue &Chain, SDValue *Flag) const {
4718 // Get the list of the values's legal parts.
4719 unsigned NumRegs = Regs.size();
4720 SmallVector<SDValue, 8> Parts(NumRegs);
4721 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004722 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004723 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004724 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004725
Dale Johannesen66978ee2009-01-31 02:22:37 +00004726 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004727 &Parts[Part], NumParts, RegisterVT);
4728 Part += NumParts;
4729 }
4730
4731 // Copy the parts into the registers.
4732 SmallVector<SDValue, 8> Chains(NumRegs);
4733 for (unsigned i = 0; i != NumRegs; ++i) {
4734 SDValue Part;
4735 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004736 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004737 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004738 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004739 *Flag = Part.getValue(1);
4740 }
4741 Chains[i] = Part.getValue(0);
4742 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004743
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004744 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004745 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004746 // flagged to it. That is the CopyToReg nodes and the user are considered
4747 // a single scheduling unit. If we create a TokenFactor and return it as
4748 // chain, then the TokenFactor is both a predecessor (operand) of the
4749 // user as well as a successor (the TF operands are flagged to the user).
4750 // c1, f1 = CopyToReg
4751 // c2, f2 = CopyToReg
4752 // c3 = TokenFactor c1, c2
4753 // ...
4754 // = op c3, ..., f2
4755 Chain = Chains[NumRegs-1];
4756 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004757 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004758}
4759
4760/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004761/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004762/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00004763void RegsForValue::AddInlineAsmOperands(unsigned Code,
4764 bool HasMatching,unsigned MatchingIdx,
4765 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004766 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004767 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00004768 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
4769 unsigned Flag = Code | (Regs.size() << 3);
4770 if (HasMatching)
4771 Flag |= 0x80000000 | (MatchingIdx << 16);
4772 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004773 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004774 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004775 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004776 for (unsigned i = 0; i != NumRegs; ++i) {
4777 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004778 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004779 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004780 }
4781}
4782
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004783/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004784/// i.e. it isn't a stack pointer or some other special register, return the
4785/// register class for the register. Otherwise, return null.
4786static const TargetRegisterClass *
4787isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4788 const TargetLowering &TLI,
4789 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004790 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004791 const TargetRegisterClass *FoundRC = 0;
4792 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4793 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004794 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004795
4796 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004797 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004798 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4799 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4800 I != E; ++I) {
4801 if (TLI.isTypeLegal(*I)) {
4802 // If we have already found this register in a different register class,
4803 // choose the one with the largest VT specified. For example, on
4804 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004805 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004806 ThisVT = *I;
4807 break;
4808 }
4809 }
4810 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004811
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004814 // NOTE: This isn't ideal. In particular, this might allocate the
4815 // frame pointer in functions that need it (due to them not being taken
4816 // out of allocation, because a variable sized allocation hasn't been seen
4817 // yet). This is a slight code pessimization, but should still work.
4818 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4819 E = RC->allocation_order_end(MF); I != E; ++I)
4820 if (*I == Reg) {
4821 // We found a matching register class. Keep looking at others in case
4822 // we find one with larger registers that this physreg is also in.
4823 FoundRC = RC;
4824 FoundVT = ThisVT;
4825 break;
4826 }
4827 }
4828 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004829}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004830
4831
4832namespace llvm {
4833/// AsmOperandInfo - This contains information for each constraint that we are
4834/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004835class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004836 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004837public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004838 /// CallOperand - If this is the result output operand or a clobber
4839 /// this is null, otherwise it is the incoming operand to the CallInst.
4840 /// This gets modified as the asm is processed.
4841 SDValue CallOperand;
4842
4843 /// AssignedRegs - If this is a register or register class operand, this
4844 /// contains the set of register corresponding to the operand.
4845 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004846
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004847 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4848 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4849 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004850
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004851 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4852 /// busy in OutputRegs/InputRegs.
4853 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004854 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004855 std::set<unsigned> &InputRegs,
4856 const TargetRegisterInfo &TRI) const {
4857 if (isOutReg) {
4858 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4859 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4860 }
4861 if (isInReg) {
4862 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4863 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4864 }
4865 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004866
Owen Andersone50ed302009-08-10 22:56:29 +00004867 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004868 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004869 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00004870 EVT getCallOperandValEVT(LLVMContext &Context,
4871 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004872 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004873 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004874
Chris Lattner81249c92008-10-17 17:05:25 +00004875 if (isa<BasicBlock>(CallOperandVal))
4876 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004877
Chris Lattner81249c92008-10-17 17:05:25 +00004878 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004879
Chris Lattner81249c92008-10-17 17:05:25 +00004880 // If this is an indirect operand, the operand is a pointer to the
4881 // accessed type.
4882 if (isIndirect)
4883 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004884
Chris Lattner81249c92008-10-17 17:05:25 +00004885 // If OpTy is not a single value, it may be a struct/union that we
4886 // can tile with integers.
4887 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4888 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4889 switch (BitSize) {
4890 default: break;
4891 case 1:
4892 case 8:
4893 case 16:
4894 case 32:
4895 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004896 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004897 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004898 break;
4899 }
4900 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004901
Chris Lattner81249c92008-10-17 17:05:25 +00004902 return TLI.getValueType(OpTy, true);
4903 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004905private:
4906 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4907 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004908 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004909 const TargetRegisterInfo &TRI) {
4910 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4911 Regs.insert(Reg);
4912 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4913 for (; *Aliases; ++Aliases)
4914 Regs.insert(*Aliases);
4915 }
4916};
4917} // end llvm namespace.
4918
4919
4920/// GetRegistersForValue - Assign registers (virtual or physical) for the
4921/// specified operand. We prefer to assign virtual registers, to allow the
4922/// register allocator handle the assignment process. However, if the asm uses
4923/// features that we can't model on machineinstrs, we have SDISel do the
4924/// allocation. This produces generally horrible, but correct, code.
4925///
4926/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004927/// Input and OutputRegs are the set of already allocated physical registers.
4928///
4929void SelectionDAGLowering::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004930GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004931 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004932 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004933 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004934
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935 // Compute whether this value requires an input register, an output register,
4936 // or both.
4937 bool isOutReg = false;
4938 bool isInReg = false;
4939 switch (OpInfo.Type) {
4940 case InlineAsm::isOutput:
4941 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004942
4943 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004944 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004945 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004946 break;
4947 case InlineAsm::isInput:
4948 isInReg = true;
4949 isOutReg = false;
4950 break;
4951 case InlineAsm::isClobber:
4952 isOutReg = true;
4953 isInReg = true;
4954 break;
4955 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004956
4957
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004958 MachineFunction &MF = DAG.getMachineFunction();
4959 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004960
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004961 // If this is a constraint for a single physreg, or a constraint for a
4962 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004963 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004964 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4965 OpInfo.ConstraintVT);
4966
4967 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004968 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004969 // If this is a FP input in an integer register (or visa versa) insert a bit
4970 // cast of the input value. More generally, handle any case where the input
4971 // value disagrees with the register class we plan to stick this in.
4972 if (OpInfo.Type == InlineAsm::isInput &&
4973 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004974 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004975 // types are identical size, use a bitcast to convert (e.g. two differing
4976 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004977 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004978 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004979 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004980 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004981 OpInfo.ConstraintVT = RegVT;
4982 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4983 // If the input is a FP value and we want it in FP registers, do a
4984 // bitcast to the corresponding integer type. This turns an f64 value
4985 // into i64, which can be passed with two i32 values on a 32-bit
4986 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00004987 RegVT = EVT::getIntegerVT(Context,
4988 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004989 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004990 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004991 OpInfo.ConstraintVT = RegVT;
4992 }
4993 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004994
Owen Anderson23b9b192009-08-12 00:36:31 +00004995 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004996 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004997
Owen Andersone50ed302009-08-10 22:56:29 +00004998 EVT RegVT;
4999 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005000
5001 // If this is a constraint for a specific physical register, like {r17},
5002 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005003 if (unsigned AssignedReg = PhysReg.first) {
5004 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005005 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005006 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005007
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005008 // Get the actual register value type. This is important, because the user
5009 // may have asked for (e.g.) the AX register in i32 type. We need to
5010 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005011 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005012
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005013 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005014 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005015
5016 // If this is an expanded reference, add the rest of the regs to Regs.
5017 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005018 TargetRegisterClass::iterator I = RC->begin();
5019 for (; *I != AssignedReg; ++I)
5020 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005021
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005022 // Already added the first reg.
5023 --NumRegs; ++I;
5024 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005025 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005026 Regs.push_back(*I);
5027 }
5028 }
5029 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5030 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5031 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5032 return;
5033 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005035 // Otherwise, if this was a reference to an LLVM register class, create vregs
5036 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005037 if (const TargetRegisterClass *RC = PhysReg.second) {
5038 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005039 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005040 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005041
Evan Chengfb112882009-03-23 08:01:15 +00005042 // Create the appropriate number of virtual registers.
5043 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5044 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005045 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005046
Evan Chengfb112882009-03-23 08:01:15 +00005047 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5048 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005049 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005050
5051 // This is a reference to a register class that doesn't directly correspond
5052 // to an LLVM register class. Allocate NumRegs consecutive, available,
5053 // registers from the class.
5054 std::vector<unsigned> RegClassRegs
5055 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5056 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005057
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005058 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5059 unsigned NumAllocated = 0;
5060 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5061 unsigned Reg = RegClassRegs[i];
5062 // See if this register is available.
5063 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5064 (isInReg && InputRegs.count(Reg))) { // Already used.
5065 // Make sure we find consecutive registers.
5066 NumAllocated = 0;
5067 continue;
5068 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005069
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005070 // Check to see if this register is allocatable (i.e. don't give out the
5071 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005072 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5073 if (!RC) { // Couldn't allocate this register.
5074 // Reset NumAllocated to make sure we return consecutive registers.
5075 NumAllocated = 0;
5076 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005077 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005078
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005079 // Okay, this register is good, we can use it.
5080 ++NumAllocated;
5081
5082 // If we allocated enough consecutive registers, succeed.
5083 if (NumAllocated == NumRegs) {
5084 unsigned RegStart = (i-NumAllocated)+1;
5085 unsigned RegEnd = i+1;
5086 // Mark all of the allocated registers used.
5087 for (unsigned i = RegStart; i != RegEnd; ++i)
5088 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005089
5090 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005091 OpInfo.ConstraintVT);
5092 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5093 return;
5094 }
5095 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005096
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005097 // Otherwise, we couldn't allocate enough registers for this.
5098}
5099
Evan Chengda43bcf2008-09-24 00:05:32 +00005100/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5101/// processed uses a memory 'm' constraint.
5102static bool
5103hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005104 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005105 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5106 InlineAsm::ConstraintInfo &CI = CInfos[i];
5107 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5108 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5109 if (CType == TargetLowering::C_Memory)
5110 return true;
5111 }
Chris Lattner6c147292009-04-30 00:48:50 +00005112
5113 // Indirect operand accesses access memory.
5114 if (CI.isIndirect)
5115 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005116 }
5117
5118 return false;
5119}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005120
5121/// visitInlineAsm - Handle a call to an InlineAsm object.
5122///
5123void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
5124 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5125
5126 /// ConstraintOperands - Information about all of the constraints.
5127 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005129 std::set<unsigned> OutputRegs, InputRegs;
5130
5131 // Do a prepass over the constraints, canonicalizing them, and building up the
5132 // ConstraintOperands list.
5133 std::vector<InlineAsm::ConstraintInfo>
5134 ConstraintInfos = IA->ParseConstraints();
5135
Evan Chengda43bcf2008-09-24 00:05:32 +00005136 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005137
5138 SDValue Chain, Flag;
5139
5140 // We won't need to flush pending loads if this asm doesn't touch
5141 // memory and is nonvolatile.
5142 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005143 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005144 else
5145 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005147 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5148 unsigned ResNo = 0; // ResNo - The result number of the next output.
5149 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5150 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5151 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005152
Owen Anderson825b72b2009-08-11 20:47:22 +00005153 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005154
5155 // Compute the value type for each operand.
5156 switch (OpInfo.Type) {
5157 case InlineAsm::isOutput:
5158 // Indirect outputs just consume an argument.
5159 if (OpInfo.isIndirect) {
5160 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5161 break;
5162 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005163
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005164 // The return value of the call is this value. As such, there is no
5165 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005166 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5167 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005168 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5169 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5170 } else {
5171 assert(ResNo == 0 && "Asm only has one result!");
5172 OpVT = TLI.getValueType(CS.getType());
5173 }
5174 ++ResNo;
5175 break;
5176 case InlineAsm::isInput:
5177 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5178 break;
5179 case InlineAsm::isClobber:
5180 // Nothing to do.
5181 break;
5182 }
5183
5184 // If this is an input or an indirect output, process the call argument.
5185 // BasicBlocks are labels, currently appearing only in asm's.
5186 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005187 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005188 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5189
Chris Lattner81249c92008-10-17 17:05:25 +00005190 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005191 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005192 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005193 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005195
Owen Anderson1d0be152009-08-13 21:58:54 +00005196 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005197 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005198
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005199 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005200 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005201
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005202 // Second pass over the constraints: compute which constraint option to use
5203 // and assign registers to constraints that want a specific physreg.
5204 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5205 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005206
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005207 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005208 // matching input. If their types mismatch, e.g. one is an integer, the
5209 // other is floating point, or their sizes are different, flag it as an
5210 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005211 if (OpInfo.hasMatchingInput()) {
5212 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5213 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005214 if ((OpInfo.ConstraintVT.isInteger() !=
5215 Input.ConstraintVT.isInteger()) ||
5216 (OpInfo.ConstraintVT.getSizeInBits() !=
5217 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005218 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005219 " with a matching output constraint of incompatible"
5220 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005221 }
5222 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005223 }
5224 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005225
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005226 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005227 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005228
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005229 // If this is a memory input, and if the operand is not indirect, do what we
5230 // need to to provide an address for the memory input.
5231 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5232 !OpInfo.isIndirect) {
5233 assert(OpInfo.Type == InlineAsm::isInput &&
5234 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005235
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005236 // Memory operands really want the address of the value. If we don't have
5237 // an indirect input, put it in the constpool if we can, otherwise spill
5238 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 // If the operand is a float, integer, or vector constant, spill to a
5241 // constant pool entry to get its address.
5242 Value *OpVal = OpInfo.CallOperandVal;
5243 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5244 isa<ConstantVector>(OpVal)) {
5245 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5246 TLI.getPointerTy());
5247 } else {
5248 // Otherwise, create a stack slot and emit a store to it before the
5249 // asm.
5250 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005251 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005252 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5253 MachineFunction &MF = DAG.getMachineFunction();
5254 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align);
5255 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005256 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005257 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 OpInfo.CallOperand = StackSlot;
5259 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261 // There is no longer a Value* corresponding to this operand.
5262 OpInfo.CallOperandVal = 0;
5263 // It is now an indirect operand.
5264 OpInfo.isIndirect = true;
5265 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005266
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005267 // If this constraint is for a specific register, allocate it before
5268 // anything else.
5269 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005270 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005271 }
5272 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005273
5274
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005275 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005276 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005277 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5278 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005279
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005280 // C_Register operands have already been allocated, Other/Memory don't need
5281 // to be.
5282 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005283 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005284 }
5285
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005286 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5287 std::vector<SDValue> AsmNodeOperands;
5288 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5289 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005290 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005291
5292
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005293 // Loop over all of the inputs, copying the operand values into the
5294 // appropriate registers and processing the output regs.
5295 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005297 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5298 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005299
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005300 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5301 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5302
5303 switch (OpInfo.Type) {
5304 case InlineAsm::isOutput: {
5305 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5306 OpInfo.ConstraintType != TargetLowering::C_Register) {
5307 // Memory output, or 'other' output (e.g. 'X' constraint).
5308 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5309
5310 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005311 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5312 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313 TLI.getPointerTy()));
5314 AsmNodeOperands.push_back(OpInfo.CallOperand);
5315 break;
5316 }
5317
5318 // Otherwise, this is a register or register class output.
5319
5320 // Copy the output from the appropriate register. Find a register that
5321 // we can use.
5322 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005323 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005324 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325 }
5326
5327 // If this is an indirect operand, store through the pointer after the
5328 // asm.
5329 if (OpInfo.isIndirect) {
5330 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5331 OpInfo.CallOperandVal));
5332 } else {
5333 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005334 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5335 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 // Concatenate this output onto the outputs list.
5337 RetValRegs.append(OpInfo.AssignedRegs);
5338 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005339
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005340 // Add information to the INLINEASM node to know that this register is
5341 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005342 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5343 6 /* EARLYCLOBBER REGDEF */ :
5344 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005345 false,
5346 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005347 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005348 break;
5349 }
5350 case InlineAsm::isInput: {
5351 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005352
Chris Lattner6bdcda32008-10-17 16:47:46 +00005353 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005354 // If this is required to match an output register we have already set,
5355 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005356 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005357
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005358 // Scan until we find the definition we already emitted of this operand.
5359 // When we find it, create a RegsForValue operand.
5360 unsigned CurOp = 2; // The first operand.
5361 for (; OperandNo; --OperandNo) {
5362 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005363 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005364 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005365 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5366 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5367 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005368 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005369 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005370 }
5371
Evan Cheng697cbbf2009-03-20 18:03:34 +00005372 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005373 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005374 if ((OpFlag & 7) == 2 /*REGDEF*/
5375 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5376 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005377 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005378 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005379 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005380 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005381 RegsForValue MatchedRegs;
5382 MatchedRegs.TLI = &TLI;
5383 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005384 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005385 MatchedRegs.RegVTs.push_back(RegVT);
5386 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005387 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005388 i != e; ++i)
5389 MatchedRegs.Regs.
5390 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005391
5392 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005393 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5394 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005395 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5396 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005397 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005398 break;
5399 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005400 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5401 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5402 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005403 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005404 // See InlineAsm.h isUseOperandTiedToDef.
5405 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005406 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005407 TLI.getPointerTy()));
5408 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5409 break;
5410 }
5411 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005412
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005413 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005414 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005415 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005416
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417 std::vector<SDValue> Ops;
5418 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005419 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005420 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005421 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005422 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005423 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005425 // Add information to the INLINEASM node to know about this input.
5426 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005427 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005428 TLI.getPointerTy()));
5429 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5430 break;
5431 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5432 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5433 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5434 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005435
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005436 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005437 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5438 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005439 TLI.getPointerTy()));
5440 AsmNodeOperands.push_back(InOperandVal);
5441 break;
5442 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005443
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005444 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5445 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5446 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005447 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005448 "Don't know how to handle indirect register inputs yet!");
5449
5450 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005451 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005452 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005453 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005454 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005455
Dale Johannesen66978ee2009-01-31 02:22:37 +00005456 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5457 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005458
Evan Cheng697cbbf2009-03-20 18:03:34 +00005459 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005460 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005461 break;
5462 }
5463 case InlineAsm::isClobber: {
5464 // Add the clobbered value to the operand list, so that the register
5465 // allocator is aware that the physreg got clobbered.
5466 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005467 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005468 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005469 break;
5470 }
5471 }
5472 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 // Finish up input operands.
5475 AsmNodeOperands[0] = Chain;
5476 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005477
Dale Johannesen66978ee2009-01-31 02:22:37 +00005478 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005479 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005480 &AsmNodeOperands[0], AsmNodeOperands.size());
5481 Flag = Chain.getValue(1);
5482
5483 // If this asm returns a register value, copy the result from that register
5484 // and set it as the value of the call.
5485 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005486 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005487 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005488
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005489 // FIXME: Why don't we do this for inline asms with MRVs?
5490 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005491 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005492
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005493 // If any of the results of the inline asm is a vector, it may have the
5494 // wrong width/num elts. This can happen for register classes that can
5495 // contain multiple different value types. The preg or vreg allocated may
5496 // not have the same VT as was expected. Convert it to the right type
5497 // with bit_convert.
5498 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005499 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005500 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005501
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005502 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005503 ResultType.isInteger() && Val.getValueType().isInteger()) {
5504 // If a result value was tied to an input value, the computed result may
5505 // have a wider width than the expected result. Extract the relevant
5506 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005507 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005508 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005509
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005510 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005511 }
Dan Gohman95915732008-10-18 01:03:45 +00005512
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005513 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005514 // Don't need to use this as a chain in this case.
5515 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5516 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005517 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005518
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005519 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005520
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005521 // Process indirect outputs, first output all of the flagged copies out of
5522 // physregs.
5523 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5524 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5525 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005526 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5527 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005528 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005530 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005531
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005532 // Emit the non-flagged stores from the physregs.
5533 SmallVector<SDValue, 8> OutChains;
5534 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005535 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005536 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005537 getValue(StoresToEmit[i].second),
5538 StoresToEmit[i].second, 0));
5539 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005540 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 &OutChains[0], OutChains.size());
5542 DAG.setRoot(Chain);
5543}
5544
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005545void SelectionDAGLowering::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005546 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005547 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005548 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005549 DAG.getSrcValue(I.getOperand(1))));
5550}
5551
5552void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005553 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5554 getRoot(), getValue(I.getOperand(0)),
5555 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005556 setValue(&I, V);
5557 DAG.setRoot(V.getValue(1));
5558}
5559
5560void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005561 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005563 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005564 DAG.getSrcValue(I.getOperand(1))));
5565}
5566
5567void SelectionDAGLowering::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005568 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005569 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005570 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005571 getValue(I.getOperand(2)),
5572 DAG.getSrcValue(I.getOperand(1)),
5573 DAG.getSrcValue(I.getOperand(2))));
5574}
5575
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005576/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005577/// implementation, which just calls LowerCall.
5578/// FIXME: When all targets are
5579/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005580std::pair<SDValue, SDValue>
5581TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5582 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005583 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005584 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005585 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005586 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005587 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005588
Dan Gohman1937e2f2008-09-16 01:42:28 +00005589 assert((!isTailCall || PerformTailCallOpt) &&
5590 "isTailCall set when tail-call optimizations are disabled!");
5591
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005592 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005593 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005594 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005595 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005596 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5597 for (unsigned Value = 0, NumValues = ValueVTs.size();
5598 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005599 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005600 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005601 SDValue Op = SDValue(Args[i].Node.getNode(),
5602 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005603 ISD::ArgFlagsTy Flags;
5604 unsigned OriginalAlignment =
5605 getTargetData()->getABITypeAlignment(ArgTy);
5606
5607 if (Args[i].isZExt)
5608 Flags.setZExt();
5609 if (Args[i].isSExt)
5610 Flags.setSExt();
5611 if (Args[i].isInReg)
5612 Flags.setInReg();
5613 if (Args[i].isSRet)
5614 Flags.setSRet();
5615 if (Args[i].isByVal) {
5616 Flags.setByVal();
5617 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5618 const Type *ElementTy = Ty->getElementType();
5619 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005620 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005621 // For ByVal, alignment should come from FE. BE will guess if this
5622 // info is not there but there are cases it cannot get right.
5623 if (Args[i].Alignment)
5624 FrameAlign = Args[i].Alignment;
5625 Flags.setByValAlign(FrameAlign);
5626 Flags.setByValSize(FrameSize);
5627 }
5628 if (Args[i].isNest)
5629 Flags.setNest();
5630 Flags.setOrigAlign(OriginalAlignment);
5631
Owen Anderson23b9b192009-08-12 00:36:31 +00005632 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5633 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005634 SmallVector<SDValue, 4> Parts(NumParts);
5635 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5636
5637 if (Args[i].isSExt)
5638 ExtendKind = ISD::SIGN_EXTEND;
5639 else if (Args[i].isZExt)
5640 ExtendKind = ISD::ZERO_EXTEND;
5641
Dale Johannesen66978ee2009-01-31 02:22:37 +00005642 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005643
Dan Gohman98ca4f22009-08-05 01:29:28 +00005644 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005645 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005646 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5647 if (NumParts > 1 && j == 0)
5648 MyFlags.Flags.setSplit();
5649 else if (j != 0)
5650 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005651
Dan Gohman98ca4f22009-08-05 01:29:28 +00005652 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005653 }
5654 }
5655 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005656
Dan Gohman98ca4f22009-08-05 01:29:28 +00005657 // Handle the incoming return values from the call.
5658 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005659 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005660 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005661 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005662 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005663 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5664 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005665 for (unsigned i = 0; i != NumRegs; ++i) {
5666 ISD::InputArg MyFlags;
5667 MyFlags.VT = RegisterVT;
5668 MyFlags.Used = isReturnValueUsed;
5669 if (RetSExt)
5670 MyFlags.Flags.setSExt();
5671 if (RetZExt)
5672 MyFlags.Flags.setZExt();
5673 if (isInreg)
5674 MyFlags.Flags.setInReg();
5675 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005676 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 }
5678
Dan Gohman98ca4f22009-08-05 01:29:28 +00005679 // Check if target-dependent constraints permit a tail call here.
5680 // Target-independent constraints should be checked by the caller.
5681 if (isTailCall &&
5682 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
5683 isTailCall = false;
5684
5685 SmallVector<SDValue, 4> InVals;
5686 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
5687 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005688
5689 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005690 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005691 "LowerCall didn't return a valid chain!");
5692 assert((!isTailCall || InVals.empty()) &&
5693 "LowerCall emitted a return value for a tail call!");
5694 assert((isTailCall || InVals.size() == Ins.size()) &&
5695 "LowerCall didn't emit the correct number of values!");
5696 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5697 assert(InVals[i].getNode() &&
5698 "LowerCall emitted a null value!");
5699 assert(Ins[i].VT == InVals[i].getValueType() &&
5700 "LowerCall emitted a value with the wrong type!");
5701 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00005702
5703 // For a tail call, the return value is merely live-out and there aren't
5704 // any nodes in the DAG representing it. Return a special value to
5705 // indicate that a tail call has been emitted and no more Instructions
5706 // should be processed in the current block.
5707 if (isTailCall) {
5708 DAG.setRoot(Chain);
5709 return std::make_pair(SDValue(), SDValue());
5710 }
5711
5712 // Collect the legal value parts into potentially illegal values
5713 // that correspond to the original function's return values.
5714 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5715 if (RetSExt)
5716 AssertOp = ISD::AssertSext;
5717 else if (RetZExt)
5718 AssertOp = ISD::AssertZext;
5719 SmallVector<SDValue, 4> ReturnValues;
5720 unsigned CurReg = 0;
5721 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005722 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005723 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5724 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005725
5726 SDValue ReturnValue =
5727 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
5728 AssertOp);
5729 ReturnValues.push_back(ReturnValue);
5730 CurReg += NumRegs;
5731 }
5732
5733 // For a function returning void, there is no return value. We can't create
5734 // such a node, so we just return a null return value in that case. In
5735 // that case, nothing will actualy look at the value.
5736 if (ReturnValues.empty())
5737 return std::make_pair(SDValue(), Chain);
5738
5739 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5740 DAG.getVTList(&RetTys[0], RetTys.size()),
5741 &ReturnValues[0], ReturnValues.size());
5742
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005743 return std::make_pair(Res, Chain);
5744}
5745
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005746void TargetLowering::LowerOperationWrapper(SDNode *N,
5747 SmallVectorImpl<SDValue> &Results,
5748 SelectionDAG &DAG) {
5749 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005750 if (Res.getNode())
5751 Results.push_back(Res);
5752}
5753
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005754SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005755 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005756 return SDValue();
5757}
5758
5759
5760void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
5761 SDValue Op = getValue(V);
5762 assert((Op.getOpcode() != ISD::CopyFromReg ||
5763 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5764 "Copy from a reg to the same reg!");
5765 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5766
Owen Anderson23b9b192009-08-12 00:36:31 +00005767 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005768 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005769 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005770 PendingExports.push_back(Chain);
5771}
5772
5773#include "llvm/CodeGen/SelectionDAGISel.h"
5774
Dan Gohman8c2b5252009-10-30 01:27:03 +00005775void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005776 // If this is the entry block, emit arguments.
5777 Function &F = *LLVMBB->getParent();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005778 SelectionDAG &DAG = SDL->DAG;
5779 SDValue OldRoot = DAG.getRoot();
5780 DebugLoc dl = SDL->getCurDebugLoc();
5781 const TargetData *TD = TLI.getTargetData();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005782
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005783 // Check whether the function can return without sret-demotion.
5784 SmallVector<EVT, 4> OutVTs;
5785 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
5786 getReturnInfo(&F, OutVTs, OutsFlags, TLI);
5787 // For now, assert and bail out if it can't.
5788 assert(TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(), OutVTs, OutsFlags,
5789 DAG) && "Cannot fit return value in registers!");
5790
Dan Gohman98ca4f22009-08-05 01:29:28 +00005791 // Set up the incoming argument description vector.
5792 SmallVector<ISD::InputArg, 16> Ins;
5793 unsigned Idx = 1;
5794 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5795 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005796 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005797 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5798 bool isArgValueUsed = !I->use_empty();
5799 for (unsigned Value = 0, NumValues = ValueVTs.size();
5800 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005801 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005802 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005803 ISD::ArgFlagsTy Flags;
5804 unsigned OriginalAlignment =
5805 TD->getABITypeAlignment(ArgTy);
5806
5807 if (F.paramHasAttr(Idx, Attribute::ZExt))
5808 Flags.setZExt();
5809 if (F.paramHasAttr(Idx, Attribute::SExt))
5810 Flags.setSExt();
5811 if (F.paramHasAttr(Idx, Attribute::InReg))
5812 Flags.setInReg();
5813 if (F.paramHasAttr(Idx, Attribute::StructRet))
5814 Flags.setSRet();
5815 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5816 Flags.setByVal();
5817 const PointerType *Ty = cast<PointerType>(I->getType());
5818 const Type *ElementTy = Ty->getElementType();
5819 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5820 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5821 // For ByVal, alignment should be passed from FE. BE will guess if
5822 // this info is not there but there are cases it cannot get right.
5823 if (F.getParamAlignment(Idx))
5824 FrameAlign = F.getParamAlignment(Idx);
5825 Flags.setByValAlign(FrameAlign);
5826 Flags.setByValSize(FrameSize);
5827 }
5828 if (F.paramHasAttr(Idx, Attribute::Nest))
5829 Flags.setNest();
5830 Flags.setOrigAlign(OriginalAlignment);
5831
Owen Anderson23b9b192009-08-12 00:36:31 +00005832 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5833 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005834 for (unsigned i = 0; i != NumRegs; ++i) {
5835 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5836 if (NumRegs > 1 && i == 0)
5837 MyFlags.Flags.setSplit();
5838 // if it isn't first piece, alignment must be 1
5839 else if (i > 0)
5840 MyFlags.Flags.setOrigAlign(1);
5841 Ins.push_back(MyFlags);
5842 }
5843 }
5844 }
5845
5846 // Call the target to set up the argument values.
5847 SmallVector<SDValue, 8> InVals;
5848 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5849 F.isVarArg(), Ins,
5850 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005851
5852 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005853 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005854 "LowerFormalArguments didn't return a valid chain!");
5855 assert(InVals.size() == Ins.size() &&
5856 "LowerFormalArguments didn't emit the correct number of values!");
5857 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5858 assert(InVals[i].getNode() &&
5859 "LowerFormalArguments emitted a null value!");
5860 assert(Ins[i].VT == InVals[i].getValueType() &&
5861 "LowerFormalArguments emitted a value with the wrong type!");
5862 });
5863
5864 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005865 DAG.setRoot(NewRoot);
5866
5867 // Set up the argument values.
5868 unsigned i = 0;
5869 Idx = 1;
5870 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5871 ++I, ++Idx) {
5872 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005873 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005874 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005875 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005876 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005877 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005878 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5879 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005880
5881 if (!I->use_empty()) {
5882 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5883 if (F.paramHasAttr(Idx, Attribute::SExt))
5884 AssertOp = ISD::AssertSext;
5885 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5886 AssertOp = ISD::AssertZext;
5887
5888 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
5889 PartVT, VT, AssertOp));
5890 }
5891 i += NumParts;
5892 }
5893 if (!I->use_empty()) {
5894 SDL->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
5895 SDL->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005896 // If this argument is live outside of the entry block, insert a copy from
5897 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005898 SDL->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005899 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005900 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005901 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005902
5903 // Finally, if the target has anything special to do, allow it to do so.
5904 // FIXME: this should insert code into the DAG!
5905 EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction());
5906}
5907
5908/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5909/// ensure constants are generated when needed. Remember the virtual registers
5910/// that need to be added to the Machine PHI nodes as input. We cannot just
5911/// directly add them, because expansion might result in multiple MBB's for one
5912/// BB. As such, the start of the BB might correspond to a different MBB than
5913/// the end.
5914///
5915void
5916SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5917 TerminatorInst *TI = LLVMBB->getTerminator();
5918
5919 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5920
5921 // Check successor nodes' PHI nodes that expect a constant to be available
5922 // from this block.
5923 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5924 BasicBlock *SuccBB = TI->getSuccessor(succ);
5925 if (!isa<PHINode>(SuccBB->begin())) continue;
5926 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005927
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005928 // If this terminator has multiple identical successors (common for
5929 // switches), only handle each succ once.
5930 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005931
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005932 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5933 PHINode *PN;
5934
5935 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5936 // nodes and Machine PHI nodes, but the incoming operands have not been
5937 // emitted yet.
5938 for (BasicBlock::iterator I = SuccBB->begin();
5939 (PN = dyn_cast<PHINode>(I)); ++I) {
5940 // Ignore dead phi's.
5941 if (PN->use_empty()) continue;
5942
5943 unsigned Reg;
5944 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5945
5946 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
5947 unsigned &RegOut = SDL->ConstantsOut[C];
5948 if (RegOut == 0) {
5949 RegOut = FuncInfo->CreateRegForValue(C);
5950 SDL->CopyValueToVirtualRegister(C, RegOut);
5951 }
5952 Reg = RegOut;
5953 } else {
5954 Reg = FuncInfo->ValueMap[PHIOp];
5955 if (Reg == 0) {
5956 assert(isa<AllocaInst>(PHIOp) &&
5957 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5958 "Didn't codegen value into a register!??");
5959 Reg = FuncInfo->CreateRegForValue(PHIOp);
5960 SDL->CopyValueToVirtualRegister(PHIOp, Reg);
5961 }
5962 }
5963
5964 // Remember that this register needs to added to the machine PHI node as
5965 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00005966 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005967 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5968 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00005969 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00005970 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005971 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
5972 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
5973 Reg += NumRegisters;
5974 }
5975 }
5976 }
5977 SDL->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005978}
5979
Dan Gohman3df24e62008-09-03 23:12:08 +00005980/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5981/// supports legal types, and it emits MachineInstrs directly instead of
5982/// creating SelectionDAG nodes.
5983///
5984bool
5985SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5986 FastISel *F) {
5987 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005988
Dan Gohman3df24e62008-09-03 23:12:08 +00005989 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5990 unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size();
5991
5992 // Check successor nodes' PHI nodes that expect a constant to be available
5993 // from this block.
5994 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5995 BasicBlock *SuccBB = TI->getSuccessor(succ);
5996 if (!isa<PHINode>(SuccBB->begin())) continue;
5997 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005998
Dan Gohman3df24e62008-09-03 23:12:08 +00005999 // If this terminator has multiple identical successors (common for
6000 // switches), only handle each succ once.
6001 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006002
Dan Gohman3df24e62008-09-03 23:12:08 +00006003 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6004 PHINode *PN;
6005
6006 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6007 // nodes and Machine PHI nodes, but the incoming operands have not been
6008 // emitted yet.
6009 for (BasicBlock::iterator I = SuccBB->begin();
6010 (PN = dyn_cast<PHINode>(I)); ++I) {
6011 // Ignore dead phi's.
6012 if (PN->use_empty()) continue;
6013
6014 // Only handle legal types. Two interesting things to note here. First,
6015 // by bailing out early, we may leave behind some dead instructions,
6016 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6017 // own moves. Second, this check is necessary becuase FastISel doesn't
6018 // use CreateRegForValue to create registers, so it always creates
6019 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006020 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006021 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6022 // Promote MVT::i1.
6023 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006024 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006025 else {
6026 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
6027 return false;
6028 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006029 }
6030
6031 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6032
6033 unsigned Reg = F->getRegForValue(PHIOp);
6034 if (Reg == 0) {
6035 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
6036 return false;
6037 }
6038 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
6039 }
6040 }
6041
6042 return true;
6043}