blob: 59c961ba6f7f41f92f23ca29a97937dec98d57e8 [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
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"
Dan Gohman2048b852009-11-23 18:04:58 +000015#include "SelectionDAGBuilder.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) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000446 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
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
Dan Gohman2048b852009-11-23 18:04:58 +0000504void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000505 AA = &aa;
506 GFI = gfi;
507 TD = DAG.getTarget().getTargetData();
508}
509
510/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000511/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000512/// 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.
Dan Gohman2048b852009-11-23 18:04:58 +0000516void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000517 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///
Dan Gohman2048b852009-11-23 18:04:58 +0000531SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000532 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///
Dan Gohman2048b852009-11-23 18:04:58 +0000554SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000555 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
Dan Gohman2048b852009-11-23 18:04:58 +0000581void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000582 visit(I.getOpcode(), I);
583}
584
Dan Gohman2048b852009-11-23 18:04:58 +0000585void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000586 // We're processing a new instruction.
587 ++SDNodeOrder;
588
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000589 // Note: this doesn't use InstVisitor, because it has to work with
590 // ConstantExpr's in addition to instructions.
591 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000592 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000593 // Build the switch statement using the Instruction.def file.
594#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000595 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000596#include "llvm/Instruction.def"
597 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000598}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000599
Dan Gohman2048b852009-11-23 18:04:58 +0000600SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000601 SDValue &N = NodeMap[V];
602 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000603
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000604 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000605 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000606
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000607 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000608 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000609
610 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
611 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000613 if (isa<ConstantPointerNull>(C))
614 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000615
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000616 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000617 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000618
Nate Begeman9008ca62009-04-27 18:41:29 +0000619 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000620 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000621
622 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
623 visit(CE->getOpcode(), *CE);
624 SDValue N1 = NodeMap[V];
625 assert(N1.getNode() && "visit didn't populate the ValueMap!");
626 return N1;
627 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000628
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000629 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
630 SmallVector<SDValue, 4> Constants;
631 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
632 OI != OE; ++OI) {
633 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000634 // If the operand is an empty aggregate, there are no values.
635 if (!Val) continue;
636 // Add each leaf value from the operand to the Constants list
637 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000638 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
639 Constants.push_back(SDValue(Val, i));
640 }
Bill Wendling87710f02009-12-21 23:47:40 +0000641
642 SDValue Res = DAG.getMergeValues(&Constants[0], Constants.size(),
643 getCurDebugLoc());
644 if (DisableScheduling)
645 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
646 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000647 }
648
649 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
650 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
651 "Unknown struct or array constant!");
652
Owen Andersone50ed302009-08-10 22:56:29 +0000653 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000654 ComputeValueVTs(TLI, C->getType(), ValueVTs);
655 unsigned NumElts = ValueVTs.size();
656 if (NumElts == 0)
657 return SDValue(); // empty struct
658 SmallVector<SDValue, 4> Constants(NumElts);
659 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000660 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000661 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000662 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000663 else if (EltVT.isFloatingPoint())
664 Constants[i] = DAG.getConstantFP(0, EltVT);
665 else
666 Constants[i] = DAG.getConstant(0, EltVT);
667 }
Bill Wendling87710f02009-12-21 23:47:40 +0000668
669 SDValue Res = DAG.getMergeValues(&Constants[0], NumElts,
670 getCurDebugLoc());
671 if (DisableScheduling)
672 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
673 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000674 }
675
Dan Gohman8c2b5252009-10-30 01:27:03 +0000676 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000677 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000679 const VectorType *VecTy = cast<VectorType>(V->getType());
680 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000681
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682 // Now that we know the number and type of the elements, get that number of
683 // elements into the Ops array based on what kind of constant it is.
684 SmallVector<SDValue, 16> Ops;
685 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
686 for (unsigned i = 0; i != NumElements; ++i)
687 Ops.push_back(getValue(CP->getOperand(i)));
688 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000689 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000690 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000691
692 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000693 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000694 Op = DAG.getConstantFP(0, EltVT);
695 else
696 Op = DAG.getConstant(0, EltVT);
697 Ops.assign(NumElements, Op);
698 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000699
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000700 // Create a BUILD_VECTOR node.
Bill Wendling87710f02009-12-21 23:47:40 +0000701 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
702 VT, &Ops[0], Ops.size());
703 if (DisableScheduling)
704 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
705
706 return NodeMap[V] = Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000707 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000708
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000709 // If this is a static alloca, generate it as the frameindex instead of
710 // computation.
711 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
712 DenseMap<const AllocaInst*, int>::iterator SI =
713 FuncInfo.StaticAllocaMap.find(AI);
714 if (SI != FuncInfo.StaticAllocaMap.end())
715 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
716 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000718 unsigned InReg = FuncInfo.ValueMap[V];
719 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000720
Owen Anderson23b9b192009-08-12 00:36:31 +0000721 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000722 SDValue Chain = DAG.getEntryNode();
Bill Wendling87710f02009-12-21 23:47:40 +0000723 SDValue Res = RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
724 if (DisableScheduling)
725 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
726 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000727}
728
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000729/// Get the EVTs and ArgFlags collections that represent the return type
730/// of the given function. This does not require a DAG or a return value, and
731/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000732static void getReturnInfo(const Type* ReturnType,
733 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000734 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000735 TargetLowering &TLI,
736 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000737 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000738 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000739 unsigned NumValues = ValueVTs.size();
740 if ( NumValues == 0 ) return;
741
742 for (unsigned j = 0, f = NumValues; j != f; ++j) {
743 EVT VT = ValueVTs[j];
744 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000745
746 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000747 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000748 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000749 ExtendKind = ISD::ZERO_EXTEND;
750
751 // FIXME: C calling convention requires the return type to be promoted to
752 // at least 32-bit. But this is not necessary for non-C calling
753 // conventions. The frontend should mark functions whose return values
754 // require promoting with signext or zeroext attributes.
755 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000756 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000757 if (VT.bitsLT(MinVT))
758 VT = MinVT;
759 }
760
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000761 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
762 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000763 // 'inreg' on function refers to return value
764 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000765 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000766 Flags.setInReg();
767
768 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000769 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000770 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000771 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000772 Flags.setZExt();
773
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000774 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000775 OutVTs.push_back(PartVT);
776 OutFlags.push_back(Flags);
777 }
778 }
779}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000780
Dan Gohman2048b852009-11-23 18:04:58 +0000781void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000782 SDValue Chain = getControlRoot();
783 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000784 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
785
786 if (!FLI.CanLowerReturn) {
787 unsigned DemoteReg = FLI.DemoteRegister;
788 const Function *F = I.getParent()->getParent();
789
790 // Emit a store of the return value through the virtual register.
791 // Leave Outs empty so that LowerReturn won't try to load return
792 // registers the usual way.
793 SmallVector<EVT, 1> PtrValueVTs;
794 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
795 PtrValueVTs);
796
797 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
798 SDValue RetOp = getValue(I.getOperand(0));
799
Owen Andersone50ed302009-08-10 22:56:29 +0000800 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000801 SmallVector<uint64_t, 4> Offsets;
802 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000803 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000804
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000805 SmallVector<SDValue, 4> Chains(NumValues);
806 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000807 for (unsigned i = 0; i != NumValues; ++i) {
808 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
809 DAG.getConstant(Offsets[i], PtrVT));
810 Chains[i] =
811 DAG.getStore(Chain, getCurDebugLoc(),
812 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
813 Add, NULL, Offsets[i], false, 0);
814
815 if (DisableScheduling) {
816 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
817 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
818 }
819 }
820
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000821 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
822 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000823
824 if (DisableScheduling)
825 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
826 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000827 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
828 SmallVector<EVT, 4> ValueVTs;
829 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
830 unsigned NumValues = ValueVTs.size();
831 if (NumValues == 0) continue;
832
833 SDValue RetOp = getValue(I.getOperand(i));
834 for (unsigned j = 0, f = NumValues; j != f; ++j) {
835 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000836
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000837 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000838
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000839 const Function *F = I.getParent()->getParent();
840 if (F->paramHasAttr(0, Attribute::SExt))
841 ExtendKind = ISD::SIGN_EXTEND;
842 else if (F->paramHasAttr(0, Attribute::ZExt))
843 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000844
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000845 // FIXME: C calling convention requires the return type to be promoted to
846 // at least 32-bit. But this is not necessary for non-C calling
847 // conventions. The frontend should mark functions whose return values
848 // require promoting with signext or zeroext attributes.
849 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
850 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
851 if (VT.bitsLT(MinVT))
852 VT = MinVT;
853 }
854
855 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
856 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
857 SmallVector<SDValue, 4> Parts(NumParts);
858 getCopyToParts(DAG, getCurDebugLoc(),
859 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
860 &Parts[0], NumParts, PartVT, ExtendKind);
861
862 // 'inreg' on function refers to return value
863 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
864 if (F->paramHasAttr(0, Attribute::InReg))
865 Flags.setInReg();
866
867 // Propagate extension type if any
868 if (F->paramHasAttr(0, Attribute::SExt))
869 Flags.setSExt();
870 else if (F->paramHasAttr(0, Attribute::ZExt))
871 Flags.setZExt();
872
873 for (unsigned i = 0; i < NumParts; ++i)
874 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000875 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000876 }
877 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000878
879 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000880 CallingConv::ID CallConv =
881 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000882 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
883 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000884
885 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000887 "LowerReturn didn't return a valid chain!");
888
889 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000890 DAG.setRoot(Chain);
Bill Wendling87710f02009-12-21 23:47:40 +0000891
892 if (DisableScheduling)
893 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000894}
895
Dan Gohmanad62f532009-04-23 23:13:24 +0000896/// CopyToExportRegsIfNeeded - If the given value has virtual registers
897/// created for it, emit nodes to copy the value into the virtual
898/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000899void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000900 if (!V->use_empty()) {
901 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
902 if (VMI != FuncInfo.ValueMap.end())
903 CopyValueToVirtualRegister(V, VMI->second);
904 }
905}
906
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000907/// ExportFromCurrentBlock - If this condition isn't known to be exported from
908/// the current basic block, add it to ValueMap now so that we'll get a
909/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000910void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000911 // No need to export constants.
912 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000914 // Already exported?
915 if (FuncInfo.isExportedInst(V)) return;
916
917 unsigned Reg = FuncInfo.InitializeRegForValue(V);
918 CopyValueToVirtualRegister(V, Reg);
919}
920
Dan Gohman2048b852009-11-23 18:04:58 +0000921bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
922 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000923 // The operands of the setcc have to be in this block. We don't know
924 // how to export them from some other block.
925 if (Instruction *VI = dyn_cast<Instruction>(V)) {
926 // Can export from current BB.
927 if (VI->getParent() == FromBB)
928 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000929
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000930 // Is already exported, noop.
931 return FuncInfo.isExportedInst(V);
932 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000934 // If this is an argument, we can export it if the BB is the entry block or
935 // if it is already exported.
936 if (isa<Argument>(V)) {
937 if (FromBB == &FromBB->getParent()->getEntryBlock())
938 return true;
939
940 // Otherwise, can only export this if it is already exported.
941 return FuncInfo.isExportedInst(V);
942 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000943
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000944 // Otherwise, constants can always be exported.
945 return true;
946}
947
948static bool InBlock(const Value *V, const BasicBlock *BB) {
949 if (const Instruction *I = dyn_cast<Instruction>(V))
950 return I->getParent() == BB;
951 return true;
952}
953
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000954/// getFCmpCondCode - Return the ISD condition code corresponding to
955/// the given LLVM IR floating-point condition code. This includes
956/// consideration of global floating-point math flags.
957///
958static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
959 ISD::CondCode FPC, FOC;
960 switch (Pred) {
961 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
962 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
963 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
964 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
965 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
966 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
967 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
968 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
969 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
970 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
971 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
972 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
973 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
974 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
975 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
976 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
977 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000978 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000979 FOC = FPC = ISD::SETFALSE;
980 break;
981 }
982 if (FiniteOnlyFPMath())
983 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000984 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000985 return FPC;
986}
987
988/// getICmpCondCode - Return the ISD condition code corresponding to
989/// the given LLVM IR integer condition code.
990///
991static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
992 switch (Pred) {
993 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
994 case ICmpInst::ICMP_NE: return ISD::SETNE;
995 case ICmpInst::ICMP_SLE: return ISD::SETLE;
996 case ICmpInst::ICMP_ULE: return ISD::SETULE;
997 case ICmpInst::ICMP_SGE: return ISD::SETGE;
998 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
999 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1000 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1001 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1002 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1003 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001004 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001005 return ISD::SETNE;
1006 }
1007}
1008
Dan Gohmanc2277342008-10-17 21:16:08 +00001009/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1010/// This function emits a branch and is used at the leaves of an OR or an
1011/// AND operator tree.
1012///
1013void
Dan Gohman2048b852009-11-23 18:04:58 +00001014SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1015 MachineBasicBlock *TBB,
1016 MachineBasicBlock *FBB,
1017 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001018 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001019
Dan Gohmanc2277342008-10-17 21:16:08 +00001020 // If the leaf of the tree is a comparison, merge the condition into
1021 // the caseblock.
1022 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1023 // The operands of the cmp have to be in this block. We don't know
1024 // how to export them from some other block. If this is the first block
1025 // of the sequence, no exporting is needed.
1026 if (CurBB == CurMBB ||
1027 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1028 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001029 ISD::CondCode Condition;
1030 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001031 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001032 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001033 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001034 } else {
1035 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001036 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001037 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001038
1039 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001040 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1041 SwitchCases.push_back(CB);
1042 return;
1043 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001044 }
1045
1046 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001047 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001048 NULL, TBB, FBB, CurBB);
1049 SwitchCases.push_back(CB);
1050}
1051
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001052/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001053void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1054 MachineBasicBlock *TBB,
1055 MachineBasicBlock *FBB,
1056 MachineBasicBlock *CurBB,
1057 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001058 // If this node is not part of the or/and tree, emit it as a branch.
1059 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001060 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001061 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1062 BOp->getParent() != CurBB->getBasicBlock() ||
1063 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1064 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1065 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001066 return;
1067 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001069 // Create TmpBB after CurBB.
1070 MachineFunction::iterator BBI = CurBB;
1071 MachineFunction &MF = DAG.getMachineFunction();
1072 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1073 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001074
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001075 if (Opc == Instruction::Or) {
1076 // Codegen X | Y as:
1077 // jmp_if_X TBB
1078 // jmp TmpBB
1079 // TmpBB:
1080 // jmp_if_Y TBB
1081 // jmp FBB
1082 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001083
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001084 // Emit the LHS condition.
1085 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001086
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001087 // Emit the RHS condition into TmpBB.
1088 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1089 } else {
1090 assert(Opc == Instruction::And && "Unknown merge op!");
1091 // Codegen X & Y as:
1092 // jmp_if_X TmpBB
1093 // jmp FBB
1094 // TmpBB:
1095 // jmp_if_Y TBB
1096 // jmp FBB
1097 //
1098 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001099
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001100 // Emit the LHS condition.
1101 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001103 // Emit the RHS condition into TmpBB.
1104 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1105 }
1106}
1107
1108/// If the set of cases should be emitted as a series of branches, return true.
1109/// If we should emit this as a bunch of and/or'd together conditions, return
1110/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001111bool
Dan Gohman2048b852009-11-23 18:04:58 +00001112SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001113 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001114
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001115 // If this is two comparisons of the same values or'd or and'd together, they
1116 // will get folded into a single comparison, so don't emit two blocks.
1117 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1118 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1119 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1120 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1121 return false;
1122 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001124 return true;
1125}
1126
Dan Gohman2048b852009-11-23 18:04:58 +00001127void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001128 // Update machine-CFG edges.
1129 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1130
1131 // Figure out which block is immediately after the current one.
1132 MachineBasicBlock *NextBlock = 0;
1133 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001134 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001135 NextBlock = BBI;
1136
1137 if (I.isUnconditional()) {
1138 // Update machine-CFG edges.
1139 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001141 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001142 if (Succ0MBB != NextBlock) {
1143 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001145 DAG.getBasicBlock(Succ0MBB));
1146 DAG.setRoot(V);
1147
1148 if (DisableScheduling)
1149 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1150 }
1151
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001152 return;
1153 }
1154
1155 // If this condition is one of the special cases we handle, do special stuff
1156 // now.
1157 Value *CondVal = I.getCondition();
1158 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1159
1160 // If this is a series of conditions that are or'd or and'd together, emit
1161 // this as a sequence of branches instead of setcc's with and/or operations.
1162 // For example, instead of something like:
1163 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001164 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001165 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001166 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001167 // or C, F
1168 // jnz foo
1169 // Emit:
1170 // cmp A, B
1171 // je foo
1172 // cmp D, E
1173 // jle foo
1174 //
1175 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001176 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001177 (BOp->getOpcode() == Instruction::And ||
1178 BOp->getOpcode() == Instruction::Or)) {
1179 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1180 // If the compares in later blocks need to use values not currently
1181 // exported from this block, export them now. This block should always
1182 // be the first entry.
1183 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001185 // Allow some cases to be rejected.
1186 if (ShouldEmitAsBranches(SwitchCases)) {
1187 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1188 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1189 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1190 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001191
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001192 // Emit the branch for this block.
1193 visitSwitchCase(SwitchCases[0]);
1194 SwitchCases.erase(SwitchCases.begin());
1195 return;
1196 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001197
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001198 // Okay, we decided not to do this, remove any inserted MBB's and clear
1199 // SwitchCases.
1200 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001201 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001202
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001203 SwitchCases.clear();
1204 }
1205 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001206
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001208 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001209 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001211 // Use visitSwitchCase to actually insert the fast branch sequence for this
1212 // cond branch.
1213 visitSwitchCase(CB);
1214}
1215
1216/// visitSwitchCase - Emits the necessary code to represent a single node in
1217/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001218void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001219 SDValue Cond;
1220 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001221 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001222
1223 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001224 if (CB.CmpMHS == NULL) {
1225 // Fold "(X == true)" to X and "(X == false)" to !X to
1226 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001227 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001228 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001229 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001230 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001231 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001232 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001233 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001234 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236 } else {
1237 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1238
Anton Korobeynikov23218582008-12-23 22:25:27 +00001239 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1240 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001241
1242 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001243 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001244
1245 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001246 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001247 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001248 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001249 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001250 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001252 DAG.getConstant(High-Low, VT), ISD::SETULE);
1253 }
1254 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001255
Bill Wendling87710f02009-12-21 23:47:40 +00001256 if (DisableScheduling)
1257 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001259 // Update successor info
1260 CurMBB->addSuccessor(CB.TrueBB);
1261 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 // Set NextBlock to be the MBB immediately after the current one, if any.
1264 // This is used to avoid emitting unnecessary branches to the next block.
1265 MachineBasicBlock *NextBlock = 0;
1266 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001267 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001268 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001270 // If the lhs block is the next block, invert the condition so that we can
1271 // fall through to the lhs instead of the rhs block.
1272 if (CB.TrueBB == NextBlock) {
1273 std::swap(CB.TrueBB, CB.FalseBB);
1274 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001275 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001276
1277 if (DisableScheduling)
1278 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001279 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001280
Dale Johannesenf5d97892009-02-04 01:48:28 +00001281 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001282 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001283 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001284
Bill Wendling87710f02009-12-21 23:47:40 +00001285 if (DisableScheduling)
1286 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1287
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001288 // If the branch was constant folded, fix up the CFG.
1289 if (BrCond.getOpcode() == ISD::BR) {
1290 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291 } else {
1292 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001293 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001294 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001295
Bill Wendling87710f02009-12-21 23:47:40 +00001296 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001297 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1298 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001299
1300 if (DisableScheduling)
1301 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1302 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001304
1305 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001306}
1307
1308/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001309void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001310 // Emit the code for the jump table
1311 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001312 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001313 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1314 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001316 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1317 MVT::Other, Index.getValue(1),
1318 Table, Index);
1319 DAG.setRoot(BrJumpTable);
1320
Bill Wendling87710f02009-12-21 23:47:40 +00001321 if (DisableScheduling) {
1322 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1323 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001324 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001325 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001326}
1327
1328/// visitJumpTableHeader - This function emits necessary code to produce index
1329/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001330void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1331 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001332 // Subtract the lowest switch case value from the value being switched on and
1333 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 // difference between smallest and largest cases.
1335 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001336 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001337 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001338 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001339
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001340 // The SDNode we just created, which holds the value being switched on minus
1341 // the the smallest case value, needs to be copied to a virtual register so it
1342 // can be used as an index into the jump table in a subsequent basic block.
1343 // This value may be smaller or larger than the target's pointer type, and
1344 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001345 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001346
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001348 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1349 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001350 JT.Reg = JumpTableReg;
1351
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001352 // Emit the range check for the jump table, and branch to the default block
1353 // for the switch statement if the value being switched on exceeds the largest
1354 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001355 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001356 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001357 DAG.getConstant(JTH.Last-JTH.First,VT),
1358 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001359
Bill Wendling87710f02009-12-21 23:47:40 +00001360 if (DisableScheduling) {
1361 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1362 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1363 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1364 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1365 }
1366
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001367 // Set NextBlock to be the MBB immediately after the current one, if any.
1368 // This is used to avoid emitting unnecessary branches to the next block.
1369 MachineBasicBlock *NextBlock = 0;
1370 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001371
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001372 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001373 NextBlock = BBI;
1374
Dale Johannesen66978ee2009-01-31 02:22:37 +00001375 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001376 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001377 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001378
Bill Wendling87710f02009-12-21 23:47:40 +00001379 if (DisableScheduling)
1380 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1381
1382 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001383 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1384 DAG.getBasicBlock(JT.MBB));
1385
Bill Wendling87710f02009-12-21 23:47:40 +00001386 if (DisableScheduling)
1387 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1388 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001389
Bill Wendling87710f02009-12-21 23:47:40 +00001390 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001391}
1392
1393/// visitBitTestHeader - This function emits necessary code to produce value
1394/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001395void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 // Subtract the minimum value
1397 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001398 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001399 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001400 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001401
1402 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001403 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001404 TLI.getSetCCResultType(Sub.getValueType()),
1405 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001406 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001407
Bill Wendling87710f02009-12-21 23:47:40 +00001408 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1409 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001410
Duncan Sands92abc622009-01-31 15:50:11 +00001411 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001412 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1413 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001414
Bill Wendling87710f02009-12-21 23:47:40 +00001415 if (DisableScheduling) {
1416 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1417 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1418 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1419 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1420 }
1421
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001422 // Set NextBlock to be the MBB immediately after the current one, if any.
1423 // This is used to avoid emitting unnecessary branches to the next block.
1424 MachineBasicBlock *NextBlock = 0;
1425 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001426 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001427 NextBlock = BBI;
1428
1429 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1430
1431 CurMBB->addSuccessor(B.Default);
1432 CurMBB->addSuccessor(MBB);
1433
Dale Johannesen66978ee2009-01-31 02:22:37 +00001434 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001435 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001436 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001437
Bill Wendling87710f02009-12-21 23:47:40 +00001438 if (DisableScheduling)
1439 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1440
1441 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001442 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1443 DAG.getBasicBlock(MBB));
1444
Bill Wendling87710f02009-12-21 23:47:40 +00001445 if (DisableScheduling)
1446 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1447 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001448
Bill Wendling87710f02009-12-21 23:47:40 +00001449 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001450}
1451
1452/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001453void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1454 unsigned Reg,
1455 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001456 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001457 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001458 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001459 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001460 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001461 DAG.getConstant(1, TLI.getPointerTy()),
1462 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001463
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001464 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001465 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001466 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001467 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001468 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1469 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001470 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001471 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001472
Bill Wendling87710f02009-12-21 23:47:40 +00001473 if (DisableScheduling) {
1474 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1475 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1476 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1477 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1478 }
1479
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001480 CurMBB->addSuccessor(B.TargetBB);
1481 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001482
Dale Johannesen66978ee2009-01-31 02:22:37 +00001483 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001484 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001485 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486
Bill Wendling87710f02009-12-21 23:47:40 +00001487 if (DisableScheduling)
1488 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1489
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001490 // Set NextBlock to be the MBB immediately after the current one, if any.
1491 // This is used to avoid emitting unnecessary branches to the next block.
1492 MachineBasicBlock *NextBlock = 0;
1493 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001494 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001495 NextBlock = BBI;
1496
Bill Wendling87710f02009-12-21 23:47:40 +00001497 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001498 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1499 DAG.getBasicBlock(NextMBB));
1500
Bill Wendling87710f02009-12-21 23:47:40 +00001501 if (DisableScheduling)
1502 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1503 }
Bill Wendling0777e922009-12-21 21:59:52 +00001504
Bill Wendling87710f02009-12-21 23:47:40 +00001505 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001506}
1507
Dan Gohman2048b852009-11-23 18:04:58 +00001508void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001509 // Retrieve successors.
1510 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1511 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1512
Gabor Greifb67e6b32009-01-15 11:10:44 +00001513 const Value *Callee(I.getCalledValue());
1514 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001515 visitInlineAsm(&I);
1516 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001517 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001518
1519 // If the value of the invoke is used outside of its defining block, make it
1520 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001521 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001522
1523 // Update successor info
1524 CurMBB->addSuccessor(Return);
1525 CurMBB->addSuccessor(LandingPad);
1526
1527 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001528 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1529 MVT::Other, getControlRoot(),
1530 DAG.getBasicBlock(Return));
1531 DAG.setRoot(Branch);
1532
1533 if (DisableScheduling)
1534 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001535}
1536
Dan Gohman2048b852009-11-23 18:04:58 +00001537void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001538}
1539
1540/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1541/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001542bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1543 CaseRecVector& WorkList,
1544 Value* SV,
1545 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001547
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001548 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001549 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001550 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001551 return false;
1552
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001553 // Get the MachineFunction which holds the current MBB. This is used when
1554 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001555 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001556
1557 // Figure out which block is immediately after the current one.
1558 MachineBasicBlock *NextBlock = 0;
1559 MachineFunction::iterator BBI = CR.CaseBB;
1560
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001561 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562 NextBlock = BBI;
1563
1564 // TODO: If any two of the cases has the same destination, and if one value
1565 // is the same as the other, but has one bit unset that the other has set,
1566 // use bit manipulation to do two compares at once. For example:
1567 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001569 // Rearrange the case blocks so that the last one falls through if possible.
1570 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1571 // The last case block won't fall through into 'NextBlock' if we emit the
1572 // branches in this order. See if rearranging a case value would help.
1573 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1574 if (I->BB == NextBlock) {
1575 std::swap(*I, BackCase);
1576 break;
1577 }
1578 }
1579 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001580
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001581 // Create a CaseBlock record representing a conditional branch to
1582 // the Case's target mbb if the value being switched on SV is equal
1583 // to C.
1584 MachineBasicBlock *CurBlock = CR.CaseBB;
1585 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1586 MachineBasicBlock *FallThrough;
1587 if (I != E-1) {
1588 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1589 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001590
1591 // Put SV in a virtual register to make it available from the new blocks.
1592 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593 } else {
1594 // If the last case doesn't match, go to the default block.
1595 FallThrough = Default;
1596 }
1597
1598 Value *RHS, *LHS, *MHS;
1599 ISD::CondCode CC;
1600 if (I->High == I->Low) {
1601 // This is just small small case range :) containing exactly 1 case
1602 CC = ISD::SETEQ;
1603 LHS = SV; RHS = I->High; MHS = NULL;
1604 } else {
1605 CC = ISD::SETLE;
1606 LHS = I->Low; MHS = SV; RHS = I->High;
1607 }
1608 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001609
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610 // If emitting the first comparison, just call visitSwitchCase to emit the
1611 // code into the current block. Otherwise, push the CaseBlock onto the
1612 // vector to be later processed by SDISel, and insert the node's MBB
1613 // before the next MBB.
1614 if (CurBlock == CurMBB)
1615 visitSwitchCase(CB);
1616 else
1617 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 CurBlock = FallThrough;
1620 }
1621
1622 return true;
1623}
1624
1625static inline bool areJTsAllowed(const TargetLowering &TLI) {
1626 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001627 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1628 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001629}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001630
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001631static APInt ComputeRange(const APInt &First, const APInt &Last) {
1632 APInt LastExt(Last), FirstExt(First);
1633 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1634 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1635 return (LastExt - FirstExt + 1ULL);
1636}
1637
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001639bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1640 CaseRecVector& WorkList,
1641 Value* SV,
1642 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001643 Case& FrontCase = *CR.Range.first;
1644 Case& BackCase = *(CR.Range.second-1);
1645
Chris Lattnere880efe2009-11-07 07:50:34 +00001646 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1647 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001648
Chris Lattnere880efe2009-11-07 07:50:34 +00001649 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001650 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1651 I!=E; ++I)
1652 TSize += I->size();
1653
Chris Lattnere880efe2009-11-07 07:50:34 +00001654 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001655 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001656
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001657 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001658 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001659 if (Density < 0.4)
1660 return false;
1661
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001662 DEBUG(errs() << "Lowering jump table\n"
1663 << "First entry: " << First << ". Last entry: " << Last << '\n'
1664 << "Range: " << Range
1665 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001666
1667 // Get the MachineFunction which holds the current MBB. This is used when
1668 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001669 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001670
1671 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001672 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001673 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001674
1675 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1676
1677 // Create a new basic block to hold the code for loading the address
1678 // of the jump table, and jumping to it. Update successor information;
1679 // we will either branch to the default case for the switch, or the jump
1680 // table.
1681 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1682 CurMF->insert(BBI, JumpTableBB);
1683 CR.CaseBB->addSuccessor(Default);
1684 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001686 // Build a vector of destination BBs, corresponding to each target
1687 // of the jump table. If the value of the jump table slot corresponds to
1688 // a case statement, push the case's BB onto the vector, otherwise, push
1689 // the default BB.
1690 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001691 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001692 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001693 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1694 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1695
1696 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001697 DestBBs.push_back(I->BB);
1698 if (TEI==High)
1699 ++I;
1700 } else {
1701 DestBBs.push_back(Default);
1702 }
1703 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001705 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001706 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1707 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001708 E = DestBBs.end(); I != E; ++I) {
1709 if (!SuccsHandled[(*I)->getNumber()]) {
1710 SuccsHandled[(*I)->getNumber()] = true;
1711 JumpTableBB->addSuccessor(*I);
1712 }
1713 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001714
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001715 // Create a jump table index for this jump table, or return an existing
1716 // one.
1717 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001718
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719 // Set the jump table information so that we can codegen it as a second
1720 // MachineBasicBlock
1721 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1722 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1723 if (CR.CaseBB == CurMBB)
1724 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 JTCases.push_back(JumpTableBlock(JTH, JT));
1727
1728 return true;
1729}
1730
1731/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1732/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001733bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1734 CaseRecVector& WorkList,
1735 Value* SV,
1736 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737 // Get the MachineFunction which holds the current MBB. This is used when
1738 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001739 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001740
1741 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001742 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001743 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744
1745 Case& FrontCase = *CR.Range.first;
1746 Case& BackCase = *(CR.Range.second-1);
1747 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1748
1749 // Size is the number of Cases represented by this range.
1750 unsigned Size = CR.Range.second - CR.Range.first;
1751
Chris Lattnere880efe2009-11-07 07:50:34 +00001752 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1753 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001754 double FMetric = 0;
1755 CaseItr Pivot = CR.Range.first + Size/2;
1756
1757 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1758 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001759 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001760 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1761 I!=E; ++I)
1762 TSize += I->size();
1763
Chris Lattnere880efe2009-11-07 07:50:34 +00001764 APInt LSize = FrontCase.size();
1765 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001766 DEBUG(errs() << "Selecting best pivot: \n"
1767 << "First: " << First << ", Last: " << Last <<'\n'
1768 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001769 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1770 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001771 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1772 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001773 APInt Range = ComputeRange(LEnd, RBegin);
1774 assert((Range - 2ULL).isNonNegative() &&
1775 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001776 double LDensity = (double)LSize.roundToDouble() /
1777 (LEnd - First + 1ULL).roundToDouble();
1778 double RDensity = (double)RSize.roundToDouble() /
1779 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001780 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001782 DEBUG(errs() <<"=>Step\n"
1783 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1784 << "LDensity: " << LDensity
1785 << ", RDensity: " << RDensity << '\n'
1786 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787 if (FMetric < Metric) {
1788 Pivot = J;
1789 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001790 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791 }
1792
1793 LSize += J->size();
1794 RSize -= J->size();
1795 }
1796 if (areJTsAllowed(TLI)) {
1797 // If our case is dense we *really* should handle it earlier!
1798 assert((FMetric > 0) && "Should handle dense range earlier!");
1799 } else {
1800 Pivot = CR.Range.first + Size/2;
1801 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001802
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001803 CaseRange LHSR(CR.Range.first, Pivot);
1804 CaseRange RHSR(Pivot, CR.Range.second);
1805 Constant *C = Pivot->Low;
1806 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001809 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001810 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001811 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001812 // Pivot's Value, then we can branch directly to the LHS's Target,
1813 // rather than creating a leaf node for it.
1814 if ((LHSR.second - LHSR.first) == 1 &&
1815 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001816 cast<ConstantInt>(C)->getValue() ==
1817 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001818 TrueBB = LHSR.first->BB;
1819 } else {
1820 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1821 CurMF->insert(BBI, TrueBB);
1822 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001823
1824 // Put SV in a virtual register to make it available from the new blocks.
1825 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001828 // Similar to the optimization above, if the Value being switched on is
1829 // known to be less than the Constant CR.LT, and the current Case Value
1830 // is CR.LT - 1, then we can branch directly to the target block for
1831 // the current Case Value, rather than emitting a RHS leaf node for it.
1832 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001833 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1834 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001835 FalseBB = RHSR.first->BB;
1836 } else {
1837 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1838 CurMF->insert(BBI, FalseBB);
1839 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001840
1841 // Put SV in a virtual register to make it available from the new blocks.
1842 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001843 }
1844
1845 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001846 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001847 // Otherwise, branch to LHS.
1848 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1849
1850 if (CR.CaseBB == CurMBB)
1851 visitSwitchCase(CB);
1852 else
1853 SwitchCases.push_back(CB);
1854
1855 return true;
1856}
1857
1858/// handleBitTestsSwitchCase - if current case range has few destination and
1859/// range span less, than machine word bitwidth, encode case range into series
1860/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001861bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1862 CaseRecVector& WorkList,
1863 Value* SV,
1864 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001865 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001866 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001867
1868 Case& FrontCase = *CR.Range.first;
1869 Case& BackCase = *(CR.Range.second-1);
1870
1871 // Get the MachineFunction which holds the current MBB. This is used when
1872 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001873 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001874
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001875 // If target does not have legal shift left, do not emit bit tests at all.
1876 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1877 return false;
1878
Anton Korobeynikov23218582008-12-23 22:25:27 +00001879 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001880 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1881 I!=E; ++I) {
1882 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001883 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001884 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001885
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 // Count unique destinations
1887 SmallSet<MachineBasicBlock*, 4> Dests;
1888 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1889 Dests.insert(I->BB);
1890 if (Dests.size() > 3)
1891 // Don't bother the code below, if there are too much unique destinations
1892 return false;
1893 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001894 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1895 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001896
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001897 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001898 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1899 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001900 APInt cmpRange = maxValue - minValue;
1901
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001902 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1903 << "Low bound: " << minValue << '\n'
1904 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001905
1906 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001907 (!(Dests.size() == 1 && numCmps >= 3) &&
1908 !(Dests.size() == 2 && numCmps >= 5) &&
1909 !(Dests.size() >= 3 && numCmps >= 6)))
1910 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001911
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001912 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001913 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001915 // Optimize the case where all the case values fit in a
1916 // word without having to subtract minValue. In this case,
1917 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001918 if (minValue.isNonNegative() &&
1919 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1920 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001922 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001923 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001924
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001925 CaseBitsVector CasesBits;
1926 unsigned i, count = 0;
1927
1928 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1929 MachineBasicBlock* Dest = I->BB;
1930 for (i = 0; i < count; ++i)
1931 if (Dest == CasesBits[i].BB)
1932 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934 if (i == count) {
1935 assert((count < 3) && "Too much destinations to test!");
1936 CasesBits.push_back(CaseBits(0, Dest, 0));
1937 count++;
1938 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001939
1940 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1941 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1942
1943 uint64_t lo = (lowValue - lowBound).getZExtValue();
1944 uint64_t hi = (highValue - lowBound).getZExtValue();
1945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 for (uint64_t j = lo; j <= hi; j++) {
1947 CasesBits[i].Mask |= 1ULL << j;
1948 CasesBits[i].Bits++;
1949 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001950
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001951 }
1952 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001953
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001954 BitTestInfo BTC;
1955
1956 // Figure out which block is immediately after the current one.
1957 MachineFunction::iterator BBI = CR.CaseBB;
1958 ++BBI;
1959
1960 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1961
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001962 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001963 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001964 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
1965 << ", Bits: " << CasesBits[i].Bits
1966 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001967
1968 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1969 CurMF->insert(BBI, CaseBB);
1970 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1971 CaseBB,
1972 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001973
1974 // Put SV in a virtual register to make it available from the new blocks.
1975 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001976 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001977
1978 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001979 -1U, (CR.CaseBB == CurMBB),
1980 CR.CaseBB, Default, BTC);
1981
1982 if (CR.CaseBB == CurMBB)
1983 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001985 BitTestCases.push_back(BTB);
1986
1987 return true;
1988}
1989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001991size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1992 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001993 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001994
1995 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001996 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001997 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1998 Cases.push_back(Case(SI.getSuccessorValue(i),
1999 SI.getSuccessorValue(i),
2000 SMBB));
2001 }
2002 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2003
2004 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002005 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002006 // Must recompute end() each iteration because it may be
2007 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002008 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2009 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2010 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002011 MachineBasicBlock* nextBB = J->BB;
2012 MachineBasicBlock* currentBB = I->BB;
2013
2014 // If the two neighboring cases go to the same destination, merge them
2015 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002016 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 I->High = J->High;
2018 J = Cases.erase(J);
2019 } else {
2020 I = J++;
2021 }
2022 }
2023
2024 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2025 if (I->Low != I->High)
2026 // A range counts double, since it requires two compares.
2027 ++numCmps;
2028 }
2029
2030 return numCmps;
2031}
2032
Dan Gohman2048b852009-11-23 18:04:58 +00002033void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 // Figure out which block is immediately after the current one.
2035 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002036 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2037
2038 // If there is only the default destination, branch to it if it is not the
2039 // next basic block. Otherwise, just fall through.
2040 if (SI.getNumOperands() == 2) {
2041 // Update machine-CFG edges.
2042
2043 // If this is not a fall-through branch, emit the branch.
2044 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002045 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002046 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002047 MVT::Other, getControlRoot(),
2048 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002049 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002050
2051 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002052 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002053 }
2054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002055 return;
2056 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002057
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002058 // If there are any non-default case statements, create a vector of Cases
2059 // representing each one, and sort the vector so that we can efficiently
2060 // create a binary search tree from them.
2061 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002062 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002063 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2064 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002065 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002066
2067 // Get the Value to be switched on and default basic blocks, which will be
2068 // inserted into CaseBlock records, representing basic blocks in the binary
2069 // search tree.
2070 Value *SV = SI.getOperand(0);
2071
2072 // Push the initial CaseRec onto the worklist
2073 CaseRecVector WorkList;
2074 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2075
2076 while (!WorkList.empty()) {
2077 // Grab a record representing a case range to process off the worklist
2078 CaseRec CR = WorkList.back();
2079 WorkList.pop_back();
2080
2081 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2082 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002083
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002084 // If the range has few cases (two or less) emit a series of specific
2085 // tests.
2086 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2087 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002088
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002089 // If the switch has more than 5 blocks, and at least 40% dense, and the
2090 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002091 // lowering the switch to a binary tree of conditional branches.
2092 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2093 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002095 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2096 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2097 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2098 }
2099}
2100
Dan Gohman2048b852009-11-23 18:04:58 +00002101void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002102 // Update machine-CFG edges.
2103 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2104 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2105
Bill Wendling49fcff82009-12-21 22:30:11 +00002106 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2107 MVT::Other, getControlRoot(),
2108 getValue(I.getAddress()));
2109 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002110
Bill Wendling49fcff82009-12-21 22:30:11 +00002111 if (DisableScheduling)
2112 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2113}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002114
Dan Gohman2048b852009-11-23 18:04:58 +00002115void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002116 // -0.0 - X --> fneg
2117 const Type *Ty = I.getType();
2118 if (isa<VectorType>(Ty)) {
2119 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2120 const VectorType *DestTy = cast<VectorType>(I.getType());
2121 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002122 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002123 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002124 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002125 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002126 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002127 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2128 Op2.getValueType(), Op2);
2129 setValue(&I, Res);
2130
2131 if (DisableScheduling)
2132 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002134 return;
2135 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002136 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002137 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002138
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002139 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002140 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002141 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002142 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2143 Op2.getValueType(), Op2);
2144 setValue(&I, Res);
2145
2146 if (DisableScheduling)
2147 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2148
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002149 return;
2150 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002152 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002153}
2154
Dan Gohman2048b852009-11-23 18:04:58 +00002155void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002156 SDValue Op1 = getValue(I.getOperand(0));
2157 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002158 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2159 Op1.getValueType(), Op1, Op2);
2160 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002161
Bill Wendling49fcff82009-12-21 22:30:11 +00002162 if (DisableScheduling)
2163 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002164}
2165
Dan Gohman2048b852009-11-23 18:04:58 +00002166void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002167 SDValue Op1 = getValue(I.getOperand(0));
2168 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002169 if (!isa<VectorType>(I.getType()) &&
2170 Op2.getValueType() != TLI.getShiftAmountTy()) {
2171 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002172 EVT PTy = TLI.getPointerTy();
2173 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002174 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002175 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2176 TLI.getShiftAmountTy(), Op2);
2177 // If the operand is larger than the shift count type but the shift
2178 // count type has enough bits to represent any shift value, truncate
2179 // it now. This is a common case and it exposes the truncate to
2180 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002181 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002182 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2183 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2184 TLI.getShiftAmountTy(), Op2);
2185 // Otherwise we'll need to temporarily settle for some other
2186 // convenient type; type legalization will make adjustments as
2187 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002188 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002189 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002190 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002191 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002192 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002193 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002195
Bill Wendling49fcff82009-12-21 22:30:11 +00002196 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2197 Op1.getValueType(), Op1, Op2);
2198 setValue(&I, Res);
2199
Bill Wendling87710f02009-12-21 23:47:40 +00002200 if (DisableScheduling) {
2201 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2202 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002203 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002204 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205}
2206
Dan Gohman2048b852009-11-23 18:04:58 +00002207void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2209 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2210 predicate = IC->getPredicate();
2211 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2212 predicate = ICmpInst::Predicate(IC->getPredicate());
2213 SDValue Op1 = getValue(I.getOperand(0));
2214 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002215 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002216
Owen Andersone50ed302009-08-10 22:56:29 +00002217 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002218 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2219 setValue(&I, Res);
2220
2221 if (DisableScheduling)
2222 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223}
2224
Dan Gohman2048b852009-11-23 18:04:58 +00002225void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2227 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2228 predicate = FC->getPredicate();
2229 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2230 predicate = FCmpInst::Predicate(FC->getPredicate());
2231 SDValue Op1 = getValue(I.getOperand(0));
2232 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002233 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002234 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002235 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2236 setValue(&I, Res);
2237
2238 if (DisableScheduling)
2239 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240}
2241
Dan Gohman2048b852009-11-23 18:04:58 +00002242void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002243 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002244 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2245 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002246 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002247
Bill Wendling49fcff82009-12-21 22:30:11 +00002248 SmallVector<SDValue, 4> Values(NumValues);
2249 SDValue Cond = getValue(I.getOperand(0));
2250 SDValue TrueVal = getValue(I.getOperand(1));
2251 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002252
Bill Wendling49fcff82009-12-21 22:30:11 +00002253 for (unsigned i = 0; i != NumValues; ++i) {
2254 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2255 TrueVal.getNode()->getValueType(i), Cond,
2256 SDValue(TrueVal.getNode(),
2257 TrueVal.getResNo() + i),
2258 SDValue(FalseVal.getNode(),
2259 FalseVal.getResNo() + i));
2260
2261 if (DisableScheduling)
2262 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002263 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002264
Bill Wendling49fcff82009-12-21 22:30:11 +00002265 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2266 DAG.getVTList(&ValueVTs[0], NumValues),
2267 &Values[0], NumValues);
2268 setValue(&I, Res);
2269
2270 if (DisableScheduling)
2271 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2272}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273
Dan Gohman2048b852009-11-23 18:04:58 +00002274void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002275 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2276 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002277 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002278 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2279 setValue(&I, Res);
2280
2281 if (DisableScheduling)
2282 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002283}
2284
Dan Gohman2048b852009-11-23 18:04:58 +00002285void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002286 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2287 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2288 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002289 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002290 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2291 setValue(&I, Res);
2292
2293 if (DisableScheduling)
2294 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295}
2296
Dan Gohman2048b852009-11-23 18:04:58 +00002297void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002298 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2299 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2300 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002301 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002302 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2303 setValue(&I, Res);
2304
2305 if (DisableScheduling)
2306 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307}
2308
Dan Gohman2048b852009-11-23 18:04:58 +00002309void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002310 // FPTrunc is never a no-op cast, no need to check
2311 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002312 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002313 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2314 DestVT, N, DAG.getIntPtrConstant(0));
2315 setValue(&I, Res);
2316
2317 if (DisableScheduling)
2318 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002319}
2320
Dan Gohman2048b852009-11-23 18:04:58 +00002321void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002322 // FPTrunc is never a no-op cast, no need to check
2323 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002324 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002325 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2326 setValue(&I, Res);
2327
2328 if (DisableScheduling)
2329 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002330}
2331
Dan Gohman2048b852009-11-23 18:04:58 +00002332void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002333 // FPToUI is never a no-op cast, no need to check
2334 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002335 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002336 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2337 setValue(&I, Res);
2338
2339 if (DisableScheduling)
2340 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002341}
2342
Dan Gohman2048b852009-11-23 18:04:58 +00002343void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002344 // FPToSI is never a no-op cast, no need to check
2345 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002346 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002347 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2348 setValue(&I, Res);
2349
2350 if (DisableScheduling)
2351 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002352}
2353
Dan Gohman2048b852009-11-23 18:04:58 +00002354void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002355 // UIToFP is never a no-op cast, no need to check
2356 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002357 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002358 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2359 setValue(&I, Res);
2360
2361 if (DisableScheduling)
2362 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002363}
2364
Dan Gohman2048b852009-11-23 18:04:58 +00002365void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002366 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002367 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002368 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002369 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2370 setValue(&I, Res);
2371
2372 if (DisableScheduling)
2373 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002374}
2375
Dan Gohman2048b852009-11-23 18:04:58 +00002376void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002377 // What to do depends on the size of the integer and the size of the pointer.
2378 // We can either truncate, zero extend, or no-op, accordingly.
2379 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002380 EVT SrcVT = N.getValueType();
2381 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002382 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2383 setValue(&I, Res);
2384
2385 if (DisableScheduling)
2386 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002387}
2388
Dan Gohman2048b852009-11-23 18:04:58 +00002389void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002390 // What to do depends on the size of the integer and the size of the pointer.
2391 // We can either truncate, zero extend, or no-op, accordingly.
2392 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002393 EVT SrcVT = N.getValueType();
2394 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002395 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2396 setValue(&I, Res);
2397
2398 if (DisableScheduling)
2399 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002400}
2401
Dan Gohman2048b852009-11-23 18:04:58 +00002402void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002403 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002404 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002405
Bill Wendling49fcff82009-12-21 22:30:11 +00002406 // BitCast assures us that source and destination are the same size so this is
2407 // either a BIT_CONVERT or a no-op.
2408 if (DestVT != N.getValueType()) {
2409 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2410 DestVT, N); // convert types.
2411 setValue(&I, Res);
2412
2413 if (DisableScheduling)
2414 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2415 } else {
2416 setValue(&I, N); // noop cast.
2417 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002418}
2419
Dan Gohman2048b852009-11-23 18:04:58 +00002420void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002421 SDValue InVec = getValue(I.getOperand(0));
2422 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002423 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002424 TLI.getPointerTy(),
2425 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002426 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2427 TLI.getValueType(I.getType()),
2428 InVec, InVal, InIdx);
2429 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002430
Bill Wendling87710f02009-12-21 23:47:40 +00002431 if (DisableScheduling) {
2432 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002433 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002434 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002435}
2436
Dan Gohman2048b852009-11-23 18:04:58 +00002437void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002438 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002439 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002440 TLI.getPointerTy(),
2441 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002442 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2443 TLI.getValueType(I.getType()), InVec, InIdx);
2444 setValue(&I, Res);
2445
Bill Wendling87710f02009-12-21 23:47:40 +00002446 if (DisableScheduling) {
2447 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002448 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002449 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002450}
2451
Mon P Wangaeb06d22008-11-10 04:46:22 +00002452
2453// Utility for visitShuffleVector - Returns true if the mask is mask starting
2454// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002455static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2456 unsigned MaskNumElts = Mask.size();
2457 for (unsigned i = 0; i != MaskNumElts; ++i)
2458 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002459 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002460 return true;
2461}
2462
Dan Gohman2048b852009-11-23 18:04:58 +00002463void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002465 SDValue Src1 = getValue(I.getOperand(0));
2466 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002467
Nate Begeman9008ca62009-04-27 18:41:29 +00002468 // Convert the ConstantVector mask operand into an array of ints, with -1
2469 // representing undef values.
2470 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002471 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2472 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002473 unsigned MaskNumElts = MaskElts.size();
2474 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002475 if (isa<UndefValue>(MaskElts[i]))
2476 Mask.push_back(-1);
2477 else
2478 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2479 }
2480
Owen Andersone50ed302009-08-10 22:56:29 +00002481 EVT VT = TLI.getValueType(I.getType());
2482 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002483 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002484
Mon P Wangc7849c22008-11-16 05:06:27 +00002485 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002486 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2487 &Mask[0]);
2488 setValue(&I, Res);
2489
2490 if (DisableScheduling)
2491 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2492
Mon P Wangaeb06d22008-11-10 04:46:22 +00002493 return;
2494 }
2495
2496 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002497 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2498 // Mask is longer than the source vectors and is a multiple of the source
2499 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002500 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002501 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2502 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002503 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2504 VT, Src1, Src2);
2505 setValue(&I, Res);
2506
2507 if (DisableScheduling)
2508 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2509
Mon P Wangaeb06d22008-11-10 04:46:22 +00002510 return;
2511 }
2512
Mon P Wangc7849c22008-11-16 05:06:27 +00002513 // Pad both vectors with undefs to make them the same length as the mask.
2514 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002515 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2516 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002517 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002518
Nate Begeman9008ca62009-04-27 18:41:29 +00002519 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2520 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002521 MOps1[0] = Src1;
2522 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002523
2524 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2525 getCurDebugLoc(), VT,
2526 &MOps1[0], NumConcat);
2527 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2528 getCurDebugLoc(), VT,
2529 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002530
Mon P Wangaeb06d22008-11-10 04:46:22 +00002531 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002532 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002533 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002534 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002535 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002536 MappedOps.push_back(Idx);
2537 else
2538 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002539 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002540
2541 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2542 &MappedOps[0]);
2543 setValue(&I, Res);
2544
Bill Wendlinge1a90422009-12-21 23:10:19 +00002545 if (DisableScheduling) {
2546 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2547 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002548 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002549 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002550
Mon P Wangaeb06d22008-11-10 04:46:22 +00002551 return;
2552 }
2553
Mon P Wangc7849c22008-11-16 05:06:27 +00002554 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002555 // Analyze the access pattern of the vector to see if we can extract
2556 // two subvectors and do the shuffle. The analysis is done by calculating
2557 // the range of elements the mask access on both vectors.
2558 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2559 int MaxRange[2] = {-1, -1};
2560
Nate Begeman5a5ca152009-04-29 05:20:52 +00002561 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002562 int Idx = Mask[i];
2563 int Input = 0;
2564 if (Idx < 0)
2565 continue;
2566
Nate Begeman5a5ca152009-04-29 05:20:52 +00002567 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002568 Input = 1;
2569 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002570 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002571 if (Idx > MaxRange[Input])
2572 MaxRange[Input] = Idx;
2573 if (Idx < MinRange[Input])
2574 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002575 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002576
Mon P Wangc7849c22008-11-16 05:06:27 +00002577 // Check if the access is smaller than the vector size and can we find
2578 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002579 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002580 int StartIdx[2]; // StartIdx to extract from
2581 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002582 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002583 RangeUse[Input] = 0; // Unused
2584 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002585 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002586 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002587 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002588 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002589 RangeUse[Input] = 1; // Extract from beginning of the vector
2590 StartIdx[Input] = 0;
2591 } else {
2592 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002593 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002594 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002595 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002596 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002597 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002598 }
2599
Bill Wendling636e2582009-08-21 18:16:06 +00002600 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002601 SDValue Res = DAG.getUNDEF(VT);
2602 setValue(&I, Res); // Vectors are not used.
2603
2604 if (DisableScheduling)
2605 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2606
Mon P Wangc7849c22008-11-16 05:06:27 +00002607 return;
2608 }
2609 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2610 // Extract appropriate subvector and generate a vector shuffle
2611 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002612 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002613 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002614 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002615 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002616 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002617 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002618
2619 if (DisableScheduling)
2620 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002621 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002622
Mon P Wangc7849c22008-11-16 05:06:27 +00002623 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002624 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002625 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002626 int Idx = Mask[i];
2627 if (Idx < 0)
2628 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002629 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 MappedOps.push_back(Idx - StartIdx[0]);
2631 else
2632 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002633 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002634
2635 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2636 &MappedOps[0]);
2637 setValue(&I, Res);
2638
2639 if (DisableScheduling)
2640 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2641
Mon P Wangc7849c22008-11-16 05:06:27 +00002642 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002643 }
2644 }
2645
Mon P Wangc7849c22008-11-16 05:06:27 +00002646 // We can't use either concat vectors or extract subvectors so fall back to
2647 // replacing the shuffle with extract and build vector.
2648 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002649 EVT EltVT = VT.getVectorElementType();
2650 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002651 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002652 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002653 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002654 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002655 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002656 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002657 SDValue Res;
2658
Nate Begeman5a5ca152009-04-29 05:20:52 +00002659 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002660 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2661 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002662 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002663 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2664 EltVT, Src2,
2665 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2666
2667 Ops.push_back(Res);
2668
2669 if (DisableScheduling)
2670 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002671 }
2672 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002673
2674 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2675 VT, &Ops[0], Ops.size());
2676 setValue(&I, Res);
2677
2678 if (DisableScheduling)
2679 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002680}
2681
Dan Gohman2048b852009-11-23 18:04:58 +00002682void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002683 const Value *Op0 = I.getOperand(0);
2684 const Value *Op1 = I.getOperand(1);
2685 const Type *AggTy = I.getType();
2686 const Type *ValTy = Op1->getType();
2687 bool IntoUndef = isa<UndefValue>(Op0);
2688 bool FromUndef = isa<UndefValue>(Op1);
2689
2690 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2691 I.idx_begin(), I.idx_end());
2692
Owen Andersone50ed302009-08-10 22:56:29 +00002693 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002694 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002695 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002696 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2697
2698 unsigned NumAggValues = AggValueVTs.size();
2699 unsigned NumValValues = ValValueVTs.size();
2700 SmallVector<SDValue, 4> Values(NumAggValues);
2701
2702 SDValue Agg = getValue(Op0);
2703 SDValue Val = getValue(Op1);
2704 unsigned i = 0;
2705 // Copy the beginning value(s) from the original aggregate.
2706 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002707 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002708 SDValue(Agg.getNode(), Agg.getResNo() + i);
2709 // Copy values from the inserted value(s).
2710 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002711 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002712 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2713 // Copy remaining value(s) from the original aggregate.
2714 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002715 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002716 SDValue(Agg.getNode(), Agg.getResNo() + i);
2717
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002718 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2719 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2720 &Values[0], NumAggValues);
2721 setValue(&I, Res);
2722
2723 if (DisableScheduling)
2724 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002725}
2726
Dan Gohman2048b852009-11-23 18:04:58 +00002727void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002728 const Value *Op0 = I.getOperand(0);
2729 const Type *AggTy = Op0->getType();
2730 const Type *ValTy = I.getType();
2731 bool OutOfUndef = isa<UndefValue>(Op0);
2732
2733 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2734 I.idx_begin(), I.idx_end());
2735
Owen Andersone50ed302009-08-10 22:56:29 +00002736 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002737 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2738
2739 unsigned NumValValues = ValValueVTs.size();
2740 SmallVector<SDValue, 4> Values(NumValValues);
2741
2742 SDValue Agg = getValue(Op0);
2743 // Copy out the selected value(s).
2744 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2745 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002746 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002747 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002748 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002749
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002750 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2751 DAG.getVTList(&ValValueVTs[0], NumValValues),
2752 &Values[0], NumValValues);
2753 setValue(&I, Res);
2754
2755 if (DisableScheduling)
2756 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002757}
2758
Dan Gohman2048b852009-11-23 18:04:58 +00002759void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002760 SDValue N = getValue(I.getOperand(0));
2761 const Type *Ty = I.getOperand(0)->getType();
2762
2763 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2764 OI != E; ++OI) {
2765 Value *Idx = *OI;
2766 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2767 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2768 if (Field) {
2769 // N = N + Offset
2770 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002771 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002772 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002773
2774 if (DisableScheduling)
2775 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002776 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002777
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002778 Ty = StTy->getElementType(Field);
2779 } else {
2780 Ty = cast<SequentialType>(Ty)->getElementType();
2781
2782 // If this is a constant subscript, handle it quickly.
2783 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2784 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002785 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002786 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002787 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002788 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002789 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002790 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002791 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2792 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002793 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002794 else
Evan Chengb1032a82009-02-09 20:54:38 +00002795 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002796
Dale Johannesen66978ee2009-01-31 02:22:37 +00002797 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002798 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002799
2800 if (DisableScheduling) {
2801 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2802 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2803 }
2804
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002805 continue;
2806 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002809 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2810 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002811 SDValue IdxN = getValue(Idx);
2812
2813 // If the index is smaller or larger than intptr_t, truncate or extend
2814 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002815 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002816
2817 // If this is a multiply by a power of two, turn it into a shl
2818 // immediately. This is a very common case.
2819 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002820 if (ElementSize.isPowerOf2()) {
2821 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002822 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002823 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002824 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002826 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002827 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002828 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002829 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002830
2831 if (DisableScheduling)
2832 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002833 }
2834
Scott Michelfdc40a02009-02-17 22:15:04 +00002835 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002836 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002837
2838 if (DisableScheduling)
2839 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002840 }
2841 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002842
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843 setValue(&I, N);
2844}
2845
Dan Gohman2048b852009-11-23 18:04:58 +00002846void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847 // If this is a fixed sized alloca in the entry block of the function,
2848 // allocate it statically on the stack.
2849 if (FuncInfo.StaticAllocaMap.count(&I))
2850 return; // getValue will auto-populate this.
2851
2852 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002853 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854 unsigned Align =
2855 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2856 I.getAlignment());
2857
2858 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002859
2860 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2861 AllocSize,
2862 DAG.getConstant(TySize, AllocSize.getValueType()));
2863
Bill Wendling856ff412009-12-22 00:12:37 +00002864 if (DisableScheduling)
2865 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Chris Lattner0b18e592009-03-17 19:36:00 +00002866
Owen Andersone50ed302009-08-10 22:56:29 +00002867 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002868 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869
Bill Wendling856ff412009-12-22 00:12:37 +00002870 if (DisableScheduling)
2871 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002873 // Handle alignment. If the requested alignment is less than or equal to
2874 // the stack alignment, ignore it. If the size is greater than or equal to
2875 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2876 unsigned StackAlign =
2877 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2878 if (Align <= StackAlign)
2879 Align = 0;
2880
2881 // Round the size of the allocation up to the stack alignment size
2882 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002883 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002884 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002885 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002886 if (DisableScheduling)
2887 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2888
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002889 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002890 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002891 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002892 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002893 if (DisableScheduling)
2894 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002895
2896 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002897 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002898 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002899 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 setValue(&I, DSA);
2901 DAG.setRoot(DSA.getValue(1));
2902
Bill Wendling856ff412009-12-22 00:12:37 +00002903 if (DisableScheduling)
2904 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
2905
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002906 // Inform the Frame Information that we have just allocated a variable-sized
2907 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002908 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002909}
2910
Dan Gohman2048b852009-11-23 18:04:58 +00002911void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002912 const Value *SV = I.getOperand(0);
2913 SDValue Ptr = getValue(SV);
2914
2915 const Type *Ty = I.getType();
2916 bool isVolatile = I.isVolatile();
2917 unsigned Alignment = I.getAlignment();
2918
Owen Andersone50ed302009-08-10 22:56:29 +00002919 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002920 SmallVector<uint64_t, 4> Offsets;
2921 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2922 unsigned NumValues = ValueVTs.size();
2923 if (NumValues == 0)
2924 return;
2925
2926 SDValue Root;
2927 bool ConstantMemory = false;
2928 if (I.isVolatile())
2929 // Serialize volatile loads with other side effects.
2930 Root = getRoot();
2931 else if (AA->pointsToConstantMemory(SV)) {
2932 // Do not serialize (non-volatile) loads of constant memory with anything.
2933 Root = DAG.getEntryNode();
2934 ConstantMemory = true;
2935 } else {
2936 // Do not serialize non-volatile loads against each other.
2937 Root = DAG.getRoot();
2938 }
2939
2940 SmallVector<SDValue, 4> Values(NumValues);
2941 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002942 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002943 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002944 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2945 PtrVT, Ptr,
2946 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002947 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00002948 A, SV, Offsets[i], isVolatile, Alignment);
2949
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002950 Values[i] = L;
2951 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00002952
2953 if (DisableScheduling) {
2954 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
2955 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
2956 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002957 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002958
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002959 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002960 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002961 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002962 if (isVolatile)
2963 DAG.setRoot(Chain);
2964 else
2965 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00002966
2967 if (DisableScheduling)
2968 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002969 }
2970
Bill Wendling856ff412009-12-22 00:12:37 +00002971 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2972 DAG.getVTList(&ValueVTs[0], NumValues),
2973 &Values[0], NumValues);
2974 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002975
Bill Wendling856ff412009-12-22 00:12:37 +00002976 if (DisableScheduling)
2977 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2978}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002979
Dan Gohman2048b852009-11-23 18:04:58 +00002980void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002981 Value *SrcV = I.getOperand(0);
2982 Value *PtrV = I.getOperand(1);
2983
Owen Andersone50ed302009-08-10 22:56:29 +00002984 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002985 SmallVector<uint64_t, 4> Offsets;
2986 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2987 unsigned NumValues = ValueVTs.size();
2988 if (NumValues == 0)
2989 return;
2990
2991 // Get the lowered operands. Note that we do this after
2992 // checking if NumResults is zero, because with zero results
2993 // the operands won't have values in the map.
2994 SDValue Src = getValue(SrcV);
2995 SDValue Ptr = getValue(PtrV);
2996
2997 SDValue Root = getRoot();
2998 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002999 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003000 bool isVolatile = I.isVolatile();
3001 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003002
3003 for (unsigned i = 0; i != NumValues; ++i) {
3004 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3005 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003006 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003007 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003008 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003009
Bill Wendling856ff412009-12-22 00:12:37 +00003010 if (DisableScheduling) {
3011 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3012 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3013 }
3014 }
3015
3016 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3017 MVT::Other, &Chains[0], NumValues);
3018 DAG.setRoot(Res);
3019
3020 if (DisableScheduling)
3021 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003022}
3023
3024/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3025/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003026void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3027 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003028 bool HasChain = !I.doesNotAccessMemory();
3029 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3030
3031 // Build the operand list.
3032 SmallVector<SDValue, 8> Ops;
3033 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3034 if (OnlyLoad) {
3035 // We don't need to serialize loads against other loads.
3036 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003037 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003038 Ops.push_back(getRoot());
3039 }
3040 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003041
3042 // Info is set by getTgtMemInstrinsic
3043 TargetLowering::IntrinsicInfo Info;
3044 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3045
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003046 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003047 if (!IsTgtIntrinsic)
3048 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003049
3050 // Add all operands of the call to the operand list.
3051 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3052 SDValue Op = getValue(I.getOperand(i));
3053 assert(TLI.isTypeLegal(Op.getValueType()) &&
3054 "Intrinsic uses a non-legal type?");
3055 Ops.push_back(Op);
3056 }
3057
Owen Andersone50ed302009-08-10 22:56:29 +00003058 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003059 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3060#ifndef NDEBUG
3061 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3062 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3063 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003064 }
Bob Wilson8d919552009-07-31 22:41:21 +00003065#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003067 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003068 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003069
Bob Wilson8d919552009-07-31 22:41:21 +00003070 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003071
3072 // Create the node.
3073 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003074 if (IsTgtIntrinsic) {
3075 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003076 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003077 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003078 Info.memVT, Info.ptrVal, Info.offset,
3079 Info.align, Info.vol,
3080 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003081 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003082 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003083 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003084 } else if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003085 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003086 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003087 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003088 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003089 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003090 }
3091
3092 if (DisableScheduling)
3093 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003094
3095 if (HasChain) {
3096 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3097 if (OnlyLoad)
3098 PendingLoads.push_back(Chain);
3099 else
3100 DAG.setRoot(Chain);
3101 }
Bill Wendling856ff412009-12-22 00:12:37 +00003102
Owen Anderson1d0be152009-08-13 21:58:54 +00003103 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003104 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003105 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003106 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003107
3108 if (DisableScheduling)
3109 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003110 }
Bill Wendling856ff412009-12-22 00:12:37 +00003111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003112 setValue(&I, Result);
3113 }
3114}
3115
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003116/// GetSignificand - Get the significand and build it into a floating-point
3117/// number with exponent of 1:
3118///
3119/// Op = (Op & 0x007fffff) | 0x3f800000;
3120///
3121/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003122static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003123GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3125 DAG.getConstant(0x007fffff, MVT::i32));
3126 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3127 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003128 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3129
3130 if (DisableScheduling) {
3131 DAG.AssignOrdering(t1.getNode(), Order);
3132 DAG.AssignOrdering(t2.getNode(), Order);
3133 DAG.AssignOrdering(Res.getNode(), Order);
3134 }
3135
3136 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003137}
3138
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003139/// GetExponent - Get the exponent:
3140///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003141/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003142///
3143/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003144static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003145GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003146 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3148 DAG.getConstant(0x7f800000, MVT::i32));
3149 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003150 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003151 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3152 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003153 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3154
3155 if (DisableScheduling) {
3156 DAG.AssignOrdering(t0.getNode(), Order);
3157 DAG.AssignOrdering(t1.getNode(), Order);
3158 DAG.AssignOrdering(t2.getNode(), Order);
3159 DAG.AssignOrdering(Res.getNode(), Order);
3160 }
3161
3162 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003163}
3164
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003165/// getF32Constant - Get 32-bit floating point constant.
3166static SDValue
3167getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003169}
3170
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003171/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003172/// visitIntrinsicCall: I is a call instruction
3173/// Op is the associated NodeType for I
3174const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003175SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003176 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003177 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003178 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003179 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003180 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003181 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003182 getValue(I.getOperand(2)),
3183 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003184 setValue(&I, L);
3185 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003186
3187 if (DisableScheduling)
3188 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3189
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003190 return 0;
3191}
3192
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003193// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003194const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003195SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003196 SDValue Op1 = getValue(I.getOperand(1));
3197 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003198
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003200 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003201
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003202 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003203
3204 if (DisableScheduling)
3205 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3206
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003207 return 0;
3208}
Bill Wendling74c37652008-12-09 22:08:41 +00003209
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003210/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3211/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003212void
Dan Gohman2048b852009-11-23 18:04:58 +00003213SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003214 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003215 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003216
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003218 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3219 SDValue Op = getValue(I.getOperand(1));
3220
3221 // Put the exponent in the right bit position for later addition to the
3222 // final result:
3223 //
3224 // #define LOG2OFe 1.4426950f
3225 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003227 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003229
3230 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3232 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003233
Bill Wendling856ff412009-12-22 00:12:37 +00003234 if (DisableScheduling) {
3235 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3236 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3237 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3238 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3239 }
3240
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003241 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003242 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003243 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003244
Bill Wendling856ff412009-12-22 00:12:37 +00003245 if (DisableScheduling)
3246 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3247
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003248 if (LimitFloatPrecision <= 6) {
3249 // For floating-point precision of 6:
3250 //
3251 // TwoToFractionalPartOfX =
3252 // 0.997535578f +
3253 // (0.735607626f + 0.252464424f * x) * x;
3254 //
3255 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003256 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003257 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003258 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003259 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003260 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3261 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003262 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003263 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003264
3265 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003266 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003267 TwoToFracPartOfX, IntegerPartOfX);
3268
Owen Anderson825b72b2009-08-11 20:47:22 +00003269 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003270
3271 if (DisableScheduling) {
3272 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3273 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3274 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3275 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3276 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3277 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3278 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3279 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003280 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3281 // For floating-point precision of 12:
3282 //
3283 // TwoToFractionalPartOfX =
3284 // 0.999892986f +
3285 // (0.696457318f +
3286 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3287 //
3288 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003290 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003292 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3294 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003295 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003296 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3297 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003298 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003299 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003300
3301 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003303 TwoToFracPartOfX, IntegerPartOfX);
3304
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003306
3307 if (DisableScheduling) {
3308 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3309 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3310 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3311 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3312 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3313 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3314 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3315 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3316 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3317 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003318 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3319 // For floating-point precision of 18:
3320 //
3321 // TwoToFractionalPartOfX =
3322 // 0.999999982f +
3323 // (0.693148872f +
3324 // (0.240227044f +
3325 // (0.554906021e-1f +
3326 // (0.961591928e-2f +
3327 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3328 //
3329 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003331 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003333 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3335 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003336 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3338 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003339 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3341 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003342 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003343 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3344 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003345 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3347 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003348 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003349 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003351
3352 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003354 TwoToFracPartOfX, IntegerPartOfX);
3355
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003357
3358 if (DisableScheduling) {
3359 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3360 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3361 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3362 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3363 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3364 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3365 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3366 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3367 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3368 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3369 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3370 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3371 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3372 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3373 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3374 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003375 }
3376 } else {
3377 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003378 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003379 getValue(I.getOperand(1)).getValueType(),
3380 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003381 if (DisableScheduling)
3382 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003383 }
3384
Dale Johannesen59e577f2008-09-05 18:38:42 +00003385 setValue(&I, result);
3386}
3387
Bill Wendling39150252008-09-09 20:39:27 +00003388/// visitLog - Lower a log intrinsic. Handles the special sequences for
3389/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003390void
Dan Gohman2048b852009-11-23 18:04:58 +00003391SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003392 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003393 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003394
Owen Anderson825b72b2009-08-11 20:47:22 +00003395 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003396 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3397 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003398 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003399
Bill Wendling856ff412009-12-22 00:12:37 +00003400 if (DisableScheduling)
3401 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3402
Bill Wendling39150252008-09-09 20:39:27 +00003403 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003404 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003406 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003407
Bill Wendling856ff412009-12-22 00:12:37 +00003408 if (DisableScheduling)
3409 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3410
Bill Wendling39150252008-09-09 20:39:27 +00003411 // Get the significand and build it into a floating-point number with
3412 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003413 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003414
3415 if (LimitFloatPrecision <= 6) {
3416 // For floating-point precision of 6:
3417 //
3418 // LogofMantissa =
3419 // -1.1609546f +
3420 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003421 //
Bill Wendling39150252008-09-09 20:39:27 +00003422 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003424 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003426 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3428 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003429 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003430
Scott Michelfdc40a02009-02-17 22:15:04 +00003431 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003433
3434 if (DisableScheduling) {
3435 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3436 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3437 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3438 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3439 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3440 }
Bill Wendling39150252008-09-09 20:39:27 +00003441 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3442 // For floating-point precision of 12:
3443 //
3444 // LogOfMantissa =
3445 // -1.7417939f +
3446 // (2.8212026f +
3447 // (-1.4699568f +
3448 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3449 //
3450 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003451 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003454 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3456 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003457 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3459 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003460 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003461 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3462 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003463 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003464
Scott Michelfdc40a02009-02-17 22:15:04 +00003465 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003467
3468 if (DisableScheduling) {
3469 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3470 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3471 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3472 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3473 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3474 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3475 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3476 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3477 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3478 }
Bill Wendling39150252008-09-09 20:39:27 +00003479 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3480 // For floating-point precision of 18:
3481 //
3482 // LogOfMantissa =
3483 // -2.1072184f +
3484 // (4.2372794f +
3485 // (-3.7029485f +
3486 // (2.2781945f +
3487 // (-0.87823314f +
3488 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3489 //
3490 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003492 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003494 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3496 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003497 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3499 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003500 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003501 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3502 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003503 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3505 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003506 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003507 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3508 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003509 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003510
Scott Michelfdc40a02009-02-17 22:15:04 +00003511 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003513
3514 if (DisableScheduling) {
3515 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3516 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3517 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3518 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3519 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3520 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3521 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3522 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3523 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3524 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3525 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3526 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3527 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3528 }
Bill Wendling39150252008-09-09 20:39:27 +00003529 }
3530 } else {
3531 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003532 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003533 getValue(I.getOperand(1)).getValueType(),
3534 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003535
3536 if (DisableScheduling)
3537 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003538 }
3539
Dale Johannesen59e577f2008-09-05 18:38:42 +00003540 setValue(&I, result);
3541}
3542
Bill Wendling3eb59402008-09-09 00:28:24 +00003543/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3544/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003545void
Dan Gohman2048b852009-11-23 18:04:58 +00003546SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003547 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003548 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003549
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003551 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3552 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003554
Bill Wendling856ff412009-12-22 00:12:37 +00003555 if (DisableScheduling)
3556 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3557
Bill Wendling39150252008-09-09 20:39:27 +00003558 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003559 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3560
3561 if (DisableScheduling)
3562 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003563
3564 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003565 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003566 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003567
Bill Wendling3eb59402008-09-09 00:28:24 +00003568 // Different possible minimax approximations of significand in
3569 // floating-point for various degrees of accuracy over [1,2].
3570 if (LimitFloatPrecision <= 6) {
3571 // For floating-point precision of 6:
3572 //
3573 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3574 //
3575 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003576 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003577 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003579 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003580 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3581 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003582 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003583
Scott Michelfdc40a02009-02-17 22:15:04 +00003584 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003585 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003586
3587 if (DisableScheduling) {
3588 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3589 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3590 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3591 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3592 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3593 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003594 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3595 // For floating-point precision of 12:
3596 //
3597 // Log2ofMantissa =
3598 // -2.51285454f +
3599 // (4.07009056f +
3600 // (-2.12067489f +
3601 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003602 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003603 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003605 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003606 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003607 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003608 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3609 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003610 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003611 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3612 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003613 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3615 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003616 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003617
Scott Michelfdc40a02009-02-17 22:15:04 +00003618 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003619 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003620
3621 if (DisableScheduling) {
3622 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3623 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3624 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3625 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3626 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3627 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3628 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3629 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3630 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3631 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003632 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3633 // For floating-point precision of 18:
3634 //
3635 // Log2ofMantissa =
3636 // -3.0400495f +
3637 // (6.1129976f +
3638 // (-5.3420409f +
3639 // (3.2865683f +
3640 // (-1.2669343f +
3641 // (0.27515199f -
3642 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3643 //
3644 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003645 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003646 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003648 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003649 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3650 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003651 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003652 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3653 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003654 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3656 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003657 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003658 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3659 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003660 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3662 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003663 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003664
Scott Michelfdc40a02009-02-17 22:15:04 +00003665 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003666 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003667
3668 if (DisableScheduling) {
3669 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3670 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3671 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3672 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3673 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3674 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3675 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3676 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3677 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3678 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3679 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3680 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3681 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3682 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003683 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003684 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003685 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003686 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003687 getValue(I.getOperand(1)).getValueType(),
3688 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003689
3690 if (DisableScheduling)
3691 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003692 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003693
Dale Johannesen59e577f2008-09-05 18:38:42 +00003694 setValue(&I, result);
3695}
3696
Bill Wendling3eb59402008-09-09 00:28:24 +00003697/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3698/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003699void
Dan Gohman2048b852009-11-23 18:04:58 +00003700SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003701 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003702 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003703
Owen Anderson825b72b2009-08-11 20:47:22 +00003704 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003705 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3706 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003707 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003708
Bill Wendling856ff412009-12-22 00:12:37 +00003709 if (DisableScheduling)
3710 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3711
Bill Wendling39150252008-09-09 20:39:27 +00003712 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003713 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003714 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003715 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003716
Bill Wendling856ff412009-12-22 00:12:37 +00003717 if (DisableScheduling)
3718 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3719
Bill Wendling3eb59402008-09-09 00:28:24 +00003720 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003721 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003722 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003723
3724 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003725 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003726 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003727 // Log10ofMantissa =
3728 // -0.50419619f +
3729 // (0.60948995f - 0.10380950f * x) * x;
3730 //
3731 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003733 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003735 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003736 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3737 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003738 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003739
Scott Michelfdc40a02009-02-17 22:15:04 +00003740 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003741 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003742
3743 if (DisableScheduling) {
3744 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3745 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3746 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3747 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3748 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3749 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003750 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3751 // For floating-point precision of 12:
3752 //
3753 // Log10ofMantissa =
3754 // -0.64831180f +
3755 // (0.91751397f +
3756 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3757 //
3758 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003759 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003760 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003761 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003762 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003763 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3764 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003765 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003766 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3767 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003768 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003769
Scott Michelfdc40a02009-02-17 22:15:04 +00003770 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003771 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003772
3773 if (DisableScheduling) {
3774 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3775 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3776 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3777 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3778 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3779 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3780 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3781 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003782 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003783 // For floating-point precision of 18:
3784 //
3785 // Log10ofMantissa =
3786 // -0.84299375f +
3787 // (1.5327582f +
3788 // (-1.0688956f +
3789 // (0.49102474f +
3790 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3791 //
3792 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003793 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003794 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003795 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003796 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003797 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3798 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003799 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3801 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003802 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003803 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3804 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003805 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003806 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3807 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003808 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003809
Scott Michelfdc40a02009-02-17 22:15:04 +00003810 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003812
3813 if (DisableScheduling) {
3814 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3815 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3816 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3817 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3818 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3819 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3820 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3821 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3822 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3823 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3824 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3825 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003826 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003827 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003828 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003829 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003830 getValue(I.getOperand(1)).getValueType(),
3831 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003832
3833 if (DisableScheduling)
3834 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003835 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003836
Dale Johannesen59e577f2008-09-05 18:38:42 +00003837 setValue(&I, result);
3838}
3839
Bill Wendlinge10c8142008-09-09 22:39:21 +00003840/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3841/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003842void
Dan Gohman2048b852009-11-23 18:04:58 +00003843SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003844 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003845 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003846
Owen Anderson825b72b2009-08-11 20:47:22 +00003847 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003848 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3849 SDValue Op = getValue(I.getOperand(1));
3850
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003852
Bill Wendling856ff412009-12-22 00:12:37 +00003853 if (DisableScheduling)
3854 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3855
Bill Wendlinge10c8142008-09-09 22:39:21 +00003856 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003857 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3858 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003859
3860 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003861 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003862 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003863
Bill Wendling856ff412009-12-22 00:12:37 +00003864 if (DisableScheduling) {
3865 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3866 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3867 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3868 }
3869
Bill Wendlinge10c8142008-09-09 22:39:21 +00003870 if (LimitFloatPrecision <= 6) {
3871 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003872 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003873 // TwoToFractionalPartOfX =
3874 // 0.997535578f +
3875 // (0.735607626f + 0.252464424f * x) * x;
3876 //
3877 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003878 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003879 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003880 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003881 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3883 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003884 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003885 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003886 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003887 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003888
Scott Michelfdc40a02009-02-17 22:15:04 +00003889 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003890 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003891
3892 if (DisableScheduling) {
3893 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3894 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3895 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3896 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3897 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3898 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3899 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3900 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003901 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3902 // For floating-point precision of 12:
3903 //
3904 // TwoToFractionalPartOfX =
3905 // 0.999892986f +
3906 // (0.696457318f +
3907 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3908 //
3909 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003910 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003911 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003912 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003913 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3915 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003916 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3918 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003919 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003921 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003923
Scott Michelfdc40a02009-02-17 22:15:04 +00003924 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003925 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003926
3927 if (DisableScheduling) {
3928 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3929 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3930 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3931 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3932 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3933 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3934 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3935 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3936 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3937 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003938 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3939 // For floating-point precision of 18:
3940 //
3941 // TwoToFractionalPartOfX =
3942 // 0.999999982f +
3943 // (0.693148872f +
3944 // (0.240227044f +
3945 // (0.554906021e-1f +
3946 // (0.961591928e-2f +
3947 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3948 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003950 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003952 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003953 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3954 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003955 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3957 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003958 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003959 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3960 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003961 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3963 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003964 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003965 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3966 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003967 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003968 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003969 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003970 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003971
Scott Michelfdc40a02009-02-17 22:15:04 +00003972 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003973 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003974
3975 if (DisableScheduling) {
3976 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3977 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3978 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3979 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3980 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3981 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3982 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3983 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3984 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3985 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3986 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3987 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3988 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3989 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3990 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3991 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003992 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003993 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003994 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003995 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003996 getValue(I.getOperand(1)).getValueType(),
3997 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003998
3999 if (DisableScheduling)
4000 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004001 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004002
Dale Johannesen601d3c02008-09-05 01:48:15 +00004003 setValue(&I, result);
4004}
4005
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004006/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4007/// limited-precision mode with x == 10.0f.
4008void
Dan Gohman2048b852009-11-23 18:04:58 +00004009SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004010 SDValue result;
4011 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004012 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004013 bool IsExp10 = false;
4014
Owen Anderson825b72b2009-08-11 20:47:22 +00004015 if (getValue(Val).getValueType() == MVT::f32 &&
4016 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004017 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4018 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4019 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4020 APFloat Ten(10.0f);
4021 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4022 }
4023 }
4024 }
4025
4026 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4027 SDValue Op = getValue(I.getOperand(2));
4028
4029 // Put the exponent in the right bit position for later addition to the
4030 // final result:
4031 //
4032 // #define LOG2OF10 3.3219281f
4033 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004034 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004035 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004037
4038 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4040 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004041
Bill Wendling856ff412009-12-22 00:12:37 +00004042 if (DisableScheduling) {
4043 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4044 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4045 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4046 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4047 }
4048
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004049 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004051 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004052
Bill Wendling856ff412009-12-22 00:12:37 +00004053 if (DisableScheduling)
4054 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4055
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004056 if (LimitFloatPrecision <= 6) {
4057 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004058 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004059 // twoToFractionalPartOfX =
4060 // 0.997535578f +
4061 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004062 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004063 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004064 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004065 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004067 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4069 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004070 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004071 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004072 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004073 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004074
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004075 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004076 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004077
4078 if (DisableScheduling) {
4079 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4080 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4081 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4082 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4083 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4084 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4085 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4086 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004087 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4088 // For floating-point precision of 12:
4089 //
4090 // TwoToFractionalPartOfX =
4091 // 0.999892986f +
4092 // (0.696457318f +
4093 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4094 //
4095 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004096 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004097 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004098 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004099 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004100 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4101 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004102 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004103 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4104 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004105 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004107 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004108 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004109
Scott Michelfdc40a02009-02-17 22:15:04 +00004110 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004111 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004112
4113 if (DisableScheduling) {
4114 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4115 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4116 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4117 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4118 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4119 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4120 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4121 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4122 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4123 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004124 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4125 // For floating-point precision of 18:
4126 //
4127 // TwoToFractionalPartOfX =
4128 // 0.999999982f +
4129 // (0.693148872f +
4130 // (0.240227044f +
4131 // (0.554906021e-1f +
4132 // (0.961591928e-2f +
4133 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4134 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004136 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004138 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4140 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004141 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4143 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004144 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4146 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004147 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4149 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004150 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004151 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4152 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004153 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004155 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004157
Scott Michelfdc40a02009-02-17 22:15:04 +00004158 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004160
4161 if (DisableScheduling) {
4162 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4163 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4164 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4165 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4166 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4167 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4168 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4169 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4170 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4171 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4172 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4173 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4174 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4175 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4176 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4177 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004178 }
4179 } else {
4180 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004181 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004182 getValue(I.getOperand(1)).getValueType(),
4183 getValue(I.getOperand(1)),
4184 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004185
4186 if (DisableScheduling)
4187 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004188 }
4189
4190 setValue(&I, result);
4191}
4192
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004193/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4194/// we want to emit this as a call to a named external function, return the name
4195/// otherwise lower it and return null.
4196const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004197SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004198 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004199 switch (Intrinsic) {
4200 default:
4201 // By default, turn this into a target intrinsic node.
4202 visitTargetIntrinsic(I, Intrinsic);
4203 return 0;
4204 case Intrinsic::vastart: visitVAStart(I); return 0;
4205 case Intrinsic::vaend: visitVAEnd(I); return 0;
4206 case Intrinsic::vacopy: visitVACopy(I); return 0;
4207 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004208 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004209 getValue(I.getOperand(1))));
4210 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004211 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004212 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004213 getValue(I.getOperand(1))));
4214 return 0;
4215 case Intrinsic::setjmp:
4216 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
4217 break;
4218 case Intrinsic::longjmp:
4219 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
4220 break;
Chris Lattner824b9582008-11-21 16:42:48 +00004221 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004222 SDValue Op1 = getValue(I.getOperand(1));
4223 SDValue Op2 = getValue(I.getOperand(2));
4224 SDValue Op3 = getValue(I.getOperand(3));
4225 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00004226 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004227 I.getOperand(1), 0, I.getOperand(2), 0));
4228 return 0;
4229 }
Chris Lattner824b9582008-11-21 16:42:48 +00004230 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004231 SDValue Op1 = getValue(I.getOperand(1));
4232 SDValue Op2 = getValue(I.getOperand(2));
4233 SDValue Op3 = getValue(I.getOperand(3));
4234 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00004235 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004236 I.getOperand(1), 0));
4237 return 0;
4238 }
Chris Lattner824b9582008-11-21 16:42:48 +00004239 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004240 SDValue Op1 = getValue(I.getOperand(1));
4241 SDValue Op2 = getValue(I.getOperand(2));
4242 SDValue Op3 = getValue(I.getOperand(3));
4243 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4244
4245 // If the source and destination are known to not be aliases, we can
4246 // lower memmove as memcpy.
4247 uint64_t Size = -1ULL;
4248 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004249 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004250 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4251 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004252 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004253 I.getOperand(1), 0, I.getOperand(2), 0));
4254 return 0;
4255 }
4256
Dale Johannesena04b7572009-02-03 23:04:43 +00004257 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004258 I.getOperand(1), 0, I.getOperand(2), 0));
4259 return 0;
4260 }
Devang Patel70d75ca2009-11-12 19:02:56 +00004261 case Intrinsic::dbg_stoppoint:
4262 case Intrinsic::dbg_region_start:
4263 case Intrinsic::dbg_region_end:
4264 case Intrinsic::dbg_func_start:
4265 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004266 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004267 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00004268 if (OptLevel != CodeGenOpt::None)
4269 // FIXME: Variable debug info is not supported here.
4270 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004271 DwarfWriter *DW = DAG.getDwarfWriter();
4272 if (!DW)
4273 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004274 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
4275 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
4276 return 0;
4277
Devang Patelac1ceb32009-10-09 22:42:28 +00004278 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004279 Value *Address = DI.getAddress();
4280 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4281 Address = BCI->getOperand(0);
4282 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4283 // Don't handle byval struct arguments or VLAs, for example.
4284 if (!AI)
4285 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004286 DenseMap<const AllocaInst*, int>::iterator SI =
4287 FuncInfo.StaticAllocaMap.find(AI);
4288 if (SI == FuncInfo.StaticAllocaMap.end())
4289 return 0; // VLAs.
4290 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004291
Devang Patelac1ceb32009-10-09 22:42:28 +00004292 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00004293 if (MMI) {
4294 MetadataContext &TheMetadata =
4295 DI.getParent()->getContext().getMetadata();
4296 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
4297 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
4298 MMI->setVariableDbgInfo(Variable, FI, Dbg);
4299 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004300 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004301 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004302 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004303 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004304 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004305 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004306 SDValue Ops[1];
4307 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004308 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004309 setValue(&I, Op);
4310 DAG.setRoot(Op.getValue(1));
4311 return 0;
4312 }
4313
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004314 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004315 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004316
Chris Lattner3a5815f2009-09-17 23:54:54 +00004317 if (CurMBB->isLandingPad())
4318 AddCatchInfo(I, MMI, CurMBB);
4319 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004320#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004321 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004322#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004323 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4324 unsigned Reg = TLI.getExceptionSelectorRegister();
4325 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004326 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004327
Chris Lattner3a5815f2009-09-17 23:54:54 +00004328 // Insert the EHSELECTION instruction.
4329 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4330 SDValue Ops[2];
4331 Ops[0] = getValue(I.getOperand(1));
4332 Ops[1] = getRoot();
4333 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4334
4335 DAG.setRoot(Op.getValue(1));
4336
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004337 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004338 return 0;
4339 }
4340
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004341 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004342 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004344 if (MMI) {
4345 // Find the type id for the given typeinfo.
4346 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
4347
4348 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004349 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004350 } else {
4351 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004352 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004353 }
4354
4355 return 0;
4356 }
4357
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004358 case Intrinsic::eh_return_i32:
4359 case Intrinsic::eh_return_i64:
4360 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004361 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004362 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004363 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004364 getControlRoot(),
4365 getValue(I.getOperand(1)),
4366 getValue(I.getOperand(2))));
4367 } else {
4368 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4369 }
4370
4371 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004372 case Intrinsic::eh_unwind_init:
4373 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4374 MMI->setCallsUnwindInit(true);
4375 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004376
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004377 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004378
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004379 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004380 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004381 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4382 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004383
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004384 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004385 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004386 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004387 TLI.getPointerTy()),
4388 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004389 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004390 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004391 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004392 TLI.getPointerTy(),
4393 DAG.getConstant(0,
4394 TLI.getPointerTy())),
4395 Offset));
4396 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004398 case Intrinsic::convertff:
4399 case Intrinsic::convertfsi:
4400 case Intrinsic::convertfui:
4401 case Intrinsic::convertsif:
4402 case Intrinsic::convertuif:
4403 case Intrinsic::convertss:
4404 case Intrinsic::convertsu:
4405 case Intrinsic::convertus:
4406 case Intrinsic::convertuu: {
4407 ISD::CvtCode Code = ISD::CVT_INVALID;
4408 switch (Intrinsic) {
4409 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4410 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4411 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4412 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4413 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4414 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4415 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4416 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4417 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4418 }
Owen Andersone50ed302009-08-10 22:56:29 +00004419 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00004420 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00004421 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00004422 DAG.getValueType(DestVT),
4423 DAG.getValueType(getValue(Op1).getValueType()),
4424 getValue(I.getOperand(2)),
4425 getValue(I.getOperand(3)),
4426 Code));
4427 return 0;
4428 }
4429
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004430 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004431 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004432 getValue(I.getOperand(1)).getValueType(),
4433 getValue(I.getOperand(1))));
4434 return 0;
4435 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004436 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004437 getValue(I.getOperand(1)).getValueType(),
4438 getValue(I.getOperand(1)),
4439 getValue(I.getOperand(2))));
4440 return 0;
4441 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004442 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004443 getValue(I.getOperand(1)).getValueType(),
4444 getValue(I.getOperand(1))));
4445 return 0;
4446 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004447 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004448 getValue(I.getOperand(1)).getValueType(),
4449 getValue(I.getOperand(1))));
4450 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004451 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004452 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004453 return 0;
4454 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004455 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004456 return 0;
4457 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004458 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004459 return 0;
4460 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004461 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004462 return 0;
4463 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004464 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004465 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004466 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004467 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004468 return 0;
4469 case Intrinsic::pcmarker: {
4470 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004471 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004472 return 0;
4473 }
4474 case Intrinsic::readcyclecounter: {
4475 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004476 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004477 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004478 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004479 setValue(&I, Tmp);
4480 DAG.setRoot(Tmp.getValue(1));
4481 return 0;
4482 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004483 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004484 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004485 getValue(I.getOperand(1)).getValueType(),
4486 getValue(I.getOperand(1))));
4487 return 0;
4488 case Intrinsic::cttz: {
4489 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004490 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004491 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004492 setValue(&I, result);
4493 return 0;
4494 }
4495 case Intrinsic::ctlz: {
4496 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004497 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004498 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004499 setValue(&I, result);
4500 return 0;
4501 }
4502 case Intrinsic::ctpop: {
4503 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004504 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004505 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004506 setValue(&I, result);
4507 return 0;
4508 }
4509 case Intrinsic::stacksave: {
4510 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004511 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004512 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004513 setValue(&I, Tmp);
4514 DAG.setRoot(Tmp.getValue(1));
4515 return 0;
4516 }
4517 case Intrinsic::stackrestore: {
4518 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004519 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004520 return 0;
4521 }
Bill Wendling57344502008-11-18 11:01:33 +00004522 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004523 // Emit code into the DAG to store the stack guard onto the stack.
4524 MachineFunction &MF = DAG.getMachineFunction();
4525 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004526 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004527
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004528 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4529 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004530
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004531 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004532 MFI->setStackProtectorIndex(FI);
4533
4534 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4535
4536 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004537 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00004538 PseudoSourceValue::getFixedStack(FI),
4539 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004540 setValue(&I, Result);
4541 DAG.setRoot(Result);
4542 return 0;
4543 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004544 case Intrinsic::objectsize: {
4545 // If we don't know by now, we're never going to know.
4546 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4547
4548 assert(CI && "Non-constant type in __builtin_object_size?");
4549
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004550 SDValue Arg = getValue(I.getOperand(0));
4551 EVT Ty = Arg.getValueType();
4552
Eric Christopher7b5e6172009-10-27 00:52:25 +00004553 if (CI->getZExtValue() < 2)
Mike Stump70e5e682009-11-09 22:28:21 +00004554 setValue(&I, DAG.getConstant(-1ULL, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004555 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004556 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004557 return 0;
4558 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004559 case Intrinsic::var_annotation:
4560 // Discard annotate attributes
4561 return 0;
4562
4563 case Intrinsic::init_trampoline: {
4564 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4565
4566 SDValue Ops[6];
4567 Ops[0] = getRoot();
4568 Ops[1] = getValue(I.getOperand(1));
4569 Ops[2] = getValue(I.getOperand(2));
4570 Ops[3] = getValue(I.getOperand(3));
4571 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4572 Ops[5] = DAG.getSrcValue(F);
4573
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004574 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004575 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004576 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004577
4578 setValue(&I, Tmp);
4579 DAG.setRoot(Tmp.getValue(1));
4580 return 0;
4581 }
4582
4583 case Intrinsic::gcroot:
4584 if (GFI) {
4585 Value *Alloca = I.getOperand(1);
4586 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004587
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4589 GFI->addStackRoot(FI->getIndex(), TypeMap);
4590 }
4591 return 0;
4592
4593 case Intrinsic::gcread:
4594 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004595 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596 return 0;
4597
4598 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004599 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004600 return 0;
4601 }
4602
4603 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004604 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004605 return 0;
4606 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004607
Bill Wendlingef375462008-11-21 02:38:44 +00004608 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004609 return implVisitAluOverflow(I, ISD::UADDO);
4610 case Intrinsic::sadd_with_overflow:
4611 return implVisitAluOverflow(I, ISD::SADDO);
4612 case Intrinsic::usub_with_overflow:
4613 return implVisitAluOverflow(I, ISD::USUBO);
4614 case Intrinsic::ssub_with_overflow:
4615 return implVisitAluOverflow(I, ISD::SSUBO);
4616 case Intrinsic::umul_with_overflow:
4617 return implVisitAluOverflow(I, ISD::UMULO);
4618 case Intrinsic::smul_with_overflow:
4619 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004620
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004621 case Intrinsic::prefetch: {
4622 SDValue Ops[4];
4623 Ops[0] = getRoot();
4624 Ops[1] = getValue(I.getOperand(1));
4625 Ops[2] = getValue(I.getOperand(2));
4626 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004627 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004628 return 0;
4629 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004630
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004631 case Intrinsic::memory_barrier: {
4632 SDValue Ops[6];
4633 Ops[0] = getRoot();
4634 for (int x = 1; x < 6; ++x)
4635 Ops[x] = getValue(I.getOperand(x));
4636
Owen Anderson825b72b2009-08-11 20:47:22 +00004637 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004638 return 0;
4639 }
4640 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004641 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004642 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004643 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004644 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4645 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004646 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004647 getValue(I.getOperand(2)),
4648 getValue(I.getOperand(3)),
4649 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650 setValue(&I, L);
4651 DAG.setRoot(L.getValue(1));
4652 return 0;
4653 }
4654 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004655 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004657 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004658 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004659 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004660 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004661 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004662 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004663 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004664 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004665 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004666 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004667 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004669 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004670 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004671 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004672 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004673 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004674 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004675 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004676
4677 case Intrinsic::invariant_start:
4678 case Intrinsic::lifetime_start:
4679 // Discard region information.
4680 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
4681 return 0;
4682 case Intrinsic::invariant_end:
4683 case Intrinsic::lifetime_end:
4684 // Discard region information.
4685 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004686 }
4687}
4688
Dan Gohman98ca4f22009-08-05 01:29:28 +00004689/// Test if the given instruction is in a position to be optimized
4690/// with a tail-call. This roughly means that it's in a block with
4691/// a return and there's nothing that needs to be scheduled
4692/// between it and the return.
4693///
4694/// This function only tests target-independent requirements.
4695/// For target-dependent requirements, a target should override
4696/// TargetLowering::IsEligibleForTailCallOptimization.
4697///
4698static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004699isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004700 const TargetLowering &TLI) {
4701 const BasicBlock *ExitBB = I->getParent();
4702 const TerminatorInst *Term = ExitBB->getTerminator();
4703 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4704 const Function *F = ExitBB->getParent();
4705
4706 // The block must end in a return statement or an unreachable.
4707 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4708
4709 // If I will have a chain, make sure no other instruction that will have a
4710 // chain interposes between I and the return.
4711 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4712 !I->isSafeToSpeculativelyExecute())
4713 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4714 --BBI) {
4715 if (&*BBI == I)
4716 break;
4717 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4718 !BBI->isSafeToSpeculativelyExecute())
4719 return false;
4720 }
4721
4722 // If the block ends with a void return or unreachable, it doesn't matter
4723 // what the call's return type is.
4724 if (!Ret || Ret->getNumOperands() == 0) return true;
4725
Dan Gohmaned9bab32009-11-14 02:06:30 +00004726 // If the return value is undef, it doesn't matter what the call's
4727 // return type is.
4728 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4729
Dan Gohman98ca4f22009-08-05 01:29:28 +00004730 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004731 // the return. Ignore noalias because it doesn't affect the call sequence.
4732 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4733 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004734 return false;
4735
4736 // Otherwise, make sure the unmodified return value of I is the return value.
4737 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4738 U = dyn_cast<Instruction>(U->getOperand(0))) {
4739 if (!U)
4740 return false;
4741 if (!U->hasOneUse())
4742 return false;
4743 if (U == I)
4744 break;
4745 // Check for a truly no-op truncate.
4746 if (isa<TruncInst>(U) &&
4747 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4748 continue;
4749 // Check for a truly no-op bitcast.
4750 if (isa<BitCastInst>(U) &&
4751 (U->getOperand(0)->getType() == U->getType() ||
4752 (isa<PointerType>(U->getOperand(0)->getType()) &&
4753 isa<PointerType>(U->getType()))))
4754 continue;
4755 // Otherwise it's not a true no-op.
4756 return false;
4757 }
4758
4759 return true;
4760}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004761
Dan Gohman2048b852009-11-23 18:04:58 +00004762void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4763 bool isTailCall,
4764 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004765 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4766 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004767 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4769 unsigned BeginLabel = 0, EndLabel = 0;
4770
4771 TargetLowering::ArgListTy Args;
4772 TargetLowering::ArgListEntry Entry;
4773 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004774
4775 // Check whether the function can return without sret-demotion.
4776 SmallVector<EVT, 4> OutVTs;
4777 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4778 SmallVector<uint64_t, 4> Offsets;
4779 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4780 OutVTs, OutsFlags, TLI, &Offsets);
4781
4782
4783 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4784 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4785
4786 SDValue DemoteStackSlot;
4787
4788 if (!CanLowerReturn) {
4789 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4790 FTy->getReturnType());
4791 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4792 FTy->getReturnType());
4793 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004794 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004795 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4796
4797 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4798 Entry.Node = DemoteStackSlot;
4799 Entry.Ty = StackSlotPtrType;
4800 Entry.isSExt = false;
4801 Entry.isZExt = false;
4802 Entry.isInReg = false;
4803 Entry.isSRet = true;
4804 Entry.isNest = false;
4805 Entry.isByVal = false;
4806 Entry.Alignment = Align;
4807 Args.push_back(Entry);
4808 RetTy = Type::getVoidTy(FTy->getContext());
4809 }
4810
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004811 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004812 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004813 SDValue ArgNode = getValue(*i);
4814 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4815
4816 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004817 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4818 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4819 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4820 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4821 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4822 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004823 Entry.Alignment = CS.getParamAlignment(attrInd);
4824 Args.push_back(Entry);
4825 }
4826
4827 if (LandingPad && MMI) {
4828 // Insert a label before the invoke call to mark the try range. This can be
4829 // used to detect deletion of the invoke via the MachineModuleInfo.
4830 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004831
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004832 // Both PendingLoads and PendingExports must be flushed here;
4833 // this call might not return.
4834 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004835 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4836 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004837 }
4838
Dan Gohman98ca4f22009-08-05 01:29:28 +00004839 // Check if target-independent constraints permit a tail call here.
4840 // Target-dependent constraints are checked within TLI.LowerCallTo.
4841 if (isTailCall &&
4842 !isInTailCallPosition(CS.getInstruction(),
4843 CS.getAttributes().getRetAttributes(),
4844 TLI))
4845 isTailCall = false;
4846
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004847 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004848 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004849 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004850 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004851 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004852 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004853 isTailCall,
4854 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004855 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004856 assert((isTailCall || Result.second.getNode()) &&
4857 "Non-null chain expected with non-tail call!");
4858 assert((Result.second.getNode() || !Result.first.getNode()) &&
4859 "Null value expected with tail call!");
4860 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004861 setValue(CS.getInstruction(), Result.first);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004862 else if (!CanLowerReturn && Result.second.getNode()) {
4863 // The instruction result is the result of loading from the
4864 // hidden sret parameter.
4865 SmallVector<EVT, 1> PVTs;
4866 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4867
4868 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4869 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4870 EVT PtrVT = PVTs[0];
4871 unsigned NumValues = OutVTs.size();
4872 SmallVector<SDValue, 4> Values(NumValues);
4873 SmallVector<SDValue, 4> Chains(NumValues);
4874
4875 for (unsigned i = 0; i < NumValues; ++i) {
4876 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
4877 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, DemoteStackSlot,
4878 DAG.getConstant(Offsets[i], PtrVT)),
4879 NULL, Offsets[i], false, 1);
4880 Values[i] = L;
4881 Chains[i] = L.getValue(1);
4882 }
4883 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4884 MVT::Other, &Chains[0], NumValues);
4885 PendingLoads.push_back(Chain);
4886
4887 setValue(CS.getInstruction(), DAG.getNode(ISD::MERGE_VALUES,
4888 getCurDebugLoc(), DAG.getVTList(&OutVTs[0], NumValues),
4889 &Values[0], NumValues));
4890 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00004891 // As a special case, a null chain means that a tail call has
4892 // been emitted and the DAG root is already updated.
4893 if (Result.second.getNode())
4894 DAG.setRoot(Result.second);
4895 else
4896 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004897
4898 if (LandingPad && MMI) {
4899 // Insert a label at the end of the invoke call to mark the try range. This
4900 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4901 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004902 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4903 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004904
4905 // Inform MachineModuleInfo of range.
4906 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4907 }
4908}
4909
4910
Dan Gohman2048b852009-11-23 18:04:58 +00004911void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004912 const char *RenameFn = 0;
4913 if (Function *F = I.getCalledFunction()) {
4914 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004915 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4916 if (II) {
4917 if (unsigned IID = II->getIntrinsicID(F)) {
4918 RenameFn = visitIntrinsicCall(I, IID);
4919 if (!RenameFn)
4920 return;
4921 }
4922 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004923 if (unsigned IID = F->getIntrinsicID()) {
4924 RenameFn = visitIntrinsicCall(I, IID);
4925 if (!RenameFn)
4926 return;
4927 }
4928 }
4929
4930 // Check for well-known libc/libm calls. If the function is internal, it
4931 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004932 if (!F->hasLocalLinkage() && F->hasName()) {
4933 StringRef Name = F->getName();
4934 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935 if (I.getNumOperands() == 3 && // Basic sanity checks.
4936 I.getOperand(1)->getType()->isFloatingPoint() &&
4937 I.getType() == I.getOperand(1)->getType() &&
4938 I.getType() == I.getOperand(2)->getType()) {
4939 SDValue LHS = getValue(I.getOperand(1));
4940 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004941 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004942 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004943 return;
4944 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004945 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004946 if (I.getNumOperands() == 2 && // Basic sanity checks.
4947 I.getOperand(1)->getType()->isFloatingPoint() &&
4948 I.getType() == I.getOperand(1)->getType()) {
4949 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004950 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004951 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004952 return;
4953 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004954 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004955 if (I.getNumOperands() == 2 && // Basic sanity checks.
4956 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004957 I.getType() == I.getOperand(1)->getType() &&
4958 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004959 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004960 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004961 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004962 return;
4963 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004964 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004965 if (I.getNumOperands() == 2 && // Basic sanity checks.
4966 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004967 I.getType() == I.getOperand(1)->getType() &&
4968 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004969 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004970 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004971 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004972 return;
4973 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004974 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4975 if (I.getNumOperands() == 2 && // Basic sanity checks.
4976 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004977 I.getType() == I.getOperand(1)->getType() &&
4978 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004979 SDValue Tmp = getValue(I.getOperand(1));
4980 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4981 Tmp.getValueType(), Tmp));
4982 return;
4983 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004984 }
4985 }
4986 } else if (isa<InlineAsm>(I.getOperand(0))) {
4987 visitInlineAsm(&I);
4988 return;
4989 }
4990
4991 SDValue Callee;
4992 if (!RenameFn)
4993 Callee = getValue(I.getOperand(0));
4994 else
Bill Wendling056292f2008-09-16 21:48:12 +00004995 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004996
Dan Gohman98ca4f22009-08-05 01:29:28 +00004997 // Check if we can potentially perform a tail call. More detailed
4998 // checking is be done within LowerCallTo, after more information
4999 // about the call is known.
5000 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5001
5002 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005003}
5004
5005
5006/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005007/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005008/// Chain/Flag as the input and updates them for the output Chain/Flag.
5009/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005010SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005011 SDValue &Chain,
5012 SDValue *Flag) const {
5013 // Assemble the legal parts into the final values.
5014 SmallVector<SDValue, 4> Values(ValueVTs.size());
5015 SmallVector<SDValue, 8> Parts;
5016 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5017 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005018 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005019 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005020 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005021
5022 Parts.resize(NumRegs);
5023 for (unsigned i = 0; i != NumRegs; ++i) {
5024 SDValue P;
5025 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00005026 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005027 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005028 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005029 *Flag = P.getValue(2);
5030 }
5031 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005032
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005033 // If the source register was virtual and if we know something about it,
5034 // add an assert node.
5035 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5036 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5037 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5038 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5039 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5040 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005041
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005042 unsigned RegSize = RegisterVT.getSizeInBits();
5043 unsigned NumSignBits = LOI.NumSignBits;
5044 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005046 // FIXME: We capture more information than the dag can represent. For
5047 // now, just use the tightest assertzext/assertsext possible.
5048 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005049 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005050 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005051 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005052 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005053 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005054 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005055 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005056 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005057 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005058 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005059 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005060 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005062 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005063 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005064 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005065 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005066
Owen Anderson825b72b2009-08-11 20:47:22 +00005067 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005068 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005069 RegisterVT, P, DAG.getValueType(FromVT));
5070
5071 }
5072 }
5073 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005074
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005075 Parts[i] = P;
5076 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005077
Scott Michelfdc40a02009-02-17 22:15:04 +00005078 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005079 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005080 Part += NumRegs;
5081 Parts.clear();
5082 }
5083
Dale Johannesen66978ee2009-01-31 02:22:37 +00005084 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00005085 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5086 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005087}
5088
5089/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005090/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005091/// Chain/Flag as the input and updates them for the output Chain/Flag.
5092/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005093void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005094 SDValue &Chain, SDValue *Flag) const {
5095 // Get the list of the values's legal parts.
5096 unsigned NumRegs = Regs.size();
5097 SmallVector<SDValue, 8> Parts(NumRegs);
5098 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005099 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005100 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005101 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005102
Dale Johannesen66978ee2009-01-31 02:22:37 +00005103 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005104 &Parts[Part], NumParts, RegisterVT);
5105 Part += NumParts;
5106 }
5107
5108 // Copy the parts into the registers.
5109 SmallVector<SDValue, 8> Chains(NumRegs);
5110 for (unsigned i = 0; i != NumRegs; ++i) {
5111 SDValue Part;
5112 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00005113 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005114 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005115 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005116 *Flag = Part.getValue(1);
5117 }
5118 Chains[i] = Part.getValue(0);
5119 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005120
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005121 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005122 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005123 // flagged to it. That is the CopyToReg nodes and the user are considered
5124 // a single scheduling unit. If we create a TokenFactor and return it as
5125 // chain, then the TokenFactor is both a predecessor (operand) of the
5126 // user as well as a successor (the TF operands are flagged to the user).
5127 // c1, f1 = CopyToReg
5128 // c2, f2 = CopyToReg
5129 // c3 = TokenFactor c1, c2
5130 // ...
5131 // = op c3, ..., f2
5132 Chain = Chains[NumRegs-1];
5133 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005134 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005135}
5136
5137/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005138/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005139/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005140void RegsForValue::AddInlineAsmOperands(unsigned Code,
5141 bool HasMatching,unsigned MatchingIdx,
5142 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005143 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005144 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005145 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5146 unsigned Flag = Code | (Regs.size() << 3);
5147 if (HasMatching)
5148 Flag |= 0x80000000 | (MatchingIdx << 16);
5149 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005150 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005151 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005152 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005153 for (unsigned i = 0; i != NumRegs; ++i) {
5154 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005155 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00005156 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005157 }
5158}
5159
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005160/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005161/// i.e. it isn't a stack pointer or some other special register, return the
5162/// register class for the register. Otherwise, return null.
5163static const TargetRegisterClass *
5164isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5165 const TargetLowering &TLI,
5166 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005167 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005168 const TargetRegisterClass *FoundRC = 0;
5169 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5170 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005171 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005172
5173 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005174 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005175 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5176 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5177 I != E; ++I) {
5178 if (TLI.isTypeLegal(*I)) {
5179 // If we have already found this register in a different register class,
5180 // choose the one with the largest VT specified. For example, on
5181 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005182 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005183 ThisVT = *I;
5184 break;
5185 }
5186 }
5187 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005188
Owen Anderson825b72b2009-08-11 20:47:22 +00005189 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005190
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005191 // NOTE: This isn't ideal. In particular, this might allocate the
5192 // frame pointer in functions that need it (due to them not being taken
5193 // out of allocation, because a variable sized allocation hasn't been seen
5194 // yet). This is a slight code pessimization, but should still work.
5195 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5196 E = RC->allocation_order_end(MF); I != E; ++I)
5197 if (*I == Reg) {
5198 // We found a matching register class. Keep looking at others in case
5199 // we find one with larger registers that this physreg is also in.
5200 FoundRC = RC;
5201 FoundVT = ThisVT;
5202 break;
5203 }
5204 }
5205 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005206}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005207
5208
5209namespace llvm {
5210/// AsmOperandInfo - This contains information for each constraint that we are
5211/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005212class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005213 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005214public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005215 /// CallOperand - If this is the result output operand or a clobber
5216 /// this is null, otherwise it is the incoming operand to the CallInst.
5217 /// This gets modified as the asm is processed.
5218 SDValue CallOperand;
5219
5220 /// AssignedRegs - If this is a register or register class operand, this
5221 /// contains the set of register corresponding to the operand.
5222 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005223
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005224 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5225 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5226 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005227
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005228 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5229 /// busy in OutputRegs/InputRegs.
5230 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005231 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005232 std::set<unsigned> &InputRegs,
5233 const TargetRegisterInfo &TRI) const {
5234 if (isOutReg) {
5235 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5236 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5237 }
5238 if (isInReg) {
5239 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5240 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5241 }
5242 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005243
Owen Andersone50ed302009-08-10 22:56:29 +00005244 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005245 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005246 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00005247 EVT getCallOperandValEVT(LLVMContext &Context,
5248 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005249 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005250 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005251
Chris Lattner81249c92008-10-17 17:05:25 +00005252 if (isa<BasicBlock>(CallOperandVal))
5253 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005254
Chris Lattner81249c92008-10-17 17:05:25 +00005255 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005256
Chris Lattner81249c92008-10-17 17:05:25 +00005257 // If this is an indirect operand, the operand is a pointer to the
5258 // accessed type.
5259 if (isIndirect)
5260 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005261
Chris Lattner81249c92008-10-17 17:05:25 +00005262 // If OpTy is not a single value, it may be a struct/union that we
5263 // can tile with integers.
5264 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5265 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5266 switch (BitSize) {
5267 default: break;
5268 case 1:
5269 case 8:
5270 case 16:
5271 case 32:
5272 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005273 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005274 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005275 break;
5276 }
5277 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005278
Chris Lattner81249c92008-10-17 17:05:25 +00005279 return TLI.getValueType(OpTy, true);
5280 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005282private:
5283 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5284 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005285 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005286 const TargetRegisterInfo &TRI) {
5287 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5288 Regs.insert(Reg);
5289 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5290 for (; *Aliases; ++Aliases)
5291 Regs.insert(*Aliases);
5292 }
5293};
5294} // end llvm namespace.
5295
5296
5297/// GetRegistersForValue - Assign registers (virtual or physical) for the
5298/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005299/// register allocator to handle the assignment process. However, if the asm
5300/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005301/// allocation. This produces generally horrible, but correct, code.
5302///
5303/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005304/// Input and OutputRegs are the set of already allocated physical registers.
5305///
Dan Gohman2048b852009-11-23 18:04:58 +00005306void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005307GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005308 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005309 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005310 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005311
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 // Compute whether this value requires an input register, an output register,
5313 // or both.
5314 bool isOutReg = false;
5315 bool isInReg = false;
5316 switch (OpInfo.Type) {
5317 case InlineAsm::isOutput:
5318 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005319
5320 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005321 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005322 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005323 break;
5324 case InlineAsm::isInput:
5325 isInReg = true;
5326 isOutReg = false;
5327 break;
5328 case InlineAsm::isClobber:
5329 isOutReg = true;
5330 isInReg = true;
5331 break;
5332 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005333
5334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 MachineFunction &MF = DAG.getMachineFunction();
5336 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005337
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005338 // If this is a constraint for a single physreg, or a constraint for a
5339 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005340 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005341 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5342 OpInfo.ConstraintVT);
5343
5344 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005345 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005346 // If this is a FP input in an integer register (or visa versa) insert a bit
5347 // cast of the input value. More generally, handle any case where the input
5348 // value disagrees with the register class we plan to stick this in.
5349 if (OpInfo.Type == InlineAsm::isInput &&
5350 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005351 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005352 // types are identical size, use a bitcast to convert (e.g. two differing
5353 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005354 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005355 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005356 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005357 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005358 OpInfo.ConstraintVT = RegVT;
5359 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5360 // If the input is a FP value and we want it in FP registers, do a
5361 // bitcast to the corresponding integer type. This turns an f64 value
5362 // into i64, which can be passed with two i32 values on a 32-bit
5363 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005364 RegVT = EVT::getIntegerVT(Context,
5365 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005366 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005367 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005368 OpInfo.ConstraintVT = RegVT;
5369 }
5370 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005371
Owen Anderson23b9b192009-08-12 00:36:31 +00005372 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005373 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005374
Owen Andersone50ed302009-08-10 22:56:29 +00005375 EVT RegVT;
5376 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005377
5378 // If this is a constraint for a specific physical register, like {r17},
5379 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005380 if (unsigned AssignedReg = PhysReg.first) {
5381 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005382 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005383 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005384
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005385 // Get the actual register value type. This is important, because the user
5386 // may have asked for (e.g.) the AX register in i32 type. We need to
5387 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005388 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005389
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005390 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005391 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005392
5393 // If this is an expanded reference, add the rest of the regs to Regs.
5394 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005395 TargetRegisterClass::iterator I = RC->begin();
5396 for (; *I != AssignedReg; ++I)
5397 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005398
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005399 // Already added the first reg.
5400 --NumRegs; ++I;
5401 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005402 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005403 Regs.push_back(*I);
5404 }
5405 }
5406 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5407 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5408 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5409 return;
5410 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005411
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005412 // Otherwise, if this was a reference to an LLVM register class, create vregs
5413 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005414 if (const TargetRegisterClass *RC = PhysReg.second) {
5415 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005416 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005417 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005418
Evan Chengfb112882009-03-23 08:01:15 +00005419 // Create the appropriate number of virtual registers.
5420 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5421 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005422 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005423
Evan Chengfb112882009-03-23 08:01:15 +00005424 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5425 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005426 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005427
5428 // This is a reference to a register class that doesn't directly correspond
5429 // to an LLVM register class. Allocate NumRegs consecutive, available,
5430 // registers from the class.
5431 std::vector<unsigned> RegClassRegs
5432 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5433 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005434
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005435 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5436 unsigned NumAllocated = 0;
5437 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5438 unsigned Reg = RegClassRegs[i];
5439 // See if this register is available.
5440 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5441 (isInReg && InputRegs.count(Reg))) { // Already used.
5442 // Make sure we find consecutive registers.
5443 NumAllocated = 0;
5444 continue;
5445 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005447 // Check to see if this register is allocatable (i.e. don't give out the
5448 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005449 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5450 if (!RC) { // Couldn't allocate this register.
5451 // Reset NumAllocated to make sure we return consecutive registers.
5452 NumAllocated = 0;
5453 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005454 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005455
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005456 // Okay, this register is good, we can use it.
5457 ++NumAllocated;
5458
5459 // If we allocated enough consecutive registers, succeed.
5460 if (NumAllocated == NumRegs) {
5461 unsigned RegStart = (i-NumAllocated)+1;
5462 unsigned RegEnd = i+1;
5463 // Mark all of the allocated registers used.
5464 for (unsigned i = RegStart; i != RegEnd; ++i)
5465 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005466
5467 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468 OpInfo.ConstraintVT);
5469 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5470 return;
5471 }
5472 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 // Otherwise, we couldn't allocate enough registers for this.
5475}
5476
Evan Chengda43bcf2008-09-24 00:05:32 +00005477/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5478/// processed uses a memory 'm' constraint.
5479static bool
5480hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005481 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005482 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5483 InlineAsm::ConstraintInfo &CI = CInfos[i];
5484 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5485 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5486 if (CType == TargetLowering::C_Memory)
5487 return true;
5488 }
Chris Lattner6c147292009-04-30 00:48:50 +00005489
5490 // Indirect operand accesses access memory.
5491 if (CI.isIndirect)
5492 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005493 }
5494
5495 return false;
5496}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005497
5498/// visitInlineAsm - Handle a call to an InlineAsm object.
5499///
Dan Gohman2048b852009-11-23 18:04:58 +00005500void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005501 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5502
5503 /// ConstraintOperands - Information about all of the constraints.
5504 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005506 std::set<unsigned> OutputRegs, InputRegs;
5507
5508 // Do a prepass over the constraints, canonicalizing them, and building up the
5509 // ConstraintOperands list.
5510 std::vector<InlineAsm::ConstraintInfo>
5511 ConstraintInfos = IA->ParseConstraints();
5512
Evan Chengda43bcf2008-09-24 00:05:32 +00005513 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005514
5515 SDValue Chain, Flag;
5516
5517 // We won't need to flush pending loads if this asm doesn't touch
5518 // memory and is nonvolatile.
5519 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005520 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005521 else
5522 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005523
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005524 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5525 unsigned ResNo = 0; // ResNo - The result number of the next output.
5526 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5527 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5528 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005529
Owen Anderson825b72b2009-08-11 20:47:22 +00005530 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005531
5532 // Compute the value type for each operand.
5533 switch (OpInfo.Type) {
5534 case InlineAsm::isOutput:
5535 // Indirect outputs just consume an argument.
5536 if (OpInfo.isIndirect) {
5537 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5538 break;
5539 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 // The return value of the call is this value. As such, there is no
5542 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005543 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5544 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005545 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5546 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5547 } else {
5548 assert(ResNo == 0 && "Asm only has one result!");
5549 OpVT = TLI.getValueType(CS.getType());
5550 }
5551 ++ResNo;
5552 break;
5553 case InlineAsm::isInput:
5554 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5555 break;
5556 case InlineAsm::isClobber:
5557 // Nothing to do.
5558 break;
5559 }
5560
5561 // If this is an input or an indirect output, process the call argument.
5562 // BasicBlocks are labels, currently appearing only in asm's.
5563 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005564 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005565 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5566
Chris Lattner81249c92008-10-17 17:05:25 +00005567 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005568 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005569 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005570 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005571 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005572
Owen Anderson1d0be152009-08-13 21:58:54 +00005573 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005574 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005575
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005576 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005577 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005578
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005579 // Second pass over the constraints: compute which constraint option to use
5580 // and assign registers to constraints that want a specific physreg.
5581 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5582 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005583
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005584 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005585 // matching input. If their types mismatch, e.g. one is an integer, the
5586 // other is floating point, or their sizes are different, flag it as an
5587 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005588 if (OpInfo.hasMatchingInput()) {
5589 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5590 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005591 if ((OpInfo.ConstraintVT.isInteger() !=
5592 Input.ConstraintVT.isInteger()) ||
5593 (OpInfo.ConstraintVT.getSizeInBits() !=
5594 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005595 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005596 " with a matching output constraint of incompatible"
5597 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005598 }
5599 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005600 }
5601 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005602
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005603 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005604 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005605
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005606 // If this is a memory input, and if the operand is not indirect, do what we
5607 // need to to provide an address for the memory input.
5608 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5609 !OpInfo.isIndirect) {
5610 assert(OpInfo.Type == InlineAsm::isInput &&
5611 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005613 // Memory operands really want the address of the value. If we don't have
5614 // an indirect input, put it in the constpool if we can, otherwise spill
5615 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005616
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005617 // If the operand is a float, integer, or vector constant, spill to a
5618 // constant pool entry to get its address.
5619 Value *OpVal = OpInfo.CallOperandVal;
5620 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5621 isa<ConstantVector>(OpVal)) {
5622 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5623 TLI.getPointerTy());
5624 } else {
5625 // Otherwise, create a stack slot and emit a store to it before the
5626 // asm.
5627 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005628 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005629 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5630 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005631 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005632 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005633 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005634 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005635 OpInfo.CallOperand = StackSlot;
5636 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005637
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005638 // There is no longer a Value* corresponding to this operand.
5639 OpInfo.CallOperandVal = 0;
5640 // It is now an indirect operand.
5641 OpInfo.isIndirect = true;
5642 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005643
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005644 // If this constraint is for a specific register, allocate it before
5645 // anything else.
5646 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005647 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005648 }
5649 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005650
5651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005653 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5655 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005656
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005657 // C_Register operands have already been allocated, Other/Memory don't need
5658 // to be.
5659 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005660 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005661 }
5662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005663 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5664 std::vector<SDValue> AsmNodeOperands;
5665 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5666 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005667 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005668
5669
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005670 // Loop over all of the inputs, copying the operand values into the
5671 // appropriate registers and processing the output regs.
5672 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005673
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005674 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5675 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005676
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5678 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5679
5680 switch (OpInfo.Type) {
5681 case InlineAsm::isOutput: {
5682 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5683 OpInfo.ConstraintType != TargetLowering::C_Register) {
5684 // Memory output, or 'other' output (e.g. 'X' constraint).
5685 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5686
5687 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005688 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5689 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005690 TLI.getPointerTy()));
5691 AsmNodeOperands.push_back(OpInfo.CallOperand);
5692 break;
5693 }
5694
5695 // Otherwise, this is a register or register class output.
5696
5697 // Copy the output from the appropriate register. Find a register that
5698 // we can use.
5699 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005700 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005701 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005702 }
5703
5704 // If this is an indirect operand, store through the pointer after the
5705 // asm.
5706 if (OpInfo.isIndirect) {
5707 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5708 OpInfo.CallOperandVal));
5709 } else {
5710 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005711 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5712 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005713 // Concatenate this output onto the outputs list.
5714 RetValRegs.append(OpInfo.AssignedRegs);
5715 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005716
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005717 // Add information to the INLINEASM node to know that this register is
5718 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005719 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5720 6 /* EARLYCLOBBER REGDEF */ :
5721 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005722 false,
5723 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005724 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005725 break;
5726 }
5727 case InlineAsm::isInput: {
5728 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005729
Chris Lattner6bdcda32008-10-17 16:47:46 +00005730 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005731 // If this is required to match an output register we have already set,
5732 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005733 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005734
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005735 // Scan until we find the definition we already emitted of this operand.
5736 // When we find it, create a RegsForValue operand.
5737 unsigned CurOp = 2; // The first operand.
5738 for (; OperandNo; --OperandNo) {
5739 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005740 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005741 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005742 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5743 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5744 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005745 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005746 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005747 }
5748
Evan Cheng697cbbf2009-03-20 18:03:34 +00005749 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005750 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005751 if ((OpFlag & 7) == 2 /*REGDEF*/
5752 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5753 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005754 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005755 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005756 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005757 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005758 RegsForValue MatchedRegs;
5759 MatchedRegs.TLI = &TLI;
5760 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005761 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005762 MatchedRegs.RegVTs.push_back(RegVT);
5763 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005764 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005765 i != e; ++i)
5766 MatchedRegs.Regs.
5767 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005768
5769 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005770 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5771 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005772 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5773 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005774 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005775 break;
5776 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005777 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5778 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5779 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005780 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005781 // See InlineAsm.h isUseOperandTiedToDef.
5782 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005783 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005784 TLI.getPointerTy()));
5785 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5786 break;
5787 }
5788 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005789
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005790 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005791 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005792 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005793
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005794 std::vector<SDValue> Ops;
5795 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005796 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005797 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005798 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005799 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005800 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 // Add information to the INLINEASM node to know about this input.
5803 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005804 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005805 TLI.getPointerTy()));
5806 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5807 break;
5808 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5809 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5810 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5811 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005812
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005813 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005814 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5815 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005816 TLI.getPointerTy()));
5817 AsmNodeOperands.push_back(InOperandVal);
5818 break;
5819 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005821 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5822 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5823 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005824 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005825 "Don't know how to handle indirect register inputs yet!");
5826
5827 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005828 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005829 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005830 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005831 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005832
Dale Johannesen66978ee2009-01-31 02:22:37 +00005833 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5834 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005835
Evan Cheng697cbbf2009-03-20 18:03:34 +00005836 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005837 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005838 break;
5839 }
5840 case InlineAsm::isClobber: {
5841 // Add the clobbered value to the operand list, so that the register
5842 // allocator is aware that the physreg got clobbered.
5843 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005844 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005845 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005846 break;
5847 }
5848 }
5849 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005850
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005851 // Finish up input operands.
5852 AsmNodeOperands[0] = Chain;
5853 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005854
Dale Johannesen66978ee2009-01-31 02:22:37 +00005855 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005856 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005857 &AsmNodeOperands[0], AsmNodeOperands.size());
5858 Flag = Chain.getValue(1);
5859
5860 // If this asm returns a register value, copy the result from that register
5861 // and set it as the value of the call.
5862 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005863 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005864 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005865
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005866 // FIXME: Why don't we do this for inline asms with MRVs?
5867 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005868 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005869
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005870 // If any of the results of the inline asm is a vector, it may have the
5871 // wrong width/num elts. This can happen for register classes that can
5872 // contain multiple different value types. The preg or vreg allocated may
5873 // not have the same VT as was expected. Convert it to the right type
5874 // with bit_convert.
5875 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005876 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005877 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005878
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005879 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005880 ResultType.isInteger() && Val.getValueType().isInteger()) {
5881 // If a result value was tied to an input value, the computed result may
5882 // have a wider width than the expected result. Extract the relevant
5883 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005884 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005885 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005886
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005887 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005888 }
Dan Gohman95915732008-10-18 01:03:45 +00005889
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005890 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005891 // Don't need to use this as a chain in this case.
5892 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5893 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005894 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005895
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005896 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005898 // Process indirect outputs, first output all of the flagged copies out of
5899 // physregs.
5900 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5901 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5902 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005903 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5904 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005905 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005906
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005907 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005908
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005909 // Emit the non-flagged stores from the physregs.
5910 SmallVector<SDValue, 8> OutChains;
5911 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005912 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005913 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005914 getValue(StoresToEmit[i].second),
5915 StoresToEmit[i].second, 0));
5916 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005917 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005918 &OutChains[0], OutChains.size());
5919 DAG.setRoot(Chain);
5920}
5921
Dan Gohman2048b852009-11-23 18:04:58 +00005922void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005923 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005924 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005925 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005926 DAG.getSrcValue(I.getOperand(1))));
5927}
5928
Dan Gohman2048b852009-11-23 18:04:58 +00005929void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005930 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5931 getRoot(), getValue(I.getOperand(0)),
5932 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005933 setValue(&I, V);
5934 DAG.setRoot(V.getValue(1));
5935}
5936
Dan Gohman2048b852009-11-23 18:04:58 +00005937void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005938 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005939 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005940 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005941 DAG.getSrcValue(I.getOperand(1))));
5942}
5943
Dan Gohman2048b852009-11-23 18:04:58 +00005944void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005945 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005946 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005947 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005948 getValue(I.getOperand(2)),
5949 DAG.getSrcValue(I.getOperand(1)),
5950 DAG.getSrcValue(I.getOperand(2))));
5951}
5952
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005953/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005954/// implementation, which just calls LowerCall.
5955/// FIXME: When all targets are
5956/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005957std::pair<SDValue, SDValue>
5958TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5959 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005960 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005961 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005962 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005963 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005964 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005965
Dan Gohman1937e2f2008-09-16 01:42:28 +00005966 assert((!isTailCall || PerformTailCallOpt) &&
5967 "isTailCall set when tail-call optimizations are disabled!");
5968
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005970 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005971 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005972 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005973 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5974 for (unsigned Value = 0, NumValues = ValueVTs.size();
5975 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005976 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005977 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005978 SDValue Op = SDValue(Args[i].Node.getNode(),
5979 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005980 ISD::ArgFlagsTy Flags;
5981 unsigned OriginalAlignment =
5982 getTargetData()->getABITypeAlignment(ArgTy);
5983
5984 if (Args[i].isZExt)
5985 Flags.setZExt();
5986 if (Args[i].isSExt)
5987 Flags.setSExt();
5988 if (Args[i].isInReg)
5989 Flags.setInReg();
5990 if (Args[i].isSRet)
5991 Flags.setSRet();
5992 if (Args[i].isByVal) {
5993 Flags.setByVal();
5994 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5995 const Type *ElementTy = Ty->getElementType();
5996 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005997 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005998 // For ByVal, alignment should come from FE. BE will guess if this
5999 // info is not there but there are cases it cannot get right.
6000 if (Args[i].Alignment)
6001 FrameAlign = Args[i].Alignment;
6002 Flags.setByValAlign(FrameAlign);
6003 Flags.setByValSize(FrameSize);
6004 }
6005 if (Args[i].isNest)
6006 Flags.setNest();
6007 Flags.setOrigAlign(OriginalAlignment);
6008
Owen Anderson23b9b192009-08-12 00:36:31 +00006009 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6010 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006011 SmallVector<SDValue, 4> Parts(NumParts);
6012 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6013
6014 if (Args[i].isSExt)
6015 ExtendKind = ISD::SIGN_EXTEND;
6016 else if (Args[i].isZExt)
6017 ExtendKind = ISD::ZERO_EXTEND;
6018
Dale Johannesen66978ee2009-01-31 02:22:37 +00006019 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006020
Dan Gohman98ca4f22009-08-05 01:29:28 +00006021 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006022 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006023 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6024 if (NumParts > 1 && j == 0)
6025 MyFlags.Flags.setSplit();
6026 else if (j != 0)
6027 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006028
Dan Gohman98ca4f22009-08-05 01:29:28 +00006029 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006030 }
6031 }
6032 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006033
Dan Gohman98ca4f22009-08-05 01:29:28 +00006034 // Handle the incoming return values from the call.
6035 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006036 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006037 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006038 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006039 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006040 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6041 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006042 for (unsigned i = 0; i != NumRegs; ++i) {
6043 ISD::InputArg MyFlags;
6044 MyFlags.VT = RegisterVT;
6045 MyFlags.Used = isReturnValueUsed;
6046 if (RetSExt)
6047 MyFlags.Flags.setSExt();
6048 if (RetZExt)
6049 MyFlags.Flags.setZExt();
6050 if (isInreg)
6051 MyFlags.Flags.setInReg();
6052 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006053 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006054 }
6055
Dan Gohman98ca4f22009-08-05 01:29:28 +00006056 // Check if target-dependent constraints permit a tail call here.
6057 // Target-independent constraints should be checked by the caller.
6058 if (isTailCall &&
6059 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6060 isTailCall = false;
6061
6062 SmallVector<SDValue, 4> InVals;
6063 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6064 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006065
6066 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006067 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006068 "LowerCall didn't return a valid chain!");
6069 assert((!isTailCall || InVals.empty()) &&
6070 "LowerCall emitted a return value for a tail call!");
6071 assert((isTailCall || InVals.size() == Ins.size()) &&
6072 "LowerCall didn't emit the correct number of values!");
6073 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6074 assert(InVals[i].getNode() &&
6075 "LowerCall emitted a null value!");
6076 assert(Ins[i].VT == InVals[i].getValueType() &&
6077 "LowerCall emitted a value with the wrong type!");
6078 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006079
6080 // For a tail call, the return value is merely live-out and there aren't
6081 // any nodes in the DAG representing it. Return a special value to
6082 // indicate that a tail call has been emitted and no more Instructions
6083 // should be processed in the current block.
6084 if (isTailCall) {
6085 DAG.setRoot(Chain);
6086 return std::make_pair(SDValue(), SDValue());
6087 }
6088
6089 // Collect the legal value parts into potentially illegal values
6090 // that correspond to the original function's return values.
6091 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6092 if (RetSExt)
6093 AssertOp = ISD::AssertSext;
6094 else if (RetZExt)
6095 AssertOp = ISD::AssertZext;
6096 SmallVector<SDValue, 4> ReturnValues;
6097 unsigned CurReg = 0;
6098 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006099 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006100 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6101 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006102
6103 SDValue ReturnValue =
6104 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
6105 AssertOp);
6106 ReturnValues.push_back(ReturnValue);
6107 CurReg += NumRegs;
6108 }
6109
6110 // For a function returning void, there is no return value. We can't create
6111 // such a node, so we just return a null return value in that case. In
6112 // that case, nothing will actualy look at the value.
6113 if (ReturnValues.empty())
6114 return std::make_pair(SDValue(), Chain);
6115
6116 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6117 DAG.getVTList(&RetTys[0], RetTys.size()),
6118 &ReturnValues[0], ReturnValues.size());
6119
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006120 return std::make_pair(Res, Chain);
6121}
6122
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006123void TargetLowering::LowerOperationWrapper(SDNode *N,
6124 SmallVectorImpl<SDValue> &Results,
6125 SelectionDAG &DAG) {
6126 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006127 if (Res.getNode())
6128 Results.push_back(Res);
6129}
6130
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006131SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006132 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006133 return SDValue();
6134}
6135
6136
Dan Gohman2048b852009-11-23 18:04:58 +00006137void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006138 SDValue Op = getValue(V);
6139 assert((Op.getOpcode() != ISD::CopyFromReg ||
6140 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6141 "Copy from a reg to the same reg!");
6142 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6143
Owen Anderson23b9b192009-08-12 00:36:31 +00006144 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00006146 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006147 PendingExports.push_back(Chain);
6148}
6149
6150#include "llvm/CodeGen/SelectionDAGISel.h"
6151
Dan Gohman8c2b5252009-10-30 01:27:03 +00006152void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006153 // If this is the entry block, emit arguments.
6154 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006155 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006156 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006157 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006158 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006159 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006160
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006161 // Check whether the function can return without sret-demotion.
6162 SmallVector<EVT, 4> OutVTs;
6163 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006164 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
6165 OutVTs, OutsFlags, TLI);
6166 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6167
6168 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
6169 OutVTs, OutsFlags, DAG);
6170 if (!FLI.CanLowerReturn) {
6171 // Put in an sret pointer parameter before all the other parameters.
6172 SmallVector<EVT, 1> ValueVTs;
6173 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6174
6175 // NOTE: Assuming that a pointer will never break down to more than one VT
6176 // or one register.
6177 ISD::ArgFlagsTy Flags;
6178 Flags.setSRet();
6179 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6180 ISD::InputArg RetArg(Flags, RegisterVT, true);
6181 Ins.push_back(RetArg);
6182 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006183
Dan Gohman98ca4f22009-08-05 01:29:28 +00006184 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006185 unsigned Idx = 1;
6186 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6187 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006188 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006189 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6190 bool isArgValueUsed = !I->use_empty();
6191 for (unsigned Value = 0, NumValues = ValueVTs.size();
6192 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006193 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006194 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006195 ISD::ArgFlagsTy Flags;
6196 unsigned OriginalAlignment =
6197 TD->getABITypeAlignment(ArgTy);
6198
6199 if (F.paramHasAttr(Idx, Attribute::ZExt))
6200 Flags.setZExt();
6201 if (F.paramHasAttr(Idx, Attribute::SExt))
6202 Flags.setSExt();
6203 if (F.paramHasAttr(Idx, Attribute::InReg))
6204 Flags.setInReg();
6205 if (F.paramHasAttr(Idx, Attribute::StructRet))
6206 Flags.setSRet();
6207 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6208 Flags.setByVal();
6209 const PointerType *Ty = cast<PointerType>(I->getType());
6210 const Type *ElementTy = Ty->getElementType();
6211 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6212 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6213 // For ByVal, alignment should be passed from FE. BE will guess if
6214 // this info is not there but there are cases it cannot get right.
6215 if (F.getParamAlignment(Idx))
6216 FrameAlign = F.getParamAlignment(Idx);
6217 Flags.setByValAlign(FrameAlign);
6218 Flags.setByValSize(FrameSize);
6219 }
6220 if (F.paramHasAttr(Idx, Attribute::Nest))
6221 Flags.setNest();
6222 Flags.setOrigAlign(OriginalAlignment);
6223
Owen Anderson23b9b192009-08-12 00:36:31 +00006224 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6225 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006226 for (unsigned i = 0; i != NumRegs; ++i) {
6227 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6228 if (NumRegs > 1 && i == 0)
6229 MyFlags.Flags.setSplit();
6230 // if it isn't first piece, alignment must be 1
6231 else if (i > 0)
6232 MyFlags.Flags.setOrigAlign(1);
6233 Ins.push_back(MyFlags);
6234 }
6235 }
6236 }
6237
6238 // Call the target to set up the argument values.
6239 SmallVector<SDValue, 8> InVals;
6240 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6241 F.isVarArg(), Ins,
6242 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006243
6244 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006245 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006246 "LowerFormalArguments didn't return a valid chain!");
6247 assert(InVals.size() == Ins.size() &&
6248 "LowerFormalArguments didn't emit the correct number of values!");
6249 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6250 assert(InVals[i].getNode() &&
6251 "LowerFormalArguments emitted a null value!");
6252 assert(Ins[i].VT == InVals[i].getValueType() &&
6253 "LowerFormalArguments emitted a value with the wrong type!");
6254 });
6255
6256 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006257 DAG.setRoot(NewRoot);
6258
6259 // Set up the argument values.
6260 unsigned i = 0;
6261 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006262 if (!FLI.CanLowerReturn) {
6263 // Create a virtual register for the sret pointer, and put in a copy
6264 // from the sret argument into it.
6265 SmallVector<EVT, 1> ValueVTs;
6266 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6267 EVT VT = ValueVTs[0];
6268 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6269 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6270 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT,
6271 VT, AssertOp);
6272
Dan Gohman2048b852009-11-23 18:04:58 +00006273 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006274 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6275 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6276 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00006277 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006278 DAG.setRoot(NewRoot);
6279
6280 // i indexes lowered arguments. Bump it past the hidden sret argument.
6281 // Idx indexes LLVM arguments. Don't touch it.
6282 ++i;
6283 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00006284 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6285 ++I, ++Idx) {
6286 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006287 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006288 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006289 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006290 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006291 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006292 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6293 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006294
6295 if (!I->use_empty()) {
6296 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6297 if (F.paramHasAttr(Idx, Attribute::SExt))
6298 AssertOp = ISD::AssertSext;
6299 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6300 AssertOp = ISD::AssertZext;
6301
6302 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
6303 PartVT, VT, AssertOp));
6304 }
6305 i += NumParts;
6306 }
6307 if (!I->use_empty()) {
Dan Gohman2048b852009-11-23 18:04:58 +00006308 SDB->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
6309 SDB->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006310 // If this argument is live outside of the entry block, insert a copy from
6311 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006312 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006313 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006314 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00006315 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006316
6317 // Finally, if the target has anything special to do, allow it to do so.
6318 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006319 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006320}
6321
6322/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6323/// ensure constants are generated when needed. Remember the virtual registers
6324/// that need to be added to the Machine PHI nodes as input. We cannot just
6325/// directly add them, because expansion might result in multiple MBB's for one
6326/// BB. As such, the start of the BB might correspond to a different MBB than
6327/// the end.
6328///
6329void
6330SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6331 TerminatorInst *TI = LLVMBB->getTerminator();
6332
6333 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6334
6335 // Check successor nodes' PHI nodes that expect a constant to be available
6336 // from this block.
6337 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6338 BasicBlock *SuccBB = TI->getSuccessor(succ);
6339 if (!isa<PHINode>(SuccBB->begin())) continue;
6340 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006341
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006342 // If this terminator has multiple identical successors (common for
6343 // switches), only handle each succ once.
6344 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006345
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006346 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6347 PHINode *PN;
6348
6349 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6350 // nodes and Machine PHI nodes, but the incoming operands have not been
6351 // emitted yet.
6352 for (BasicBlock::iterator I = SuccBB->begin();
6353 (PN = dyn_cast<PHINode>(I)); ++I) {
6354 // Ignore dead phi's.
6355 if (PN->use_empty()) continue;
6356
6357 unsigned Reg;
6358 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6359
6360 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006361 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006362 if (RegOut == 0) {
6363 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006364 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006365 }
6366 Reg = RegOut;
6367 } else {
6368 Reg = FuncInfo->ValueMap[PHIOp];
6369 if (Reg == 0) {
6370 assert(isa<AllocaInst>(PHIOp) &&
6371 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6372 "Didn't codegen value into a register!??");
6373 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006374 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006375 }
6376 }
6377
6378 // Remember that this register needs to added to the machine PHI node as
6379 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006380 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006381 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6382 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006383 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006384 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006385 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006386 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006387 Reg += NumRegisters;
6388 }
6389 }
6390 }
Dan Gohman2048b852009-11-23 18:04:58 +00006391 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006392}
6393
Dan Gohman3df24e62008-09-03 23:12:08 +00006394/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6395/// supports legal types, and it emits MachineInstrs directly instead of
6396/// creating SelectionDAG nodes.
6397///
6398bool
6399SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6400 FastISel *F) {
6401 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006402
Dan Gohman3df24e62008-09-03 23:12:08 +00006403 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006404 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006405
6406 // Check successor nodes' PHI nodes that expect a constant to be available
6407 // from this block.
6408 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6409 BasicBlock *SuccBB = TI->getSuccessor(succ);
6410 if (!isa<PHINode>(SuccBB->begin())) continue;
6411 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006412
Dan Gohman3df24e62008-09-03 23:12:08 +00006413 // If this terminator has multiple identical successors (common for
6414 // switches), only handle each succ once.
6415 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006416
Dan Gohman3df24e62008-09-03 23:12:08 +00006417 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6418 PHINode *PN;
6419
6420 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6421 // nodes and Machine PHI nodes, but the incoming operands have not been
6422 // emitted yet.
6423 for (BasicBlock::iterator I = SuccBB->begin();
6424 (PN = dyn_cast<PHINode>(I)); ++I) {
6425 // Ignore dead phi's.
6426 if (PN->use_empty()) continue;
6427
6428 // Only handle legal types. Two interesting things to note here. First,
6429 // by bailing out early, we may leave behind some dead instructions,
6430 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6431 // own moves. Second, this check is necessary becuase FastISel doesn't
6432 // use CreateRegForValue to create registers, so it always creates
6433 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006434 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006435 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6436 // Promote MVT::i1.
6437 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006438 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006439 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006440 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006441 return false;
6442 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006443 }
6444
6445 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6446
6447 unsigned Reg = F->getRegForValue(PHIOp);
6448 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006449 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006450 return false;
6451 }
Dan Gohman2048b852009-11-23 18:04:58 +00006452 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006453 }
6454 }
6455
6456 return true;
6457}