blob: cb6be3fc5255a5b728b9fd4367908a51d2e230bf [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"
Dan Gohman6277eb22009-11-23 17:16:22 +000016#include "FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000018#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000019#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Constants.h"
21#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 Gohmanf9bd4502009-11-23 17:46:23 +000071namespace {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000072 /// RegsForValue - This struct represents the registers (physical or virtual)
73 /// that a particular set of values is assigned, and the type information about
74 /// the value. The most common situation is to represent one value at a time,
75 /// but struct or array values are handled element-wise as multiple values.
76 /// The splitting of aggregates is performed recursively, so that we never
77 /// have aggregate-typed registers. The values at this point do not necessarily
78 /// have legal types, so each value may require one or more registers of some
79 /// legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000080 ///
Dan Gohmanf9bd4502009-11-23 17:46:23 +000081 struct RegsForValue {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000082 /// TLI - The TargetLowering object.
83 ///
84 const TargetLowering *TLI;
85
86 /// ValueVTs - The value types of the values, which may not be legal, and
87 /// may need be promoted or synthesized from one or more registers.
88 ///
Owen Andersone50ed302009-08-10 22:56:29 +000089 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000091 /// RegVTs - The value types of the registers. This is the same size as
92 /// ValueVTs and it records, for each value, what the type of the assigned
93 /// register or registers are. (Individual values are never synthesized
94 /// from more than one type of register.)
95 ///
96 /// With virtual registers, the contents of RegVTs is redundant with TLI's
97 /// getRegisterType member function, however when with physical registers
98 /// it is necessary to have a separate record of the types.
99 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000100 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000102 /// Regs - This list holds the registers assigned to the values.
103 /// Each legal or promoted value requires one register, and each
104 /// expanded value requires multiple registers.
105 ///
106 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000108 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000111 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000112 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000113 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
114 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000115 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000116 const SmallVector<EVT, 4> &regvts,
117 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000118 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000119 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000120 unsigned Reg, const Type *Ty) : TLI(&tli) {
121 ComputeValueVTs(tli, Ty, ValueVTs);
122
123 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000124 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000125 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
126 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000127 for (unsigned i = 0; i != NumRegs; ++i)
128 Regs.push_back(Reg + i);
129 RegVTs.push_back(RegisterVT);
130 Reg += NumRegs;
131 }
132 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 /// append - Add the specified values to this one.
135 void append(const RegsForValue &RHS) {
136 TLI = RHS.TLI;
137 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
138 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
139 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
140 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000141
142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000143 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000144 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000145 /// Chain/Flag as the input and updates them for the output Chain/Flag.
146 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000147 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000148 SDValue &Chain, SDValue *Flag) const;
149
150 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000151 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000152 /// Chain/Flag as the input and updates them for the output Chain/Flag.
153 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000154 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000155 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000158 /// operand list. This adds the code marker, matching input operand index
159 /// (if applicable), and includes the number of values added into it.
160 void AddInlineAsmOperands(unsigned Code,
161 bool HasMatching, unsigned MatchingIdx,
162 SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000163 };
164}
165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000166/// getCopyFromParts - Create a value that contains the specified legal parts
167/// combined into the value they represent. If the parts combine to a type
168/// larger then ValueVT then AssertOp can be used to specify whether the extra
169/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
170/// (ISD::AssertSext).
Dale Johannesen66978ee2009-01-31 02:22:37 +0000171static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
172 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000173 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000174 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000175 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000176 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000177 SDValue Val = Parts[0];
178
179 if (NumParts > 1) {
180 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000181 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000182 unsigned PartBits = PartVT.getSizeInBits();
183 unsigned ValueBits = ValueVT.getSizeInBits();
184
185 // Assemble the power of 2 part.
186 unsigned RoundParts = NumParts & (NumParts - 1) ?
187 1 << Log2_32(NumParts) : NumParts;
188 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000189 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000190 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000191 SDValue Lo, Hi;
192
Owen Anderson23b9b192009-08-12 00:36:31 +0000193 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 if (RoundParts > 2) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000196 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT);
197 Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000198 PartVT, HalfVT);
199 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000200 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
201 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000202 }
203 if (TLI.isBigEndian())
204 std::swap(Lo, Hi);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000205 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000206
207 if (RoundParts < NumParts) {
208 // Assemble the trailing non-power-of-2 part.
209 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000210 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Scott Michelfdc40a02009-02-17 22:15:04 +0000211 Hi = getCopyFromParts(DAG, dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000212 Parts+RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000213
214 // Combine the round and odd parts.
215 Lo = Val;
216 if (TLI.isBigEndian())
217 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000218 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000219 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
220 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000221 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000222 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000223 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
224 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000225 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000226 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000227 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000228 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000229 unsigned NumIntermediates;
230 unsigned NumRegs =
Owen Anderson23b9b192009-08-12 00:36:31 +0000231 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
232 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000233 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
234 NumParts = NumRegs; // Silence a compiler warning.
235 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
236 assert(RegisterVT == Parts[0].getValueType() &&
237 "Part type doesn't match part!");
238
239 // Assemble the parts into intermediate operands.
240 SmallVector<SDValue, 8> Ops(NumIntermediates);
241 if (NumIntermediates == NumParts) {
242 // If the register was not expanded, truncate or copy the value,
243 // as appropriate.
244 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000245 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000246 PartVT, IntermediateVT);
247 } else if (NumParts > 0) {
248 // If the intermediate type was expanded, build the intermediate operands
249 // from the parts.
250 assert(NumParts % NumIntermediates == 0 &&
251 "Must expand into a divisible number of parts!");
252 unsigned Factor = NumParts / NumIntermediates;
253 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000254 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000255 PartVT, IntermediateVT);
256 }
257
258 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
259 // operands.
260 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000261 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000262 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000263 } else if (PartVT.isFloatingPoint()) {
264 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000266 "Unexpected split");
267 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
269 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000270 if (TLI.isBigEndian())
271 std::swap(Lo, Hi);
272 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
273 } else {
274 // FP split into integer parts (soft fp)
275 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
276 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000277 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Eli Friedman2ac8b322009-05-20 06:02:09 +0000278 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000279 }
280 }
281
282 // There is now one part, held in Val. Correct it to match ValueVT.
283 PartVT = Val.getValueType();
284
285 if (PartVT == ValueVT)
286 return Val;
287
288 if (PartVT.isVector()) {
289 assert(ValueVT.isVector() && "Unknown vector conversion!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000290 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000291 }
292
293 if (ValueVT.isVector()) {
294 assert(ValueVT.getVectorElementType() == PartVT &&
295 ValueVT.getVectorNumElements() == 1 &&
296 "Only trivial scalar-to-vector conversions should get here!");
Evan Chenga87008d2009-02-25 22:49:59 +0000297 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000298 }
299
300 if (PartVT.isInteger() &&
301 ValueVT.isInteger()) {
302 if (ValueVT.bitsLT(PartVT)) {
303 // For a truncate, see if we have any information to
304 // indicate whether the truncated bits will always be
305 // zero or sign-extension.
306 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000307 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000308 DAG.getValueType(ValueVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000309 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000310 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000311 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000312 }
313 }
314
315 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
316 if (ValueVT.bitsLT(Val.getValueType()))
317 // FP_ROUND's are always exact here.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000318 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000319 DAG.getIntPtrConstant(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000320 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000321 }
322
323 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000324 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000325
Torok Edwinc23197a2009-07-14 16:55:14 +0000326 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000327 return SDValue();
328}
329
330/// getCopyToParts - Create a series of nodes that contain the specified value
331/// split into legal parts. If the parts contain more bits than Val, then, for
332/// integers, ExtendKind can be used to specify how to generate the extra bits.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000333static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val,
Owen Andersone50ed302009-08-10 22:56:29 +0000334 SDValue *Parts, unsigned NumParts, EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000336 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000337 EVT PtrVT = TLI.getPointerTy();
338 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000339 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000340 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
342
343 if (!NumParts)
344 return;
345
346 if (!ValueVT.isVector()) {
347 if (PartVT == ValueVT) {
348 assert(NumParts == 1 && "No-op copy with multiple parts!");
349 Parts[0] = Val;
350 return;
351 }
352
353 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
354 // If the parts cover more bits than the value has, promote the value.
355 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
356 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000357 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000358 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000359 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000360 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000361 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000362 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000363 }
364 } else if (PartBits == ValueVT.getSizeInBits()) {
365 // Different types of the same size.
366 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000367 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000368 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
369 // If the parts cover less bits than value has, truncate the value.
370 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000371 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000372 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000373 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000374 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000375 }
376 }
377
378 // The value may have changed - recompute ValueVT.
379 ValueVT = Val.getValueType();
380 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
381 "Failed to tile the value with PartVT!");
382
383 if (NumParts == 1) {
384 assert(PartVT == ValueVT && "Type conversion failed!");
385 Parts[0] = Val;
386 return;
387 }
388
389 // Expand the value into multiple parts.
390 if (NumParts & (NumParts - 1)) {
391 // The number of parts is not a power of 2. Split off and copy the tail.
392 assert(PartVT.isInteger() && ValueVT.isInteger() &&
393 "Do not know what to expand to!");
394 unsigned RoundParts = 1 << Log2_32(NumParts);
395 unsigned RoundBits = RoundParts * PartBits;
396 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000397 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000398 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000399 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000400 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000401 if (TLI.isBigEndian())
402 // The odd parts were reversed by getCopyToParts - unreverse them.
403 std::reverse(Parts + RoundParts, Parts + NumParts);
404 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000405 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000406 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000407 }
408
409 // The number of parts is a power of 2. Repeatedly bisect the value using
410 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000411 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson23b9b192009-08-12 00:36:31 +0000412 EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000413 Val);
414 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
415 for (unsigned i = 0; i < NumParts; i += StepSize) {
416 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000417 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000418 SDValue &Part0 = Parts[i];
419 SDValue &Part1 = Parts[i+StepSize/2];
420
Scott Michelfdc40a02009-02-17 22:15:04 +0000421 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000422 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000423 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000424 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000425 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000426 DAG.getConstant(0, PtrVT));
427
428 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000429 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000430 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000431 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000432 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000433 }
434 }
435 }
436
437 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000438 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000439
440 return;
441 }
442
443 // Vector ValueVT.
444 if (NumParts == 1) {
445 if (PartVT != ValueVT) {
446 if (PartVT.isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000447 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000448 } else {
449 assert(ValueVT.getVectorElementType() == PartVT &&
450 ValueVT.getVectorNumElements() == 1 &&
451 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000452 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000453 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000454 DAG.getConstant(0, PtrVT));
455 }
456 }
457
458 Parts[0] = Val;
459 return;
460 }
461
462 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000463 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000464 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000465 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
466 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000467 unsigned NumElements = ValueVT.getVectorNumElements();
468
469 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
470 NumParts = NumRegs; // Silence a compiler warning.
471 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
472
473 // Split the vector into intermediate operands.
474 SmallVector<SDValue, 8> Ops(NumIntermediates);
475 for (unsigned i = 0; i != NumIntermediates; ++i)
476 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000477 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000478 IntermediateVT, Val,
479 DAG.getConstant(i * (NumElements / NumIntermediates),
480 PtrVT));
481 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000482 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000483 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000484 DAG.getConstant(i, PtrVT));
485
486 // Split the intermediate operands into legal parts.
487 if (NumParts == NumIntermediates) {
488 // If the register was not expanded, promote or copy the value,
489 // as appropriate.
490 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000491 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000492 } else if (NumParts > 0) {
493 // If the intermediate type was expanded, split each the value into
494 // legal parts.
495 assert(NumParts % NumIntermediates == 0 &&
496 "Must expand into a divisible number of parts!");
497 unsigned Factor = NumParts / NumIntermediates;
498 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000499 getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000500 }
501}
502
503
504void SelectionDAGLowering::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
505 AA = &aa;
506 GFI = gfi;
507 TD = DAG.getTarget().getTargetData();
508}
509
510/// clear - Clear out the curret SelectionDAG and the associated
511/// state and prepare this SelectionDAGLowering object to be used
512/// for a new block. This doesn't clear out information about
513/// additional blocks that are needed to complete switch lowering
514/// or PHI node updating; that information is cleared out as it is
515/// consumed.
516void SelectionDAGLowering::clear() {
517 NodeMap.clear();
518 PendingLoads.clear();
519 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000520 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000521 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000522 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000523 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000524}
525
526/// getRoot - Return the current virtual root of the Selection DAG,
527/// flushing any PendingLoad items. This must be done before emitting
528/// a store or any other node that may need to be ordered after any
529/// prior load instructions.
530///
531SDValue SelectionDAGLowering::getRoot() {
532 if (PendingLoads.empty())
533 return DAG.getRoot();
534
535 if (PendingLoads.size() == 1) {
536 SDValue Root = PendingLoads[0];
537 DAG.setRoot(Root);
538 PendingLoads.clear();
539 return Root;
540 }
541
542 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000544 &PendingLoads[0], PendingLoads.size());
545 PendingLoads.clear();
546 DAG.setRoot(Root);
547 return Root;
548}
549
550/// getControlRoot - Similar to getRoot, but instead of flushing all the
551/// PendingLoad items, flush all the PendingExports items. It is necessary
552/// to do this before emitting a terminator instruction.
553///
554SDValue SelectionDAGLowering::getControlRoot() {
555 SDValue Root = DAG.getRoot();
556
557 if (PendingExports.empty())
558 return Root;
559
560 // Turn all of the CopyToReg chains into one factored node.
561 if (Root.getOpcode() != ISD::EntryToken) {
562 unsigned i = 0, e = PendingExports.size();
563 for (; i != e; ++i) {
564 assert(PendingExports[i].getNode()->getNumOperands() > 1);
565 if (PendingExports[i].getNode()->getOperand(0) == Root)
566 break; // Don't add the root if we already indirectly depend on it.
567 }
568
569 if (i == e)
570 PendingExports.push_back(Root);
571 }
572
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000574 &PendingExports[0],
575 PendingExports.size());
576 PendingExports.clear();
577 DAG.setRoot(Root);
578 return Root;
579}
580
581void SelectionDAGLowering::visit(Instruction &I) {
582 visit(I.getOpcode(), I);
583}
584
585void SelectionDAGLowering::visit(unsigned Opcode, User &I) {
586 // Note: this doesn't use InstVisitor, because it has to work with
587 // ConstantExpr's in addition to instructions.
588 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000589 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000590 // Build the switch statement using the Instruction.def file.
591#define HANDLE_INST(NUM, OPCODE, CLASS) \
592 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
593#include "llvm/Instruction.def"
594 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000595}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000596
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000597SDValue SelectionDAGLowering::getValue(const Value *V) {
598 SDValue &N = NodeMap[V];
599 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000600
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000601 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000602 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000603
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000604 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000605 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000606
607 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
608 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000609
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000610 if (isa<ConstantPointerNull>(C))
611 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000613 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000614 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000615
Nate Begeman9008ca62009-04-27 18:41:29 +0000616 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000617 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000618
619 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
620 visit(CE->getOpcode(), *CE);
621 SDValue N1 = NodeMap[V];
622 assert(N1.getNode() && "visit didn't populate the ValueMap!");
623 return N1;
624 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000625
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000626 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
627 SmallVector<SDValue, 4> Constants;
628 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
629 OI != OE; ++OI) {
630 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000631 // If the operand is an empty aggregate, there are no values.
632 if (!Val) continue;
633 // Add each leaf value from the operand to the Constants list
634 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000635 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
636 Constants.push_back(SDValue(Val, i));
637 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000638 return DAG.getMergeValues(&Constants[0], Constants.size(),
639 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000640 }
641
642 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
643 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
644 "Unknown struct or array constant!");
645
Owen Andersone50ed302009-08-10 22:56:29 +0000646 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000647 ComputeValueVTs(TLI, C->getType(), ValueVTs);
648 unsigned NumElts = ValueVTs.size();
649 if (NumElts == 0)
650 return SDValue(); // empty struct
651 SmallVector<SDValue, 4> Constants(NumElts);
652 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000653 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000654 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000655 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000656 else if (EltVT.isFloatingPoint())
657 Constants[i] = DAG.getConstantFP(0, EltVT);
658 else
659 Constants[i] = DAG.getConstant(0, EltVT);
660 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000661 return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000662 }
663
Dan Gohman8c2b5252009-10-30 01:27:03 +0000664 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000665 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000666
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000667 const VectorType *VecTy = cast<VectorType>(V->getType());
668 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000669
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000670 // Now that we know the number and type of the elements, get that number of
671 // elements into the Ops array based on what kind of constant it is.
672 SmallVector<SDValue, 16> Ops;
673 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
674 for (unsigned i = 0; i != NumElements; ++i)
675 Ops.push_back(getValue(CP->getOperand(i)));
676 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000677 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000678 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000679
680 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000681 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682 Op = DAG.getConstantFP(0, EltVT);
683 else
684 Op = DAG.getConstant(0, EltVT);
685 Ops.assign(NumElements, Op);
686 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000687
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000688 // Create a BUILD_VECTOR node.
Evan Chenga87008d2009-02-25 22:49:59 +0000689 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
690 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000691 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000692
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000693 // If this is a static alloca, generate it as the frameindex instead of
694 // computation.
695 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
696 DenseMap<const AllocaInst*, int>::iterator SI =
697 FuncInfo.StaticAllocaMap.find(AI);
698 if (SI != FuncInfo.StaticAllocaMap.end())
699 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
700 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000701
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000702 unsigned InReg = FuncInfo.ValueMap[V];
703 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000704
Owen Anderson23b9b192009-08-12 00:36:31 +0000705 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000706 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +0000707 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000708}
709
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000710/// Get the EVTs and ArgFlags collections that represent the return type
711/// of the given function. This does not require a DAG or a return value, and
712/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000713static void getReturnInfo(const Type* ReturnType,
714 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000715 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000716 TargetLowering &TLI,
717 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000718 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000719 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000720 unsigned NumValues = ValueVTs.size();
721 if ( NumValues == 0 ) return;
722
723 for (unsigned j = 0, f = NumValues; j != f; ++j) {
724 EVT VT = ValueVTs[j];
725 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000726
727 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000728 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000729 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000730 ExtendKind = ISD::ZERO_EXTEND;
731
732 // FIXME: C calling convention requires the return type to be promoted to
733 // at least 32-bit. But this is not necessary for non-C calling
734 // conventions. The frontend should mark functions whose return values
735 // require promoting with signext or zeroext attributes.
736 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000737 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000738 if (VT.bitsLT(MinVT))
739 VT = MinVT;
740 }
741
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000742 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
743 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000744 // 'inreg' on function refers to return value
745 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000746 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000747 Flags.setInReg();
748
749 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000750 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000751 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000752 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000753 Flags.setZExt();
754
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000755 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000756 OutVTs.push_back(PartVT);
757 OutFlags.push_back(Flags);
758 }
759 }
760}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000761
762void SelectionDAGLowering::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000763 SDValue Chain = getControlRoot();
764 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000765 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
766
767 if (!FLI.CanLowerReturn) {
768 unsigned DemoteReg = FLI.DemoteRegister;
769 const Function *F = I.getParent()->getParent();
770
771 // Emit a store of the return value through the virtual register.
772 // Leave Outs empty so that LowerReturn won't try to load return
773 // registers the usual way.
774 SmallVector<EVT, 1> PtrValueVTs;
775 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
776 PtrValueVTs);
777
778 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
779 SDValue RetOp = getValue(I.getOperand(0));
780
Owen Andersone50ed302009-08-10 22:56:29 +0000781 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000782 SmallVector<uint64_t, 4> Offsets;
783 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000784 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000785
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000786 SmallVector<SDValue, 4> Chains(NumValues);
787 EVT PtrVT = PtrValueVTs[0];
788 for (unsigned i = 0; i != NumValues; ++i)
789 Chains[i] = DAG.getStore(Chain, getCurDebugLoc(),
790 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
791 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
792 DAG.getConstant(Offsets[i], PtrVT)),
793 NULL, Offsets[i], false, 0);
794 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
795 MVT::Other, &Chains[0], NumValues);
796 }
797 else {
798 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
799 SmallVector<EVT, 4> ValueVTs;
800 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
801 unsigned NumValues = ValueVTs.size();
802 if (NumValues == 0) continue;
803
804 SDValue RetOp = getValue(I.getOperand(i));
805 for (unsigned j = 0, f = NumValues; j != f; ++j) {
806 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000807
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000808 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000809
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000810 const Function *F = I.getParent()->getParent();
811 if (F->paramHasAttr(0, Attribute::SExt))
812 ExtendKind = ISD::SIGN_EXTEND;
813 else if (F->paramHasAttr(0, Attribute::ZExt))
814 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000815
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000816 // FIXME: C calling convention requires the return type to be promoted to
817 // at least 32-bit. But this is not necessary for non-C calling
818 // conventions. The frontend should mark functions whose return values
819 // require promoting with signext or zeroext attributes.
820 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
821 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
822 if (VT.bitsLT(MinVT))
823 VT = MinVT;
824 }
825
826 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
827 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
828 SmallVector<SDValue, 4> Parts(NumParts);
829 getCopyToParts(DAG, getCurDebugLoc(),
830 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
831 &Parts[0], NumParts, PartVT, ExtendKind);
832
833 // 'inreg' on function refers to return value
834 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
835 if (F->paramHasAttr(0, Attribute::InReg))
836 Flags.setInReg();
837
838 // Propagate extension type if any
839 if (F->paramHasAttr(0, Attribute::SExt))
840 Flags.setSExt();
841 else if (F->paramHasAttr(0, Attribute::ZExt))
842 Flags.setZExt();
843
844 for (unsigned i = 0; i < NumParts; ++i)
845 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000846 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000847 }
848 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000849
850 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000851 CallingConv::ID CallConv =
852 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000853 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
854 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000855
856 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000857 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000858 "LowerReturn didn't return a valid chain!");
859
860 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000861 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000862}
863
Dan Gohmanad62f532009-04-23 23:13:24 +0000864/// CopyToExportRegsIfNeeded - If the given value has virtual registers
865/// created for it, emit nodes to copy the value into the virtual
866/// registers.
867void SelectionDAGLowering::CopyToExportRegsIfNeeded(Value *V) {
868 if (!V->use_empty()) {
869 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
870 if (VMI != FuncInfo.ValueMap.end())
871 CopyValueToVirtualRegister(V, VMI->second);
872 }
873}
874
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000875/// ExportFromCurrentBlock - If this condition isn't known to be exported from
876/// the current basic block, add it to ValueMap now so that we'll get a
877/// CopyTo/FromReg.
878void SelectionDAGLowering::ExportFromCurrentBlock(Value *V) {
879 // No need to export constants.
880 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000881
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000882 // Already exported?
883 if (FuncInfo.isExportedInst(V)) return;
884
885 unsigned Reg = FuncInfo.InitializeRegForValue(V);
886 CopyValueToVirtualRegister(V, Reg);
887}
888
889bool SelectionDAGLowering::isExportableFromCurrentBlock(Value *V,
890 const BasicBlock *FromBB) {
891 // The operands of the setcc have to be in this block. We don't know
892 // how to export them from some other block.
893 if (Instruction *VI = dyn_cast<Instruction>(V)) {
894 // Can export from current BB.
895 if (VI->getParent() == FromBB)
896 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000898 // Is already exported, noop.
899 return FuncInfo.isExportedInst(V);
900 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000901
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000902 // If this is an argument, we can export it if the BB is the entry block or
903 // if it is already exported.
904 if (isa<Argument>(V)) {
905 if (FromBB == &FromBB->getParent()->getEntryBlock())
906 return true;
907
908 // Otherwise, can only export this if it is already exported.
909 return FuncInfo.isExportedInst(V);
910 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000911
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000912 // Otherwise, constants can always be exported.
913 return true;
914}
915
916static bool InBlock(const Value *V, const BasicBlock *BB) {
917 if (const Instruction *I = dyn_cast<Instruction>(V))
918 return I->getParent() == BB;
919 return true;
920}
921
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000922/// getFCmpCondCode - Return the ISD condition code corresponding to
923/// the given LLVM IR floating-point condition code. This includes
924/// consideration of global floating-point math flags.
925///
926static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
927 ISD::CondCode FPC, FOC;
928 switch (Pred) {
929 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
930 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
931 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
932 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
933 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
934 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
935 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
936 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
937 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
938 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
939 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
940 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
941 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
942 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
943 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
944 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
945 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000946 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000947 FOC = FPC = ISD::SETFALSE;
948 break;
949 }
950 if (FiniteOnlyFPMath())
951 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000952 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000953 return FPC;
954}
955
956/// getICmpCondCode - Return the ISD condition code corresponding to
957/// the given LLVM IR integer condition code.
958///
959static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
960 switch (Pred) {
961 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
962 case ICmpInst::ICMP_NE: return ISD::SETNE;
963 case ICmpInst::ICMP_SLE: return ISD::SETLE;
964 case ICmpInst::ICMP_ULE: return ISD::SETULE;
965 case ICmpInst::ICMP_SGE: return ISD::SETGE;
966 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
967 case ICmpInst::ICMP_SLT: return ISD::SETLT;
968 case ICmpInst::ICMP_ULT: return ISD::SETULT;
969 case ICmpInst::ICMP_SGT: return ISD::SETGT;
970 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
971 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000972 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000973 return ISD::SETNE;
974 }
975}
976
Dan Gohmanc2277342008-10-17 21:16:08 +0000977/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
978/// This function emits a branch and is used at the leaves of an OR or an
979/// AND operator tree.
980///
981void
982SelectionDAGLowering::EmitBranchForMergedCondition(Value *Cond,
983 MachineBasicBlock *TBB,
984 MachineBasicBlock *FBB,
985 MachineBasicBlock *CurBB) {
986 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000987
Dan Gohmanc2277342008-10-17 21:16:08 +0000988 // If the leaf of the tree is a comparison, merge the condition into
989 // the caseblock.
990 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
991 // The operands of the cmp have to be in this block. We don't know
992 // how to export them from some other block. If this is the first block
993 // of the sequence, no exporting is needed.
994 if (CurBB == CurMBB ||
995 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
996 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000997 ISD::CondCode Condition;
998 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000999 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001000 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001001 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001002 } else {
1003 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001004 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001005 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001006
1007 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001008 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1009 SwitchCases.push_back(CB);
1010 return;
1011 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001012 }
1013
1014 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001015 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001016 NULL, TBB, FBB, CurBB);
1017 SwitchCases.push_back(CB);
1018}
1019
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001020/// FindMergedConditions - If Cond is an expression like
Dan Gohmanc2277342008-10-17 21:16:08 +00001021void SelectionDAGLowering::FindMergedConditions(Value *Cond,
1022 MachineBasicBlock *TBB,
1023 MachineBasicBlock *FBB,
1024 MachineBasicBlock *CurBB,
1025 unsigned Opc) {
1026 // If this node is not part of the or/and tree, emit it as a branch.
1027 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001028 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001029 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1030 BOp->getParent() != CurBB->getBasicBlock() ||
1031 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1032 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1033 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001034 return;
1035 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001036
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001037 // Create TmpBB after CurBB.
1038 MachineFunction::iterator BBI = CurBB;
1039 MachineFunction &MF = DAG.getMachineFunction();
1040 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1041 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001042
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001043 if (Opc == Instruction::Or) {
1044 // Codegen X | Y as:
1045 // jmp_if_X TBB
1046 // jmp TmpBB
1047 // TmpBB:
1048 // jmp_if_Y TBB
1049 // jmp FBB
1050 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001052 // Emit the LHS condition.
1053 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001055 // Emit the RHS condition into TmpBB.
1056 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1057 } else {
1058 assert(Opc == Instruction::And && "Unknown merge op!");
1059 // Codegen X & Y as:
1060 // jmp_if_X TmpBB
1061 // jmp FBB
1062 // TmpBB:
1063 // jmp_if_Y TBB
1064 // jmp FBB
1065 //
1066 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001068 // Emit the LHS condition.
1069 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001070
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001071 // Emit the RHS condition into TmpBB.
1072 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1073 }
1074}
1075
1076/// If the set of cases should be emitted as a series of branches, return true.
1077/// If we should emit this as a bunch of and/or'd together conditions, return
1078/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001079bool
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001080SelectionDAGLowering::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
1081 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 // If this is two comparisons of the same values or'd or and'd together, they
1084 // will get folded into a single comparison, so don't emit two blocks.
1085 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1086 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1087 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1088 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1089 return false;
1090 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001092 return true;
1093}
1094
1095void SelectionDAGLowering::visitBr(BranchInst &I) {
1096 // Update machine-CFG edges.
1097 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1098
1099 // Figure out which block is immediately after the current one.
1100 MachineBasicBlock *NextBlock = 0;
1101 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001102 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001103 NextBlock = BBI;
1104
1105 if (I.isUnconditional()) {
1106 // Update machine-CFG edges.
1107 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001109 // If this is not a fall-through branch, emit the branch.
1110 if (Succ0MBB != NextBlock)
Scott Michelfdc40a02009-02-17 22:15:04 +00001111 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001112 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001113 DAG.getBasicBlock(Succ0MBB)));
1114 return;
1115 }
1116
1117 // If this condition is one of the special cases we handle, do special stuff
1118 // now.
1119 Value *CondVal = I.getCondition();
1120 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1121
1122 // If this is a series of conditions that are or'd or and'd together, emit
1123 // this as a sequence of branches instead of setcc's with and/or operations.
1124 // For example, instead of something like:
1125 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001126 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001127 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001128 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001129 // or C, F
1130 // jnz foo
1131 // Emit:
1132 // cmp A, B
1133 // je foo
1134 // cmp D, E
1135 // jle foo
1136 //
1137 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001138 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001139 (BOp->getOpcode() == Instruction::And ||
1140 BOp->getOpcode() == Instruction::Or)) {
1141 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1142 // If the compares in later blocks need to use values not currently
1143 // exported from this block, export them now. This block should always
1144 // be the first entry.
1145 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001147 // Allow some cases to be rejected.
1148 if (ShouldEmitAsBranches(SwitchCases)) {
1149 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1150 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1151 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1152 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001154 // Emit the branch for this block.
1155 visitSwitchCase(SwitchCases[0]);
1156 SwitchCases.erase(SwitchCases.begin());
1157 return;
1158 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001159
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001160 // Okay, we decided not to do this, remove any inserted MBB's and clear
1161 // SwitchCases.
1162 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001163 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001164
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001165 SwitchCases.clear();
1166 }
1167 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001169 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001170 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001171 NULL, Succ0MBB, Succ1MBB, CurMBB);
1172 // Use visitSwitchCase to actually insert the fast branch sequence for this
1173 // cond branch.
1174 visitSwitchCase(CB);
1175}
1176
1177/// visitSwitchCase - Emits the necessary code to represent a single node in
1178/// the binary search tree resulting from lowering a switch instruction.
1179void SelectionDAGLowering::visitSwitchCase(CaseBlock &CB) {
1180 SDValue Cond;
1181 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001182 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001183
1184 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001185 if (CB.CmpMHS == NULL) {
1186 // Fold "(X == true)" to X and "(X == false)" to !X to
1187 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001188 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001189 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001190 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001191 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001192 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001193 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001194 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001195 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001196 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001197 } else {
1198 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1199
Anton Korobeynikov23218582008-12-23 22:25:27 +00001200 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1201 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001202
1203 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001204 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205
1206 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001207 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001208 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001209 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001210 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001211 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001212 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001213 DAG.getConstant(High-Low, VT), ISD::SETULE);
1214 }
1215 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001216
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001217 // Update successor info
1218 CurMBB->addSuccessor(CB.TrueBB);
1219 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001220
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001221 // Set NextBlock to be the MBB immediately after the current one, if any.
1222 // This is used to avoid emitting unnecessary branches to the next block.
1223 MachineBasicBlock *NextBlock = 0;
1224 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001225 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001226 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001227
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001228 // If the lhs block is the next block, invert the condition so that we can
1229 // fall through to the lhs instead of the rhs block.
1230 if (CB.TrueBB == NextBlock) {
1231 std::swap(CB.TrueBB, CB.FalseBB);
1232 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001233 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001234 }
Dale Johannesenf5d97892009-02-04 01:48:28 +00001235 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001236 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001237 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001238
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239 // If the branch was constant folded, fix up the CFG.
1240 if (BrCond.getOpcode() == ISD::BR) {
1241 CurMBB->removeSuccessor(CB.FalseBB);
1242 DAG.setRoot(BrCond);
1243 } else {
1244 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001245 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001246 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001247
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001248 if (CB.FalseBB == NextBlock)
1249 DAG.setRoot(BrCond);
1250 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001252 DAG.getBasicBlock(CB.FalseBB)));
1253 }
1254}
1255
1256/// visitJumpTable - Emit JumpTable node in the current MBB
1257void SelectionDAGLowering::visitJumpTable(JumpTable &JT) {
1258 // Emit the code for the jump table
1259 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001260 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001261 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1262 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Scott Michelfdc40a02009-02-17 22:15:04 +00001264 DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001265 MVT::Other, Index.getValue(1),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001266 Table, Index));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001267}
1268
1269/// visitJumpTableHeader - This function emits necessary code to produce index
1270/// in the JumpTable from switch case.
1271void SelectionDAGLowering::visitJumpTableHeader(JumpTable &JT,
1272 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001273 // Subtract the lowest switch case value from the value being switched on and
1274 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275 // difference between smallest and largest cases.
1276 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001277 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001278 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001279 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001280
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001281 // The SDNode we just created, which holds the value being switched on minus
1282 // the the smallest case value, needs to be copied to a virtual register so it
1283 // can be used as an index into the jump table in a subsequent basic block.
1284 // This value may be smaller or larger than the target's pointer type, and
1285 // therefore require extension or truncating.
Duncan Sands3a66a682009-10-13 21:04:12 +00001286 SwitchOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001287
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001288 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001289 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1290 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291 JT.Reg = JumpTableReg;
1292
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001293 // Emit the range check for the jump table, and branch to the default block
1294 // for the switch statement if the value being switched on exceeds the largest
1295 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001296 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
1297 TLI.getSetCCResultType(SUB.getValueType()), SUB,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001298 DAG.getConstant(JTH.Last-JTH.First,VT),
1299 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001300
1301 // Set NextBlock to be the MBB immediately after the current one, if any.
1302 // This is used to avoid emitting unnecessary branches to the next block.
1303 MachineBasicBlock *NextBlock = 0;
1304 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001305 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001306 NextBlock = BBI;
1307
Dale Johannesen66978ee2009-01-31 02:22:37 +00001308 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001310 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001311
1312 if (JT.MBB == NextBlock)
1313 DAG.setRoot(BrCond);
1314 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001315 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001316 DAG.getBasicBlock(JT.MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001317}
1318
1319/// visitBitTestHeader - This function emits necessary code to produce value
1320/// suitable for "bit tests"
1321void SelectionDAGLowering::visitBitTestHeader(BitTestBlock &B) {
1322 // Subtract the minimum value
1323 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001324 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001325 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001326 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001327
1328 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001329 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
1330 TLI.getSetCCResultType(SUB.getValueType()),
1331 SUB, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001332 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001333
Duncan Sands3a66a682009-10-13 21:04:12 +00001334 SDValue ShiftOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001335
Duncan Sands92abc622009-01-31 15:50:11 +00001336 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001337 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1338 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001339
1340 // Set NextBlock to be the MBB immediately after the current one, if any.
1341 // This is used to avoid emitting unnecessary branches to the next block.
1342 MachineBasicBlock *NextBlock = 0;
1343 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001344 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001345 NextBlock = BBI;
1346
1347 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1348
1349 CurMBB->addSuccessor(B.Default);
1350 CurMBB->addSuccessor(MBB);
1351
Dale Johannesen66978ee2009-01-31 02:22:37 +00001352 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001353 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001354 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001355
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001356 if (MBB == NextBlock)
1357 DAG.setRoot(BrRange);
1358 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001359 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001360 DAG.getBasicBlock(MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001361}
1362
1363/// visitBitTestCase - this function produces one "bit test"
1364void SelectionDAGLowering::visitBitTestCase(MachineBasicBlock* NextMBB,
1365 unsigned Reg,
1366 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001367 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001368 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001369 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001370 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001371 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001372 DAG.getConstant(1, TLI.getPointerTy()),
1373 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001374
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001375 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001376 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001377 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001378 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001379 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1380 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001381 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001382 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001383
1384 CurMBB->addSuccessor(B.TargetBB);
1385 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001386
Dale Johannesen66978ee2009-01-31 02:22:37 +00001387 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001388 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001389 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001390
1391 // Set NextBlock to be the MBB immediately after the current one, if any.
1392 // This is used to avoid emitting unnecessary branches to the next block.
1393 MachineBasicBlock *NextBlock = 0;
1394 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001395 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 NextBlock = BBI;
1397
1398 if (NextMBB == NextBlock)
1399 DAG.setRoot(BrAnd);
1400 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001401 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001402 DAG.getBasicBlock(NextMBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001403}
1404
1405void SelectionDAGLowering::visitInvoke(InvokeInst &I) {
1406 // Retrieve successors.
1407 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1408 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1409
Gabor Greifb67e6b32009-01-15 11:10:44 +00001410 const Value *Callee(I.getCalledValue());
1411 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001412 visitInlineAsm(&I);
1413 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001414 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001415
1416 // If the value of the invoke is used outside of its defining block, make it
1417 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001418 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001419
1420 // Update successor info
1421 CurMBB->addSuccessor(Return);
1422 CurMBB->addSuccessor(LandingPad);
1423
1424 // Drop into normal successor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001425 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001426 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001427 DAG.getBasicBlock(Return)));
1428}
1429
1430void SelectionDAGLowering::visitUnwind(UnwindInst &I) {
1431}
1432
1433/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1434/// small case ranges).
1435bool SelectionDAGLowering::handleSmallSwitchRange(CaseRec& CR,
1436 CaseRecVector& WorkList,
1437 Value* SV,
1438 MachineBasicBlock* Default) {
1439 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001441 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001442 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001443 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001444 return false;
1445
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001446 // Get the MachineFunction which holds the current MBB. This is used when
1447 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001448 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449
1450 // Figure out which block is immediately after the current one.
1451 MachineBasicBlock *NextBlock = 0;
1452 MachineFunction::iterator BBI = CR.CaseBB;
1453
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001454 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001455 NextBlock = BBI;
1456
1457 // TODO: If any two of the cases has the same destination, and if one value
1458 // is the same as the other, but has one bit unset that the other has set,
1459 // use bit manipulation to do two compares at once. For example:
1460 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001461
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001462 // Rearrange the case blocks so that the last one falls through if possible.
1463 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1464 // The last case block won't fall through into 'NextBlock' if we emit the
1465 // branches in this order. See if rearranging a case value would help.
1466 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1467 if (I->BB == NextBlock) {
1468 std::swap(*I, BackCase);
1469 break;
1470 }
1471 }
1472 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001474 // Create a CaseBlock record representing a conditional branch to
1475 // the Case's target mbb if the value being switched on SV is equal
1476 // to C.
1477 MachineBasicBlock *CurBlock = CR.CaseBB;
1478 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1479 MachineBasicBlock *FallThrough;
1480 if (I != E-1) {
1481 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1482 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001483
1484 // Put SV in a virtual register to make it available from the new blocks.
1485 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486 } else {
1487 // If the last case doesn't match, go to the default block.
1488 FallThrough = Default;
1489 }
1490
1491 Value *RHS, *LHS, *MHS;
1492 ISD::CondCode CC;
1493 if (I->High == I->Low) {
1494 // This is just small small case range :) containing exactly 1 case
1495 CC = ISD::SETEQ;
1496 LHS = SV; RHS = I->High; MHS = NULL;
1497 } else {
1498 CC = ISD::SETLE;
1499 LHS = I->Low; MHS = SV; RHS = I->High;
1500 }
1501 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001502
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001503 // If emitting the first comparison, just call visitSwitchCase to emit the
1504 // code into the current block. Otherwise, push the CaseBlock onto the
1505 // vector to be later processed by SDISel, and insert the node's MBB
1506 // before the next MBB.
1507 if (CurBlock == CurMBB)
1508 visitSwitchCase(CB);
1509 else
1510 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001511
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001512 CurBlock = FallThrough;
1513 }
1514
1515 return true;
1516}
1517
1518static inline bool areJTsAllowed(const TargetLowering &TLI) {
1519 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001520 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1521 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001522}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001523
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001524static APInt ComputeRange(const APInt &First, const APInt &Last) {
1525 APInt LastExt(Last), FirstExt(First);
1526 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1527 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1528 return (LastExt - FirstExt + 1ULL);
1529}
1530
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001531/// handleJTSwitchCase - Emit jumptable for current switch case range
1532bool SelectionDAGLowering::handleJTSwitchCase(CaseRec& CR,
1533 CaseRecVector& WorkList,
1534 Value* SV,
1535 MachineBasicBlock* Default) {
1536 Case& FrontCase = *CR.Range.first;
1537 Case& BackCase = *(CR.Range.second-1);
1538
Chris Lattnere880efe2009-11-07 07:50:34 +00001539 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1540 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001541
Chris Lattnere880efe2009-11-07 07:50:34 +00001542 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001543 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1544 I!=E; ++I)
1545 TSize += I->size();
1546
Chris Lattnere880efe2009-11-07 07:50:34 +00001547 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001548 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001549
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001550 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001551 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552 if (Density < 0.4)
1553 return false;
1554
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001555 DEBUG(errs() << "Lowering jump table\n"
1556 << "First entry: " << First << ". Last entry: " << Last << '\n'
1557 << "Range: " << Range
1558 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001559
1560 // Get the MachineFunction which holds the current MBB. This is used when
1561 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001562 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001563
1564 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001565 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001566 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567
1568 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1569
1570 // Create a new basic block to hold the code for loading the address
1571 // of the jump table, and jumping to it. Update successor information;
1572 // we will either branch to the default case for the switch, or the jump
1573 // table.
1574 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1575 CurMF->insert(BBI, JumpTableBB);
1576 CR.CaseBB->addSuccessor(Default);
1577 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001578
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001579 // Build a vector of destination BBs, corresponding to each target
1580 // of the jump table. If the value of the jump table slot corresponds to
1581 // a case statement, push the case's BB onto the vector, otherwise, push
1582 // the default BB.
1583 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001584 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001585 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001586 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1587 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1588
1589 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001590 DestBBs.push_back(I->BB);
1591 if (TEI==High)
1592 ++I;
1593 } else {
1594 DestBBs.push_back(Default);
1595 }
1596 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001597
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001598 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001599 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1600 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001601 E = DestBBs.end(); I != E; ++I) {
1602 if (!SuccsHandled[(*I)->getNumber()]) {
1603 SuccsHandled[(*I)->getNumber()] = true;
1604 JumpTableBB->addSuccessor(*I);
1605 }
1606 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001607
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001608 // Create a jump table index for this jump table, or return an existing
1609 // one.
1610 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001611
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001612 // Set the jump table information so that we can codegen it as a second
1613 // MachineBasicBlock
1614 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1615 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1616 if (CR.CaseBB == CurMBB)
1617 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 JTCases.push_back(JumpTableBlock(JTH, JT));
1620
1621 return true;
1622}
1623
1624/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1625/// 2 subtrees.
1626bool SelectionDAGLowering::handleBTSplitSwitchCase(CaseRec& CR,
1627 CaseRecVector& WorkList,
1628 Value* SV,
1629 MachineBasicBlock* Default) {
1630 // Get the MachineFunction which holds the current MBB. This is used when
1631 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001632 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001633
1634 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001635 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001636 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001637
1638 Case& FrontCase = *CR.Range.first;
1639 Case& BackCase = *(CR.Range.second-1);
1640 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1641
1642 // Size is the number of Cases represented by this range.
1643 unsigned Size = CR.Range.second - CR.Range.first;
1644
Chris Lattnere880efe2009-11-07 07:50:34 +00001645 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1646 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001647 double FMetric = 0;
1648 CaseItr Pivot = CR.Range.first + Size/2;
1649
1650 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1651 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001652 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1654 I!=E; ++I)
1655 TSize += I->size();
1656
Chris Lattnere880efe2009-11-07 07:50:34 +00001657 APInt LSize = FrontCase.size();
1658 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001659 DEBUG(errs() << "Selecting best pivot: \n"
1660 << "First: " << First << ", Last: " << Last <<'\n'
1661 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001662 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1663 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001664 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1665 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001666 APInt Range = ComputeRange(LEnd, RBegin);
1667 assert((Range - 2ULL).isNonNegative() &&
1668 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001669 double LDensity = (double)LSize.roundToDouble() /
1670 (LEnd - First + 1ULL).roundToDouble();
1671 double RDensity = (double)RSize.roundToDouble() /
1672 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001673 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001674 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001675 DEBUG(errs() <<"=>Step\n"
1676 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1677 << "LDensity: " << LDensity
1678 << ", RDensity: " << RDensity << '\n'
1679 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001680 if (FMetric < Metric) {
1681 Pivot = J;
1682 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001683 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001684 }
1685
1686 LSize += J->size();
1687 RSize -= J->size();
1688 }
1689 if (areJTsAllowed(TLI)) {
1690 // If our case is dense we *really* should handle it earlier!
1691 assert((FMetric > 0) && "Should handle dense range earlier!");
1692 } else {
1693 Pivot = CR.Range.first + Size/2;
1694 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001695
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696 CaseRange LHSR(CR.Range.first, Pivot);
1697 CaseRange RHSR(Pivot, CR.Range.second);
1698 Constant *C = Pivot->Low;
1699 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001700
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001701 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001702 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001703 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001704 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001705 // Pivot's Value, then we can branch directly to the LHS's Target,
1706 // rather than creating a leaf node for it.
1707 if ((LHSR.second - LHSR.first) == 1 &&
1708 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001709 cast<ConstantInt>(C)->getValue() ==
1710 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001711 TrueBB = LHSR.first->BB;
1712 } else {
1713 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1714 CurMF->insert(BBI, TrueBB);
1715 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001716
1717 // Put SV in a virtual register to make it available from the new blocks.
1718 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001720
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001721 // Similar to the optimization above, if the Value being switched on is
1722 // known to be less than the Constant CR.LT, and the current Case Value
1723 // is CR.LT - 1, then we can branch directly to the target block for
1724 // the current Case Value, rather than emitting a RHS leaf node for it.
1725 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001726 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1727 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001728 FalseBB = RHSR.first->BB;
1729 } else {
1730 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1731 CurMF->insert(BBI, FalseBB);
1732 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001733
1734 // Put SV in a virtual register to make it available from the new blocks.
1735 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001736 }
1737
1738 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001739 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001740 // Otherwise, branch to LHS.
1741 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1742
1743 if (CR.CaseBB == CurMBB)
1744 visitSwitchCase(CB);
1745 else
1746 SwitchCases.push_back(CB);
1747
1748 return true;
1749}
1750
1751/// handleBitTestsSwitchCase - if current case range has few destination and
1752/// range span less, than machine word bitwidth, encode case range into series
1753/// of masks and emit bit tests with these masks.
1754bool SelectionDAGLowering::handleBitTestsSwitchCase(CaseRec& CR,
1755 CaseRecVector& WorkList,
1756 Value* SV,
1757 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001758 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001759 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001760
1761 Case& FrontCase = *CR.Range.first;
1762 Case& BackCase = *(CR.Range.second-1);
1763
1764 // Get the MachineFunction which holds the current MBB. This is used when
1765 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001766 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001767
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001768 // If target does not have legal shift left, do not emit bit tests at all.
1769 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1770 return false;
1771
Anton Korobeynikov23218582008-12-23 22:25:27 +00001772 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001773 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1774 I!=E; ++I) {
1775 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001776 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001777 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001778
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001779 // Count unique destinations
1780 SmallSet<MachineBasicBlock*, 4> Dests;
1781 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1782 Dests.insert(I->BB);
1783 if (Dests.size() > 3)
1784 // Don't bother the code below, if there are too much unique destinations
1785 return false;
1786 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001787 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1788 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001789
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001790 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001791 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1792 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001793 APInt cmpRange = maxValue - minValue;
1794
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001795 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1796 << "Low bound: " << minValue << '\n'
1797 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001798
1799 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001800 (!(Dests.size() == 1 && numCmps >= 3) &&
1801 !(Dests.size() == 2 && numCmps >= 5) &&
1802 !(Dests.size() >= 3 && numCmps >= 6)))
1803 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001804
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001805 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001806 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 // Optimize the case where all the case values fit in a
1809 // word without having to subtract minValue. In this case,
1810 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001811 if (minValue.isNonNegative() &&
1812 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1813 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001814 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001815 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001817
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001818 CaseBitsVector CasesBits;
1819 unsigned i, count = 0;
1820
1821 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1822 MachineBasicBlock* Dest = I->BB;
1823 for (i = 0; i < count; ++i)
1824 if (Dest == CasesBits[i].BB)
1825 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001826
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001827 if (i == count) {
1828 assert((count < 3) && "Too much destinations to test!");
1829 CasesBits.push_back(CaseBits(0, Dest, 0));
1830 count++;
1831 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001832
1833 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1834 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1835
1836 uint64_t lo = (lowValue - lowBound).getZExtValue();
1837 uint64_t hi = (highValue - lowBound).getZExtValue();
1838
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001839 for (uint64_t j = lo; j <= hi; j++) {
1840 CasesBits[i].Mask |= 1ULL << j;
1841 CasesBits[i].Bits++;
1842 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001843
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001844 }
1845 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001846
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001847 BitTestInfo BTC;
1848
1849 // Figure out which block is immediately after the current one.
1850 MachineFunction::iterator BBI = CR.CaseBB;
1851 ++BBI;
1852
1853 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1854
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001855 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001856 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001857 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
1858 << ", Bits: " << CasesBits[i].Bits
1859 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001860
1861 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1862 CurMF->insert(BBI, CaseBB);
1863 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1864 CaseBB,
1865 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001866
1867 // Put SV in a virtual register to make it available from the new blocks.
1868 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001869 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001870
1871 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001872 -1U, (CR.CaseBB == CurMBB),
1873 CR.CaseBB, Default, BTC);
1874
1875 if (CR.CaseBB == CurMBB)
1876 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001877
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001878 BitTestCases.push_back(BTB);
1879
1880 return true;
1881}
1882
1883
1884/// Clusterify - Transform simple list of Cases into list of CaseRange's
Anton Korobeynikov23218582008-12-23 22:25:27 +00001885size_t SelectionDAGLowering::Clusterify(CaseVector& Cases,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001887 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001888
1889 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001890 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001891 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1892 Cases.push_back(Case(SI.getSuccessorValue(i),
1893 SI.getSuccessorValue(i),
1894 SMBB));
1895 }
1896 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1897
1898 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001899 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001900 // Must recompute end() each iteration because it may be
1901 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001902 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1903 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1904 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001905 MachineBasicBlock* nextBB = J->BB;
1906 MachineBasicBlock* currentBB = I->BB;
1907
1908 // If the two neighboring cases go to the same destination, merge them
1909 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001910 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001911 I->High = J->High;
1912 J = Cases.erase(J);
1913 } else {
1914 I = J++;
1915 }
1916 }
1917
1918 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1919 if (I->Low != I->High)
1920 // A range counts double, since it requires two compares.
1921 ++numCmps;
1922 }
1923
1924 return numCmps;
1925}
1926
Anton Korobeynikov23218582008-12-23 22:25:27 +00001927void SelectionDAGLowering::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001928 // Figure out which block is immediately after the current one.
1929 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001930
1931 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1932
1933 // If there is only the default destination, branch to it if it is not the
1934 // next basic block. Otherwise, just fall through.
1935 if (SI.getNumOperands() == 2) {
1936 // Update machine-CFG edges.
1937
1938 // If this is not a fall-through branch, emit the branch.
1939 CurMBB->addSuccessor(Default);
1940 if (Default != NextBlock)
Dale Johannesen66978ee2009-01-31 02:22:37 +00001941 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001942 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001943 DAG.getBasicBlock(Default)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001944 return;
1945 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001946
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001947 // If there are any non-default case statements, create a vector of Cases
1948 // representing each one, and sort the vector so that we can efficiently
1949 // create a binary search tree from them.
1950 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001951 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001952 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
1953 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001954 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955
1956 // Get the Value to be switched on and default basic blocks, which will be
1957 // inserted into CaseBlock records, representing basic blocks in the binary
1958 // search tree.
1959 Value *SV = SI.getOperand(0);
1960
1961 // Push the initial CaseRec onto the worklist
1962 CaseRecVector WorkList;
1963 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
1964
1965 while (!WorkList.empty()) {
1966 // Grab a record representing a case range to process off the worklist
1967 CaseRec CR = WorkList.back();
1968 WorkList.pop_back();
1969
1970 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
1971 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001972
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001973 // If the range has few cases (two or less) emit a series of specific
1974 // tests.
1975 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
1976 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001977
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001978 // If the switch has more than 5 blocks, and at least 40% dense, and the
1979 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001980 // lowering the switch to a binary tree of conditional branches.
1981 if (handleJTSwitchCase(CR, WorkList, SV, Default))
1982 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001983
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001984 // Emit binary tree. We need to pick a pivot, and push left and right ranges
1985 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
1986 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
1987 }
1988}
1989
Chris Lattnerab21db72009-10-28 00:19:10 +00001990void SelectionDAGLowering::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00001991 // Update machine-CFG edges.
1992 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
1993 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
1994
Dan Gohman64825152009-10-27 21:56:26 +00001995 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
1996 MVT::Other, getControlRoot(),
1997 getValue(I.getAddress())));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00001998}
1999
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002000
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002001void SelectionDAGLowering::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002002 // -0.0 - X --> fneg
2003 const Type *Ty = I.getType();
2004 if (isa<VectorType>(Ty)) {
2005 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2006 const VectorType *DestTy = cast<VectorType>(I.getType());
2007 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002008 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002009 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002010 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002011 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002012 SDValue Op2 = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002013 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002014 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002015 return;
2016 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002017 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002018 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002019 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002020 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002021 SDValue Op2 = getValue(I.getOperand(1));
2022 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2023 Op2.getValueType(), Op2));
2024 return;
2025 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002027 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002028}
2029
2030void SelectionDAGLowering::visitBinary(User &I, unsigned OpCode) {
2031 SDValue Op1 = getValue(I.getOperand(0));
2032 SDValue Op2 = getValue(I.getOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002033
Scott Michelfdc40a02009-02-17 22:15:04 +00002034 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002035 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002036}
2037
2038void SelectionDAGLowering::visitShift(User &I, unsigned Opcode) {
2039 SDValue Op1 = getValue(I.getOperand(0));
2040 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002041 if (!isa<VectorType>(I.getType()) &&
2042 Op2.getValueType() != TLI.getShiftAmountTy()) {
2043 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002044 EVT PTy = TLI.getPointerTy();
2045 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002046 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002047 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2048 TLI.getShiftAmountTy(), Op2);
2049 // If the operand is larger than the shift count type but the shift
2050 // count type has enough bits to represent any shift value, truncate
2051 // it now. This is a common case and it exposes the truncate to
2052 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002053 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002054 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2055 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2056 TLI.getShiftAmountTy(), Op2);
2057 // Otherwise we'll need to temporarily settle for some other
2058 // convenient type; type legalization will make adjustments as
2059 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002060 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002061 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002062 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002063 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002064 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002065 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002066 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002067
Scott Michelfdc40a02009-02-17 22:15:04 +00002068 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002069 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002070}
2071
2072void SelectionDAGLowering::visitICmp(User &I) {
2073 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2074 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2075 predicate = IC->getPredicate();
2076 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2077 predicate = ICmpInst::Predicate(IC->getPredicate());
2078 SDValue Op1 = getValue(I.getOperand(0));
2079 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002080 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002081
Owen Andersone50ed302009-08-10 22:56:29 +00002082 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002083 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002084}
2085
2086void SelectionDAGLowering::visitFCmp(User &I) {
2087 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2088 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2089 predicate = FC->getPredicate();
2090 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2091 predicate = FCmpInst::Predicate(FC->getPredicate());
2092 SDValue Op1 = getValue(I.getOperand(0));
2093 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002094 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002095 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002096 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002097}
2098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002099void SelectionDAGLowering::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002100 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002101 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2102 unsigned NumValues = ValueVTs.size();
2103 if (NumValues != 0) {
2104 SmallVector<SDValue, 4> Values(NumValues);
2105 SDValue Cond = getValue(I.getOperand(0));
2106 SDValue TrueVal = getValue(I.getOperand(1));
2107 SDValue FalseVal = getValue(I.getOperand(2));
2108
2109 for (unsigned i = 0; i != NumValues; ++i)
Scott Michelfdc40a02009-02-17 22:15:04 +00002110 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002111 TrueVal.getValueType(), Cond,
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002112 SDValue(TrueVal.getNode(), TrueVal.getResNo() + i),
2113 SDValue(FalseVal.getNode(), FalseVal.getResNo() + i));
2114
Scott Michelfdc40a02009-02-17 22:15:04 +00002115 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002116 DAG.getVTList(&ValueVTs[0], NumValues),
2117 &Values[0], NumValues));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002118 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002119}
2120
2121
2122void SelectionDAGLowering::visitTrunc(User &I) {
2123 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2124 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002125 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002126 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127}
2128
2129void SelectionDAGLowering::visitZExt(User &I) {
2130 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2131 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2132 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002133 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002134 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002135}
2136
2137void SelectionDAGLowering::visitSExt(User &I) {
2138 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2139 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2140 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002141 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002142 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002143}
2144
2145void SelectionDAGLowering::visitFPTrunc(User &I) {
2146 // FPTrunc is never a no-op cast, no need to check
2147 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002148 EVT DestVT = TLI.getValueType(I.getType());
Scott Michelfdc40a02009-02-17 22:15:04 +00002149 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002150 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151}
2152
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002153void SelectionDAGLowering::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002154 // FPTrunc is never a no-op cast, no need to check
2155 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002156 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002157 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002158}
2159
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002160void SelectionDAGLowering::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002161 // FPToUI is never a no-op cast, no need to check
2162 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002163 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002164 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002165}
2166
2167void SelectionDAGLowering::visitFPToSI(User &I) {
2168 // FPToSI is never a no-op cast, no need to check
2169 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002170 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002171 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002172}
2173
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002174void SelectionDAGLowering::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002175 // UIToFP is never a no-op cast, no need to check
2176 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002177 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002178 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002179}
2180
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002181void SelectionDAGLowering::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002182 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002183 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002184 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002185 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002186}
2187
2188void SelectionDAGLowering::visitPtrToInt(User &I) {
2189 // What to do depends on the size of the integer and the size of the pointer.
2190 // We can either truncate, zero extend, or no-op, accordingly.
2191 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002192 EVT SrcVT = N.getValueType();
2193 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002194 SDValue Result = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002195 setValue(&I, Result);
2196}
2197
2198void SelectionDAGLowering::visitIntToPtr(User &I) {
2199 // What to do depends on the size of the integer and the size of the pointer.
2200 // We can either truncate, zero extend, or no-op, accordingly.
2201 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002202 EVT SrcVT = N.getValueType();
2203 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002204 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205}
2206
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002207void SelectionDAGLowering::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002209 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002210
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002211 // BitCast assures us that source and destination are the same size so this
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002212 // is either a BIT_CONVERT or a no-op.
2213 if (DestVT != N.getValueType())
Scott Michelfdc40a02009-02-17 22:15:04 +00002214 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002215 DestVT, N)); // convert types
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002216 else
2217 setValue(&I, N); // noop cast.
2218}
2219
2220void SelectionDAGLowering::visitInsertElement(User &I) {
2221 SDValue InVec = getValue(I.getOperand(0));
2222 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002223 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002224 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225 getValue(I.getOperand(2)));
2226
Scott Michelfdc40a02009-02-17 22:15:04 +00002227 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228 TLI.getValueType(I.getType()),
2229 InVec, InVal, InIdx));
2230}
2231
2232void SelectionDAGLowering::visitExtractElement(User &I) {
2233 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002234 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002235 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002236 getValue(I.getOperand(1)));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002237 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002238 TLI.getValueType(I.getType()), InVec, InIdx));
2239}
2240
Mon P Wangaeb06d22008-11-10 04:46:22 +00002241
2242// Utility for visitShuffleVector - Returns true if the mask is mask starting
2243// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002244static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2245 unsigned MaskNumElts = Mask.size();
2246 for (unsigned i = 0; i != MaskNumElts; ++i)
2247 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002248 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002249 return true;
2250}
2251
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252void SelectionDAGLowering::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002253 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002254 SDValue Src1 = getValue(I.getOperand(0));
2255 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002256
Nate Begeman9008ca62009-04-27 18:41:29 +00002257 // Convert the ConstantVector mask operand into an array of ints, with -1
2258 // representing undef values.
2259 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002260 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2261 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002262 unsigned MaskNumElts = MaskElts.size();
2263 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002264 if (isa<UndefValue>(MaskElts[i]))
2265 Mask.push_back(-1);
2266 else
2267 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2268 }
2269
Owen Andersone50ed302009-08-10 22:56:29 +00002270 EVT VT = TLI.getValueType(I.getType());
2271 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002272 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002273
Mon P Wangc7849c22008-11-16 05:06:27 +00002274 if (SrcNumElts == MaskNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002275 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2276 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002277 return;
2278 }
2279
2280 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002281 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2282 // Mask is longer than the source vectors and is a multiple of the source
2283 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002284 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002285 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2286 // The shuffle is concatenating two vectors together.
Scott Michelfdc40a02009-02-17 22:15:04 +00002287 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002288 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002289 return;
2290 }
2291
Mon P Wangc7849c22008-11-16 05:06:27 +00002292 // Pad both vectors with undefs to make them the same length as the mask.
2293 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002294 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2295 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002296 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002297
Nate Begeman9008ca62009-04-27 18:41:29 +00002298 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2299 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002300 MOps1[0] = Src1;
2301 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002302
2303 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2304 getCurDebugLoc(), VT,
2305 &MOps1[0], NumConcat);
2306 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2307 getCurDebugLoc(), VT,
2308 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002309
Mon P Wangaeb06d22008-11-10 04:46:22 +00002310 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002311 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002312 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002313 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002314 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002315 MappedOps.push_back(Idx);
2316 else
2317 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002318 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002319 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2320 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002321 return;
2322 }
2323
Mon P Wangc7849c22008-11-16 05:06:27 +00002324 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002325 // Analyze the access pattern of the vector to see if we can extract
2326 // two subvectors and do the shuffle. The analysis is done by calculating
2327 // the range of elements the mask access on both vectors.
2328 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2329 int MaxRange[2] = {-1, -1};
2330
Nate Begeman5a5ca152009-04-29 05:20:52 +00002331 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002332 int Idx = Mask[i];
2333 int Input = 0;
2334 if (Idx < 0)
2335 continue;
2336
Nate Begeman5a5ca152009-04-29 05:20:52 +00002337 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002338 Input = 1;
2339 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002340 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002341 if (Idx > MaxRange[Input])
2342 MaxRange[Input] = Idx;
2343 if (Idx < MinRange[Input])
2344 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002345 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002346
Mon P Wangc7849c22008-11-16 05:06:27 +00002347 // Check if the access is smaller than the vector size and can we find
2348 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002349 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002350 int StartIdx[2]; // StartIdx to extract from
2351 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002352 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002353 RangeUse[Input] = 0; // Unused
2354 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002355 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002356 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002357 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002358 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002359 RangeUse[Input] = 1; // Extract from beginning of the vector
2360 StartIdx[Input] = 0;
2361 } else {
2362 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002363 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002364 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002365 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002366 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002367 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002368 }
2369
Bill Wendling636e2582009-08-21 18:16:06 +00002370 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002371 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002372 return;
2373 }
2374 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2375 // Extract appropriate subvector and generate a vector shuffle
2376 for (int Input=0; Input < 2; ++Input) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002377 SDValue& Src = Input == 0 ? Src1 : Src2;
Mon P Wangc7849c22008-11-16 05:06:27 +00002378 if (RangeUse[Input] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002379 Src = DAG.getUNDEF(VT);
Mon P Wangc7849c22008-11-16 05:06:27 +00002380 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002381 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002382 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002383 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002384 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002385 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002386 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002387 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002388 int Idx = Mask[i];
2389 if (Idx < 0)
2390 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002391 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002392 MappedOps.push_back(Idx - StartIdx[0]);
2393 else
2394 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002395 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002396 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2397 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002398 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002399 }
2400 }
2401
Mon P Wangc7849c22008-11-16 05:06:27 +00002402 // We can't use either concat vectors or extract subvectors so fall back to
2403 // replacing the shuffle with extract and build vector.
2404 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002405 EVT EltVT = VT.getVectorElementType();
2406 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002407 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002408 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002409 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002410 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002411 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002412 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002413 if (Idx < (int)SrcNumElts)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002414 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002415 EltVT, Src1, DAG.getConstant(Idx, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002416 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002417 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Scott Michelfdc40a02009-02-17 22:15:04 +00002418 EltVT, Src2,
Mon P Wangc7849c22008-11-16 05:06:27 +00002419 DAG.getConstant(Idx - SrcNumElts, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002420 }
2421 }
Evan Chenga87008d2009-02-25 22:49:59 +00002422 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2423 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002424}
2425
2426void SelectionDAGLowering::visitInsertValue(InsertValueInst &I) {
2427 const Value *Op0 = I.getOperand(0);
2428 const Value *Op1 = I.getOperand(1);
2429 const Type *AggTy = I.getType();
2430 const Type *ValTy = Op1->getType();
2431 bool IntoUndef = isa<UndefValue>(Op0);
2432 bool FromUndef = isa<UndefValue>(Op1);
2433
2434 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2435 I.idx_begin(), I.idx_end());
2436
Owen Andersone50ed302009-08-10 22:56:29 +00002437 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002438 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002439 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002440 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2441
2442 unsigned NumAggValues = AggValueVTs.size();
2443 unsigned NumValValues = ValValueVTs.size();
2444 SmallVector<SDValue, 4> Values(NumAggValues);
2445
2446 SDValue Agg = getValue(Op0);
2447 SDValue Val = getValue(Op1);
2448 unsigned i = 0;
2449 // Copy the beginning value(s) from the original aggregate.
2450 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002451 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002452 SDValue(Agg.getNode(), Agg.getResNo() + i);
2453 // Copy values from the inserted value(s).
2454 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002455 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002456 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2457 // Copy remaining value(s) from the original aggregate.
2458 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002459 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002460 SDValue(Agg.getNode(), Agg.getResNo() + i);
2461
Scott Michelfdc40a02009-02-17 22:15:04 +00002462 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002463 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2464 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002465}
2466
2467void SelectionDAGLowering::visitExtractValue(ExtractValueInst &I) {
2468 const Value *Op0 = I.getOperand(0);
2469 const Type *AggTy = Op0->getType();
2470 const Type *ValTy = I.getType();
2471 bool OutOfUndef = isa<UndefValue>(Op0);
2472
2473 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2474 I.idx_begin(), I.idx_end());
2475
Owen Andersone50ed302009-08-10 22:56:29 +00002476 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002477 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2478
2479 unsigned NumValValues = ValValueVTs.size();
2480 SmallVector<SDValue, 4> Values(NumValValues);
2481
2482 SDValue Agg = getValue(Op0);
2483 // Copy out the selected value(s).
2484 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2485 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002486 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002487 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002488 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002489
Scott Michelfdc40a02009-02-17 22:15:04 +00002490 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002491 DAG.getVTList(&ValValueVTs[0], NumValValues),
2492 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002493}
2494
2495
2496void SelectionDAGLowering::visitGetElementPtr(User &I) {
2497 SDValue N = getValue(I.getOperand(0));
2498 const Type *Ty = I.getOperand(0)->getType();
2499
2500 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2501 OI != E; ++OI) {
2502 Value *Idx = *OI;
2503 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2504 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2505 if (Field) {
2506 // N = N + Offset
2507 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002508 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002509 DAG.getIntPtrConstant(Offset));
2510 }
2511 Ty = StTy->getElementType(Field);
2512 } else {
2513 Ty = cast<SequentialType>(Ty)->getElementType();
2514
2515 // If this is a constant subscript, handle it quickly.
2516 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2517 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002518 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002519 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002520 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002521 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002522 unsigned PtrBits = PTy.getSizeInBits();
Evan Cheng65b52df2009-02-09 21:01:06 +00002523 if (PtrBits < 64) {
2524 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2525 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002526 DAG.getConstant(Offs, MVT::i64));
Evan Cheng65b52df2009-02-09 21:01:06 +00002527 } else
Evan Chengb1032a82009-02-09 20:54:38 +00002528 OffsVal = DAG.getIntPtrConstant(Offs);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002529 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002530 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002531 continue;
2532 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002533
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002534 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002535 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2536 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002537 SDValue IdxN = getValue(Idx);
2538
2539 // If the index is smaller or larger than intptr_t, truncate or extend
2540 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002541 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002542
2543 // If this is a multiply by a power of two, turn it into a shl
2544 // immediately. This is a very common case.
2545 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002546 if (ElementSize.isPowerOf2()) {
2547 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002548 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002549 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002550 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002551 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002552 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002553 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002554 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002555 }
2556 }
2557
Scott Michelfdc40a02009-02-17 22:15:04 +00002558 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002559 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002560 }
2561 }
2562 setValue(&I, N);
2563}
2564
2565void SelectionDAGLowering::visitAlloca(AllocaInst &I) {
2566 // If this is a fixed sized alloca in the entry block of the function,
2567 // allocate it statically on the stack.
2568 if (FuncInfo.StaticAllocaMap.count(&I))
2569 return; // getValue will auto-populate this.
2570
2571 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002572 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002573 unsigned Align =
2574 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2575 I.getAlignment());
2576
2577 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002578
2579 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2580 AllocSize,
2581 DAG.getConstant(TySize, AllocSize.getValueType()));
2582
2583
2584
Owen Andersone50ed302009-08-10 22:56:29 +00002585 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002586 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002587
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002588 // Handle alignment. If the requested alignment is less than or equal to
2589 // the stack alignment, ignore it. If the size is greater than or equal to
2590 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2591 unsigned StackAlign =
2592 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2593 if (Align <= StackAlign)
2594 Align = 0;
2595
2596 // Round the size of the allocation up to the stack alignment size
2597 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002598 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002599 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002600 DAG.getIntPtrConstant(StackAlign-1));
2601 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002602 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002603 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002604 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2605
2606 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002607 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002608 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002609 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002610 setValue(&I, DSA);
2611 DAG.setRoot(DSA.getValue(1));
2612
2613 // Inform the Frame Information that we have just allocated a variable-sized
2614 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002615 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002616}
2617
2618void SelectionDAGLowering::visitLoad(LoadInst &I) {
2619 const Value *SV = I.getOperand(0);
2620 SDValue Ptr = getValue(SV);
2621
2622 const Type *Ty = I.getType();
2623 bool isVolatile = I.isVolatile();
2624 unsigned Alignment = I.getAlignment();
2625
Owen Andersone50ed302009-08-10 22:56:29 +00002626 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002627 SmallVector<uint64_t, 4> Offsets;
2628 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2629 unsigned NumValues = ValueVTs.size();
2630 if (NumValues == 0)
2631 return;
2632
2633 SDValue Root;
2634 bool ConstantMemory = false;
2635 if (I.isVolatile())
2636 // Serialize volatile loads with other side effects.
2637 Root = getRoot();
2638 else if (AA->pointsToConstantMemory(SV)) {
2639 // Do not serialize (non-volatile) loads of constant memory with anything.
2640 Root = DAG.getEntryNode();
2641 ConstantMemory = true;
2642 } else {
2643 // Do not serialize non-volatile loads against each other.
2644 Root = DAG.getRoot();
2645 }
2646
2647 SmallVector<SDValue, 4> Values(NumValues);
2648 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002649 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002650 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002651 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Nate Begemane6798372009-09-15 00:13:12 +00002652 DAG.getNode(ISD::ADD, getCurDebugLoc(),
2653 PtrVT, Ptr,
2654 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002655 SV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002656 Values[i] = L;
2657 Chains[i] = L.getValue(1);
2658 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002659
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002660 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002661 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002662 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002663 &Chains[0], NumValues);
2664 if (isVolatile)
2665 DAG.setRoot(Chain);
2666 else
2667 PendingLoads.push_back(Chain);
2668 }
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(&ValueVTs[0], NumValues),
2672 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002673}
2674
2675
2676void SelectionDAGLowering::visitStore(StoreInst &I) {
2677 Value *SrcV = I.getOperand(0);
2678 Value *PtrV = I.getOperand(1);
2679
Owen Andersone50ed302009-08-10 22:56:29 +00002680 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002681 SmallVector<uint64_t, 4> Offsets;
2682 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2683 unsigned NumValues = ValueVTs.size();
2684 if (NumValues == 0)
2685 return;
2686
2687 // Get the lowered operands. Note that we do this after
2688 // checking if NumResults is zero, because with zero results
2689 // the operands won't have values in the map.
2690 SDValue Src = getValue(SrcV);
2691 SDValue Ptr = getValue(PtrV);
2692
2693 SDValue Root = getRoot();
2694 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002695 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002696 bool isVolatile = I.isVolatile();
2697 unsigned Alignment = I.getAlignment();
2698 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002699 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002700 SDValue(Src.getNode(), Src.getResNo() + i),
Scott Michelfdc40a02009-02-17 22:15:04 +00002701 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002702 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002703 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002704 PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002705
Scott Michelfdc40a02009-02-17 22:15:04 +00002706 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002707 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002708}
2709
2710/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2711/// node.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002712void SelectionDAGLowering::visitTargetIntrinsic(CallInst &I,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002713 unsigned Intrinsic) {
2714 bool HasChain = !I.doesNotAccessMemory();
2715 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2716
2717 // Build the operand list.
2718 SmallVector<SDValue, 8> Ops;
2719 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2720 if (OnlyLoad) {
2721 // We don't need to serialize loads against other loads.
2722 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002723 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002724 Ops.push_back(getRoot());
2725 }
2726 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002727
2728 // Info is set by getTgtMemInstrinsic
2729 TargetLowering::IntrinsicInfo Info;
2730 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2731
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002732 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002733 if (!IsTgtIntrinsic)
2734 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002735
2736 // Add all operands of the call to the operand list.
2737 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2738 SDValue Op = getValue(I.getOperand(i));
2739 assert(TLI.isTypeLegal(Op.getValueType()) &&
2740 "Intrinsic uses a non-legal type?");
2741 Ops.push_back(Op);
2742 }
2743
Owen Andersone50ed302009-08-10 22:56:29 +00002744 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002745 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2746#ifndef NDEBUG
2747 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2748 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2749 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002750 }
Bob Wilson8d919552009-07-31 22:41:21 +00002751#endif // NDEBUG
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002752 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002753 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002754
Bob Wilson8d919552009-07-31 22:41:21 +00002755 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002756
2757 // Create the node.
2758 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002759 if (IsTgtIntrinsic) {
2760 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002761 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002762 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002763 Info.memVT, Info.ptrVal, Info.offset,
2764 Info.align, Info.vol,
2765 Info.readMem, Info.writeMem);
2766 }
2767 else if (!HasChain)
Scott Michelfdc40a02009-02-17 22:15:04 +00002768 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002769 VTs, &Ops[0], Ops.size());
Owen Anderson1d0be152009-08-13 21:58:54 +00002770 else if (I.getType() != Type::getVoidTy(*DAG.getContext()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002771 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002772 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002773 else
Scott Michelfdc40a02009-02-17 22:15:04 +00002774 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002775 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002776
2777 if (HasChain) {
2778 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2779 if (OnlyLoad)
2780 PendingLoads.push_back(Chain);
2781 else
2782 DAG.setRoot(Chain);
2783 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002784 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002785 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002786 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002787 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002788 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002789 setValue(&I, Result);
2790 }
2791}
2792
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002793/// GetSignificand - Get the significand and build it into a floating-point
2794/// number with exponent of 1:
2795///
2796/// Op = (Op & 0x007fffff) | 0x3f800000;
2797///
2798/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002799static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002800GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002801 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2802 DAG.getConstant(0x007fffff, MVT::i32));
2803 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2804 DAG.getConstant(0x3f800000, MVT::i32));
2805 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002806}
2807
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002808/// GetExponent - Get the exponent:
2809///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002810/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002811///
2812/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002813static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002814GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
2815 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002816 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2817 DAG.getConstant(0x7f800000, MVT::i32));
2818 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002819 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002820 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2821 DAG.getConstant(127, MVT::i32));
2822 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002823}
2824
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002825/// getF32Constant - Get 32-bit floating point constant.
2826static SDValue
2827getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002828 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002829}
2830
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002831/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832/// visitIntrinsicCall: I is a call instruction
2833/// Op is the associated NodeType for I
2834const char *
2835SelectionDAGLowering::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002836 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002837 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002838 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002839 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002840 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002841 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002842 getValue(I.getOperand(2)),
2843 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002844 setValue(&I, L);
2845 DAG.setRoot(L.getValue(1));
2846 return 0;
2847}
2848
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002849// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002850const char *
2851SelectionDAGLowering::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002852 SDValue Op1 = getValue(I.getOperand(1));
2853 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002854
Owen Anderson825b72b2009-08-11 20:47:22 +00002855 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00002856 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00002857
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002858 setValue(&I, Result);
2859 return 0;
2860}
Bill Wendling74c37652008-12-09 22:08:41 +00002861
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002862/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2863/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002864void
2865SelectionDAGLowering::visitExp(CallInst &I) {
2866 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002867 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002868
Owen Anderson825b72b2009-08-11 20:47:22 +00002869 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002870 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2871 SDValue Op = getValue(I.getOperand(1));
2872
2873 // Put the exponent in the right bit position for later addition to the
2874 // final result:
2875 //
2876 // #define LOG2OFe 1.4426950f
2877 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002878 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002879 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002880 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002881
2882 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002883 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2884 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002885
2886 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002887 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002888 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002889
2890 if (LimitFloatPrecision <= 6) {
2891 // For floating-point precision of 6:
2892 //
2893 // TwoToFractionalPartOfX =
2894 // 0.997535578f +
2895 // (0.735607626f + 0.252464424f * x) * x;
2896 //
2897 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002898 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002899 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002900 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002901 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002902 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2903 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002904 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002905 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002906
2907 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002908 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002909 TwoToFracPartOfX, IntegerPartOfX);
2910
Owen Anderson825b72b2009-08-11 20:47:22 +00002911 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002912 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2913 // For floating-point precision of 12:
2914 //
2915 // TwoToFractionalPartOfX =
2916 // 0.999892986f +
2917 // (0.696457318f +
2918 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2919 //
2920 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002921 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002922 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002923 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002924 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002925 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2926 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002927 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00002928 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2929 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002930 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002932
2933 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002934 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002935 TwoToFracPartOfX, IntegerPartOfX);
2936
Owen Anderson825b72b2009-08-11 20:47:22 +00002937 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002938 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2939 // For floating-point precision of 18:
2940 //
2941 // TwoToFractionalPartOfX =
2942 // 0.999999982f +
2943 // (0.693148872f +
2944 // (0.240227044f +
2945 // (0.554906021e-1f +
2946 // (0.961591928e-2f +
2947 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
2948 //
2949 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002950 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002951 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002952 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002953 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00002954 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2955 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002956 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2958 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002959 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
2961 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002962 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00002963 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
2964 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002965 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00002966 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
2967 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002968 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00002969 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002970 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002971
2972 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002973 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002974 TwoToFracPartOfX, IntegerPartOfX);
2975
Owen Anderson825b72b2009-08-11 20:47:22 +00002976 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002977 }
2978 } else {
2979 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002980 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002981 getValue(I.getOperand(1)).getValueType(),
2982 getValue(I.getOperand(1)));
2983 }
2984
Dale Johannesen59e577f2008-09-05 18:38:42 +00002985 setValue(&I, result);
2986}
2987
Bill Wendling39150252008-09-09 20:39:27 +00002988/// visitLog - Lower a log intrinsic. Handles the special sequences for
2989/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002990void
2991SelectionDAGLowering::visitLog(CallInst &I) {
2992 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002993 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00002994
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00002996 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2997 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00002999
3000 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003001 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003002 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003003 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003004
3005 // Get the significand and build it into a floating-point number with
3006 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003007 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003008
3009 if (LimitFloatPrecision <= 6) {
3010 // For floating-point precision of 6:
3011 //
3012 // LogofMantissa =
3013 // -1.1609546f +
3014 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003015 //
Bill Wendling39150252008-09-09 20:39:27 +00003016 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003017 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003018 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003019 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003020 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003021 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3022 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003023 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003024
Scott Michelfdc40a02009-02-17 22:15:04 +00003025 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003026 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003027 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3028 // For floating-point precision of 12:
3029 //
3030 // LogOfMantissa =
3031 // -1.7417939f +
3032 // (2.8212026f +
3033 // (-1.4699568f +
3034 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3035 //
3036 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003037 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003038 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003039 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003040 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003041 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3042 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003043 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003044 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3045 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003046 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003047 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3048 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003049 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003050
Scott Michelfdc40a02009-02-17 22:15:04 +00003051 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003052 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003053 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3054 // For floating-point precision of 18:
3055 //
3056 // LogOfMantissa =
3057 // -2.1072184f +
3058 // (4.2372794f +
3059 // (-3.7029485f +
3060 // (2.2781945f +
3061 // (-0.87823314f +
3062 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3063 //
3064 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003065 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003066 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003067 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003068 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003069 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3070 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003071 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003072 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3073 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003074 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3076 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003077 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003078 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3079 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003080 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3082 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003083 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003084
Scott Michelfdc40a02009-02-17 22:15:04 +00003085 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003086 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003087 }
3088 } else {
3089 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003090 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003091 getValue(I.getOperand(1)).getValueType(),
3092 getValue(I.getOperand(1)));
3093 }
3094
Dale Johannesen59e577f2008-09-05 18:38:42 +00003095 setValue(&I, result);
3096}
3097
Bill Wendling3eb59402008-09-09 00:28:24 +00003098/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3099/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003100void
3101SelectionDAGLowering::visitLog2(CallInst &I) {
3102 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003103 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003104
Owen Anderson825b72b2009-08-11 20:47:22 +00003105 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003106 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3107 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003108 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003109
Bill Wendling39150252008-09-09 20:39:27 +00003110 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003111 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003112
3113 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003114 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003115 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003116
Bill Wendling3eb59402008-09-09 00:28:24 +00003117 // Different possible minimax approximations of significand in
3118 // floating-point for various degrees of accuracy over [1,2].
3119 if (LimitFloatPrecision <= 6) {
3120 // For floating-point precision of 6:
3121 //
3122 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3123 //
3124 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003125 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003126 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003128 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003129 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3130 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003131 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003132
Scott Michelfdc40a02009-02-17 22:15:04 +00003133 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003134 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003135 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3136 // For floating-point precision of 12:
3137 //
3138 // Log2ofMantissa =
3139 // -2.51285454f +
3140 // (4.07009056f +
3141 // (-2.12067489f +
3142 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003143 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003144 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003145 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003146 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003148 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003149 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3150 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003151 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3153 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003154 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3156 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003157 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003158
Scott Michelfdc40a02009-02-17 22:15:04 +00003159 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003160 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003161 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3162 // For floating-point precision of 18:
3163 //
3164 // Log2ofMantissa =
3165 // -3.0400495f +
3166 // (6.1129976f +
3167 // (-5.3420409f +
3168 // (3.2865683f +
3169 // (-1.2669343f +
3170 // (0.27515199f -
3171 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3172 //
3173 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003174 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003175 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003176 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003177 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003178 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3179 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003180 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003181 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3182 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003183 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003184 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3185 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003186 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3188 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003189 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003190 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3191 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003192 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003193
Scott Michelfdc40a02009-02-17 22:15:04 +00003194 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003196 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003197 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003198 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003199 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003200 getValue(I.getOperand(1)).getValueType(),
3201 getValue(I.getOperand(1)));
3202 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003203
Dale Johannesen59e577f2008-09-05 18:38:42 +00003204 setValue(&I, result);
3205}
3206
Bill Wendling3eb59402008-09-09 00:28:24 +00003207/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3208/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003209void
3210SelectionDAGLowering::visitLog10(CallInst &I) {
3211 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003212 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003213
Owen Anderson825b72b2009-08-11 20:47:22 +00003214 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003215 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3216 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003218
Bill Wendling39150252008-09-09 20:39:27 +00003219 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003220 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003221 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003222 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003223
3224 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003225 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003226 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003227
3228 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003229 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003230 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003231 // Log10ofMantissa =
3232 // -0.50419619f +
3233 // (0.60948995f - 0.10380950f * x) * x;
3234 //
3235 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003236 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003237 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003238 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003239 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003240 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3241 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003242 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003243
Scott Michelfdc40a02009-02-17 22:15:04 +00003244 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003245 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003246 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3247 // For floating-point precision of 12:
3248 //
3249 // Log10ofMantissa =
3250 // -0.64831180f +
3251 // (0.91751397f +
3252 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3253 //
3254 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003255 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003256 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003258 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3260 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003261 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003262 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3263 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003264 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003265
Scott Michelfdc40a02009-02-17 22:15:04 +00003266 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003267 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003268 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003269 // For floating-point precision of 18:
3270 //
3271 // Log10ofMantissa =
3272 // -0.84299375f +
3273 // (1.5327582f +
3274 // (-1.0688956f +
3275 // (0.49102474f +
3276 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3277 //
3278 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003279 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003280 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003281 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003282 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003283 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3284 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003285 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003286 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3287 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003288 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3290 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003291 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3293 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003294 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003295
Scott Michelfdc40a02009-02-17 22:15:04 +00003296 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003298 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003299 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003300 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003301 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003302 getValue(I.getOperand(1)).getValueType(),
3303 getValue(I.getOperand(1)));
3304 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003305
Dale Johannesen59e577f2008-09-05 18:38:42 +00003306 setValue(&I, result);
3307}
3308
Bill Wendlinge10c8142008-09-09 22:39:21 +00003309/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3310/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003311void
3312SelectionDAGLowering::visitExp2(CallInst &I) {
3313 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003314 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003315
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003317 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3318 SDValue Op = getValue(I.getOperand(1));
3319
Owen Anderson825b72b2009-08-11 20:47:22 +00003320 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003321
3322 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003323 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3324 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003325
3326 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003328 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003329
3330 if (LimitFloatPrecision <= 6) {
3331 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003332 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003333 // TwoToFractionalPartOfX =
3334 // 0.997535578f +
3335 // (0.735607626f + 0.252464424f * x) * x;
3336 //
3337 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003338 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003339 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003341 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3343 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003344 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003346 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003348
Scott Michelfdc40a02009-02-17 22:15:04 +00003349 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003351 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3352 // For floating-point precision of 12:
3353 //
3354 // TwoToFractionalPartOfX =
3355 // 0.999892986f +
3356 // (0.696457318f +
3357 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3358 //
3359 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003361 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003363 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3365 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003366 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3368 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003369 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003371 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003373
Scott Michelfdc40a02009-02-17 22:15:04 +00003374 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003376 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3377 // For floating-point precision of 18:
3378 //
3379 // TwoToFractionalPartOfX =
3380 // 0.999999982f +
3381 // (0.693148872f +
3382 // (0.240227044f +
3383 // (0.554906021e-1f +
3384 // (0.961591928e-2f +
3385 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3386 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003387 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003388 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003389 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003390 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3392 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003393 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003394 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3395 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003396 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3398 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003399 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3401 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003402 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3404 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003405 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003407 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003409
Scott Michelfdc40a02009-02-17 22:15:04 +00003410 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003412 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003413 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003414 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003415 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003416 getValue(I.getOperand(1)).getValueType(),
3417 getValue(I.getOperand(1)));
3418 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003419
Dale Johannesen601d3c02008-09-05 01:48:15 +00003420 setValue(&I, result);
3421}
3422
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003423/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3424/// limited-precision mode with x == 10.0f.
3425void
3426SelectionDAGLowering::visitPow(CallInst &I) {
3427 SDValue result;
3428 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003429 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003430 bool IsExp10 = false;
3431
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 if (getValue(Val).getValueType() == MVT::f32 &&
3433 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003434 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3435 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3436 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3437 APFloat Ten(10.0f);
3438 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3439 }
3440 }
3441 }
3442
3443 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3444 SDValue Op = getValue(I.getOperand(2));
3445
3446 // Put the exponent in the right bit position for later addition to the
3447 // final result:
3448 //
3449 // #define LOG2OF10 3.3219281f
3450 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003451 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003454
3455 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3457 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003458
3459 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003460 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003461 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003462
3463 if (LimitFloatPrecision <= 6) {
3464 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003465 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003466 // twoToFractionalPartOfX =
3467 // 0.997535578f +
3468 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003469 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003470 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003472 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003473 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003474 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003475 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3476 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003477 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003478 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003479 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003480 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003481
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003482 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003484 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3485 // For floating-point precision of 12:
3486 //
3487 // TwoToFractionalPartOfX =
3488 // 0.999892986f +
3489 // (0.696457318f +
3490 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3491 //
3492 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003494 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003496 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3498 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003499 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003500 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3501 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003502 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003503 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003504 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003505 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003506
Scott Michelfdc40a02009-02-17 22:15:04 +00003507 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003509 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3510 // For floating-point precision of 18:
3511 //
3512 // TwoToFractionalPartOfX =
3513 // 0.999999982f +
3514 // (0.693148872f +
3515 // (0.240227044f +
3516 // (0.554906021e-1f +
3517 // (0.961591928e-2f +
3518 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3519 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003521 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003523 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3525 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003526 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3528 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003529 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3531 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003532 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3534 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003535 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3537 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003538 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003540 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003542
Scott Michelfdc40a02009-02-17 22:15:04 +00003543 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003545 }
3546 } else {
3547 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003548 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003549 getValue(I.getOperand(1)).getValueType(),
3550 getValue(I.getOperand(1)),
3551 getValue(I.getOperand(2)));
3552 }
3553
3554 setValue(&I, result);
3555}
3556
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003557/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3558/// we want to emit this as a call to a named external function, return the name
3559/// otherwise lower it and return null.
3560const char *
3561SelectionDAGLowering::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003562 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003563 switch (Intrinsic) {
3564 default:
3565 // By default, turn this into a target intrinsic node.
3566 visitTargetIntrinsic(I, Intrinsic);
3567 return 0;
3568 case Intrinsic::vastart: visitVAStart(I); return 0;
3569 case Intrinsic::vaend: visitVAEnd(I); return 0;
3570 case Intrinsic::vacopy: visitVACopy(I); return 0;
3571 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003572 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003573 getValue(I.getOperand(1))));
3574 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003575 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003576 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003577 getValue(I.getOperand(1))));
3578 return 0;
3579 case Intrinsic::setjmp:
3580 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3581 break;
3582 case Intrinsic::longjmp:
3583 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3584 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003585 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003586 SDValue Op1 = getValue(I.getOperand(1));
3587 SDValue Op2 = getValue(I.getOperand(2));
3588 SDValue Op3 = getValue(I.getOperand(3));
3589 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003590 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003591 I.getOperand(1), 0, I.getOperand(2), 0));
3592 return 0;
3593 }
Chris Lattner824b9582008-11-21 16:42:48 +00003594 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003595 SDValue Op1 = getValue(I.getOperand(1));
3596 SDValue Op2 = getValue(I.getOperand(2));
3597 SDValue Op3 = getValue(I.getOperand(3));
3598 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003599 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003600 I.getOperand(1), 0));
3601 return 0;
3602 }
Chris Lattner824b9582008-11-21 16:42:48 +00003603 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003604 SDValue Op1 = getValue(I.getOperand(1));
3605 SDValue Op2 = getValue(I.getOperand(2));
3606 SDValue Op3 = getValue(I.getOperand(3));
3607 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3608
3609 // If the source and destination are known to not be aliases, we can
3610 // lower memmove as memcpy.
3611 uint64_t Size = -1ULL;
3612 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003613 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003614 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3615 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00003616 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003617 I.getOperand(1), 0, I.getOperand(2), 0));
3618 return 0;
3619 }
3620
Dale Johannesena04b7572009-02-03 23:04:43 +00003621 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003622 I.getOperand(1), 0, I.getOperand(2), 0));
3623 return 0;
3624 }
Devang Patel70d75ca2009-11-12 19:02:56 +00003625 case Intrinsic::dbg_stoppoint:
3626 case Intrinsic::dbg_region_start:
3627 case Intrinsic::dbg_region_end:
3628 case Intrinsic::dbg_func_start:
3629 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003630 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003631 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00003632 if (OptLevel != CodeGenOpt::None)
3633 // FIXME: Variable debug info is not supported here.
3634 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00003635 DwarfWriter *DW = DAG.getDwarfWriter();
3636 if (!DW)
3637 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003638 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3639 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
3640 return 0;
3641
Devang Patelac1ceb32009-10-09 22:42:28 +00003642 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00003643 Value *Address = DI.getAddress();
3644 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
3645 Address = BCI->getOperand(0);
3646 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
3647 // Don't handle byval struct arguments or VLAs, for example.
3648 if (!AI)
3649 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003650 DenseMap<const AllocaInst*, int>::iterator SI =
3651 FuncInfo.StaticAllocaMap.find(AI);
3652 if (SI == FuncInfo.StaticAllocaMap.end())
3653 return 0; // VLAs.
3654 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003655
Devang Patelac1ceb32009-10-09 22:42:28 +00003656 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00003657 if (MMI) {
3658 MetadataContext &TheMetadata =
3659 DI.getParent()->getContext().getMetadata();
3660 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
3661 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
3662 MMI->setVariableDbgInfo(Variable, FI, Dbg);
3663 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003664 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003665 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003666 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003667 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00003668 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003669 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003670 SDValue Ops[1];
3671 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003672 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003673 setValue(&I, Op);
3674 DAG.setRoot(Op.getValue(1));
3675 return 0;
3676 }
3677
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003678 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003679 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003680
Chris Lattner3a5815f2009-09-17 23:54:54 +00003681 if (CurMBB->isLandingPad())
3682 AddCatchInfo(I, MMI, CurMBB);
3683 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003684#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003685 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003686#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003687 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3688 unsigned Reg = TLI.getExceptionSelectorRegister();
3689 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003690 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003691
Chris Lattner3a5815f2009-09-17 23:54:54 +00003692 // Insert the EHSELECTION instruction.
3693 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3694 SDValue Ops[2];
3695 Ops[0] = getValue(I.getOperand(1));
3696 Ops[1] = getRoot();
3697 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
3698
3699 DAG.setRoot(Op.getValue(1));
3700
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003701 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003702 return 0;
3703 }
3704
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003705 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003706 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003707
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003708 if (MMI) {
3709 // Find the type id for the given typeinfo.
3710 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3711
3712 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003713 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003714 } else {
3715 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003716 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003717 }
3718
3719 return 0;
3720 }
3721
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003722 case Intrinsic::eh_return_i32:
3723 case Intrinsic::eh_return_i64:
3724 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003725 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003726 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003728 getControlRoot(),
3729 getValue(I.getOperand(1)),
3730 getValue(I.getOperand(2))));
3731 } else {
3732 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
3733 }
3734
3735 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003736 case Intrinsic::eh_unwind_init:
3737 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3738 MMI->setCallsUnwindInit(true);
3739 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003740
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003741 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003742
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003743 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00003744 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00003745 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
3746 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003747
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003748 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003749 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003750 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003751 TLI.getPointerTy()),
3752 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003753 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003754 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003755 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003756 TLI.getPointerTy(),
3757 DAG.getConstant(0,
3758 TLI.getPointerTy())),
3759 Offset));
3760 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003761 }
Mon P Wang77cdf302008-11-10 20:54:11 +00003762 case Intrinsic::convertff:
3763 case Intrinsic::convertfsi:
3764 case Intrinsic::convertfui:
3765 case Intrinsic::convertsif:
3766 case Intrinsic::convertuif:
3767 case Intrinsic::convertss:
3768 case Intrinsic::convertsu:
3769 case Intrinsic::convertus:
3770 case Intrinsic::convertuu: {
3771 ISD::CvtCode Code = ISD::CVT_INVALID;
3772 switch (Intrinsic) {
3773 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3774 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3775 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3776 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3777 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3778 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3779 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3780 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3781 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3782 }
Owen Andersone50ed302009-08-10 22:56:29 +00003783 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00003784 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00003785 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00003786 DAG.getValueType(DestVT),
3787 DAG.getValueType(getValue(Op1).getValueType()),
3788 getValue(I.getOperand(2)),
3789 getValue(I.getOperand(3)),
3790 Code));
3791 return 0;
3792 }
3793
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003794 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003795 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003796 getValue(I.getOperand(1)).getValueType(),
3797 getValue(I.getOperand(1))));
3798 return 0;
3799 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003800 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003801 getValue(I.getOperand(1)).getValueType(),
3802 getValue(I.getOperand(1)),
3803 getValue(I.getOperand(2))));
3804 return 0;
3805 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003806 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003807 getValue(I.getOperand(1)).getValueType(),
3808 getValue(I.getOperand(1))));
3809 return 0;
3810 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003811 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003812 getValue(I.getOperand(1)).getValueType(),
3813 getValue(I.getOperand(1))));
3814 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003815 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003816 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003817 return 0;
3818 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003819 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003820 return 0;
3821 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003822 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003823 return 0;
3824 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003825 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003826 return 0;
3827 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003828 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003829 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003830 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003831 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003832 return 0;
3833 case Intrinsic::pcmarker: {
3834 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003835 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003836 return 0;
3837 }
3838 case Intrinsic::readcyclecounter: {
3839 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003840 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003841 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00003842 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003843 setValue(&I, Tmp);
3844 DAG.setRoot(Tmp.getValue(1));
3845 return 0;
3846 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003847 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003848 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003849 getValue(I.getOperand(1)).getValueType(),
3850 getValue(I.getOperand(1))));
3851 return 0;
3852 case Intrinsic::cttz: {
3853 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003854 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003855 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003856 setValue(&I, result);
3857 return 0;
3858 }
3859 case Intrinsic::ctlz: {
3860 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003861 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003862 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003863 setValue(&I, result);
3864 return 0;
3865 }
3866 case Intrinsic::ctpop: {
3867 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003868 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003869 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003870 setValue(&I, result);
3871 return 0;
3872 }
3873 case Intrinsic::stacksave: {
3874 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003875 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003876 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003877 setValue(&I, Tmp);
3878 DAG.setRoot(Tmp.getValue(1));
3879 return 0;
3880 }
3881 case Intrinsic::stackrestore: {
3882 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003883 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003884 return 0;
3885 }
Bill Wendling57344502008-11-18 11:01:33 +00003886 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00003887 // Emit code into the DAG to store the stack guard onto the stack.
3888 MachineFunction &MF = DAG.getMachineFunction();
3889 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003890 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00003891
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00003892 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
3893 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00003894
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00003895 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00003896 MFI->setStackProtectorIndex(FI);
3897
3898 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3899
3900 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003901 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00003902 PseudoSourceValue::getFixedStack(FI),
3903 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00003904 setValue(&I, Result);
3905 DAG.setRoot(Result);
3906 return 0;
3907 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00003908 case Intrinsic::objectsize: {
3909 // If we don't know by now, we're never going to know.
3910 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
3911
3912 assert(CI && "Non-constant type in __builtin_object_size?");
3913
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00003914 SDValue Arg = getValue(I.getOperand(0));
3915 EVT Ty = Arg.getValueType();
3916
Eric Christopher7b5e6172009-10-27 00:52:25 +00003917 if (CI->getZExtValue() < 2)
Mike Stump70e5e682009-11-09 22:28:21 +00003918 setValue(&I, DAG.getConstant(-1ULL, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00003919 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00003920 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00003921 return 0;
3922 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003923 case Intrinsic::var_annotation:
3924 // Discard annotate attributes
3925 return 0;
3926
3927 case Intrinsic::init_trampoline: {
3928 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
3929
3930 SDValue Ops[6];
3931 Ops[0] = getRoot();
3932 Ops[1] = getValue(I.getOperand(1));
3933 Ops[2] = getValue(I.getOperand(2));
3934 Ops[3] = getValue(I.getOperand(3));
3935 Ops[4] = DAG.getSrcValue(I.getOperand(1));
3936 Ops[5] = DAG.getSrcValue(F);
3937
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003938 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003939 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00003940 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003941
3942 setValue(&I, Tmp);
3943 DAG.setRoot(Tmp.getValue(1));
3944 return 0;
3945 }
3946
3947 case Intrinsic::gcroot:
3948 if (GFI) {
3949 Value *Alloca = I.getOperand(1);
3950 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003951
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003952 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
3953 GFI->addStackRoot(FI->getIndex(), TypeMap);
3954 }
3955 return 0;
3956
3957 case Intrinsic::gcread:
3958 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00003959 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003960 return 0;
3961
3962 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00003963 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003964 return 0;
3965 }
3966
3967 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00003968 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003969 return 0;
3970 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00003971
Bill Wendlingef375462008-11-21 02:38:44 +00003972 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00003973 return implVisitAluOverflow(I, ISD::UADDO);
3974 case Intrinsic::sadd_with_overflow:
3975 return implVisitAluOverflow(I, ISD::SADDO);
3976 case Intrinsic::usub_with_overflow:
3977 return implVisitAluOverflow(I, ISD::USUBO);
3978 case Intrinsic::ssub_with_overflow:
3979 return implVisitAluOverflow(I, ISD::SSUBO);
3980 case Intrinsic::umul_with_overflow:
3981 return implVisitAluOverflow(I, ISD::UMULO);
3982 case Intrinsic::smul_with_overflow:
3983 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00003984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003985 case Intrinsic::prefetch: {
3986 SDValue Ops[4];
3987 Ops[0] = getRoot();
3988 Ops[1] = getValue(I.getOperand(1));
3989 Ops[2] = getValue(I.getOperand(2));
3990 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003991 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003992 return 0;
3993 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003994
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003995 case Intrinsic::memory_barrier: {
3996 SDValue Ops[6];
3997 Ops[0] = getRoot();
3998 for (int x = 1; x < 6; ++x)
3999 Ops[x] = getValue(I.getOperand(x));
4000
Owen Anderson825b72b2009-08-11 20:47:22 +00004001 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004002 return 0;
4003 }
4004 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004005 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004006 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004007 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004008 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4009 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004010 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004011 getValue(I.getOperand(2)),
4012 getValue(I.getOperand(3)),
4013 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004014 setValue(&I, L);
4015 DAG.setRoot(L.getValue(1));
4016 return 0;
4017 }
4018 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004019 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004020 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004021 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004022 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004023 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004024 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004025 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004026 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004027 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004028 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004029 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004030 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004031 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004032 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004033 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004034 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004035 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004036 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004037 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004038 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004039 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004040
4041 case Intrinsic::invariant_start:
4042 case Intrinsic::lifetime_start:
4043 // Discard region information.
4044 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
4045 return 0;
4046 case Intrinsic::invariant_end:
4047 case Intrinsic::lifetime_end:
4048 // Discard region information.
4049 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004050 }
4051}
4052
Dan Gohman98ca4f22009-08-05 01:29:28 +00004053/// Test if the given instruction is in a position to be optimized
4054/// with a tail-call. This roughly means that it's in a block with
4055/// a return and there's nothing that needs to be scheduled
4056/// between it and the return.
4057///
4058/// This function only tests target-independent requirements.
4059/// For target-dependent requirements, a target should override
4060/// TargetLowering::IsEligibleForTailCallOptimization.
4061///
4062static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004063isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004064 const TargetLowering &TLI) {
4065 const BasicBlock *ExitBB = I->getParent();
4066 const TerminatorInst *Term = ExitBB->getTerminator();
4067 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4068 const Function *F = ExitBB->getParent();
4069
4070 // The block must end in a return statement or an unreachable.
4071 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4072
4073 // If I will have a chain, make sure no other instruction that will have a
4074 // chain interposes between I and the return.
4075 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4076 !I->isSafeToSpeculativelyExecute())
4077 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4078 --BBI) {
4079 if (&*BBI == I)
4080 break;
4081 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4082 !BBI->isSafeToSpeculativelyExecute())
4083 return false;
4084 }
4085
4086 // If the block ends with a void return or unreachable, it doesn't matter
4087 // what the call's return type is.
4088 if (!Ret || Ret->getNumOperands() == 0) return true;
4089
Dan Gohmaned9bab32009-11-14 02:06:30 +00004090 // If the return value is undef, it doesn't matter what the call's
4091 // return type is.
4092 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4093
Dan Gohman98ca4f22009-08-05 01:29:28 +00004094 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004095 // the return. Ignore noalias because it doesn't affect the call sequence.
4096 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4097 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004098 return false;
4099
4100 // Otherwise, make sure the unmodified return value of I is the return value.
4101 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4102 U = dyn_cast<Instruction>(U->getOperand(0))) {
4103 if (!U)
4104 return false;
4105 if (!U->hasOneUse())
4106 return false;
4107 if (U == I)
4108 break;
4109 // Check for a truly no-op truncate.
4110 if (isa<TruncInst>(U) &&
4111 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4112 continue;
4113 // Check for a truly no-op bitcast.
4114 if (isa<BitCastInst>(U) &&
4115 (U->getOperand(0)->getType() == U->getType() ||
4116 (isa<PointerType>(U->getOperand(0)->getType()) &&
4117 isa<PointerType>(U->getType()))))
4118 continue;
4119 // Otherwise it's not a true no-op.
4120 return false;
4121 }
4122
4123 return true;
4124}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004125
4126void SelectionDAGLowering::LowerCallTo(CallSite CS, SDValue Callee,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004127 bool isTailCall,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004128 MachineBasicBlock *LandingPad) {
4129 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4130 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004131 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004132 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4133 unsigned BeginLabel = 0, EndLabel = 0;
4134
4135 TargetLowering::ArgListTy Args;
4136 TargetLowering::ArgListEntry Entry;
4137 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004138
4139 // Check whether the function can return without sret-demotion.
4140 SmallVector<EVT, 4> OutVTs;
4141 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4142 SmallVector<uint64_t, 4> Offsets;
4143 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4144 OutVTs, OutsFlags, TLI, &Offsets);
4145
4146
4147 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4148 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4149
4150 SDValue DemoteStackSlot;
4151
4152 if (!CanLowerReturn) {
4153 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4154 FTy->getReturnType());
4155 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4156 FTy->getReturnType());
4157 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004158 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004159 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4160
4161 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4162 Entry.Node = DemoteStackSlot;
4163 Entry.Ty = StackSlotPtrType;
4164 Entry.isSExt = false;
4165 Entry.isZExt = false;
4166 Entry.isInReg = false;
4167 Entry.isSRet = true;
4168 Entry.isNest = false;
4169 Entry.isByVal = false;
4170 Entry.Alignment = Align;
4171 Args.push_back(Entry);
4172 RetTy = Type::getVoidTy(FTy->getContext());
4173 }
4174
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004175 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004176 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004177 SDValue ArgNode = getValue(*i);
4178 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4179
4180 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004181 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4182 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4183 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4184 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4185 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4186 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004187 Entry.Alignment = CS.getParamAlignment(attrInd);
4188 Args.push_back(Entry);
4189 }
4190
4191 if (LandingPad && MMI) {
4192 // Insert a label before the invoke call to mark the try range. This can be
4193 // used to detect deletion of the invoke via the MachineModuleInfo.
4194 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004196 // Both PendingLoads and PendingExports must be flushed here;
4197 // this call might not return.
4198 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004199 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4200 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004201 }
4202
Dan Gohman98ca4f22009-08-05 01:29:28 +00004203 // Check if target-independent constraints permit a tail call here.
4204 // Target-dependent constraints are checked within TLI.LowerCallTo.
4205 if (isTailCall &&
4206 !isInTailCallPosition(CS.getInstruction(),
4207 CS.getAttributes().getRetAttributes(),
4208 TLI))
4209 isTailCall = false;
4210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004211 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004212 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004213 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004214 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004215 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004216 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004217 isTailCall,
4218 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004219 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004220 assert((isTailCall || Result.second.getNode()) &&
4221 "Non-null chain expected with non-tail call!");
4222 assert((Result.second.getNode() || !Result.first.getNode()) &&
4223 "Null value expected with tail call!");
4224 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004225 setValue(CS.getInstruction(), Result.first);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004226 else if (!CanLowerReturn && Result.second.getNode()) {
4227 // The instruction result is the result of loading from the
4228 // hidden sret parameter.
4229 SmallVector<EVT, 1> PVTs;
4230 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4231
4232 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4233 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4234 EVT PtrVT = PVTs[0];
4235 unsigned NumValues = OutVTs.size();
4236 SmallVector<SDValue, 4> Values(NumValues);
4237 SmallVector<SDValue, 4> Chains(NumValues);
4238
4239 for (unsigned i = 0; i < NumValues; ++i) {
4240 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
4241 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, DemoteStackSlot,
4242 DAG.getConstant(Offsets[i], PtrVT)),
4243 NULL, Offsets[i], false, 1);
4244 Values[i] = L;
4245 Chains[i] = L.getValue(1);
4246 }
4247 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4248 MVT::Other, &Chains[0], NumValues);
4249 PendingLoads.push_back(Chain);
4250
4251 setValue(CS.getInstruction(), DAG.getNode(ISD::MERGE_VALUES,
4252 getCurDebugLoc(), DAG.getVTList(&OutVTs[0], NumValues),
4253 &Values[0], NumValues));
4254 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00004255 // As a special case, a null chain means that a tail call has
4256 // been emitted and the DAG root is already updated.
4257 if (Result.second.getNode())
4258 DAG.setRoot(Result.second);
4259 else
4260 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004261
4262 if (LandingPad && MMI) {
4263 // Insert a label at the end of the invoke call to mark the try range. This
4264 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4265 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004266 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4267 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004268
4269 // Inform MachineModuleInfo of range.
4270 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4271 }
4272}
4273
4274
4275void SelectionDAGLowering::visitCall(CallInst &I) {
4276 const char *RenameFn = 0;
4277 if (Function *F = I.getCalledFunction()) {
4278 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004279 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4280 if (II) {
4281 if (unsigned IID = II->getIntrinsicID(F)) {
4282 RenameFn = visitIntrinsicCall(I, IID);
4283 if (!RenameFn)
4284 return;
4285 }
4286 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004287 if (unsigned IID = F->getIntrinsicID()) {
4288 RenameFn = visitIntrinsicCall(I, IID);
4289 if (!RenameFn)
4290 return;
4291 }
4292 }
4293
4294 // Check for well-known libc/libm calls. If the function is internal, it
4295 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004296 if (!F->hasLocalLinkage() && F->hasName()) {
4297 StringRef Name = F->getName();
4298 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004299 if (I.getNumOperands() == 3 && // Basic sanity checks.
4300 I.getOperand(1)->getType()->isFloatingPoint() &&
4301 I.getType() == I.getOperand(1)->getType() &&
4302 I.getType() == I.getOperand(2)->getType()) {
4303 SDValue LHS = getValue(I.getOperand(1));
4304 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004305 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004306 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004307 return;
4308 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004309 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004310 if (I.getNumOperands() == 2 && // Basic sanity checks.
4311 I.getOperand(1)->getType()->isFloatingPoint() &&
4312 I.getType() == I.getOperand(1)->getType()) {
4313 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004314 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004315 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004316 return;
4317 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004318 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004319 if (I.getNumOperands() == 2 && // Basic sanity checks.
4320 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004321 I.getType() == I.getOperand(1)->getType() &&
4322 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004323 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004324 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004325 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004326 return;
4327 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004328 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004329 if (I.getNumOperands() == 2 && // Basic sanity checks.
4330 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004331 I.getType() == I.getOperand(1)->getType() &&
4332 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004333 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004334 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004335 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004336 return;
4337 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004338 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4339 if (I.getNumOperands() == 2 && // Basic sanity checks.
4340 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004341 I.getType() == I.getOperand(1)->getType() &&
4342 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004343 SDValue Tmp = getValue(I.getOperand(1));
4344 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4345 Tmp.getValueType(), Tmp));
4346 return;
4347 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004348 }
4349 }
4350 } else if (isa<InlineAsm>(I.getOperand(0))) {
4351 visitInlineAsm(&I);
4352 return;
4353 }
4354
4355 SDValue Callee;
4356 if (!RenameFn)
4357 Callee = getValue(I.getOperand(0));
4358 else
Bill Wendling056292f2008-09-16 21:48:12 +00004359 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004360
Dan Gohman98ca4f22009-08-05 01:29:28 +00004361 // Check if we can potentially perform a tail call. More detailed
4362 // checking is be done within LowerCallTo, after more information
4363 // about the call is known.
4364 bool isTailCall = PerformTailCallOpt && I.isTailCall();
4365
4366 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004367}
4368
4369
4370/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004371/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004372/// Chain/Flag as the input and updates them for the output Chain/Flag.
4373/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004374SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004375 SDValue &Chain,
4376 SDValue *Flag) const {
4377 // Assemble the legal parts into the final values.
4378 SmallVector<SDValue, 4> Values(ValueVTs.size());
4379 SmallVector<SDValue, 8> Parts;
4380 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4381 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004382 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004383 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004384 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004385
4386 Parts.resize(NumRegs);
4387 for (unsigned i = 0; i != NumRegs; ++i) {
4388 SDValue P;
4389 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004390 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004391 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004392 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004393 *Flag = P.getValue(2);
4394 }
4395 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004396
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397 // If the source register was virtual and if we know something about it,
4398 // add an assert node.
4399 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4400 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4401 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4402 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4403 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4404 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004405
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406 unsigned RegSize = RegisterVT.getSizeInBits();
4407 unsigned NumSignBits = LOI.NumSignBits;
4408 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004409
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004410 // FIXME: We capture more information than the dag can represent. For
4411 // now, just use the tightest assertzext/assertsext possible.
4412 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004413 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004414 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004415 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004416 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004417 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004418 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004419 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004420 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004421 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004422 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004423 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004424 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004425 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004426 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004427 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004428 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004429 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004430
Owen Anderson825b72b2009-08-11 20:47:22 +00004431 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004432 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004433 RegisterVT, P, DAG.getValueType(FromVT));
4434
4435 }
4436 }
4437 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004438
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004439 Parts[i] = P;
4440 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004441
Scott Michelfdc40a02009-02-17 22:15:04 +00004442 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004443 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004444 Part += NumRegs;
4445 Parts.clear();
4446 }
4447
Dale Johannesen66978ee2009-01-31 02:22:37 +00004448 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004449 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4450 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004451}
4452
4453/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004454/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004455/// Chain/Flag as the input and updates them for the output Chain/Flag.
4456/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004457void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004458 SDValue &Chain, SDValue *Flag) const {
4459 // Get the list of the values's legal parts.
4460 unsigned NumRegs = Regs.size();
4461 SmallVector<SDValue, 8> Parts(NumRegs);
4462 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004463 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004464 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004465 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004466
Dale Johannesen66978ee2009-01-31 02:22:37 +00004467 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004468 &Parts[Part], NumParts, RegisterVT);
4469 Part += NumParts;
4470 }
4471
4472 // Copy the parts into the registers.
4473 SmallVector<SDValue, 8> Chains(NumRegs);
4474 for (unsigned i = 0; i != NumRegs; ++i) {
4475 SDValue Part;
4476 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004477 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004478 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004479 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004480 *Flag = Part.getValue(1);
4481 }
4482 Chains[i] = Part.getValue(0);
4483 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004484
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004485 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004486 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004487 // flagged to it. That is the CopyToReg nodes and the user are considered
4488 // a single scheduling unit. If we create a TokenFactor and return it as
4489 // chain, then the TokenFactor is both a predecessor (operand) of the
4490 // user as well as a successor (the TF operands are flagged to the user).
4491 // c1, f1 = CopyToReg
4492 // c2, f2 = CopyToReg
4493 // c3 = TokenFactor c1, c2
4494 // ...
4495 // = op c3, ..., f2
4496 Chain = Chains[NumRegs-1];
4497 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004498 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004499}
4500
4501/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004502/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004503/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00004504void RegsForValue::AddInlineAsmOperands(unsigned Code,
4505 bool HasMatching,unsigned MatchingIdx,
4506 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004507 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004508 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00004509 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
4510 unsigned Flag = Code | (Regs.size() << 3);
4511 if (HasMatching)
4512 Flag |= 0x80000000 | (MatchingIdx << 16);
4513 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004514 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004515 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004516 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004517 for (unsigned i = 0; i != NumRegs; ++i) {
4518 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004519 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004520 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004521 }
4522}
4523
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004524/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004525/// i.e. it isn't a stack pointer or some other special register, return the
4526/// register class for the register. Otherwise, return null.
4527static const TargetRegisterClass *
4528isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4529 const TargetLowering &TLI,
4530 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004531 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004532 const TargetRegisterClass *FoundRC = 0;
4533 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4534 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004535 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004536
4537 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004538 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004539 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4540 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4541 I != E; ++I) {
4542 if (TLI.isTypeLegal(*I)) {
4543 // If we have already found this register in a different register class,
4544 // choose the one with the largest VT specified. For example, on
4545 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004546 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004547 ThisVT = *I;
4548 break;
4549 }
4550 }
4551 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004552
Owen Anderson825b72b2009-08-11 20:47:22 +00004553 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004554
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004555 // NOTE: This isn't ideal. In particular, this might allocate the
4556 // frame pointer in functions that need it (due to them not being taken
4557 // out of allocation, because a variable sized allocation hasn't been seen
4558 // yet). This is a slight code pessimization, but should still work.
4559 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4560 E = RC->allocation_order_end(MF); I != E; ++I)
4561 if (*I == Reg) {
4562 // We found a matching register class. Keep looking at others in case
4563 // we find one with larger registers that this physreg is also in.
4564 FoundRC = RC;
4565 FoundVT = ThisVT;
4566 break;
4567 }
4568 }
4569 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004570}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004571
4572
4573namespace llvm {
4574/// AsmOperandInfo - This contains information for each constraint that we are
4575/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004576class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004577 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004578public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004579 /// CallOperand - If this is the result output operand or a clobber
4580 /// this is null, otherwise it is the incoming operand to the CallInst.
4581 /// This gets modified as the asm is processed.
4582 SDValue CallOperand;
4583
4584 /// AssignedRegs - If this is a register or register class operand, this
4585 /// contains the set of register corresponding to the operand.
4586 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004587
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4589 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4590 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004591
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004592 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4593 /// busy in OutputRegs/InputRegs.
4594 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004595 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596 std::set<unsigned> &InputRegs,
4597 const TargetRegisterInfo &TRI) const {
4598 if (isOutReg) {
4599 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4600 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4601 }
4602 if (isInReg) {
4603 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4604 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4605 }
4606 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004607
Owen Andersone50ed302009-08-10 22:56:29 +00004608 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004609 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004610 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00004611 EVT getCallOperandValEVT(LLVMContext &Context,
4612 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004613 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004614 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004615
Chris Lattner81249c92008-10-17 17:05:25 +00004616 if (isa<BasicBlock>(CallOperandVal))
4617 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004618
Chris Lattner81249c92008-10-17 17:05:25 +00004619 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004620
Chris Lattner81249c92008-10-17 17:05:25 +00004621 // If this is an indirect operand, the operand is a pointer to the
4622 // accessed type.
4623 if (isIndirect)
4624 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004625
Chris Lattner81249c92008-10-17 17:05:25 +00004626 // If OpTy is not a single value, it may be a struct/union that we
4627 // can tile with integers.
4628 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4629 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4630 switch (BitSize) {
4631 default: break;
4632 case 1:
4633 case 8:
4634 case 16:
4635 case 32:
4636 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004637 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004638 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004639 break;
4640 }
4641 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004642
Chris Lattner81249c92008-10-17 17:05:25 +00004643 return TLI.getValueType(OpTy, true);
4644 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004645
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004646private:
4647 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4648 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004649 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650 const TargetRegisterInfo &TRI) {
4651 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4652 Regs.insert(Reg);
4653 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4654 for (; *Aliases; ++Aliases)
4655 Regs.insert(*Aliases);
4656 }
4657};
4658} // end llvm namespace.
4659
4660
4661/// GetRegistersForValue - Assign registers (virtual or physical) for the
4662/// specified operand. We prefer to assign virtual registers, to allow the
4663/// register allocator handle the assignment process. However, if the asm uses
4664/// features that we can't model on machineinstrs, we have SDISel do the
4665/// allocation. This produces generally horrible, but correct, code.
4666///
4667/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668/// Input and OutputRegs are the set of already allocated physical registers.
4669///
4670void SelectionDAGLowering::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004671GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004672 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004673 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004674 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004675
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 // Compute whether this value requires an input register, an output register,
4677 // or both.
4678 bool isOutReg = false;
4679 bool isInReg = false;
4680 switch (OpInfo.Type) {
4681 case InlineAsm::isOutput:
4682 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004683
4684 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004685 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004686 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004687 break;
4688 case InlineAsm::isInput:
4689 isInReg = true;
4690 isOutReg = false;
4691 break;
4692 case InlineAsm::isClobber:
4693 isOutReg = true;
4694 isInReg = true;
4695 break;
4696 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004697
4698
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004699 MachineFunction &MF = DAG.getMachineFunction();
4700 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004701
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004702 // If this is a constraint for a single physreg, or a constraint for a
4703 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004704 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004705 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4706 OpInfo.ConstraintVT);
4707
4708 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004709 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004710 // If this is a FP input in an integer register (or visa versa) insert a bit
4711 // cast of the input value. More generally, handle any case where the input
4712 // value disagrees with the register class we plan to stick this in.
4713 if (OpInfo.Type == InlineAsm::isInput &&
4714 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004715 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004716 // types are identical size, use a bitcast to convert (e.g. two differing
4717 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004718 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004719 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004720 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004721 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004722 OpInfo.ConstraintVT = RegVT;
4723 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4724 // If the input is a FP value and we want it in FP registers, do a
4725 // bitcast to the corresponding integer type. This turns an f64 value
4726 // into i64, which can be passed with two i32 values on a 32-bit
4727 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00004728 RegVT = EVT::getIntegerVT(Context,
4729 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004730 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004731 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004732 OpInfo.ConstraintVT = RegVT;
4733 }
4734 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004735
Owen Anderson23b9b192009-08-12 00:36:31 +00004736 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004737 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004738
Owen Andersone50ed302009-08-10 22:56:29 +00004739 EVT RegVT;
4740 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004741
4742 // If this is a constraint for a specific physical register, like {r17},
4743 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004744 if (unsigned AssignedReg = PhysReg.first) {
4745 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00004746 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004747 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004748
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004749 // Get the actual register value type. This is important, because the user
4750 // may have asked for (e.g.) the AX register in i32 type. We need to
4751 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004752 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004753
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004754 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004755 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004756
4757 // If this is an expanded reference, add the rest of the regs to Regs.
4758 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004759 TargetRegisterClass::iterator I = RC->begin();
4760 for (; *I != AssignedReg; ++I)
4761 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004762
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004763 // Already added the first reg.
4764 --NumRegs; ++I;
4765 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004766 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004767 Regs.push_back(*I);
4768 }
4769 }
4770 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4771 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4772 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4773 return;
4774 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004775
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004776 // Otherwise, if this was a reference to an LLVM register class, create vregs
4777 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00004778 if (const TargetRegisterClass *RC = PhysReg.second) {
4779 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00004780 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00004781 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004782
Evan Chengfb112882009-03-23 08:01:15 +00004783 // Create the appropriate number of virtual registers.
4784 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4785 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00004786 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004787
Evan Chengfb112882009-03-23 08:01:15 +00004788 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4789 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00004791
4792 // This is a reference to a register class that doesn't directly correspond
4793 // to an LLVM register class. Allocate NumRegs consecutive, available,
4794 // registers from the class.
4795 std::vector<unsigned> RegClassRegs
4796 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
4797 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004798
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004799 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4800 unsigned NumAllocated = 0;
4801 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
4802 unsigned Reg = RegClassRegs[i];
4803 // See if this register is available.
4804 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
4805 (isInReg && InputRegs.count(Reg))) { // Already used.
4806 // Make sure we find consecutive registers.
4807 NumAllocated = 0;
4808 continue;
4809 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004810
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004811 // Check to see if this register is allocatable (i.e. don't give out the
4812 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00004813 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
4814 if (!RC) { // Couldn't allocate this register.
4815 // Reset NumAllocated to make sure we return consecutive registers.
4816 NumAllocated = 0;
4817 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004818 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004820 // Okay, this register is good, we can use it.
4821 ++NumAllocated;
4822
4823 // If we allocated enough consecutive registers, succeed.
4824 if (NumAllocated == NumRegs) {
4825 unsigned RegStart = (i-NumAllocated)+1;
4826 unsigned RegEnd = i+1;
4827 // Mark all of the allocated registers used.
4828 for (unsigned i = RegStart; i != RegEnd; ++i)
4829 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004830
4831 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004832 OpInfo.ConstraintVT);
4833 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4834 return;
4835 }
4836 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004837
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004838 // Otherwise, we couldn't allocate enough registers for this.
4839}
4840
Evan Chengda43bcf2008-09-24 00:05:32 +00004841/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
4842/// processed uses a memory 'm' constraint.
4843static bool
4844hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00004845 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00004846 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
4847 InlineAsm::ConstraintInfo &CI = CInfos[i];
4848 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
4849 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
4850 if (CType == TargetLowering::C_Memory)
4851 return true;
4852 }
Chris Lattner6c147292009-04-30 00:48:50 +00004853
4854 // Indirect operand accesses access memory.
4855 if (CI.isIndirect)
4856 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00004857 }
4858
4859 return false;
4860}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004861
4862/// visitInlineAsm - Handle a call to an InlineAsm object.
4863///
4864void SelectionDAGLowering::visitInlineAsm(CallSite CS) {
4865 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
4866
4867 /// ConstraintOperands - Information about all of the constraints.
4868 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004869
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004870 std::set<unsigned> OutputRegs, InputRegs;
4871
4872 // Do a prepass over the constraints, canonicalizing them, and building up the
4873 // ConstraintOperands list.
4874 std::vector<InlineAsm::ConstraintInfo>
4875 ConstraintInfos = IA->ParseConstraints();
4876
Evan Chengda43bcf2008-09-24 00:05:32 +00004877 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00004878
4879 SDValue Chain, Flag;
4880
4881 // We won't need to flush pending loads if this asm doesn't touch
4882 // memory and is nonvolatile.
4883 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00004884 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00004885 else
4886 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004887
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004888 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
4889 unsigned ResNo = 0; // ResNo - The result number of the next output.
4890 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4891 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
4892 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004893
Owen Anderson825b72b2009-08-11 20:47:22 +00004894 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004895
4896 // Compute the value type for each operand.
4897 switch (OpInfo.Type) {
4898 case InlineAsm::isOutput:
4899 // Indirect outputs just consume an argument.
4900 if (OpInfo.isIndirect) {
4901 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4902 break;
4903 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004905 // The return value of the call is this value. As such, there is no
4906 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00004907 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
4908 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004909 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
4910 OpVT = TLI.getValueType(STy->getElementType(ResNo));
4911 } else {
4912 assert(ResNo == 0 && "Asm only has one result!");
4913 OpVT = TLI.getValueType(CS.getType());
4914 }
4915 ++ResNo;
4916 break;
4917 case InlineAsm::isInput:
4918 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4919 break;
4920 case InlineAsm::isClobber:
4921 // Nothing to do.
4922 break;
4923 }
4924
4925 // If this is an input or an indirect output, process the call argument.
4926 // BasicBlocks are labels, currently appearing only in asm's.
4927 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00004928 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00004929 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
4930
Chris Lattner81249c92008-10-17 17:05:25 +00004931 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004932 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00004933 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004934 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004936
Owen Anderson1d0be152009-08-13 21:58:54 +00004937 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004938 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004939
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004940 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004941 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004942
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004943 // Second pass over the constraints: compute which constraint option to use
4944 // and assign registers to constraints that want a specific physreg.
4945 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4946 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004947
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004948 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00004949 // matching input. If their types mismatch, e.g. one is an integer, the
4950 // other is floating point, or their sizes are different, flag it as an
4951 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004952 if (OpInfo.hasMatchingInput()) {
4953 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
4954 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00004955 if ((OpInfo.ConstraintVT.isInteger() !=
4956 Input.ConstraintVT.isInteger()) ||
4957 (OpInfo.ConstraintVT.getSizeInBits() !=
4958 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00004959 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00004960 " with a matching output constraint of incompatible"
4961 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00004962 }
4963 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004964 }
4965 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004966
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004967 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00004968 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004969
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004970 // If this is a memory input, and if the operand is not indirect, do what we
4971 // need to to provide an address for the memory input.
4972 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
4973 !OpInfo.isIndirect) {
4974 assert(OpInfo.Type == InlineAsm::isInput &&
4975 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004976
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004977 // Memory operands really want the address of the value. If we don't have
4978 // an indirect input, put it in the constpool if we can, otherwise spill
4979 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004980
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004981 // If the operand is a float, integer, or vector constant, spill to a
4982 // constant pool entry to get its address.
4983 Value *OpVal = OpInfo.CallOperandVal;
4984 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
4985 isa<ConstantVector>(OpVal)) {
4986 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
4987 TLI.getPointerTy());
4988 } else {
4989 // Otherwise, create a stack slot and emit a store to it before the
4990 // asm.
4991 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00004992 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004993 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
4994 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004995 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004996 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004997 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004998 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004999 OpInfo.CallOperand = StackSlot;
5000 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005001
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005002 // There is no longer a Value* corresponding to this operand.
5003 OpInfo.CallOperandVal = 0;
5004 // It is now an indirect operand.
5005 OpInfo.isIndirect = true;
5006 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005007
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005008 // If this constraint is for a specific register, allocate it before
5009 // anything else.
5010 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005011 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005012 }
5013 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005014
5015
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005016 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005017 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005018 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5019 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005021 // C_Register operands have already been allocated, Other/Memory don't need
5022 // to be.
5023 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005024 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005025 }
5026
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005027 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5028 std::vector<SDValue> AsmNodeOperands;
5029 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5030 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005031 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005032
5033
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005034 // Loop over all of the inputs, copying the operand values into the
5035 // appropriate registers and processing the output regs.
5036 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005037
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005038 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5039 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005041 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5042 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5043
5044 switch (OpInfo.Type) {
5045 case InlineAsm::isOutput: {
5046 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5047 OpInfo.ConstraintType != TargetLowering::C_Register) {
5048 // Memory output, or 'other' output (e.g. 'X' constraint).
5049 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5050
5051 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005052 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5053 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005054 TLI.getPointerTy()));
5055 AsmNodeOperands.push_back(OpInfo.CallOperand);
5056 break;
5057 }
5058
5059 // Otherwise, this is a register or register class output.
5060
5061 // Copy the output from the appropriate register. Find a register that
5062 // we can use.
5063 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005064 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005065 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005066 }
5067
5068 // If this is an indirect operand, store through the pointer after the
5069 // asm.
5070 if (OpInfo.isIndirect) {
5071 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5072 OpInfo.CallOperandVal));
5073 } else {
5074 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005075 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5076 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005077 // Concatenate this output onto the outputs list.
5078 RetValRegs.append(OpInfo.AssignedRegs);
5079 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 // Add information to the INLINEASM node to know that this register is
5082 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005083 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5084 6 /* EARLYCLOBBER REGDEF */ :
5085 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005086 false,
5087 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005088 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005089 break;
5090 }
5091 case InlineAsm::isInput: {
5092 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005093
Chris Lattner6bdcda32008-10-17 16:47:46 +00005094 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005095 // If this is required to match an output register we have already set,
5096 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005097 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005099 // Scan until we find the definition we already emitted of this operand.
5100 // When we find it, create a RegsForValue operand.
5101 unsigned CurOp = 2; // The first operand.
5102 for (; OperandNo; --OperandNo) {
5103 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005104 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005105 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005106 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5107 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5108 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005109 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005110 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005111 }
5112
Evan Cheng697cbbf2009-03-20 18:03:34 +00005113 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005114 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005115 if ((OpFlag & 7) == 2 /*REGDEF*/
5116 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5117 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005118 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005119 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005120 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005121 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005122 RegsForValue MatchedRegs;
5123 MatchedRegs.TLI = &TLI;
5124 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005125 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005126 MatchedRegs.RegVTs.push_back(RegVT);
5127 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005128 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005129 i != e; ++i)
5130 MatchedRegs.Regs.
5131 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005132
5133 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005134 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5135 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005136 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5137 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005138 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005139 break;
5140 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005141 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5142 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5143 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005144 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005145 // See InlineAsm.h isUseOperandTiedToDef.
5146 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005147 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005148 TLI.getPointerTy()));
5149 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5150 break;
5151 }
5152 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005154 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005155 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005158 std::vector<SDValue> Ops;
5159 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005160 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005161 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005162 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005163 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005164 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005166 // Add information to the INLINEASM node to know about this input.
5167 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005168 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005169 TLI.getPointerTy()));
5170 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5171 break;
5172 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5173 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5174 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5175 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005176
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005177 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005178 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5179 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005180 TLI.getPointerTy()));
5181 AsmNodeOperands.push_back(InOperandVal);
5182 break;
5183 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005185 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5186 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5187 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005188 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005189 "Don't know how to handle indirect register inputs yet!");
5190
5191 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005192 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005193 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005194 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005195 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005196
Dale Johannesen66978ee2009-01-31 02:22:37 +00005197 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5198 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005199
Evan Cheng697cbbf2009-03-20 18:03:34 +00005200 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005201 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005202 break;
5203 }
5204 case InlineAsm::isClobber: {
5205 // Add the clobbered value to the operand list, so that the register
5206 // allocator is aware that the physreg got clobbered.
5207 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005208 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005209 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005210 break;
5211 }
5212 }
5213 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005214
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005215 // Finish up input operands.
5216 AsmNodeOperands[0] = Chain;
5217 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005218
Dale Johannesen66978ee2009-01-31 02:22:37 +00005219 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005220 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005221 &AsmNodeOperands[0], AsmNodeOperands.size());
5222 Flag = Chain.getValue(1);
5223
5224 // If this asm returns a register value, copy the result from that register
5225 // and set it as the value of the call.
5226 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005227 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005228 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005229
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005230 // FIXME: Why don't we do this for inline asms with MRVs?
5231 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005232 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005233
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005234 // If any of the results of the inline asm is a vector, it may have the
5235 // wrong width/num elts. This can happen for register classes that can
5236 // contain multiple different value types. The preg or vreg allocated may
5237 // not have the same VT as was expected. Convert it to the right type
5238 // with bit_convert.
5239 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005240 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005241 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005242
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005243 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005244 ResultType.isInteger() && Val.getValueType().isInteger()) {
5245 // If a result value was tied to an input value, the computed result may
5246 // have a wider width than the expected result. Extract the relevant
5247 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005248 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005249 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005250
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005251 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005252 }
Dan Gohman95915732008-10-18 01:03:45 +00005253
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005254 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005255 // Don't need to use this as a chain in this case.
5256 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5257 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005259
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005260 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262 // Process indirect outputs, first output all of the flagged copies out of
5263 // physregs.
5264 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5265 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5266 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005267 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5268 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005269 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005270
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005271 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005272
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005273 // Emit the non-flagged stores from the physregs.
5274 SmallVector<SDValue, 8> OutChains;
5275 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005276 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005277 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005278 getValue(StoresToEmit[i].second),
5279 StoresToEmit[i].second, 0));
5280 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005281 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005282 &OutChains[0], OutChains.size());
5283 DAG.setRoot(Chain);
5284}
5285
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005286void SelectionDAGLowering::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005287 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005288 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005289 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005290 DAG.getSrcValue(I.getOperand(1))));
5291}
5292
5293void SelectionDAGLowering::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005294 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5295 getRoot(), getValue(I.getOperand(0)),
5296 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005297 setValue(&I, V);
5298 DAG.setRoot(V.getValue(1));
5299}
5300
5301void SelectionDAGLowering::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005302 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005303 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005304 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 DAG.getSrcValue(I.getOperand(1))));
5306}
5307
5308void SelectionDAGLowering::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005309 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005310 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005311 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 getValue(I.getOperand(2)),
5313 DAG.getSrcValue(I.getOperand(1)),
5314 DAG.getSrcValue(I.getOperand(2))));
5315}
5316
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005317/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005318/// implementation, which just calls LowerCall.
5319/// FIXME: When all targets are
5320/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005321std::pair<SDValue, SDValue>
5322TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5323 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005324 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005325 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005326 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005327 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005328 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005329
Dan Gohman1937e2f2008-09-16 01:42:28 +00005330 assert((!isTailCall || PerformTailCallOpt) &&
5331 "isTailCall set when tail-call optimizations are disabled!");
5332
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005333 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005334 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005336 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005337 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5338 for (unsigned Value = 0, NumValues = ValueVTs.size();
5339 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005340 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005341 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005342 SDValue Op = SDValue(Args[i].Node.getNode(),
5343 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005344 ISD::ArgFlagsTy Flags;
5345 unsigned OriginalAlignment =
5346 getTargetData()->getABITypeAlignment(ArgTy);
5347
5348 if (Args[i].isZExt)
5349 Flags.setZExt();
5350 if (Args[i].isSExt)
5351 Flags.setSExt();
5352 if (Args[i].isInReg)
5353 Flags.setInReg();
5354 if (Args[i].isSRet)
5355 Flags.setSRet();
5356 if (Args[i].isByVal) {
5357 Flags.setByVal();
5358 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5359 const Type *ElementTy = Ty->getElementType();
5360 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005361 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005362 // For ByVal, alignment should come from FE. BE will guess if this
5363 // info is not there but there are cases it cannot get right.
5364 if (Args[i].Alignment)
5365 FrameAlign = Args[i].Alignment;
5366 Flags.setByValAlign(FrameAlign);
5367 Flags.setByValSize(FrameSize);
5368 }
5369 if (Args[i].isNest)
5370 Flags.setNest();
5371 Flags.setOrigAlign(OriginalAlignment);
5372
Owen Anderson23b9b192009-08-12 00:36:31 +00005373 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5374 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005375 SmallVector<SDValue, 4> Parts(NumParts);
5376 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5377
5378 if (Args[i].isSExt)
5379 ExtendKind = ISD::SIGN_EXTEND;
5380 else if (Args[i].isZExt)
5381 ExtendKind = ISD::ZERO_EXTEND;
5382
Dale Johannesen66978ee2009-01-31 02:22:37 +00005383 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005384
Dan Gohman98ca4f22009-08-05 01:29:28 +00005385 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005386 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005387 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5388 if (NumParts > 1 && j == 0)
5389 MyFlags.Flags.setSplit();
5390 else if (j != 0)
5391 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005392
Dan Gohman98ca4f22009-08-05 01:29:28 +00005393 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005394 }
5395 }
5396 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005397
Dan Gohman98ca4f22009-08-05 01:29:28 +00005398 // Handle the incoming return values from the call.
5399 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005400 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005401 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005402 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005403 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005404 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5405 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005406 for (unsigned i = 0; i != NumRegs; ++i) {
5407 ISD::InputArg MyFlags;
5408 MyFlags.VT = RegisterVT;
5409 MyFlags.Used = isReturnValueUsed;
5410 if (RetSExt)
5411 MyFlags.Flags.setSExt();
5412 if (RetZExt)
5413 MyFlags.Flags.setZExt();
5414 if (isInreg)
5415 MyFlags.Flags.setInReg();
5416 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005418 }
5419
Dan Gohman98ca4f22009-08-05 01:29:28 +00005420 // Check if target-dependent constraints permit a tail call here.
5421 // Target-independent constraints should be checked by the caller.
5422 if (isTailCall &&
5423 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
5424 isTailCall = false;
5425
5426 SmallVector<SDValue, 4> InVals;
5427 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
5428 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005429
5430 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005431 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005432 "LowerCall didn't return a valid chain!");
5433 assert((!isTailCall || InVals.empty()) &&
5434 "LowerCall emitted a return value for a tail call!");
5435 assert((isTailCall || InVals.size() == Ins.size()) &&
5436 "LowerCall didn't emit the correct number of values!");
5437 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5438 assert(InVals[i].getNode() &&
5439 "LowerCall emitted a null value!");
5440 assert(Ins[i].VT == InVals[i].getValueType() &&
5441 "LowerCall emitted a value with the wrong type!");
5442 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00005443
5444 // For a tail call, the return value is merely live-out and there aren't
5445 // any nodes in the DAG representing it. Return a special value to
5446 // indicate that a tail call has been emitted and no more Instructions
5447 // should be processed in the current block.
5448 if (isTailCall) {
5449 DAG.setRoot(Chain);
5450 return std::make_pair(SDValue(), SDValue());
5451 }
5452
5453 // Collect the legal value parts into potentially illegal values
5454 // that correspond to the original function's return values.
5455 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5456 if (RetSExt)
5457 AssertOp = ISD::AssertSext;
5458 else if (RetZExt)
5459 AssertOp = ISD::AssertZext;
5460 SmallVector<SDValue, 4> ReturnValues;
5461 unsigned CurReg = 0;
5462 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005463 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005464 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5465 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005466
5467 SDValue ReturnValue =
5468 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
5469 AssertOp);
5470 ReturnValues.push_back(ReturnValue);
5471 CurReg += NumRegs;
5472 }
5473
5474 // For a function returning void, there is no return value. We can't create
5475 // such a node, so we just return a null return value in that case. In
5476 // that case, nothing will actualy look at the value.
5477 if (ReturnValues.empty())
5478 return std::make_pair(SDValue(), Chain);
5479
5480 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5481 DAG.getVTList(&RetTys[0], RetTys.size()),
5482 &ReturnValues[0], ReturnValues.size());
5483
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005484 return std::make_pair(Res, Chain);
5485}
5486
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005487void TargetLowering::LowerOperationWrapper(SDNode *N,
5488 SmallVectorImpl<SDValue> &Results,
5489 SelectionDAG &DAG) {
5490 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005491 if (Res.getNode())
5492 Results.push_back(Res);
5493}
5494
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005495SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005496 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005497 return SDValue();
5498}
5499
5500
5501void SelectionDAGLowering::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
5502 SDValue Op = getValue(V);
5503 assert((Op.getOpcode() != ISD::CopyFromReg ||
5504 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5505 "Copy from a reg to the same reg!");
5506 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5507
Owen Anderson23b9b192009-08-12 00:36:31 +00005508 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005509 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005510 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005511 PendingExports.push_back(Chain);
5512}
5513
5514#include "llvm/CodeGen/SelectionDAGISel.h"
5515
Dan Gohman8c2b5252009-10-30 01:27:03 +00005516void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005517 // If this is the entry block, emit arguments.
5518 Function &F = *LLVMBB->getParent();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005519 SelectionDAG &DAG = SDL->DAG;
5520 SDValue OldRoot = DAG.getRoot();
5521 DebugLoc dl = SDL->getCurDebugLoc();
5522 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005523 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005524
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005525 // Check whether the function can return without sret-demotion.
5526 SmallVector<EVT, 4> OutVTs;
5527 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005528 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
5529 OutVTs, OutsFlags, TLI);
5530 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5531
5532 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
5533 OutVTs, OutsFlags, DAG);
5534 if (!FLI.CanLowerReturn) {
5535 // Put in an sret pointer parameter before all the other parameters.
5536 SmallVector<EVT, 1> ValueVTs;
5537 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5538
5539 // NOTE: Assuming that a pointer will never break down to more than one VT
5540 // or one register.
5541 ISD::ArgFlagsTy Flags;
5542 Flags.setSRet();
5543 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5544 ISD::InputArg RetArg(Flags, RegisterVT, true);
5545 Ins.push_back(RetArg);
5546 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005547
Dan Gohman98ca4f22009-08-05 01:29:28 +00005548 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005549 unsigned Idx = 1;
5550 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5551 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005552 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005553 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5554 bool isArgValueUsed = !I->use_empty();
5555 for (unsigned Value = 0, NumValues = ValueVTs.size();
5556 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005557 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005558 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005559 ISD::ArgFlagsTy Flags;
5560 unsigned OriginalAlignment =
5561 TD->getABITypeAlignment(ArgTy);
5562
5563 if (F.paramHasAttr(Idx, Attribute::ZExt))
5564 Flags.setZExt();
5565 if (F.paramHasAttr(Idx, Attribute::SExt))
5566 Flags.setSExt();
5567 if (F.paramHasAttr(Idx, Attribute::InReg))
5568 Flags.setInReg();
5569 if (F.paramHasAttr(Idx, Attribute::StructRet))
5570 Flags.setSRet();
5571 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5572 Flags.setByVal();
5573 const PointerType *Ty = cast<PointerType>(I->getType());
5574 const Type *ElementTy = Ty->getElementType();
5575 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5576 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5577 // For ByVal, alignment should be passed from FE. BE will guess if
5578 // this info is not there but there are cases it cannot get right.
5579 if (F.getParamAlignment(Idx))
5580 FrameAlign = F.getParamAlignment(Idx);
5581 Flags.setByValAlign(FrameAlign);
5582 Flags.setByValSize(FrameSize);
5583 }
5584 if (F.paramHasAttr(Idx, Attribute::Nest))
5585 Flags.setNest();
5586 Flags.setOrigAlign(OriginalAlignment);
5587
Owen Anderson23b9b192009-08-12 00:36:31 +00005588 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5589 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005590 for (unsigned i = 0; i != NumRegs; ++i) {
5591 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5592 if (NumRegs > 1 && i == 0)
5593 MyFlags.Flags.setSplit();
5594 // if it isn't first piece, alignment must be 1
5595 else if (i > 0)
5596 MyFlags.Flags.setOrigAlign(1);
5597 Ins.push_back(MyFlags);
5598 }
5599 }
5600 }
5601
5602 // Call the target to set up the argument values.
5603 SmallVector<SDValue, 8> InVals;
5604 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5605 F.isVarArg(), Ins,
5606 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005607
5608 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005609 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005610 "LowerFormalArguments didn't return a valid chain!");
5611 assert(InVals.size() == Ins.size() &&
5612 "LowerFormalArguments didn't emit the correct number of values!");
5613 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5614 assert(InVals[i].getNode() &&
5615 "LowerFormalArguments emitted a null value!");
5616 assert(Ins[i].VT == InVals[i].getValueType() &&
5617 "LowerFormalArguments emitted a value with the wrong type!");
5618 });
5619
5620 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005621 DAG.setRoot(NewRoot);
5622
5623 // Set up the argument values.
5624 unsigned i = 0;
5625 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005626 if (!FLI.CanLowerReturn) {
5627 // Create a virtual register for the sret pointer, and put in a copy
5628 // from the sret argument into it.
5629 SmallVector<EVT, 1> ValueVTs;
5630 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5631 EVT VT = ValueVTs[0];
5632 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5633 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5634 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT,
5635 VT, AssertOp);
5636
5637 MachineFunction& MF = SDL->DAG.getMachineFunction();
5638 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5639 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5640 FLI.DemoteRegister = SRetReg;
5641 NewRoot = SDL->DAG.getCopyToReg(NewRoot, SDL->getCurDebugLoc(), SRetReg, ArgValue);
5642 DAG.setRoot(NewRoot);
5643
5644 // i indexes lowered arguments. Bump it past the hidden sret argument.
5645 // Idx indexes LLVM arguments. Don't touch it.
5646 ++i;
5647 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005648 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5649 ++I, ++Idx) {
5650 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005651 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005652 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005653 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005654 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005655 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005656 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5657 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005658
5659 if (!I->use_empty()) {
5660 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5661 if (F.paramHasAttr(Idx, Attribute::SExt))
5662 AssertOp = ISD::AssertSext;
5663 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5664 AssertOp = ISD::AssertZext;
5665
5666 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
5667 PartVT, VT, AssertOp));
5668 }
5669 i += NumParts;
5670 }
5671 if (!I->use_empty()) {
5672 SDL->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
5673 SDL->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005674 // If this argument is live outside of the entry block, insert a copy from
5675 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005676 SDL->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005678 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005679 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005680
5681 // Finally, if the target has anything special to do, allow it to do so.
5682 // FIXME: this should insert code into the DAG!
5683 EmitFunctionEntryCode(F, SDL->DAG.getMachineFunction());
5684}
5685
5686/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5687/// ensure constants are generated when needed. Remember the virtual registers
5688/// that need to be added to the Machine PHI nodes as input. We cannot just
5689/// directly add them, because expansion might result in multiple MBB's for one
5690/// BB. As such, the start of the BB might correspond to a different MBB than
5691/// the end.
5692///
5693void
5694SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5695 TerminatorInst *TI = LLVMBB->getTerminator();
5696
5697 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5698
5699 // Check successor nodes' PHI nodes that expect a constant to be available
5700 // from this block.
5701 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5702 BasicBlock *SuccBB = TI->getSuccessor(succ);
5703 if (!isa<PHINode>(SuccBB->begin())) continue;
5704 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005705
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005706 // If this terminator has multiple identical successors (common for
5707 // switches), only handle each succ once.
5708 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005709
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005710 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5711 PHINode *PN;
5712
5713 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5714 // nodes and Machine PHI nodes, but the incoming operands have not been
5715 // emitted yet.
5716 for (BasicBlock::iterator I = SuccBB->begin();
5717 (PN = dyn_cast<PHINode>(I)); ++I) {
5718 // Ignore dead phi's.
5719 if (PN->use_empty()) continue;
5720
5721 unsigned Reg;
5722 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5723
5724 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
5725 unsigned &RegOut = SDL->ConstantsOut[C];
5726 if (RegOut == 0) {
5727 RegOut = FuncInfo->CreateRegForValue(C);
5728 SDL->CopyValueToVirtualRegister(C, RegOut);
5729 }
5730 Reg = RegOut;
5731 } else {
5732 Reg = FuncInfo->ValueMap[PHIOp];
5733 if (Reg == 0) {
5734 assert(isa<AllocaInst>(PHIOp) &&
5735 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5736 "Didn't codegen value into a register!??");
5737 Reg = FuncInfo->CreateRegForValue(PHIOp);
5738 SDL->CopyValueToVirtualRegister(PHIOp, Reg);
5739 }
5740 }
5741
5742 // Remember that this register needs to added to the machine PHI node as
5743 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00005744 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005745 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5746 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00005747 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00005748 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005749 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
5750 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
5751 Reg += NumRegisters;
5752 }
5753 }
5754 }
5755 SDL->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005756}
5757
Dan Gohman3df24e62008-09-03 23:12:08 +00005758/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5759/// supports legal types, and it emits MachineInstrs directly instead of
5760/// creating SelectionDAG nodes.
5761///
5762bool
5763SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5764 FastISel *F) {
5765 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005766
Dan Gohman3df24e62008-09-03 23:12:08 +00005767 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5768 unsigned OrigNumPHINodesToUpdate = SDL->PHINodesToUpdate.size();
5769
5770 // Check successor nodes' PHI nodes that expect a constant to be available
5771 // from this block.
5772 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5773 BasicBlock *SuccBB = TI->getSuccessor(succ);
5774 if (!isa<PHINode>(SuccBB->begin())) continue;
5775 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005776
Dan Gohman3df24e62008-09-03 23:12:08 +00005777 // If this terminator has multiple identical successors (common for
5778 // switches), only handle each succ once.
5779 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005780
Dan Gohman3df24e62008-09-03 23:12:08 +00005781 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5782 PHINode *PN;
5783
5784 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5785 // nodes and Machine PHI nodes, but the incoming operands have not been
5786 // emitted yet.
5787 for (BasicBlock::iterator I = SuccBB->begin();
5788 (PN = dyn_cast<PHINode>(I)); ++I) {
5789 // Ignore dead phi's.
5790 if (PN->use_empty()) continue;
5791
5792 // Only handle legal types. Two interesting things to note here. First,
5793 // by bailing out early, we may leave behind some dead instructions,
5794 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
5795 // own moves. Second, this check is necessary becuase FastISel doesn't
5796 // use CreateRegForValue to create registers, so it always creates
5797 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00005798 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00005799 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
5800 // Promote MVT::i1.
5801 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00005802 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00005803 else {
5804 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5805 return false;
5806 }
Dan Gohman3df24e62008-09-03 23:12:08 +00005807 }
5808
5809 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5810
5811 unsigned Reg = F->getRegForValue(PHIOp);
5812 if (Reg == 0) {
5813 SDL->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
5814 return false;
5815 }
5816 SDL->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
5817 }
5818 }
5819
5820 return true;
5821}