blob: 1ee1fdce9d97384bb5f30ef8c0f48e904608e057 [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Dan Gohman2048b852009-11-23 18:04:58 +000015#include "SelectionDAGBuilder.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000016#include "FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000018#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000019#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Constants.h"
21#include "llvm/CallingConv.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/Function.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/InlineAsm.h"
26#include "llvm/Instructions.h"
27#include "llvm/Intrinsics.h"
28#include "llvm/IntrinsicInst.h"
Devang Patel53bb5c92009-11-10 23:06:00 +000029#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000030#include "llvm/Module.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000031#include "llvm/CodeGen/FastISel.h"
32#include "llvm/CodeGen/GCStrategy.h"
33#include "llvm/CodeGen/GCMetadata.h"
34#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
37#include "llvm/CodeGen/MachineJumpTableInfo.h"
38#include "llvm/CodeGen/MachineModuleInfo.h"
39#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000040#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000041#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000042#include "llvm/CodeGen/DwarfWriter.h"
43#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044#include "llvm/Target/TargetRegisterInfo.h"
45#include "llvm/Target/TargetData.h"
46#include "llvm/Target/TargetFrameInfo.h"
47#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000048#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000049#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000050#include "llvm/Target/TargetOptions.h"
51#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000052#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000053#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000054#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000056#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057#include <algorithm>
58using namespace llvm;
59
Dale Johannesen601d3c02008-09-05 01:48:15 +000060/// LimitFloatPrecision - Generate low-precision inline sequences for
61/// some float libcalls (6, 8 or 12 bits).
62static unsigned LimitFloatPrecision;
63
64static cl::opt<unsigned, true>
65LimitFPPrecision("limit-float-precision",
66 cl::desc("Generate low-precision inline sequences "
67 "for some float libcalls"),
68 cl::location(LimitFloatPrecision),
69 cl::init(0));
70
Dan Gohmanf9bd4502009-11-23 17:46:23 +000071namespace {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000072 /// RegsForValue - This struct represents the registers (physical or virtual)
73 /// that a particular set of values is assigned, and the type information about
74 /// the value. The most common situation is to represent one value at a time,
75 /// but struct or array values are handled element-wise as multiple values.
76 /// The splitting of aggregates is performed recursively, so that we never
77 /// have aggregate-typed registers. The values at this point do not necessarily
78 /// have legal types, so each value may require one or more registers of some
79 /// legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000080 ///
Dan Gohmanf9bd4502009-11-23 17:46:23 +000081 struct RegsForValue {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000082 /// TLI - The TargetLowering object.
83 ///
84 const TargetLowering *TLI;
85
86 /// ValueVTs - The value types of the values, which may not be legal, and
87 /// may need be promoted or synthesized from one or more registers.
88 ///
Owen Andersone50ed302009-08-10 22:56:29 +000089 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000091 /// RegVTs - The value types of the registers. This is the same size as
92 /// ValueVTs and it records, for each value, what the type of the assigned
93 /// register or registers are. (Individual values are never synthesized
94 /// from more than one type of register.)
95 ///
96 /// With virtual registers, the contents of RegVTs is redundant with TLI's
97 /// getRegisterType member function, however when with physical registers
98 /// it is necessary to have a separate record of the types.
99 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000100 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000102 /// Regs - This list holds the registers assigned to the values.
103 /// Each legal or promoted value requires one register, and each
104 /// expanded value requires multiple registers.
105 ///
106 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000108 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000111 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000112 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000113 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
114 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000115 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000116 const SmallVector<EVT, 4> &regvts,
117 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000118 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000119 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000120 unsigned Reg, const Type *Ty) : TLI(&tli) {
121 ComputeValueVTs(tli, Ty, ValueVTs);
122
123 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000124 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000125 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
126 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000127 for (unsigned i = 0; i != NumRegs; ++i)
128 Regs.push_back(Reg + i);
129 RegVTs.push_back(RegisterVT);
130 Reg += NumRegs;
131 }
132 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 /// append - Add the specified values to this one.
135 void append(const RegsForValue &RHS) {
136 TLI = RHS.TLI;
137 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
138 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
139 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
140 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000141
142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000143 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000144 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000145 /// Chain/Flag as the input and updates them for the output Chain/Flag.
146 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000147 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000148 SDValue &Chain, SDValue *Flag) const;
149
150 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000151 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000152 /// Chain/Flag as the input and updates them for the output Chain/Flag.
153 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000154 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000155 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000158 /// operand list. This adds the code marker, matching input operand index
159 /// (if applicable), and includes the number of values added into it.
160 void AddInlineAsmOperands(unsigned Code,
161 bool HasMatching, unsigned MatchingIdx,
162 SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000163 };
164}
165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000166/// getCopyFromParts - Create a value that contains the specified legal parts
167/// combined into the value they represent. If the parts combine to a type
168/// larger then ValueVT then AssertOp can be used to specify whether the extra
169/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
170/// (ISD::AssertSext).
Dale Johannesen66978ee2009-01-31 02:22:37 +0000171static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
172 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000173 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000174 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000175 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000176 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000177 SDValue Val = Parts[0];
178
179 if (NumParts > 1) {
180 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000181 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000182 unsigned PartBits = PartVT.getSizeInBits();
183 unsigned ValueBits = ValueVT.getSizeInBits();
184
185 // Assemble the power of 2 part.
186 unsigned RoundParts = NumParts & (NumParts - 1) ?
187 1 << Log2_32(NumParts) : NumParts;
188 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000189 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000190 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000191 SDValue Lo, Hi;
192
Owen Anderson23b9b192009-08-12 00:36:31 +0000193 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 if (RoundParts > 2) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000196 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT);
197 Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000198 PartVT, HalfVT);
199 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000200 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
201 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000202 }
203 if (TLI.isBigEndian())
204 std::swap(Lo, Hi);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000205 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000206
207 if (RoundParts < NumParts) {
208 // Assemble the trailing non-power-of-2 part.
209 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000210 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Scott Michelfdc40a02009-02-17 22:15:04 +0000211 Hi = getCopyFromParts(DAG, dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000212 Parts+RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000213
214 // Combine the round and odd parts.
215 Lo = Val;
216 if (TLI.isBigEndian())
217 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000218 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000219 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
220 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000221 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000222 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000223 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
224 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000225 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000226 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000227 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000228 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000229 unsigned NumIntermediates;
230 unsigned NumRegs =
Owen Anderson23b9b192009-08-12 00:36:31 +0000231 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
232 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000233 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
234 NumParts = NumRegs; // Silence a compiler warning.
235 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
236 assert(RegisterVT == Parts[0].getValueType() &&
237 "Part type doesn't match part!");
238
239 // Assemble the parts into intermediate operands.
240 SmallVector<SDValue, 8> Ops(NumIntermediates);
241 if (NumIntermediates == NumParts) {
242 // If the register was not expanded, truncate or copy the value,
243 // as appropriate.
244 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000245 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000246 PartVT, IntermediateVT);
247 } else if (NumParts > 0) {
248 // If the intermediate type was expanded, build the intermediate operands
249 // from the parts.
250 assert(NumParts % NumIntermediates == 0 &&
251 "Must expand into a divisible number of parts!");
252 unsigned Factor = NumParts / NumIntermediates;
253 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000254 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000255 PartVT, IntermediateVT);
256 }
257
258 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
259 // operands.
260 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000261 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000262 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000263 } else if (PartVT.isFloatingPoint()) {
264 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000266 "Unexpected split");
267 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
269 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000270 if (TLI.isBigEndian())
271 std::swap(Lo, Hi);
272 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
273 } else {
274 // FP split into integer parts (soft fp)
275 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
276 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000277 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Eli Friedman2ac8b322009-05-20 06:02:09 +0000278 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000279 }
280 }
281
282 // There is now one part, held in Val. Correct it to match ValueVT.
283 PartVT = Val.getValueType();
284
285 if (PartVT == ValueVT)
286 return Val;
287
288 if (PartVT.isVector()) {
289 assert(ValueVT.isVector() && "Unknown vector conversion!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000290 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000291 }
292
293 if (ValueVT.isVector()) {
294 assert(ValueVT.getVectorElementType() == PartVT &&
295 ValueVT.getVectorNumElements() == 1 &&
296 "Only trivial scalar-to-vector conversions should get here!");
Evan Chenga87008d2009-02-25 22:49:59 +0000297 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000298 }
299
300 if (PartVT.isInteger() &&
301 ValueVT.isInteger()) {
302 if (ValueVT.bitsLT(PartVT)) {
303 // For a truncate, see if we have any information to
304 // indicate whether the truncated bits will always be
305 // zero or sign-extension.
306 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000307 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000308 DAG.getValueType(ValueVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000309 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000310 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000311 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000312 }
313 }
314
315 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
316 if (ValueVT.bitsLT(Val.getValueType()))
317 // FP_ROUND's are always exact here.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000318 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000319 DAG.getIntPtrConstant(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000320 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000321 }
322
323 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000324 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000325
Torok Edwinc23197a2009-07-14 16:55:14 +0000326 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000327 return SDValue();
328}
329
330/// getCopyToParts - Create a series of nodes that contain the specified value
331/// split into legal parts. If the parts contain more bits than Val, then, for
332/// integers, ExtendKind can be used to specify how to generate the extra bits.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000333static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val,
Owen Andersone50ed302009-08-10 22:56:29 +0000334 SDValue *Parts, unsigned NumParts, EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000336 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000337 EVT PtrVT = TLI.getPointerTy();
338 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000339 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000340 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
342
343 if (!NumParts)
344 return;
345
346 if (!ValueVT.isVector()) {
347 if (PartVT == ValueVT) {
348 assert(NumParts == 1 && "No-op copy with multiple parts!");
349 Parts[0] = Val;
350 return;
351 }
352
353 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
354 // If the parts cover more bits than the value has, promote the value.
355 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
356 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000357 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000358 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000359 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000360 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000361 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000362 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000363 }
364 } else if (PartBits == ValueVT.getSizeInBits()) {
365 // Different types of the same size.
366 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000367 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000368 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
369 // If the parts cover less bits than value has, truncate the value.
370 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000371 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000372 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000373 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000374 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000375 }
376 }
377
378 // The value may have changed - recompute ValueVT.
379 ValueVT = Val.getValueType();
380 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
381 "Failed to tile the value with PartVT!");
382
383 if (NumParts == 1) {
384 assert(PartVT == ValueVT && "Type conversion failed!");
385 Parts[0] = Val;
386 return;
387 }
388
389 // Expand the value into multiple parts.
390 if (NumParts & (NumParts - 1)) {
391 // The number of parts is not a power of 2. Split off and copy the tail.
392 assert(PartVT.isInteger() && ValueVT.isInteger() &&
393 "Do not know what to expand to!");
394 unsigned RoundParts = 1 << Log2_32(NumParts);
395 unsigned RoundBits = RoundParts * PartBits;
396 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000397 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000398 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000399 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000400 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000401 if (TLI.isBigEndian())
402 // The odd parts were reversed by getCopyToParts - unreverse them.
403 std::reverse(Parts + RoundParts, Parts + NumParts);
404 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000405 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000406 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000407 }
408
409 // The number of parts is a power of 2. Repeatedly bisect the value using
410 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000411 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson23b9b192009-08-12 00:36:31 +0000412 EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000413 Val);
414 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
415 for (unsigned i = 0; i < NumParts; i += StepSize) {
416 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000417 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000418 SDValue &Part0 = Parts[i];
419 SDValue &Part1 = Parts[i+StepSize/2];
420
Scott Michelfdc40a02009-02-17 22:15:04 +0000421 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000422 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000423 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000424 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000425 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000426 DAG.getConstant(0, PtrVT));
427
428 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000429 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000430 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000431 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000432 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000433 }
434 }
435 }
436
437 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000438 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000439
440 return;
441 }
442
443 // Vector ValueVT.
444 if (NumParts == 1) {
445 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000446 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000447 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000448 } else {
449 assert(ValueVT.getVectorElementType() == PartVT &&
450 ValueVT.getVectorNumElements() == 1 &&
451 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000452 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000453 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000454 DAG.getConstant(0, PtrVT));
455 }
456 }
457
458 Parts[0] = Val;
459 return;
460 }
461
462 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000463 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000464 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000465 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
466 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000467 unsigned NumElements = ValueVT.getVectorNumElements();
468
469 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
470 NumParts = NumRegs; // Silence a compiler warning.
471 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
472
473 // Split the vector into intermediate operands.
474 SmallVector<SDValue, 8> Ops(NumIntermediates);
475 for (unsigned i = 0; i != NumIntermediates; ++i)
476 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000477 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000478 IntermediateVT, Val,
479 DAG.getConstant(i * (NumElements / NumIntermediates),
480 PtrVT));
481 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000482 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000483 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000484 DAG.getConstant(i, PtrVT));
485
486 // Split the intermediate operands into legal parts.
487 if (NumParts == NumIntermediates) {
488 // If the register was not expanded, promote or copy the value,
489 // as appropriate.
490 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000491 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000492 } else if (NumParts > 0) {
493 // If the intermediate type was expanded, split each the value into
494 // legal parts.
495 assert(NumParts % NumIntermediates == 0 &&
496 "Must expand into a divisible number of parts!");
497 unsigned Factor = NumParts / NumIntermediates;
498 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000499 getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000500 }
501}
502
503
Dan Gohman2048b852009-11-23 18:04:58 +0000504void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000505 AA = &aa;
506 GFI = gfi;
507 TD = DAG.getTarget().getTargetData();
508}
509
510/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000511/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000512/// for a new block. This doesn't clear out information about
513/// additional blocks that are needed to complete switch lowering
514/// or PHI node updating; that information is cleared out as it is
515/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000516void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000517 NodeMap.clear();
518 PendingLoads.clear();
519 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000520 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000521 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000522 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000523 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000524}
525
526/// getRoot - Return the current virtual root of the Selection DAG,
527/// flushing any PendingLoad items. This must be done before emitting
528/// a store or any other node that may need to be ordered after any
529/// prior load instructions.
530///
Dan Gohman2048b852009-11-23 18:04:58 +0000531SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000532 if (PendingLoads.empty())
533 return DAG.getRoot();
534
535 if (PendingLoads.size() == 1) {
536 SDValue Root = PendingLoads[0];
537 DAG.setRoot(Root);
538 PendingLoads.clear();
539 return Root;
540 }
541
542 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000544 &PendingLoads[0], PendingLoads.size());
545 PendingLoads.clear();
546 DAG.setRoot(Root);
547 return Root;
548}
549
550/// getControlRoot - Similar to getRoot, but instead of flushing all the
551/// PendingLoad items, flush all the PendingExports items. It is necessary
552/// to do this before emitting a terminator instruction.
553///
Dan Gohman2048b852009-11-23 18:04:58 +0000554SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000555 SDValue Root = DAG.getRoot();
556
557 if (PendingExports.empty())
558 return Root;
559
560 // Turn all of the CopyToReg chains into one factored node.
561 if (Root.getOpcode() != ISD::EntryToken) {
562 unsigned i = 0, e = PendingExports.size();
563 for (; i != e; ++i) {
564 assert(PendingExports[i].getNode()->getNumOperands() > 1);
565 if (PendingExports[i].getNode()->getOperand(0) == Root)
566 break; // Don't add the root if we already indirectly depend on it.
567 }
568
569 if (i == e)
570 PendingExports.push_back(Root);
571 }
572
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000574 &PendingExports[0],
575 PendingExports.size());
576 PendingExports.clear();
577 DAG.setRoot(Root);
578 return Root;
579}
580
Dan Gohman2048b852009-11-23 18:04:58 +0000581void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000582 visit(I.getOpcode(), I);
583}
584
Dan Gohman2048b852009-11-23 18:04:58 +0000585void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000586 // We're processing a new instruction.
587 ++SDNodeOrder;
588
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000589 // Note: this doesn't use InstVisitor, because it has to work with
590 // ConstantExpr's in addition to instructions.
591 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000592 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000593 // Build the switch statement using the Instruction.def file.
594#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000595 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000596#include "llvm/Instruction.def"
597 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000598}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000599
Dan Gohman2048b852009-11-23 18:04:58 +0000600SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000601 SDValue &N = NodeMap[V];
602 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000603
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000604 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000605 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000606
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000607 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000608 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000609
610 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
611 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000613 if (isa<ConstantPointerNull>(C))
614 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000615
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000616 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000617 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000618
Nate Begeman9008ca62009-04-27 18:41:29 +0000619 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000620 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000621
622 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
623 visit(CE->getOpcode(), *CE);
624 SDValue N1 = NodeMap[V];
625 assert(N1.getNode() && "visit didn't populate the ValueMap!");
626 return N1;
627 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000628
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000629 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
630 SmallVector<SDValue, 4> Constants;
631 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
632 OI != OE; ++OI) {
633 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000634 // If the operand is an empty aggregate, there are no values.
635 if (!Val) continue;
636 // Add each leaf value from the operand to the Constants list
637 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000638 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
639 Constants.push_back(SDValue(Val, i));
640 }
Bill Wendling87710f02009-12-21 23:47:40 +0000641
642 SDValue Res = DAG.getMergeValues(&Constants[0], Constants.size(),
643 getCurDebugLoc());
644 if (DisableScheduling)
645 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
646 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000647 }
648
649 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
650 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
651 "Unknown struct or array constant!");
652
Owen Andersone50ed302009-08-10 22:56:29 +0000653 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000654 ComputeValueVTs(TLI, C->getType(), ValueVTs);
655 unsigned NumElts = ValueVTs.size();
656 if (NumElts == 0)
657 return SDValue(); // empty struct
658 SmallVector<SDValue, 4> Constants(NumElts);
659 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000660 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000661 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000662 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000663 else if (EltVT.isFloatingPoint())
664 Constants[i] = DAG.getConstantFP(0, EltVT);
665 else
666 Constants[i] = DAG.getConstant(0, EltVT);
667 }
Bill Wendling87710f02009-12-21 23:47:40 +0000668
669 SDValue Res = DAG.getMergeValues(&Constants[0], NumElts,
670 getCurDebugLoc());
671 if (DisableScheduling)
672 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
673 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000674 }
675
Dan Gohman8c2b5252009-10-30 01:27:03 +0000676 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000677 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000679 const VectorType *VecTy = cast<VectorType>(V->getType());
680 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000681
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682 // Now that we know the number and type of the elements, get that number of
683 // elements into the Ops array based on what kind of constant it is.
684 SmallVector<SDValue, 16> Ops;
685 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
686 for (unsigned i = 0; i != NumElements; ++i)
687 Ops.push_back(getValue(CP->getOperand(i)));
688 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000689 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000690 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000691
692 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000693 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000694 Op = DAG.getConstantFP(0, EltVT);
695 else
696 Op = DAG.getConstant(0, EltVT);
697 Ops.assign(NumElements, Op);
698 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000699
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000700 // Create a BUILD_VECTOR node.
Bill Wendling87710f02009-12-21 23:47:40 +0000701 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
702 VT, &Ops[0], Ops.size());
703 if (DisableScheduling)
704 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
705
706 return NodeMap[V] = Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000707 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000708
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000709 // If this is a static alloca, generate it as the frameindex instead of
710 // computation.
711 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
712 DenseMap<const AllocaInst*, int>::iterator SI =
713 FuncInfo.StaticAllocaMap.find(AI);
714 if (SI != FuncInfo.StaticAllocaMap.end())
715 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
716 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000718 unsigned InReg = FuncInfo.ValueMap[V];
719 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000720
Owen Anderson23b9b192009-08-12 00:36:31 +0000721 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000722 SDValue Chain = DAG.getEntryNode();
Bill Wendling87710f02009-12-21 23:47:40 +0000723 SDValue Res = RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
724 if (DisableScheduling)
725 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
726 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000727}
728
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000729/// Get the EVTs and ArgFlags collections that represent the return type
730/// of the given function. This does not require a DAG or a return value, and
731/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000732static void getReturnInfo(const Type* ReturnType,
733 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000734 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000735 TargetLowering &TLI,
736 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000737 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000738 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000739 unsigned NumValues = ValueVTs.size();
740 if ( NumValues == 0 ) return;
741
742 for (unsigned j = 0, f = NumValues; j != f; ++j) {
743 EVT VT = ValueVTs[j];
744 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000745
746 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000747 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000748 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000749 ExtendKind = ISD::ZERO_EXTEND;
750
751 // FIXME: C calling convention requires the return type to be promoted to
752 // at least 32-bit. But this is not necessary for non-C calling
753 // conventions. The frontend should mark functions whose return values
754 // require promoting with signext or zeroext attributes.
755 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000756 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000757 if (VT.bitsLT(MinVT))
758 VT = MinVT;
759 }
760
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000761 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
762 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000763 // 'inreg' on function refers to return value
764 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000765 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000766 Flags.setInReg();
767
768 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000769 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000770 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000771 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000772 Flags.setZExt();
773
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000774 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000775 OutVTs.push_back(PartVT);
776 OutFlags.push_back(Flags);
777 }
778 }
779}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000780
Dan Gohman2048b852009-11-23 18:04:58 +0000781void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000782 SDValue Chain = getControlRoot();
783 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000784 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
785
786 if (!FLI.CanLowerReturn) {
787 unsigned DemoteReg = FLI.DemoteRegister;
788 const Function *F = I.getParent()->getParent();
789
790 // Emit a store of the return value through the virtual register.
791 // Leave Outs empty so that LowerReturn won't try to load return
792 // registers the usual way.
793 SmallVector<EVT, 1> PtrValueVTs;
794 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
795 PtrValueVTs);
796
797 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
798 SDValue RetOp = getValue(I.getOperand(0));
799
Owen Andersone50ed302009-08-10 22:56:29 +0000800 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000801 SmallVector<uint64_t, 4> Offsets;
802 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000803 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000804
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000805 SmallVector<SDValue, 4> Chains(NumValues);
806 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000807 for (unsigned i = 0; i != NumValues; ++i) {
808 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
809 DAG.getConstant(Offsets[i], PtrVT));
810 Chains[i] =
811 DAG.getStore(Chain, getCurDebugLoc(),
812 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
813 Add, NULL, Offsets[i], false, 0);
814
815 if (DisableScheduling) {
816 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
817 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
818 }
819 }
820
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000821 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
822 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000823
824 if (DisableScheduling)
825 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
826 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000827 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
828 SmallVector<EVT, 4> ValueVTs;
829 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
830 unsigned NumValues = ValueVTs.size();
831 if (NumValues == 0) continue;
832
833 SDValue RetOp = getValue(I.getOperand(i));
834 for (unsigned j = 0, f = NumValues; j != f; ++j) {
835 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000836
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000837 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000838
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000839 const Function *F = I.getParent()->getParent();
840 if (F->paramHasAttr(0, Attribute::SExt))
841 ExtendKind = ISD::SIGN_EXTEND;
842 else if (F->paramHasAttr(0, Attribute::ZExt))
843 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000844
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000845 // FIXME: C calling convention requires the return type to be promoted to
846 // at least 32-bit. But this is not necessary for non-C calling
847 // conventions. The frontend should mark functions whose return values
848 // require promoting with signext or zeroext attributes.
849 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
850 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
851 if (VT.bitsLT(MinVT))
852 VT = MinVT;
853 }
854
855 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
856 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
857 SmallVector<SDValue, 4> Parts(NumParts);
858 getCopyToParts(DAG, getCurDebugLoc(),
859 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
860 &Parts[0], NumParts, PartVT, ExtendKind);
861
862 // 'inreg' on function refers to return value
863 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
864 if (F->paramHasAttr(0, Attribute::InReg))
865 Flags.setInReg();
866
867 // Propagate extension type if any
868 if (F->paramHasAttr(0, Attribute::SExt))
869 Flags.setSExt();
870 else if (F->paramHasAttr(0, Attribute::ZExt))
871 Flags.setZExt();
872
873 for (unsigned i = 0; i < NumParts; ++i)
874 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000875 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000876 }
877 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000878
879 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000880 CallingConv::ID CallConv =
881 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000882 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
883 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000884
885 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000886 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000887 "LowerReturn didn't return a valid chain!");
888
889 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000890 DAG.setRoot(Chain);
Bill Wendling87710f02009-12-21 23:47:40 +0000891
892 if (DisableScheduling)
893 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000894}
895
Dan Gohmanad62f532009-04-23 23:13:24 +0000896/// CopyToExportRegsIfNeeded - If the given value has virtual registers
897/// created for it, emit nodes to copy the value into the virtual
898/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000899void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000900 if (!V->use_empty()) {
901 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
902 if (VMI != FuncInfo.ValueMap.end())
903 CopyValueToVirtualRegister(V, VMI->second);
904 }
905}
906
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000907/// ExportFromCurrentBlock - If this condition isn't known to be exported from
908/// the current basic block, add it to ValueMap now so that we'll get a
909/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000910void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000911 // No need to export constants.
912 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000914 // Already exported?
915 if (FuncInfo.isExportedInst(V)) return;
916
917 unsigned Reg = FuncInfo.InitializeRegForValue(V);
918 CopyValueToVirtualRegister(V, Reg);
919}
920
Dan Gohman2048b852009-11-23 18:04:58 +0000921bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
922 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000923 // The operands of the setcc have to be in this block. We don't know
924 // how to export them from some other block.
925 if (Instruction *VI = dyn_cast<Instruction>(V)) {
926 // Can export from current BB.
927 if (VI->getParent() == FromBB)
928 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000929
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000930 // Is already exported, noop.
931 return FuncInfo.isExportedInst(V);
932 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000934 // If this is an argument, we can export it if the BB is the entry block or
935 // if it is already exported.
936 if (isa<Argument>(V)) {
937 if (FromBB == &FromBB->getParent()->getEntryBlock())
938 return true;
939
940 // Otherwise, can only export this if it is already exported.
941 return FuncInfo.isExportedInst(V);
942 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000943
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000944 // Otherwise, constants can always be exported.
945 return true;
946}
947
948static bool InBlock(const Value *V, const BasicBlock *BB) {
949 if (const Instruction *I = dyn_cast<Instruction>(V))
950 return I->getParent() == BB;
951 return true;
952}
953
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000954/// getFCmpCondCode - Return the ISD condition code corresponding to
955/// the given LLVM IR floating-point condition code. This includes
956/// consideration of global floating-point math flags.
957///
958static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
959 ISD::CondCode FPC, FOC;
960 switch (Pred) {
961 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
962 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
963 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
964 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
965 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
966 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
967 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
968 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
969 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
970 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
971 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
972 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
973 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
974 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
975 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
976 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
977 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000978 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000979 FOC = FPC = ISD::SETFALSE;
980 break;
981 }
982 if (FiniteOnlyFPMath())
983 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000984 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000985 return FPC;
986}
987
988/// getICmpCondCode - Return the ISD condition code corresponding to
989/// the given LLVM IR integer condition code.
990///
991static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
992 switch (Pred) {
993 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
994 case ICmpInst::ICMP_NE: return ISD::SETNE;
995 case ICmpInst::ICMP_SLE: return ISD::SETLE;
996 case ICmpInst::ICMP_ULE: return ISD::SETULE;
997 case ICmpInst::ICMP_SGE: return ISD::SETGE;
998 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
999 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1000 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1001 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1002 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1003 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001004 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001005 return ISD::SETNE;
1006 }
1007}
1008
Dan Gohmanc2277342008-10-17 21:16:08 +00001009/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1010/// This function emits a branch and is used at the leaves of an OR or an
1011/// AND operator tree.
1012///
1013void
Dan Gohman2048b852009-11-23 18:04:58 +00001014SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1015 MachineBasicBlock *TBB,
1016 MachineBasicBlock *FBB,
1017 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001018 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001019
Dan Gohmanc2277342008-10-17 21:16:08 +00001020 // If the leaf of the tree is a comparison, merge the condition into
1021 // the caseblock.
1022 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1023 // The operands of the cmp have to be in this block. We don't know
1024 // how to export them from some other block. If this is the first block
1025 // of the sequence, no exporting is needed.
1026 if (CurBB == CurMBB ||
1027 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1028 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001029 ISD::CondCode Condition;
1030 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001031 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001032 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001033 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001034 } else {
1035 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001036 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001037 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001038
1039 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001040 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1041 SwitchCases.push_back(CB);
1042 return;
1043 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001044 }
1045
1046 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001047 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001048 NULL, TBB, FBB, CurBB);
1049 SwitchCases.push_back(CB);
1050}
1051
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001052/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001053void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1054 MachineBasicBlock *TBB,
1055 MachineBasicBlock *FBB,
1056 MachineBasicBlock *CurBB,
1057 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001058 // If this node is not part of the or/and tree, emit it as a branch.
1059 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001060 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001061 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1062 BOp->getParent() != CurBB->getBasicBlock() ||
1063 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1064 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1065 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001066 return;
1067 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001069 // Create TmpBB after CurBB.
1070 MachineFunction::iterator BBI = CurBB;
1071 MachineFunction &MF = DAG.getMachineFunction();
1072 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1073 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001074
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001075 if (Opc == Instruction::Or) {
1076 // Codegen X | Y as:
1077 // jmp_if_X TBB
1078 // jmp TmpBB
1079 // TmpBB:
1080 // jmp_if_Y TBB
1081 // jmp FBB
1082 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001083
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001084 // Emit the LHS condition.
1085 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001086
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001087 // Emit the RHS condition into TmpBB.
1088 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1089 } else {
1090 assert(Opc == Instruction::And && "Unknown merge op!");
1091 // Codegen X & Y as:
1092 // jmp_if_X TmpBB
1093 // jmp FBB
1094 // TmpBB:
1095 // jmp_if_Y TBB
1096 // jmp FBB
1097 //
1098 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001099
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001100 // Emit the LHS condition.
1101 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001103 // Emit the RHS condition into TmpBB.
1104 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1105 }
1106}
1107
1108/// If the set of cases should be emitted as a series of branches, return true.
1109/// If we should emit this as a bunch of and/or'd together conditions, return
1110/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001111bool
Dan Gohman2048b852009-11-23 18:04:58 +00001112SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001113 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001114
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001115 // If this is two comparisons of the same values or'd or and'd together, they
1116 // will get folded into a single comparison, so don't emit two blocks.
1117 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1118 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1119 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1120 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1121 return false;
1122 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001124 return true;
1125}
1126
Dan Gohman2048b852009-11-23 18:04:58 +00001127void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001128 // Update machine-CFG edges.
1129 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1130
1131 // Figure out which block is immediately after the current one.
1132 MachineBasicBlock *NextBlock = 0;
1133 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001134 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001135 NextBlock = BBI;
1136
1137 if (I.isUnconditional()) {
1138 // Update machine-CFG edges.
1139 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001141 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001142 if (Succ0MBB != NextBlock) {
1143 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001144 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001145 DAG.getBasicBlock(Succ0MBB));
1146 DAG.setRoot(V);
1147
1148 if (DisableScheduling)
1149 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1150 }
1151
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001152 return;
1153 }
1154
1155 // If this condition is one of the special cases we handle, do special stuff
1156 // now.
1157 Value *CondVal = I.getCondition();
1158 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1159
1160 // If this is a series of conditions that are or'd or and'd together, emit
1161 // this as a sequence of branches instead of setcc's with and/or operations.
1162 // For example, instead of something like:
1163 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001164 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001165 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001166 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001167 // or C, F
1168 // jnz foo
1169 // Emit:
1170 // cmp A, B
1171 // je foo
1172 // cmp D, E
1173 // jle foo
1174 //
1175 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001176 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001177 (BOp->getOpcode() == Instruction::And ||
1178 BOp->getOpcode() == Instruction::Or)) {
1179 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1180 // If the compares in later blocks need to use values not currently
1181 // exported from this block, export them now. This block should always
1182 // be the first entry.
1183 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001185 // Allow some cases to be rejected.
1186 if (ShouldEmitAsBranches(SwitchCases)) {
1187 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1188 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1189 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1190 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001191
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001192 // Emit the branch for this block.
1193 visitSwitchCase(SwitchCases[0]);
1194 SwitchCases.erase(SwitchCases.begin());
1195 return;
1196 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001197
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001198 // Okay, we decided not to do this, remove any inserted MBB's and clear
1199 // SwitchCases.
1200 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001201 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001202
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001203 SwitchCases.clear();
1204 }
1205 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001206
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001208 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001209 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001211 // Use visitSwitchCase to actually insert the fast branch sequence for this
1212 // cond branch.
1213 visitSwitchCase(CB);
1214}
1215
1216/// visitSwitchCase - Emits the necessary code to represent a single node in
1217/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001218void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001219 SDValue Cond;
1220 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001221 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001222
1223 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001224 if (CB.CmpMHS == NULL) {
1225 // Fold "(X == true)" to X and "(X == false)" to !X to
1226 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001227 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001228 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001229 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001230 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001231 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001232 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001233 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001234 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001235 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236 } else {
1237 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1238
Anton Korobeynikov23218582008-12-23 22:25:27 +00001239 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1240 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001241
1242 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001243 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001244
1245 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001246 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001247 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001248 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001249 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001250 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001251 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001252 DAG.getConstant(High-Low, VT), ISD::SETULE);
1253 }
1254 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001255
Bill Wendling87710f02009-12-21 23:47:40 +00001256 if (DisableScheduling)
1257 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001259 // Update successor info
1260 CurMBB->addSuccessor(CB.TrueBB);
1261 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 // Set NextBlock to be the MBB immediately after the current one, if any.
1264 // This is used to avoid emitting unnecessary branches to the next block.
1265 MachineBasicBlock *NextBlock = 0;
1266 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001267 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001268 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001270 // If the lhs block is the next block, invert the condition so that we can
1271 // fall through to the lhs instead of the rhs block.
1272 if (CB.TrueBB == NextBlock) {
1273 std::swap(CB.TrueBB, CB.FalseBB);
1274 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001275 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001276
1277 if (DisableScheduling)
1278 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001279 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001280
Dale Johannesenf5d97892009-02-04 01:48:28 +00001281 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001282 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001283 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001284
Bill Wendling87710f02009-12-21 23:47:40 +00001285 if (DisableScheduling)
1286 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1287
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001288 // If the branch was constant folded, fix up the CFG.
1289 if (BrCond.getOpcode() == ISD::BR) {
1290 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291 } else {
1292 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001293 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001294 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001295
Bill Wendling87710f02009-12-21 23:47:40 +00001296 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001297 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1298 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001299
1300 if (DisableScheduling)
1301 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1302 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001304
1305 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001306}
1307
1308/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001309void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001310 // Emit the code for the jump table
1311 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001312 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001313 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1314 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001316 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1317 MVT::Other, Index.getValue(1),
1318 Table, Index);
1319 DAG.setRoot(BrJumpTable);
1320
Bill Wendling87710f02009-12-21 23:47:40 +00001321 if (DisableScheduling) {
1322 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1323 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001324 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001325 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001326}
1327
1328/// visitJumpTableHeader - This function emits necessary code to produce index
1329/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001330void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1331 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001332 // Subtract the lowest switch case value from the value being switched on and
1333 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 // difference between smallest and largest cases.
1335 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001336 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001337 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001338 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001339
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001340 // The SDNode we just created, which holds the value being switched on minus
1341 // the the smallest case value, needs to be copied to a virtual register so it
1342 // can be used as an index into the jump table in a subsequent basic block.
1343 // This value may be smaller or larger than the target's pointer type, and
1344 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001345 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001346
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001348 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1349 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001350 JT.Reg = JumpTableReg;
1351
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001352 // Emit the range check for the jump table, and branch to the default block
1353 // for the switch statement if the value being switched on exceeds the largest
1354 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001355 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001356 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001357 DAG.getConstant(JTH.Last-JTH.First,VT),
1358 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001359
Bill Wendling87710f02009-12-21 23:47:40 +00001360 if (DisableScheduling) {
1361 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1362 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1363 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1364 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1365 }
1366
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001367 // Set NextBlock to be the MBB immediately after the current one, if any.
1368 // This is used to avoid emitting unnecessary branches to the next block.
1369 MachineBasicBlock *NextBlock = 0;
1370 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001371
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001372 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001373 NextBlock = BBI;
1374
Dale Johannesen66978ee2009-01-31 02:22:37 +00001375 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001376 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001377 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001378
Bill Wendling87710f02009-12-21 23:47:40 +00001379 if (DisableScheduling)
1380 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1381
1382 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001383 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1384 DAG.getBasicBlock(JT.MBB));
1385
Bill Wendling87710f02009-12-21 23:47:40 +00001386 if (DisableScheduling)
1387 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1388 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001389
Bill Wendling87710f02009-12-21 23:47:40 +00001390 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001391}
1392
1393/// visitBitTestHeader - This function emits necessary code to produce value
1394/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001395void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 // Subtract the minimum value
1397 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001398 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001399 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001400 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001401
1402 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001403 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001404 TLI.getSetCCResultType(Sub.getValueType()),
1405 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001406 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001407
Bill Wendling87710f02009-12-21 23:47:40 +00001408 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1409 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001410
Duncan Sands92abc622009-01-31 15:50:11 +00001411 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001412 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1413 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001414
Bill Wendling87710f02009-12-21 23:47:40 +00001415 if (DisableScheduling) {
1416 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1417 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1418 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1419 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1420 }
1421
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001422 // Set NextBlock to be the MBB immediately after the current one, if any.
1423 // This is used to avoid emitting unnecessary branches to the next block.
1424 MachineBasicBlock *NextBlock = 0;
1425 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001426 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001427 NextBlock = BBI;
1428
1429 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1430
1431 CurMBB->addSuccessor(B.Default);
1432 CurMBB->addSuccessor(MBB);
1433
Dale Johannesen66978ee2009-01-31 02:22:37 +00001434 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001435 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001436 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001437
Bill Wendling87710f02009-12-21 23:47:40 +00001438 if (DisableScheduling)
1439 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1440
1441 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001442 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1443 DAG.getBasicBlock(MBB));
1444
Bill Wendling87710f02009-12-21 23:47:40 +00001445 if (DisableScheduling)
1446 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1447 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001448
Bill Wendling87710f02009-12-21 23:47:40 +00001449 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001450}
1451
1452/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001453void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1454 unsigned Reg,
1455 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001456 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001457 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001458 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001459 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001460 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001461 DAG.getConstant(1, TLI.getPointerTy()),
1462 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001463
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001464 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001465 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001466 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001467 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001468 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1469 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001470 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001471 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001472
Bill Wendling87710f02009-12-21 23:47:40 +00001473 if (DisableScheduling) {
1474 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1475 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1476 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1477 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1478 }
1479
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001480 CurMBB->addSuccessor(B.TargetBB);
1481 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001482
Dale Johannesen66978ee2009-01-31 02:22:37 +00001483 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001484 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001485 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486
Bill Wendling87710f02009-12-21 23:47:40 +00001487 if (DisableScheduling)
1488 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1489
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001490 // Set NextBlock to be the MBB immediately after the current one, if any.
1491 // This is used to avoid emitting unnecessary branches to the next block.
1492 MachineBasicBlock *NextBlock = 0;
1493 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001494 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001495 NextBlock = BBI;
1496
Bill Wendling87710f02009-12-21 23:47:40 +00001497 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001498 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1499 DAG.getBasicBlock(NextMBB));
1500
Bill Wendling87710f02009-12-21 23:47:40 +00001501 if (DisableScheduling)
1502 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1503 }
Bill Wendling0777e922009-12-21 21:59:52 +00001504
Bill Wendling87710f02009-12-21 23:47:40 +00001505 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001506}
1507
Dan Gohman2048b852009-11-23 18:04:58 +00001508void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001509 // Retrieve successors.
1510 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1511 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1512
Gabor Greifb67e6b32009-01-15 11:10:44 +00001513 const Value *Callee(I.getCalledValue());
1514 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001515 visitInlineAsm(&I);
1516 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001517 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001518
1519 // If the value of the invoke is used outside of its defining block, make it
1520 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001521 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001522
1523 // Update successor info
1524 CurMBB->addSuccessor(Return);
1525 CurMBB->addSuccessor(LandingPad);
1526
1527 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001528 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1529 MVT::Other, getControlRoot(),
1530 DAG.getBasicBlock(Return));
1531 DAG.setRoot(Branch);
1532
1533 if (DisableScheduling)
1534 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001535}
1536
Dan Gohman2048b852009-11-23 18:04:58 +00001537void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001538}
1539
1540/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1541/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001542bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1543 CaseRecVector& WorkList,
1544 Value* SV,
1545 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001547
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001548 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001549 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001550 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001551 return false;
1552
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001553 // Get the MachineFunction which holds the current MBB. This is used when
1554 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001555 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001556
1557 // Figure out which block is immediately after the current one.
1558 MachineBasicBlock *NextBlock = 0;
1559 MachineFunction::iterator BBI = CR.CaseBB;
1560
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001561 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562 NextBlock = BBI;
1563
1564 // TODO: If any two of the cases has the same destination, and if one value
1565 // is the same as the other, but has one bit unset that the other has set,
1566 // use bit manipulation to do two compares at once. For example:
1567 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001569 // Rearrange the case blocks so that the last one falls through if possible.
1570 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1571 // The last case block won't fall through into 'NextBlock' if we emit the
1572 // branches in this order. See if rearranging a case value would help.
1573 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1574 if (I->BB == NextBlock) {
1575 std::swap(*I, BackCase);
1576 break;
1577 }
1578 }
1579 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001580
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001581 // Create a CaseBlock record representing a conditional branch to
1582 // the Case's target mbb if the value being switched on SV is equal
1583 // to C.
1584 MachineBasicBlock *CurBlock = CR.CaseBB;
1585 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1586 MachineBasicBlock *FallThrough;
1587 if (I != E-1) {
1588 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1589 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001590
1591 // Put SV in a virtual register to make it available from the new blocks.
1592 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593 } else {
1594 // If the last case doesn't match, go to the default block.
1595 FallThrough = Default;
1596 }
1597
1598 Value *RHS, *LHS, *MHS;
1599 ISD::CondCode CC;
1600 if (I->High == I->Low) {
1601 // This is just small small case range :) containing exactly 1 case
1602 CC = ISD::SETEQ;
1603 LHS = SV; RHS = I->High; MHS = NULL;
1604 } else {
1605 CC = ISD::SETLE;
1606 LHS = I->Low; MHS = SV; RHS = I->High;
1607 }
1608 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001609
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610 // If emitting the first comparison, just call visitSwitchCase to emit the
1611 // code into the current block. Otherwise, push the CaseBlock onto the
1612 // vector to be later processed by SDISel, and insert the node's MBB
1613 // before the next MBB.
1614 if (CurBlock == CurMBB)
1615 visitSwitchCase(CB);
1616 else
1617 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 CurBlock = FallThrough;
1620 }
1621
1622 return true;
1623}
1624
1625static inline bool areJTsAllowed(const TargetLowering &TLI) {
1626 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001627 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1628 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001629}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001630
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001631static APInt ComputeRange(const APInt &First, const APInt &Last) {
1632 APInt LastExt(Last), FirstExt(First);
1633 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1634 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1635 return (LastExt - FirstExt + 1ULL);
1636}
1637
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001639bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1640 CaseRecVector& WorkList,
1641 Value* SV,
1642 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001643 Case& FrontCase = *CR.Range.first;
1644 Case& BackCase = *(CR.Range.second-1);
1645
Chris Lattnere880efe2009-11-07 07:50:34 +00001646 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1647 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001648
Chris Lattnere880efe2009-11-07 07:50:34 +00001649 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001650 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1651 I!=E; ++I)
1652 TSize += I->size();
1653
Chris Lattnere880efe2009-11-07 07:50:34 +00001654 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001655 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001656
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001657 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001658 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001659 if (Density < 0.4)
1660 return false;
1661
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001662 DEBUG(errs() << "Lowering jump table\n"
1663 << "First entry: " << First << ". Last entry: " << Last << '\n'
1664 << "Range: " << Range
1665 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001666
1667 // Get the MachineFunction which holds the current MBB. This is used when
1668 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001669 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001670
1671 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001672 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001673 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001674
1675 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1676
1677 // Create a new basic block to hold the code for loading the address
1678 // of the jump table, and jumping to it. Update successor information;
1679 // we will either branch to the default case for the switch, or the jump
1680 // table.
1681 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1682 CurMF->insert(BBI, JumpTableBB);
1683 CR.CaseBB->addSuccessor(Default);
1684 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001686 // Build a vector of destination BBs, corresponding to each target
1687 // of the jump table. If the value of the jump table slot corresponds to
1688 // a case statement, push the case's BB onto the vector, otherwise, push
1689 // the default BB.
1690 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001691 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001692 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001693 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1694 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1695
1696 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001697 DestBBs.push_back(I->BB);
1698 if (TEI==High)
1699 ++I;
1700 } else {
1701 DestBBs.push_back(Default);
1702 }
1703 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001705 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001706 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1707 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001708 E = DestBBs.end(); I != E; ++I) {
1709 if (!SuccsHandled[(*I)->getNumber()]) {
1710 SuccsHandled[(*I)->getNumber()] = true;
1711 JumpTableBB->addSuccessor(*I);
1712 }
1713 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001714
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001715 // Create a jump table index for this jump table, or return an existing
1716 // one.
1717 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001718
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719 // Set the jump table information so that we can codegen it as a second
1720 // MachineBasicBlock
1721 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1722 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1723 if (CR.CaseBB == CurMBB)
1724 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 JTCases.push_back(JumpTableBlock(JTH, JT));
1727
1728 return true;
1729}
1730
1731/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1732/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001733bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1734 CaseRecVector& WorkList,
1735 Value* SV,
1736 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737 // Get the MachineFunction which holds the current MBB. This is used when
1738 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001739 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001740
1741 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001742 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001743 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744
1745 Case& FrontCase = *CR.Range.first;
1746 Case& BackCase = *(CR.Range.second-1);
1747 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1748
1749 // Size is the number of Cases represented by this range.
1750 unsigned Size = CR.Range.second - CR.Range.first;
1751
Chris Lattnere880efe2009-11-07 07:50:34 +00001752 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1753 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001754 double FMetric = 0;
1755 CaseItr Pivot = CR.Range.first + Size/2;
1756
1757 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1758 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001759 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001760 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1761 I!=E; ++I)
1762 TSize += I->size();
1763
Chris Lattnere880efe2009-11-07 07:50:34 +00001764 APInt LSize = FrontCase.size();
1765 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001766 DEBUG(errs() << "Selecting best pivot: \n"
1767 << "First: " << First << ", Last: " << Last <<'\n'
1768 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001769 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1770 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001771 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1772 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001773 APInt Range = ComputeRange(LEnd, RBegin);
1774 assert((Range - 2ULL).isNonNegative() &&
1775 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001776 double LDensity = (double)LSize.roundToDouble() /
1777 (LEnd - First + 1ULL).roundToDouble();
1778 double RDensity = (double)RSize.roundToDouble() /
1779 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001780 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001782 DEBUG(errs() <<"=>Step\n"
1783 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1784 << "LDensity: " << LDensity
1785 << ", RDensity: " << RDensity << '\n'
1786 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787 if (FMetric < Metric) {
1788 Pivot = J;
1789 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001790 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791 }
1792
1793 LSize += J->size();
1794 RSize -= J->size();
1795 }
1796 if (areJTsAllowed(TLI)) {
1797 // If our case is dense we *really* should handle it earlier!
1798 assert((FMetric > 0) && "Should handle dense range earlier!");
1799 } else {
1800 Pivot = CR.Range.first + Size/2;
1801 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001802
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001803 CaseRange LHSR(CR.Range.first, Pivot);
1804 CaseRange RHSR(Pivot, CR.Range.second);
1805 Constant *C = Pivot->Low;
1806 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001809 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001810 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001811 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001812 // Pivot's Value, then we can branch directly to the LHS's Target,
1813 // rather than creating a leaf node for it.
1814 if ((LHSR.second - LHSR.first) == 1 &&
1815 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001816 cast<ConstantInt>(C)->getValue() ==
1817 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001818 TrueBB = LHSR.first->BB;
1819 } else {
1820 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1821 CurMF->insert(BBI, TrueBB);
1822 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001823
1824 // Put SV in a virtual register to make it available from the new blocks.
1825 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001828 // Similar to the optimization above, if the Value being switched on is
1829 // known to be less than the Constant CR.LT, and the current Case Value
1830 // is CR.LT - 1, then we can branch directly to the target block for
1831 // the current Case Value, rather than emitting a RHS leaf node for it.
1832 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001833 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1834 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001835 FalseBB = RHSR.first->BB;
1836 } else {
1837 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1838 CurMF->insert(BBI, FalseBB);
1839 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001840
1841 // Put SV in a virtual register to make it available from the new blocks.
1842 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001843 }
1844
1845 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001846 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001847 // Otherwise, branch to LHS.
1848 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1849
1850 if (CR.CaseBB == CurMBB)
1851 visitSwitchCase(CB);
1852 else
1853 SwitchCases.push_back(CB);
1854
1855 return true;
1856}
1857
1858/// handleBitTestsSwitchCase - if current case range has few destination and
1859/// range span less, than machine word bitwidth, encode case range into series
1860/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001861bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1862 CaseRecVector& WorkList,
1863 Value* SV,
1864 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001865 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001866 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001867
1868 Case& FrontCase = *CR.Range.first;
1869 Case& BackCase = *(CR.Range.second-1);
1870
1871 // Get the MachineFunction which holds the current MBB. This is used when
1872 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001873 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001874
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001875 // If target does not have legal shift left, do not emit bit tests at all.
1876 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1877 return false;
1878
Anton Korobeynikov23218582008-12-23 22:25:27 +00001879 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001880 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1881 I!=E; ++I) {
1882 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001883 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001884 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001885
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 // Count unique destinations
1887 SmallSet<MachineBasicBlock*, 4> Dests;
1888 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1889 Dests.insert(I->BB);
1890 if (Dests.size() > 3)
1891 // Don't bother the code below, if there are too much unique destinations
1892 return false;
1893 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001894 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1895 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001896
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001897 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001898 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1899 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001900 APInt cmpRange = maxValue - minValue;
1901
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001902 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1903 << "Low bound: " << minValue << '\n'
1904 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001905
1906 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001907 (!(Dests.size() == 1 && numCmps >= 3) &&
1908 !(Dests.size() == 2 && numCmps >= 5) &&
1909 !(Dests.size() >= 3 && numCmps >= 6)))
1910 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001911
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001912 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001913 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001915 // Optimize the case where all the case values fit in a
1916 // word without having to subtract minValue. In this case,
1917 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001918 if (minValue.isNonNegative() &&
1919 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1920 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001922 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001923 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001924
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001925 CaseBitsVector CasesBits;
1926 unsigned i, count = 0;
1927
1928 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1929 MachineBasicBlock* Dest = I->BB;
1930 for (i = 0; i < count; ++i)
1931 if (Dest == CasesBits[i].BB)
1932 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001934 if (i == count) {
1935 assert((count < 3) && "Too much destinations to test!");
1936 CasesBits.push_back(CaseBits(0, Dest, 0));
1937 count++;
1938 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001939
1940 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1941 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1942
1943 uint64_t lo = (lowValue - lowBound).getZExtValue();
1944 uint64_t hi = (highValue - lowBound).getZExtValue();
1945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 for (uint64_t j = lo; j <= hi; j++) {
1947 CasesBits[i].Mask |= 1ULL << j;
1948 CasesBits[i].Bits++;
1949 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001950
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001951 }
1952 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001953
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001954 BitTestInfo BTC;
1955
1956 // Figure out which block is immediately after the current one.
1957 MachineFunction::iterator BBI = CR.CaseBB;
1958 ++BBI;
1959
1960 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1961
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001962 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001963 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001964 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
1965 << ", Bits: " << CasesBits[i].Bits
1966 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001967
1968 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1969 CurMF->insert(BBI, CaseBB);
1970 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1971 CaseBB,
1972 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001973
1974 // Put SV in a virtual register to make it available from the new blocks.
1975 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001976 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001977
1978 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001979 -1U, (CR.CaseBB == CurMBB),
1980 CR.CaseBB, Default, BTC);
1981
1982 if (CR.CaseBB == CurMBB)
1983 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001985 BitTestCases.push_back(BTB);
1986
1987 return true;
1988}
1989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001991size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1992 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001993 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001994
1995 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001996 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001997 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1998 Cases.push_back(Case(SI.getSuccessorValue(i),
1999 SI.getSuccessorValue(i),
2000 SMBB));
2001 }
2002 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2003
2004 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002005 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002006 // Must recompute end() each iteration because it may be
2007 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002008 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2009 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2010 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002011 MachineBasicBlock* nextBB = J->BB;
2012 MachineBasicBlock* currentBB = I->BB;
2013
2014 // If the two neighboring cases go to the same destination, merge them
2015 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002016 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 I->High = J->High;
2018 J = Cases.erase(J);
2019 } else {
2020 I = J++;
2021 }
2022 }
2023
2024 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2025 if (I->Low != I->High)
2026 // A range counts double, since it requires two compares.
2027 ++numCmps;
2028 }
2029
2030 return numCmps;
2031}
2032
Dan Gohman2048b852009-11-23 18:04:58 +00002033void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 // Figure out which block is immediately after the current one.
2035 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002036 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2037
2038 // If there is only the default destination, branch to it if it is not the
2039 // next basic block. Otherwise, just fall through.
2040 if (SI.getNumOperands() == 2) {
2041 // Update machine-CFG edges.
2042
2043 // If this is not a fall-through branch, emit the branch.
2044 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002045 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002046 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002047 MVT::Other, getControlRoot(),
2048 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002049 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002050
2051 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002052 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002053 }
2054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002055 return;
2056 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002057
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002058 // If there are any non-default case statements, create a vector of Cases
2059 // representing each one, and sort the vector so that we can efficiently
2060 // create a binary search tree from them.
2061 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002062 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002063 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2064 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002065 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002066
2067 // Get the Value to be switched on and default basic blocks, which will be
2068 // inserted into CaseBlock records, representing basic blocks in the binary
2069 // search tree.
2070 Value *SV = SI.getOperand(0);
2071
2072 // Push the initial CaseRec onto the worklist
2073 CaseRecVector WorkList;
2074 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2075
2076 while (!WorkList.empty()) {
2077 // Grab a record representing a case range to process off the worklist
2078 CaseRec CR = WorkList.back();
2079 WorkList.pop_back();
2080
2081 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2082 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002083
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002084 // If the range has few cases (two or less) emit a series of specific
2085 // tests.
2086 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2087 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002088
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002089 // If the switch has more than 5 blocks, and at least 40% dense, and the
2090 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002091 // lowering the switch to a binary tree of conditional branches.
2092 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2093 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002095 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2096 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2097 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2098 }
2099}
2100
Dan Gohman2048b852009-11-23 18:04:58 +00002101void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002102 // Update machine-CFG edges.
2103 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2104 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2105
Bill Wendling49fcff82009-12-21 22:30:11 +00002106 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2107 MVT::Other, getControlRoot(),
2108 getValue(I.getAddress()));
2109 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002110
Bill Wendling49fcff82009-12-21 22:30:11 +00002111 if (DisableScheduling)
2112 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2113}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002114
Dan Gohman2048b852009-11-23 18:04:58 +00002115void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002116 // -0.0 - X --> fneg
2117 const Type *Ty = I.getType();
2118 if (isa<VectorType>(Ty)) {
2119 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2120 const VectorType *DestTy = cast<VectorType>(I.getType());
2121 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002122 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002123 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002124 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002125 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002126 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002127 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2128 Op2.getValueType(), Op2);
2129 setValue(&I, Res);
2130
2131 if (DisableScheduling)
2132 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002134 return;
2135 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002136 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002137 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002138
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002139 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002140 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002141 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002142 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2143 Op2.getValueType(), Op2);
2144 setValue(&I, Res);
2145
2146 if (DisableScheduling)
2147 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2148
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002149 return;
2150 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002152 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002153}
2154
Dan Gohman2048b852009-11-23 18:04:58 +00002155void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002156 SDValue Op1 = getValue(I.getOperand(0));
2157 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002158 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2159 Op1.getValueType(), Op1, Op2);
2160 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002161
Bill Wendling49fcff82009-12-21 22:30:11 +00002162 if (DisableScheduling)
2163 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002164}
2165
Dan Gohman2048b852009-11-23 18:04:58 +00002166void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002167 SDValue Op1 = getValue(I.getOperand(0));
2168 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002169 if (!isa<VectorType>(I.getType()) &&
2170 Op2.getValueType() != TLI.getShiftAmountTy()) {
2171 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002172 EVT PTy = TLI.getPointerTy();
2173 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002174 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002175 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2176 TLI.getShiftAmountTy(), Op2);
2177 // If the operand is larger than the shift count type but the shift
2178 // count type has enough bits to represent any shift value, truncate
2179 // it now. This is a common case and it exposes the truncate to
2180 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002181 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002182 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2183 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2184 TLI.getShiftAmountTy(), Op2);
2185 // Otherwise we'll need to temporarily settle for some other
2186 // convenient type; type legalization will make adjustments as
2187 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002188 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002189 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002190 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002191 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002192 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002193 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002195
Bill Wendling49fcff82009-12-21 22:30:11 +00002196 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2197 Op1.getValueType(), Op1, Op2);
2198 setValue(&I, Res);
2199
Bill Wendling87710f02009-12-21 23:47:40 +00002200 if (DisableScheduling) {
2201 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2202 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002203 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002204 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205}
2206
Dan Gohman2048b852009-11-23 18:04:58 +00002207void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2209 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2210 predicate = IC->getPredicate();
2211 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2212 predicate = ICmpInst::Predicate(IC->getPredicate());
2213 SDValue Op1 = getValue(I.getOperand(0));
2214 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002215 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002216
Owen Andersone50ed302009-08-10 22:56:29 +00002217 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002218 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2219 setValue(&I, Res);
2220
2221 if (DisableScheduling)
2222 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223}
2224
Dan Gohman2048b852009-11-23 18:04:58 +00002225void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2227 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2228 predicate = FC->getPredicate();
2229 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2230 predicate = FCmpInst::Predicate(FC->getPredicate());
2231 SDValue Op1 = getValue(I.getOperand(0));
2232 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002233 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002234 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002235 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2236 setValue(&I, Res);
2237
2238 if (DisableScheduling)
2239 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240}
2241
Dan Gohman2048b852009-11-23 18:04:58 +00002242void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002243 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002244 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2245 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002246 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002247
Bill Wendling49fcff82009-12-21 22:30:11 +00002248 SmallVector<SDValue, 4> Values(NumValues);
2249 SDValue Cond = getValue(I.getOperand(0));
2250 SDValue TrueVal = getValue(I.getOperand(1));
2251 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002252
Bill Wendling49fcff82009-12-21 22:30:11 +00002253 for (unsigned i = 0; i != NumValues; ++i) {
2254 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2255 TrueVal.getNode()->getValueType(i), Cond,
2256 SDValue(TrueVal.getNode(),
2257 TrueVal.getResNo() + i),
2258 SDValue(FalseVal.getNode(),
2259 FalseVal.getResNo() + i));
2260
2261 if (DisableScheduling)
2262 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002263 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002264
Bill Wendling49fcff82009-12-21 22:30:11 +00002265 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2266 DAG.getVTList(&ValueVTs[0], NumValues),
2267 &Values[0], NumValues);
2268 setValue(&I, Res);
2269
2270 if (DisableScheduling)
2271 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2272}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002273
Dan Gohman2048b852009-11-23 18:04:58 +00002274void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002275 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2276 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002277 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002278 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2279 setValue(&I, Res);
2280
2281 if (DisableScheduling)
2282 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002283}
2284
Dan Gohman2048b852009-11-23 18:04:58 +00002285void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002286 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2287 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2288 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002289 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002290 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2291 setValue(&I, Res);
2292
2293 if (DisableScheduling)
2294 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295}
2296
Dan Gohman2048b852009-11-23 18:04:58 +00002297void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002298 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2299 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2300 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002301 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002302 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2303 setValue(&I, Res);
2304
2305 if (DisableScheduling)
2306 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002307}
2308
Dan Gohman2048b852009-11-23 18:04:58 +00002309void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002310 // FPTrunc is never a no-op cast, no need to check
2311 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002312 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002313 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2314 DestVT, N, DAG.getIntPtrConstant(0));
2315 setValue(&I, Res);
2316
2317 if (DisableScheduling)
2318 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002319}
2320
Dan Gohman2048b852009-11-23 18:04:58 +00002321void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002322 // FPTrunc is never a no-op cast, no need to check
2323 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002324 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002325 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2326 setValue(&I, Res);
2327
2328 if (DisableScheduling)
2329 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002330}
2331
Dan Gohman2048b852009-11-23 18:04:58 +00002332void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002333 // FPToUI is never a no-op cast, no need to check
2334 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002335 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002336 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2337 setValue(&I, Res);
2338
2339 if (DisableScheduling)
2340 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002341}
2342
Dan Gohman2048b852009-11-23 18:04:58 +00002343void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002344 // FPToSI is never a no-op cast, no need to check
2345 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002346 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002347 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2348 setValue(&I, Res);
2349
2350 if (DisableScheduling)
2351 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002352}
2353
Dan Gohman2048b852009-11-23 18:04:58 +00002354void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002355 // UIToFP is never a no-op cast, no need to check
2356 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002357 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002358 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2359 setValue(&I, Res);
2360
2361 if (DisableScheduling)
2362 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002363}
2364
Dan Gohman2048b852009-11-23 18:04:58 +00002365void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002366 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002367 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002368 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002369 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2370 setValue(&I, Res);
2371
2372 if (DisableScheduling)
2373 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002374}
2375
Dan Gohman2048b852009-11-23 18:04:58 +00002376void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002377 // What to do depends on the size of the integer and the size of the pointer.
2378 // We can either truncate, zero extend, or no-op, accordingly.
2379 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002380 EVT SrcVT = N.getValueType();
2381 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002382 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2383 setValue(&I, Res);
2384
2385 if (DisableScheduling)
2386 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002387}
2388
Dan Gohman2048b852009-11-23 18:04:58 +00002389void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002390 // What to do depends on the size of the integer and the size of the pointer.
2391 // We can either truncate, zero extend, or no-op, accordingly.
2392 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002393 EVT SrcVT = N.getValueType();
2394 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002395 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2396 setValue(&I, Res);
2397
2398 if (DisableScheduling)
2399 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002400}
2401
Dan Gohman2048b852009-11-23 18:04:58 +00002402void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002403 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002404 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002405
Bill Wendling49fcff82009-12-21 22:30:11 +00002406 // BitCast assures us that source and destination are the same size so this is
2407 // either a BIT_CONVERT or a no-op.
2408 if (DestVT != N.getValueType()) {
2409 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2410 DestVT, N); // convert types.
2411 setValue(&I, Res);
2412
2413 if (DisableScheduling)
2414 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2415 } else {
2416 setValue(&I, N); // noop cast.
2417 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002418}
2419
Dan Gohman2048b852009-11-23 18:04:58 +00002420void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002421 SDValue InVec = getValue(I.getOperand(0));
2422 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002423 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002424 TLI.getPointerTy(),
2425 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002426 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2427 TLI.getValueType(I.getType()),
2428 InVec, InVal, InIdx);
2429 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002430
Bill Wendling87710f02009-12-21 23:47:40 +00002431 if (DisableScheduling) {
2432 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002433 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002434 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002435}
2436
Dan Gohman2048b852009-11-23 18:04:58 +00002437void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002438 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002439 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002440 TLI.getPointerTy(),
2441 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002442 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2443 TLI.getValueType(I.getType()), InVec, InIdx);
2444 setValue(&I, Res);
2445
Bill Wendling87710f02009-12-21 23:47:40 +00002446 if (DisableScheduling) {
2447 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002448 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002449 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002450}
2451
Mon P Wangaeb06d22008-11-10 04:46:22 +00002452
2453// Utility for visitShuffleVector - Returns true if the mask is mask starting
2454// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002455static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2456 unsigned MaskNumElts = Mask.size();
2457 for (unsigned i = 0; i != MaskNumElts; ++i)
2458 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002459 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002460 return true;
2461}
2462
Dan Gohman2048b852009-11-23 18:04:58 +00002463void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002465 SDValue Src1 = getValue(I.getOperand(0));
2466 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002467
Nate Begeman9008ca62009-04-27 18:41:29 +00002468 // Convert the ConstantVector mask operand into an array of ints, with -1
2469 // representing undef values.
2470 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002471 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2472 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002473 unsigned MaskNumElts = MaskElts.size();
2474 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002475 if (isa<UndefValue>(MaskElts[i]))
2476 Mask.push_back(-1);
2477 else
2478 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2479 }
2480
Owen Andersone50ed302009-08-10 22:56:29 +00002481 EVT VT = TLI.getValueType(I.getType());
2482 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002483 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002484
Mon P Wangc7849c22008-11-16 05:06:27 +00002485 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002486 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2487 &Mask[0]);
2488 setValue(&I, Res);
2489
2490 if (DisableScheduling)
2491 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2492
Mon P Wangaeb06d22008-11-10 04:46:22 +00002493 return;
2494 }
2495
2496 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002497 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2498 // Mask is longer than the source vectors and is a multiple of the source
2499 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002500 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002501 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2502 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002503 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2504 VT, Src1, Src2);
2505 setValue(&I, Res);
2506
2507 if (DisableScheduling)
2508 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2509
Mon P Wangaeb06d22008-11-10 04:46:22 +00002510 return;
2511 }
2512
Mon P Wangc7849c22008-11-16 05:06:27 +00002513 // Pad both vectors with undefs to make them the same length as the mask.
2514 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002515 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2516 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002517 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002518
Nate Begeman9008ca62009-04-27 18:41:29 +00002519 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2520 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002521 MOps1[0] = Src1;
2522 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002523
2524 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2525 getCurDebugLoc(), VT,
2526 &MOps1[0], NumConcat);
2527 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2528 getCurDebugLoc(), VT,
2529 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002530
Mon P Wangaeb06d22008-11-10 04:46:22 +00002531 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002532 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002533 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002534 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002535 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002536 MappedOps.push_back(Idx);
2537 else
2538 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002539 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002540
2541 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2542 &MappedOps[0]);
2543 setValue(&I, Res);
2544
Bill Wendlinge1a90422009-12-21 23:10:19 +00002545 if (DisableScheduling) {
2546 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2547 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002548 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002549 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002550
Mon P Wangaeb06d22008-11-10 04:46:22 +00002551 return;
2552 }
2553
Mon P Wangc7849c22008-11-16 05:06:27 +00002554 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002555 // Analyze the access pattern of the vector to see if we can extract
2556 // two subvectors and do the shuffle. The analysis is done by calculating
2557 // the range of elements the mask access on both vectors.
2558 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2559 int MaxRange[2] = {-1, -1};
2560
Nate Begeman5a5ca152009-04-29 05:20:52 +00002561 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002562 int Idx = Mask[i];
2563 int Input = 0;
2564 if (Idx < 0)
2565 continue;
2566
Nate Begeman5a5ca152009-04-29 05:20:52 +00002567 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002568 Input = 1;
2569 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002570 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002571 if (Idx > MaxRange[Input])
2572 MaxRange[Input] = Idx;
2573 if (Idx < MinRange[Input])
2574 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002575 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002576
Mon P Wangc7849c22008-11-16 05:06:27 +00002577 // Check if the access is smaller than the vector size and can we find
2578 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002579 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002580 int StartIdx[2]; // StartIdx to extract from
2581 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002582 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002583 RangeUse[Input] = 0; // Unused
2584 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002585 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002586 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002587 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002588 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002589 RangeUse[Input] = 1; // Extract from beginning of the vector
2590 StartIdx[Input] = 0;
2591 } else {
2592 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002593 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002594 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002595 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002596 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002597 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002598 }
2599
Bill Wendling636e2582009-08-21 18:16:06 +00002600 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002601 SDValue Res = DAG.getUNDEF(VT);
2602 setValue(&I, Res); // Vectors are not used.
2603
2604 if (DisableScheduling)
2605 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2606
Mon P Wangc7849c22008-11-16 05:06:27 +00002607 return;
2608 }
2609 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2610 // Extract appropriate subvector and generate a vector shuffle
2611 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002612 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002613 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002614 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002615 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002616 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002617 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002618
2619 if (DisableScheduling)
2620 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002621 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002622
Mon P Wangc7849c22008-11-16 05:06:27 +00002623 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002624 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002625 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002626 int Idx = Mask[i];
2627 if (Idx < 0)
2628 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002629 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 MappedOps.push_back(Idx - StartIdx[0]);
2631 else
2632 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002633 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002634
2635 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2636 &MappedOps[0]);
2637 setValue(&I, Res);
2638
2639 if (DisableScheduling)
2640 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2641
Mon P Wangc7849c22008-11-16 05:06:27 +00002642 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002643 }
2644 }
2645
Mon P Wangc7849c22008-11-16 05:06:27 +00002646 // We can't use either concat vectors or extract subvectors so fall back to
2647 // replacing the shuffle with extract and build vector.
2648 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002649 EVT EltVT = VT.getVectorElementType();
2650 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002651 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002652 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002653 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002654 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002655 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002656 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002657 SDValue Res;
2658
Nate Begeman5a5ca152009-04-29 05:20:52 +00002659 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002660 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2661 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002662 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002663 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2664 EltVT, Src2,
2665 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2666
2667 Ops.push_back(Res);
2668
2669 if (DisableScheduling)
2670 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002671 }
2672 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002673
2674 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2675 VT, &Ops[0], Ops.size());
2676 setValue(&I, Res);
2677
2678 if (DisableScheduling)
2679 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002680}
2681
Dan Gohman2048b852009-11-23 18:04:58 +00002682void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002683 const Value *Op0 = I.getOperand(0);
2684 const Value *Op1 = I.getOperand(1);
2685 const Type *AggTy = I.getType();
2686 const Type *ValTy = Op1->getType();
2687 bool IntoUndef = isa<UndefValue>(Op0);
2688 bool FromUndef = isa<UndefValue>(Op1);
2689
2690 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2691 I.idx_begin(), I.idx_end());
2692
Owen Andersone50ed302009-08-10 22:56:29 +00002693 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002694 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002695 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002696 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2697
2698 unsigned NumAggValues = AggValueVTs.size();
2699 unsigned NumValValues = ValValueVTs.size();
2700 SmallVector<SDValue, 4> Values(NumAggValues);
2701
2702 SDValue Agg = getValue(Op0);
2703 SDValue Val = getValue(Op1);
2704 unsigned i = 0;
2705 // Copy the beginning value(s) from the original aggregate.
2706 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002707 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002708 SDValue(Agg.getNode(), Agg.getResNo() + i);
2709 // Copy values from the inserted value(s).
2710 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002711 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002712 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2713 // Copy remaining value(s) from the original aggregate.
2714 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002715 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002716 SDValue(Agg.getNode(), Agg.getResNo() + i);
2717
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002718 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2719 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2720 &Values[0], NumAggValues);
2721 setValue(&I, Res);
2722
2723 if (DisableScheduling)
2724 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002725}
2726
Dan Gohman2048b852009-11-23 18:04:58 +00002727void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002728 const Value *Op0 = I.getOperand(0);
2729 const Type *AggTy = Op0->getType();
2730 const Type *ValTy = I.getType();
2731 bool OutOfUndef = isa<UndefValue>(Op0);
2732
2733 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2734 I.idx_begin(), I.idx_end());
2735
Owen Andersone50ed302009-08-10 22:56:29 +00002736 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002737 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2738
2739 unsigned NumValValues = ValValueVTs.size();
2740 SmallVector<SDValue, 4> Values(NumValValues);
2741
2742 SDValue Agg = getValue(Op0);
2743 // Copy out the selected value(s).
2744 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2745 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002746 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002747 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002748 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002749
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002750 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2751 DAG.getVTList(&ValValueVTs[0], NumValValues),
2752 &Values[0], NumValValues);
2753 setValue(&I, Res);
2754
2755 if (DisableScheduling)
2756 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002757}
2758
Dan Gohman2048b852009-11-23 18:04:58 +00002759void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002760 SDValue N = getValue(I.getOperand(0));
2761 const Type *Ty = I.getOperand(0)->getType();
2762
2763 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2764 OI != E; ++OI) {
2765 Value *Idx = *OI;
2766 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2767 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2768 if (Field) {
2769 // N = N + Offset
2770 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002771 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002772 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002773
2774 if (DisableScheduling)
2775 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002776 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002777
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002778 Ty = StTy->getElementType(Field);
2779 } else {
2780 Ty = cast<SequentialType>(Ty)->getElementType();
2781
2782 // If this is a constant subscript, handle it quickly.
2783 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2784 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002785 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002786 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002787 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002788 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002789 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002790 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002791 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2792 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002793 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002794 else
Evan Chengb1032a82009-02-09 20:54:38 +00002795 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002796
Dale Johannesen66978ee2009-01-31 02:22:37 +00002797 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002798 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002799
2800 if (DisableScheduling) {
2801 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2802 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2803 }
2804
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002805 continue;
2806 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002809 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2810 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002811 SDValue IdxN = getValue(Idx);
2812
2813 // If the index is smaller or larger than intptr_t, truncate or extend
2814 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002815 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002816
2817 // If this is a multiply by a power of two, turn it into a shl
2818 // immediately. This is a very common case.
2819 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002820 if (ElementSize.isPowerOf2()) {
2821 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002822 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002823 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002824 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002826 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002827 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002828 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002829 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002830
2831 if (DisableScheduling)
2832 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002833 }
2834
Scott Michelfdc40a02009-02-17 22:15:04 +00002835 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002836 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002837
2838 if (DisableScheduling)
2839 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002840 }
2841 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002842
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843 setValue(&I, N);
2844}
2845
Dan Gohman2048b852009-11-23 18:04:58 +00002846void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847 // If this is a fixed sized alloca in the entry block of the function,
2848 // allocate it statically on the stack.
2849 if (FuncInfo.StaticAllocaMap.count(&I))
2850 return; // getValue will auto-populate this.
2851
2852 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002853 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854 unsigned Align =
2855 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2856 I.getAlignment());
2857
2858 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002859
2860 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2861 AllocSize,
2862 DAG.getConstant(TySize, AllocSize.getValueType()));
2863
Bill Wendling856ff412009-12-22 00:12:37 +00002864 if (DisableScheduling)
2865 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Chris Lattner0b18e592009-03-17 19:36:00 +00002866
Owen Andersone50ed302009-08-10 22:56:29 +00002867 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002868 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869
Bill Wendling856ff412009-12-22 00:12:37 +00002870 if (DisableScheduling)
2871 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002873 // Handle alignment. If the requested alignment is less than or equal to
2874 // the stack alignment, ignore it. If the size is greater than or equal to
2875 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2876 unsigned StackAlign =
2877 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2878 if (Align <= StackAlign)
2879 Align = 0;
2880
2881 // Round the size of the allocation up to the stack alignment size
2882 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002883 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002884 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002885 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002886 if (DisableScheduling)
2887 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2888
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002889 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002890 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002891 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002892 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002893 if (DisableScheduling)
2894 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002895
2896 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002897 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002898 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002899 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 setValue(&I, DSA);
2901 DAG.setRoot(DSA.getValue(1));
2902
Bill Wendling856ff412009-12-22 00:12:37 +00002903 if (DisableScheduling)
2904 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
2905
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002906 // Inform the Frame Information that we have just allocated a variable-sized
2907 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002908 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002909}
2910
Dan Gohman2048b852009-11-23 18:04:58 +00002911void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002912 const Value *SV = I.getOperand(0);
2913 SDValue Ptr = getValue(SV);
2914
2915 const Type *Ty = I.getType();
2916 bool isVolatile = I.isVolatile();
2917 unsigned Alignment = I.getAlignment();
2918
Owen Andersone50ed302009-08-10 22:56:29 +00002919 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002920 SmallVector<uint64_t, 4> Offsets;
2921 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2922 unsigned NumValues = ValueVTs.size();
2923 if (NumValues == 0)
2924 return;
2925
2926 SDValue Root;
2927 bool ConstantMemory = false;
2928 if (I.isVolatile())
2929 // Serialize volatile loads with other side effects.
2930 Root = getRoot();
2931 else if (AA->pointsToConstantMemory(SV)) {
2932 // Do not serialize (non-volatile) loads of constant memory with anything.
2933 Root = DAG.getEntryNode();
2934 ConstantMemory = true;
2935 } else {
2936 // Do not serialize non-volatile loads against each other.
2937 Root = DAG.getRoot();
2938 }
2939
2940 SmallVector<SDValue, 4> Values(NumValues);
2941 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002942 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002943 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002944 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2945 PtrVT, Ptr,
2946 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002947 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00002948 A, SV, Offsets[i], isVolatile, Alignment);
2949
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002950 Values[i] = L;
2951 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00002952
2953 if (DisableScheduling) {
2954 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
2955 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
2956 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002957 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002958
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002959 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002960 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002961 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002962 if (isVolatile)
2963 DAG.setRoot(Chain);
2964 else
2965 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00002966
2967 if (DisableScheduling)
2968 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002969 }
2970
Bill Wendling856ff412009-12-22 00:12:37 +00002971 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2972 DAG.getVTList(&ValueVTs[0], NumValues),
2973 &Values[0], NumValues);
2974 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002975
Bill Wendling856ff412009-12-22 00:12:37 +00002976 if (DisableScheduling)
2977 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2978}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002979
Dan Gohman2048b852009-11-23 18:04:58 +00002980void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002981 Value *SrcV = I.getOperand(0);
2982 Value *PtrV = I.getOperand(1);
2983
Owen Andersone50ed302009-08-10 22:56:29 +00002984 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002985 SmallVector<uint64_t, 4> Offsets;
2986 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2987 unsigned NumValues = ValueVTs.size();
2988 if (NumValues == 0)
2989 return;
2990
2991 // Get the lowered operands. Note that we do this after
2992 // checking if NumResults is zero, because with zero results
2993 // the operands won't have values in the map.
2994 SDValue Src = getValue(SrcV);
2995 SDValue Ptr = getValue(PtrV);
2996
2997 SDValue Root = getRoot();
2998 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002999 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003000 bool isVolatile = I.isVolatile();
3001 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003002
3003 for (unsigned i = 0; i != NumValues; ++i) {
3004 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3005 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003006 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003007 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003008 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003009
Bill Wendling856ff412009-12-22 00:12:37 +00003010 if (DisableScheduling) {
3011 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3012 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3013 }
3014 }
3015
3016 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3017 MVT::Other, &Chains[0], NumValues);
3018 DAG.setRoot(Res);
3019
3020 if (DisableScheduling)
3021 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003022}
3023
3024/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3025/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003026void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3027 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003028 bool HasChain = !I.doesNotAccessMemory();
3029 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3030
3031 // Build the operand list.
3032 SmallVector<SDValue, 8> Ops;
3033 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3034 if (OnlyLoad) {
3035 // We don't need to serialize loads against other loads.
3036 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003037 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003038 Ops.push_back(getRoot());
3039 }
3040 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003041
3042 // Info is set by getTgtMemInstrinsic
3043 TargetLowering::IntrinsicInfo Info;
3044 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3045
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003046 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003047 if (!IsTgtIntrinsic)
3048 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003049
3050 // Add all operands of the call to the operand list.
3051 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3052 SDValue Op = getValue(I.getOperand(i));
3053 assert(TLI.isTypeLegal(Op.getValueType()) &&
3054 "Intrinsic uses a non-legal type?");
3055 Ops.push_back(Op);
3056 }
3057
Owen Andersone50ed302009-08-10 22:56:29 +00003058 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003059 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3060#ifndef NDEBUG
3061 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3062 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3063 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003064 }
Bob Wilson8d919552009-07-31 22:41:21 +00003065#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003067 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003068 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003069
Bob Wilson8d919552009-07-31 22:41:21 +00003070 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003071
3072 // Create the node.
3073 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003074 if (IsTgtIntrinsic) {
3075 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003076 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003077 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003078 Info.memVT, Info.ptrVal, Info.offset,
3079 Info.align, Info.vol,
3080 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003081 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003082 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003083 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003084 } else if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003085 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003086 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003087 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003088 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003089 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003090 }
3091
3092 if (DisableScheduling)
3093 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003094
3095 if (HasChain) {
3096 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3097 if (OnlyLoad)
3098 PendingLoads.push_back(Chain);
3099 else
3100 DAG.setRoot(Chain);
3101 }
Bill Wendling856ff412009-12-22 00:12:37 +00003102
Owen Anderson1d0be152009-08-13 21:58:54 +00003103 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003104 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003105 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003106 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003107
3108 if (DisableScheduling)
3109 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003110 }
Bill Wendling856ff412009-12-22 00:12:37 +00003111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003112 setValue(&I, Result);
3113 }
3114}
3115
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003116/// GetSignificand - Get the significand and build it into a floating-point
3117/// number with exponent of 1:
3118///
3119/// Op = (Op & 0x007fffff) | 0x3f800000;
3120///
3121/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003122static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003123GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3125 DAG.getConstant(0x007fffff, MVT::i32));
3126 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3127 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003128 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3129
3130 if (DisableScheduling) {
3131 DAG.AssignOrdering(t1.getNode(), Order);
3132 DAG.AssignOrdering(t2.getNode(), Order);
3133 DAG.AssignOrdering(Res.getNode(), Order);
3134 }
3135
3136 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003137}
3138
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003139/// GetExponent - Get the exponent:
3140///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003141/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003142///
3143/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003144static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003145GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003146 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003147 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3148 DAG.getConstant(0x7f800000, MVT::i32));
3149 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003150 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003151 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3152 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003153 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3154
3155 if (DisableScheduling) {
3156 DAG.AssignOrdering(t0.getNode(), Order);
3157 DAG.AssignOrdering(t1.getNode(), Order);
3158 DAG.AssignOrdering(t2.getNode(), Order);
3159 DAG.AssignOrdering(Res.getNode(), Order);
3160 }
3161
3162 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003163}
3164
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003165/// getF32Constant - Get 32-bit floating point constant.
3166static SDValue
3167getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003169}
3170
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003171/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003172/// visitIntrinsicCall: I is a call instruction
3173/// Op is the associated NodeType for I
3174const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003175SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003176 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003177 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003178 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003179 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003180 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003181 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003182 getValue(I.getOperand(2)),
3183 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003184 setValue(&I, L);
3185 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003186
3187 if (DisableScheduling)
3188 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3189
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003190 return 0;
3191}
3192
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003193// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003194const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003195SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003196 SDValue Op1 = getValue(I.getOperand(1));
3197 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003198
Owen Anderson825b72b2009-08-11 20:47:22 +00003199 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003200 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003201
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003202 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003203
3204 if (DisableScheduling)
3205 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3206
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003207 return 0;
3208}
Bill Wendling74c37652008-12-09 22:08:41 +00003209
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003210/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3211/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003212void
Dan Gohman2048b852009-11-23 18:04:58 +00003213SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003214 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003215 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003216
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003218 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3219 SDValue Op = getValue(I.getOperand(1));
3220
3221 // Put the exponent in the right bit position for later addition to the
3222 // final result:
3223 //
3224 // #define LOG2OFe 1.4426950f
3225 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003227 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003229
3230 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003231 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3232 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003233
Bill Wendling856ff412009-12-22 00:12:37 +00003234 if (DisableScheduling) {
3235 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3236 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3237 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3238 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3239 }
3240
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003241 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003242 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003243 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003244
Bill Wendling856ff412009-12-22 00:12:37 +00003245 if (DisableScheduling)
3246 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3247
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003248 if (LimitFloatPrecision <= 6) {
3249 // For floating-point precision of 6:
3250 //
3251 // TwoToFractionalPartOfX =
3252 // 0.997535578f +
3253 // (0.735607626f + 0.252464424f * x) * x;
3254 //
3255 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003256 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003257 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003258 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003259 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003260 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3261 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003262 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003263 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003264
3265 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003266 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003267 TwoToFracPartOfX, IntegerPartOfX);
3268
Owen Anderson825b72b2009-08-11 20:47:22 +00003269 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003270
3271 if (DisableScheduling) {
3272 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3273 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3274 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3275 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3276 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3277 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3278 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3279 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003280 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3281 // For floating-point precision of 12:
3282 //
3283 // TwoToFractionalPartOfX =
3284 // 0.999892986f +
3285 // (0.696457318f +
3286 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3287 //
3288 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003290 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003292 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3294 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003295 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003296 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3297 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003298 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003299 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003300
3301 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003303 TwoToFracPartOfX, IntegerPartOfX);
3304
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003306
3307 if (DisableScheduling) {
3308 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3309 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3310 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3311 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3312 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3313 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3314 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3315 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3316 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3317 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003318 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3319 // For floating-point precision of 18:
3320 //
3321 // TwoToFractionalPartOfX =
3322 // 0.999999982f +
3323 // (0.693148872f +
3324 // (0.240227044f +
3325 // (0.554906021e-1f +
3326 // (0.961591928e-2f +
3327 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3328 //
3329 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003331 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003333 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3335 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003336 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3338 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003339 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3341 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003342 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003343 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3344 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003345 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3347 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003348 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003349 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003351
3352 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003354 TwoToFracPartOfX, IntegerPartOfX);
3355
Owen Anderson825b72b2009-08-11 20:47:22 +00003356 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003357
3358 if (DisableScheduling) {
3359 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3360 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3361 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3362 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3363 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3364 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3365 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3366 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3367 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3368 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3369 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3370 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3371 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3372 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3373 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3374 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003375 }
3376 } else {
3377 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003378 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003379 getValue(I.getOperand(1)).getValueType(),
3380 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003381 if (DisableScheduling)
3382 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003383 }
3384
Dale Johannesen59e577f2008-09-05 18:38:42 +00003385 setValue(&I, result);
3386}
3387
Bill Wendling39150252008-09-09 20:39:27 +00003388/// visitLog - Lower a log intrinsic. Handles the special sequences for
3389/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003390void
Dan Gohman2048b852009-11-23 18:04:58 +00003391SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003392 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003393 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003394
Owen Anderson825b72b2009-08-11 20:47:22 +00003395 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003396 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3397 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003398 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003399
Bill Wendling856ff412009-12-22 00:12:37 +00003400 if (DisableScheduling)
3401 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3402
Bill Wendling39150252008-09-09 20:39:27 +00003403 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003404 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003406 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003407
Bill Wendling856ff412009-12-22 00:12:37 +00003408 if (DisableScheduling)
3409 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3410
Bill Wendling39150252008-09-09 20:39:27 +00003411 // Get the significand and build it into a floating-point number with
3412 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003413 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003414
3415 if (LimitFloatPrecision <= 6) {
3416 // For floating-point precision of 6:
3417 //
3418 // LogofMantissa =
3419 // -1.1609546f +
3420 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003421 //
Bill Wendling39150252008-09-09 20:39:27 +00003422 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003424 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003426 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3428 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003429 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003430
Scott Michelfdc40a02009-02-17 22:15:04 +00003431 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003433
3434 if (DisableScheduling) {
3435 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3436 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3437 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3438 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3439 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3440 }
Bill Wendling39150252008-09-09 20:39:27 +00003441 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3442 // For floating-point precision of 12:
3443 //
3444 // LogOfMantissa =
3445 // -1.7417939f +
3446 // (2.8212026f +
3447 // (-1.4699568f +
3448 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3449 //
3450 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003451 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003452 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003454 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003455 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3456 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003457 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003458 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3459 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003460 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003461 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3462 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003463 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003464
Scott Michelfdc40a02009-02-17 22:15:04 +00003465 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003467
3468 if (DisableScheduling) {
3469 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3470 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3471 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3472 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3473 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3474 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3475 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3476 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3477 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3478 }
Bill Wendling39150252008-09-09 20:39:27 +00003479 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3480 // For floating-point precision of 18:
3481 //
3482 // LogOfMantissa =
3483 // -2.1072184f +
3484 // (4.2372794f +
3485 // (-3.7029485f +
3486 // (2.2781945f +
3487 // (-0.87823314f +
3488 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3489 //
3490 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003492 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003494 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3496 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003497 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3499 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003500 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003501 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3502 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003503 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3505 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003506 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003507 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3508 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003509 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003510
Scott Michelfdc40a02009-02-17 22:15:04 +00003511 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003513
3514 if (DisableScheduling) {
3515 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3516 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3517 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3518 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3519 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3520 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3521 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3522 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3523 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3524 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3525 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3526 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3527 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3528 }
Bill Wendling39150252008-09-09 20:39:27 +00003529 }
3530 } else {
3531 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003532 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003533 getValue(I.getOperand(1)).getValueType(),
3534 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003535
3536 if (DisableScheduling)
3537 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003538 }
3539
Dale Johannesen59e577f2008-09-05 18:38:42 +00003540 setValue(&I, result);
3541}
3542
Bill Wendling3eb59402008-09-09 00:28:24 +00003543/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3544/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003545void
Dan Gohman2048b852009-11-23 18:04:58 +00003546SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003547 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003548 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003549
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003551 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3552 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003554
Bill Wendling856ff412009-12-22 00:12:37 +00003555 if (DisableScheduling)
3556 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3557
Bill Wendling39150252008-09-09 20:39:27 +00003558 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003559 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3560
3561 if (DisableScheduling)
3562 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003563
3564 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003565 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003566 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003567
Bill Wendling3eb59402008-09-09 00:28:24 +00003568 // Different possible minimax approximations of significand in
3569 // floating-point for various degrees of accuracy over [1,2].
3570 if (LimitFloatPrecision <= 6) {
3571 // For floating-point precision of 6:
3572 //
3573 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3574 //
3575 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003576 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003577 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003579 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003580 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3581 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003582 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003583
Scott Michelfdc40a02009-02-17 22:15:04 +00003584 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003585 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003586
3587 if (DisableScheduling) {
3588 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3589 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3590 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3591 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3592 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3593 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003594 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3595 // For floating-point precision of 12:
3596 //
3597 // Log2ofMantissa =
3598 // -2.51285454f +
3599 // (4.07009056f +
3600 // (-2.12067489f +
3601 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003602 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003603 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003605 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003606 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003607 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003608 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3609 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003610 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003611 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3612 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003613 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003614 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3615 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003616 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003617
Scott Michelfdc40a02009-02-17 22:15:04 +00003618 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003619 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003620
3621 if (DisableScheduling) {
3622 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3623 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3624 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3625 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3626 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3627 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3628 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3629 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3630 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3631 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003632 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3633 // For floating-point precision of 18:
3634 //
3635 // Log2ofMantissa =
3636 // -3.0400495f +
3637 // (6.1129976f +
3638 // (-5.3420409f +
3639 // (3.2865683f +
3640 // (-1.2669343f +
3641 // (0.27515199f -
3642 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3643 //
3644 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003645 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003646 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003648 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003649 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3650 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003651 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003652 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3653 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003654 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003655 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3656 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003657 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003658 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3659 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003660 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3662 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003663 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003664
Scott Michelfdc40a02009-02-17 22:15:04 +00003665 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003666 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003667
3668 if (DisableScheduling) {
3669 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3670 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3671 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3672 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3673 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3674 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3675 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3676 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3677 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3678 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3679 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3680 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3681 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3682 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003683 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003684 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003685 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003686 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003687 getValue(I.getOperand(1)).getValueType(),
3688 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003689
3690 if (DisableScheduling)
3691 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003692 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003693
Dale Johannesen59e577f2008-09-05 18:38:42 +00003694 setValue(&I, result);
3695}
3696
Bill Wendling3eb59402008-09-09 00:28:24 +00003697/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3698/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003699void
Dan Gohman2048b852009-11-23 18:04:58 +00003700SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003701 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003702 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003703
Owen Anderson825b72b2009-08-11 20:47:22 +00003704 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003705 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3706 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003707 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003708
Bill Wendling856ff412009-12-22 00:12:37 +00003709 if (DisableScheduling)
3710 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3711
Bill Wendling39150252008-09-09 20:39:27 +00003712 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003713 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003714 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003715 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003716
Bill Wendling856ff412009-12-22 00:12:37 +00003717 if (DisableScheduling)
3718 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3719
Bill Wendling3eb59402008-09-09 00:28:24 +00003720 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003721 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003722 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003723
3724 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003725 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003726 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003727 // Log10ofMantissa =
3728 // -0.50419619f +
3729 // (0.60948995f - 0.10380950f * x) * x;
3730 //
3731 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003733 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003735 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003736 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3737 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003738 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003739
Scott Michelfdc40a02009-02-17 22:15:04 +00003740 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003741 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003742
3743 if (DisableScheduling) {
3744 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3745 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3746 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3747 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3748 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3749 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003750 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3751 // For floating-point precision of 12:
3752 //
3753 // Log10ofMantissa =
3754 // -0.64831180f +
3755 // (0.91751397f +
3756 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3757 //
3758 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003759 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003760 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003761 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003762 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003763 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3764 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003765 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003766 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3767 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003768 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003769
Scott Michelfdc40a02009-02-17 22:15:04 +00003770 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003771 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003772
3773 if (DisableScheduling) {
3774 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3775 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3776 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3777 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3778 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3779 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3780 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3781 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003782 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003783 // For floating-point precision of 18:
3784 //
3785 // Log10ofMantissa =
3786 // -0.84299375f +
3787 // (1.5327582f +
3788 // (-1.0688956f +
3789 // (0.49102474f +
3790 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3791 //
3792 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003793 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003794 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003795 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003796 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003797 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3798 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003799 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003800 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3801 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003802 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003803 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3804 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003805 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003806 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3807 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003808 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003809
Scott Michelfdc40a02009-02-17 22:15:04 +00003810 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003812
3813 if (DisableScheduling) {
3814 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3815 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3816 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3817 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3818 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3819 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3820 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3821 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3822 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3823 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3824 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3825 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003826 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003827 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003828 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003829 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003830 getValue(I.getOperand(1)).getValueType(),
3831 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003832
3833 if (DisableScheduling)
3834 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003835 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003836
Dale Johannesen59e577f2008-09-05 18:38:42 +00003837 setValue(&I, result);
3838}
3839
Bill Wendlinge10c8142008-09-09 22:39:21 +00003840/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3841/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003842void
Dan Gohman2048b852009-11-23 18:04:58 +00003843SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003844 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003845 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003846
Owen Anderson825b72b2009-08-11 20:47:22 +00003847 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003848 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3849 SDValue Op = getValue(I.getOperand(1));
3850
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003852
Bill Wendling856ff412009-12-22 00:12:37 +00003853 if (DisableScheduling)
3854 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3855
Bill Wendlinge10c8142008-09-09 22:39:21 +00003856 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003857 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3858 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003859
3860 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003861 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003862 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003863
Bill Wendling856ff412009-12-22 00:12:37 +00003864 if (DisableScheduling) {
3865 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3866 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3867 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3868 }
3869
Bill Wendlinge10c8142008-09-09 22:39:21 +00003870 if (LimitFloatPrecision <= 6) {
3871 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003872 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003873 // TwoToFractionalPartOfX =
3874 // 0.997535578f +
3875 // (0.735607626f + 0.252464424f * x) * x;
3876 //
3877 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003878 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003879 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003880 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003881 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3883 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003884 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003885 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003886 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003887 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003888
Scott Michelfdc40a02009-02-17 22:15:04 +00003889 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003890 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003891
3892 if (DisableScheduling) {
3893 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3894 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3895 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3896 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3897 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3898 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3899 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3900 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003901 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3902 // For floating-point precision of 12:
3903 //
3904 // TwoToFractionalPartOfX =
3905 // 0.999892986f +
3906 // (0.696457318f +
3907 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3908 //
3909 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003910 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003911 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003912 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003913 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003914 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3915 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003916 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3918 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003919 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003921 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003923
Scott Michelfdc40a02009-02-17 22:15:04 +00003924 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003925 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003926
3927 if (DisableScheduling) {
3928 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3929 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3930 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3931 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3932 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3933 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3934 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3935 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3936 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3937 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003938 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3939 // For floating-point precision of 18:
3940 //
3941 // TwoToFractionalPartOfX =
3942 // 0.999999982f +
3943 // (0.693148872f +
3944 // (0.240227044f +
3945 // (0.554906021e-1f +
3946 // (0.961591928e-2f +
3947 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3948 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003950 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003952 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003953 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3954 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003955 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3957 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003958 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003959 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3960 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003961 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3963 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003964 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003965 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3966 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003967 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003968 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003969 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003970 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003971
Scott Michelfdc40a02009-02-17 22:15:04 +00003972 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003973 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003974
3975 if (DisableScheduling) {
3976 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3977 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3978 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3979 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3980 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3981 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3982 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3983 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3984 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3985 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3986 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3987 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3988 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3989 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3990 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3991 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003992 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003993 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003994 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003995 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003996 getValue(I.getOperand(1)).getValueType(),
3997 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003998
3999 if (DisableScheduling)
4000 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004001 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004002
Dale Johannesen601d3c02008-09-05 01:48:15 +00004003 setValue(&I, result);
4004}
4005
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004006/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4007/// limited-precision mode with x == 10.0f.
4008void
Dan Gohman2048b852009-11-23 18:04:58 +00004009SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004010 SDValue result;
4011 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004012 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004013 bool IsExp10 = false;
4014
Owen Anderson825b72b2009-08-11 20:47:22 +00004015 if (getValue(Val).getValueType() == MVT::f32 &&
4016 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004017 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4018 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4019 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4020 APFloat Ten(10.0f);
4021 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4022 }
4023 }
4024 }
4025
4026 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4027 SDValue Op = getValue(I.getOperand(2));
4028
4029 // Put the exponent in the right bit position for later addition to the
4030 // final result:
4031 //
4032 // #define LOG2OF10 3.3219281f
4033 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004034 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004035 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004037
4038 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4040 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004041
Bill Wendling856ff412009-12-22 00:12:37 +00004042 if (DisableScheduling) {
4043 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4044 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4045 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4046 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4047 }
4048
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004049 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004051 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004052
Bill Wendling856ff412009-12-22 00:12:37 +00004053 if (DisableScheduling)
4054 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4055
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004056 if (LimitFloatPrecision <= 6) {
4057 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004058 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004059 // twoToFractionalPartOfX =
4060 // 0.997535578f +
4061 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004062 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004063 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004064 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004065 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004066 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004067 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4069 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004070 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004071 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004072 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004073 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004074
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004075 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004076 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004077
4078 if (DisableScheduling) {
4079 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4080 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4081 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4082 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4083 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4084 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4085 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4086 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004087 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4088 // For floating-point precision of 12:
4089 //
4090 // TwoToFractionalPartOfX =
4091 // 0.999892986f +
4092 // (0.696457318f +
4093 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4094 //
4095 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004096 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004097 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004098 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004099 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004100 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4101 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004102 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004103 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4104 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004105 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004107 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004108 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004109
Scott Michelfdc40a02009-02-17 22:15:04 +00004110 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004111 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004112
4113 if (DisableScheduling) {
4114 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4115 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4116 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4117 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4118 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4119 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4120 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4121 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4122 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4123 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004124 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4125 // For floating-point precision of 18:
4126 //
4127 // TwoToFractionalPartOfX =
4128 // 0.999999982f +
4129 // (0.693148872f +
4130 // (0.240227044f +
4131 // (0.554906021e-1f +
4132 // (0.961591928e-2f +
4133 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4134 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004136 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004138 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4140 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004141 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4143 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004144 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4146 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004147 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4149 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004150 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004151 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4152 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004153 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004155 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004157
Scott Michelfdc40a02009-02-17 22:15:04 +00004158 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004160
4161 if (DisableScheduling) {
4162 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4163 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4164 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4165 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4166 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4167 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4168 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4169 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4170 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4171 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4172 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4173 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4174 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4175 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4176 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4177 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004178 }
4179 } else {
4180 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004181 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004182 getValue(I.getOperand(1)).getValueType(),
4183 getValue(I.getOperand(1)),
4184 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004185
4186 if (DisableScheduling)
4187 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004188 }
4189
4190 setValue(&I, result);
4191}
4192
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004193/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4194/// we want to emit this as a call to a named external function, return the name
4195/// otherwise lower it and return null.
4196const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004197SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004198 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004199 SDValue Res;
4200
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004201 switch (Intrinsic) {
4202 default:
4203 // By default, turn this into a target intrinsic node.
4204 visitTargetIntrinsic(I, Intrinsic);
4205 return 0;
4206 case Intrinsic::vastart: visitVAStart(I); return 0;
4207 case Intrinsic::vaend: visitVAEnd(I); return 0;
4208 case Intrinsic::vacopy: visitVACopy(I); return 0;
4209 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004210 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4211 getValue(I.getOperand(1)));
4212 setValue(&I, Res);
4213 if (DisableScheduling)
4214 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004215 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004216 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004217 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4218 getValue(I.getOperand(1)));
4219 setValue(&I, Res);
4220 if (DisableScheduling)
4221 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004222 return 0;
4223 case Intrinsic::setjmp:
4224 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004225 case Intrinsic::longjmp:
4226 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004227 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004228 SDValue Op1 = getValue(I.getOperand(1));
4229 SDValue Op2 = getValue(I.getOperand(2));
4230 SDValue Op3 = getValue(I.getOperand(3));
4231 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004232 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4233 I.getOperand(1), 0, I.getOperand(2), 0);
4234 DAG.setRoot(Res);
4235 if (DisableScheduling)
4236 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004237 return 0;
4238 }
Chris Lattner824b9582008-11-21 16:42:48 +00004239 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004240 SDValue Op1 = getValue(I.getOperand(1));
4241 SDValue Op2 = getValue(I.getOperand(2));
4242 SDValue Op3 = getValue(I.getOperand(3));
4243 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004244 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4245 I.getOperand(1), 0);
4246 DAG.setRoot(Res);
4247 if (DisableScheduling)
4248 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004249 return 0;
4250 }
Chris Lattner824b9582008-11-21 16:42:48 +00004251 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004252 SDValue Op1 = getValue(I.getOperand(1));
4253 SDValue Op2 = getValue(I.getOperand(2));
4254 SDValue Op3 = getValue(I.getOperand(3));
4255 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4256
4257 // If the source and destination are known to not be aliases, we can
4258 // lower memmove as memcpy.
4259 uint64_t Size = -1ULL;
4260 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004261 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004262 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4263 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004264 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4265 I.getOperand(1), 0, I.getOperand(2), 0);
4266 DAG.setRoot(Res);
4267 if (DisableScheduling)
4268 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004269 return 0;
4270 }
4271
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004272 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4273 I.getOperand(1), 0, I.getOperand(2), 0);
4274 DAG.setRoot(Res);
4275 if (DisableScheduling)
4276 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004277 return 0;
4278 }
Devang Patel70d75ca2009-11-12 19:02:56 +00004279 case Intrinsic::dbg_stoppoint:
4280 case Intrinsic::dbg_region_start:
4281 case Intrinsic::dbg_region_end:
4282 case Intrinsic::dbg_func_start:
4283 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004284 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004285 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00004286 if (OptLevel != CodeGenOpt::None)
4287 // FIXME: Variable debug info is not supported here.
4288 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004289 DwarfWriter *DW = DAG.getDwarfWriter();
4290 if (!DW)
4291 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004292 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
4293 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
4294 return 0;
4295
Devang Patelac1ceb32009-10-09 22:42:28 +00004296 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004297 Value *Address = DI.getAddress();
4298 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4299 Address = BCI->getOperand(0);
4300 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4301 // Don't handle byval struct arguments or VLAs, for example.
4302 if (!AI)
4303 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004304 DenseMap<const AllocaInst*, int>::iterator SI =
4305 FuncInfo.StaticAllocaMap.find(AI);
4306 if (SI == FuncInfo.StaticAllocaMap.end())
4307 return 0; // VLAs.
4308 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004309
Devang Patelac1ceb32009-10-09 22:42:28 +00004310 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00004311 if (MMI) {
4312 MetadataContext &TheMetadata =
4313 DI.getParent()->getContext().getMetadata();
4314 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
4315 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
4316 MMI->setVariableDbgInfo(Variable, FI, Dbg);
4317 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004318 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004319 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004320 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004321 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004322 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004323 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004324 SDValue Ops[1];
4325 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004326 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004327 setValue(&I, Op);
4328 DAG.setRoot(Op.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004329 if (DisableScheduling)
4330 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004331 return 0;
4332 }
4333
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004334 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004335 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004336
Chris Lattner3a5815f2009-09-17 23:54:54 +00004337 if (CurMBB->isLandingPad())
4338 AddCatchInfo(I, MMI, CurMBB);
4339 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004340#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004341 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004342#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004343 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4344 unsigned Reg = TLI.getExceptionSelectorRegister();
4345 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004346 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004347
Chris Lattner3a5815f2009-09-17 23:54:54 +00004348 // Insert the EHSELECTION instruction.
4349 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4350 SDValue Ops[2];
4351 Ops[0] = getValue(I.getOperand(1));
4352 Ops[1] = getRoot();
4353 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4354
4355 DAG.setRoot(Op.getValue(1));
4356
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004357 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4358 setValue(&I, Res);
4359 if (DisableScheduling) {
4360 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4361 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4362 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004363 return 0;
4364 }
4365
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004366 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004367 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004368
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004369 if (MMI) {
4370 // Find the type id for the given typeinfo.
4371 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004372 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004373 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004374 } else {
4375 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004376 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004377 }
4378
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004379 setValue(&I, Res);
4380 if (DisableScheduling)
4381 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004382 return 0;
4383 }
4384
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004385 case Intrinsic::eh_return_i32:
4386 case Intrinsic::eh_return_i64:
4387 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004388 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004389 Res = DAG.getNode(ISD::EH_RETURN, dl,
4390 MVT::Other,
4391 getControlRoot(),
4392 getValue(I.getOperand(1)),
4393 getValue(I.getOperand(2)));
4394 DAG.setRoot(Res);
4395 if (DisableScheduling)
4396 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397 } else {
4398 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4399 }
4400
4401 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004402 case Intrinsic::eh_unwind_init:
4403 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4404 MMI->setCallsUnwindInit(true);
4405 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004406 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004407 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004408 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004409 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4410 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004411 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004412 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004413 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004414 TLI.getPointerTy()),
4415 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004416 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004417 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004418 DAG.getConstant(0, TLI.getPointerTy()));
4419 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4420 FA, Offset);
4421 setValue(&I, Res);
4422 if (DisableScheduling) {
4423 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4424 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4425 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4426 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4427 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004428 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004429 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004430 case Intrinsic::convertff:
4431 case Intrinsic::convertfsi:
4432 case Intrinsic::convertfui:
4433 case Intrinsic::convertsif:
4434 case Intrinsic::convertuif:
4435 case Intrinsic::convertss:
4436 case Intrinsic::convertsu:
4437 case Intrinsic::convertus:
4438 case Intrinsic::convertuu: {
4439 ISD::CvtCode Code = ISD::CVT_INVALID;
4440 switch (Intrinsic) {
4441 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4442 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4443 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4444 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4445 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4446 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4447 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4448 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4449 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4450 }
Owen Andersone50ed302009-08-10 22:56:29 +00004451 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004452 Value *Op1 = I.getOperand(1);
4453 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4454 DAG.getValueType(DestVT),
4455 DAG.getValueType(getValue(Op1).getValueType()),
4456 getValue(I.getOperand(2)),
4457 getValue(I.getOperand(3)),
4458 Code);
4459 setValue(&I, Res);
4460 if (DisableScheduling)
4461 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004462 return 0;
4463 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004464 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004465 Res = DAG.getNode(ISD::FSQRT, dl,
4466 getValue(I.getOperand(1)).getValueType(),
4467 getValue(I.getOperand(1)));
4468 setValue(&I, Res);
4469 if (DisableScheduling)
4470 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004471 return 0;
4472 case Intrinsic::powi:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004473 Res = DAG.getNode(ISD::FPOWI, dl,
4474 getValue(I.getOperand(1)).getValueType(),
4475 getValue(I.getOperand(1)),
4476 getValue(I.getOperand(2)));
4477 setValue(&I, Res);
4478 if (DisableScheduling)
4479 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004480 return 0;
4481 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004482 Res = DAG.getNode(ISD::FSIN, dl,
4483 getValue(I.getOperand(1)).getValueType(),
4484 getValue(I.getOperand(1)));
4485 setValue(&I, Res);
4486 if (DisableScheduling)
4487 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004488 return 0;
4489 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004490 Res = DAG.getNode(ISD::FCOS, dl,
4491 getValue(I.getOperand(1)).getValueType(),
4492 getValue(I.getOperand(1)));
4493 setValue(&I, Res);
4494 if (DisableScheduling)
4495 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004496 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004497 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004498 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004499 return 0;
4500 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004501 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004502 return 0;
4503 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004504 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004505 return 0;
4506 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004507 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004508 return 0;
4509 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004510 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004511 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004512 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004513 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004514 return 0;
4515 case Intrinsic::pcmarker: {
4516 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004517 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4518 DAG.setRoot(Res);
4519 if (DisableScheduling)
4520 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004521 return 0;
4522 }
4523 case Intrinsic::readcyclecounter: {
4524 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004525 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4526 DAG.getVTList(MVT::i64, MVT::Other),
4527 &Op, 1);
4528 setValue(&I, Res);
4529 DAG.setRoot(Res.getValue(1));
4530 if (DisableScheduling)
4531 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004532 return 0;
4533 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004534 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004535 Res = DAG.getNode(ISD::BSWAP, dl,
4536 getValue(I.getOperand(1)).getValueType(),
4537 getValue(I.getOperand(1)));
4538 setValue(&I, Res);
4539 if (DisableScheduling)
4540 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004541 return 0;
4542 case Intrinsic::cttz: {
4543 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004544 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004545 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4546 setValue(&I, Res);
4547 if (DisableScheduling)
4548 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004549 return 0;
4550 }
4551 case Intrinsic::ctlz: {
4552 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004553 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004554 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4555 setValue(&I, Res);
4556 if (DisableScheduling)
4557 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004558 return 0;
4559 }
4560 case Intrinsic::ctpop: {
4561 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004562 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004563 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4564 setValue(&I, Res);
4565 if (DisableScheduling)
4566 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004567 return 0;
4568 }
4569 case Intrinsic::stacksave: {
4570 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004571 Res = DAG.getNode(ISD::STACKSAVE, dl,
4572 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4573 setValue(&I, Res);
4574 DAG.setRoot(Res.getValue(1));
4575 if (DisableScheduling)
4576 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004577 return 0;
4578 }
4579 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004580 Res = getValue(I.getOperand(1));
4581 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4582 DAG.setRoot(Res);
4583 if (DisableScheduling)
4584 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004585 return 0;
4586 }
Bill Wendling57344502008-11-18 11:01:33 +00004587 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004588 // Emit code into the DAG to store the stack guard onto the stack.
4589 MachineFunction &MF = DAG.getMachineFunction();
4590 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004591 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004592
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004593 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4594 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004595
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004596 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004597 MFI->setStackProtectorIndex(FI);
4598
4599 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4600
4601 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004602 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4603 PseudoSourceValue::getFixedStack(FI),
4604 0, true);
4605 setValue(&I, Res);
4606 DAG.setRoot(Res);
4607 if (DisableScheduling)
4608 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004609 return 0;
4610 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004611 case Intrinsic::objectsize: {
4612 // If we don't know by now, we're never going to know.
4613 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4614
4615 assert(CI && "Non-constant type in __builtin_object_size?");
4616
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004617 SDValue Arg = getValue(I.getOperand(0));
4618 EVT Ty = Arg.getValueType();
4619
Eric Christopher7b5e6172009-10-27 00:52:25 +00004620 if (CI->getZExtValue() < 2)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004621 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004622 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004623 Res = DAG.getConstant(0, Ty);
4624
4625 setValue(&I, Res);
4626 if (DisableScheduling)
4627 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004628 return 0;
4629 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004630 case Intrinsic::var_annotation:
4631 // Discard annotate attributes
4632 return 0;
4633
4634 case Intrinsic::init_trampoline: {
4635 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4636
4637 SDValue Ops[6];
4638 Ops[0] = getRoot();
4639 Ops[1] = getValue(I.getOperand(1));
4640 Ops[2] = getValue(I.getOperand(2));
4641 Ops[3] = getValue(I.getOperand(3));
4642 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4643 Ops[5] = DAG.getSrcValue(F);
4644
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004645 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4646 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4647 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004648
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004649 setValue(&I, Res);
4650 DAG.setRoot(Res.getValue(1));
4651 if (DisableScheduling)
4652 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004653 return 0;
4654 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004655 case Intrinsic::gcroot:
4656 if (GFI) {
4657 Value *Alloca = I.getOperand(1);
4658 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004659
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004660 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4661 GFI->addStackRoot(FI->getIndex(), TypeMap);
4662 }
4663 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004664 case Intrinsic::gcread:
4665 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004666 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004667 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004668 case Intrinsic::flt_rounds:
4669 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4670 setValue(&I, Res);
4671 if (DisableScheduling)
4672 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004673 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004674 case Intrinsic::trap:
4675 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4676 DAG.setRoot(Res);
4677 if (DisableScheduling)
4678 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004679 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004680 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004681 return implVisitAluOverflow(I, ISD::UADDO);
4682 case Intrinsic::sadd_with_overflow:
4683 return implVisitAluOverflow(I, ISD::SADDO);
4684 case Intrinsic::usub_with_overflow:
4685 return implVisitAluOverflow(I, ISD::USUBO);
4686 case Intrinsic::ssub_with_overflow:
4687 return implVisitAluOverflow(I, ISD::SSUBO);
4688 case Intrinsic::umul_with_overflow:
4689 return implVisitAluOverflow(I, ISD::UMULO);
4690 case Intrinsic::smul_with_overflow:
4691 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004692
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004693 case Intrinsic::prefetch: {
4694 SDValue Ops[4];
4695 Ops[0] = getRoot();
4696 Ops[1] = getValue(I.getOperand(1));
4697 Ops[2] = getValue(I.getOperand(2));
4698 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004699 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4700 DAG.setRoot(Res);
4701 if (DisableScheduling)
4702 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004703 return 0;
4704 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004705
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004706 case Intrinsic::memory_barrier: {
4707 SDValue Ops[6];
4708 Ops[0] = getRoot();
4709 for (int x = 1; x < 6; ++x)
4710 Ops[x] = getValue(I.getOperand(x));
4711
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004712 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4713 DAG.setRoot(Res);
4714 if (DisableScheduling)
4715 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004716 return 0;
4717 }
4718 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004719 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004720 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004721 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004722 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4723 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004724 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004725 getValue(I.getOperand(2)),
4726 getValue(I.getOperand(3)),
4727 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004728 setValue(&I, L);
4729 DAG.setRoot(L.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004730 if (DisableScheduling)
4731 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004732 return 0;
4733 }
4734 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004735 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004736 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004737 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004738 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004739 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004740 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004741 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004742 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004743 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004744 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004745 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004746 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004747 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004748 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004749 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004750 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004751 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004752 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004753 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004754 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004755 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004756
4757 case Intrinsic::invariant_start:
4758 case Intrinsic::lifetime_start:
4759 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004760 Res = DAG.getUNDEF(TLI.getPointerTy());
4761 setValue(&I, Res);
4762 if (DisableScheduling)
4763 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004764 return 0;
4765 case Intrinsic::invariant_end:
4766 case Intrinsic::lifetime_end:
4767 // Discard region information.
4768 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004769 }
4770}
4771
Dan Gohman98ca4f22009-08-05 01:29:28 +00004772/// Test if the given instruction is in a position to be optimized
4773/// with a tail-call. This roughly means that it's in a block with
4774/// a return and there's nothing that needs to be scheduled
4775/// between it and the return.
4776///
4777/// This function only tests target-independent requirements.
4778/// For target-dependent requirements, a target should override
4779/// TargetLowering::IsEligibleForTailCallOptimization.
4780///
4781static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004782isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004783 const TargetLowering &TLI) {
4784 const BasicBlock *ExitBB = I->getParent();
4785 const TerminatorInst *Term = ExitBB->getTerminator();
4786 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4787 const Function *F = ExitBB->getParent();
4788
4789 // The block must end in a return statement or an unreachable.
4790 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4791
4792 // If I will have a chain, make sure no other instruction that will have a
4793 // chain interposes between I and the return.
4794 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4795 !I->isSafeToSpeculativelyExecute())
4796 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4797 --BBI) {
4798 if (&*BBI == I)
4799 break;
4800 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4801 !BBI->isSafeToSpeculativelyExecute())
4802 return false;
4803 }
4804
4805 // If the block ends with a void return or unreachable, it doesn't matter
4806 // what the call's return type is.
4807 if (!Ret || Ret->getNumOperands() == 0) return true;
4808
Dan Gohmaned9bab32009-11-14 02:06:30 +00004809 // If the return value is undef, it doesn't matter what the call's
4810 // return type is.
4811 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4812
Dan Gohman98ca4f22009-08-05 01:29:28 +00004813 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004814 // the return. Ignore noalias because it doesn't affect the call sequence.
4815 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4816 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004817 return false;
4818
4819 // Otherwise, make sure the unmodified return value of I is the return value.
4820 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4821 U = dyn_cast<Instruction>(U->getOperand(0))) {
4822 if (!U)
4823 return false;
4824 if (!U->hasOneUse())
4825 return false;
4826 if (U == I)
4827 break;
4828 // Check for a truly no-op truncate.
4829 if (isa<TruncInst>(U) &&
4830 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4831 continue;
4832 // Check for a truly no-op bitcast.
4833 if (isa<BitCastInst>(U) &&
4834 (U->getOperand(0)->getType() == U->getType() ||
4835 (isa<PointerType>(U->getOperand(0)->getType()) &&
4836 isa<PointerType>(U->getType()))))
4837 continue;
4838 // Otherwise it's not a true no-op.
4839 return false;
4840 }
4841
4842 return true;
4843}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004844
Dan Gohman2048b852009-11-23 18:04:58 +00004845void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4846 bool isTailCall,
4847 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004848 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4849 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004850 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004851 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4852 unsigned BeginLabel = 0, EndLabel = 0;
4853
4854 TargetLowering::ArgListTy Args;
4855 TargetLowering::ArgListEntry Entry;
4856 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004857
4858 // Check whether the function can return without sret-demotion.
4859 SmallVector<EVT, 4> OutVTs;
4860 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4861 SmallVector<uint64_t, 4> Offsets;
4862 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4863 OutVTs, OutsFlags, TLI, &Offsets);
4864
4865
4866 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4867 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4868
4869 SDValue DemoteStackSlot;
4870
4871 if (!CanLowerReturn) {
4872 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4873 FTy->getReturnType());
4874 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4875 FTy->getReturnType());
4876 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004877 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004878 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4879
4880 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4881 Entry.Node = DemoteStackSlot;
4882 Entry.Ty = StackSlotPtrType;
4883 Entry.isSExt = false;
4884 Entry.isZExt = false;
4885 Entry.isInReg = false;
4886 Entry.isSRet = true;
4887 Entry.isNest = false;
4888 Entry.isByVal = false;
4889 Entry.Alignment = Align;
4890 Args.push_back(Entry);
4891 RetTy = Type::getVoidTy(FTy->getContext());
4892 }
4893
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004894 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004895 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004896 SDValue ArgNode = getValue(*i);
4897 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4898
4899 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004900 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4901 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4902 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4903 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4904 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4905 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004906 Entry.Alignment = CS.getParamAlignment(attrInd);
4907 Args.push_back(Entry);
4908 }
4909
4910 if (LandingPad && MMI) {
4911 // Insert a label before the invoke call to mark the try range. This can be
4912 // used to detect deletion of the invoke via the MachineModuleInfo.
4913 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004915 // Both PendingLoads and PendingExports must be flushed here;
4916 // this call might not return.
4917 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004918 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4919 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004920 }
4921
Dan Gohman98ca4f22009-08-05 01:29:28 +00004922 // Check if target-independent constraints permit a tail call here.
4923 // Target-dependent constraints are checked within TLI.LowerCallTo.
4924 if (isTailCall &&
4925 !isInTailCallPosition(CS.getInstruction(),
4926 CS.getAttributes().getRetAttributes(),
4927 TLI))
4928 isTailCall = false;
4929
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004930 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004931 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004932 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004933 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004934 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004935 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004936 isTailCall,
4937 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004938 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004939 assert((isTailCall || Result.second.getNode()) &&
4940 "Non-null chain expected with non-tail call!");
4941 assert((Result.second.getNode() || !Result.first.getNode()) &&
4942 "Null value expected with tail call!");
4943 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004944 setValue(CS.getInstruction(), Result.first);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004945 else if (!CanLowerReturn && Result.second.getNode()) {
4946 // The instruction result is the result of loading from the
4947 // hidden sret parameter.
4948 SmallVector<EVT, 1> PVTs;
4949 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4950
4951 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4952 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4953 EVT PtrVT = PVTs[0];
4954 unsigned NumValues = OutVTs.size();
4955 SmallVector<SDValue, 4> Values(NumValues);
4956 SmallVector<SDValue, 4> Chains(NumValues);
4957
4958 for (unsigned i = 0; i < NumValues; ++i) {
4959 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
4960 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, DemoteStackSlot,
4961 DAG.getConstant(Offsets[i], PtrVT)),
4962 NULL, Offsets[i], false, 1);
4963 Values[i] = L;
4964 Chains[i] = L.getValue(1);
4965 }
4966 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4967 MVT::Other, &Chains[0], NumValues);
4968 PendingLoads.push_back(Chain);
4969
4970 setValue(CS.getInstruction(), DAG.getNode(ISD::MERGE_VALUES,
4971 getCurDebugLoc(), DAG.getVTList(&OutVTs[0], NumValues),
4972 &Values[0], NumValues));
4973 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00004974 // As a special case, a null chain means that a tail call has
4975 // been emitted and the DAG root is already updated.
4976 if (Result.second.getNode())
4977 DAG.setRoot(Result.second);
4978 else
4979 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004980
4981 if (LandingPad && MMI) {
4982 // Insert a label at the end of the invoke call to mark the try range. This
4983 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4984 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004985 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4986 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004987
4988 // Inform MachineModuleInfo of range.
4989 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4990 }
4991}
4992
4993
Dan Gohman2048b852009-11-23 18:04:58 +00004994void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004995 const char *RenameFn = 0;
4996 if (Function *F = I.getCalledFunction()) {
4997 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004998 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4999 if (II) {
5000 if (unsigned IID = II->getIntrinsicID(F)) {
5001 RenameFn = visitIntrinsicCall(I, IID);
5002 if (!RenameFn)
5003 return;
5004 }
5005 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005006 if (unsigned IID = F->getIntrinsicID()) {
5007 RenameFn = visitIntrinsicCall(I, IID);
5008 if (!RenameFn)
5009 return;
5010 }
5011 }
5012
5013 // Check for well-known libc/libm calls. If the function is internal, it
5014 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005015 if (!F->hasLocalLinkage() && F->hasName()) {
5016 StringRef Name = F->getName();
5017 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005018 if (I.getNumOperands() == 3 && // Basic sanity checks.
5019 I.getOperand(1)->getType()->isFloatingPoint() &&
5020 I.getType() == I.getOperand(1)->getType() &&
5021 I.getType() == I.getOperand(2)->getType()) {
5022 SDValue LHS = getValue(I.getOperand(1));
5023 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00005024 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005025 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005026 return;
5027 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005028 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005029 if (I.getNumOperands() == 2 && // Basic sanity checks.
5030 I.getOperand(1)->getType()->isFloatingPoint() &&
5031 I.getType() == I.getOperand(1)->getType()) {
5032 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005033 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005034 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005035 return;
5036 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005037 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005038 if (I.getNumOperands() == 2 && // Basic sanity checks.
5039 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005040 I.getType() == I.getOperand(1)->getType() &&
5041 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005042 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005043 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005044 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005045 return;
5046 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005047 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005048 if (I.getNumOperands() == 2 && // Basic sanity checks.
5049 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005050 I.getType() == I.getOperand(1)->getType() &&
5051 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005052 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00005053 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005054 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005055 return;
5056 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005057 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5058 if (I.getNumOperands() == 2 && // Basic sanity checks.
5059 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005060 I.getType() == I.getOperand(1)->getType() &&
5061 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005062 SDValue Tmp = getValue(I.getOperand(1));
5063 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5064 Tmp.getValueType(), Tmp));
5065 return;
5066 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067 }
5068 }
5069 } else if (isa<InlineAsm>(I.getOperand(0))) {
5070 visitInlineAsm(&I);
5071 return;
5072 }
5073
5074 SDValue Callee;
5075 if (!RenameFn)
5076 Callee = getValue(I.getOperand(0));
5077 else
Bill Wendling056292f2008-09-16 21:48:12 +00005078 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005079
Dan Gohman98ca4f22009-08-05 01:29:28 +00005080 // Check if we can potentially perform a tail call. More detailed
5081 // checking is be done within LowerCallTo, after more information
5082 // about the call is known.
5083 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5084
5085 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005086}
5087
5088
5089/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005090/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005091/// Chain/Flag as the input and updates them for the output Chain/Flag.
5092/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005093SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005094 SDValue &Chain,
5095 SDValue *Flag) const {
5096 // Assemble the legal parts into the final values.
5097 SmallVector<SDValue, 4> Values(ValueVTs.size());
5098 SmallVector<SDValue, 8> Parts;
5099 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5100 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005101 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005102 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005103 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005104
5105 Parts.resize(NumRegs);
5106 for (unsigned i = 0; i != NumRegs; ++i) {
5107 SDValue P;
5108 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00005109 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005110 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005111 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005112 *Flag = P.getValue(2);
5113 }
5114 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005115
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005116 // If the source register was virtual and if we know something about it,
5117 // add an assert node.
5118 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5119 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5120 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5121 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5122 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5123 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005124
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005125 unsigned RegSize = RegisterVT.getSizeInBits();
5126 unsigned NumSignBits = LOI.NumSignBits;
5127 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005129 // FIXME: We capture more information than the dag can represent. For
5130 // now, just use the tightest assertzext/assertsext possible.
5131 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005132 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005133 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005134 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005135 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005136 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005137 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005138 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005139 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005140 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005141 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005142 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005143 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005144 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005145 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005146 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005147 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005148 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005149
Owen Anderson825b72b2009-08-11 20:47:22 +00005150 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005151 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005152 RegisterVT, P, DAG.getValueType(FromVT));
5153
5154 }
5155 }
5156 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005158 Parts[i] = P;
5159 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005160
Scott Michelfdc40a02009-02-17 22:15:04 +00005161 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005162 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005163 Part += NumRegs;
5164 Parts.clear();
5165 }
5166
Dale Johannesen66978ee2009-01-31 02:22:37 +00005167 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00005168 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5169 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005170}
5171
5172/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005173/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005174/// Chain/Flag as the input and updates them for the output Chain/Flag.
5175/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005176void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005177 SDValue &Chain, SDValue *Flag) const {
5178 // Get the list of the values's legal parts.
5179 unsigned NumRegs = Regs.size();
5180 SmallVector<SDValue, 8> Parts(NumRegs);
5181 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005182 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005183 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005184 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005185
Dale Johannesen66978ee2009-01-31 02:22:37 +00005186 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005187 &Parts[Part], NumParts, RegisterVT);
5188 Part += NumParts;
5189 }
5190
5191 // Copy the parts into the registers.
5192 SmallVector<SDValue, 8> Chains(NumRegs);
5193 for (unsigned i = 0; i != NumRegs; ++i) {
5194 SDValue Part;
5195 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00005196 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005197 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005198 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005199 *Flag = Part.getValue(1);
5200 }
5201 Chains[i] = Part.getValue(0);
5202 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005203
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005204 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005205 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005206 // flagged to it. That is the CopyToReg nodes and the user are considered
5207 // a single scheduling unit. If we create a TokenFactor and return it as
5208 // chain, then the TokenFactor is both a predecessor (operand) of the
5209 // user as well as a successor (the TF operands are flagged to the user).
5210 // c1, f1 = CopyToReg
5211 // c2, f2 = CopyToReg
5212 // c3 = TokenFactor c1, c2
5213 // ...
5214 // = op c3, ..., f2
5215 Chain = Chains[NumRegs-1];
5216 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005217 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005218}
5219
5220/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005221/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005222/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005223void RegsForValue::AddInlineAsmOperands(unsigned Code,
5224 bool HasMatching,unsigned MatchingIdx,
5225 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005226 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005227 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005228 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5229 unsigned Flag = Code | (Regs.size() << 3);
5230 if (HasMatching)
5231 Flag |= 0x80000000 | (MatchingIdx << 16);
5232 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005233 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005234 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005235 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005236 for (unsigned i = 0; i != NumRegs; ++i) {
5237 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005238 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00005239 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 }
5241}
5242
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005243/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005244/// i.e. it isn't a stack pointer or some other special register, return the
5245/// register class for the register. Otherwise, return null.
5246static const TargetRegisterClass *
5247isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5248 const TargetLowering &TLI,
5249 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005250 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005251 const TargetRegisterClass *FoundRC = 0;
5252 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5253 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005254 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005255
5256 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005257 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5259 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5260 I != E; ++I) {
5261 if (TLI.isTypeLegal(*I)) {
5262 // If we have already found this register in a different register class,
5263 // choose the one with the largest VT specified. For example, on
5264 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005265 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005266 ThisVT = *I;
5267 break;
5268 }
5269 }
5270 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005271
Owen Anderson825b72b2009-08-11 20:47:22 +00005272 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005273
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005274 // NOTE: This isn't ideal. In particular, this might allocate the
5275 // frame pointer in functions that need it (due to them not being taken
5276 // out of allocation, because a variable sized allocation hasn't been seen
5277 // yet). This is a slight code pessimization, but should still work.
5278 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5279 E = RC->allocation_order_end(MF); I != E; ++I)
5280 if (*I == Reg) {
5281 // We found a matching register class. Keep looking at others in case
5282 // we find one with larger registers that this physreg is also in.
5283 FoundRC = RC;
5284 FoundVT = ThisVT;
5285 break;
5286 }
5287 }
5288 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005289}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005290
5291
5292namespace llvm {
5293/// AsmOperandInfo - This contains information for each constraint that we are
5294/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005295class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005296 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005297public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005298 /// CallOperand - If this is the result output operand or a clobber
5299 /// this is null, otherwise it is the incoming operand to the CallInst.
5300 /// This gets modified as the asm is processed.
5301 SDValue CallOperand;
5302
5303 /// AssignedRegs - If this is a register or register class operand, this
5304 /// contains the set of register corresponding to the operand.
5305 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005306
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005307 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5308 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5309 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005310
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005311 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5312 /// busy in OutputRegs/InputRegs.
5313 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005314 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005315 std::set<unsigned> &InputRegs,
5316 const TargetRegisterInfo &TRI) const {
5317 if (isOutReg) {
5318 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5319 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5320 }
5321 if (isInReg) {
5322 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5323 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5324 }
5325 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005326
Owen Andersone50ed302009-08-10 22:56:29 +00005327 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005328 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005329 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00005330 EVT getCallOperandValEVT(LLVMContext &Context,
5331 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005332 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005333 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005334
Chris Lattner81249c92008-10-17 17:05:25 +00005335 if (isa<BasicBlock>(CallOperandVal))
5336 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005337
Chris Lattner81249c92008-10-17 17:05:25 +00005338 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005339
Chris Lattner81249c92008-10-17 17:05:25 +00005340 // If this is an indirect operand, the operand is a pointer to the
5341 // accessed type.
5342 if (isIndirect)
5343 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005344
Chris Lattner81249c92008-10-17 17:05:25 +00005345 // If OpTy is not a single value, it may be a struct/union that we
5346 // can tile with integers.
5347 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5348 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5349 switch (BitSize) {
5350 default: break;
5351 case 1:
5352 case 8:
5353 case 16:
5354 case 32:
5355 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005356 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005357 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005358 break;
5359 }
5360 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005361
Chris Lattner81249c92008-10-17 17:05:25 +00005362 return TLI.getValueType(OpTy, true);
5363 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005364
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005365private:
5366 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5367 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005368 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005369 const TargetRegisterInfo &TRI) {
5370 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5371 Regs.insert(Reg);
5372 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5373 for (; *Aliases; ++Aliases)
5374 Regs.insert(*Aliases);
5375 }
5376};
5377} // end llvm namespace.
5378
5379
5380/// GetRegistersForValue - Assign registers (virtual or physical) for the
5381/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005382/// register allocator to handle the assignment process. However, if the asm
5383/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005384/// allocation. This produces generally horrible, but correct, code.
5385///
5386/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005387/// Input and OutputRegs are the set of already allocated physical registers.
5388///
Dan Gohman2048b852009-11-23 18:04:58 +00005389void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005390GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005391 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005392 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005393 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005394
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005395 // Compute whether this value requires an input register, an output register,
5396 // or both.
5397 bool isOutReg = false;
5398 bool isInReg = false;
5399 switch (OpInfo.Type) {
5400 case InlineAsm::isOutput:
5401 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005402
5403 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005404 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005405 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005406 break;
5407 case InlineAsm::isInput:
5408 isInReg = true;
5409 isOutReg = false;
5410 break;
5411 case InlineAsm::isClobber:
5412 isOutReg = true;
5413 isInReg = true;
5414 break;
5415 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005416
5417
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005418 MachineFunction &MF = DAG.getMachineFunction();
5419 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005421 // If this is a constraint for a single physreg, or a constraint for a
5422 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005423 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005424 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5425 OpInfo.ConstraintVT);
5426
5427 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005428 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005429 // If this is a FP input in an integer register (or visa versa) insert a bit
5430 // cast of the input value. More generally, handle any case where the input
5431 // value disagrees with the register class we plan to stick this in.
5432 if (OpInfo.Type == InlineAsm::isInput &&
5433 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005434 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005435 // types are identical size, use a bitcast to convert (e.g. two differing
5436 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005437 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005438 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005439 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005440 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005441 OpInfo.ConstraintVT = RegVT;
5442 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5443 // If the input is a FP value and we want it in FP registers, do a
5444 // bitcast to the corresponding integer type. This turns an f64 value
5445 // into i64, which can be passed with two i32 values on a 32-bit
5446 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005447 RegVT = EVT::getIntegerVT(Context,
5448 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005449 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005450 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005451 OpInfo.ConstraintVT = RegVT;
5452 }
5453 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005454
Owen Anderson23b9b192009-08-12 00:36:31 +00005455 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005456 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005457
Owen Andersone50ed302009-08-10 22:56:29 +00005458 EVT RegVT;
5459 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005460
5461 // If this is a constraint for a specific physical register, like {r17},
5462 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005463 if (unsigned AssignedReg = PhysReg.first) {
5464 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005465 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005466 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005467
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468 // Get the actual register value type. This is important, because the user
5469 // may have asked for (e.g.) the AX register in i32 type. We need to
5470 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005471 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005472
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005473 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005474 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005475
5476 // If this is an expanded reference, add the rest of the regs to Regs.
5477 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005478 TargetRegisterClass::iterator I = RC->begin();
5479 for (; *I != AssignedReg; ++I)
5480 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005481
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005482 // Already added the first reg.
5483 --NumRegs; ++I;
5484 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005485 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005486 Regs.push_back(*I);
5487 }
5488 }
5489 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5490 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5491 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5492 return;
5493 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005494
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005495 // Otherwise, if this was a reference to an LLVM register class, create vregs
5496 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005497 if (const TargetRegisterClass *RC = PhysReg.second) {
5498 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005499 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005500 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005501
Evan Chengfb112882009-03-23 08:01:15 +00005502 // Create the appropriate number of virtual registers.
5503 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5504 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005505 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005506
Evan Chengfb112882009-03-23 08:01:15 +00005507 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5508 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005509 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005510
5511 // This is a reference to a register class that doesn't directly correspond
5512 // to an LLVM register class. Allocate NumRegs consecutive, available,
5513 // registers from the class.
5514 std::vector<unsigned> RegClassRegs
5515 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5516 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005517
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005518 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5519 unsigned NumAllocated = 0;
5520 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5521 unsigned Reg = RegClassRegs[i];
5522 // See if this register is available.
5523 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5524 (isInReg && InputRegs.count(Reg))) { // Already used.
5525 // Make sure we find consecutive registers.
5526 NumAllocated = 0;
5527 continue;
5528 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005530 // Check to see if this register is allocatable (i.e. don't give out the
5531 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005532 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5533 if (!RC) { // Couldn't allocate this register.
5534 // Reset NumAllocated to make sure we return consecutive registers.
5535 NumAllocated = 0;
5536 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005537 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005538
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005539 // Okay, this register is good, we can use it.
5540 ++NumAllocated;
5541
5542 // If we allocated enough consecutive registers, succeed.
5543 if (NumAllocated == NumRegs) {
5544 unsigned RegStart = (i-NumAllocated)+1;
5545 unsigned RegEnd = i+1;
5546 // Mark all of the allocated registers used.
5547 for (unsigned i = RegStart; i != RegEnd; ++i)
5548 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005549
5550 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005551 OpInfo.ConstraintVT);
5552 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5553 return;
5554 }
5555 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005556
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005557 // Otherwise, we couldn't allocate enough registers for this.
5558}
5559
Evan Chengda43bcf2008-09-24 00:05:32 +00005560/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5561/// processed uses a memory 'm' constraint.
5562static bool
5563hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005564 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005565 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5566 InlineAsm::ConstraintInfo &CI = CInfos[i];
5567 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5568 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5569 if (CType == TargetLowering::C_Memory)
5570 return true;
5571 }
Chris Lattner6c147292009-04-30 00:48:50 +00005572
5573 // Indirect operand accesses access memory.
5574 if (CI.isIndirect)
5575 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005576 }
5577
5578 return false;
5579}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005580
5581/// visitInlineAsm - Handle a call to an InlineAsm object.
5582///
Dan Gohman2048b852009-11-23 18:04:58 +00005583void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005584 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5585
5586 /// ConstraintOperands - Information about all of the constraints.
5587 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005588
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005589 std::set<unsigned> OutputRegs, InputRegs;
5590
5591 // Do a prepass over the constraints, canonicalizing them, and building up the
5592 // ConstraintOperands list.
5593 std::vector<InlineAsm::ConstraintInfo>
5594 ConstraintInfos = IA->ParseConstraints();
5595
Evan Chengda43bcf2008-09-24 00:05:32 +00005596 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005597
5598 SDValue Chain, Flag;
5599
5600 // We won't need to flush pending loads if this asm doesn't touch
5601 // memory and is nonvolatile.
5602 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005603 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005604 else
5605 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005606
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005607 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5608 unsigned ResNo = 0; // ResNo - The result number of the next output.
5609 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5610 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5611 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005612
Owen Anderson825b72b2009-08-11 20:47:22 +00005613 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005614
5615 // Compute the value type for each operand.
5616 switch (OpInfo.Type) {
5617 case InlineAsm::isOutput:
5618 // Indirect outputs just consume an argument.
5619 if (OpInfo.isIndirect) {
5620 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5621 break;
5622 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005624 // The return value of the call is this value. As such, there is no
5625 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005626 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5627 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005628 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5629 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5630 } else {
5631 assert(ResNo == 0 && "Asm only has one result!");
5632 OpVT = TLI.getValueType(CS.getType());
5633 }
5634 ++ResNo;
5635 break;
5636 case InlineAsm::isInput:
5637 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5638 break;
5639 case InlineAsm::isClobber:
5640 // Nothing to do.
5641 break;
5642 }
5643
5644 // If this is an input or an indirect output, process the call argument.
5645 // BasicBlocks are labels, currently appearing only in asm's.
5646 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005647 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005648 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5649
Chris Lattner81249c92008-10-17 17:05:25 +00005650 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005651 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005652 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005653 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005655
Owen Anderson1d0be152009-08-13 21:58:54 +00005656 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005657 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005658
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005659 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005660 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005661
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005662 // Second pass over the constraints: compute which constraint option to use
5663 // and assign registers to constraints that want a specific physreg.
5664 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5665 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005666
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005667 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005668 // matching input. If their types mismatch, e.g. one is an integer, the
5669 // other is floating point, or their sizes are different, flag it as an
5670 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005671 if (OpInfo.hasMatchingInput()) {
5672 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5673 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005674 if ((OpInfo.ConstraintVT.isInteger() !=
5675 Input.ConstraintVT.isInteger()) ||
5676 (OpInfo.ConstraintVT.getSizeInBits() !=
5677 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005678 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005679 " with a matching output constraint of incompatible"
5680 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005681 }
5682 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005683 }
5684 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005686 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005687 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005688
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005689 // If this is a memory input, and if the operand is not indirect, do what we
5690 // need to to provide an address for the memory input.
5691 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5692 !OpInfo.isIndirect) {
5693 assert(OpInfo.Type == InlineAsm::isInput &&
5694 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005695
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005696 // Memory operands really want the address of the value. If we don't have
5697 // an indirect input, put it in the constpool if we can, otherwise spill
5698 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005699
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005700 // If the operand is a float, integer, or vector constant, spill to a
5701 // constant pool entry to get its address.
5702 Value *OpVal = OpInfo.CallOperandVal;
5703 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5704 isa<ConstantVector>(OpVal)) {
5705 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5706 TLI.getPointerTy());
5707 } else {
5708 // Otherwise, create a stack slot and emit a store to it before the
5709 // asm.
5710 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005711 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005712 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5713 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005714 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005715 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005716 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005717 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005718 OpInfo.CallOperand = StackSlot;
5719 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005720
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005721 // There is no longer a Value* corresponding to this operand.
5722 OpInfo.CallOperandVal = 0;
5723 // It is now an indirect operand.
5724 OpInfo.isIndirect = true;
5725 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005726
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005727 // If this constraint is for a specific register, allocate it before
5728 // anything else.
5729 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005730 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005731 }
5732 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005733
5734
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005735 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005736 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005737 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5738 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005739
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005740 // C_Register operands have already been allocated, Other/Memory don't need
5741 // to be.
5742 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005743 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005744 }
5745
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005746 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5747 std::vector<SDValue> AsmNodeOperands;
5748 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5749 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005751
5752
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005753 // Loop over all of the inputs, copying the operand values into the
5754 // appropriate registers and processing the output regs.
5755 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005756
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005757 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5758 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005759
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005760 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5761 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5762
5763 switch (OpInfo.Type) {
5764 case InlineAsm::isOutput: {
5765 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5766 OpInfo.ConstraintType != TargetLowering::C_Register) {
5767 // Memory output, or 'other' output (e.g. 'X' constraint).
5768 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5769
5770 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005771 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5772 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005773 TLI.getPointerTy()));
5774 AsmNodeOperands.push_back(OpInfo.CallOperand);
5775 break;
5776 }
5777
5778 // Otherwise, this is a register or register class output.
5779
5780 // Copy the output from the appropriate register. Find a register that
5781 // we can use.
5782 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005783 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005784 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005785 }
5786
5787 // If this is an indirect operand, store through the pointer after the
5788 // asm.
5789 if (OpInfo.isIndirect) {
5790 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5791 OpInfo.CallOperandVal));
5792 } else {
5793 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005794 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5795 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005796 // Concatenate this output onto the outputs list.
5797 RetValRegs.append(OpInfo.AssignedRegs);
5798 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005799
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005800 // Add information to the INLINEASM node to know that this register is
5801 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005802 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5803 6 /* EARLYCLOBBER REGDEF */ :
5804 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005805 false,
5806 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005807 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808 break;
5809 }
5810 case InlineAsm::isInput: {
5811 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005812
Chris Lattner6bdcda32008-10-17 16:47:46 +00005813 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005814 // If this is required to match an output register we have already set,
5815 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005816 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005817
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005818 // Scan until we find the definition we already emitted of this operand.
5819 // When we find it, create a RegsForValue operand.
5820 unsigned CurOp = 2; // The first operand.
5821 for (; OperandNo; --OperandNo) {
5822 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005823 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005824 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005825 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5826 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5827 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005828 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005829 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005830 }
5831
Evan Cheng697cbbf2009-03-20 18:03:34 +00005832 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005833 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005834 if ((OpFlag & 7) == 2 /*REGDEF*/
5835 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5836 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005837 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005838 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005839 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005840 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005841 RegsForValue MatchedRegs;
5842 MatchedRegs.TLI = &TLI;
5843 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005844 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005845 MatchedRegs.RegVTs.push_back(RegVT);
5846 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005847 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005848 i != e; ++i)
5849 MatchedRegs.Regs.
5850 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005851
5852 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005853 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5854 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005855 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5856 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005857 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005858 break;
5859 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005860 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5861 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5862 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005863 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005864 // See InlineAsm.h isUseOperandTiedToDef.
5865 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005866 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005867 TLI.getPointerTy()));
5868 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5869 break;
5870 }
5871 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005873 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005874 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005875 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005877 std::vector<SDValue> Ops;
5878 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005879 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005880 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005881 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005882 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005883 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005885 // Add information to the INLINEASM node to know about this input.
5886 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005887 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005888 TLI.getPointerTy()));
5889 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5890 break;
5891 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5892 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5893 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5894 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005895
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005896 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005897 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5898 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005899 TLI.getPointerTy()));
5900 AsmNodeOperands.push_back(InOperandVal);
5901 break;
5902 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005903
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005904 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5905 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5906 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005907 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005908 "Don't know how to handle indirect register inputs yet!");
5909
5910 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005911 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005912 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005913 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005914 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005915
Dale Johannesen66978ee2009-01-31 02:22:37 +00005916 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5917 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005918
Evan Cheng697cbbf2009-03-20 18:03:34 +00005919 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005920 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005921 break;
5922 }
5923 case InlineAsm::isClobber: {
5924 // Add the clobbered value to the operand list, so that the register
5925 // allocator is aware that the physreg got clobbered.
5926 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005927 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005928 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005929 break;
5930 }
5931 }
5932 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934 // Finish up input operands.
5935 AsmNodeOperands[0] = Chain;
5936 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005937
Dale Johannesen66978ee2009-01-31 02:22:37 +00005938 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005939 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005940 &AsmNodeOperands[0], AsmNodeOperands.size());
5941 Flag = Chain.getValue(1);
5942
5943 // If this asm returns a register value, copy the result from that register
5944 // and set it as the value of the call.
5945 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005946 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005947 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005948
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005949 // FIXME: Why don't we do this for inline asms with MRVs?
5950 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005951 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005952
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005953 // If any of the results of the inline asm is a vector, it may have the
5954 // wrong width/num elts. This can happen for register classes that can
5955 // contain multiple different value types. The preg or vreg allocated may
5956 // not have the same VT as was expected. Convert it to the right type
5957 // with bit_convert.
5958 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005959 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005960 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005961
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005962 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005963 ResultType.isInteger() && Val.getValueType().isInteger()) {
5964 // If a result value was tied to an input value, the computed result may
5965 // have a wider width than the expected result. Extract the relevant
5966 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005967 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005968 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005969
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005970 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005971 }
Dan Gohman95915732008-10-18 01:03:45 +00005972
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005973 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005974 // Don't need to use this as a chain in this case.
5975 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5976 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005977 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005978
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005979 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005980
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005981 // Process indirect outputs, first output all of the flagged copies out of
5982 // physregs.
5983 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5984 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5985 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005986 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5987 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005988 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005990 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005992 // Emit the non-flagged stores from the physregs.
5993 SmallVector<SDValue, 8> OutChains;
5994 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005995 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005996 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005997 getValue(StoresToEmit[i].second),
5998 StoresToEmit[i].second, 0));
5999 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006000 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006001 &OutChains[0], OutChains.size());
6002 DAG.setRoot(Chain);
6003}
6004
Dan Gohman2048b852009-11-23 18:04:58 +00006005void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006006 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006007 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006008 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006009 DAG.getSrcValue(I.getOperand(1))));
6010}
6011
Dan Gohman2048b852009-11-23 18:04:58 +00006012void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006013 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6014 getRoot(), getValue(I.getOperand(0)),
6015 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006016 setValue(&I, V);
6017 DAG.setRoot(V.getValue(1));
6018}
6019
Dan Gohman2048b852009-11-23 18:04:58 +00006020void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006021 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006022 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006023 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006024 DAG.getSrcValue(I.getOperand(1))));
6025}
6026
Dan Gohman2048b852009-11-23 18:04:58 +00006027void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006028 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006029 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006030 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006031 getValue(I.getOperand(2)),
6032 DAG.getSrcValue(I.getOperand(1)),
6033 DAG.getSrcValue(I.getOperand(2))));
6034}
6035
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006036/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006037/// implementation, which just calls LowerCall.
6038/// FIXME: When all targets are
6039/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006040std::pair<SDValue, SDValue>
6041TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6042 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006043 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006044 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006045 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006046 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00006047 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006048
Dan Gohman1937e2f2008-09-16 01:42:28 +00006049 assert((!isTailCall || PerformTailCallOpt) &&
6050 "isTailCall set when tail-call optimizations are disabled!");
6051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006052 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006053 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006054 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006055 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006056 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6057 for (unsigned Value = 0, NumValues = ValueVTs.size();
6058 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006059 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006060 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006061 SDValue Op = SDValue(Args[i].Node.getNode(),
6062 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006063 ISD::ArgFlagsTy Flags;
6064 unsigned OriginalAlignment =
6065 getTargetData()->getABITypeAlignment(ArgTy);
6066
6067 if (Args[i].isZExt)
6068 Flags.setZExt();
6069 if (Args[i].isSExt)
6070 Flags.setSExt();
6071 if (Args[i].isInReg)
6072 Flags.setInReg();
6073 if (Args[i].isSRet)
6074 Flags.setSRet();
6075 if (Args[i].isByVal) {
6076 Flags.setByVal();
6077 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6078 const Type *ElementTy = Ty->getElementType();
6079 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006080 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081 // For ByVal, alignment should come from FE. BE will guess if this
6082 // info is not there but there are cases it cannot get right.
6083 if (Args[i].Alignment)
6084 FrameAlign = Args[i].Alignment;
6085 Flags.setByValAlign(FrameAlign);
6086 Flags.setByValSize(FrameSize);
6087 }
6088 if (Args[i].isNest)
6089 Flags.setNest();
6090 Flags.setOrigAlign(OriginalAlignment);
6091
Owen Anderson23b9b192009-08-12 00:36:31 +00006092 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6093 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006094 SmallVector<SDValue, 4> Parts(NumParts);
6095 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6096
6097 if (Args[i].isSExt)
6098 ExtendKind = ISD::SIGN_EXTEND;
6099 else if (Args[i].isZExt)
6100 ExtendKind = ISD::ZERO_EXTEND;
6101
Dale Johannesen66978ee2009-01-31 02:22:37 +00006102 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006103
Dan Gohman98ca4f22009-08-05 01:29:28 +00006104 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006105 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006106 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6107 if (NumParts > 1 && j == 0)
6108 MyFlags.Flags.setSplit();
6109 else if (j != 0)
6110 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006111
Dan Gohman98ca4f22009-08-05 01:29:28 +00006112 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006113 }
6114 }
6115 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006116
Dan Gohman98ca4f22009-08-05 01:29:28 +00006117 // Handle the incoming return values from the call.
6118 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006119 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006120 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006121 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006122 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006123 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6124 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006125 for (unsigned i = 0; i != NumRegs; ++i) {
6126 ISD::InputArg MyFlags;
6127 MyFlags.VT = RegisterVT;
6128 MyFlags.Used = isReturnValueUsed;
6129 if (RetSExt)
6130 MyFlags.Flags.setSExt();
6131 if (RetZExt)
6132 MyFlags.Flags.setZExt();
6133 if (isInreg)
6134 MyFlags.Flags.setInReg();
6135 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006136 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006137 }
6138
Dan Gohman98ca4f22009-08-05 01:29:28 +00006139 // Check if target-dependent constraints permit a tail call here.
6140 // Target-independent constraints should be checked by the caller.
6141 if (isTailCall &&
6142 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6143 isTailCall = false;
6144
6145 SmallVector<SDValue, 4> InVals;
6146 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6147 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006148
6149 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006150 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006151 "LowerCall didn't return a valid chain!");
6152 assert((!isTailCall || InVals.empty()) &&
6153 "LowerCall emitted a return value for a tail call!");
6154 assert((isTailCall || InVals.size() == Ins.size()) &&
6155 "LowerCall didn't emit the correct number of values!");
6156 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6157 assert(InVals[i].getNode() &&
6158 "LowerCall emitted a null value!");
6159 assert(Ins[i].VT == InVals[i].getValueType() &&
6160 "LowerCall emitted a value with the wrong type!");
6161 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006162
6163 // For a tail call, the return value is merely live-out and there aren't
6164 // any nodes in the DAG representing it. Return a special value to
6165 // indicate that a tail call has been emitted and no more Instructions
6166 // should be processed in the current block.
6167 if (isTailCall) {
6168 DAG.setRoot(Chain);
6169 return std::make_pair(SDValue(), SDValue());
6170 }
6171
6172 // Collect the legal value parts into potentially illegal values
6173 // that correspond to the original function's return values.
6174 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6175 if (RetSExt)
6176 AssertOp = ISD::AssertSext;
6177 else if (RetZExt)
6178 AssertOp = ISD::AssertZext;
6179 SmallVector<SDValue, 4> ReturnValues;
6180 unsigned CurReg = 0;
6181 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006182 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006183 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6184 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006185
6186 SDValue ReturnValue =
6187 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
6188 AssertOp);
6189 ReturnValues.push_back(ReturnValue);
6190 CurReg += NumRegs;
6191 }
6192
6193 // For a function returning void, there is no return value. We can't create
6194 // such a node, so we just return a null return value in that case. In
6195 // that case, nothing will actualy look at the value.
6196 if (ReturnValues.empty())
6197 return std::make_pair(SDValue(), Chain);
6198
6199 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6200 DAG.getVTList(&RetTys[0], RetTys.size()),
6201 &ReturnValues[0], ReturnValues.size());
6202
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006203 return std::make_pair(Res, Chain);
6204}
6205
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006206void TargetLowering::LowerOperationWrapper(SDNode *N,
6207 SmallVectorImpl<SDValue> &Results,
6208 SelectionDAG &DAG) {
6209 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006210 if (Res.getNode())
6211 Results.push_back(Res);
6212}
6213
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006214SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006215 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006216 return SDValue();
6217}
6218
6219
Dan Gohman2048b852009-11-23 18:04:58 +00006220void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006221 SDValue Op = getValue(V);
6222 assert((Op.getOpcode() != ISD::CopyFromReg ||
6223 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6224 "Copy from a reg to the same reg!");
6225 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6226
Owen Anderson23b9b192009-08-12 00:36:31 +00006227 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006228 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00006229 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006230 PendingExports.push_back(Chain);
6231}
6232
6233#include "llvm/CodeGen/SelectionDAGISel.h"
6234
Dan Gohman8c2b5252009-10-30 01:27:03 +00006235void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006236 // If this is the entry block, emit arguments.
6237 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006238 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006239 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006240 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006241 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006242 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006243
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006244 // Check whether the function can return without sret-demotion.
6245 SmallVector<EVT, 4> OutVTs;
6246 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006247 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
6248 OutVTs, OutsFlags, TLI);
6249 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6250
6251 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
6252 OutVTs, OutsFlags, DAG);
6253 if (!FLI.CanLowerReturn) {
6254 // Put in an sret pointer parameter before all the other parameters.
6255 SmallVector<EVT, 1> ValueVTs;
6256 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6257
6258 // NOTE: Assuming that a pointer will never break down to more than one VT
6259 // or one register.
6260 ISD::ArgFlagsTy Flags;
6261 Flags.setSRet();
6262 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6263 ISD::InputArg RetArg(Flags, RegisterVT, true);
6264 Ins.push_back(RetArg);
6265 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006266
Dan Gohman98ca4f22009-08-05 01:29:28 +00006267 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006268 unsigned Idx = 1;
6269 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6270 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006271 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006272 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6273 bool isArgValueUsed = !I->use_empty();
6274 for (unsigned Value = 0, NumValues = ValueVTs.size();
6275 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006276 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006277 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006278 ISD::ArgFlagsTy Flags;
6279 unsigned OriginalAlignment =
6280 TD->getABITypeAlignment(ArgTy);
6281
6282 if (F.paramHasAttr(Idx, Attribute::ZExt))
6283 Flags.setZExt();
6284 if (F.paramHasAttr(Idx, Attribute::SExt))
6285 Flags.setSExt();
6286 if (F.paramHasAttr(Idx, Attribute::InReg))
6287 Flags.setInReg();
6288 if (F.paramHasAttr(Idx, Attribute::StructRet))
6289 Flags.setSRet();
6290 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6291 Flags.setByVal();
6292 const PointerType *Ty = cast<PointerType>(I->getType());
6293 const Type *ElementTy = Ty->getElementType();
6294 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6295 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6296 // For ByVal, alignment should be passed from FE. BE will guess if
6297 // this info is not there but there are cases it cannot get right.
6298 if (F.getParamAlignment(Idx))
6299 FrameAlign = F.getParamAlignment(Idx);
6300 Flags.setByValAlign(FrameAlign);
6301 Flags.setByValSize(FrameSize);
6302 }
6303 if (F.paramHasAttr(Idx, Attribute::Nest))
6304 Flags.setNest();
6305 Flags.setOrigAlign(OriginalAlignment);
6306
Owen Anderson23b9b192009-08-12 00:36:31 +00006307 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6308 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006309 for (unsigned i = 0; i != NumRegs; ++i) {
6310 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6311 if (NumRegs > 1 && i == 0)
6312 MyFlags.Flags.setSplit();
6313 // if it isn't first piece, alignment must be 1
6314 else if (i > 0)
6315 MyFlags.Flags.setOrigAlign(1);
6316 Ins.push_back(MyFlags);
6317 }
6318 }
6319 }
6320
6321 // Call the target to set up the argument values.
6322 SmallVector<SDValue, 8> InVals;
6323 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6324 F.isVarArg(), Ins,
6325 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006326
6327 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006328 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006329 "LowerFormalArguments didn't return a valid chain!");
6330 assert(InVals.size() == Ins.size() &&
6331 "LowerFormalArguments didn't emit the correct number of values!");
6332 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6333 assert(InVals[i].getNode() &&
6334 "LowerFormalArguments emitted a null value!");
6335 assert(Ins[i].VT == InVals[i].getValueType() &&
6336 "LowerFormalArguments emitted a value with the wrong type!");
6337 });
6338
6339 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006340 DAG.setRoot(NewRoot);
6341
6342 // Set up the argument values.
6343 unsigned i = 0;
6344 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006345 if (!FLI.CanLowerReturn) {
6346 // Create a virtual register for the sret pointer, and put in a copy
6347 // from the sret argument into it.
6348 SmallVector<EVT, 1> ValueVTs;
6349 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6350 EVT VT = ValueVTs[0];
6351 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6352 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6353 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT,
6354 VT, AssertOp);
6355
Dan Gohman2048b852009-11-23 18:04:58 +00006356 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006357 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6358 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6359 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00006360 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006361 DAG.setRoot(NewRoot);
6362
6363 // i indexes lowered arguments. Bump it past the hidden sret argument.
6364 // Idx indexes LLVM arguments. Don't touch it.
6365 ++i;
6366 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00006367 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6368 ++I, ++Idx) {
6369 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006370 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006371 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006372 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006373 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006374 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006375 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6376 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006377
6378 if (!I->use_empty()) {
6379 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6380 if (F.paramHasAttr(Idx, Attribute::SExt))
6381 AssertOp = ISD::AssertSext;
6382 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6383 AssertOp = ISD::AssertZext;
6384
6385 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
6386 PartVT, VT, AssertOp));
6387 }
6388 i += NumParts;
6389 }
6390 if (!I->use_empty()) {
Dan Gohman2048b852009-11-23 18:04:58 +00006391 SDB->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
6392 SDB->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006393 // If this argument is live outside of the entry block, insert a copy from
6394 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006395 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006396 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006397 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00006398 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006399
6400 // Finally, if the target has anything special to do, allow it to do so.
6401 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006402 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006403}
6404
6405/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6406/// ensure constants are generated when needed. Remember the virtual registers
6407/// that need to be added to the Machine PHI nodes as input. We cannot just
6408/// directly add them, because expansion might result in multiple MBB's for one
6409/// BB. As such, the start of the BB might correspond to a different MBB than
6410/// the end.
6411///
6412void
6413SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6414 TerminatorInst *TI = LLVMBB->getTerminator();
6415
6416 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6417
6418 // Check successor nodes' PHI nodes that expect a constant to be available
6419 // from this block.
6420 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6421 BasicBlock *SuccBB = TI->getSuccessor(succ);
6422 if (!isa<PHINode>(SuccBB->begin())) continue;
6423 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006425 // If this terminator has multiple identical successors (common for
6426 // switches), only handle each succ once.
6427 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006428
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006429 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6430 PHINode *PN;
6431
6432 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6433 // nodes and Machine PHI nodes, but the incoming operands have not been
6434 // emitted yet.
6435 for (BasicBlock::iterator I = SuccBB->begin();
6436 (PN = dyn_cast<PHINode>(I)); ++I) {
6437 // Ignore dead phi's.
6438 if (PN->use_empty()) continue;
6439
6440 unsigned Reg;
6441 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6442
6443 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006444 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006445 if (RegOut == 0) {
6446 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006447 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006448 }
6449 Reg = RegOut;
6450 } else {
6451 Reg = FuncInfo->ValueMap[PHIOp];
6452 if (Reg == 0) {
6453 assert(isa<AllocaInst>(PHIOp) &&
6454 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6455 "Didn't codegen value into a register!??");
6456 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006457 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006458 }
6459 }
6460
6461 // Remember that this register needs to added to the machine PHI node as
6462 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006463 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006464 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6465 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006466 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006467 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006468 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006469 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006470 Reg += NumRegisters;
6471 }
6472 }
6473 }
Dan Gohman2048b852009-11-23 18:04:58 +00006474 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006475}
6476
Dan Gohman3df24e62008-09-03 23:12:08 +00006477/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6478/// supports legal types, and it emits MachineInstrs directly instead of
6479/// creating SelectionDAG nodes.
6480///
6481bool
6482SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6483 FastISel *F) {
6484 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006485
Dan Gohman3df24e62008-09-03 23:12:08 +00006486 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006487 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006488
6489 // Check successor nodes' PHI nodes that expect a constant to be available
6490 // from this block.
6491 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6492 BasicBlock *SuccBB = TI->getSuccessor(succ);
6493 if (!isa<PHINode>(SuccBB->begin())) continue;
6494 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006495
Dan Gohman3df24e62008-09-03 23:12:08 +00006496 // If this terminator has multiple identical successors (common for
6497 // switches), only handle each succ once.
6498 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006499
Dan Gohman3df24e62008-09-03 23:12:08 +00006500 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6501 PHINode *PN;
6502
6503 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6504 // nodes and Machine PHI nodes, but the incoming operands have not been
6505 // emitted yet.
6506 for (BasicBlock::iterator I = SuccBB->begin();
6507 (PN = dyn_cast<PHINode>(I)); ++I) {
6508 // Ignore dead phi's.
6509 if (PN->use_empty()) continue;
6510
6511 // Only handle legal types. Two interesting things to note here. First,
6512 // by bailing out early, we may leave behind some dead instructions,
6513 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6514 // own moves. Second, this check is necessary becuase FastISel doesn't
6515 // use CreateRegForValue to create registers, so it always creates
6516 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006517 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006518 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6519 // Promote MVT::i1.
6520 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006521 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006522 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006523 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006524 return false;
6525 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006526 }
6527
6528 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6529
6530 unsigned Reg = F->getRegForValue(PHIOp);
6531 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006532 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006533 return false;
6534 }
Dan Gohman2048b852009-11-23 18:04:58 +00006535 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006536 }
6537 }
6538
6539 return true;
6540}