blob: cd8d74019b235d6f44d51070b33e40ee6b139df6 [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.
Bill Wendlingec72e322009-12-22 01:11:43 +0000147 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, unsigned Order,
148 SDValue &Chain, SDValue *Flag) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000149
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,
Bill Wendlingec72e322009-12-22 01:11:43 +0000155 unsigned Order, 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,
Bill Wendling651ad132009-12-22 01:25:10 +0000162 SelectionDAG &DAG, unsigned Order,
163 std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000164 };
165}
166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000167/// getCopyFromParts - Create a value that contains the specified legal parts
168/// combined into the value they represent. If the parts combine to a type
169/// larger then ValueVT then AssertOp can be used to specify whether the extra
170/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
171/// (ISD::AssertSext).
Dale Johannesen66978ee2009-01-31 02:22:37 +0000172static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
173 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000174 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000175 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000176 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000177 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000178 SDValue Val = Parts[0];
179
180 if (NumParts > 1) {
181 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000182 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000183 unsigned PartBits = PartVT.getSizeInBits();
184 unsigned ValueBits = ValueVT.getSizeInBits();
185
186 // Assemble the power of 2 part.
187 unsigned RoundParts = NumParts & (NumParts - 1) ?
188 1 << Log2_32(NumParts) : NumParts;
189 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000190 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000191 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000192 SDValue Lo, Hi;
193
Owen Anderson23b9b192009-08-12 00:36:31 +0000194 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000196 if (RoundParts > 2) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000197 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT);
198 Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000199 PartVT, HalfVT);
200 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000201 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
202 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000203 }
204 if (TLI.isBigEndian())
205 std::swap(Lo, Hi);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000206 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000207
208 if (RoundParts < NumParts) {
209 // Assemble the trailing non-power-of-2 part.
210 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000211 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Scott Michelfdc40a02009-02-17 22:15:04 +0000212 Hi = getCopyFromParts(DAG, dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000213 Parts+RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000214
215 // Combine the round and odd parts.
216 Lo = Val;
217 if (TLI.isBigEndian())
218 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000219 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000220 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
221 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000222 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000223 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000224 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
225 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000226 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000227 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000228 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000229 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000230 unsigned NumIntermediates;
231 unsigned NumRegs =
Owen Anderson23b9b192009-08-12 00:36:31 +0000232 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
233 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000234 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
235 NumParts = NumRegs; // Silence a compiler warning.
236 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
237 assert(RegisterVT == Parts[0].getValueType() &&
238 "Part type doesn't match part!");
239
240 // Assemble the parts into intermediate operands.
241 SmallVector<SDValue, 8> Ops(NumIntermediates);
242 if (NumIntermediates == NumParts) {
243 // If the register was not expanded, truncate or copy the value,
244 // as appropriate.
245 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000246 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000247 PartVT, IntermediateVT);
248 } else if (NumParts > 0) {
249 // If the intermediate type was expanded, build the intermediate operands
250 // from the parts.
251 assert(NumParts % NumIntermediates == 0 &&
252 "Must expand into a divisible number of parts!");
253 unsigned Factor = NumParts / NumIntermediates;
254 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000255 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000256 PartVT, IntermediateVT);
257 }
258
259 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
260 // operands.
261 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000262 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000263 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000264 } else if (PartVT.isFloatingPoint()) {
265 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000266 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000267 "Unexpected split");
268 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000269 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
270 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000271 if (TLI.isBigEndian())
272 std::swap(Lo, Hi);
273 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
274 } else {
275 // FP split into integer parts (soft fp)
276 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
277 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000278 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Eli Friedman2ac8b322009-05-20 06:02:09 +0000279 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000280 }
281 }
282
283 // There is now one part, held in Val. Correct it to match ValueVT.
284 PartVT = Val.getValueType();
285
286 if (PartVT == ValueVT)
287 return Val;
288
289 if (PartVT.isVector()) {
290 assert(ValueVT.isVector() && "Unknown vector conversion!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000291 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000292 }
293
294 if (ValueVT.isVector()) {
295 assert(ValueVT.getVectorElementType() == PartVT &&
296 ValueVT.getVectorNumElements() == 1 &&
297 "Only trivial scalar-to-vector conversions should get here!");
Evan Chenga87008d2009-02-25 22:49:59 +0000298 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000299 }
300
301 if (PartVT.isInteger() &&
302 ValueVT.isInteger()) {
303 if (ValueVT.bitsLT(PartVT)) {
304 // For a truncate, see if we have any information to
305 // indicate whether the truncated bits will always be
306 // zero or sign-extension.
307 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000308 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000309 DAG.getValueType(ValueVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000310 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000311 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000312 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000313 }
314 }
315
316 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
317 if (ValueVT.bitsLT(Val.getValueType()))
318 // FP_ROUND's are always exact here.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000319 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000320 DAG.getIntPtrConstant(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000321 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000322 }
323
324 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000325 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000326
Torok Edwinc23197a2009-07-14 16:55:14 +0000327 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000328 return SDValue();
329}
330
331/// getCopyToParts - Create a series of nodes that contain the specified value
332/// split into legal parts. If the parts contain more bits than Val, then, for
333/// integers, ExtendKind can be used to specify how to generate the extra bits.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000334static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val,
Owen Andersone50ed302009-08-10 22:56:29 +0000335 SDValue *Parts, unsigned NumParts, EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000336 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000337 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000338 EVT PtrVT = TLI.getPointerTy();
339 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000340 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000341 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000342 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
343
344 if (!NumParts)
345 return;
346
347 if (!ValueVT.isVector()) {
348 if (PartVT == ValueVT) {
349 assert(NumParts == 1 && "No-op copy with multiple parts!");
350 Parts[0] = Val;
351 return;
352 }
353
354 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
355 // If the parts cover more bits than the value has, promote the value.
356 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
357 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000358 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000359 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000360 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000361 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000362 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000363 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000364 }
365 } else if (PartBits == ValueVT.getSizeInBits()) {
366 // Different types of the same size.
367 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000368 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000369 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
370 // If the parts cover less bits than value has, truncate the value.
371 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000372 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000373 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000374 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000375 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000376 }
377 }
378
379 // The value may have changed - recompute ValueVT.
380 ValueVT = Val.getValueType();
381 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
382 "Failed to tile the value with PartVT!");
383
384 if (NumParts == 1) {
385 assert(PartVT == ValueVT && "Type conversion failed!");
386 Parts[0] = Val;
387 return;
388 }
389
390 // Expand the value into multiple parts.
391 if (NumParts & (NumParts - 1)) {
392 // The number of parts is not a power of 2. Split off and copy the tail.
393 assert(PartVT.isInteger() && ValueVT.isInteger() &&
394 "Do not know what to expand to!");
395 unsigned RoundParts = 1 << Log2_32(NumParts);
396 unsigned RoundBits = RoundParts * PartBits;
397 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000398 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000399 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000400 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000401 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000402 if (TLI.isBigEndian())
403 // The odd parts were reversed by getCopyToParts - unreverse them.
404 std::reverse(Parts + RoundParts, Parts + NumParts);
405 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000406 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000407 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000408 }
409
410 // The number of parts is a power of 2. Repeatedly bisect the value using
411 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000412 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson23b9b192009-08-12 00:36:31 +0000413 EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000414 Val);
415 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
416 for (unsigned i = 0; i < NumParts; i += StepSize) {
417 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000418 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000419 SDValue &Part0 = Parts[i];
420 SDValue &Part1 = Parts[i+StepSize/2];
421
Scott Michelfdc40a02009-02-17 22:15:04 +0000422 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000423 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000424 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000425 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000426 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000427 DAG.getConstant(0, PtrVT));
428
429 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000430 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000431 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000432 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000433 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000434 }
435 }
436 }
437
438 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000439 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000440
441 return;
442 }
443
444 // Vector ValueVT.
445 if (NumParts == 1) {
446 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000447 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000448 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000449 } else {
450 assert(ValueVT.getVectorElementType() == PartVT &&
451 ValueVT.getVectorNumElements() == 1 &&
452 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000453 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000454 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000455 DAG.getConstant(0, PtrVT));
456 }
457 }
458
459 Parts[0] = Val;
460 return;
461 }
462
463 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000464 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000465 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000466 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
467 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000468 unsigned NumElements = ValueVT.getVectorNumElements();
469
470 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
471 NumParts = NumRegs; // Silence a compiler warning.
472 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
473
474 // Split the vector into intermediate operands.
475 SmallVector<SDValue, 8> Ops(NumIntermediates);
476 for (unsigned i = 0; i != NumIntermediates; ++i)
477 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000478 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000479 IntermediateVT, Val,
480 DAG.getConstant(i * (NumElements / NumIntermediates),
481 PtrVT));
482 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000483 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000484 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000485 DAG.getConstant(i, PtrVT));
486
487 // Split the intermediate operands into legal parts.
488 if (NumParts == NumIntermediates) {
489 // If the register was not expanded, promote or copy the value,
490 // as appropriate.
491 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000492 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000493 } else if (NumParts > 0) {
494 // If the intermediate type was expanded, split each the value into
495 // legal parts.
496 assert(NumParts % NumIntermediates == 0 &&
497 "Must expand into a divisible number of parts!");
498 unsigned Factor = NumParts / NumIntermediates;
499 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000500 getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000501 }
502}
503
504
Dan Gohman2048b852009-11-23 18:04:58 +0000505void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000506 AA = &aa;
507 GFI = gfi;
508 TD = DAG.getTarget().getTargetData();
509}
510
511/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000512/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000513/// for a new block. This doesn't clear out information about
514/// additional blocks that are needed to complete switch lowering
515/// or PHI node updating; that information is cleared out as it is
516/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000517void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000518 NodeMap.clear();
519 PendingLoads.clear();
520 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000521 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000522 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000523 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000524 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000525}
526
527/// getRoot - Return the current virtual root of the Selection DAG,
528/// flushing any PendingLoad items. This must be done before emitting
529/// a store or any other node that may need to be ordered after any
530/// prior load instructions.
531///
Dan Gohman2048b852009-11-23 18:04:58 +0000532SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000533 if (PendingLoads.empty())
534 return DAG.getRoot();
535
536 if (PendingLoads.size() == 1) {
537 SDValue Root = PendingLoads[0];
538 DAG.setRoot(Root);
539 PendingLoads.clear();
540 return Root;
541 }
542
543 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000544 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000545 &PendingLoads[0], PendingLoads.size());
546 PendingLoads.clear();
547 DAG.setRoot(Root);
548 return Root;
549}
550
551/// getControlRoot - Similar to getRoot, but instead of flushing all the
552/// PendingLoad items, flush all the PendingExports items. It is necessary
553/// to do this before emitting a terminator instruction.
554///
Dan Gohman2048b852009-11-23 18:04:58 +0000555SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000556 SDValue Root = DAG.getRoot();
557
558 if (PendingExports.empty())
559 return Root;
560
561 // Turn all of the CopyToReg chains into one factored node.
562 if (Root.getOpcode() != ISD::EntryToken) {
563 unsigned i = 0, e = PendingExports.size();
564 for (; i != e; ++i) {
565 assert(PendingExports[i].getNode()->getNumOperands() > 1);
566 if (PendingExports[i].getNode()->getOperand(0) == Root)
567 break; // Don't add the root if we already indirectly depend on it.
568 }
569
570 if (i == e)
571 PendingExports.push_back(Root);
572 }
573
Owen Anderson825b72b2009-08-11 20:47:22 +0000574 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000575 &PendingExports[0],
576 PendingExports.size());
577 PendingExports.clear();
578 DAG.setRoot(Root);
579 return Root;
580}
581
Dan Gohman2048b852009-11-23 18:04:58 +0000582void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000583 visit(I.getOpcode(), I);
584}
585
Dan Gohman2048b852009-11-23 18:04:58 +0000586void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000587 // We're processing a new instruction.
588 ++SDNodeOrder;
589
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000590 // Note: this doesn't use InstVisitor, because it has to work with
591 // ConstantExpr's in addition to instructions.
592 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000593 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000594 // Build the switch statement using the Instruction.def file.
595#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000596 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000597#include "llvm/Instruction.def"
598 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000599}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000600
Dan Gohman2048b852009-11-23 18:04:58 +0000601SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000602 SDValue &N = NodeMap[V];
603 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000604
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000605 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000606 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000607
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000608 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000609 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000610
611 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
612 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000613
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000614 if (isa<ConstantPointerNull>(C))
615 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000616
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000617 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000618 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000619
Nate Begeman9008ca62009-04-27 18:41:29 +0000620 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000621 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000622
623 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
624 visit(CE->getOpcode(), *CE);
625 SDValue N1 = NodeMap[V];
626 assert(N1.getNode() && "visit didn't populate the ValueMap!");
627 return N1;
628 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000629
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000630 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
631 SmallVector<SDValue, 4> Constants;
632 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
633 OI != OE; ++OI) {
634 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000635 // If the operand is an empty aggregate, there are no values.
636 if (!Val) continue;
637 // Add each leaf value from the operand to the Constants list
638 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000639 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
640 Constants.push_back(SDValue(Val, i));
641 }
Bill Wendling87710f02009-12-21 23:47:40 +0000642
643 SDValue Res = DAG.getMergeValues(&Constants[0], Constants.size(),
644 getCurDebugLoc());
645 if (DisableScheduling)
646 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
647 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000648 }
649
650 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
651 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
652 "Unknown struct or array constant!");
653
Owen Andersone50ed302009-08-10 22:56:29 +0000654 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000655 ComputeValueVTs(TLI, C->getType(), ValueVTs);
656 unsigned NumElts = ValueVTs.size();
657 if (NumElts == 0)
658 return SDValue(); // empty struct
659 SmallVector<SDValue, 4> Constants(NumElts);
660 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000661 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000662 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000663 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000664 else if (EltVT.isFloatingPoint())
665 Constants[i] = DAG.getConstantFP(0, EltVT);
666 else
667 Constants[i] = DAG.getConstant(0, EltVT);
668 }
Bill Wendling87710f02009-12-21 23:47:40 +0000669
670 SDValue Res = DAG.getMergeValues(&Constants[0], NumElts,
671 getCurDebugLoc());
672 if (DisableScheduling)
673 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
674 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000675 }
676
Dan Gohman8c2b5252009-10-30 01:27:03 +0000677 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000678 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000679
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000680 const VectorType *VecTy = cast<VectorType>(V->getType());
681 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000682
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000683 // Now that we know the number and type of the elements, get that number of
684 // elements into the Ops array based on what kind of constant it is.
685 SmallVector<SDValue, 16> Ops;
686 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
687 for (unsigned i = 0; i != NumElements; ++i)
688 Ops.push_back(getValue(CP->getOperand(i)));
689 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000690 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000691 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000692
693 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000694 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000695 Op = DAG.getConstantFP(0, EltVT);
696 else
697 Op = DAG.getConstant(0, EltVT);
698 Ops.assign(NumElements, Op);
699 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000700
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000701 // Create a BUILD_VECTOR node.
Bill Wendling87710f02009-12-21 23:47:40 +0000702 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
703 VT, &Ops[0], Ops.size());
704 if (DisableScheduling)
705 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
706
707 return NodeMap[V] = Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000708 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000709
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000710 // If this is a static alloca, generate it as the frameindex instead of
711 // computation.
712 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
713 DenseMap<const AllocaInst*, int>::iterator SI =
714 FuncInfo.StaticAllocaMap.find(AI);
715 if (SI != FuncInfo.StaticAllocaMap.end())
716 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
717 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000718
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000719 unsigned InReg = FuncInfo.ValueMap[V];
720 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000721
Owen Anderson23b9b192009-08-12 00:36:31 +0000722 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000723 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +0000724 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(),
725 SDNodeOrder, Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000726}
727
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000728/// Get the EVTs and ArgFlags collections that represent the return type
729/// of the given function. This does not require a DAG or a return value, and
730/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000731static void getReturnInfo(const Type* ReturnType,
732 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000733 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000734 TargetLowering &TLI,
735 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000736 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000737 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000738 unsigned NumValues = ValueVTs.size();
739 if ( NumValues == 0 ) return;
740
741 for (unsigned j = 0, f = NumValues; j != f; ++j) {
742 EVT VT = ValueVTs[j];
743 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000744
745 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000746 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000747 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000748 ExtendKind = ISD::ZERO_EXTEND;
749
750 // FIXME: C calling convention requires the return type to be promoted to
751 // at least 32-bit. But this is not necessary for non-C calling
752 // conventions. The frontend should mark functions whose return values
753 // require promoting with signext or zeroext attributes.
754 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000755 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000756 if (VT.bitsLT(MinVT))
757 VT = MinVT;
758 }
759
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000760 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
761 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000762 // 'inreg' on function refers to return value
763 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000764 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000765 Flags.setInReg();
766
767 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000768 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000769 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000770 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000771 Flags.setZExt();
772
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000773 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000774 OutVTs.push_back(PartVT);
775 OutFlags.push_back(Flags);
776 }
777 }
778}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000779
Dan Gohman2048b852009-11-23 18:04:58 +0000780void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000781 SDValue Chain = getControlRoot();
782 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000783 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
784
785 if (!FLI.CanLowerReturn) {
786 unsigned DemoteReg = FLI.DemoteRegister;
787 const Function *F = I.getParent()->getParent();
788
789 // Emit a store of the return value through the virtual register.
790 // Leave Outs empty so that LowerReturn won't try to load return
791 // registers the usual way.
792 SmallVector<EVT, 1> PtrValueVTs;
793 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
794 PtrValueVTs);
795
796 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
797 SDValue RetOp = getValue(I.getOperand(0));
798
Owen Andersone50ed302009-08-10 22:56:29 +0000799 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000800 SmallVector<uint64_t, 4> Offsets;
801 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000802 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000803
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000804 SmallVector<SDValue, 4> Chains(NumValues);
805 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000806 for (unsigned i = 0; i != NumValues; ++i) {
807 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
808 DAG.getConstant(Offsets[i], PtrVT));
809 Chains[i] =
810 DAG.getStore(Chain, getCurDebugLoc(),
811 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
812 Add, NULL, Offsets[i], false, 0);
813
814 if (DisableScheduling) {
815 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
816 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
817 }
818 }
819
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000820 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
821 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000822
823 if (DisableScheduling)
824 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
825 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000826 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
827 SmallVector<EVT, 4> ValueVTs;
828 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
829 unsigned NumValues = ValueVTs.size();
830 if (NumValues == 0) continue;
831
832 SDValue RetOp = getValue(I.getOperand(i));
833 for (unsigned j = 0, f = NumValues; j != f; ++j) {
834 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000835
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000836 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000837
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000838 const Function *F = I.getParent()->getParent();
839 if (F->paramHasAttr(0, Attribute::SExt))
840 ExtendKind = ISD::SIGN_EXTEND;
841 else if (F->paramHasAttr(0, Attribute::ZExt))
842 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000843
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000844 // FIXME: C calling convention requires the return type to be promoted to
845 // at least 32-bit. But this is not necessary for non-C calling
846 // conventions. The frontend should mark functions whose return values
847 // require promoting with signext or zeroext attributes.
848 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
849 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
850 if (VT.bitsLT(MinVT))
851 VT = MinVT;
852 }
853
854 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
855 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
856 SmallVector<SDValue, 4> Parts(NumParts);
857 getCopyToParts(DAG, getCurDebugLoc(),
858 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
859 &Parts[0], NumParts, PartVT, ExtendKind);
860
861 // 'inreg' on function refers to return value
862 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
863 if (F->paramHasAttr(0, Attribute::InReg))
864 Flags.setInReg();
865
866 // Propagate extension type if any
867 if (F->paramHasAttr(0, Attribute::SExt))
868 Flags.setSExt();
869 else if (F->paramHasAttr(0, Attribute::ZExt))
870 Flags.setZExt();
871
872 for (unsigned i = 0; i < NumParts; ++i)
873 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000874 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000875 }
876 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000877
878 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000879 CallingConv::ID CallConv =
880 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000881 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
882 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000883
884 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000885 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000886 "LowerReturn didn't return a valid chain!");
887
888 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000889 DAG.setRoot(Chain);
Bill Wendling87710f02009-12-21 23:47:40 +0000890
891 if (DisableScheduling)
892 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000893}
894
Dan Gohmanad62f532009-04-23 23:13:24 +0000895/// CopyToExportRegsIfNeeded - If the given value has virtual registers
896/// created for it, emit nodes to copy the value into the virtual
897/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000898void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000899 if (!V->use_empty()) {
900 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
901 if (VMI != FuncInfo.ValueMap.end())
902 CopyValueToVirtualRegister(V, VMI->second);
903 }
904}
905
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000906/// ExportFromCurrentBlock - If this condition isn't known to be exported from
907/// the current basic block, add it to ValueMap now so that we'll get a
908/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000909void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000910 // No need to export constants.
911 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000912
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000913 // Already exported?
914 if (FuncInfo.isExportedInst(V)) return;
915
916 unsigned Reg = FuncInfo.InitializeRegForValue(V);
917 CopyValueToVirtualRegister(V, Reg);
918}
919
Dan Gohman2048b852009-11-23 18:04:58 +0000920bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
921 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000922 // The operands of the setcc have to be in this block. We don't know
923 // how to export them from some other block.
924 if (Instruction *VI = dyn_cast<Instruction>(V)) {
925 // Can export from current BB.
926 if (VI->getParent() == FromBB)
927 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000928
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000929 // Is already exported, noop.
930 return FuncInfo.isExportedInst(V);
931 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000932
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000933 // If this is an argument, we can export it if the BB is the entry block or
934 // if it is already exported.
935 if (isa<Argument>(V)) {
936 if (FromBB == &FromBB->getParent()->getEntryBlock())
937 return true;
938
939 // Otherwise, can only export this if it is already exported.
940 return FuncInfo.isExportedInst(V);
941 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000943 // Otherwise, constants can always be exported.
944 return true;
945}
946
947static bool InBlock(const Value *V, const BasicBlock *BB) {
948 if (const Instruction *I = dyn_cast<Instruction>(V))
949 return I->getParent() == BB;
950 return true;
951}
952
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000953/// getFCmpCondCode - Return the ISD condition code corresponding to
954/// the given LLVM IR floating-point condition code. This includes
955/// consideration of global floating-point math flags.
956///
957static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
958 ISD::CondCode FPC, FOC;
959 switch (Pred) {
960 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
961 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
962 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
963 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
964 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
965 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
966 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
967 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
968 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
969 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
970 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
971 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
972 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
973 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
974 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
975 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
976 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000977 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000978 FOC = FPC = ISD::SETFALSE;
979 break;
980 }
981 if (FiniteOnlyFPMath())
982 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000983 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000984 return FPC;
985}
986
987/// getICmpCondCode - Return the ISD condition code corresponding to
988/// the given LLVM IR integer condition code.
989///
990static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
991 switch (Pred) {
992 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
993 case ICmpInst::ICMP_NE: return ISD::SETNE;
994 case ICmpInst::ICMP_SLE: return ISD::SETLE;
995 case ICmpInst::ICMP_ULE: return ISD::SETULE;
996 case ICmpInst::ICMP_SGE: return ISD::SETGE;
997 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
998 case ICmpInst::ICMP_SLT: return ISD::SETLT;
999 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1000 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1001 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1002 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001003 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001004 return ISD::SETNE;
1005 }
1006}
1007
Dan Gohmanc2277342008-10-17 21:16:08 +00001008/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1009/// This function emits a branch and is used at the leaves of an OR or an
1010/// AND operator tree.
1011///
1012void
Dan Gohman2048b852009-11-23 18:04:58 +00001013SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1014 MachineBasicBlock *TBB,
1015 MachineBasicBlock *FBB,
1016 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001017 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001018
Dan Gohmanc2277342008-10-17 21:16:08 +00001019 // If the leaf of the tree is a comparison, merge the condition into
1020 // the caseblock.
1021 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1022 // The operands of the cmp have to be in this block. We don't know
1023 // how to export them from some other block. If this is the first block
1024 // of the sequence, no exporting is needed.
1025 if (CurBB == CurMBB ||
1026 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1027 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001028 ISD::CondCode Condition;
1029 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001030 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001031 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001032 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001033 } else {
1034 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001035 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001036 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001037
1038 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001039 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1040 SwitchCases.push_back(CB);
1041 return;
1042 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001043 }
1044
1045 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001046 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001047 NULL, TBB, FBB, CurBB);
1048 SwitchCases.push_back(CB);
1049}
1050
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001051/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001052void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1053 MachineBasicBlock *TBB,
1054 MachineBasicBlock *FBB,
1055 MachineBasicBlock *CurBB,
1056 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001057 // If this node is not part of the or/and tree, emit it as a branch.
1058 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001059 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001060 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1061 BOp->getParent() != CurBB->getBasicBlock() ||
1062 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1063 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1064 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001065 return;
1066 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001068 // Create TmpBB after CurBB.
1069 MachineFunction::iterator BBI = CurBB;
1070 MachineFunction &MF = DAG.getMachineFunction();
1071 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1072 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001074 if (Opc == Instruction::Or) {
1075 // Codegen X | Y as:
1076 // jmp_if_X TBB
1077 // jmp TmpBB
1078 // TmpBB:
1079 // jmp_if_Y TBB
1080 // jmp FBB
1081 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001083 // Emit the LHS condition.
1084 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001086 // Emit the RHS condition into TmpBB.
1087 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1088 } else {
1089 assert(Opc == Instruction::And && "Unknown merge op!");
1090 // Codegen X & Y as:
1091 // jmp_if_X TmpBB
1092 // jmp FBB
1093 // TmpBB:
1094 // jmp_if_Y TBB
1095 // jmp FBB
1096 //
1097 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001099 // Emit the LHS condition.
1100 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001102 // Emit the RHS condition into TmpBB.
1103 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1104 }
1105}
1106
1107/// If the set of cases should be emitted as a series of branches, return true.
1108/// If we should emit this as a bunch of and/or'd together conditions, return
1109/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001110bool
Dan Gohman2048b852009-11-23 18:04:58 +00001111SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001112 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001113
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001114 // If this is two comparisons of the same values or'd or and'd together, they
1115 // will get folded into a single comparison, so don't emit two blocks.
1116 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1117 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1118 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1119 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1120 return false;
1121 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001122
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001123 return true;
1124}
1125
Dan Gohman2048b852009-11-23 18:04:58 +00001126void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001127 // Update machine-CFG edges.
1128 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1129
1130 // Figure out which block is immediately after the current one.
1131 MachineBasicBlock *NextBlock = 0;
1132 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001133 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001134 NextBlock = BBI;
1135
1136 if (I.isUnconditional()) {
1137 // Update machine-CFG edges.
1138 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001139
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001140 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001141 if (Succ0MBB != NextBlock) {
1142 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001143 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001144 DAG.getBasicBlock(Succ0MBB));
1145 DAG.setRoot(V);
1146
1147 if (DisableScheduling)
1148 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1149 }
1150
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001151 return;
1152 }
1153
1154 // If this condition is one of the special cases we handle, do special stuff
1155 // now.
1156 Value *CondVal = I.getCondition();
1157 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1158
1159 // If this is a series of conditions that are or'd or and'd together, emit
1160 // this as a sequence of branches instead of setcc's with and/or operations.
1161 // For example, instead of something like:
1162 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001163 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001164 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001165 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001166 // or C, F
1167 // jnz foo
1168 // Emit:
1169 // cmp A, B
1170 // je foo
1171 // cmp D, E
1172 // jle foo
1173 //
1174 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001175 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001176 (BOp->getOpcode() == Instruction::And ||
1177 BOp->getOpcode() == Instruction::Or)) {
1178 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1179 // If the compares in later blocks need to use values not currently
1180 // exported from this block, export them now. This block should always
1181 // be the first entry.
1182 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001183
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001184 // Allow some cases to be rejected.
1185 if (ShouldEmitAsBranches(SwitchCases)) {
1186 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1187 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1188 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1189 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001190
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001191 // Emit the branch for this block.
1192 visitSwitchCase(SwitchCases[0]);
1193 SwitchCases.erase(SwitchCases.begin());
1194 return;
1195 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001196
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001197 // Okay, we decided not to do this, remove any inserted MBB's and clear
1198 // SwitchCases.
1199 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001200 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001201
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001202 SwitchCases.clear();
1203 }
1204 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001205
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001206 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001207 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001208 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001209
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001210 // Use visitSwitchCase to actually insert the fast branch sequence for this
1211 // cond branch.
1212 visitSwitchCase(CB);
1213}
1214
1215/// visitSwitchCase - Emits the necessary code to represent a single node in
1216/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001217void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001218 SDValue Cond;
1219 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001220 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001221
1222 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 if (CB.CmpMHS == NULL) {
1224 // Fold "(X == true)" to X and "(X == false)" to !X to
1225 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001226 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001227 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001228 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001229 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001230 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001231 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001232 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001233 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001234 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001235 } else {
1236 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1237
Anton Korobeynikov23218582008-12-23 22:25:27 +00001238 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1239 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001240
1241 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001242 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001243
1244 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001245 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001246 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001247 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001248 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001249 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001250 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001251 DAG.getConstant(High-Low, VT), ISD::SETULE);
1252 }
1253 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001254
Bill Wendling87710f02009-12-21 23:47:40 +00001255 if (DisableScheduling)
1256 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001258 // Update successor info
1259 CurMBB->addSuccessor(CB.TrueBB);
1260 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001262 // Set NextBlock to be the MBB immediately after the current one, if any.
1263 // This is used to avoid emitting unnecessary branches to the next block.
1264 MachineBasicBlock *NextBlock = 0;
1265 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001266 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001267 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001268
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001269 // If the lhs block is the next block, invert the condition so that we can
1270 // fall through to the lhs instead of the rhs block.
1271 if (CB.TrueBB == NextBlock) {
1272 std::swap(CB.TrueBB, CB.FalseBB);
1273 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001274 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001275
1276 if (DisableScheduling)
1277 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001279
Dale Johannesenf5d97892009-02-04 01:48:28 +00001280 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001281 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001282 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001283
Bill Wendling87710f02009-12-21 23:47:40 +00001284 if (DisableScheduling)
1285 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1286
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001287 // If the branch was constant folded, fix up the CFG.
1288 if (BrCond.getOpcode() == ISD::BR) {
1289 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001290 } else {
1291 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001292 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001293 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001294
Bill Wendling87710f02009-12-21 23:47:40 +00001295 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001296 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1297 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001298
1299 if (DisableScheduling)
1300 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1301 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001302 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001303
1304 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001305}
1306
1307/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001308void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001309 // Emit the code for the jump table
1310 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001311 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001312 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1313 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001314 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001315 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1316 MVT::Other, Index.getValue(1),
1317 Table, Index);
1318 DAG.setRoot(BrJumpTable);
1319
Bill Wendling87710f02009-12-21 23:47:40 +00001320 if (DisableScheduling) {
1321 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1322 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001323 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001324 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001325}
1326
1327/// visitJumpTableHeader - This function emits necessary code to produce index
1328/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001329void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1330 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001331 // Subtract the lowest switch case value from the value being switched on and
1332 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001333 // difference between smallest and largest cases.
1334 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001335 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001336 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001337 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001338
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001339 // The SDNode we just created, which holds the value being switched on minus
1340 // the the smallest case value, needs to be copied to a virtual register so it
1341 // can be used as an index into the jump table in a subsequent basic block.
1342 // This value may be smaller or larger than the target's pointer type, and
1343 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001344 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001345
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001346 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001347 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1348 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001349 JT.Reg = JumpTableReg;
1350
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001351 // Emit the range check for the jump table, and branch to the default block
1352 // for the switch statement if the value being switched on exceeds the largest
1353 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001354 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001355 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001356 DAG.getConstant(JTH.Last-JTH.First,VT),
1357 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001358
Bill Wendling87710f02009-12-21 23:47:40 +00001359 if (DisableScheduling) {
1360 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1361 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1362 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1363 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1364 }
1365
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001366 // Set NextBlock to be the MBB immediately after the current one, if any.
1367 // This is used to avoid emitting unnecessary branches to the next block.
1368 MachineBasicBlock *NextBlock = 0;
1369 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001370
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001371 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001372 NextBlock = BBI;
1373
Dale Johannesen66978ee2009-01-31 02:22:37 +00001374 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001375 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001376 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377
Bill Wendling87710f02009-12-21 23:47:40 +00001378 if (DisableScheduling)
1379 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1380
1381 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001382 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1383 DAG.getBasicBlock(JT.MBB));
1384
Bill Wendling87710f02009-12-21 23:47:40 +00001385 if (DisableScheduling)
1386 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1387 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001388
Bill Wendling87710f02009-12-21 23:47:40 +00001389 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001390}
1391
1392/// visitBitTestHeader - This function emits necessary code to produce value
1393/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001394void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001395 // Subtract the minimum value
1396 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001397 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001398 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001399 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001400
1401 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001402 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001403 TLI.getSetCCResultType(Sub.getValueType()),
1404 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001405 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406
Bill Wendling87710f02009-12-21 23:47:40 +00001407 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1408 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409
Duncan Sands92abc622009-01-31 15:50:11 +00001410 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001411 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1412 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001413
Bill Wendling87710f02009-12-21 23:47:40 +00001414 if (DisableScheduling) {
1415 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1416 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1417 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1418 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1419 }
1420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421 // Set NextBlock to be the MBB immediately after the current one, if any.
1422 // This is used to avoid emitting unnecessary branches to the next block.
1423 MachineBasicBlock *NextBlock = 0;
1424 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001425 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001426 NextBlock = BBI;
1427
1428 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1429
1430 CurMBB->addSuccessor(B.Default);
1431 CurMBB->addSuccessor(MBB);
1432
Dale Johannesen66978ee2009-01-31 02:22:37 +00001433 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001435 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001436
Bill Wendling87710f02009-12-21 23:47:40 +00001437 if (DisableScheduling)
1438 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1439
1440 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001441 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1442 DAG.getBasicBlock(MBB));
1443
Bill Wendling87710f02009-12-21 23:47:40 +00001444 if (DisableScheduling)
1445 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1446 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001447
Bill Wendling87710f02009-12-21 23:47:40 +00001448 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449}
1450
1451/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001452void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1453 unsigned Reg,
1454 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001455 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001456 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001457 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001458 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001459 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001460 DAG.getConstant(1, TLI.getPointerTy()),
1461 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001462
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001463 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001464 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001465 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001466 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001467 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1468 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001469 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001470 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001471
Bill Wendling87710f02009-12-21 23:47:40 +00001472 if (DisableScheduling) {
1473 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1474 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1475 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1476 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1477 }
1478
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001479 CurMBB->addSuccessor(B.TargetBB);
1480 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001481
Dale Johannesen66978ee2009-01-31 02:22:37 +00001482 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001483 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001484 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485
Bill Wendling87710f02009-12-21 23:47:40 +00001486 if (DisableScheduling)
1487 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1488
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001489 // Set NextBlock to be the MBB immediately after the current one, if any.
1490 // This is used to avoid emitting unnecessary branches to the next block.
1491 MachineBasicBlock *NextBlock = 0;
1492 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001493 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001494 NextBlock = BBI;
1495
Bill Wendling87710f02009-12-21 23:47:40 +00001496 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001497 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1498 DAG.getBasicBlock(NextMBB));
1499
Bill Wendling87710f02009-12-21 23:47:40 +00001500 if (DisableScheduling)
1501 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1502 }
Bill Wendling0777e922009-12-21 21:59:52 +00001503
Bill Wendling87710f02009-12-21 23:47:40 +00001504 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001505}
1506
Dan Gohman2048b852009-11-23 18:04:58 +00001507void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001508 // Retrieve successors.
1509 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1510 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1511
Gabor Greifb67e6b32009-01-15 11:10:44 +00001512 const Value *Callee(I.getCalledValue());
1513 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001514 visitInlineAsm(&I);
1515 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001516 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001517
1518 // If the value of the invoke is used outside of its defining block, make it
1519 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001520 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001521
1522 // Update successor info
1523 CurMBB->addSuccessor(Return);
1524 CurMBB->addSuccessor(LandingPad);
1525
1526 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001527 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1528 MVT::Other, getControlRoot(),
1529 DAG.getBasicBlock(Return));
1530 DAG.setRoot(Branch);
1531
1532 if (DisableScheduling)
1533 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001534}
1535
Dan Gohman2048b852009-11-23 18:04:58 +00001536void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001537}
1538
1539/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1540/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001541bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1542 CaseRecVector& WorkList,
1543 Value* SV,
1544 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001545 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001546
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001547 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001548 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001549 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001550 return false;
1551
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552 // Get the MachineFunction which holds the current MBB. This is used when
1553 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001554 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001555
1556 // Figure out which block is immediately after the current one.
1557 MachineBasicBlock *NextBlock = 0;
1558 MachineFunction::iterator BBI = CR.CaseBB;
1559
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001560 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001561 NextBlock = BBI;
1562
1563 // TODO: If any two of the cases has the same destination, and if one value
1564 // is the same as the other, but has one bit unset that the other has set,
1565 // use bit manipulation to do two compares at once. For example:
1566 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001567
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001568 // Rearrange the case blocks so that the last one falls through if possible.
1569 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1570 // The last case block won't fall through into 'NextBlock' if we emit the
1571 // branches in this order. See if rearranging a case value would help.
1572 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1573 if (I->BB == NextBlock) {
1574 std::swap(*I, BackCase);
1575 break;
1576 }
1577 }
1578 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001579
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 // Create a CaseBlock record representing a conditional branch to
1581 // the Case's target mbb if the value being switched on SV is equal
1582 // to C.
1583 MachineBasicBlock *CurBlock = CR.CaseBB;
1584 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1585 MachineBasicBlock *FallThrough;
1586 if (I != E-1) {
1587 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1588 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001589
1590 // Put SV in a virtual register to make it available from the new blocks.
1591 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001592 } else {
1593 // If the last case doesn't match, go to the default block.
1594 FallThrough = Default;
1595 }
1596
1597 Value *RHS, *LHS, *MHS;
1598 ISD::CondCode CC;
1599 if (I->High == I->Low) {
1600 // This is just small small case range :) containing exactly 1 case
1601 CC = ISD::SETEQ;
1602 LHS = SV; RHS = I->High; MHS = NULL;
1603 } else {
1604 CC = ISD::SETLE;
1605 LHS = I->Low; MHS = SV; RHS = I->High;
1606 }
1607 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001608
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609 // If emitting the first comparison, just call visitSwitchCase to emit the
1610 // code into the current block. Otherwise, push the CaseBlock onto the
1611 // vector to be later processed by SDISel, and insert the node's MBB
1612 // before the next MBB.
1613 if (CurBlock == CurMBB)
1614 visitSwitchCase(CB);
1615 else
1616 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001617
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001618 CurBlock = FallThrough;
1619 }
1620
1621 return true;
1622}
1623
1624static inline bool areJTsAllowed(const TargetLowering &TLI) {
1625 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001626 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1627 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001628}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001629
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001630static APInt ComputeRange(const APInt &First, const APInt &Last) {
1631 APInt LastExt(Last), FirstExt(First);
1632 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1633 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1634 return (LastExt - FirstExt + 1ULL);
1635}
1636
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001637/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001638bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1639 CaseRecVector& WorkList,
1640 Value* SV,
1641 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001642 Case& FrontCase = *CR.Range.first;
1643 Case& BackCase = *(CR.Range.second-1);
1644
Chris Lattnere880efe2009-11-07 07:50:34 +00001645 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1646 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001647
Chris Lattnere880efe2009-11-07 07:50:34 +00001648 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001649 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1650 I!=E; ++I)
1651 TSize += I->size();
1652
Chris Lattnere880efe2009-11-07 07:50:34 +00001653 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001654 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001655
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001656 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001657 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001658 if (Density < 0.4)
1659 return false;
1660
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001661 DEBUG(errs() << "Lowering jump table\n"
1662 << "First entry: " << First << ". Last entry: " << Last << '\n'
1663 << "Range: " << Range
1664 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001665
1666 // Get the MachineFunction which holds the current MBB. This is used when
1667 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001668 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001669
1670 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001671 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001672 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001673
1674 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1675
1676 // Create a new basic block to hold the code for loading the address
1677 // of the jump table, and jumping to it. Update successor information;
1678 // we will either branch to the default case for the switch, or the jump
1679 // table.
1680 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1681 CurMF->insert(BBI, JumpTableBB);
1682 CR.CaseBB->addSuccessor(Default);
1683 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001684
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001685 // Build a vector of destination BBs, corresponding to each target
1686 // of the jump table. If the value of the jump table slot corresponds to
1687 // a case statement, push the case's BB onto the vector, otherwise, push
1688 // the default BB.
1689 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001690 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001691 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001692 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1693 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1694
1695 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696 DestBBs.push_back(I->BB);
1697 if (TEI==High)
1698 ++I;
1699 } else {
1700 DestBBs.push_back(Default);
1701 }
1702 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001703
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001704 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001705 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1706 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001707 E = DestBBs.end(); I != E; ++I) {
1708 if (!SuccsHandled[(*I)->getNumber()]) {
1709 SuccsHandled[(*I)->getNumber()] = true;
1710 JumpTableBB->addSuccessor(*I);
1711 }
1712 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001713
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 // Create a jump table index for this jump table, or return an existing
1715 // one.
1716 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001718 // Set the jump table information so that we can codegen it as a second
1719 // MachineBasicBlock
1720 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1721 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1722 if (CR.CaseBB == CurMBB)
1723 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001724
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001725 JTCases.push_back(JumpTableBlock(JTH, JT));
1726
1727 return true;
1728}
1729
1730/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1731/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001732bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1733 CaseRecVector& WorkList,
1734 Value* SV,
1735 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001736 // Get the MachineFunction which holds the current MBB. This is used when
1737 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001738 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001739
1740 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001742 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001743
1744 Case& FrontCase = *CR.Range.first;
1745 Case& BackCase = *(CR.Range.second-1);
1746 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1747
1748 // Size is the number of Cases represented by this range.
1749 unsigned Size = CR.Range.second - CR.Range.first;
1750
Chris Lattnere880efe2009-11-07 07:50:34 +00001751 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1752 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001753 double FMetric = 0;
1754 CaseItr Pivot = CR.Range.first + Size/2;
1755
1756 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1757 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001758 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001759 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1760 I!=E; ++I)
1761 TSize += I->size();
1762
Chris Lattnere880efe2009-11-07 07:50:34 +00001763 APInt LSize = FrontCase.size();
1764 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001765 DEBUG(errs() << "Selecting best pivot: \n"
1766 << "First: " << First << ", Last: " << Last <<'\n'
1767 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001768 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1769 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001770 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1771 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001772 APInt Range = ComputeRange(LEnd, RBegin);
1773 assert((Range - 2ULL).isNonNegative() &&
1774 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001775 double LDensity = (double)LSize.roundToDouble() /
1776 (LEnd - First + 1ULL).roundToDouble();
1777 double RDensity = (double)RSize.roundToDouble() /
1778 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001779 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001780 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001781 DEBUG(errs() <<"=>Step\n"
1782 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1783 << "LDensity: " << LDensity
1784 << ", RDensity: " << RDensity << '\n'
1785 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001786 if (FMetric < Metric) {
1787 Pivot = J;
1788 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001789 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001790 }
1791
1792 LSize += J->size();
1793 RSize -= J->size();
1794 }
1795 if (areJTsAllowed(TLI)) {
1796 // If our case is dense we *really* should handle it earlier!
1797 assert((FMetric > 0) && "Should handle dense range earlier!");
1798 } else {
1799 Pivot = CR.Range.first + Size/2;
1800 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001802 CaseRange LHSR(CR.Range.first, Pivot);
1803 CaseRange RHSR(Pivot, CR.Range.second);
1804 Constant *C = Pivot->Low;
1805 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001806
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001807 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001808 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001809 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001810 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001811 // Pivot's Value, then we can branch directly to the LHS's Target,
1812 // rather than creating a leaf node for it.
1813 if ((LHSR.second - LHSR.first) == 1 &&
1814 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001815 cast<ConstantInt>(C)->getValue() ==
1816 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001817 TrueBB = LHSR.first->BB;
1818 } else {
1819 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1820 CurMF->insert(BBI, TrueBB);
1821 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001822
1823 // Put SV in a virtual register to make it available from the new blocks.
1824 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001825 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001826
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001827 // Similar to the optimization above, if the Value being switched on is
1828 // known to be less than the Constant CR.LT, and the current Case Value
1829 // is CR.LT - 1, then we can branch directly to the target block for
1830 // the current Case Value, rather than emitting a RHS leaf node for it.
1831 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001832 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1833 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001834 FalseBB = RHSR.first->BB;
1835 } else {
1836 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1837 CurMF->insert(BBI, FalseBB);
1838 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001839
1840 // Put SV in a virtual register to make it available from the new blocks.
1841 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001842 }
1843
1844 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001845 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001846 // Otherwise, branch to LHS.
1847 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1848
1849 if (CR.CaseBB == CurMBB)
1850 visitSwitchCase(CB);
1851 else
1852 SwitchCases.push_back(CB);
1853
1854 return true;
1855}
1856
1857/// handleBitTestsSwitchCase - if current case range has few destination and
1858/// range span less, than machine word bitwidth, encode case range into series
1859/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001860bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1861 CaseRecVector& WorkList,
1862 Value* SV,
1863 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001864 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001865 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001866
1867 Case& FrontCase = *CR.Range.first;
1868 Case& BackCase = *(CR.Range.second-1);
1869
1870 // Get the MachineFunction which holds the current MBB. This is used when
1871 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001872 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001873
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001874 // If target does not have legal shift left, do not emit bit tests at all.
1875 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1876 return false;
1877
Anton Korobeynikov23218582008-12-23 22:25:27 +00001878 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001879 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1880 I!=E; ++I) {
1881 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001882 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001883 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001885 // Count unique destinations
1886 SmallSet<MachineBasicBlock*, 4> Dests;
1887 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1888 Dests.insert(I->BB);
1889 if (Dests.size() > 3)
1890 // Don't bother the code below, if there are too much unique destinations
1891 return false;
1892 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001893 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1894 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001895
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001896 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001897 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1898 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001899 APInt cmpRange = maxValue - minValue;
1900
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001901 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1902 << "Low bound: " << minValue << '\n'
1903 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001904
1905 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001906 (!(Dests.size() == 1 && numCmps >= 3) &&
1907 !(Dests.size() == 2 && numCmps >= 5) &&
1908 !(Dests.size() >= 3 && numCmps >= 6)))
1909 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001910
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001911 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001912 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001914 // Optimize the case where all the case values fit in a
1915 // word without having to subtract minValue. In this case,
1916 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001917 if (minValue.isNonNegative() &&
1918 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1919 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001920 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001921 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001922 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001923
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001924 CaseBitsVector CasesBits;
1925 unsigned i, count = 0;
1926
1927 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1928 MachineBasicBlock* Dest = I->BB;
1929 for (i = 0; i < count; ++i)
1930 if (Dest == CasesBits[i].BB)
1931 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001932
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001933 if (i == count) {
1934 assert((count < 3) && "Too much destinations to test!");
1935 CasesBits.push_back(CaseBits(0, Dest, 0));
1936 count++;
1937 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001938
1939 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1940 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1941
1942 uint64_t lo = (lowValue - lowBound).getZExtValue();
1943 uint64_t hi = (highValue - lowBound).getZExtValue();
1944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945 for (uint64_t j = lo; j <= hi; j++) {
1946 CasesBits[i].Mask |= 1ULL << j;
1947 CasesBits[i].Bits++;
1948 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001949
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001950 }
1951 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001952
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001953 BitTestInfo BTC;
1954
1955 // Figure out which block is immediately after the current one.
1956 MachineFunction::iterator BBI = CR.CaseBB;
1957 ++BBI;
1958
1959 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1960
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001961 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001962 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001963 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
1964 << ", Bits: " << CasesBits[i].Bits
1965 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001966
1967 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1968 CurMF->insert(BBI, CaseBB);
1969 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1970 CaseBB,
1971 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001972
1973 // Put SV in a virtual register to make it available from the new blocks.
1974 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001975 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001976
1977 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001978 -1U, (CR.CaseBB == CurMBB),
1979 CR.CaseBB, Default, BTC);
1980
1981 if (CR.CaseBB == CurMBB)
1982 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001983
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001984 BitTestCases.push_back(BTB);
1985
1986 return true;
1987}
1988
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001989/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001990size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1991 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001992 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001993
1994 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001995 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001996 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1997 Cases.push_back(Case(SI.getSuccessorValue(i),
1998 SI.getSuccessorValue(i),
1999 SMBB));
2000 }
2001 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2002
2003 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002004 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002005 // Must recompute end() each iteration because it may be
2006 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002007 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2008 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2009 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002010 MachineBasicBlock* nextBB = J->BB;
2011 MachineBasicBlock* currentBB = I->BB;
2012
2013 // If the two neighboring cases go to the same destination, merge them
2014 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002015 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002016 I->High = J->High;
2017 J = Cases.erase(J);
2018 } else {
2019 I = J++;
2020 }
2021 }
2022
2023 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2024 if (I->Low != I->High)
2025 // A range counts double, since it requires two compares.
2026 ++numCmps;
2027 }
2028
2029 return numCmps;
2030}
2031
Dan Gohman2048b852009-11-23 18:04:58 +00002032void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002033 // Figure out which block is immediately after the current one.
2034 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002035 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2036
2037 // If there is only the default destination, branch to it if it is not the
2038 // next basic block. Otherwise, just fall through.
2039 if (SI.getNumOperands() == 2) {
2040 // Update machine-CFG edges.
2041
2042 // If this is not a fall-through branch, emit the branch.
2043 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002044 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002045 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002046 MVT::Other, getControlRoot(),
2047 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002048 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002049
2050 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002051 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002052 }
2053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002054 return;
2055 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002057 // If there are any non-default case statements, create a vector of Cases
2058 // representing each one, and sort the vector so that we can efficiently
2059 // create a binary search tree from them.
2060 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002061 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002062 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2063 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002064 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002065
2066 // Get the Value to be switched on and default basic blocks, which will be
2067 // inserted into CaseBlock records, representing basic blocks in the binary
2068 // search tree.
2069 Value *SV = SI.getOperand(0);
2070
2071 // Push the initial CaseRec onto the worklist
2072 CaseRecVector WorkList;
2073 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2074
2075 while (!WorkList.empty()) {
2076 // Grab a record representing a case range to process off the worklist
2077 CaseRec CR = WorkList.back();
2078 WorkList.pop_back();
2079
2080 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2081 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002082
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002083 // If the range has few cases (two or less) emit a series of specific
2084 // tests.
2085 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2086 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002087
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002088 // If the switch has more than 5 blocks, and at least 40% dense, and the
2089 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002090 // lowering the switch to a binary tree of conditional branches.
2091 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2092 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002093
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002094 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2095 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2096 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2097 }
2098}
2099
Dan Gohman2048b852009-11-23 18:04:58 +00002100void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002101 // Update machine-CFG edges.
2102 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2103 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2104
Bill Wendling49fcff82009-12-21 22:30:11 +00002105 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2106 MVT::Other, getControlRoot(),
2107 getValue(I.getAddress()));
2108 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002109
Bill Wendling49fcff82009-12-21 22:30:11 +00002110 if (DisableScheduling)
2111 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2112}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002113
Dan Gohman2048b852009-11-23 18:04:58 +00002114void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002115 // -0.0 - X --> fneg
2116 const Type *Ty = I.getType();
2117 if (isa<VectorType>(Ty)) {
2118 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2119 const VectorType *DestTy = cast<VectorType>(I.getType());
2120 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002121 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002122 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002123 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002124 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002125 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002126 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2127 Op2.getValueType(), Op2);
2128 setValue(&I, Res);
2129
2130 if (DisableScheduling)
2131 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2132
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133 return;
2134 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002135 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002136 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002137
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002138 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002139 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002140 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002141 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2142 Op2.getValueType(), Op2);
2143 setValue(&I, Res);
2144
2145 if (DisableScheduling)
2146 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2147
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002148 return;
2149 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002150
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002151 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002152}
2153
Dan Gohman2048b852009-11-23 18:04:58 +00002154void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002155 SDValue Op1 = getValue(I.getOperand(0));
2156 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002157 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2158 Op1.getValueType(), Op1, Op2);
2159 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002160
Bill Wendling49fcff82009-12-21 22:30:11 +00002161 if (DisableScheduling)
2162 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002163}
2164
Dan Gohman2048b852009-11-23 18:04:58 +00002165void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166 SDValue Op1 = getValue(I.getOperand(0));
2167 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002168 if (!isa<VectorType>(I.getType()) &&
2169 Op2.getValueType() != TLI.getShiftAmountTy()) {
2170 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002171 EVT PTy = TLI.getPointerTy();
2172 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002173 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002174 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2175 TLI.getShiftAmountTy(), Op2);
2176 // If the operand is larger than the shift count type but the shift
2177 // count type has enough bits to represent any shift value, truncate
2178 // it now. This is a common case and it exposes the truncate to
2179 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002180 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002181 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2182 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2183 TLI.getShiftAmountTy(), Op2);
2184 // Otherwise we'll need to temporarily settle for some other
2185 // convenient type; type legalization will make adjustments as
2186 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002187 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002188 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002189 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002190 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002191 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002192 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002193 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002194
Bill Wendling49fcff82009-12-21 22:30:11 +00002195 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2196 Op1.getValueType(), Op1, Op2);
2197 setValue(&I, Res);
2198
Bill Wendling87710f02009-12-21 23:47:40 +00002199 if (DisableScheduling) {
2200 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2201 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002202 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002203 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002204}
2205
Dan Gohman2048b852009-11-23 18:04:58 +00002206void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2208 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2209 predicate = IC->getPredicate();
2210 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2211 predicate = ICmpInst::Predicate(IC->getPredicate());
2212 SDValue Op1 = getValue(I.getOperand(0));
2213 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002214 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002215
Owen Andersone50ed302009-08-10 22:56:29 +00002216 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002217 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2218 setValue(&I, Res);
2219
2220 if (DisableScheduling)
2221 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222}
2223
Dan Gohman2048b852009-11-23 18:04:58 +00002224void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2226 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2227 predicate = FC->getPredicate();
2228 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2229 predicate = FCmpInst::Predicate(FC->getPredicate());
2230 SDValue Op1 = getValue(I.getOperand(0));
2231 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002232 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002233 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002234 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2235 setValue(&I, Res);
2236
2237 if (DisableScheduling)
2238 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239}
2240
Dan Gohman2048b852009-11-23 18:04:58 +00002241void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002242 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002243 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2244 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002245 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002246
Bill Wendling49fcff82009-12-21 22:30:11 +00002247 SmallVector<SDValue, 4> Values(NumValues);
2248 SDValue Cond = getValue(I.getOperand(0));
2249 SDValue TrueVal = getValue(I.getOperand(1));
2250 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002251
Bill Wendling49fcff82009-12-21 22:30:11 +00002252 for (unsigned i = 0; i != NumValues; ++i) {
2253 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2254 TrueVal.getNode()->getValueType(i), Cond,
2255 SDValue(TrueVal.getNode(),
2256 TrueVal.getResNo() + i),
2257 SDValue(FalseVal.getNode(),
2258 FalseVal.getResNo() + i));
2259
2260 if (DisableScheduling)
2261 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002262 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002263
Bill Wendling49fcff82009-12-21 22:30:11 +00002264 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2265 DAG.getVTList(&ValueVTs[0], NumValues),
2266 &Values[0], NumValues);
2267 setValue(&I, Res);
2268
2269 if (DisableScheduling)
2270 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2271}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002272
Dan Gohman2048b852009-11-23 18:04:58 +00002273void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2275 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002276 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002277 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2278 setValue(&I, Res);
2279
2280 if (DisableScheduling)
2281 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002282}
2283
Dan Gohman2048b852009-11-23 18:04:58 +00002284void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002285 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2286 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2287 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002288 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002289 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2290 setValue(&I, Res);
2291
2292 if (DisableScheduling)
2293 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002294}
2295
Dan Gohman2048b852009-11-23 18:04:58 +00002296void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002297 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2298 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2299 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002300 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002301 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2302 setValue(&I, Res);
2303
2304 if (DisableScheduling)
2305 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002306}
2307
Dan Gohman2048b852009-11-23 18:04:58 +00002308void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002309 // FPTrunc is never a no-op cast, no need to check
2310 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002311 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002312 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2313 DestVT, N, DAG.getIntPtrConstant(0));
2314 setValue(&I, Res);
2315
2316 if (DisableScheduling)
2317 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002318}
2319
Dan Gohman2048b852009-11-23 18:04:58 +00002320void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002321 // FPTrunc is never a no-op cast, no need to check
2322 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002323 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002324 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2325 setValue(&I, Res);
2326
2327 if (DisableScheduling)
2328 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002329}
2330
Dan Gohman2048b852009-11-23 18:04:58 +00002331void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002332 // FPToUI is never a no-op cast, no need to check
2333 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002334 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002335 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2336 setValue(&I, Res);
2337
2338 if (DisableScheduling)
2339 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002340}
2341
Dan Gohman2048b852009-11-23 18:04:58 +00002342void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002343 // FPToSI is never a no-op cast, no need to check
2344 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002345 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002346 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2347 setValue(&I, Res);
2348
2349 if (DisableScheduling)
2350 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002351}
2352
Dan Gohman2048b852009-11-23 18:04:58 +00002353void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002354 // UIToFP is never a no-op cast, no need to check
2355 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002356 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002357 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2358 setValue(&I, Res);
2359
2360 if (DisableScheduling)
2361 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002362}
2363
Dan Gohman2048b852009-11-23 18:04:58 +00002364void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002365 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002366 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002367 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002368 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2369 setValue(&I, Res);
2370
2371 if (DisableScheduling)
2372 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002373}
2374
Dan Gohman2048b852009-11-23 18:04:58 +00002375void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002376 // What to do depends on the size of the integer and the size of the pointer.
2377 // We can either truncate, zero extend, or no-op, accordingly.
2378 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002379 EVT SrcVT = N.getValueType();
2380 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002381 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2382 setValue(&I, Res);
2383
2384 if (DisableScheduling)
2385 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002386}
2387
Dan Gohman2048b852009-11-23 18:04:58 +00002388void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002389 // What to do depends on the size of the integer and the size of the pointer.
2390 // We can either truncate, zero extend, or no-op, accordingly.
2391 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002392 EVT SrcVT = N.getValueType();
2393 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002394 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2395 setValue(&I, Res);
2396
2397 if (DisableScheduling)
2398 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002399}
2400
Dan Gohman2048b852009-11-23 18:04:58 +00002401void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002402 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002403 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002404
Bill Wendling49fcff82009-12-21 22:30:11 +00002405 // BitCast assures us that source and destination are the same size so this is
2406 // either a BIT_CONVERT or a no-op.
2407 if (DestVT != N.getValueType()) {
2408 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2409 DestVT, N); // convert types.
2410 setValue(&I, Res);
2411
2412 if (DisableScheduling)
2413 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2414 } else {
2415 setValue(&I, N); // noop cast.
2416 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002417}
2418
Dan Gohman2048b852009-11-23 18:04:58 +00002419void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002420 SDValue InVec = getValue(I.getOperand(0));
2421 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002422 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002423 TLI.getPointerTy(),
2424 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002425 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2426 TLI.getValueType(I.getType()),
2427 InVec, InVal, InIdx);
2428 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002429
Bill Wendling87710f02009-12-21 23:47:40 +00002430 if (DisableScheduling) {
2431 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002432 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002433 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002434}
2435
Dan Gohman2048b852009-11-23 18:04:58 +00002436void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002437 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002438 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002439 TLI.getPointerTy(),
2440 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002441 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2442 TLI.getValueType(I.getType()), InVec, InIdx);
2443 setValue(&I, Res);
2444
Bill Wendling87710f02009-12-21 23:47:40 +00002445 if (DisableScheduling) {
2446 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002447 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002448 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002449}
2450
Mon P Wangaeb06d22008-11-10 04:46:22 +00002451
2452// Utility for visitShuffleVector - Returns true if the mask is mask starting
2453// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002454static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2455 unsigned MaskNumElts = Mask.size();
2456 for (unsigned i = 0; i != MaskNumElts; ++i)
2457 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002458 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002459 return true;
2460}
2461
Dan Gohman2048b852009-11-23 18:04:58 +00002462void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002463 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002464 SDValue Src1 = getValue(I.getOperand(0));
2465 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002466
Nate Begeman9008ca62009-04-27 18:41:29 +00002467 // Convert the ConstantVector mask operand into an array of ints, with -1
2468 // representing undef values.
2469 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002470 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2471 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002472 unsigned MaskNumElts = MaskElts.size();
2473 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002474 if (isa<UndefValue>(MaskElts[i]))
2475 Mask.push_back(-1);
2476 else
2477 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2478 }
2479
Owen Andersone50ed302009-08-10 22:56:29 +00002480 EVT VT = TLI.getValueType(I.getType());
2481 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002482 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002483
Mon P Wangc7849c22008-11-16 05:06:27 +00002484 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002485 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2486 &Mask[0]);
2487 setValue(&I, Res);
2488
2489 if (DisableScheduling)
2490 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2491
Mon P Wangaeb06d22008-11-10 04:46:22 +00002492 return;
2493 }
2494
2495 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002496 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2497 // Mask is longer than the source vectors and is a multiple of the source
2498 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002499 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002500 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2501 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002502 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2503 VT, Src1, Src2);
2504 setValue(&I, Res);
2505
2506 if (DisableScheduling)
2507 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2508
Mon P Wangaeb06d22008-11-10 04:46:22 +00002509 return;
2510 }
2511
Mon P Wangc7849c22008-11-16 05:06:27 +00002512 // Pad both vectors with undefs to make them the same length as the mask.
2513 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002514 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2515 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002516 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002517
Nate Begeman9008ca62009-04-27 18:41:29 +00002518 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2519 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002520 MOps1[0] = Src1;
2521 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002522
2523 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2524 getCurDebugLoc(), VT,
2525 &MOps1[0], NumConcat);
2526 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2527 getCurDebugLoc(), VT,
2528 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002529
Mon P Wangaeb06d22008-11-10 04:46:22 +00002530 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002531 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002532 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002533 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002534 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002535 MappedOps.push_back(Idx);
2536 else
2537 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002538 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002539
2540 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2541 &MappedOps[0]);
2542 setValue(&I, Res);
2543
Bill Wendlinge1a90422009-12-21 23:10:19 +00002544 if (DisableScheduling) {
2545 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2546 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002547 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002548 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002549
Mon P Wangaeb06d22008-11-10 04:46:22 +00002550 return;
2551 }
2552
Mon P Wangc7849c22008-11-16 05:06:27 +00002553 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002554 // Analyze the access pattern of the vector to see if we can extract
2555 // two subvectors and do the shuffle. The analysis is done by calculating
2556 // the range of elements the mask access on both vectors.
2557 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2558 int MaxRange[2] = {-1, -1};
2559
Nate Begeman5a5ca152009-04-29 05:20:52 +00002560 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002561 int Idx = Mask[i];
2562 int Input = 0;
2563 if (Idx < 0)
2564 continue;
2565
Nate Begeman5a5ca152009-04-29 05:20:52 +00002566 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002567 Input = 1;
2568 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002569 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002570 if (Idx > MaxRange[Input])
2571 MaxRange[Input] = Idx;
2572 if (Idx < MinRange[Input])
2573 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002574 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002575
Mon P Wangc7849c22008-11-16 05:06:27 +00002576 // Check if the access is smaller than the vector size and can we find
2577 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002578 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002579 int StartIdx[2]; // StartIdx to extract from
2580 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002581 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002582 RangeUse[Input] = 0; // Unused
2583 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002584 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002585 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002586 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002587 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002588 RangeUse[Input] = 1; // Extract from beginning of the vector
2589 StartIdx[Input] = 0;
2590 } else {
2591 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002592 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002593 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002594 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002595 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002596 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002597 }
2598
Bill Wendling636e2582009-08-21 18:16:06 +00002599 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002600 SDValue Res = DAG.getUNDEF(VT);
2601 setValue(&I, Res); // Vectors are not used.
2602
2603 if (DisableScheduling)
2604 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2605
Mon P Wangc7849c22008-11-16 05:06:27 +00002606 return;
2607 }
2608 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2609 // Extract appropriate subvector and generate a vector shuffle
2610 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002611 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002612 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002613 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002614 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002615 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002616 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002617
2618 if (DisableScheduling)
2619 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002620 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002621
Mon P Wangc7849c22008-11-16 05:06:27 +00002622 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002623 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002624 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002625 int Idx = Mask[i];
2626 if (Idx < 0)
2627 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002628 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002629 MappedOps.push_back(Idx - StartIdx[0]);
2630 else
2631 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002632 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002633
2634 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2635 &MappedOps[0]);
2636 setValue(&I, Res);
2637
2638 if (DisableScheduling)
2639 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2640
Mon P Wangc7849c22008-11-16 05:06:27 +00002641 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002642 }
2643 }
2644
Mon P Wangc7849c22008-11-16 05:06:27 +00002645 // We can't use either concat vectors or extract subvectors so fall back to
2646 // replacing the shuffle with extract and build vector.
2647 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002648 EVT EltVT = VT.getVectorElementType();
2649 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002650 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002651 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002652 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002653 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002654 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002655 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002656 SDValue Res;
2657
Nate Begeman5a5ca152009-04-29 05:20:52 +00002658 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002659 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2660 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002661 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002662 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2663 EltVT, Src2,
2664 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2665
2666 Ops.push_back(Res);
2667
2668 if (DisableScheduling)
2669 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002670 }
2671 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002672
2673 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2674 VT, &Ops[0], Ops.size());
2675 setValue(&I, Res);
2676
2677 if (DisableScheduling)
2678 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002679}
2680
Dan Gohman2048b852009-11-23 18:04:58 +00002681void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002682 const Value *Op0 = I.getOperand(0);
2683 const Value *Op1 = I.getOperand(1);
2684 const Type *AggTy = I.getType();
2685 const Type *ValTy = Op1->getType();
2686 bool IntoUndef = isa<UndefValue>(Op0);
2687 bool FromUndef = isa<UndefValue>(Op1);
2688
2689 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2690 I.idx_begin(), I.idx_end());
2691
Owen Andersone50ed302009-08-10 22:56:29 +00002692 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002693 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002694 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002695 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2696
2697 unsigned NumAggValues = AggValueVTs.size();
2698 unsigned NumValValues = ValValueVTs.size();
2699 SmallVector<SDValue, 4> Values(NumAggValues);
2700
2701 SDValue Agg = getValue(Op0);
2702 SDValue Val = getValue(Op1);
2703 unsigned i = 0;
2704 // Copy the beginning value(s) from the original aggregate.
2705 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002706 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002707 SDValue(Agg.getNode(), Agg.getResNo() + i);
2708 // Copy values from the inserted value(s).
2709 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002710 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2712 // Copy remaining value(s) from the original aggregate.
2713 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002714 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002715 SDValue(Agg.getNode(), Agg.getResNo() + i);
2716
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002717 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2718 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2719 &Values[0], NumAggValues);
2720 setValue(&I, Res);
2721
2722 if (DisableScheduling)
2723 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002724}
2725
Dan Gohman2048b852009-11-23 18:04:58 +00002726void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002727 const Value *Op0 = I.getOperand(0);
2728 const Type *AggTy = Op0->getType();
2729 const Type *ValTy = I.getType();
2730 bool OutOfUndef = isa<UndefValue>(Op0);
2731
2732 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2733 I.idx_begin(), I.idx_end());
2734
Owen Andersone50ed302009-08-10 22:56:29 +00002735 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002736 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2737
2738 unsigned NumValValues = ValValueVTs.size();
2739 SmallVector<SDValue, 4> Values(NumValValues);
2740
2741 SDValue Agg = getValue(Op0);
2742 // Copy out the selected value(s).
2743 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2744 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002745 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002746 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002747 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002748
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002749 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2750 DAG.getVTList(&ValValueVTs[0], NumValValues),
2751 &Values[0], NumValValues);
2752 setValue(&I, Res);
2753
2754 if (DisableScheduling)
2755 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002756}
2757
Dan Gohman2048b852009-11-23 18:04:58 +00002758void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002759 SDValue N = getValue(I.getOperand(0));
2760 const Type *Ty = I.getOperand(0)->getType();
2761
2762 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2763 OI != E; ++OI) {
2764 Value *Idx = *OI;
2765 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2766 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2767 if (Field) {
2768 // N = N + Offset
2769 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002770 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002771 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002772
2773 if (DisableScheduling)
2774 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002775 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002776
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002777 Ty = StTy->getElementType(Field);
2778 } else {
2779 Ty = cast<SequentialType>(Ty)->getElementType();
2780
2781 // If this is a constant subscript, handle it quickly.
2782 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2783 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002784 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002785 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002786 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002787 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002788 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002789 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002790 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2791 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002792 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002793 else
Evan Chengb1032a82009-02-09 20:54:38 +00002794 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002795
Dale Johannesen66978ee2009-01-31 02:22:37 +00002796 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002797 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002798
2799 if (DisableScheduling) {
2800 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2801 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2802 }
2803
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002804 continue;
2805 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002806
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002808 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2809 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002810 SDValue IdxN = getValue(Idx);
2811
2812 // If the index is smaller or larger than intptr_t, truncate or extend
2813 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002814 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002815
2816 // If this is a multiply by a power of two, turn it into a shl
2817 // immediately. This is a very common case.
2818 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002819 if (ElementSize.isPowerOf2()) {
2820 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002821 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002822 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002823 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002824 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002825 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002826 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002827 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002828 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002829
2830 if (DisableScheduling)
2831 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 }
2833
Scott Michelfdc40a02009-02-17 22:15:04 +00002834 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002835 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002836
2837 if (DisableScheduling)
2838 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002839 }
2840 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002841
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002842 setValue(&I, N);
2843}
2844
Dan Gohman2048b852009-11-23 18:04:58 +00002845void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002846 // If this is a fixed sized alloca in the entry block of the function,
2847 // allocate it statically on the stack.
2848 if (FuncInfo.StaticAllocaMap.count(&I))
2849 return; // getValue will auto-populate this.
2850
2851 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002852 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002853 unsigned Align =
2854 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2855 I.getAlignment());
2856
2857 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002858
2859 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2860 AllocSize,
2861 DAG.getConstant(TySize, AllocSize.getValueType()));
2862
Bill Wendling856ff412009-12-22 00:12:37 +00002863 if (DisableScheduling)
2864 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Chris Lattner0b18e592009-03-17 19:36:00 +00002865
Owen Andersone50ed302009-08-10 22:56:29 +00002866 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002867 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002868
Bill Wendling856ff412009-12-22 00:12:37 +00002869 if (DisableScheduling)
2870 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2871
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002872 // Handle alignment. If the requested alignment is less than or equal to
2873 // the stack alignment, ignore it. If the size is greater than or equal to
2874 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2875 unsigned StackAlign =
2876 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2877 if (Align <= StackAlign)
2878 Align = 0;
2879
2880 // Round the size of the allocation up to the stack alignment size
2881 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002882 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002883 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002884 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002885 if (DisableScheduling)
2886 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2887
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002888 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002889 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002890 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002891 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002892 if (DisableScheduling)
2893 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002894
2895 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002896 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002897 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002898 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002899 setValue(&I, DSA);
2900 DAG.setRoot(DSA.getValue(1));
2901
Bill Wendling856ff412009-12-22 00:12:37 +00002902 if (DisableScheduling)
2903 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
2904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002905 // Inform the Frame Information that we have just allocated a variable-sized
2906 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002907 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002908}
2909
Dan Gohman2048b852009-11-23 18:04:58 +00002910void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002911 const Value *SV = I.getOperand(0);
2912 SDValue Ptr = getValue(SV);
2913
2914 const Type *Ty = I.getType();
2915 bool isVolatile = I.isVolatile();
2916 unsigned Alignment = I.getAlignment();
2917
Owen Andersone50ed302009-08-10 22:56:29 +00002918 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002919 SmallVector<uint64_t, 4> Offsets;
2920 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2921 unsigned NumValues = ValueVTs.size();
2922 if (NumValues == 0)
2923 return;
2924
2925 SDValue Root;
2926 bool ConstantMemory = false;
2927 if (I.isVolatile())
2928 // Serialize volatile loads with other side effects.
2929 Root = getRoot();
2930 else if (AA->pointsToConstantMemory(SV)) {
2931 // Do not serialize (non-volatile) loads of constant memory with anything.
2932 Root = DAG.getEntryNode();
2933 ConstantMemory = true;
2934 } else {
2935 // Do not serialize non-volatile loads against each other.
2936 Root = DAG.getRoot();
2937 }
2938
2939 SmallVector<SDValue, 4> Values(NumValues);
2940 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002941 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002942 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002943 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2944 PtrVT, Ptr,
2945 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002946 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00002947 A, SV, Offsets[i], isVolatile, Alignment);
2948
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002949 Values[i] = L;
2950 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00002951
2952 if (DisableScheduling) {
2953 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
2954 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
2955 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002956 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002957
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002958 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002959 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002960 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002961 if (isVolatile)
2962 DAG.setRoot(Chain);
2963 else
2964 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00002965
2966 if (DisableScheduling)
2967 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002968 }
2969
Bill Wendling856ff412009-12-22 00:12:37 +00002970 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2971 DAG.getVTList(&ValueVTs[0], NumValues),
2972 &Values[0], NumValues);
2973 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002974
Bill Wendling856ff412009-12-22 00:12:37 +00002975 if (DisableScheduling)
2976 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2977}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002978
Dan Gohman2048b852009-11-23 18:04:58 +00002979void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002980 Value *SrcV = I.getOperand(0);
2981 Value *PtrV = I.getOperand(1);
2982
Owen Andersone50ed302009-08-10 22:56:29 +00002983 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002984 SmallVector<uint64_t, 4> Offsets;
2985 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2986 unsigned NumValues = ValueVTs.size();
2987 if (NumValues == 0)
2988 return;
2989
2990 // Get the lowered operands. Note that we do this after
2991 // checking if NumResults is zero, because with zero results
2992 // the operands won't have values in the map.
2993 SDValue Src = getValue(SrcV);
2994 SDValue Ptr = getValue(PtrV);
2995
2996 SDValue Root = getRoot();
2997 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002998 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002999 bool isVolatile = I.isVolatile();
3000 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003001
3002 for (unsigned i = 0; i != NumValues; ++i) {
3003 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3004 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003005 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003006 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003007 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003008
Bill Wendling856ff412009-12-22 00:12:37 +00003009 if (DisableScheduling) {
3010 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3011 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3012 }
3013 }
3014
3015 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3016 MVT::Other, &Chains[0], NumValues);
3017 DAG.setRoot(Res);
3018
3019 if (DisableScheduling)
3020 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003021}
3022
3023/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3024/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003025void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3026 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003027 bool HasChain = !I.doesNotAccessMemory();
3028 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3029
3030 // Build the operand list.
3031 SmallVector<SDValue, 8> Ops;
3032 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3033 if (OnlyLoad) {
3034 // We don't need to serialize loads against other loads.
3035 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003036 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003037 Ops.push_back(getRoot());
3038 }
3039 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003040
3041 // Info is set by getTgtMemInstrinsic
3042 TargetLowering::IntrinsicInfo Info;
3043 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3044
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003045 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003046 if (!IsTgtIntrinsic)
3047 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003048
3049 // Add all operands of the call to the operand list.
3050 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3051 SDValue Op = getValue(I.getOperand(i));
3052 assert(TLI.isTypeLegal(Op.getValueType()) &&
3053 "Intrinsic uses a non-legal type?");
3054 Ops.push_back(Op);
3055 }
3056
Owen Andersone50ed302009-08-10 22:56:29 +00003057 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003058 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3059#ifndef NDEBUG
3060 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3061 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3062 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003063 }
Bob Wilson8d919552009-07-31 22:41:21 +00003064#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003065
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003066 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003067 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003068
Bob Wilson8d919552009-07-31 22:41:21 +00003069 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003070
3071 // Create the node.
3072 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003073 if (IsTgtIntrinsic) {
3074 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003075 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003076 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003077 Info.memVT, Info.ptrVal, Info.offset,
3078 Info.align, Info.vol,
3079 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003080 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003081 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003082 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003083 } else if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003084 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003085 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003086 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003087 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003088 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003089 }
3090
3091 if (DisableScheduling)
3092 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003093
3094 if (HasChain) {
3095 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3096 if (OnlyLoad)
3097 PendingLoads.push_back(Chain);
3098 else
3099 DAG.setRoot(Chain);
3100 }
Bill Wendling856ff412009-12-22 00:12:37 +00003101
Owen Anderson1d0be152009-08-13 21:58:54 +00003102 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003103 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003104 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003105 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003106
3107 if (DisableScheduling)
3108 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003109 }
Bill Wendling856ff412009-12-22 00:12:37 +00003110
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003111 setValue(&I, Result);
3112 }
3113}
3114
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003115/// GetSignificand - Get the significand and build it into a floating-point
3116/// number with exponent of 1:
3117///
3118/// Op = (Op & 0x007fffff) | 0x3f800000;
3119///
3120/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003121static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003122GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003123 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3124 DAG.getConstant(0x007fffff, MVT::i32));
3125 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3126 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003127 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3128
3129 if (DisableScheduling) {
3130 DAG.AssignOrdering(t1.getNode(), Order);
3131 DAG.AssignOrdering(t2.getNode(), Order);
3132 DAG.AssignOrdering(Res.getNode(), Order);
3133 }
3134
3135 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003136}
3137
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003138/// GetExponent - Get the exponent:
3139///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003140/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003141///
3142/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003143static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003144GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003145 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003146 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3147 DAG.getConstant(0x7f800000, MVT::i32));
3148 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003149 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3151 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003152 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3153
3154 if (DisableScheduling) {
3155 DAG.AssignOrdering(t0.getNode(), Order);
3156 DAG.AssignOrdering(t1.getNode(), Order);
3157 DAG.AssignOrdering(t2.getNode(), Order);
3158 DAG.AssignOrdering(Res.getNode(), Order);
3159 }
3160
3161 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003162}
3163
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003164/// getF32Constant - Get 32-bit floating point constant.
3165static SDValue
3166getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003167 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003168}
3169
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003170/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003171/// visitIntrinsicCall: I is a call instruction
3172/// Op is the associated NodeType for I
3173const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003174SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003175 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003176 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003177 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003178 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003179 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003180 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003181 getValue(I.getOperand(2)),
3182 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003183 setValue(&I, L);
3184 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003185
3186 if (DisableScheduling)
3187 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003189 return 0;
3190}
3191
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003192// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003193const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003194SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003195 SDValue Op1 = getValue(I.getOperand(1));
3196 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003197
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003199 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003200
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003201 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003202
3203 if (DisableScheduling)
3204 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3205
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003206 return 0;
3207}
Bill Wendling74c37652008-12-09 22:08:41 +00003208
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003209/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3210/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003211void
Dan Gohman2048b852009-11-23 18:04:58 +00003212SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003213 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003214 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003215
Owen Anderson825b72b2009-08-11 20:47:22 +00003216 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003217 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3218 SDValue Op = getValue(I.getOperand(1));
3219
3220 // Put the exponent in the right bit position for later addition to the
3221 // final result:
3222 //
3223 // #define LOG2OFe 1.4426950f
3224 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003225 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003226 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003227 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003228
3229 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003230 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3231 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003232
Bill Wendling856ff412009-12-22 00:12:37 +00003233 if (DisableScheduling) {
3234 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3235 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3236 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3237 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3238 }
3239
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003240 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003241 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003242 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003243
Bill Wendling856ff412009-12-22 00:12:37 +00003244 if (DisableScheduling)
3245 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3246
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003247 if (LimitFloatPrecision <= 6) {
3248 // For floating-point precision of 6:
3249 //
3250 // TwoToFractionalPartOfX =
3251 // 0.997535578f +
3252 // (0.735607626f + 0.252464424f * x) * x;
3253 //
3254 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003255 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003256 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003258 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3260 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003261 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003262 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003263
3264 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003265 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003266 TwoToFracPartOfX, IntegerPartOfX);
3267
Owen Anderson825b72b2009-08-11 20:47:22 +00003268 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003269
3270 if (DisableScheduling) {
3271 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3272 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3273 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3274 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3275 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3276 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3277 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3278 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003279 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3280 // For floating-point precision of 12:
3281 //
3282 // TwoToFractionalPartOfX =
3283 // 0.999892986f +
3284 // (0.696457318f +
3285 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3286 //
3287 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003288 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003289 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003290 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003291 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3293 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003294 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003295 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3296 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003297 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003299
3300 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003301 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003302 TwoToFracPartOfX, IntegerPartOfX);
3303
Owen Anderson825b72b2009-08-11 20:47:22 +00003304 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003305
3306 if (DisableScheduling) {
3307 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3308 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3309 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3310 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3311 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3312 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3313 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3314 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3315 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3316 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003317 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3318 // For floating-point precision of 18:
3319 //
3320 // TwoToFractionalPartOfX =
3321 // 0.999999982f +
3322 // (0.693148872f +
3323 // (0.240227044f +
3324 // (0.554906021e-1f +
3325 // (0.961591928e-2f +
3326 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3327 //
3328 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003330 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003332 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003333 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3334 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003335 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3337 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003338 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3340 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003341 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3343 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003344 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3346 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003347 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003348 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003349 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003350
3351 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003353 TwoToFracPartOfX, IntegerPartOfX);
3354
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003356
3357 if (DisableScheduling) {
3358 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3359 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3360 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3361 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3362 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3363 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3364 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3365 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3366 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3367 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3368 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3369 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3370 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3371 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3372 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3373 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003374 }
3375 } else {
3376 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003377 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003378 getValue(I.getOperand(1)).getValueType(),
3379 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003380 if (DisableScheduling)
3381 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003382 }
3383
Dale Johannesen59e577f2008-09-05 18:38:42 +00003384 setValue(&I, result);
3385}
3386
Bill Wendling39150252008-09-09 20:39:27 +00003387/// visitLog - Lower a log intrinsic. Handles the special sequences for
3388/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003389void
Dan Gohman2048b852009-11-23 18:04:58 +00003390SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003391 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003392 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003393
Owen Anderson825b72b2009-08-11 20:47:22 +00003394 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003395 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3396 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003398
Bill Wendling856ff412009-12-22 00:12:37 +00003399 if (DisableScheduling)
3400 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3401
Bill Wendling39150252008-09-09 20:39:27 +00003402 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003403 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003404 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003405 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003406
Bill Wendling856ff412009-12-22 00:12:37 +00003407 if (DisableScheduling)
3408 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3409
Bill Wendling39150252008-09-09 20:39:27 +00003410 // Get the significand and build it into a floating-point number with
3411 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003412 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003413
3414 if (LimitFloatPrecision <= 6) {
3415 // For floating-point precision of 6:
3416 //
3417 // LogofMantissa =
3418 // -1.1609546f +
3419 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003420 //
Bill Wendling39150252008-09-09 20:39:27 +00003421 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003423 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003425 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003426 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3427 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003428 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003429
Scott Michelfdc40a02009-02-17 22:15:04 +00003430 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003431 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003432
3433 if (DisableScheduling) {
3434 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3435 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3436 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3437 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3438 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3439 }
Bill Wendling39150252008-09-09 20:39:27 +00003440 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3441 // For floating-point precision of 12:
3442 //
3443 // LogOfMantissa =
3444 // -1.7417939f +
3445 // (2.8212026f +
3446 // (-1.4699568f +
3447 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3448 //
3449 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003451 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003452 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003453 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003454 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3455 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003456 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003457 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3458 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003459 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003460 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3461 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003462 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003463
Scott Michelfdc40a02009-02-17 22:15:04 +00003464 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003466
3467 if (DisableScheduling) {
3468 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3469 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3470 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3471 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3472 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3473 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3474 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3475 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3476 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3477 }
Bill Wendling39150252008-09-09 20:39:27 +00003478 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3479 // For floating-point precision of 18:
3480 //
3481 // LogOfMantissa =
3482 // -2.1072184f +
3483 // (4.2372794f +
3484 // (-3.7029485f +
3485 // (2.2781945f +
3486 // (-0.87823314f +
3487 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3488 //
3489 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003490 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003491 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003492 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003493 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003494 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3495 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003496 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3498 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003499 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003500 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3501 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003502 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003503 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3504 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003505 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003506 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3507 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003508 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003509
Scott Michelfdc40a02009-02-17 22:15:04 +00003510 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003512
3513 if (DisableScheduling) {
3514 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3515 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3516 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3517 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3518 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3519 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3520 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3521 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3522 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3523 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3524 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3525 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3526 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3527 }
Bill Wendling39150252008-09-09 20:39:27 +00003528 }
3529 } else {
3530 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003531 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003532 getValue(I.getOperand(1)).getValueType(),
3533 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003534
3535 if (DisableScheduling)
3536 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003537 }
3538
Dale Johannesen59e577f2008-09-05 18:38:42 +00003539 setValue(&I, result);
3540}
3541
Bill Wendling3eb59402008-09-09 00:28:24 +00003542/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3543/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003544void
Dan Gohman2048b852009-11-23 18:04:58 +00003545SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003546 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003547 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003548
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003550 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3551 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003553
Bill Wendling856ff412009-12-22 00:12:37 +00003554 if (DisableScheduling)
3555 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3556
Bill Wendling39150252008-09-09 20:39:27 +00003557 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003558 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3559
3560 if (DisableScheduling)
3561 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003562
3563 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003564 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003565 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003566
Bill Wendling3eb59402008-09-09 00:28:24 +00003567 // Different possible minimax approximations of significand in
3568 // floating-point for various degrees of accuracy over [1,2].
3569 if (LimitFloatPrecision <= 6) {
3570 // For floating-point precision of 6:
3571 //
3572 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3573 //
3574 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003575 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003576 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003578 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3580 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003581 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003582
Scott Michelfdc40a02009-02-17 22:15:04 +00003583 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003584 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003585
3586 if (DisableScheduling) {
3587 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3588 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3589 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3590 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3591 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3592 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003593 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3594 // For floating-point precision of 12:
3595 //
3596 // Log2ofMantissa =
3597 // -2.51285454f +
3598 // (4.07009056f +
3599 // (-2.12067489f +
3600 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003601 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003602 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003603 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003604 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003605 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003606 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003607 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3608 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003609 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003610 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3611 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003612 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003613 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3614 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003615 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003616
Scott Michelfdc40a02009-02-17 22:15:04 +00003617 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003618 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003619
3620 if (DisableScheduling) {
3621 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3622 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3623 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3624 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3625 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3626 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3627 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3628 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3629 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3630 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003631 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3632 // For floating-point precision of 18:
3633 //
3634 // Log2ofMantissa =
3635 // -3.0400495f +
3636 // (6.1129976f +
3637 // (-5.3420409f +
3638 // (3.2865683f +
3639 // (-1.2669343f +
3640 // (0.27515199f -
3641 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3642 //
3643 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003644 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003645 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003646 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003647 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003648 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3649 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003650 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003651 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3652 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003653 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003654 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3655 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003656 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003657 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3658 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003659 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003660 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3661 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003662 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003663
Scott Michelfdc40a02009-02-17 22:15:04 +00003664 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003665 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003666
3667 if (DisableScheduling) {
3668 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3669 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3670 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3671 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3672 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3673 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3674 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3675 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3676 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3677 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3678 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3679 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3680 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3681 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003682 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003683 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003684 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003685 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003686 getValue(I.getOperand(1)).getValueType(),
3687 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003688
3689 if (DisableScheduling)
3690 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003691 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003692
Dale Johannesen59e577f2008-09-05 18:38:42 +00003693 setValue(&I, result);
3694}
3695
Bill Wendling3eb59402008-09-09 00:28:24 +00003696/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3697/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003698void
Dan Gohman2048b852009-11-23 18:04:58 +00003699SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003700 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003701 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003702
Owen Anderson825b72b2009-08-11 20:47:22 +00003703 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003704 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3705 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003706 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003707
Bill Wendling856ff412009-12-22 00:12:37 +00003708 if (DisableScheduling)
3709 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3710
Bill Wendling39150252008-09-09 20:39:27 +00003711 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003712 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003713 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003714 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003715
Bill Wendling856ff412009-12-22 00:12:37 +00003716 if (DisableScheduling)
3717 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3718
Bill Wendling3eb59402008-09-09 00:28:24 +00003719 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003720 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003721 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003722
3723 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003724 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003725 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003726 // Log10ofMantissa =
3727 // -0.50419619f +
3728 // (0.60948995f - 0.10380950f * x) * x;
3729 //
3730 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003731 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003732 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003733 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003734 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003735 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3736 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003737 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003738
Scott Michelfdc40a02009-02-17 22:15:04 +00003739 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003740 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003741
3742 if (DisableScheduling) {
3743 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3744 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3745 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3746 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3747 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3748 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003749 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3750 // For floating-point precision of 12:
3751 //
3752 // Log10ofMantissa =
3753 // -0.64831180f +
3754 // (0.91751397f +
3755 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3756 //
3757 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003758 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003759 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003760 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003761 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003762 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3763 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003764 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003765 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3766 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003767 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003768
Scott Michelfdc40a02009-02-17 22:15:04 +00003769 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003770 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003771
3772 if (DisableScheduling) {
3773 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3774 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3775 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3776 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3777 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3778 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3779 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3780 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003781 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003782 // For floating-point precision of 18:
3783 //
3784 // Log10ofMantissa =
3785 // -0.84299375f +
3786 // (1.5327582f +
3787 // (-1.0688956f +
3788 // (0.49102474f +
3789 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3790 //
3791 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003792 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003793 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003794 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003795 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003796 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3797 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003798 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3800 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003801 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3803 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003804 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3806 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003807 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003808
Scott Michelfdc40a02009-02-17 22:15:04 +00003809 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003810 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003811
3812 if (DisableScheduling) {
3813 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3814 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3815 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3816 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3817 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3818 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3819 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3820 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3821 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3822 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3823 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3824 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003825 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003826 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003827 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003828 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003829 getValue(I.getOperand(1)).getValueType(),
3830 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003831
3832 if (DisableScheduling)
3833 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003834 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003835
Dale Johannesen59e577f2008-09-05 18:38:42 +00003836 setValue(&I, result);
3837}
3838
Bill Wendlinge10c8142008-09-09 22:39:21 +00003839/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3840/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003841void
Dan Gohman2048b852009-11-23 18:04:58 +00003842SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003843 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003844 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003845
Owen Anderson825b72b2009-08-11 20:47:22 +00003846 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003847 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3848 SDValue Op = getValue(I.getOperand(1));
3849
Owen Anderson825b72b2009-08-11 20:47:22 +00003850 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003851
Bill Wendling856ff412009-12-22 00:12:37 +00003852 if (DisableScheduling)
3853 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3854
Bill Wendlinge10c8142008-09-09 22:39:21 +00003855 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3857 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003858
3859 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003860 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003861 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003862
Bill Wendling856ff412009-12-22 00:12:37 +00003863 if (DisableScheduling) {
3864 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3865 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3866 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3867 }
3868
Bill Wendlinge10c8142008-09-09 22:39:21 +00003869 if (LimitFloatPrecision <= 6) {
3870 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003871 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003872 // TwoToFractionalPartOfX =
3873 // 0.997535578f +
3874 // (0.735607626f + 0.252464424f * x) * x;
3875 //
3876 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003877 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003878 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003879 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003880 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003881 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3882 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003883 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003884 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003885 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003887
Scott Michelfdc40a02009-02-17 22:15:04 +00003888 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003889 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003890
3891 if (DisableScheduling) {
3892 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3893 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3894 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3895 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3896 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3897 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3898 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3899 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003900 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3901 // For floating-point precision of 12:
3902 //
3903 // TwoToFractionalPartOfX =
3904 // 0.999892986f +
3905 // (0.696457318f +
3906 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3907 //
3908 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003909 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003910 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003911 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003912 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003913 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3914 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003915 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003916 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3917 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003918 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003919 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003920 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003921 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003922
Scott Michelfdc40a02009-02-17 22:15:04 +00003923 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003924 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003925
3926 if (DisableScheduling) {
3927 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3928 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3929 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3930 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3931 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3932 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3933 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3934 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3935 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3936 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003937 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3938 // For floating-point precision of 18:
3939 //
3940 // TwoToFractionalPartOfX =
3941 // 0.999999982f +
3942 // (0.693148872f +
3943 // (0.240227044f +
3944 // (0.554906021e-1f +
3945 // (0.961591928e-2f +
3946 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3947 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003948 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003949 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003950 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003951 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003952 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3953 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003954 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003955 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3956 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003957 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3959 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003960 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3962 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003963 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3965 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003966 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003967 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003968 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003969 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003970
Scott Michelfdc40a02009-02-17 22:15:04 +00003971 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003972 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003973
3974 if (DisableScheduling) {
3975 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3976 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3977 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3978 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3979 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3980 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3981 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3982 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3983 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3984 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3985 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3986 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3987 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3988 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3989 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3990 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003991 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003992 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003993 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003994 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003995 getValue(I.getOperand(1)).getValueType(),
3996 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003997
3998 if (DisableScheduling)
3999 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004000 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004001
Dale Johannesen601d3c02008-09-05 01:48:15 +00004002 setValue(&I, result);
4003}
4004
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004005/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4006/// limited-precision mode with x == 10.0f.
4007void
Dan Gohman2048b852009-11-23 18:04:58 +00004008SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004009 SDValue result;
4010 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004011 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004012 bool IsExp10 = false;
4013
Owen Anderson825b72b2009-08-11 20:47:22 +00004014 if (getValue(Val).getValueType() == MVT::f32 &&
4015 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004016 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4017 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4018 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4019 APFloat Ten(10.0f);
4020 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4021 }
4022 }
4023 }
4024
4025 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4026 SDValue Op = getValue(I.getOperand(2));
4027
4028 // Put the exponent in the right bit position for later addition to the
4029 // final result:
4030 //
4031 // #define LOG2OF10 3.3219281f
4032 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004034 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004036
4037 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004038 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4039 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004040
Bill Wendling856ff412009-12-22 00:12:37 +00004041 if (DisableScheduling) {
4042 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4043 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4044 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4045 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4046 }
4047
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004048 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004049 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004050 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004051
Bill Wendling856ff412009-12-22 00:12:37 +00004052 if (DisableScheduling)
4053 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4054
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004055 if (LimitFloatPrecision <= 6) {
4056 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004057 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004058 // twoToFractionalPartOfX =
4059 // 0.997535578f +
4060 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004061 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004062 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004063 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004064 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004065 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004066 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004067 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4068 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004069 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004070 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004071 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004072 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004073
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004074 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004075 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004076
4077 if (DisableScheduling) {
4078 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4079 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4080 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4081 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4082 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4083 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4084 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4085 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004086 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4087 // For floating-point precision of 12:
4088 //
4089 // TwoToFractionalPartOfX =
4090 // 0.999892986f +
4091 // (0.696457318f +
4092 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4093 //
4094 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004095 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004096 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004097 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004098 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004099 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4100 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004101 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004102 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4103 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004104 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004105 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004106 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004107 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004108
Scott Michelfdc40a02009-02-17 22:15:04 +00004109 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004111
4112 if (DisableScheduling) {
4113 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4114 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4115 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4116 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4117 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4118 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4119 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4120 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4121 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4122 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004123 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4124 // For floating-point precision of 18:
4125 //
4126 // TwoToFractionalPartOfX =
4127 // 0.999999982f +
4128 // (0.693148872f +
4129 // (0.240227044f +
4130 // (0.554906021e-1f +
4131 // (0.961591928e-2f +
4132 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4133 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004134 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004135 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004136 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004137 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4139 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004140 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004141 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4142 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004143 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4145 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004146 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4148 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004149 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004150 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4151 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004152 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004153 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004154 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004155 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004156
Scott Michelfdc40a02009-02-17 22:15:04 +00004157 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004158 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004159
4160 if (DisableScheduling) {
4161 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4162 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4163 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4164 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4165 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4166 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4167 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4168 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4169 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4170 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4171 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4172 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4173 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4174 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4175 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4176 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004177 }
4178 } else {
4179 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004180 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004181 getValue(I.getOperand(1)).getValueType(),
4182 getValue(I.getOperand(1)),
4183 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004184
4185 if (DisableScheduling)
4186 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004187 }
4188
4189 setValue(&I, result);
4190}
4191
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004192/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4193/// we want to emit this as a call to a named external function, return the name
4194/// otherwise lower it and return null.
4195const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004196SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004197 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004198 SDValue Res;
4199
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004200 switch (Intrinsic) {
4201 default:
4202 // By default, turn this into a target intrinsic node.
4203 visitTargetIntrinsic(I, Intrinsic);
4204 return 0;
4205 case Intrinsic::vastart: visitVAStart(I); return 0;
4206 case Intrinsic::vaend: visitVAEnd(I); return 0;
4207 case Intrinsic::vacopy: visitVACopy(I); return 0;
4208 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004209 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4210 getValue(I.getOperand(1)));
4211 setValue(&I, Res);
4212 if (DisableScheduling)
4213 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004214 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004215 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004216 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4217 getValue(I.getOperand(1)));
4218 setValue(&I, Res);
4219 if (DisableScheduling)
4220 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004221 return 0;
4222 case Intrinsic::setjmp:
4223 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004224 case Intrinsic::longjmp:
4225 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004226 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004227 SDValue Op1 = getValue(I.getOperand(1));
4228 SDValue Op2 = getValue(I.getOperand(2));
4229 SDValue Op3 = getValue(I.getOperand(3));
4230 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004231 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4232 I.getOperand(1), 0, I.getOperand(2), 0);
4233 DAG.setRoot(Res);
4234 if (DisableScheduling)
4235 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004236 return 0;
4237 }
Chris Lattner824b9582008-11-21 16:42:48 +00004238 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004239 SDValue Op1 = getValue(I.getOperand(1));
4240 SDValue Op2 = getValue(I.getOperand(2));
4241 SDValue Op3 = getValue(I.getOperand(3));
4242 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004243 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4244 I.getOperand(1), 0);
4245 DAG.setRoot(Res);
4246 if (DisableScheduling)
4247 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004248 return 0;
4249 }
Chris Lattner824b9582008-11-21 16:42:48 +00004250 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004251 SDValue Op1 = getValue(I.getOperand(1));
4252 SDValue Op2 = getValue(I.getOperand(2));
4253 SDValue Op3 = getValue(I.getOperand(3));
4254 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4255
4256 // If the source and destination are known to not be aliases, we can
4257 // lower memmove as memcpy.
4258 uint64_t Size = -1ULL;
4259 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004260 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004261 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4262 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004263 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4264 I.getOperand(1), 0, I.getOperand(2), 0);
4265 DAG.setRoot(Res);
4266 if (DisableScheduling)
4267 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004268 return 0;
4269 }
4270
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004271 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4272 I.getOperand(1), 0, I.getOperand(2), 0);
4273 DAG.setRoot(Res);
4274 if (DisableScheduling)
4275 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004276 return 0;
4277 }
Devang Patel70d75ca2009-11-12 19:02:56 +00004278 case Intrinsic::dbg_stoppoint:
4279 case Intrinsic::dbg_region_start:
4280 case Intrinsic::dbg_region_end:
4281 case Intrinsic::dbg_func_start:
4282 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004283 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004284 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00004285 if (OptLevel != CodeGenOpt::None)
4286 // FIXME: Variable debug info is not supported here.
4287 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004288 DwarfWriter *DW = DAG.getDwarfWriter();
4289 if (!DW)
4290 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004291 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
4292 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
4293 return 0;
4294
Devang Patelac1ceb32009-10-09 22:42:28 +00004295 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004296 Value *Address = DI.getAddress();
4297 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4298 Address = BCI->getOperand(0);
4299 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4300 // Don't handle byval struct arguments or VLAs, for example.
4301 if (!AI)
4302 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004303 DenseMap<const AllocaInst*, int>::iterator SI =
4304 FuncInfo.StaticAllocaMap.find(AI);
4305 if (SI == FuncInfo.StaticAllocaMap.end())
4306 return 0; // VLAs.
4307 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004308
Devang Patelac1ceb32009-10-09 22:42:28 +00004309 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00004310 if (MMI) {
4311 MetadataContext &TheMetadata =
4312 DI.getParent()->getContext().getMetadata();
4313 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
4314 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
4315 MMI->setVariableDbgInfo(Variable, FI, Dbg);
4316 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004317 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004318 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004319 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004320 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004321 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004322 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004323 SDValue Ops[1];
4324 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004325 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004326 setValue(&I, Op);
4327 DAG.setRoot(Op.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004328 if (DisableScheduling)
4329 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004330 return 0;
4331 }
4332
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004333 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004334 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004335
Chris Lattner3a5815f2009-09-17 23:54:54 +00004336 if (CurMBB->isLandingPad())
4337 AddCatchInfo(I, MMI, CurMBB);
4338 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004339#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004340 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004341#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004342 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4343 unsigned Reg = TLI.getExceptionSelectorRegister();
4344 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004345 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004346
Chris Lattner3a5815f2009-09-17 23:54:54 +00004347 // Insert the EHSELECTION instruction.
4348 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4349 SDValue Ops[2];
4350 Ops[0] = getValue(I.getOperand(1));
4351 Ops[1] = getRoot();
4352 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4353
4354 DAG.setRoot(Op.getValue(1));
4355
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004356 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4357 setValue(&I, Res);
4358 if (DisableScheduling) {
4359 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4360 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4361 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004362 return 0;
4363 }
4364
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004365 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004366 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004367
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004368 if (MMI) {
4369 // Find the type id for the given typeinfo.
4370 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004371 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004372 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004373 } else {
4374 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004375 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004376 }
4377
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004378 setValue(&I, Res);
4379 if (DisableScheduling)
4380 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004381 return 0;
4382 }
4383
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004384 case Intrinsic::eh_return_i32:
4385 case Intrinsic::eh_return_i64:
4386 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004387 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004388 Res = DAG.getNode(ISD::EH_RETURN, dl,
4389 MVT::Other,
4390 getControlRoot(),
4391 getValue(I.getOperand(1)),
4392 getValue(I.getOperand(2)));
4393 DAG.setRoot(Res);
4394 if (DisableScheduling)
4395 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004396 } else {
4397 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4398 }
4399
4400 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004401 case Intrinsic::eh_unwind_init:
4402 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4403 MMI->setCallsUnwindInit(true);
4404 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004405 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004406 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004407 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004408 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4409 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004410 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004411 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004412 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004413 TLI.getPointerTy()),
4414 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004415 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004416 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004417 DAG.getConstant(0, TLI.getPointerTy()));
4418 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4419 FA, Offset);
4420 setValue(&I, Res);
4421 if (DisableScheduling) {
4422 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4423 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4424 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4425 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4426 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004427 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004428 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004429 case Intrinsic::convertff:
4430 case Intrinsic::convertfsi:
4431 case Intrinsic::convertfui:
4432 case Intrinsic::convertsif:
4433 case Intrinsic::convertuif:
4434 case Intrinsic::convertss:
4435 case Intrinsic::convertsu:
4436 case Intrinsic::convertus:
4437 case Intrinsic::convertuu: {
4438 ISD::CvtCode Code = ISD::CVT_INVALID;
4439 switch (Intrinsic) {
4440 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4441 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4442 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4443 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4444 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4445 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4446 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4447 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4448 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4449 }
Owen Andersone50ed302009-08-10 22:56:29 +00004450 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004451 Value *Op1 = I.getOperand(1);
4452 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4453 DAG.getValueType(DestVT),
4454 DAG.getValueType(getValue(Op1).getValueType()),
4455 getValue(I.getOperand(2)),
4456 getValue(I.getOperand(3)),
4457 Code);
4458 setValue(&I, Res);
4459 if (DisableScheduling)
4460 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004461 return 0;
4462 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004463 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004464 Res = DAG.getNode(ISD::FSQRT, dl,
4465 getValue(I.getOperand(1)).getValueType(),
4466 getValue(I.getOperand(1)));
4467 setValue(&I, Res);
4468 if (DisableScheduling)
4469 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004470 return 0;
4471 case Intrinsic::powi:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004472 Res = DAG.getNode(ISD::FPOWI, dl,
4473 getValue(I.getOperand(1)).getValueType(),
4474 getValue(I.getOperand(1)),
4475 getValue(I.getOperand(2)));
4476 setValue(&I, Res);
4477 if (DisableScheduling)
4478 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004479 return 0;
4480 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004481 Res = DAG.getNode(ISD::FSIN, dl,
4482 getValue(I.getOperand(1)).getValueType(),
4483 getValue(I.getOperand(1)));
4484 setValue(&I, Res);
4485 if (DisableScheduling)
4486 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004487 return 0;
4488 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004489 Res = DAG.getNode(ISD::FCOS, dl,
4490 getValue(I.getOperand(1)).getValueType(),
4491 getValue(I.getOperand(1)));
4492 setValue(&I, Res);
4493 if (DisableScheduling)
4494 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004495 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004496 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004497 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004498 return 0;
4499 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004500 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004501 return 0;
4502 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004503 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004504 return 0;
4505 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004506 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004507 return 0;
4508 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004509 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004510 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004511 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004512 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004513 return 0;
4514 case Intrinsic::pcmarker: {
4515 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004516 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4517 DAG.setRoot(Res);
4518 if (DisableScheduling)
4519 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004520 return 0;
4521 }
4522 case Intrinsic::readcyclecounter: {
4523 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004524 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4525 DAG.getVTList(MVT::i64, MVT::Other),
4526 &Op, 1);
4527 setValue(&I, Res);
4528 DAG.setRoot(Res.getValue(1));
4529 if (DisableScheduling)
4530 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004531 return 0;
4532 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004533 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004534 Res = DAG.getNode(ISD::BSWAP, dl,
4535 getValue(I.getOperand(1)).getValueType(),
4536 getValue(I.getOperand(1)));
4537 setValue(&I, Res);
4538 if (DisableScheduling)
4539 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004540 return 0;
4541 case Intrinsic::cttz: {
4542 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004543 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004544 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4545 setValue(&I, Res);
4546 if (DisableScheduling)
4547 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004548 return 0;
4549 }
4550 case Intrinsic::ctlz: {
4551 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004552 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004553 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4554 setValue(&I, Res);
4555 if (DisableScheduling)
4556 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004557 return 0;
4558 }
4559 case Intrinsic::ctpop: {
4560 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004561 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004562 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4563 setValue(&I, Res);
4564 if (DisableScheduling)
4565 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004566 return 0;
4567 }
4568 case Intrinsic::stacksave: {
4569 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004570 Res = DAG.getNode(ISD::STACKSAVE, dl,
4571 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4572 setValue(&I, Res);
4573 DAG.setRoot(Res.getValue(1));
4574 if (DisableScheduling)
4575 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004576 return 0;
4577 }
4578 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004579 Res = getValue(I.getOperand(1));
4580 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4581 DAG.setRoot(Res);
4582 if (DisableScheduling)
4583 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004584 return 0;
4585 }
Bill Wendling57344502008-11-18 11:01:33 +00004586 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004587 // Emit code into the DAG to store the stack guard onto the stack.
4588 MachineFunction &MF = DAG.getMachineFunction();
4589 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004590 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004591
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004592 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4593 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004594
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004595 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004596 MFI->setStackProtectorIndex(FI);
4597
4598 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4599
4600 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004601 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4602 PseudoSourceValue::getFixedStack(FI),
4603 0, true);
4604 setValue(&I, Res);
4605 DAG.setRoot(Res);
4606 if (DisableScheduling)
4607 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004608 return 0;
4609 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004610 case Intrinsic::objectsize: {
4611 // If we don't know by now, we're never going to know.
4612 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4613
4614 assert(CI && "Non-constant type in __builtin_object_size?");
4615
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004616 SDValue Arg = getValue(I.getOperand(0));
4617 EVT Ty = Arg.getValueType();
4618
Eric Christopher7b5e6172009-10-27 00:52:25 +00004619 if (CI->getZExtValue() < 2)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004620 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004621 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004622 Res = DAG.getConstant(0, Ty);
4623
4624 setValue(&I, Res);
4625 if (DisableScheduling)
4626 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004627 return 0;
4628 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004629 case Intrinsic::var_annotation:
4630 // Discard annotate attributes
4631 return 0;
4632
4633 case Intrinsic::init_trampoline: {
4634 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4635
4636 SDValue Ops[6];
4637 Ops[0] = getRoot();
4638 Ops[1] = getValue(I.getOperand(1));
4639 Ops[2] = getValue(I.getOperand(2));
4640 Ops[3] = getValue(I.getOperand(3));
4641 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4642 Ops[5] = DAG.getSrcValue(F);
4643
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004644 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4645 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4646 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004647
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004648 setValue(&I, Res);
4649 DAG.setRoot(Res.getValue(1));
4650 if (DisableScheduling)
4651 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004652 return 0;
4653 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004654 case Intrinsic::gcroot:
4655 if (GFI) {
4656 Value *Alloca = I.getOperand(1);
4657 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004658
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004659 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4660 GFI->addStackRoot(FI->getIndex(), TypeMap);
4661 }
4662 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004663 case Intrinsic::gcread:
4664 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004665 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004666 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004667 case Intrinsic::flt_rounds:
4668 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4669 setValue(&I, Res);
4670 if (DisableScheduling)
4671 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004672 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004673 case Intrinsic::trap:
4674 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4675 DAG.setRoot(Res);
4676 if (DisableScheduling)
4677 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004678 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004679 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004680 return implVisitAluOverflow(I, ISD::UADDO);
4681 case Intrinsic::sadd_with_overflow:
4682 return implVisitAluOverflow(I, ISD::SADDO);
4683 case Intrinsic::usub_with_overflow:
4684 return implVisitAluOverflow(I, ISD::USUBO);
4685 case Intrinsic::ssub_with_overflow:
4686 return implVisitAluOverflow(I, ISD::SSUBO);
4687 case Intrinsic::umul_with_overflow:
4688 return implVisitAluOverflow(I, ISD::UMULO);
4689 case Intrinsic::smul_with_overflow:
4690 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004691
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004692 case Intrinsic::prefetch: {
4693 SDValue Ops[4];
4694 Ops[0] = getRoot();
4695 Ops[1] = getValue(I.getOperand(1));
4696 Ops[2] = getValue(I.getOperand(2));
4697 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004698 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4699 DAG.setRoot(Res);
4700 if (DisableScheduling)
4701 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004702 return 0;
4703 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004705 case Intrinsic::memory_barrier: {
4706 SDValue Ops[6];
4707 Ops[0] = getRoot();
4708 for (int x = 1; x < 6; ++x)
4709 Ops[x] = getValue(I.getOperand(x));
4710
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004711 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4712 DAG.setRoot(Res);
4713 if (DisableScheduling)
4714 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004715 return 0;
4716 }
4717 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004718 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004719 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004720 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004721 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4722 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004723 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004724 getValue(I.getOperand(2)),
4725 getValue(I.getOperand(3)),
4726 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004727 setValue(&I, L);
4728 DAG.setRoot(L.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004729 if (DisableScheduling)
4730 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004731 return 0;
4732 }
4733 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004734 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004735 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004736 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004737 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004738 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004739 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004740 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004741 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004742 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004743 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004744 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004745 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004746 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004747 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004748 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004749 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004750 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004751 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004752 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004753 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004754 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004755
4756 case Intrinsic::invariant_start:
4757 case Intrinsic::lifetime_start:
4758 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004759 Res = DAG.getUNDEF(TLI.getPointerTy());
4760 setValue(&I, Res);
4761 if (DisableScheduling)
4762 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004763 return 0;
4764 case Intrinsic::invariant_end:
4765 case Intrinsic::lifetime_end:
4766 // Discard region information.
4767 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 }
4769}
4770
Dan Gohman98ca4f22009-08-05 01:29:28 +00004771/// Test if the given instruction is in a position to be optimized
4772/// with a tail-call. This roughly means that it's in a block with
4773/// a return and there's nothing that needs to be scheduled
4774/// between it and the return.
4775///
4776/// This function only tests target-independent requirements.
4777/// For target-dependent requirements, a target should override
4778/// TargetLowering::IsEligibleForTailCallOptimization.
4779///
4780static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004781isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004782 const TargetLowering &TLI) {
4783 const BasicBlock *ExitBB = I->getParent();
4784 const TerminatorInst *Term = ExitBB->getTerminator();
4785 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4786 const Function *F = ExitBB->getParent();
4787
4788 // The block must end in a return statement or an unreachable.
4789 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4790
4791 // If I will have a chain, make sure no other instruction that will have a
4792 // chain interposes between I and the return.
4793 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4794 !I->isSafeToSpeculativelyExecute())
4795 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4796 --BBI) {
4797 if (&*BBI == I)
4798 break;
4799 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4800 !BBI->isSafeToSpeculativelyExecute())
4801 return false;
4802 }
4803
4804 // If the block ends with a void return or unreachable, it doesn't matter
4805 // what the call's return type is.
4806 if (!Ret || Ret->getNumOperands() == 0) return true;
4807
Dan Gohmaned9bab32009-11-14 02:06:30 +00004808 // If the return value is undef, it doesn't matter what the call's
4809 // return type is.
4810 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4811
Dan Gohman98ca4f22009-08-05 01:29:28 +00004812 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004813 // the return. Ignore noalias because it doesn't affect the call sequence.
4814 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4815 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004816 return false;
4817
4818 // Otherwise, make sure the unmodified return value of I is the return value.
4819 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4820 U = dyn_cast<Instruction>(U->getOperand(0))) {
4821 if (!U)
4822 return false;
4823 if (!U->hasOneUse())
4824 return false;
4825 if (U == I)
4826 break;
4827 // Check for a truly no-op truncate.
4828 if (isa<TruncInst>(U) &&
4829 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4830 continue;
4831 // Check for a truly no-op bitcast.
4832 if (isa<BitCastInst>(U) &&
4833 (U->getOperand(0)->getType() == U->getType() ||
4834 (isa<PointerType>(U->getOperand(0)->getType()) &&
4835 isa<PointerType>(U->getType()))))
4836 continue;
4837 // Otherwise it's not a true no-op.
4838 return false;
4839 }
4840
4841 return true;
4842}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004843
Dan Gohman2048b852009-11-23 18:04:58 +00004844void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4845 bool isTailCall,
4846 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004847 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4848 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004849 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004850 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4851 unsigned BeginLabel = 0, EndLabel = 0;
4852
4853 TargetLowering::ArgListTy Args;
4854 TargetLowering::ArgListEntry Entry;
4855 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004856
4857 // Check whether the function can return without sret-demotion.
4858 SmallVector<EVT, 4> OutVTs;
4859 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4860 SmallVector<uint64_t, 4> Offsets;
4861 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004862 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004863
4864 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4865 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4866
4867 SDValue DemoteStackSlot;
4868
4869 if (!CanLowerReturn) {
4870 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4871 FTy->getReturnType());
4872 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4873 FTy->getReturnType());
4874 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004875 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004876 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4877
4878 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4879 Entry.Node = DemoteStackSlot;
4880 Entry.Ty = StackSlotPtrType;
4881 Entry.isSExt = false;
4882 Entry.isZExt = false;
4883 Entry.isInReg = false;
4884 Entry.isSRet = true;
4885 Entry.isNest = false;
4886 Entry.isByVal = false;
4887 Entry.Alignment = Align;
4888 Args.push_back(Entry);
4889 RetTy = Type::getVoidTy(FTy->getContext());
4890 }
4891
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004892 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004893 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004894 SDValue ArgNode = getValue(*i);
4895 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4896
4897 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004898 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4899 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4900 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4901 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4902 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4903 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004904 Entry.Alignment = CS.getParamAlignment(attrInd);
4905 Args.push_back(Entry);
4906 }
4907
4908 if (LandingPad && MMI) {
4909 // Insert a label before the invoke call to mark the try range. This can be
4910 // used to detect deletion of the invoke via the MachineModuleInfo.
4911 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004912
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004913 // Both PendingLoads and PendingExports must be flushed here;
4914 // this call might not return.
4915 (void)getRoot();
Bill Wendlinge80ae832009-12-22 00:50:32 +00004916 SDValue Label = DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4917 getControlRoot(), BeginLabel);
4918 DAG.setRoot(Label);
4919 if (DisableScheduling)
4920 DAG.AssignOrdering(Label.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004921 }
4922
Dan Gohman98ca4f22009-08-05 01:29:28 +00004923 // Check if target-independent constraints permit a tail call here.
4924 // Target-dependent constraints are checked within TLI.LowerCallTo.
4925 if (isTailCall &&
4926 !isInTailCallPosition(CS.getInstruction(),
4927 CS.getAttributes().getRetAttributes(),
4928 TLI))
4929 isTailCall = false;
4930
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004931 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004932 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004933 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004934 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004935 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004936 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004937 isTailCall,
4938 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004939 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004940 assert((isTailCall || Result.second.getNode()) &&
4941 "Non-null chain expected with non-tail call!");
4942 assert((Result.second.getNode() || !Result.first.getNode()) &&
4943 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004944 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004945 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004946 if (DisableScheduling)
4947 DAG.AssignOrdering(Result.first.getNode(), SDNodeOrder);
4948 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004949 // The instruction result is the result of loading from the
4950 // hidden sret parameter.
4951 SmallVector<EVT, 1> PVTs;
4952 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4953
4954 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4955 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4956 EVT PtrVT = PVTs[0];
4957 unsigned NumValues = OutVTs.size();
4958 SmallVector<SDValue, 4> Values(NumValues);
4959 SmallVector<SDValue, 4> Chains(NumValues);
4960
4961 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004962 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4963 DemoteStackSlot,
4964 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004965 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
Bill Wendlinge80ae832009-12-22 00:50:32 +00004966 Add, NULL, Offsets[i], false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004967 Values[i] = L;
4968 Chains[i] = L.getValue(1);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004969
4970 if (DisableScheduling) {
4971 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
4972 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
4973 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004974 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004975
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004976 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4977 MVT::Other, &Chains[0], NumValues);
4978 PendingLoads.push_back(Chain);
4979
Bill Wendlinge80ae832009-12-22 00:50:32 +00004980 SDValue MV = DAG.getNode(ISD::MERGE_VALUES,
4981 getCurDebugLoc(),
4982 DAG.getVTList(&OutVTs[0], NumValues),
4983 &Values[0], NumValues);
4984 setValue(CS.getInstruction(), MV);
4985
4986 if (DisableScheduling) {
4987 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
4988 DAG.AssignOrdering(MV.getNode(), SDNodeOrder);
4989 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004990 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004991
4992 // As a special case, a null chain means that a tail call has been emitted and
4993 // the DAG root is already updated.
4994 if (Result.second.getNode()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00004995 DAG.setRoot(Result.second);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004996 if (DisableScheduling)
4997 DAG.AssignOrdering(Result.second.getNode(), SDNodeOrder);
4998 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00004999 HasTailCall = true;
Bill Wendlinge80ae832009-12-22 00:50:32 +00005000 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005001
5002 if (LandingPad && MMI) {
5003 // Insert a label at the end of the invoke call to mark the try range. This
5004 // can be used to detect deletion of the invoke via the MachineModuleInfo.
5005 EndLabel = MMI->NextLabelID();
Bill Wendlinge80ae832009-12-22 00:50:32 +00005006 SDValue Label = DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5007 getRoot(), EndLabel);
5008 DAG.setRoot(Label);
5009
5010 if (DisableScheduling)
5011 DAG.AssignOrdering(Label.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005012
5013 // Inform MachineModuleInfo of range.
5014 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
5015 }
5016}
5017
Dan Gohman2048b852009-11-23 18:04:58 +00005018void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005019 const char *RenameFn = 0;
5020 if (Function *F = I.getCalledFunction()) {
5021 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005022 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
5023 if (II) {
5024 if (unsigned IID = II->getIntrinsicID(F)) {
5025 RenameFn = visitIntrinsicCall(I, IID);
5026 if (!RenameFn)
5027 return;
5028 }
5029 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005030 if (unsigned IID = F->getIntrinsicID()) {
5031 RenameFn = visitIntrinsicCall(I, IID);
5032 if (!RenameFn)
5033 return;
5034 }
5035 }
5036
5037 // Check for well-known libc/libm calls. If the function is internal, it
5038 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005039 if (!F->hasLocalLinkage() && F->hasName()) {
5040 StringRef Name = F->getName();
5041 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005042 if (I.getNumOperands() == 3 && // Basic sanity checks.
5043 I.getOperand(1)->getType()->isFloatingPoint() &&
5044 I.getType() == I.getOperand(1)->getType() &&
5045 I.getType() == I.getOperand(2)->getType()) {
5046 SDValue LHS = getValue(I.getOperand(1));
5047 SDValue RHS = getValue(I.getOperand(2));
Bill Wendlingec72e322009-12-22 01:11:43 +00005048 SDValue Res = DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5049 LHS.getValueType(), LHS, RHS);
5050 setValue(&I, Res);
5051 if (DisableScheduling)
5052 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005053 return;
5054 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005055 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005056 if (I.getNumOperands() == 2 && // Basic sanity checks.
5057 I.getOperand(1)->getType()->isFloatingPoint() &&
5058 I.getType() == I.getOperand(1)->getType()) {
5059 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingec72e322009-12-22 01:11:43 +00005060 SDValue Res = DAG.getNode(ISD::FABS, getCurDebugLoc(),
5061 Tmp.getValueType(), Tmp);
5062 setValue(&I, Res);
5063 if (DisableScheduling)
5064 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005065 return;
5066 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005067 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005068 if (I.getNumOperands() == 2 && // Basic sanity checks.
5069 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005070 I.getType() == I.getOperand(1)->getType() &&
5071 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005072 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingec72e322009-12-22 01:11:43 +00005073 SDValue Res = DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5074 Tmp.getValueType(), Tmp);
5075 setValue(&I, Res);
5076 if (DisableScheduling)
5077 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005078 return;
5079 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005080 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 if (I.getNumOperands() == 2 && // Basic sanity checks.
5082 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005083 I.getType() == I.getOperand(1)->getType() &&
5084 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005085 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingec72e322009-12-22 01:11:43 +00005086 SDValue Res = DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5087 Tmp.getValueType(), Tmp);
5088 setValue(&I, Res);
5089 if (DisableScheduling)
5090 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005091 return;
5092 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005093 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5094 if (I.getNumOperands() == 2 && // Basic sanity checks.
5095 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005096 I.getType() == I.getOperand(1)->getType() &&
5097 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005098 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingec72e322009-12-22 01:11:43 +00005099 SDValue Res = DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5100 Tmp.getValueType(), Tmp);
5101 setValue(&I, Res);
5102 if (DisableScheduling)
5103 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005104 return;
5105 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005106 }
5107 }
5108 } else if (isa<InlineAsm>(I.getOperand(0))) {
5109 visitInlineAsm(&I);
5110 return;
5111 }
5112
5113 SDValue Callee;
5114 if (!RenameFn)
5115 Callee = getValue(I.getOperand(0));
5116 else
Bill Wendling056292f2008-09-16 21:48:12 +00005117 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005118
Bill Wendlingec72e322009-12-22 01:11:43 +00005119 if (DisableScheduling)
5120 DAG.AssignOrdering(Callee.getNode(), SDNodeOrder);
5121
Dan Gohman98ca4f22009-08-05 01:29:28 +00005122 // Check if we can potentially perform a tail call. More detailed
5123 // checking is be done within LowerCallTo, after more information
5124 // about the call is known.
5125 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5126
5127 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005128}
5129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005130/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005131/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005132/// Chain/Flag as the input and updates them for the output Chain/Flag.
5133/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005134SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005135 unsigned Order, SDValue &Chain,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005136 SDValue *Flag) const {
5137 // Assemble the legal parts into the final values.
5138 SmallVector<SDValue, 4> Values(ValueVTs.size());
5139 SmallVector<SDValue, 8> Parts;
5140 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5141 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005142 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005143 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005144 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005145
5146 Parts.resize(NumRegs);
5147 for (unsigned i = 0; i != NumRegs; ++i) {
5148 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00005149 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005150 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005151 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005152 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005153 *Flag = P.getValue(2);
5154 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005157
Bill Wendlingec72e322009-12-22 01:11:43 +00005158 if (DisableScheduling)
5159 DAG.AssignOrdering(P.getNode(), Order);
5160
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005161 // If the source register was virtual and if we know something about it,
5162 // add an assert node.
5163 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5164 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5165 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5166 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5167 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5168 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005170 unsigned RegSize = RegisterVT.getSizeInBits();
5171 unsigned NumSignBits = LOI.NumSignBits;
5172 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005174 // FIXME: We capture more information than the dag can represent. For
5175 // now, just use the tightest assertzext/assertsext possible.
5176 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005177 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005178 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005179 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005180 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005181 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005182 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005183 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005184 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005185 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005186 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005187 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005188 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005189 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005190 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005191 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005192 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005193 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005194
Owen Anderson825b72b2009-08-11 20:47:22 +00005195 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005196 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005197 RegisterVT, P, DAG.getValueType(FromVT));
5198
Bill Wendlingec72e322009-12-22 01:11:43 +00005199 if (DisableScheduling)
5200 DAG.AssignOrdering(P.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005201 }
5202 }
5203 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005205 Parts[i] = P;
5206 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005207
Scott Michelfdc40a02009-02-17 22:15:04 +00005208 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005209 NumRegs, RegisterVT, ValueVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005210 if (DisableScheduling)
5211 DAG.AssignOrdering(Values[Value].getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005212 Part += NumRegs;
5213 Parts.clear();
5214 }
5215
Bill Wendlingec72e322009-12-22 01:11:43 +00005216 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5217 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5218 &Values[0], ValueVTs.size());
5219 if (DisableScheduling)
5220 DAG.AssignOrdering(Res.getNode(), Order);
5221 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005222}
5223
5224/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005225/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005226/// Chain/Flag as the input and updates them for the output Chain/Flag.
5227/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005228void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005229 unsigned Order, SDValue &Chain,
5230 SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005231 // Get the list of the values's legal parts.
5232 unsigned NumRegs = Regs.size();
5233 SmallVector<SDValue, 8> Parts(NumRegs);
5234 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005235 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005236 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005237 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005238
Dale Johannesen66978ee2009-01-31 02:22:37 +00005239 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 &Parts[Part], NumParts, RegisterVT);
5241 Part += NumParts;
5242 }
5243
5244 // Copy the parts into the registers.
5245 SmallVector<SDValue, 8> Chains(NumRegs);
5246 for (unsigned i = 0; i != NumRegs; ++i) {
5247 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00005248 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005249 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00005250 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005251 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005252 *Flag = Part.getValue(1);
5253 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005254
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005255 Chains[i] = Part.getValue(0);
Bill Wendlingec72e322009-12-22 01:11:43 +00005256
5257 if (DisableScheduling)
5258 DAG.AssignOrdering(Part.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005259 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005262 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005263 // flagged to it. That is the CopyToReg nodes and the user are considered
5264 // a single scheduling unit. If we create a TokenFactor and return it as
5265 // chain, then the TokenFactor is both a predecessor (operand) of the
5266 // user as well as a successor (the TF operands are flagged to the user).
5267 // c1, f1 = CopyToReg
5268 // c2, f2 = CopyToReg
5269 // c3 = TokenFactor c1, c2
5270 // ...
5271 // = op c3, ..., f2
5272 Chain = Chains[NumRegs-1];
5273 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005274 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Bill Wendlingec72e322009-12-22 01:11:43 +00005275
5276 if (DisableScheduling)
5277 DAG.AssignOrdering(Chain.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005278}
5279
5280/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005281/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005282/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005283void RegsForValue::AddInlineAsmOperands(unsigned Code,
5284 bool HasMatching,unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +00005285 SelectionDAG &DAG, unsigned Order,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005286 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005287 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005288 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5289 unsigned Flag = Code | (Regs.size() << 3);
5290 if (HasMatching)
5291 Flag |= 0x80000000 | (MatchingIdx << 16);
Bill Wendling651ad132009-12-22 01:25:10 +00005292
5293 SDValue Res = DAG.getTargetConstant(Flag, IntPtrTy);
5294 Ops.push_back(Res);
5295
5296 if (DisableScheduling)
5297 DAG.AssignOrdering(Res.getNode(), Order);
5298
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005299 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005300 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005301 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005302 for (unsigned i = 0; i != NumRegs; ++i) {
5303 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling651ad132009-12-22 01:25:10 +00005304 SDValue Res = DAG.getRegister(Regs[Reg++], RegisterVT);
5305 Ops.push_back(Res);
5306
5307 if (DisableScheduling)
5308 DAG.AssignOrdering(Res.getNode(), Order);
Chris Lattner58f15c42008-10-17 16:21:11 +00005309 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005310 }
5311}
5312
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005313/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005314/// i.e. it isn't a stack pointer or some other special register, return the
5315/// register class for the register. Otherwise, return null.
5316static const TargetRegisterClass *
5317isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5318 const TargetLowering &TLI,
5319 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005320 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005321 const TargetRegisterClass *FoundRC = 0;
5322 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5323 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005324 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325
5326 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005327 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005328 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5329 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5330 I != E; ++I) {
5331 if (TLI.isTypeLegal(*I)) {
5332 // If we have already found this register in a different register class,
5333 // choose the one with the largest VT specified. For example, on
5334 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005335 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 ThisVT = *I;
5337 break;
5338 }
5339 }
5340 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005341
Owen Anderson825b72b2009-08-11 20:47:22 +00005342 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005344 // NOTE: This isn't ideal. In particular, this might allocate the
5345 // frame pointer in functions that need it (due to them not being taken
5346 // out of allocation, because a variable sized allocation hasn't been seen
5347 // yet). This is a slight code pessimization, but should still work.
5348 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5349 E = RC->allocation_order_end(MF); I != E; ++I)
5350 if (*I == Reg) {
5351 // We found a matching register class. Keep looking at others in case
5352 // we find one with larger registers that this physreg is also in.
5353 FoundRC = RC;
5354 FoundVT = ThisVT;
5355 break;
5356 }
5357 }
5358 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005359}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005360
5361
5362namespace llvm {
5363/// AsmOperandInfo - This contains information for each constraint that we are
5364/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005365class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005366 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005367public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005368 /// CallOperand - If this is the result output operand or a clobber
5369 /// this is null, otherwise it is the incoming operand to the CallInst.
5370 /// This gets modified as the asm is processed.
5371 SDValue CallOperand;
5372
5373 /// AssignedRegs - If this is a register or register class operand, this
5374 /// contains the set of register corresponding to the operand.
5375 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005376
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005377 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5378 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5379 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005380
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005381 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5382 /// busy in OutputRegs/InputRegs.
5383 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005384 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005385 std::set<unsigned> &InputRegs,
5386 const TargetRegisterInfo &TRI) const {
5387 if (isOutReg) {
5388 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5389 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5390 }
5391 if (isInReg) {
5392 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5393 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5394 }
5395 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005396
Owen Andersone50ed302009-08-10 22:56:29 +00005397 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005398 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005399 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00005400 EVT getCallOperandValEVT(LLVMContext &Context,
5401 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005402 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005403 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005404
Chris Lattner81249c92008-10-17 17:05:25 +00005405 if (isa<BasicBlock>(CallOperandVal))
5406 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005407
Chris Lattner81249c92008-10-17 17:05:25 +00005408 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005409
Chris Lattner81249c92008-10-17 17:05:25 +00005410 // If this is an indirect operand, the operand is a pointer to the
5411 // accessed type.
5412 if (isIndirect)
5413 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005414
Chris Lattner81249c92008-10-17 17:05:25 +00005415 // If OpTy is not a single value, it may be a struct/union that we
5416 // can tile with integers.
5417 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5418 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5419 switch (BitSize) {
5420 default: break;
5421 case 1:
5422 case 8:
5423 case 16:
5424 case 32:
5425 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005426 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005427 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005428 break;
5429 }
5430 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005431
Chris Lattner81249c92008-10-17 17:05:25 +00005432 return TLI.getValueType(OpTy, true);
5433 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005434
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005435private:
5436 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5437 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005438 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005439 const TargetRegisterInfo &TRI) {
5440 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5441 Regs.insert(Reg);
5442 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5443 for (; *Aliases; ++Aliases)
5444 Regs.insert(*Aliases);
5445 }
5446};
5447} // end llvm namespace.
5448
5449
5450/// GetRegistersForValue - Assign registers (virtual or physical) for the
5451/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005452/// register allocator to handle the assignment process. However, if the asm
5453/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005454/// allocation. This produces generally horrible, but correct, code.
5455///
5456/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005457/// Input and OutputRegs are the set of already allocated physical registers.
5458///
Dan Gohman2048b852009-11-23 18:04:58 +00005459void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005460GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005461 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005462 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005463 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005464
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005465 // Compute whether this value requires an input register, an output register,
5466 // or both.
5467 bool isOutReg = false;
5468 bool isInReg = false;
5469 switch (OpInfo.Type) {
5470 case InlineAsm::isOutput:
5471 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005472
5473 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005474 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005475 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005476 break;
5477 case InlineAsm::isInput:
5478 isInReg = true;
5479 isOutReg = false;
5480 break;
5481 case InlineAsm::isClobber:
5482 isOutReg = true;
5483 isInReg = true;
5484 break;
5485 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005486
5487
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005488 MachineFunction &MF = DAG.getMachineFunction();
5489 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005490
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005491 // If this is a constraint for a single physreg, or a constraint for a
5492 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005493 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005494 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5495 OpInfo.ConstraintVT);
5496
5497 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005498 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005499 // If this is a FP input in an integer register (or visa versa) insert a bit
5500 // cast of the input value. More generally, handle any case where the input
5501 // value disagrees with the register class we plan to stick this in.
5502 if (OpInfo.Type == InlineAsm::isInput &&
5503 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005504 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005505 // types are identical size, use a bitcast to convert (e.g. two differing
5506 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005507 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005508 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005509 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005510 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005511 OpInfo.ConstraintVT = RegVT;
5512 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5513 // If the input is a FP value and we want it in FP registers, do a
5514 // bitcast to the corresponding integer type. This turns an f64 value
5515 // into i64, which can be passed with two i32 values on a 32-bit
5516 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005517 RegVT = EVT::getIntegerVT(Context,
5518 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005519 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005520 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005521 OpInfo.ConstraintVT = RegVT;
5522 }
Bill Wendling651ad132009-12-22 01:25:10 +00005523
5524 if (DisableScheduling)
5525 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Chris Lattner01426e12008-10-21 00:45:36 +00005526 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005527
Owen Anderson23b9b192009-08-12 00:36:31 +00005528 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005529 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005530
Owen Andersone50ed302009-08-10 22:56:29 +00005531 EVT RegVT;
5532 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005533
5534 // If this is a constraint for a specific physical register, like {r17},
5535 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005536 if (unsigned AssignedReg = PhysReg.first) {
5537 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005538 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005539 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 // Get the actual register value type. This is important, because the user
5542 // may have asked for (e.g.) the AX register in i32 type. We need to
5543 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005544 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005545
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005546 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005547 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005548
5549 // If this is an expanded reference, add the rest of the regs to Regs.
5550 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005551 TargetRegisterClass::iterator I = RC->begin();
5552 for (; *I != AssignedReg; ++I)
5553 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005554
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005555 // Already added the first reg.
5556 --NumRegs; ++I;
5557 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005558 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005559 Regs.push_back(*I);
5560 }
5561 }
Bill Wendling651ad132009-12-22 01:25:10 +00005562
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005563 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5564 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5565 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5566 return;
5567 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005569 // Otherwise, if this was a reference to an LLVM register class, create vregs
5570 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005571 if (const TargetRegisterClass *RC = PhysReg.second) {
5572 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005573 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005574 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005575
Evan Chengfb112882009-03-23 08:01:15 +00005576 // Create the appropriate number of virtual registers.
5577 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5578 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005579 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005580
Evan Chengfb112882009-03-23 08:01:15 +00005581 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5582 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005583 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005584
5585 // This is a reference to a register class that doesn't directly correspond
5586 // to an LLVM register class. Allocate NumRegs consecutive, available,
5587 // registers from the class.
5588 std::vector<unsigned> RegClassRegs
5589 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5590 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005591
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005592 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5593 unsigned NumAllocated = 0;
5594 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5595 unsigned Reg = RegClassRegs[i];
5596 // See if this register is available.
5597 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5598 (isInReg && InputRegs.count(Reg))) { // Already used.
5599 // Make sure we find consecutive registers.
5600 NumAllocated = 0;
5601 continue;
5602 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005603
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005604 // Check to see if this register is allocatable (i.e. don't give out the
5605 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005606 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5607 if (!RC) { // Couldn't allocate this register.
5608 // Reset NumAllocated to make sure we return consecutive registers.
5609 NumAllocated = 0;
5610 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005611 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005613 // Okay, this register is good, we can use it.
5614 ++NumAllocated;
5615
5616 // If we allocated enough consecutive registers, succeed.
5617 if (NumAllocated == NumRegs) {
5618 unsigned RegStart = (i-NumAllocated)+1;
5619 unsigned RegEnd = i+1;
5620 // Mark all of the allocated registers used.
5621 for (unsigned i = RegStart; i != RegEnd; ++i)
5622 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005623
5624 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005625 OpInfo.ConstraintVT);
5626 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5627 return;
5628 }
5629 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005630
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005631 // Otherwise, we couldn't allocate enough registers for this.
5632}
5633
Evan Chengda43bcf2008-09-24 00:05:32 +00005634/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5635/// processed uses a memory 'm' constraint.
5636static bool
5637hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005638 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005639 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5640 InlineAsm::ConstraintInfo &CI = CInfos[i];
5641 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5642 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5643 if (CType == TargetLowering::C_Memory)
5644 return true;
5645 }
Chris Lattner6c147292009-04-30 00:48:50 +00005646
5647 // Indirect operand accesses access memory.
5648 if (CI.isIndirect)
5649 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005650 }
5651
5652 return false;
5653}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005654
5655/// visitInlineAsm - Handle a call to an InlineAsm object.
5656///
Dan Gohman2048b852009-11-23 18:04:58 +00005657void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005658 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5659
5660 /// ConstraintOperands - Information about all of the constraints.
5661 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005663 std::set<unsigned> OutputRegs, InputRegs;
5664
5665 // Do a prepass over the constraints, canonicalizing them, and building up the
5666 // ConstraintOperands list.
5667 std::vector<InlineAsm::ConstraintInfo>
5668 ConstraintInfos = IA->ParseConstraints();
5669
Evan Chengda43bcf2008-09-24 00:05:32 +00005670 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005671
5672 SDValue Chain, Flag;
5673
5674 // We won't need to flush pending loads if this asm doesn't touch
5675 // memory and is nonvolatile.
5676 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005677 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005678 else
5679 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005681 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5682 unsigned ResNo = 0; // ResNo - The result number of the next output.
5683 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5684 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5685 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005686
Owen Anderson825b72b2009-08-11 20:47:22 +00005687 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005688
5689 // Compute the value type for each operand.
5690 switch (OpInfo.Type) {
5691 case InlineAsm::isOutput:
5692 // Indirect outputs just consume an argument.
5693 if (OpInfo.isIndirect) {
5694 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5695 break;
5696 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005697
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005698 // The return value of the call is this value. As such, there is no
5699 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005700 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5701 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005702 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5703 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5704 } else {
5705 assert(ResNo == 0 && "Asm only has one result!");
5706 OpVT = TLI.getValueType(CS.getType());
5707 }
5708 ++ResNo;
5709 break;
5710 case InlineAsm::isInput:
5711 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5712 break;
5713 case InlineAsm::isClobber:
5714 // Nothing to do.
5715 break;
5716 }
5717
5718 // If this is an input or an indirect output, process the call argument.
5719 // BasicBlocks are labels, currently appearing only in asm's.
5720 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005721 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005722 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5723
Chris Lattner81249c92008-10-17 17:05:25 +00005724 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005725 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005726 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005727 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005728 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005729
Owen Anderson1d0be152009-08-13 21:58:54 +00005730 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005731 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005732
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005733 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005734 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005735
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005736 // Second pass over the constraints: compute which constraint option to use
5737 // and assign registers to constraints that want a specific physreg.
5738 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5739 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005740
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005741 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005742 // matching input. If their types mismatch, e.g. one is an integer, the
5743 // other is floating point, or their sizes are different, flag it as an
5744 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005745 if (OpInfo.hasMatchingInput()) {
5746 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5747 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005748 if ((OpInfo.ConstraintVT.isInteger() !=
5749 Input.ConstraintVT.isInteger()) ||
5750 (OpInfo.ConstraintVT.getSizeInBits() !=
5751 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005752 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005753 " with a matching output constraint of incompatible"
5754 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005755 }
5756 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005757 }
5758 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005759
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005760 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005761 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005762
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005763 // If this is a memory input, and if the operand is not indirect, do what we
5764 // need to to provide an address for the memory input.
5765 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5766 !OpInfo.isIndirect) {
5767 assert(OpInfo.Type == InlineAsm::isInput &&
5768 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005769
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005770 // Memory operands really want the address of the value. If we don't have
5771 // an indirect input, put it in the constpool if we can, otherwise spill
5772 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005773
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005774 // If the operand is a float, integer, or vector constant, spill to a
5775 // constant pool entry to get its address.
5776 Value *OpVal = OpInfo.CallOperandVal;
5777 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5778 isa<ConstantVector>(OpVal)) {
5779 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5780 TLI.getPointerTy());
5781 } else {
5782 // Otherwise, create a stack slot and emit a store to it before the
5783 // asm.
5784 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005785 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005786 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5787 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005788 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005789 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005790 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005791 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005792 OpInfo.CallOperand = StackSlot;
Bill Wendling651ad132009-12-22 01:25:10 +00005793 if (DisableScheduling)
5794 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005795 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005796
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005797 // There is no longer a Value* corresponding to this operand.
5798 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005799
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005800 // It is now an indirect operand.
5801 OpInfo.isIndirect = true;
Bill Wendling651ad132009-12-22 01:25:10 +00005802
5803 if (DisableScheduling)
5804 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005805 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005806
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005807 // If this constraint is for a specific register, allocate it before
5808 // anything else.
5809 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005810 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005811 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005812
Bill Wendling651ad132009-12-22 01:25:10 +00005813 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005814
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005815 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005816 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005817 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5818 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005820 // C_Register operands have already been allocated, Other/Memory don't need
5821 // to be.
5822 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005823 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005824 }
5825
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005826 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5827 std::vector<SDValue> AsmNodeOperands;
5828 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5829 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005830 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005831
5832
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005833 // Loop over all of the inputs, copying the operand values into the
5834 // appropriate registers and processing the output regs.
5835 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005836
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005837 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5838 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005839
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005840 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5841 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5842
5843 switch (OpInfo.Type) {
5844 case InlineAsm::isOutput: {
5845 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5846 OpInfo.ConstraintType != TargetLowering::C_Register) {
5847 // Memory output, or 'other' output (e.g. 'X' constraint).
5848 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5849
5850 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005851 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5852 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005853 TLI.getPointerTy()));
5854 AsmNodeOperands.push_back(OpInfo.CallOperand);
5855 break;
5856 }
5857
5858 // Otherwise, this is a register or register class output.
5859
5860 // Copy the output from the appropriate register. Find a register that
5861 // we can use.
5862 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005863 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005864 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005865 }
5866
5867 // If this is an indirect operand, store through the pointer after the
5868 // asm.
5869 if (OpInfo.isIndirect) {
5870 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5871 OpInfo.CallOperandVal));
5872 } else {
5873 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005874 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5875 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005876 // Concatenate this output onto the outputs list.
5877 RetValRegs.append(OpInfo.AssignedRegs);
5878 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005879
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005880 // Add information to the INLINEASM node to know that this register is
5881 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005882 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5883 6 /* EARLYCLOBBER REGDEF */ :
5884 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005885 false,
5886 0,
Bill Wendling651ad132009-12-22 01:25:10 +00005887 DAG, SDNodeOrder,
5888 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005889 break;
5890 }
5891 case InlineAsm::isInput: {
5892 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005893
Chris Lattner6bdcda32008-10-17 16:47:46 +00005894 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005895 // If this is required to match an output register we have already set,
5896 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005897 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005898
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005899 // Scan until we find the definition we already emitted of this operand.
5900 // When we find it, create a RegsForValue operand.
5901 unsigned CurOp = 2; // The first operand.
5902 for (; OperandNo; --OperandNo) {
5903 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005904 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005905 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005906 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5907 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5908 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005909 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005910 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005911 }
5912
Evan Cheng697cbbf2009-03-20 18:03:34 +00005913 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005914 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005915 if ((OpFlag & 7) == 2 /*REGDEF*/
5916 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5917 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005918 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005919 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005920 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005921 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005922 RegsForValue MatchedRegs;
5923 MatchedRegs.TLI = &TLI;
5924 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005925 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005926 MatchedRegs.RegVTs.push_back(RegVT);
5927 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005928 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005929 i != e; ++i)
5930 MatchedRegs.Regs.
5931 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005932
5933 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005934 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00005935 SDNodeOrder, Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005936 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5937 true, OpInfo.getMatchedOperand(),
Bill Wendling651ad132009-12-22 01:25:10 +00005938 DAG, SDNodeOrder, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005939 break;
5940 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005941 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5942 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5943 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005944 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005945 // See InlineAsm.h isUseOperandTiedToDef.
5946 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005947 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005948 TLI.getPointerTy()));
5949 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5950 break;
5951 }
5952 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005953
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005954 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005955 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005956 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005957
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005958 std::vector<SDValue> Ops;
5959 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005960 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005961 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005962 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005963 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005964 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005965
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005966 // Add information to the INLINEASM node to know about this input.
5967 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005968 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969 TLI.getPointerTy()));
5970 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5971 break;
5972 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5973 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5974 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5975 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005976
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005977 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005978 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5979 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005980 TLI.getPointerTy()));
5981 AsmNodeOperands.push_back(InOperandVal);
5982 break;
5983 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005985 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5986 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5987 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005988 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005989 "Don't know how to handle indirect register inputs yet!");
5990
5991 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005992 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005993 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005994 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005995 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005996
Dale Johannesen66978ee2009-01-31 02:22:37 +00005997 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00005998 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005999
Evan Cheng697cbbf2009-03-20 18:03:34 +00006000 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006001 DAG, SDNodeOrder,
6002 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003 break;
6004 }
6005 case InlineAsm::isClobber: {
6006 // Add the clobbered value to the operand list, so that the register
6007 // allocator is aware that the physreg got clobbered.
6008 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00006009 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Bill Wendling651ad132009-12-22 01:25:10 +00006010 false, 0, DAG, SDNodeOrder,
6011 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006012 break;
6013 }
6014 }
6015 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006017 // Finish up input operands.
6018 AsmNodeOperands[0] = Chain;
6019 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006020
Dale Johannesen66978ee2009-01-31 02:22:37 +00006021 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006022 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006023 &AsmNodeOperands[0], AsmNodeOperands.size());
6024 Flag = Chain.getValue(1);
6025
Bill Wendling651ad132009-12-22 01:25:10 +00006026 if (DisableScheduling)
6027 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
6028
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006029 // If this asm returns a register value, copy the result from that register
6030 // and set it as the value of the call.
6031 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006032 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006033 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006034
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006035 // FIXME: Why don't we do this for inline asms with MRVs?
6036 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006037 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006038
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006039 // If any of the results of the inline asm is a vector, it may have the
6040 // wrong width/num elts. This can happen for register classes that can
6041 // contain multiple different value types. The preg or vreg allocated may
6042 // not have the same VT as was expected. Convert it to the right type
6043 // with bit_convert.
6044 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006045 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006046 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006047
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006048 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006049 ResultType.isInteger() && Val.getValueType().isInteger()) {
6050 // If a result value was tied to an input value, the computed result may
6051 // have a wider width than the expected result. Extract the relevant
6052 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006053 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006054 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006055
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006056 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Bill Wendling651ad132009-12-22 01:25:10 +00006057
6058 if (DisableScheduling)
6059 DAG.AssignOrdering(Val.getNode(), SDNodeOrder);
Chris Lattner0c526442008-10-17 17:52:49 +00006060 }
Dan Gohman95915732008-10-18 01:03:45 +00006061
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006062 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006063 // Don't need to use this as a chain in this case.
6064 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6065 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006066 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006068 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006069
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006070 // Process indirect outputs, first output all of the flagged copies out of
6071 // physregs.
6072 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6073 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
6074 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00006075 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006076 SDNodeOrder, Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006077 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00006078
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006079 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081 // Emit the non-flagged stores from the physregs.
6082 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006083 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6084 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6085 StoresToEmit[i].first,
6086 getValue(StoresToEmit[i].second),
6087 StoresToEmit[i].second, 0);
6088 OutChains.push_back(Val);
6089 if (DisableScheduling)
6090 DAG.AssignOrdering(Val.getNode(), SDNodeOrder);
6091 }
6092
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006093 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006094 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006095 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006096
6097 if (DisableScheduling)
6098 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
6099
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006100 DAG.setRoot(Chain);
6101}
6102
Dan Gohman2048b852009-11-23 18:04:58 +00006103void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Bill Wendling651ad132009-12-22 01:25:10 +00006104 SDValue Res = DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6105 MVT::Other, getRoot(),
6106 getValue(I.getOperand(1)),
6107 DAG.getSrcValue(I.getOperand(1)));
6108 DAG.setRoot(Res);
6109 if (DisableScheduling)
6110 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006111}
6112
Dan Gohman2048b852009-11-23 18:04:58 +00006113void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006114 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6115 getRoot(), getValue(I.getOperand(0)),
6116 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006117 setValue(&I, V);
6118 DAG.setRoot(V.getValue(1));
Bill Wendling651ad132009-12-22 01:25:10 +00006119 if (DisableScheduling)
6120 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006121}
6122
Dan Gohman2048b852009-11-23 18:04:58 +00006123void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Bill Wendling651ad132009-12-22 01:25:10 +00006124 SDValue Res = DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6125 MVT::Other, getRoot(),
6126 getValue(I.getOperand(1)),
6127 DAG.getSrcValue(I.getOperand(1)));
6128 DAG.setRoot(Res);
6129 if (DisableScheduling)
6130 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006131}
6132
Dan Gohman2048b852009-11-23 18:04:58 +00006133void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Bill Wendling651ad132009-12-22 01:25:10 +00006134 SDValue Res = DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6135 MVT::Other, getRoot(),
6136 getValue(I.getOperand(1)),
6137 getValue(I.getOperand(2)),
6138 DAG.getSrcValue(I.getOperand(1)),
6139 DAG.getSrcValue(I.getOperand(2)));
6140 DAG.setRoot(Res);
6141 if (DisableScheduling)
6142 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006143}
6144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006146/// implementation, which just calls LowerCall.
6147/// FIXME: When all targets are
6148/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006149std::pair<SDValue, SDValue>
6150TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6151 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006152 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006153 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006154 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006155 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00006156 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00006157
Dan Gohman1937e2f2008-09-16 01:42:28 +00006158 assert((!isTailCall || PerformTailCallOpt) &&
6159 "isTailCall set when tail-call optimizations are disabled!");
6160
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006161 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006162 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006163 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006164 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006165 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6166 for (unsigned Value = 0, NumValues = ValueVTs.size();
6167 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006168 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006169 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006170 SDValue Op = SDValue(Args[i].Node.getNode(),
6171 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006172 ISD::ArgFlagsTy Flags;
6173 unsigned OriginalAlignment =
6174 getTargetData()->getABITypeAlignment(ArgTy);
6175
6176 if (Args[i].isZExt)
6177 Flags.setZExt();
6178 if (Args[i].isSExt)
6179 Flags.setSExt();
6180 if (Args[i].isInReg)
6181 Flags.setInReg();
6182 if (Args[i].isSRet)
6183 Flags.setSRet();
6184 if (Args[i].isByVal) {
6185 Flags.setByVal();
6186 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6187 const Type *ElementTy = Ty->getElementType();
6188 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006189 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006190 // For ByVal, alignment should come from FE. BE will guess if this
6191 // info is not there but there are cases it cannot get right.
6192 if (Args[i].Alignment)
6193 FrameAlign = Args[i].Alignment;
6194 Flags.setByValAlign(FrameAlign);
6195 Flags.setByValSize(FrameSize);
6196 }
6197 if (Args[i].isNest)
6198 Flags.setNest();
6199 Flags.setOrigAlign(OriginalAlignment);
6200
Owen Anderson23b9b192009-08-12 00:36:31 +00006201 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6202 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006203 SmallVector<SDValue, 4> Parts(NumParts);
6204 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6205
6206 if (Args[i].isSExt)
6207 ExtendKind = ISD::SIGN_EXTEND;
6208 else if (Args[i].isZExt)
6209 ExtendKind = ISD::ZERO_EXTEND;
6210
Dale Johannesen66978ee2009-01-31 02:22:37 +00006211 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006212
Dan Gohman98ca4f22009-08-05 01:29:28 +00006213 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006214 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006215 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6216 if (NumParts > 1 && j == 0)
6217 MyFlags.Flags.setSplit();
6218 else if (j != 0)
6219 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006220
Dan Gohman98ca4f22009-08-05 01:29:28 +00006221 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006222 }
6223 }
6224 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006225
Dan Gohman98ca4f22009-08-05 01:29:28 +00006226 // Handle the incoming return values from the call.
6227 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006228 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006229 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006230 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006231 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006232 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6233 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006234 for (unsigned i = 0; i != NumRegs; ++i) {
6235 ISD::InputArg MyFlags;
6236 MyFlags.VT = RegisterVT;
6237 MyFlags.Used = isReturnValueUsed;
6238 if (RetSExt)
6239 MyFlags.Flags.setSExt();
6240 if (RetZExt)
6241 MyFlags.Flags.setZExt();
6242 if (isInreg)
6243 MyFlags.Flags.setInReg();
6244 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006245 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006246 }
6247
Dan Gohman98ca4f22009-08-05 01:29:28 +00006248 // Check if target-dependent constraints permit a tail call here.
6249 // Target-independent constraints should be checked by the caller.
6250 if (isTailCall &&
6251 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6252 isTailCall = false;
6253
6254 SmallVector<SDValue, 4> InVals;
6255 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6256 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006257
6258 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006259 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006260 "LowerCall didn't return a valid chain!");
6261 assert((!isTailCall || InVals.empty()) &&
6262 "LowerCall emitted a return value for a tail call!");
6263 assert((isTailCall || InVals.size() == Ins.size()) &&
6264 "LowerCall didn't emit the correct number of values!");
6265 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6266 assert(InVals[i].getNode() &&
6267 "LowerCall emitted a null value!");
6268 assert(Ins[i].VT == InVals[i].getValueType() &&
6269 "LowerCall emitted a value with the wrong type!");
6270 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006271
6272 // For a tail call, the return value is merely live-out and there aren't
6273 // any nodes in the DAG representing it. Return a special value to
6274 // indicate that a tail call has been emitted and no more Instructions
6275 // should be processed in the current block.
6276 if (isTailCall) {
6277 DAG.setRoot(Chain);
6278 return std::make_pair(SDValue(), SDValue());
6279 }
6280
6281 // Collect the legal value parts into potentially illegal values
6282 // that correspond to the original function's return values.
6283 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6284 if (RetSExt)
6285 AssertOp = ISD::AssertSext;
6286 else if (RetZExt)
6287 AssertOp = ISD::AssertZext;
6288 SmallVector<SDValue, 4> ReturnValues;
6289 unsigned CurReg = 0;
6290 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006291 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006292 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6293 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006294
6295 SDValue ReturnValue =
6296 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
6297 AssertOp);
6298 ReturnValues.push_back(ReturnValue);
6299 CurReg += NumRegs;
6300 }
6301
6302 // For a function returning void, there is no return value. We can't create
6303 // such a node, so we just return a null return value in that case. In
6304 // that case, nothing will actualy look at the value.
6305 if (ReturnValues.empty())
6306 return std::make_pair(SDValue(), Chain);
6307
6308 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6309 DAG.getVTList(&RetTys[0], RetTys.size()),
6310 &ReturnValues[0], ReturnValues.size());
6311
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006312 return std::make_pair(Res, Chain);
6313}
6314
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006315void TargetLowering::LowerOperationWrapper(SDNode *N,
6316 SmallVectorImpl<SDValue> &Results,
6317 SelectionDAG &DAG) {
6318 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006319 if (Res.getNode())
6320 Results.push_back(Res);
6321}
6322
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006323SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006324 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006325 return SDValue();
6326}
6327
6328
Dan Gohman2048b852009-11-23 18:04:58 +00006329void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006330 SDValue Op = getValue(V);
6331 assert((Op.getOpcode() != ISD::CopyFromReg ||
6332 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6333 "Copy from a reg to the same reg!");
6334 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6335
Owen Anderson23b9b192009-08-12 00:36:31 +00006336 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006337 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +00006338 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006339 PendingExports.push_back(Chain);
6340}
6341
6342#include "llvm/CodeGen/SelectionDAGISel.h"
6343
Dan Gohman8c2b5252009-10-30 01:27:03 +00006344void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006345 // If this is the entry block, emit arguments.
6346 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006347 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006348 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006349 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006350 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006351 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006352
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006353 // Check whether the function can return without sret-demotion.
6354 SmallVector<EVT, 4> OutVTs;
6355 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006356 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
6357 OutVTs, OutsFlags, TLI);
6358 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6359
6360 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
6361 OutVTs, OutsFlags, DAG);
6362 if (!FLI.CanLowerReturn) {
6363 // Put in an sret pointer parameter before all the other parameters.
6364 SmallVector<EVT, 1> ValueVTs;
6365 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6366
6367 // NOTE: Assuming that a pointer will never break down to more than one VT
6368 // or one register.
6369 ISD::ArgFlagsTy Flags;
6370 Flags.setSRet();
6371 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6372 ISD::InputArg RetArg(Flags, RegisterVT, true);
6373 Ins.push_back(RetArg);
6374 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006375
Dan Gohman98ca4f22009-08-05 01:29:28 +00006376 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006377 unsigned Idx = 1;
6378 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6379 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006380 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006381 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6382 bool isArgValueUsed = !I->use_empty();
6383 for (unsigned Value = 0, NumValues = ValueVTs.size();
6384 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006385 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006386 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006387 ISD::ArgFlagsTy Flags;
6388 unsigned OriginalAlignment =
6389 TD->getABITypeAlignment(ArgTy);
6390
6391 if (F.paramHasAttr(Idx, Attribute::ZExt))
6392 Flags.setZExt();
6393 if (F.paramHasAttr(Idx, Attribute::SExt))
6394 Flags.setSExt();
6395 if (F.paramHasAttr(Idx, Attribute::InReg))
6396 Flags.setInReg();
6397 if (F.paramHasAttr(Idx, Attribute::StructRet))
6398 Flags.setSRet();
6399 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6400 Flags.setByVal();
6401 const PointerType *Ty = cast<PointerType>(I->getType());
6402 const Type *ElementTy = Ty->getElementType();
6403 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6404 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6405 // For ByVal, alignment should be passed from FE. BE will guess if
6406 // this info is not there but there are cases it cannot get right.
6407 if (F.getParamAlignment(Idx))
6408 FrameAlign = F.getParamAlignment(Idx);
6409 Flags.setByValAlign(FrameAlign);
6410 Flags.setByValSize(FrameSize);
6411 }
6412 if (F.paramHasAttr(Idx, Attribute::Nest))
6413 Flags.setNest();
6414 Flags.setOrigAlign(OriginalAlignment);
6415
Owen Anderson23b9b192009-08-12 00:36:31 +00006416 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6417 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006418 for (unsigned i = 0; i != NumRegs; ++i) {
6419 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6420 if (NumRegs > 1 && i == 0)
6421 MyFlags.Flags.setSplit();
6422 // if it isn't first piece, alignment must be 1
6423 else if (i > 0)
6424 MyFlags.Flags.setOrigAlign(1);
6425 Ins.push_back(MyFlags);
6426 }
6427 }
6428 }
6429
6430 // Call the target to set up the argument values.
6431 SmallVector<SDValue, 8> InVals;
6432 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6433 F.isVarArg(), Ins,
6434 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006435
6436 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006437 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006438 "LowerFormalArguments didn't return a valid chain!");
6439 assert(InVals.size() == Ins.size() &&
6440 "LowerFormalArguments didn't emit the correct number of values!");
6441 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6442 assert(InVals[i].getNode() &&
6443 "LowerFormalArguments emitted a null value!");
6444 assert(Ins[i].VT == InVals[i].getValueType() &&
6445 "LowerFormalArguments emitted a value with the wrong type!");
6446 });
6447
6448 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006449 DAG.setRoot(NewRoot);
6450
6451 // Set up the argument values.
6452 unsigned i = 0;
6453 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006454 if (!FLI.CanLowerReturn) {
6455 // Create a virtual register for the sret pointer, and put in a copy
6456 // from the sret argument into it.
6457 SmallVector<EVT, 1> ValueVTs;
6458 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6459 EVT VT = ValueVTs[0];
6460 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6461 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6462 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT,
6463 VT, AssertOp);
6464
Dan Gohman2048b852009-11-23 18:04:58 +00006465 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006466 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6467 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6468 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00006469 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006470 DAG.setRoot(NewRoot);
6471
6472 // i indexes lowered arguments. Bump it past the hidden sret argument.
6473 // Idx indexes LLVM arguments. Don't touch it.
6474 ++i;
6475 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00006476 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6477 ++I, ++Idx) {
6478 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006479 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006480 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006481 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006482 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006483 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006484 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6485 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006486
6487 if (!I->use_empty()) {
6488 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6489 if (F.paramHasAttr(Idx, Attribute::SExt))
6490 AssertOp = ISD::AssertSext;
6491 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6492 AssertOp = ISD::AssertZext;
6493
6494 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
6495 PartVT, VT, AssertOp));
6496 }
6497 i += NumParts;
6498 }
6499 if (!I->use_empty()) {
Dan Gohman2048b852009-11-23 18:04:58 +00006500 SDB->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
6501 SDB->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006502 // If this argument is live outside of the entry block, insert a copy from
6503 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006504 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006505 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006506 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00006507 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006508
6509 // Finally, if the target has anything special to do, allow it to do so.
6510 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006511 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006512}
6513
6514/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6515/// ensure constants are generated when needed. Remember the virtual registers
6516/// that need to be added to the Machine PHI nodes as input. We cannot just
6517/// directly add them, because expansion might result in multiple MBB's for one
6518/// BB. As such, the start of the BB might correspond to a different MBB than
6519/// the end.
6520///
6521void
6522SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6523 TerminatorInst *TI = LLVMBB->getTerminator();
6524
6525 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6526
6527 // Check successor nodes' PHI nodes that expect a constant to be available
6528 // from this block.
6529 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6530 BasicBlock *SuccBB = TI->getSuccessor(succ);
6531 if (!isa<PHINode>(SuccBB->begin())) continue;
6532 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006533
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006534 // If this terminator has multiple identical successors (common for
6535 // switches), only handle each succ once.
6536 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006537
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006538 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6539 PHINode *PN;
6540
6541 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6542 // nodes and Machine PHI nodes, but the incoming operands have not been
6543 // emitted yet.
6544 for (BasicBlock::iterator I = SuccBB->begin();
6545 (PN = dyn_cast<PHINode>(I)); ++I) {
6546 // Ignore dead phi's.
6547 if (PN->use_empty()) continue;
6548
6549 unsigned Reg;
6550 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6551
6552 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006553 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006554 if (RegOut == 0) {
6555 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006556 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006557 }
6558 Reg = RegOut;
6559 } else {
6560 Reg = FuncInfo->ValueMap[PHIOp];
6561 if (Reg == 0) {
6562 assert(isa<AllocaInst>(PHIOp) &&
6563 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6564 "Didn't codegen value into a register!??");
6565 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006566 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006567 }
6568 }
6569
6570 // Remember that this register needs to added to the machine PHI node as
6571 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006572 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006573 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6574 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006575 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006576 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006577 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006578 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006579 Reg += NumRegisters;
6580 }
6581 }
6582 }
Dan Gohman2048b852009-11-23 18:04:58 +00006583 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006584}
6585
Dan Gohman3df24e62008-09-03 23:12:08 +00006586/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6587/// supports legal types, and it emits MachineInstrs directly instead of
6588/// creating SelectionDAG nodes.
6589///
6590bool
6591SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6592 FastISel *F) {
6593 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006594
Dan Gohman3df24e62008-09-03 23:12:08 +00006595 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006596 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006597
6598 // Check successor nodes' PHI nodes that expect a constant to be available
6599 // from this block.
6600 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6601 BasicBlock *SuccBB = TI->getSuccessor(succ);
6602 if (!isa<PHINode>(SuccBB->begin())) continue;
6603 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006604
Dan Gohman3df24e62008-09-03 23:12:08 +00006605 // If this terminator has multiple identical successors (common for
6606 // switches), only handle each succ once.
6607 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006608
Dan Gohman3df24e62008-09-03 23:12:08 +00006609 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6610 PHINode *PN;
6611
6612 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6613 // nodes and Machine PHI nodes, but the incoming operands have not been
6614 // emitted yet.
6615 for (BasicBlock::iterator I = SuccBB->begin();
6616 (PN = dyn_cast<PHINode>(I)); ++I) {
6617 // Ignore dead phi's.
6618 if (PN->use_empty()) continue;
6619
6620 // Only handle legal types. Two interesting things to note here. First,
6621 // by bailing out early, we may leave behind some dead instructions,
6622 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6623 // own moves. Second, this check is necessary becuase FastISel doesn't
6624 // use CreateRegForValue to create registers, so it always creates
6625 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006626 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006627 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6628 // Promote MVT::i1.
6629 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006630 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006631 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006632 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006633 return false;
6634 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006635 }
6636
6637 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6638
6639 unsigned Reg = F->getRegForValue(PHIOp);
6640 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006641 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006642 return false;
6643 }
Dan Gohman2048b852009-11-23 18:04:58 +00006644 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006645 }
6646 }
6647
6648 return true;
6649}