blob: 657ba628927cefdfd76f127afc7cb162047ac090 [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
2864
2865
Owen Andersone50ed302009-08-10 22:56:29 +00002866 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002867 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002868
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869 // Handle alignment. If the requested alignment is less than or equal to
2870 // the stack alignment, ignore it. If the size is greater than or equal to
2871 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2872 unsigned StackAlign =
2873 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2874 if (Align <= StackAlign)
2875 Align = 0;
2876
2877 // Round the size of the allocation up to the stack alignment size
2878 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002879 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002880 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881 DAG.getIntPtrConstant(StackAlign-1));
2882 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002883 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002884 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002885 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2886
2887 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002888 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002889 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002890 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002891 setValue(&I, DSA);
2892 DAG.setRoot(DSA.getValue(1));
2893
2894 // Inform the Frame Information that we have just allocated a variable-sized
2895 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002896 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002897}
2898
Dan Gohman2048b852009-11-23 18:04:58 +00002899void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 const Value *SV = I.getOperand(0);
2901 SDValue Ptr = getValue(SV);
2902
2903 const Type *Ty = I.getType();
2904 bool isVolatile = I.isVolatile();
2905 unsigned Alignment = I.getAlignment();
2906
Owen Andersone50ed302009-08-10 22:56:29 +00002907 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002908 SmallVector<uint64_t, 4> Offsets;
2909 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2910 unsigned NumValues = ValueVTs.size();
2911 if (NumValues == 0)
2912 return;
2913
2914 SDValue Root;
2915 bool ConstantMemory = false;
2916 if (I.isVolatile())
2917 // Serialize volatile loads with other side effects.
2918 Root = getRoot();
2919 else if (AA->pointsToConstantMemory(SV)) {
2920 // Do not serialize (non-volatile) loads of constant memory with anything.
2921 Root = DAG.getEntryNode();
2922 ConstantMemory = true;
2923 } else {
2924 // Do not serialize non-volatile loads against each other.
2925 Root = DAG.getRoot();
2926 }
2927
2928 SmallVector<SDValue, 4> Values(NumValues);
2929 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002930 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002931 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002932 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Nate Begemane6798372009-09-15 00:13:12 +00002933 DAG.getNode(ISD::ADD, getCurDebugLoc(),
2934 PtrVT, Ptr,
2935 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002936 SV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002937 Values[i] = L;
2938 Chains[i] = L.getValue(1);
2939 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002940
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002941 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002942 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002943 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002944 &Chains[0], NumValues);
2945 if (isVolatile)
2946 DAG.setRoot(Chain);
2947 else
2948 PendingLoads.push_back(Chain);
2949 }
2950
Scott Michelfdc40a02009-02-17 22:15:04 +00002951 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002952 DAG.getVTList(&ValueVTs[0], NumValues),
2953 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002954}
2955
2956
Dan Gohman2048b852009-11-23 18:04:58 +00002957void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002958 Value *SrcV = I.getOperand(0);
2959 Value *PtrV = I.getOperand(1);
2960
Owen Andersone50ed302009-08-10 22:56:29 +00002961 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002962 SmallVector<uint64_t, 4> Offsets;
2963 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2964 unsigned NumValues = ValueVTs.size();
2965 if (NumValues == 0)
2966 return;
2967
2968 // Get the lowered operands. Note that we do this after
2969 // checking if NumResults is zero, because with zero results
2970 // the operands won't have values in the map.
2971 SDValue Src = getValue(SrcV);
2972 SDValue Ptr = getValue(PtrV);
2973
2974 SDValue Root = getRoot();
2975 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002976 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002977 bool isVolatile = I.isVolatile();
2978 unsigned Alignment = I.getAlignment();
2979 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002980 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002981 SDValue(Src.getNode(), Src.getResNo() + i),
Scott Michelfdc40a02009-02-17 22:15:04 +00002982 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002983 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002984 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002985 PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002986
Scott Michelfdc40a02009-02-17 22:15:04 +00002987 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002988 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002989}
2990
2991/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2992/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00002993void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
2994 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002995 bool HasChain = !I.doesNotAccessMemory();
2996 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2997
2998 // Build the operand list.
2999 SmallVector<SDValue, 8> Ops;
3000 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3001 if (OnlyLoad) {
3002 // We don't need to serialize loads against other loads.
3003 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003004 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003005 Ops.push_back(getRoot());
3006 }
3007 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003008
3009 // Info is set by getTgtMemInstrinsic
3010 TargetLowering::IntrinsicInfo Info;
3011 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3012
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003013 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003014 if (!IsTgtIntrinsic)
3015 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003016
3017 // Add all operands of the call to the operand list.
3018 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3019 SDValue Op = getValue(I.getOperand(i));
3020 assert(TLI.isTypeLegal(Op.getValueType()) &&
3021 "Intrinsic uses a non-legal type?");
3022 Ops.push_back(Op);
3023 }
3024
Owen Andersone50ed302009-08-10 22:56:29 +00003025 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003026 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3027#ifndef NDEBUG
3028 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3029 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3030 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003031 }
Bob Wilson8d919552009-07-31 22:41:21 +00003032#endif // NDEBUG
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003033 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003034 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003035
Bob Wilson8d919552009-07-31 22:41:21 +00003036 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003037
3038 // Create the node.
3039 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003040 if (IsTgtIntrinsic) {
3041 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003042 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003043 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003044 Info.memVT, Info.ptrVal, Info.offset,
3045 Info.align, Info.vol,
3046 Info.readMem, Info.writeMem);
3047 }
3048 else if (!HasChain)
Scott Michelfdc40a02009-02-17 22:15:04 +00003049 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003050 VTs, &Ops[0], Ops.size());
Owen Anderson1d0be152009-08-13 21:58:54 +00003051 else if (I.getType() != Type::getVoidTy(*DAG.getContext()))
Scott Michelfdc40a02009-02-17 22:15:04 +00003052 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003053 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003054 else
Scott Michelfdc40a02009-02-17 22:15:04 +00003055 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003056 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003057
3058 if (HasChain) {
3059 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3060 if (OnlyLoad)
3061 PendingLoads.push_back(Chain);
3062 else
3063 DAG.setRoot(Chain);
3064 }
Owen Anderson1d0be152009-08-13 21:58:54 +00003065 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003066 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003067 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003068 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003069 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003070 setValue(&I, Result);
3071 }
3072}
3073
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003074/// GetSignificand - Get the significand and build it into a floating-point
3075/// number with exponent of 1:
3076///
3077/// Op = (Op & 0x007fffff) | 0x3f800000;
3078///
3079/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003080static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003081GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003082 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3083 DAG.getConstant(0x007fffff, MVT::i32));
3084 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3085 DAG.getConstant(0x3f800000, MVT::i32));
3086 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003087}
3088
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003089/// GetExponent - Get the exponent:
3090///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003091/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003092///
3093/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003094static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003095GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
3096 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003097 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3098 DAG.getConstant(0x7f800000, MVT::i32));
3099 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003100 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003101 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3102 DAG.getConstant(127, MVT::i32));
3103 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00003104}
3105
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003106/// getF32Constant - Get 32-bit floating point constant.
3107static SDValue
3108getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003109 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003110}
3111
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003112/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003113/// visitIntrinsicCall: I is a call instruction
3114/// Op is the associated NodeType for I
3115const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003116SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003117 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003118 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003119 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003120 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003121 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003122 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003123 getValue(I.getOperand(2)),
3124 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003125 setValue(&I, L);
3126 DAG.setRoot(L.getValue(1));
3127 return 0;
3128}
3129
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003130// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003131const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003132SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003133 SDValue Op1 = getValue(I.getOperand(1));
3134 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003135
Owen Anderson825b72b2009-08-11 20:47:22 +00003136 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003137 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003138
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003139 setValue(&I, Result);
3140 return 0;
3141}
Bill Wendling74c37652008-12-09 22:08:41 +00003142
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003143/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3144/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003145void
Dan Gohman2048b852009-11-23 18:04:58 +00003146SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003147 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003148 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003149
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003151 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3152 SDValue Op = getValue(I.getOperand(1));
3153
3154 // Put the exponent in the right bit position for later addition to the
3155 // final result:
3156 //
3157 // #define LOG2OFe 1.4426950f
3158 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003159 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003160 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003161 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003162
3163 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003164 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3165 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003166
3167 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003169 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003170
3171 if (LimitFloatPrecision <= 6) {
3172 // For floating-point precision of 6:
3173 //
3174 // TwoToFractionalPartOfX =
3175 // 0.997535578f +
3176 // (0.735607626f + 0.252464424f * x) * x;
3177 //
3178 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003179 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003180 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003181 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003182 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003183 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3184 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003185 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003186 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003187
3188 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003189 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003190 TwoToFracPartOfX, IntegerPartOfX);
3191
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003193 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3194 // For floating-point precision of 12:
3195 //
3196 // TwoToFractionalPartOfX =
3197 // 0.999892986f +
3198 // (0.696457318f +
3199 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3200 //
3201 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003202 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003203 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003204 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003205 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003206 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3207 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003208 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003209 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3210 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003211 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003212 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003213
3214 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003215 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003216 TwoToFracPartOfX, IntegerPartOfX);
3217
Owen Anderson825b72b2009-08-11 20:47:22 +00003218 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003219 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3220 // For floating-point precision of 18:
3221 //
3222 // TwoToFractionalPartOfX =
3223 // 0.999999982f +
3224 // (0.693148872f +
3225 // (0.240227044f +
3226 // (0.554906021e-1f +
3227 // (0.961591928e-2f +
3228 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3229 //
3230 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003232 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003233 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003234 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003235 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3236 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003237 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003238 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3239 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003240 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003241 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3242 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003243 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3245 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003246 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003247 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3248 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003249 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003250 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003251 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003252
3253 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003254 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003255 TwoToFracPartOfX, IntegerPartOfX);
3256
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003258 }
3259 } else {
3260 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003261 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003262 getValue(I.getOperand(1)).getValueType(),
3263 getValue(I.getOperand(1)));
3264 }
3265
Dale Johannesen59e577f2008-09-05 18:38:42 +00003266 setValue(&I, result);
3267}
3268
Bill Wendling39150252008-09-09 20:39:27 +00003269/// visitLog - Lower a log intrinsic. Handles the special sequences for
3270/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003271void
Dan Gohman2048b852009-11-23 18:04:58 +00003272SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003273 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003274 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003275
Owen Anderson825b72b2009-08-11 20:47:22 +00003276 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003277 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3278 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003279 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003280
3281 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003282 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003283 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003284 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003285
3286 // Get the significand and build it into a floating-point number with
3287 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003288 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003289
3290 if (LimitFloatPrecision <= 6) {
3291 // For floating-point precision of 6:
3292 //
3293 // LogofMantissa =
3294 // -1.1609546f +
3295 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003296 //
Bill Wendling39150252008-09-09 20:39:27 +00003297 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003299 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003301 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3303 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003304 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003305
Scott Michelfdc40a02009-02-17 22:15:04 +00003306 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003307 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003308 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3309 // For floating-point precision of 12:
3310 //
3311 // LogOfMantissa =
3312 // -1.7417939f +
3313 // (2.8212026f +
3314 // (-1.4699568f +
3315 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3316 //
3317 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003318 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003319 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003320 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003321 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003322 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3323 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003324 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003325 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3326 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003327 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3329 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003330 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003331
Scott Michelfdc40a02009-02-17 22:15:04 +00003332 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003333 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003334 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3335 // For floating-point precision of 18:
3336 //
3337 // LogOfMantissa =
3338 // -2.1072184f +
3339 // (4.2372794f +
3340 // (-3.7029485f +
3341 // (2.2781945f +
3342 // (-0.87823314f +
3343 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3344 //
3345 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003347 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3351 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003352 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3354 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003355 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3357 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003358 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003359 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3360 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003361 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3363 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003364 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003365
Scott Michelfdc40a02009-02-17 22:15:04 +00003366 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003368 }
3369 } else {
3370 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003371 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003372 getValue(I.getOperand(1)).getValueType(),
3373 getValue(I.getOperand(1)));
3374 }
3375
Dale Johannesen59e577f2008-09-05 18:38:42 +00003376 setValue(&I, result);
3377}
3378
Bill Wendling3eb59402008-09-09 00:28:24 +00003379/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3380/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003381void
Dan Gohman2048b852009-11-23 18:04:58 +00003382SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003383 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003384 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003385
Owen Anderson825b72b2009-08-11 20:47:22 +00003386 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003387 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3388 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003389 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003390
Bill Wendling39150252008-09-09 20:39:27 +00003391 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003392 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003393
3394 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003395 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003396 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003397
Bill Wendling3eb59402008-09-09 00:28:24 +00003398 // Different possible minimax approximations of significand in
3399 // floating-point for various degrees of accuracy over [1,2].
3400 if (LimitFloatPrecision <= 6) {
3401 // For floating-point precision of 6:
3402 //
3403 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3404 //
3405 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003407 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003409 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003410 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3411 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003412 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003413
Scott Michelfdc40a02009-02-17 22:15:04 +00003414 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003415 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003416 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3417 // For floating-point precision of 12:
3418 //
3419 // Log2ofMantissa =
3420 // -2.51285454f +
3421 // (4.07009056f +
3422 // (-2.12067489f +
3423 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003424 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003425 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003426 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003427 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003428 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003429 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003430 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3431 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003432 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003433 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3434 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003435 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003436 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3437 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003438 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003439
Scott Michelfdc40a02009-02-17 22:15:04 +00003440 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003442 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3443 // For floating-point precision of 18:
3444 //
3445 // Log2ofMantissa =
3446 // -3.0400495f +
3447 // (6.1129976f +
3448 // (-5.3420409f +
3449 // (3.2865683f +
3450 // (-1.2669343f +
3451 // (0.27515199f -
3452 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3453 //
3454 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003456 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003457 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003458 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3460 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003461 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3463 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003464 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3466 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003467 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003468 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3469 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003470 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3472 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003473 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003474
Scott Michelfdc40a02009-02-17 22:15:04 +00003475 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003477 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003478 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003479 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003480 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003481 getValue(I.getOperand(1)).getValueType(),
3482 getValue(I.getOperand(1)));
3483 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003484
Dale Johannesen59e577f2008-09-05 18:38:42 +00003485 setValue(&I, result);
3486}
3487
Bill Wendling3eb59402008-09-09 00:28:24 +00003488/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3489/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003490void
Dan Gohman2048b852009-11-23 18:04:58 +00003491SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003492 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003493 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003494
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003496 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3497 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003499
Bill Wendling39150252008-09-09 20:39:27 +00003500 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003501 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003502 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003503 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003504
3505 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003506 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003507 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003508
3509 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003510 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003511 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003512 // Log10ofMantissa =
3513 // -0.50419619f +
3514 // (0.60948995f - 0.10380950f * x) * x;
3515 //
3516 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003517 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003518 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003519 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003520 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003521 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3522 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003523 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003524
Scott Michelfdc40a02009-02-17 22:15:04 +00003525 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003527 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3528 // For floating-point precision of 12:
3529 //
3530 // Log10ofMantissa =
3531 // -0.64831180f +
3532 // (0.91751397f +
3533 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3534 //
3535 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003537 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003539 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3541 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003542 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3544 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003545 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003546
Scott Michelfdc40a02009-02-17 22:15:04 +00003547 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003549 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003550 // For floating-point precision of 18:
3551 //
3552 // Log10ofMantissa =
3553 // -0.84299375f +
3554 // (1.5327582f +
3555 // (-1.0688956f +
3556 // (0.49102474f +
3557 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3558 //
3559 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003561 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3565 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3568 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003569 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3571 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003572 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3574 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003575 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003576
Scott Michelfdc40a02009-02-17 22:15:04 +00003577 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003579 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003580 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003581 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003582 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003583 getValue(I.getOperand(1)).getValueType(),
3584 getValue(I.getOperand(1)));
3585 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003586
Dale Johannesen59e577f2008-09-05 18:38:42 +00003587 setValue(&I, result);
3588}
3589
Bill Wendlinge10c8142008-09-09 22:39:21 +00003590/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3591/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003592void
Dan Gohman2048b852009-11-23 18:04:58 +00003593SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003594 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003595 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003596
Owen Anderson825b72b2009-08-11 20:47:22 +00003597 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003598 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3599 SDValue Op = getValue(I.getOperand(1));
3600
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003602
3603 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3605 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003606
3607 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003608 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003609 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003610
3611 if (LimitFloatPrecision <= 6) {
3612 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003613 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003614 // TwoToFractionalPartOfX =
3615 // 0.997535578f +
3616 // (0.735607626f + 0.252464424f * x) * x;
3617 //
3618 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003619 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003620 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003621 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003622 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003623 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3624 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003625 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003626 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003627 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003628 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003629
Scott Michelfdc40a02009-02-17 22:15:04 +00003630 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003631 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003632 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3633 // For floating-point precision of 12:
3634 //
3635 // TwoToFractionalPartOfX =
3636 // 0.999892986f +
3637 // (0.696457318f +
3638 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3639 //
3640 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003641 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003642 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003643 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003644 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003645 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3646 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003647 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003648 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3649 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003650 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003651 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003652 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003653 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003654
Scott Michelfdc40a02009-02-17 22:15:04 +00003655 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003656 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003657 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3658 // For floating-point precision of 18:
3659 //
3660 // TwoToFractionalPartOfX =
3661 // 0.999999982f +
3662 // (0.693148872f +
3663 // (0.240227044f +
3664 // (0.554906021e-1f +
3665 // (0.961591928e-2f +
3666 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3667 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003669 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003670 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003671 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003672 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3673 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003674 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003675 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3676 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003677 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003678 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3679 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003680 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003681 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3682 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003683 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003684 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3685 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003686 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003687 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003688 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003689 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003690
Scott Michelfdc40a02009-02-17 22:15:04 +00003691 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003692 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003693 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003694 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003695 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003696 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003697 getValue(I.getOperand(1)).getValueType(),
3698 getValue(I.getOperand(1)));
3699 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003700
Dale Johannesen601d3c02008-09-05 01:48:15 +00003701 setValue(&I, result);
3702}
3703
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003704/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3705/// limited-precision mode with x == 10.0f.
3706void
Dan Gohman2048b852009-11-23 18:04:58 +00003707SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003708 SDValue result;
3709 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003710 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003711 bool IsExp10 = false;
3712
Owen Anderson825b72b2009-08-11 20:47:22 +00003713 if (getValue(Val).getValueType() == MVT::f32 &&
3714 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003715 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3716 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3717 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3718 APFloat Ten(10.0f);
3719 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3720 }
3721 }
3722 }
3723
3724 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3725 SDValue Op = getValue(I.getOperand(2));
3726
3727 // Put the exponent in the right bit position for later addition to the
3728 // final result:
3729 //
3730 // #define LOG2OF10 3.3219281f
3731 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003733 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003735
3736 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3738 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003739
3740 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003741 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003742 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003743
3744 if (LimitFloatPrecision <= 6) {
3745 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003746 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003747 // twoToFractionalPartOfX =
3748 // 0.997535578f +
3749 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003750 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003751 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003752 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003753 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003754 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003755 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003756 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3757 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003758 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003759 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003760 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003761 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003762
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003763 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003764 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003765 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3766 // For floating-point precision of 12:
3767 //
3768 // TwoToFractionalPartOfX =
3769 // 0.999892986f +
3770 // (0.696457318f +
3771 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3772 //
3773 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003774 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003775 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003776 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003777 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003778 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3779 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003780 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003781 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3782 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003783 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003784 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003785 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003786 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003787
Scott Michelfdc40a02009-02-17 22:15:04 +00003788 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003789 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003790 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3791 // For floating-point precision of 18:
3792 //
3793 // TwoToFractionalPartOfX =
3794 // 0.999999982f +
3795 // (0.693148872f +
3796 // (0.240227044f +
3797 // (0.554906021e-1f +
3798 // (0.961591928e-2f +
3799 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3800 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003801 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003802 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003803 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003804 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3806 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003807 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003808 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3809 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003810 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3812 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003813 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003814 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3815 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003816 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003817 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3818 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003819 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003820 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003821 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003823
Scott Michelfdc40a02009-02-17 22:15:04 +00003824 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003825 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003826 }
3827 } else {
3828 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003829 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003830 getValue(I.getOperand(1)).getValueType(),
3831 getValue(I.getOperand(1)),
3832 getValue(I.getOperand(2)));
3833 }
3834
3835 setValue(&I, result);
3836}
3837
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003838/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3839/// we want to emit this as a call to a named external function, return the name
3840/// otherwise lower it and return null.
3841const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003842SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003843 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003844 switch (Intrinsic) {
3845 default:
3846 // By default, turn this into a target intrinsic node.
3847 visitTargetIntrinsic(I, Intrinsic);
3848 return 0;
3849 case Intrinsic::vastart: visitVAStart(I); return 0;
3850 case Intrinsic::vaend: visitVAEnd(I); return 0;
3851 case Intrinsic::vacopy: visitVACopy(I); return 0;
3852 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003853 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003854 getValue(I.getOperand(1))));
3855 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003856 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003857 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003858 getValue(I.getOperand(1))));
3859 return 0;
3860 case Intrinsic::setjmp:
3861 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3862 break;
3863 case Intrinsic::longjmp:
3864 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3865 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003866 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003867 SDValue Op1 = getValue(I.getOperand(1));
3868 SDValue Op2 = getValue(I.getOperand(2));
3869 SDValue Op3 = getValue(I.getOperand(3));
3870 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003871 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003872 I.getOperand(1), 0, I.getOperand(2), 0));
3873 return 0;
3874 }
Chris Lattner824b9582008-11-21 16:42:48 +00003875 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003876 SDValue Op1 = getValue(I.getOperand(1));
3877 SDValue Op2 = getValue(I.getOperand(2));
3878 SDValue Op3 = getValue(I.getOperand(3));
3879 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003880 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003881 I.getOperand(1), 0));
3882 return 0;
3883 }
Chris Lattner824b9582008-11-21 16:42:48 +00003884 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003885 SDValue Op1 = getValue(I.getOperand(1));
3886 SDValue Op2 = getValue(I.getOperand(2));
3887 SDValue Op3 = getValue(I.getOperand(3));
3888 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3889
3890 // If the source and destination are known to not be aliases, we can
3891 // lower memmove as memcpy.
3892 uint64_t Size = -1ULL;
3893 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003894 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003895 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3896 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00003897 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003898 I.getOperand(1), 0, I.getOperand(2), 0));
3899 return 0;
3900 }
3901
Dale Johannesena04b7572009-02-03 23:04:43 +00003902 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003903 I.getOperand(1), 0, I.getOperand(2), 0));
3904 return 0;
3905 }
Devang Patel70d75ca2009-11-12 19:02:56 +00003906 case Intrinsic::dbg_stoppoint:
3907 case Intrinsic::dbg_region_start:
3908 case Intrinsic::dbg_region_end:
3909 case Intrinsic::dbg_func_start:
3910 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003911 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003912 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00003913 if (OptLevel != CodeGenOpt::None)
3914 // FIXME: Variable debug info is not supported here.
3915 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00003916 DwarfWriter *DW = DAG.getDwarfWriter();
3917 if (!DW)
3918 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003919 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3920 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
3921 return 0;
3922
Devang Patelac1ceb32009-10-09 22:42:28 +00003923 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00003924 Value *Address = DI.getAddress();
3925 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
3926 Address = BCI->getOperand(0);
3927 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
3928 // Don't handle byval struct arguments or VLAs, for example.
3929 if (!AI)
3930 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003931 DenseMap<const AllocaInst*, int>::iterator SI =
3932 FuncInfo.StaticAllocaMap.find(AI);
3933 if (SI == FuncInfo.StaticAllocaMap.end())
3934 return 0; // VLAs.
3935 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003936
Devang Patelac1ceb32009-10-09 22:42:28 +00003937 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00003938 if (MMI) {
3939 MetadataContext &TheMetadata =
3940 DI.getParent()->getContext().getMetadata();
3941 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
3942 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
3943 MMI->setVariableDbgInfo(Variable, FI, Dbg);
3944 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003945 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003946 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003947 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003948 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00003949 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003950 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003951 SDValue Ops[1];
3952 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003953 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003954 setValue(&I, Op);
3955 DAG.setRoot(Op.getValue(1));
3956 return 0;
3957 }
3958
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003959 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003960 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003961
Chris Lattner3a5815f2009-09-17 23:54:54 +00003962 if (CurMBB->isLandingPad())
3963 AddCatchInfo(I, MMI, CurMBB);
3964 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003965#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003966 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003967#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003968 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3969 unsigned Reg = TLI.getExceptionSelectorRegister();
3970 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003971 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003972
Chris Lattner3a5815f2009-09-17 23:54:54 +00003973 // Insert the EHSELECTION instruction.
3974 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3975 SDValue Ops[2];
3976 Ops[0] = getValue(I.getOperand(1));
3977 Ops[1] = getRoot();
3978 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
3979
3980 DAG.setRoot(Op.getValue(1));
3981
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003982 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003983 return 0;
3984 }
3985
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003986 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003987 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003988
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003989 if (MMI) {
3990 // Find the type id for the given typeinfo.
3991 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3992
3993 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003994 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003995 } else {
3996 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003997 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003998 }
3999
4000 return 0;
4001 }
4002
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004003 case Intrinsic::eh_return_i32:
4004 case Intrinsic::eh_return_i64:
4005 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004006 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004007 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004008 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004009 getControlRoot(),
4010 getValue(I.getOperand(1)),
4011 getValue(I.getOperand(2))));
4012 } else {
4013 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4014 }
4015
4016 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004017 case Intrinsic::eh_unwind_init:
4018 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4019 MMI->setCallsUnwindInit(true);
4020 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004021
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004022 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004023
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004024 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004025 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004026 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4027 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004028
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004029 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004030 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004031 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004032 TLI.getPointerTy()),
4033 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004034 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004035 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004036 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004037 TLI.getPointerTy(),
4038 DAG.getConstant(0,
4039 TLI.getPointerTy())),
4040 Offset));
4041 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004042 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004043 case Intrinsic::convertff:
4044 case Intrinsic::convertfsi:
4045 case Intrinsic::convertfui:
4046 case Intrinsic::convertsif:
4047 case Intrinsic::convertuif:
4048 case Intrinsic::convertss:
4049 case Intrinsic::convertsu:
4050 case Intrinsic::convertus:
4051 case Intrinsic::convertuu: {
4052 ISD::CvtCode Code = ISD::CVT_INVALID;
4053 switch (Intrinsic) {
4054 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4055 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4056 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4057 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4058 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4059 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4060 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4061 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4062 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4063 }
Owen Andersone50ed302009-08-10 22:56:29 +00004064 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00004065 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00004066 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00004067 DAG.getValueType(DestVT),
4068 DAG.getValueType(getValue(Op1).getValueType()),
4069 getValue(I.getOperand(2)),
4070 getValue(I.getOperand(3)),
4071 Code));
4072 return 0;
4073 }
4074
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004075 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004076 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004077 getValue(I.getOperand(1)).getValueType(),
4078 getValue(I.getOperand(1))));
4079 return 0;
4080 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004081 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004082 getValue(I.getOperand(1)).getValueType(),
4083 getValue(I.getOperand(1)),
4084 getValue(I.getOperand(2))));
4085 return 0;
4086 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004087 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004088 getValue(I.getOperand(1)).getValueType(),
4089 getValue(I.getOperand(1))));
4090 return 0;
4091 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004092 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004093 getValue(I.getOperand(1)).getValueType(),
4094 getValue(I.getOperand(1))));
4095 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004096 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004097 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004098 return 0;
4099 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004100 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004101 return 0;
4102 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004103 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004104 return 0;
4105 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004106 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004107 return 0;
4108 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004109 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004110 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004111 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004112 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004113 return 0;
4114 case Intrinsic::pcmarker: {
4115 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004116 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004117 return 0;
4118 }
4119 case Intrinsic::readcyclecounter: {
4120 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004121 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004122 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004123 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004124 setValue(&I, Tmp);
4125 DAG.setRoot(Tmp.getValue(1));
4126 return 0;
4127 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004128 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004129 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004130 getValue(I.getOperand(1)).getValueType(),
4131 getValue(I.getOperand(1))));
4132 return 0;
4133 case Intrinsic::cttz: {
4134 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004135 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004136 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004137 setValue(&I, result);
4138 return 0;
4139 }
4140 case Intrinsic::ctlz: {
4141 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004142 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004143 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004144 setValue(&I, result);
4145 return 0;
4146 }
4147 case Intrinsic::ctpop: {
4148 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004149 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004150 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004151 setValue(&I, result);
4152 return 0;
4153 }
4154 case Intrinsic::stacksave: {
4155 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004156 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004157 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004158 setValue(&I, Tmp);
4159 DAG.setRoot(Tmp.getValue(1));
4160 return 0;
4161 }
4162 case Intrinsic::stackrestore: {
4163 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004165 return 0;
4166 }
Bill Wendling57344502008-11-18 11:01:33 +00004167 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004168 // Emit code into the DAG to store the stack guard onto the stack.
4169 MachineFunction &MF = DAG.getMachineFunction();
4170 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004171 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004172
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004173 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4174 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004175
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004176 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004177 MFI->setStackProtectorIndex(FI);
4178
4179 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4180
4181 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004182 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00004183 PseudoSourceValue::getFixedStack(FI),
4184 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004185 setValue(&I, Result);
4186 DAG.setRoot(Result);
4187 return 0;
4188 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004189 case Intrinsic::objectsize: {
4190 // If we don't know by now, we're never going to know.
4191 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4192
4193 assert(CI && "Non-constant type in __builtin_object_size?");
4194
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004195 SDValue Arg = getValue(I.getOperand(0));
4196 EVT Ty = Arg.getValueType();
4197
Eric Christopher7b5e6172009-10-27 00:52:25 +00004198 if (CI->getZExtValue() < 2)
Mike Stump70e5e682009-11-09 22:28:21 +00004199 setValue(&I, DAG.getConstant(-1ULL, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004200 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004201 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004202 return 0;
4203 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004204 case Intrinsic::var_annotation:
4205 // Discard annotate attributes
4206 return 0;
4207
4208 case Intrinsic::init_trampoline: {
4209 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4210
4211 SDValue Ops[6];
4212 Ops[0] = getRoot();
4213 Ops[1] = getValue(I.getOperand(1));
4214 Ops[2] = getValue(I.getOperand(2));
4215 Ops[3] = getValue(I.getOperand(3));
4216 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4217 Ops[5] = DAG.getSrcValue(F);
4218
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004219 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004221 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004222
4223 setValue(&I, Tmp);
4224 DAG.setRoot(Tmp.getValue(1));
4225 return 0;
4226 }
4227
4228 case Intrinsic::gcroot:
4229 if (GFI) {
4230 Value *Alloca = I.getOperand(1);
4231 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004232
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004233 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4234 GFI->addStackRoot(FI->getIndex(), TypeMap);
4235 }
4236 return 0;
4237
4238 case Intrinsic::gcread:
4239 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004240 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004241 return 0;
4242
4243 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004244 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004245 return 0;
4246 }
4247
4248 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004249 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004250 return 0;
4251 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004252
Bill Wendlingef375462008-11-21 02:38:44 +00004253 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004254 return implVisitAluOverflow(I, ISD::UADDO);
4255 case Intrinsic::sadd_with_overflow:
4256 return implVisitAluOverflow(I, ISD::SADDO);
4257 case Intrinsic::usub_with_overflow:
4258 return implVisitAluOverflow(I, ISD::USUBO);
4259 case Intrinsic::ssub_with_overflow:
4260 return implVisitAluOverflow(I, ISD::SSUBO);
4261 case Intrinsic::umul_with_overflow:
4262 return implVisitAluOverflow(I, ISD::UMULO);
4263 case Intrinsic::smul_with_overflow:
4264 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004266 case Intrinsic::prefetch: {
4267 SDValue Ops[4];
4268 Ops[0] = getRoot();
4269 Ops[1] = getValue(I.getOperand(1));
4270 Ops[2] = getValue(I.getOperand(2));
4271 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004272 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004273 return 0;
4274 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004275
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004276 case Intrinsic::memory_barrier: {
4277 SDValue Ops[6];
4278 Ops[0] = getRoot();
4279 for (int x = 1; x < 6; ++x)
4280 Ops[x] = getValue(I.getOperand(x));
4281
Owen Anderson825b72b2009-08-11 20:47:22 +00004282 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004283 return 0;
4284 }
4285 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004286 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004287 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004288 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004289 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4290 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004291 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004292 getValue(I.getOperand(2)),
4293 getValue(I.getOperand(3)),
4294 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004295 setValue(&I, L);
4296 DAG.setRoot(L.getValue(1));
4297 return 0;
4298 }
4299 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004300 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004301 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004302 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004303 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004304 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004305 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004306 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004307 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004308 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004309 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004310 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004311 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004312 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004313 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004314 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004315 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004316 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004317 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004318 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004319 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004320 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004321
4322 case Intrinsic::invariant_start:
4323 case Intrinsic::lifetime_start:
4324 // Discard region information.
4325 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
4326 return 0;
4327 case Intrinsic::invariant_end:
4328 case Intrinsic::lifetime_end:
4329 // Discard region information.
4330 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004331 }
4332}
4333
Dan Gohman98ca4f22009-08-05 01:29:28 +00004334/// Test if the given instruction is in a position to be optimized
4335/// with a tail-call. This roughly means that it's in a block with
4336/// a return and there's nothing that needs to be scheduled
4337/// between it and the return.
4338///
4339/// This function only tests target-independent requirements.
4340/// For target-dependent requirements, a target should override
4341/// TargetLowering::IsEligibleForTailCallOptimization.
4342///
4343static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004344isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004345 const TargetLowering &TLI) {
4346 const BasicBlock *ExitBB = I->getParent();
4347 const TerminatorInst *Term = ExitBB->getTerminator();
4348 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4349 const Function *F = ExitBB->getParent();
4350
4351 // The block must end in a return statement or an unreachable.
4352 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4353
4354 // If I will have a chain, make sure no other instruction that will have a
4355 // chain interposes between I and the return.
4356 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4357 !I->isSafeToSpeculativelyExecute())
4358 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4359 --BBI) {
4360 if (&*BBI == I)
4361 break;
4362 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4363 !BBI->isSafeToSpeculativelyExecute())
4364 return false;
4365 }
4366
4367 // If the block ends with a void return or unreachable, it doesn't matter
4368 // what the call's return type is.
4369 if (!Ret || Ret->getNumOperands() == 0) return true;
4370
Dan Gohmaned9bab32009-11-14 02:06:30 +00004371 // If the return value is undef, it doesn't matter what the call's
4372 // return type is.
4373 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4374
Dan Gohman98ca4f22009-08-05 01:29:28 +00004375 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004376 // the return. Ignore noalias because it doesn't affect the call sequence.
4377 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4378 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004379 return false;
4380
4381 // Otherwise, make sure the unmodified return value of I is the return value.
4382 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4383 U = dyn_cast<Instruction>(U->getOperand(0))) {
4384 if (!U)
4385 return false;
4386 if (!U->hasOneUse())
4387 return false;
4388 if (U == I)
4389 break;
4390 // Check for a truly no-op truncate.
4391 if (isa<TruncInst>(U) &&
4392 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4393 continue;
4394 // Check for a truly no-op bitcast.
4395 if (isa<BitCastInst>(U) &&
4396 (U->getOperand(0)->getType() == U->getType() ||
4397 (isa<PointerType>(U->getOperand(0)->getType()) &&
4398 isa<PointerType>(U->getType()))))
4399 continue;
4400 // Otherwise it's not a true no-op.
4401 return false;
4402 }
4403
4404 return true;
4405}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406
Dan Gohman2048b852009-11-23 18:04:58 +00004407void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4408 bool isTailCall,
4409 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004410 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4411 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004412 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004413 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4414 unsigned BeginLabel = 0, EndLabel = 0;
4415
4416 TargetLowering::ArgListTy Args;
4417 TargetLowering::ArgListEntry Entry;
4418 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004419
4420 // Check whether the function can return without sret-demotion.
4421 SmallVector<EVT, 4> OutVTs;
4422 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4423 SmallVector<uint64_t, 4> Offsets;
4424 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4425 OutVTs, OutsFlags, TLI, &Offsets);
4426
4427
4428 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4429 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4430
4431 SDValue DemoteStackSlot;
4432
4433 if (!CanLowerReturn) {
4434 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4435 FTy->getReturnType());
4436 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4437 FTy->getReturnType());
4438 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004439 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004440 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4441
4442 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4443 Entry.Node = DemoteStackSlot;
4444 Entry.Ty = StackSlotPtrType;
4445 Entry.isSExt = false;
4446 Entry.isZExt = false;
4447 Entry.isInReg = false;
4448 Entry.isSRet = true;
4449 Entry.isNest = false;
4450 Entry.isByVal = false;
4451 Entry.Alignment = Align;
4452 Args.push_back(Entry);
4453 RetTy = Type::getVoidTy(FTy->getContext());
4454 }
4455
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004456 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004457 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004458 SDValue ArgNode = getValue(*i);
4459 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4460
4461 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004462 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4463 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4464 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4465 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4466 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4467 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004468 Entry.Alignment = CS.getParamAlignment(attrInd);
4469 Args.push_back(Entry);
4470 }
4471
4472 if (LandingPad && MMI) {
4473 // Insert a label before the invoke call to mark the try range. This can be
4474 // used to detect deletion of the invoke via the MachineModuleInfo.
4475 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004476
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004477 // Both PendingLoads and PendingExports must be flushed here;
4478 // this call might not return.
4479 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004480 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4481 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004482 }
4483
Dan Gohman98ca4f22009-08-05 01:29:28 +00004484 // Check if target-independent constraints permit a tail call here.
4485 // Target-dependent constraints are checked within TLI.LowerCallTo.
4486 if (isTailCall &&
4487 !isInTailCallPosition(CS.getInstruction(),
4488 CS.getAttributes().getRetAttributes(),
4489 TLI))
4490 isTailCall = false;
4491
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004492 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004493 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004494 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004495 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004496 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004497 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004498 isTailCall,
4499 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004500 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004501 assert((isTailCall || Result.second.getNode()) &&
4502 "Non-null chain expected with non-tail call!");
4503 assert((Result.second.getNode() || !Result.first.getNode()) &&
4504 "Null value expected with tail call!");
4505 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004506 setValue(CS.getInstruction(), Result.first);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004507 else if (!CanLowerReturn && Result.second.getNode()) {
4508 // The instruction result is the result of loading from the
4509 // hidden sret parameter.
4510 SmallVector<EVT, 1> PVTs;
4511 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4512
4513 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4514 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4515 EVT PtrVT = PVTs[0];
4516 unsigned NumValues = OutVTs.size();
4517 SmallVector<SDValue, 4> Values(NumValues);
4518 SmallVector<SDValue, 4> Chains(NumValues);
4519
4520 for (unsigned i = 0; i < NumValues; ++i) {
4521 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
4522 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, DemoteStackSlot,
4523 DAG.getConstant(Offsets[i], PtrVT)),
4524 NULL, Offsets[i], false, 1);
4525 Values[i] = L;
4526 Chains[i] = L.getValue(1);
4527 }
4528 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4529 MVT::Other, &Chains[0], NumValues);
4530 PendingLoads.push_back(Chain);
4531
4532 setValue(CS.getInstruction(), DAG.getNode(ISD::MERGE_VALUES,
4533 getCurDebugLoc(), DAG.getVTList(&OutVTs[0], NumValues),
4534 &Values[0], NumValues));
4535 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00004536 // As a special case, a null chain means that a tail call has
4537 // been emitted and the DAG root is already updated.
4538 if (Result.second.getNode())
4539 DAG.setRoot(Result.second);
4540 else
4541 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004542
4543 if (LandingPad && MMI) {
4544 // Insert a label at the end of the invoke call to mark the try range. This
4545 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4546 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004547 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4548 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004549
4550 // Inform MachineModuleInfo of range.
4551 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4552 }
4553}
4554
4555
Dan Gohman2048b852009-11-23 18:04:58 +00004556void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004557 const char *RenameFn = 0;
4558 if (Function *F = I.getCalledFunction()) {
4559 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004560 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4561 if (II) {
4562 if (unsigned IID = II->getIntrinsicID(F)) {
4563 RenameFn = visitIntrinsicCall(I, IID);
4564 if (!RenameFn)
4565 return;
4566 }
4567 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004568 if (unsigned IID = F->getIntrinsicID()) {
4569 RenameFn = visitIntrinsicCall(I, IID);
4570 if (!RenameFn)
4571 return;
4572 }
4573 }
4574
4575 // Check for well-known libc/libm calls. If the function is internal, it
4576 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004577 if (!F->hasLocalLinkage() && F->hasName()) {
4578 StringRef Name = F->getName();
4579 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004580 if (I.getNumOperands() == 3 && // Basic sanity checks.
4581 I.getOperand(1)->getType()->isFloatingPoint() &&
4582 I.getType() == I.getOperand(1)->getType() &&
4583 I.getType() == I.getOperand(2)->getType()) {
4584 SDValue LHS = getValue(I.getOperand(1));
4585 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004586 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004587 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 return;
4589 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004590 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004591 if (I.getNumOperands() == 2 && // Basic sanity checks.
4592 I.getOperand(1)->getType()->isFloatingPoint() &&
4593 I.getType() == I.getOperand(1)->getType()) {
4594 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004595 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004596 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004597 return;
4598 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004599 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004600 if (I.getNumOperands() == 2 && // Basic sanity checks.
4601 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004602 I.getType() == I.getOperand(1)->getType() &&
4603 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004604 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004605 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004606 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004607 return;
4608 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004609 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004610 if (I.getNumOperands() == 2 && // Basic sanity checks.
4611 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004612 I.getType() == I.getOperand(1)->getType() &&
4613 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004614 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004615 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004616 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004617 return;
4618 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004619 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4620 if (I.getNumOperands() == 2 && // Basic sanity checks.
4621 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004622 I.getType() == I.getOperand(1)->getType() &&
4623 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004624 SDValue Tmp = getValue(I.getOperand(1));
4625 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4626 Tmp.getValueType(), Tmp));
4627 return;
4628 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004629 }
4630 }
4631 } else if (isa<InlineAsm>(I.getOperand(0))) {
4632 visitInlineAsm(&I);
4633 return;
4634 }
4635
4636 SDValue Callee;
4637 if (!RenameFn)
4638 Callee = getValue(I.getOperand(0));
4639 else
Bill Wendling056292f2008-09-16 21:48:12 +00004640 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004641
Dan Gohman98ca4f22009-08-05 01:29:28 +00004642 // Check if we can potentially perform a tail call. More detailed
4643 // checking is be done within LowerCallTo, after more information
4644 // about the call is known.
4645 bool isTailCall = PerformTailCallOpt && I.isTailCall();
4646
4647 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004648}
4649
4650
4651/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004652/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004653/// Chain/Flag as the input and updates them for the output Chain/Flag.
4654/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004655SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 SDValue &Chain,
4657 SDValue *Flag) const {
4658 // Assemble the legal parts into the final values.
4659 SmallVector<SDValue, 4> Values(ValueVTs.size());
4660 SmallVector<SDValue, 8> Parts;
4661 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4662 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004663 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004664 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004665 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004666
4667 Parts.resize(NumRegs);
4668 for (unsigned i = 0; i != NumRegs; ++i) {
4669 SDValue P;
4670 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004671 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004672 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004673 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004674 *Flag = P.getValue(2);
4675 }
4676 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004677
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004678 // If the source register was virtual and if we know something about it,
4679 // add an assert node.
4680 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4681 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4682 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4683 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4684 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4685 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004686
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004687 unsigned RegSize = RegisterVT.getSizeInBits();
4688 unsigned NumSignBits = LOI.NumSignBits;
4689 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004691 // FIXME: We capture more information than the dag can represent. For
4692 // now, just use the tightest assertzext/assertsext possible.
4693 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004694 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004695 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004696 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004697 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004698 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004699 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004700 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004701 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004702 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004703 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004704 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004705 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004706 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004707 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004708 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004709 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004710 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004711
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004713 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004714 RegisterVT, P, DAG.getValueType(FromVT));
4715
4716 }
4717 }
4718 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004719
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004720 Parts[i] = P;
4721 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004722
Scott Michelfdc40a02009-02-17 22:15:04 +00004723 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004724 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004725 Part += NumRegs;
4726 Parts.clear();
4727 }
4728
Dale Johannesen66978ee2009-01-31 02:22:37 +00004729 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004730 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4731 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004732}
4733
4734/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004735/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004736/// Chain/Flag as the input and updates them for the output Chain/Flag.
4737/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004738void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004739 SDValue &Chain, SDValue *Flag) const {
4740 // Get the list of the values's legal parts.
4741 unsigned NumRegs = Regs.size();
4742 SmallVector<SDValue, 8> Parts(NumRegs);
4743 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004744 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004745 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004746 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004747
Dale Johannesen66978ee2009-01-31 02:22:37 +00004748 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004749 &Parts[Part], NumParts, RegisterVT);
4750 Part += NumParts;
4751 }
4752
4753 // Copy the parts into the registers.
4754 SmallVector<SDValue, 8> Chains(NumRegs);
4755 for (unsigned i = 0; i != NumRegs; ++i) {
4756 SDValue Part;
4757 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004758 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004759 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004760 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004761 *Flag = Part.getValue(1);
4762 }
4763 Chains[i] = Part.getValue(0);
4764 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004766 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004767 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 // flagged to it. That is the CopyToReg nodes and the user are considered
4769 // a single scheduling unit. If we create a TokenFactor and return it as
4770 // chain, then the TokenFactor is both a predecessor (operand) of the
4771 // user as well as a successor (the TF operands are flagged to the user).
4772 // c1, f1 = CopyToReg
4773 // c2, f2 = CopyToReg
4774 // c3 = TokenFactor c1, c2
4775 // ...
4776 // = op c3, ..., f2
4777 Chain = Chains[NumRegs-1];
4778 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004779 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004780}
4781
4782/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004783/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004784/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00004785void RegsForValue::AddInlineAsmOperands(unsigned Code,
4786 bool HasMatching,unsigned MatchingIdx,
4787 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004788 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004789 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00004790 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
4791 unsigned Flag = Code | (Regs.size() << 3);
4792 if (HasMatching)
4793 Flag |= 0x80000000 | (MatchingIdx << 16);
4794 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004795 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004796 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004797 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004798 for (unsigned i = 0; i != NumRegs; ++i) {
4799 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004800 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004801 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802 }
4803}
4804
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004805/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004806/// i.e. it isn't a stack pointer or some other special register, return the
4807/// register class for the register. Otherwise, return null.
4808static const TargetRegisterClass *
4809isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4810 const TargetLowering &TLI,
4811 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004812 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004813 const TargetRegisterClass *FoundRC = 0;
4814 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4815 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004816 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004817
4818 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004819 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004820 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4821 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4822 I != E; ++I) {
4823 if (TLI.isTypeLegal(*I)) {
4824 // If we have already found this register in a different register class,
4825 // choose the one with the largest VT specified. For example, on
4826 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004827 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 ThisVT = *I;
4829 break;
4830 }
4831 }
4832 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004833
Owen Anderson825b72b2009-08-11 20:47:22 +00004834 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004835
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004836 // NOTE: This isn't ideal. In particular, this might allocate the
4837 // frame pointer in functions that need it (due to them not being taken
4838 // out of allocation, because a variable sized allocation hasn't been seen
4839 // yet). This is a slight code pessimization, but should still work.
4840 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4841 E = RC->allocation_order_end(MF); I != E; ++I)
4842 if (*I == Reg) {
4843 // We found a matching register class. Keep looking at others in case
4844 // we find one with larger registers that this physreg is also in.
4845 FoundRC = RC;
4846 FoundVT = ThisVT;
4847 break;
4848 }
4849 }
4850 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004851}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004852
4853
4854namespace llvm {
4855/// AsmOperandInfo - This contains information for each constraint that we are
4856/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004857class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004858 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004859public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004860 /// CallOperand - If this is the result output operand or a clobber
4861 /// this is null, otherwise it is the incoming operand to the CallInst.
4862 /// This gets modified as the asm is processed.
4863 SDValue CallOperand;
4864
4865 /// AssignedRegs - If this is a register or register class operand, this
4866 /// contains the set of register corresponding to the operand.
4867 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004868
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004869 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4870 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4871 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004873 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4874 /// busy in OutputRegs/InputRegs.
4875 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004876 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004877 std::set<unsigned> &InputRegs,
4878 const TargetRegisterInfo &TRI) const {
4879 if (isOutReg) {
4880 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4881 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4882 }
4883 if (isInReg) {
4884 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4885 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4886 }
4887 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004888
Owen Andersone50ed302009-08-10 22:56:29 +00004889 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004890 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004891 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00004892 EVT getCallOperandValEVT(LLVMContext &Context,
4893 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004894 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004895 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004896
Chris Lattner81249c92008-10-17 17:05:25 +00004897 if (isa<BasicBlock>(CallOperandVal))
4898 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004899
Chris Lattner81249c92008-10-17 17:05:25 +00004900 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004901
Chris Lattner81249c92008-10-17 17:05:25 +00004902 // If this is an indirect operand, the operand is a pointer to the
4903 // accessed type.
4904 if (isIndirect)
4905 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004906
Chris Lattner81249c92008-10-17 17:05:25 +00004907 // If OpTy is not a single value, it may be a struct/union that we
4908 // can tile with integers.
4909 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4910 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4911 switch (BitSize) {
4912 default: break;
4913 case 1:
4914 case 8:
4915 case 16:
4916 case 32:
4917 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004918 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004919 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004920 break;
4921 }
4922 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004923
Chris Lattner81249c92008-10-17 17:05:25 +00004924 return TLI.getValueType(OpTy, true);
4925 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004926
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004927private:
4928 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4929 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004930 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004931 const TargetRegisterInfo &TRI) {
4932 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4933 Regs.insert(Reg);
4934 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4935 for (; *Aliases; ++Aliases)
4936 Regs.insert(*Aliases);
4937 }
4938};
4939} // end llvm namespace.
4940
4941
4942/// GetRegistersForValue - Assign registers (virtual or physical) for the
4943/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004944/// register allocator to handle the assignment process. However, if the asm
4945/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004946/// allocation. This produces generally horrible, but correct, code.
4947///
4948/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004949/// Input and OutputRegs are the set of already allocated physical registers.
4950///
Dan Gohman2048b852009-11-23 18:04:58 +00004951void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004952GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004953 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004954 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004955 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004956
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004957 // Compute whether this value requires an input register, an output register,
4958 // or both.
4959 bool isOutReg = false;
4960 bool isInReg = false;
4961 switch (OpInfo.Type) {
4962 case InlineAsm::isOutput:
4963 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004964
4965 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004966 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004967 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004968 break;
4969 case InlineAsm::isInput:
4970 isInReg = true;
4971 isOutReg = false;
4972 break;
4973 case InlineAsm::isClobber:
4974 isOutReg = true;
4975 isInReg = true;
4976 break;
4977 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004978
4979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004980 MachineFunction &MF = DAG.getMachineFunction();
4981 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004982
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004983 // If this is a constraint for a single physreg, or a constraint for a
4984 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004985 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004986 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4987 OpInfo.ConstraintVT);
4988
4989 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004990 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004991 // If this is a FP input in an integer register (or visa versa) insert a bit
4992 // cast of the input value. More generally, handle any case where the input
4993 // value disagrees with the register class we plan to stick this in.
4994 if (OpInfo.Type == InlineAsm::isInput &&
4995 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004996 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004997 // types are identical size, use a bitcast to convert (e.g. two differing
4998 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004999 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005000 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005001 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005002 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005003 OpInfo.ConstraintVT = RegVT;
5004 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5005 // If the input is a FP value and we want it in FP registers, do a
5006 // bitcast to the corresponding integer type. This turns an f64 value
5007 // into i64, which can be passed with two i32 values on a 32-bit
5008 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005009 RegVT = EVT::getIntegerVT(Context,
5010 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005011 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005012 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005013 OpInfo.ConstraintVT = RegVT;
5014 }
5015 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005016
Owen Anderson23b9b192009-08-12 00:36:31 +00005017 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005018 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005019
Owen Andersone50ed302009-08-10 22:56:29 +00005020 EVT RegVT;
5021 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005022
5023 // If this is a constraint for a specific physical register, like {r17},
5024 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005025 if (unsigned AssignedReg = PhysReg.first) {
5026 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005027 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005028 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005030 // Get the actual register value type. This is important, because the user
5031 // may have asked for (e.g.) the AX register in i32 type. We need to
5032 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005033 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005035 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005036 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005037
5038 // If this is an expanded reference, add the rest of the regs to Regs.
5039 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005040 TargetRegisterClass::iterator I = RC->begin();
5041 for (; *I != AssignedReg; ++I)
5042 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005043
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005044 // Already added the first reg.
5045 --NumRegs; ++I;
5046 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005047 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005048 Regs.push_back(*I);
5049 }
5050 }
5051 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5052 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5053 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5054 return;
5055 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005057 // Otherwise, if this was a reference to an LLVM register class, create vregs
5058 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005059 if (const TargetRegisterClass *RC = PhysReg.second) {
5060 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005061 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005062 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005063
Evan Chengfb112882009-03-23 08:01:15 +00005064 // Create the appropriate number of virtual registers.
5065 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5066 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005067 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005068
Evan Chengfb112882009-03-23 08:01:15 +00005069 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5070 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005071 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005072
5073 // This is a reference to a register class that doesn't directly correspond
5074 // to an LLVM register class. Allocate NumRegs consecutive, available,
5075 // registers from the class.
5076 std::vector<unsigned> RegClassRegs
5077 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5078 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005079
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005080 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5081 unsigned NumAllocated = 0;
5082 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5083 unsigned Reg = RegClassRegs[i];
5084 // See if this register is available.
5085 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5086 (isInReg && InputRegs.count(Reg))) { // Already used.
5087 // Make sure we find consecutive registers.
5088 NumAllocated = 0;
5089 continue;
5090 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005092 // Check to see if this register is allocatable (i.e. don't give out the
5093 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005094 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5095 if (!RC) { // Couldn't allocate this register.
5096 // Reset NumAllocated to make sure we return consecutive registers.
5097 NumAllocated = 0;
5098 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005099 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005100
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005101 // Okay, this register is good, we can use it.
5102 ++NumAllocated;
5103
5104 // If we allocated enough consecutive registers, succeed.
5105 if (NumAllocated == NumRegs) {
5106 unsigned RegStart = (i-NumAllocated)+1;
5107 unsigned RegEnd = i+1;
5108 // Mark all of the allocated registers used.
5109 for (unsigned i = RegStart; i != RegEnd; ++i)
5110 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005111
5112 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005113 OpInfo.ConstraintVT);
5114 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5115 return;
5116 }
5117 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005118
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005119 // Otherwise, we couldn't allocate enough registers for this.
5120}
5121
Evan Chengda43bcf2008-09-24 00:05:32 +00005122/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5123/// processed uses a memory 'm' constraint.
5124static bool
5125hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005126 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005127 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5128 InlineAsm::ConstraintInfo &CI = CInfos[i];
5129 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5130 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5131 if (CType == TargetLowering::C_Memory)
5132 return true;
5133 }
Chris Lattner6c147292009-04-30 00:48:50 +00005134
5135 // Indirect operand accesses access memory.
5136 if (CI.isIndirect)
5137 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005138 }
5139
5140 return false;
5141}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005142
5143/// visitInlineAsm - Handle a call to an InlineAsm object.
5144///
Dan Gohman2048b852009-11-23 18:04:58 +00005145void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005146 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5147
5148 /// ConstraintOperands - Information about all of the constraints.
5149 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005150
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005151 std::set<unsigned> OutputRegs, InputRegs;
5152
5153 // Do a prepass over the constraints, canonicalizing them, and building up the
5154 // ConstraintOperands list.
5155 std::vector<InlineAsm::ConstraintInfo>
5156 ConstraintInfos = IA->ParseConstraints();
5157
Evan Chengda43bcf2008-09-24 00:05:32 +00005158 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005159
5160 SDValue Chain, Flag;
5161
5162 // We won't need to flush pending loads if this asm doesn't touch
5163 // memory and is nonvolatile.
5164 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005165 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005166 else
5167 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005169 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5170 unsigned ResNo = 0; // ResNo - The result number of the next output.
5171 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5172 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5173 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005174
Owen Anderson825b72b2009-08-11 20:47:22 +00005175 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005176
5177 // Compute the value type for each operand.
5178 switch (OpInfo.Type) {
5179 case InlineAsm::isOutput:
5180 // Indirect outputs just consume an argument.
5181 if (OpInfo.isIndirect) {
5182 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5183 break;
5184 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005186 // The return value of the call is this value. As such, there is no
5187 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005188 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5189 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005190 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5191 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5192 } else {
5193 assert(ResNo == 0 && "Asm only has one result!");
5194 OpVT = TLI.getValueType(CS.getType());
5195 }
5196 ++ResNo;
5197 break;
5198 case InlineAsm::isInput:
5199 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5200 break;
5201 case InlineAsm::isClobber:
5202 // Nothing to do.
5203 break;
5204 }
5205
5206 // If this is an input or an indirect output, process the call argument.
5207 // BasicBlocks are labels, currently appearing only in asm's.
5208 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005209 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005210 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5211
Chris Lattner81249c92008-10-17 17:05:25 +00005212 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005213 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005214 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005215 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005216 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005217
Owen Anderson1d0be152009-08-13 21:58:54 +00005218 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005219 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005220
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005221 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005222 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005223
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005224 // Second pass over the constraints: compute which constraint option to use
5225 // and assign registers to constraints that want a specific physreg.
5226 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5227 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005228
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005229 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005230 // matching input. If their types mismatch, e.g. one is an integer, the
5231 // other is floating point, or their sizes are different, flag it as an
5232 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005233 if (OpInfo.hasMatchingInput()) {
5234 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5235 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005236 if ((OpInfo.ConstraintVT.isInteger() !=
5237 Input.ConstraintVT.isInteger()) ||
5238 (OpInfo.ConstraintVT.getSizeInBits() !=
5239 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005240 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005241 " with a matching output constraint of incompatible"
5242 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005243 }
5244 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005245 }
5246 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005247
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005248 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005249 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005251 // If this is a memory input, and if the operand is not indirect, do what we
5252 // need to to provide an address for the memory input.
5253 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5254 !OpInfo.isIndirect) {
5255 assert(OpInfo.Type == InlineAsm::isInput &&
5256 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 // Memory operands really want the address of the value. If we don't have
5259 // an indirect input, put it in the constpool if we can, otherwise spill
5260 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262 // If the operand is a float, integer, or vector constant, spill to a
5263 // constant pool entry to get its address.
5264 Value *OpVal = OpInfo.CallOperandVal;
5265 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5266 isa<ConstantVector>(OpVal)) {
5267 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5268 TLI.getPointerTy());
5269 } else {
5270 // Otherwise, create a stack slot and emit a store to it before the
5271 // asm.
5272 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005273 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005274 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5275 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005276 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005277 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005278 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005279 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005280 OpInfo.CallOperand = StackSlot;
5281 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005282
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005283 // There is no longer a Value* corresponding to this operand.
5284 OpInfo.CallOperandVal = 0;
5285 // It is now an indirect operand.
5286 OpInfo.isIndirect = true;
5287 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005288
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005289 // If this constraint is for a specific register, allocate it before
5290 // anything else.
5291 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005292 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005293 }
5294 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005295
5296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005297 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005298 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005299 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5300 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005301
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005302 // C_Register operands have already been allocated, Other/Memory don't need
5303 // to be.
5304 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005305 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005306 }
5307
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005308 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5309 std::vector<SDValue> AsmNodeOperands;
5310 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5311 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005312 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005313
5314
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005315 // Loop over all of the inputs, copying the operand values into the
5316 // appropriate registers and processing the output regs.
5317 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005318
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005319 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5320 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005321
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005322 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5323 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5324
5325 switch (OpInfo.Type) {
5326 case InlineAsm::isOutput: {
5327 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5328 OpInfo.ConstraintType != TargetLowering::C_Register) {
5329 // Memory output, or 'other' output (e.g. 'X' constraint).
5330 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5331
5332 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005333 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5334 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 TLI.getPointerTy()));
5336 AsmNodeOperands.push_back(OpInfo.CallOperand);
5337 break;
5338 }
5339
5340 // Otherwise, this is a register or register class output.
5341
5342 // Copy the output from the appropriate register. Find a register that
5343 // we can use.
5344 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005345 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005346 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005347 }
5348
5349 // If this is an indirect operand, store through the pointer after the
5350 // asm.
5351 if (OpInfo.isIndirect) {
5352 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5353 OpInfo.CallOperandVal));
5354 } else {
5355 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005356 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5357 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005358 // Concatenate this output onto the outputs list.
5359 RetValRegs.append(OpInfo.AssignedRegs);
5360 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005361
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005362 // Add information to the INLINEASM node to know that this register is
5363 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005364 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5365 6 /* EARLYCLOBBER REGDEF */ :
5366 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005367 false,
5368 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005369 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005370 break;
5371 }
5372 case InlineAsm::isInput: {
5373 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005374
Chris Lattner6bdcda32008-10-17 16:47:46 +00005375 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005376 // If this is required to match an output register we have already set,
5377 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005378 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005379
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005380 // Scan until we find the definition we already emitted of this operand.
5381 // When we find it, create a RegsForValue operand.
5382 unsigned CurOp = 2; // The first operand.
5383 for (; OperandNo; --OperandNo) {
5384 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005385 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005386 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005387 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5388 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5389 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005390 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005391 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005392 }
5393
Evan Cheng697cbbf2009-03-20 18:03:34 +00005394 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005395 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005396 if ((OpFlag & 7) == 2 /*REGDEF*/
5397 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5398 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005399 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005400 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005401 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005402 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005403 RegsForValue MatchedRegs;
5404 MatchedRegs.TLI = &TLI;
5405 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005406 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005407 MatchedRegs.RegVTs.push_back(RegVT);
5408 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005409 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005410 i != e; ++i)
5411 MatchedRegs.Regs.
5412 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005413
5414 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005415 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5416 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005417 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5418 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005419 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005420 break;
5421 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005422 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5423 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5424 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005425 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005426 // See InlineAsm.h isUseOperandTiedToDef.
5427 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005428 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005429 TLI.getPointerTy()));
5430 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5431 break;
5432 }
5433 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005434
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005435 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005436 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005437 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005438
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005439 std::vector<SDValue> Ops;
5440 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005441 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005442 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005443 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005444 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005445 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005447 // Add information to the INLINEASM node to know about this input.
5448 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005449 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005450 TLI.getPointerTy()));
5451 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5452 break;
5453 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5454 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5455 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5456 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005457
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005458 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005459 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5460 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005461 TLI.getPointerTy()));
5462 AsmNodeOperands.push_back(InOperandVal);
5463 break;
5464 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005465
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005466 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5467 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5468 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005469 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005470 "Don't know how to handle indirect register inputs yet!");
5471
5472 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005473 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005474 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005475 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005476 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005477
Dale Johannesen66978ee2009-01-31 02:22:37 +00005478 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5479 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005480
Evan Cheng697cbbf2009-03-20 18:03:34 +00005481 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005482 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005483 break;
5484 }
5485 case InlineAsm::isClobber: {
5486 // Add the clobbered value to the operand list, so that the register
5487 // allocator is aware that the physreg got clobbered.
5488 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005489 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005490 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005491 break;
5492 }
5493 }
5494 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005495
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005496 // Finish up input operands.
5497 AsmNodeOperands[0] = Chain;
5498 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005499
Dale Johannesen66978ee2009-01-31 02:22:37 +00005500 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005501 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005502 &AsmNodeOperands[0], AsmNodeOperands.size());
5503 Flag = Chain.getValue(1);
5504
5505 // If this asm returns a register value, copy the result from that register
5506 // and set it as the value of the call.
5507 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005508 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005509 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005510
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005511 // FIXME: Why don't we do this for inline asms with MRVs?
5512 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005513 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005514
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005515 // If any of the results of the inline asm is a vector, it may have the
5516 // wrong width/num elts. This can happen for register classes that can
5517 // contain multiple different value types. The preg or vreg allocated may
5518 // not have the same VT as was expected. Convert it to the right type
5519 // with bit_convert.
5520 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005521 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005522 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005523
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005524 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005525 ResultType.isInteger() && Val.getValueType().isInteger()) {
5526 // If a result value was tied to an input value, the computed result may
5527 // have a wider width than the expected result. Extract the relevant
5528 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005529 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005530 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005531
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005532 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005533 }
Dan Gohman95915732008-10-18 01:03:45 +00005534
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005535 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005536 // Don't need to use this as a chain in this case.
5537 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5538 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005539 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005542
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005543 // Process indirect outputs, first output all of the flagged copies out of
5544 // physregs.
5545 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5546 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5547 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005548 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5549 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005550 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005551
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005552 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005553
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005554 // Emit the non-flagged stores from the physregs.
5555 SmallVector<SDValue, 8> OutChains;
5556 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005557 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005558 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005559 getValue(StoresToEmit[i].second),
5560 StoresToEmit[i].second, 0));
5561 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005563 &OutChains[0], OutChains.size());
5564 DAG.setRoot(Chain);
5565}
5566
Dan Gohman2048b852009-11-23 18:04:58 +00005567void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005568 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005569 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005570 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005571 DAG.getSrcValue(I.getOperand(1))));
5572}
5573
Dan Gohman2048b852009-11-23 18:04:58 +00005574void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005575 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5576 getRoot(), getValue(I.getOperand(0)),
5577 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005578 setValue(&I, V);
5579 DAG.setRoot(V.getValue(1));
5580}
5581
Dan Gohman2048b852009-11-23 18:04:58 +00005582void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005583 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005584 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005585 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005586 DAG.getSrcValue(I.getOperand(1))));
5587}
5588
Dan Gohman2048b852009-11-23 18:04:58 +00005589void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005590 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005591 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005592 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005593 getValue(I.getOperand(2)),
5594 DAG.getSrcValue(I.getOperand(1)),
5595 DAG.getSrcValue(I.getOperand(2))));
5596}
5597
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005598/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005599/// implementation, which just calls LowerCall.
5600/// FIXME: When all targets are
5601/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005602std::pair<SDValue, SDValue>
5603TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5604 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005605 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005606 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005607 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005608 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005609 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005610
Dan Gohman1937e2f2008-09-16 01:42:28 +00005611 assert((!isTailCall || PerformTailCallOpt) &&
5612 "isTailCall set when tail-call optimizations are disabled!");
5613
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005614 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005615 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005616 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005617 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005618 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5619 for (unsigned Value = 0, NumValues = ValueVTs.size();
5620 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005621 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005622 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005623 SDValue Op = SDValue(Args[i].Node.getNode(),
5624 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005625 ISD::ArgFlagsTy Flags;
5626 unsigned OriginalAlignment =
5627 getTargetData()->getABITypeAlignment(ArgTy);
5628
5629 if (Args[i].isZExt)
5630 Flags.setZExt();
5631 if (Args[i].isSExt)
5632 Flags.setSExt();
5633 if (Args[i].isInReg)
5634 Flags.setInReg();
5635 if (Args[i].isSRet)
5636 Flags.setSRet();
5637 if (Args[i].isByVal) {
5638 Flags.setByVal();
5639 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5640 const Type *ElementTy = Ty->getElementType();
5641 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005642 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005643 // For ByVal, alignment should come from FE. BE will guess if this
5644 // info is not there but there are cases it cannot get right.
5645 if (Args[i].Alignment)
5646 FrameAlign = Args[i].Alignment;
5647 Flags.setByValAlign(FrameAlign);
5648 Flags.setByValSize(FrameSize);
5649 }
5650 if (Args[i].isNest)
5651 Flags.setNest();
5652 Flags.setOrigAlign(OriginalAlignment);
5653
Owen Anderson23b9b192009-08-12 00:36:31 +00005654 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5655 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005656 SmallVector<SDValue, 4> Parts(NumParts);
5657 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5658
5659 if (Args[i].isSExt)
5660 ExtendKind = ISD::SIGN_EXTEND;
5661 else if (Args[i].isZExt)
5662 ExtendKind = ISD::ZERO_EXTEND;
5663
Dale Johannesen66978ee2009-01-31 02:22:37 +00005664 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005665
Dan Gohman98ca4f22009-08-05 01:29:28 +00005666 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005667 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005668 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5669 if (NumParts > 1 && j == 0)
5670 MyFlags.Flags.setSplit();
5671 else if (j != 0)
5672 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005673
Dan Gohman98ca4f22009-08-05 01:29:28 +00005674 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005675 }
5676 }
5677 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005678
Dan Gohman98ca4f22009-08-05 01:29:28 +00005679 // Handle the incoming return values from the call.
5680 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005681 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005682 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005683 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005684 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005685 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5686 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005687 for (unsigned i = 0; i != NumRegs; ++i) {
5688 ISD::InputArg MyFlags;
5689 MyFlags.VT = RegisterVT;
5690 MyFlags.Used = isReturnValueUsed;
5691 if (RetSExt)
5692 MyFlags.Flags.setSExt();
5693 if (RetZExt)
5694 MyFlags.Flags.setZExt();
5695 if (isInreg)
5696 MyFlags.Flags.setInReg();
5697 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005698 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005699 }
5700
Dan Gohman98ca4f22009-08-05 01:29:28 +00005701 // Check if target-dependent constraints permit a tail call here.
5702 // Target-independent constraints should be checked by the caller.
5703 if (isTailCall &&
5704 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
5705 isTailCall = false;
5706
5707 SmallVector<SDValue, 4> InVals;
5708 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
5709 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005710
5711 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005712 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005713 "LowerCall didn't return a valid chain!");
5714 assert((!isTailCall || InVals.empty()) &&
5715 "LowerCall emitted a return value for a tail call!");
5716 assert((isTailCall || InVals.size() == Ins.size()) &&
5717 "LowerCall didn't emit the correct number of values!");
5718 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5719 assert(InVals[i].getNode() &&
5720 "LowerCall emitted a null value!");
5721 assert(Ins[i].VT == InVals[i].getValueType() &&
5722 "LowerCall emitted a value with the wrong type!");
5723 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00005724
5725 // For a tail call, the return value is merely live-out and there aren't
5726 // any nodes in the DAG representing it. Return a special value to
5727 // indicate that a tail call has been emitted and no more Instructions
5728 // should be processed in the current block.
5729 if (isTailCall) {
5730 DAG.setRoot(Chain);
5731 return std::make_pair(SDValue(), SDValue());
5732 }
5733
5734 // Collect the legal value parts into potentially illegal values
5735 // that correspond to the original function's return values.
5736 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5737 if (RetSExt)
5738 AssertOp = ISD::AssertSext;
5739 else if (RetZExt)
5740 AssertOp = ISD::AssertZext;
5741 SmallVector<SDValue, 4> ReturnValues;
5742 unsigned CurReg = 0;
5743 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005744 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005745 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5746 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005747
5748 SDValue ReturnValue =
5749 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
5750 AssertOp);
5751 ReturnValues.push_back(ReturnValue);
5752 CurReg += NumRegs;
5753 }
5754
5755 // For a function returning void, there is no return value. We can't create
5756 // such a node, so we just return a null return value in that case. In
5757 // that case, nothing will actualy look at the value.
5758 if (ReturnValues.empty())
5759 return std::make_pair(SDValue(), Chain);
5760
5761 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5762 DAG.getVTList(&RetTys[0], RetTys.size()),
5763 &ReturnValues[0], ReturnValues.size());
5764
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005765 return std::make_pair(Res, Chain);
5766}
5767
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005768void TargetLowering::LowerOperationWrapper(SDNode *N,
5769 SmallVectorImpl<SDValue> &Results,
5770 SelectionDAG &DAG) {
5771 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005772 if (Res.getNode())
5773 Results.push_back(Res);
5774}
5775
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005776SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005777 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005778 return SDValue();
5779}
5780
5781
Dan Gohman2048b852009-11-23 18:04:58 +00005782void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005783 SDValue Op = getValue(V);
5784 assert((Op.getOpcode() != ISD::CopyFromReg ||
5785 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5786 "Copy from a reg to the same reg!");
5787 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5788
Owen Anderson23b9b192009-08-12 00:36:31 +00005789 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005790 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005791 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005792 PendingExports.push_back(Chain);
5793}
5794
5795#include "llvm/CodeGen/SelectionDAGISel.h"
5796
Dan Gohman8c2b5252009-10-30 01:27:03 +00005797void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005798 // If this is the entry block, emit arguments.
5799 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005800 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005801 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005802 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005803 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005804 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005805
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005806 // Check whether the function can return without sret-demotion.
5807 SmallVector<EVT, 4> OutVTs;
5808 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005809 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
5810 OutVTs, OutsFlags, TLI);
5811 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5812
5813 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
5814 OutVTs, OutsFlags, DAG);
5815 if (!FLI.CanLowerReturn) {
5816 // Put in an sret pointer parameter before all the other parameters.
5817 SmallVector<EVT, 1> ValueVTs;
5818 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5819
5820 // NOTE: Assuming that a pointer will never break down to more than one VT
5821 // or one register.
5822 ISD::ArgFlagsTy Flags;
5823 Flags.setSRet();
5824 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5825 ISD::InputArg RetArg(Flags, RegisterVT, true);
5826 Ins.push_back(RetArg);
5827 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005828
Dan Gohman98ca4f22009-08-05 01:29:28 +00005829 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005830 unsigned Idx = 1;
5831 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5832 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005833 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005834 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5835 bool isArgValueUsed = !I->use_empty();
5836 for (unsigned Value = 0, NumValues = ValueVTs.size();
5837 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005838 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005839 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005840 ISD::ArgFlagsTy Flags;
5841 unsigned OriginalAlignment =
5842 TD->getABITypeAlignment(ArgTy);
5843
5844 if (F.paramHasAttr(Idx, Attribute::ZExt))
5845 Flags.setZExt();
5846 if (F.paramHasAttr(Idx, Attribute::SExt))
5847 Flags.setSExt();
5848 if (F.paramHasAttr(Idx, Attribute::InReg))
5849 Flags.setInReg();
5850 if (F.paramHasAttr(Idx, Attribute::StructRet))
5851 Flags.setSRet();
5852 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5853 Flags.setByVal();
5854 const PointerType *Ty = cast<PointerType>(I->getType());
5855 const Type *ElementTy = Ty->getElementType();
5856 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5857 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5858 // For ByVal, alignment should be passed from FE. BE will guess if
5859 // this info is not there but there are cases it cannot get right.
5860 if (F.getParamAlignment(Idx))
5861 FrameAlign = F.getParamAlignment(Idx);
5862 Flags.setByValAlign(FrameAlign);
5863 Flags.setByValSize(FrameSize);
5864 }
5865 if (F.paramHasAttr(Idx, Attribute::Nest))
5866 Flags.setNest();
5867 Flags.setOrigAlign(OriginalAlignment);
5868
Owen Anderson23b9b192009-08-12 00:36:31 +00005869 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5870 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005871 for (unsigned i = 0; i != NumRegs; ++i) {
5872 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5873 if (NumRegs > 1 && i == 0)
5874 MyFlags.Flags.setSplit();
5875 // if it isn't first piece, alignment must be 1
5876 else if (i > 0)
5877 MyFlags.Flags.setOrigAlign(1);
5878 Ins.push_back(MyFlags);
5879 }
5880 }
5881 }
5882
5883 // Call the target to set up the argument values.
5884 SmallVector<SDValue, 8> InVals;
5885 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5886 F.isVarArg(), Ins,
5887 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005888
5889 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005890 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005891 "LowerFormalArguments didn't return a valid chain!");
5892 assert(InVals.size() == Ins.size() &&
5893 "LowerFormalArguments didn't emit the correct number of values!");
5894 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5895 assert(InVals[i].getNode() &&
5896 "LowerFormalArguments emitted a null value!");
5897 assert(Ins[i].VT == InVals[i].getValueType() &&
5898 "LowerFormalArguments emitted a value with the wrong type!");
5899 });
5900
5901 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005902 DAG.setRoot(NewRoot);
5903
5904 // Set up the argument values.
5905 unsigned i = 0;
5906 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005907 if (!FLI.CanLowerReturn) {
5908 // Create a virtual register for the sret pointer, and put in a copy
5909 // from the sret argument into it.
5910 SmallVector<EVT, 1> ValueVTs;
5911 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5912 EVT VT = ValueVTs[0];
5913 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5914 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5915 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT,
5916 VT, AssertOp);
5917
Dan Gohman2048b852009-11-23 18:04:58 +00005918 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005919 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5920 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5921 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00005922 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005923 DAG.setRoot(NewRoot);
5924
5925 // i indexes lowered arguments. Bump it past the hidden sret argument.
5926 // Idx indexes LLVM arguments. Don't touch it.
5927 ++i;
5928 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005929 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5930 ++I, ++Idx) {
5931 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005932 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005933 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005935 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005936 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005937 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5938 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005939
5940 if (!I->use_empty()) {
5941 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5942 if (F.paramHasAttr(Idx, Attribute::SExt))
5943 AssertOp = ISD::AssertSext;
5944 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5945 AssertOp = ISD::AssertZext;
5946
5947 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
5948 PartVT, VT, AssertOp));
5949 }
5950 i += NumParts;
5951 }
5952 if (!I->use_empty()) {
Dan Gohman2048b852009-11-23 18:04:58 +00005953 SDB->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
5954 SDB->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005955 // If this argument is live outside of the entry block, insert a copy from
5956 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005957 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005958 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005959 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005960 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005961
5962 // Finally, if the target has anything special to do, allow it to do so.
5963 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00005964 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005965}
5966
5967/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5968/// ensure constants are generated when needed. Remember the virtual registers
5969/// that need to be added to the Machine PHI nodes as input. We cannot just
5970/// directly add them, because expansion might result in multiple MBB's for one
5971/// BB. As such, the start of the BB might correspond to a different MBB than
5972/// the end.
5973///
5974void
5975SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5976 TerminatorInst *TI = LLVMBB->getTerminator();
5977
5978 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5979
5980 // Check successor nodes' PHI nodes that expect a constant to be available
5981 // from this block.
5982 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5983 BasicBlock *SuccBB = TI->getSuccessor(succ);
5984 if (!isa<PHINode>(SuccBB->begin())) continue;
5985 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005986
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005987 // If this terminator has multiple identical successors (common for
5988 // switches), only handle each succ once.
5989 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005990
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005991 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5992 PHINode *PN;
5993
5994 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5995 // nodes and Machine PHI nodes, but the incoming operands have not been
5996 // emitted yet.
5997 for (BasicBlock::iterator I = SuccBB->begin();
5998 (PN = dyn_cast<PHINode>(I)); ++I) {
5999 // Ignore dead phi's.
6000 if (PN->use_empty()) continue;
6001
6002 unsigned Reg;
6003 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6004
6005 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006006 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006007 if (RegOut == 0) {
6008 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006009 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006010 }
6011 Reg = RegOut;
6012 } else {
6013 Reg = FuncInfo->ValueMap[PHIOp];
6014 if (Reg == 0) {
6015 assert(isa<AllocaInst>(PHIOp) &&
6016 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6017 "Didn't codegen value into a register!??");
6018 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006019 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006020 }
6021 }
6022
6023 // Remember that this register needs to added to the machine PHI node as
6024 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006025 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006026 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6027 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006028 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006029 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006030 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006031 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006032 Reg += NumRegisters;
6033 }
6034 }
6035 }
Dan Gohman2048b852009-11-23 18:04:58 +00006036 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006037}
6038
Dan Gohman3df24e62008-09-03 23:12:08 +00006039/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6040/// supports legal types, and it emits MachineInstrs directly instead of
6041/// creating SelectionDAG nodes.
6042///
6043bool
6044SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6045 FastISel *F) {
6046 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006047
Dan Gohman3df24e62008-09-03 23:12:08 +00006048 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006049 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006050
6051 // Check successor nodes' PHI nodes that expect a constant to be available
6052 // from this block.
6053 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6054 BasicBlock *SuccBB = TI->getSuccessor(succ);
6055 if (!isa<PHINode>(SuccBB->begin())) continue;
6056 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006057
Dan Gohman3df24e62008-09-03 23:12:08 +00006058 // If this terminator has multiple identical successors (common for
6059 // switches), only handle each succ once.
6060 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006061
Dan Gohman3df24e62008-09-03 23:12:08 +00006062 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6063 PHINode *PN;
6064
6065 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6066 // nodes and Machine PHI nodes, but the incoming operands have not been
6067 // emitted yet.
6068 for (BasicBlock::iterator I = SuccBB->begin();
6069 (PN = dyn_cast<PHINode>(I)); ++I) {
6070 // Ignore dead phi's.
6071 if (PN->use_empty()) continue;
6072
6073 // Only handle legal types. Two interesting things to note here. First,
6074 // by bailing out early, we may leave behind some dead instructions,
6075 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6076 // own moves. Second, this check is necessary becuase FastISel doesn't
6077 // use CreateRegForValue to create registers, so it always creates
6078 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006079 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006080 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6081 // Promote MVT::i1.
6082 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006083 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006084 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006085 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006086 return false;
6087 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006088 }
6089
6090 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6091
6092 unsigned Reg = F->getRegForValue(PHIOp);
6093 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006094 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006095 return false;
6096 }
Dan Gohman2048b852009-11-23 18:04:58 +00006097 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006098 }
6099 }
6100
6101 return true;
6102}