blob: 8bcdfd16dc96c8759cb133fd0b348f3d80ed2c49 [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Dan Gohman2048b852009-11-23 18:04:58 +000015#include "SelectionDAGBuilder.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000016#include "FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000018#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000019#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Constants.h"
21#include "llvm/CallingConv.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/Function.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/InlineAsm.h"
26#include "llvm/Instructions.h"
27#include "llvm/Intrinsics.h"
28#include "llvm/IntrinsicInst.h"
Devang Patel53bb5c92009-11-10 23:06:00 +000029#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000030#include "llvm/Module.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000031#include "llvm/CodeGen/FastISel.h"
32#include "llvm/CodeGen/GCStrategy.h"
33#include "llvm/CodeGen/GCMetadata.h"
34#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
37#include "llvm/CodeGen/MachineJumpTableInfo.h"
38#include "llvm/CodeGen/MachineModuleInfo.h"
39#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000040#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000041#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000042#include "llvm/CodeGen/DwarfWriter.h"
43#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044#include "llvm/Target/TargetRegisterInfo.h"
45#include "llvm/Target/TargetData.h"
46#include "llvm/Target/TargetFrameInfo.h"
47#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000048#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000049#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000050#include "llvm/Target/TargetOptions.h"
51#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000052#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000053#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000054#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000056#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057#include <algorithm>
58using namespace llvm;
59
Dale Johannesen601d3c02008-09-05 01:48:15 +000060/// LimitFloatPrecision - Generate low-precision inline sequences for
61/// some float libcalls (6, 8 or 12 bits).
62static unsigned LimitFloatPrecision;
63
64static cl::opt<unsigned, true>
65LimitFPPrecision("limit-float-precision",
66 cl::desc("Generate low-precision inline sequences "
67 "for some float libcalls"),
68 cl::location(LimitFloatPrecision),
69 cl::init(0));
70
Dan Gohmanf9bd4502009-11-23 17:46:23 +000071namespace {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000072 /// RegsForValue - This struct represents the registers (physical or virtual)
73 /// that a particular set of values is assigned, and the type information about
74 /// the value. The most common situation is to represent one value at a time,
75 /// but struct or array values are handled element-wise as multiple values.
76 /// The splitting of aggregates is performed recursively, so that we never
77 /// have aggregate-typed registers. The values at this point do not necessarily
78 /// have legal types, so each value may require one or more registers of some
79 /// legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000080 ///
Dan Gohmanf9bd4502009-11-23 17:46:23 +000081 struct RegsForValue {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000082 /// TLI - The TargetLowering object.
83 ///
84 const TargetLowering *TLI;
85
86 /// ValueVTs - The value types of the values, which may not be legal, and
87 /// may need be promoted or synthesized from one or more registers.
88 ///
Owen Andersone50ed302009-08-10 22:56:29 +000089 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000091 /// RegVTs - The value types of the registers. This is the same size as
92 /// ValueVTs and it records, for each value, what the type of the assigned
93 /// register or registers are. (Individual values are never synthesized
94 /// from more than one type of register.)
95 ///
96 /// With virtual registers, the contents of RegVTs is redundant with TLI's
97 /// getRegisterType member function, however when with physical registers
98 /// it is necessary to have a separate record of the types.
99 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000100 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000102 /// Regs - This list holds the registers assigned to the values.
103 /// Each legal or promoted value requires one register, and each
104 /// expanded value requires multiple registers.
105 ///
106 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000108 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000111 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000112 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000113 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
114 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000115 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000116 const SmallVector<EVT, 4> &regvts,
117 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000118 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000119 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000120 unsigned Reg, const Type *Ty) : TLI(&tli) {
121 ComputeValueVTs(tli, Ty, ValueVTs);
122
123 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000124 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000125 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
126 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000127 for (unsigned i = 0; i != NumRegs; ++i)
128 Regs.push_back(Reg + i);
129 RegVTs.push_back(RegisterVT);
130 Reg += NumRegs;
131 }
132 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 /// append - Add the specified values to this one.
135 void append(const RegsForValue &RHS) {
136 TLI = RHS.TLI;
137 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
138 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
139 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
140 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000141
142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000143 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000144 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000145 /// Chain/Flag as the input and updates them for the output Chain/Flag.
146 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000147 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000148 SDValue &Chain, SDValue *Flag) const;
149
150 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000151 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000152 /// Chain/Flag as the input and updates them for the output Chain/Flag.
153 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000154 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000155 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000158 /// operand list. This adds the code marker, matching input operand index
159 /// (if applicable), and includes the number of values added into it.
160 void AddInlineAsmOperands(unsigned Code,
161 bool HasMatching, unsigned MatchingIdx,
162 SelectionDAG &DAG, std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000163 };
164}
165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000166/// getCopyFromParts - Create a value that contains the specified legal parts
167/// combined into the value they represent. If the parts combine to a type
168/// larger then ValueVT then AssertOp can be used to specify whether the extra
169/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
170/// (ISD::AssertSext).
Dale Johannesen66978ee2009-01-31 02:22:37 +0000171static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
172 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000173 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000174 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000175 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000176 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000177 SDValue Val = Parts[0];
178
179 if (NumParts > 1) {
180 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000181 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000182 unsigned PartBits = PartVT.getSizeInBits();
183 unsigned ValueBits = ValueVT.getSizeInBits();
184
185 // Assemble the power of 2 part.
186 unsigned RoundParts = NumParts & (NumParts - 1) ?
187 1 << Log2_32(NumParts) : NumParts;
188 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000189 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000190 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000191 SDValue Lo, Hi;
192
Owen Anderson23b9b192009-08-12 00:36:31 +0000193 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 if (RoundParts > 2) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000196 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts/2, PartVT, HalfVT);
197 Hi = getCopyFromParts(DAG, dl, Parts+RoundParts/2, RoundParts/2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000198 PartVT, HalfVT);
199 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000200 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
201 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000202 }
203 if (TLI.isBigEndian())
204 std::swap(Lo, Hi);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000205 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000206
207 if (RoundParts < NumParts) {
208 // Assemble the trailing non-power-of-2 part.
209 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000210 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Scott Michelfdc40a02009-02-17 22:15:04 +0000211 Hi = getCopyFromParts(DAG, dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000212 Parts+RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000213
214 // Combine the round and odd parts.
215 Lo = Val;
216 if (TLI.isBigEndian())
217 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000218 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000219 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
220 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000221 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000222 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000223 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
224 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000225 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000226 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000227 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000228 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000229 unsigned NumIntermediates;
230 unsigned NumRegs =
Owen Anderson23b9b192009-08-12 00:36:31 +0000231 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
232 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000233 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
234 NumParts = NumRegs; // Silence a compiler warning.
235 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
236 assert(RegisterVT == Parts[0].getValueType() &&
237 "Part type doesn't match part!");
238
239 // Assemble the parts into intermediate operands.
240 SmallVector<SDValue, 8> Ops(NumIntermediates);
241 if (NumIntermediates == NumParts) {
242 // If the register was not expanded, truncate or copy the value,
243 // as appropriate.
244 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000245 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000246 PartVT, IntermediateVT);
247 } else if (NumParts > 0) {
248 // If the intermediate type was expanded, build the intermediate operands
249 // from the parts.
250 assert(NumParts % NumIntermediates == 0 &&
251 "Must expand into a divisible number of parts!");
252 unsigned Factor = NumParts / NumIntermediates;
253 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000254 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000255 PartVT, IntermediateVT);
256 }
257
258 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
259 // operands.
260 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000261 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000262 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000263 } else if (PartVT.isFloatingPoint()) {
264 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000265 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000266 "Unexpected split");
267 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000268 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
269 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000270 if (TLI.isBigEndian())
271 std::swap(Lo, Hi);
272 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
273 } else {
274 // FP split into integer parts (soft fp)
275 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
276 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000277 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Eli Friedman2ac8b322009-05-20 06:02:09 +0000278 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000279 }
280 }
281
282 // There is now one part, held in Val. Correct it to match ValueVT.
283 PartVT = Val.getValueType();
284
285 if (PartVT == ValueVT)
286 return Val;
287
288 if (PartVT.isVector()) {
289 assert(ValueVT.isVector() && "Unknown vector conversion!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000290 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000291 }
292
293 if (ValueVT.isVector()) {
294 assert(ValueVT.getVectorElementType() == PartVT &&
295 ValueVT.getVectorNumElements() == 1 &&
296 "Only trivial scalar-to-vector conversions should get here!");
Evan Chenga87008d2009-02-25 22:49:59 +0000297 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000298 }
299
300 if (PartVT.isInteger() &&
301 ValueVT.isInteger()) {
302 if (ValueVT.bitsLT(PartVT)) {
303 // For a truncate, see if we have any information to
304 // indicate whether the truncated bits will always be
305 // zero or sign-extension.
306 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000307 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000308 DAG.getValueType(ValueVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000309 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000310 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000311 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000312 }
313 }
314
315 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
316 if (ValueVT.bitsLT(Val.getValueType()))
317 // FP_ROUND's are always exact here.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000318 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000319 DAG.getIntPtrConstant(1));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000320 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000321 }
322
323 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
Dale Johannesen66978ee2009-01-31 02:22:37 +0000324 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000325
Torok Edwinc23197a2009-07-14 16:55:14 +0000326 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000327 return SDValue();
328}
329
330/// getCopyToParts - Create a series of nodes that contain the specified value
331/// split into legal parts. If the parts contain more bits than Val, then, for
332/// integers, ExtendKind can be used to specify how to generate the extra bits.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000333static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, SDValue Val,
Owen Andersone50ed302009-08-10 22:56:29 +0000334 SDValue *Parts, unsigned NumParts, EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000336 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000337 EVT PtrVT = TLI.getPointerTy();
338 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000339 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000340 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
342
343 if (!NumParts)
344 return;
345
346 if (!ValueVT.isVector()) {
347 if (PartVT == ValueVT) {
348 assert(NumParts == 1 && "No-op copy with multiple parts!");
349 Parts[0] = Val;
350 return;
351 }
352
353 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
354 // If the parts cover more bits than the value has, promote the value.
355 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
356 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000357 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000358 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000359 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000360 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000361 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000362 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000363 }
364 } else if (PartBits == ValueVT.getSizeInBits()) {
365 // Different types of the same size.
366 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000367 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000368 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
369 // If the parts cover less bits than value has, truncate the value.
370 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000371 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000372 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000373 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000374 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000375 }
376 }
377
378 // The value may have changed - recompute ValueVT.
379 ValueVT = Val.getValueType();
380 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
381 "Failed to tile the value with PartVT!");
382
383 if (NumParts == 1) {
384 assert(PartVT == ValueVT && "Type conversion failed!");
385 Parts[0] = Val;
386 return;
387 }
388
389 // Expand the value into multiple parts.
390 if (NumParts & (NumParts - 1)) {
391 // The number of parts is not a power of 2. Split off and copy the tail.
392 assert(PartVT.isInteger() && ValueVT.isInteger() &&
393 "Do not know what to expand to!");
394 unsigned RoundParts = 1 << Log2_32(NumParts);
395 unsigned RoundBits = RoundParts * PartBits;
396 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000397 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000398 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000399 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000400 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts, OddParts, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000401 if (TLI.isBigEndian())
402 // The odd parts were reversed by getCopyToParts - unreverse them.
403 std::reverse(Parts + RoundParts, Parts + NumParts);
404 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000405 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000406 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000407 }
408
409 // The number of parts is a power of 2. Repeatedly bisect the value using
410 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000411 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson23b9b192009-08-12 00:36:31 +0000412 EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000413 Val);
414 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
415 for (unsigned i = 0; i < NumParts; i += StepSize) {
416 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000417 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000418 SDValue &Part0 = Parts[i];
419 SDValue &Part1 = Parts[i+StepSize/2];
420
Scott Michelfdc40a02009-02-17 22:15:04 +0000421 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000422 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000423 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000424 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000425 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000426 DAG.getConstant(0, PtrVT));
427
428 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000429 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000430 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000431 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000432 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000433 }
434 }
435 }
436
437 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000438 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000439
440 return;
441 }
442
443 // Vector ValueVT.
444 if (NumParts == 1) {
445 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000446 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000447 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000448 } else {
449 assert(ValueVT.getVectorElementType() == PartVT &&
450 ValueVT.getVectorNumElements() == 1 &&
451 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000452 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000453 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000454 DAG.getConstant(0, PtrVT));
455 }
456 }
457
458 Parts[0] = Val;
459 return;
460 }
461
462 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000463 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000464 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000465 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
466 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000467 unsigned NumElements = ValueVT.getVectorNumElements();
468
469 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
470 NumParts = NumRegs; // Silence a compiler warning.
471 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
472
473 // Split the vector into intermediate operands.
474 SmallVector<SDValue, 8> Ops(NumIntermediates);
475 for (unsigned i = 0; i != NumIntermediates; ++i)
476 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000477 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000478 IntermediateVT, Val,
479 DAG.getConstant(i * (NumElements / NumIntermediates),
480 PtrVT));
481 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000482 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000483 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000484 DAG.getConstant(i, PtrVT));
485
486 // Split the intermediate operands into legal parts.
487 if (NumParts == NumIntermediates) {
488 // If the register was not expanded, promote or copy the value,
489 // as appropriate.
490 for (unsigned i = 0; i != NumParts; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000491 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000492 } else if (NumParts > 0) {
493 // If the intermediate type was expanded, split each the value into
494 // legal parts.
495 assert(NumParts % NumIntermediates == 0 &&
496 "Must expand into a divisible number of parts!");
497 unsigned Factor = NumParts / NumIntermediates;
498 for (unsigned i = 0; i != NumIntermediates; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000499 getCopyToParts(DAG, dl, Ops[i], &Parts[i * Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000500 }
501}
502
503
Dan Gohman2048b852009-11-23 18:04:58 +0000504void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000505 AA = &aa;
506 GFI = gfi;
507 TD = DAG.getTarget().getTargetData();
508}
509
510/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000511/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000512/// for a new block. This doesn't clear out information about
513/// additional blocks that are needed to complete switch lowering
514/// or PHI node updating; that information is cleared out as it is
515/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000516void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000517 NodeMap.clear();
518 PendingLoads.clear();
519 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000520 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000521 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000522 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000523 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000524}
525
526/// getRoot - Return the current virtual root of the Selection DAG,
527/// flushing any PendingLoad items. This must be done before emitting
528/// a store or any other node that may need to be ordered after any
529/// prior load instructions.
530///
Dan Gohman2048b852009-11-23 18:04:58 +0000531SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000532 if (PendingLoads.empty())
533 return DAG.getRoot();
534
535 if (PendingLoads.size() == 1) {
536 SDValue Root = PendingLoads[0];
537 DAG.setRoot(Root);
538 PendingLoads.clear();
539 return Root;
540 }
541
542 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000543 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000544 &PendingLoads[0], PendingLoads.size());
545 PendingLoads.clear();
546 DAG.setRoot(Root);
547 return Root;
548}
549
550/// getControlRoot - Similar to getRoot, but instead of flushing all the
551/// PendingLoad items, flush all the PendingExports items. It is necessary
552/// to do this before emitting a terminator instruction.
553///
Dan Gohman2048b852009-11-23 18:04:58 +0000554SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000555 SDValue Root = DAG.getRoot();
556
557 if (PendingExports.empty())
558 return Root;
559
560 // Turn all of the CopyToReg chains into one factored node.
561 if (Root.getOpcode() != ISD::EntryToken) {
562 unsigned i = 0, e = PendingExports.size();
563 for (; i != e; ++i) {
564 assert(PendingExports[i].getNode()->getNumOperands() > 1);
565 if (PendingExports[i].getNode()->getOperand(0) == Root)
566 break; // Don't add the root if we already indirectly depend on it.
567 }
568
569 if (i == e)
570 PendingExports.push_back(Root);
571 }
572
Owen Anderson825b72b2009-08-11 20:47:22 +0000573 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000574 &PendingExports[0],
575 PendingExports.size());
576 PendingExports.clear();
577 DAG.setRoot(Root);
578 return Root;
579}
580
Dan Gohman2048b852009-11-23 18:04:58 +0000581void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000582 visit(I.getOpcode(), I);
583}
584
Dan Gohman2048b852009-11-23 18:04:58 +0000585void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000586 // We're processing a new instruction.
587 ++SDNodeOrder;
588
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000589 // Note: this doesn't use InstVisitor, because it has to work with
590 // ConstantExpr's in addition to instructions.
591 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000592 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000593 // Build the switch statement using the Instruction.def file.
594#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000595 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000596#include "llvm/Instruction.def"
597 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000598}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000599
Dan Gohman2048b852009-11-23 18:04:58 +0000600SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000601 SDValue &N = NodeMap[V];
602 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000603
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000604 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000605 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000606
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000607 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000608 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000609
610 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
611 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000613 if (isa<ConstantPointerNull>(C))
614 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000615
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000616 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000617 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000618
Nate Begeman9008ca62009-04-27 18:41:29 +0000619 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000620 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000621
622 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
623 visit(CE->getOpcode(), *CE);
624 SDValue N1 = NodeMap[V];
625 assert(N1.getNode() && "visit didn't populate the ValueMap!");
626 return N1;
627 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000628
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000629 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
630 SmallVector<SDValue, 4> Constants;
631 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
632 OI != OE; ++OI) {
633 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000634 // If the operand is an empty aggregate, there are no values.
635 if (!Val) continue;
636 // Add each leaf value from the operand to the Constants list
637 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000638 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
639 Constants.push_back(SDValue(Val, i));
640 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000641 return DAG.getMergeValues(&Constants[0], Constants.size(),
642 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000643 }
644
645 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
646 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
647 "Unknown struct or array constant!");
648
Owen Andersone50ed302009-08-10 22:56:29 +0000649 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000650 ComputeValueVTs(TLI, C->getType(), ValueVTs);
651 unsigned NumElts = ValueVTs.size();
652 if (NumElts == 0)
653 return SDValue(); // empty struct
654 SmallVector<SDValue, 4> Constants(NumElts);
655 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000656 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000657 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000658 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000659 else if (EltVT.isFloatingPoint())
660 Constants[i] = DAG.getConstantFP(0, EltVT);
661 else
662 Constants[i] = DAG.getConstant(0, EltVT);
663 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000664 return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000665 }
666
Dan Gohman8c2b5252009-10-30 01:27:03 +0000667 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000668 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000669
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000670 const VectorType *VecTy = cast<VectorType>(V->getType());
671 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000672
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000673 // Now that we know the number and type of the elements, get that number of
674 // elements into the Ops array based on what kind of constant it is.
675 SmallVector<SDValue, 16> Ops;
676 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
677 for (unsigned i = 0; i != NumElements; ++i)
678 Ops.push_back(getValue(CP->getOperand(i)));
679 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000680 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000681 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682
683 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000684 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000685 Op = DAG.getConstantFP(0, EltVT);
686 else
687 Op = DAG.getConstant(0, EltVT);
688 Ops.assign(NumElements, Op);
689 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000691 // Create a BUILD_VECTOR node.
Evan Chenga87008d2009-02-25 22:49:59 +0000692 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
693 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000694 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000695
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000696 // If this is a static alloca, generate it as the frameindex instead of
697 // computation.
698 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
699 DenseMap<const AllocaInst*, int>::iterator SI =
700 FuncInfo.StaticAllocaMap.find(AI);
701 if (SI != FuncInfo.StaticAllocaMap.end())
702 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
703 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000705 unsigned InReg = FuncInfo.ValueMap[V];
706 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000707
Owen Anderson23b9b192009-08-12 00:36:31 +0000708 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000709 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +0000710 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000711}
712
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000713/// Get the EVTs and ArgFlags collections that represent the return type
714/// of the given function. This does not require a DAG or a return value, and
715/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000716static void getReturnInfo(const Type* ReturnType,
717 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000718 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000719 TargetLowering &TLI,
720 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000721 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000722 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000723 unsigned NumValues = ValueVTs.size();
724 if ( NumValues == 0 ) return;
725
726 for (unsigned j = 0, f = NumValues; j != f; ++j) {
727 EVT VT = ValueVTs[j];
728 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000729
730 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000731 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000732 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000733 ExtendKind = ISD::ZERO_EXTEND;
734
735 // FIXME: C calling convention requires the return type to be promoted to
736 // at least 32-bit. But this is not necessary for non-C calling
737 // conventions. The frontend should mark functions whose return values
738 // require promoting with signext or zeroext attributes.
739 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000740 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000741 if (VT.bitsLT(MinVT))
742 VT = MinVT;
743 }
744
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000745 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
746 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000747 // 'inreg' on function refers to return value
748 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000749 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000750 Flags.setInReg();
751
752 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000753 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000754 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000755 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000756 Flags.setZExt();
757
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000758 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000759 OutVTs.push_back(PartVT);
760 OutFlags.push_back(Flags);
761 }
762 }
763}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000764
Dan Gohman2048b852009-11-23 18:04:58 +0000765void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000766 SDValue Chain = getControlRoot();
767 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000768 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
769
770 if (!FLI.CanLowerReturn) {
771 unsigned DemoteReg = FLI.DemoteRegister;
772 const Function *F = I.getParent()->getParent();
773
774 // Emit a store of the return value through the virtual register.
775 // Leave Outs empty so that LowerReturn won't try to load return
776 // registers the usual way.
777 SmallVector<EVT, 1> PtrValueVTs;
778 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
779 PtrValueVTs);
780
781 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
782 SDValue RetOp = getValue(I.getOperand(0));
783
Owen Andersone50ed302009-08-10 22:56:29 +0000784 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000785 SmallVector<uint64_t, 4> Offsets;
786 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000787 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000788
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000789 SmallVector<SDValue, 4> Chains(NumValues);
790 EVT PtrVT = PtrValueVTs[0];
791 for (unsigned i = 0; i != NumValues; ++i)
792 Chains[i] = DAG.getStore(Chain, getCurDebugLoc(),
793 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
794 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
795 DAG.getConstant(Offsets[i], PtrVT)),
796 NULL, Offsets[i], false, 0);
797 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
798 MVT::Other, &Chains[0], NumValues);
799 }
800 else {
801 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
802 SmallVector<EVT, 4> ValueVTs;
803 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
804 unsigned NumValues = ValueVTs.size();
805 if (NumValues == 0) continue;
806
807 SDValue RetOp = getValue(I.getOperand(i));
808 for (unsigned j = 0, f = NumValues; j != f; ++j) {
809 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000810
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000811 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000812
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000813 const Function *F = I.getParent()->getParent();
814 if (F->paramHasAttr(0, Attribute::SExt))
815 ExtendKind = ISD::SIGN_EXTEND;
816 else if (F->paramHasAttr(0, Attribute::ZExt))
817 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000818
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000819 // FIXME: C calling convention requires the return type to be promoted to
820 // at least 32-bit. But this is not necessary for non-C calling
821 // conventions. The frontend should mark functions whose return values
822 // require promoting with signext or zeroext attributes.
823 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
824 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
825 if (VT.bitsLT(MinVT))
826 VT = MinVT;
827 }
828
829 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
830 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
831 SmallVector<SDValue, 4> Parts(NumParts);
832 getCopyToParts(DAG, getCurDebugLoc(),
833 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
834 &Parts[0], NumParts, PartVT, ExtendKind);
835
836 // 'inreg' on function refers to return value
837 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
838 if (F->paramHasAttr(0, Attribute::InReg))
839 Flags.setInReg();
840
841 // Propagate extension type if any
842 if (F->paramHasAttr(0, Attribute::SExt))
843 Flags.setSExt();
844 else if (F->paramHasAttr(0, Attribute::ZExt))
845 Flags.setZExt();
846
847 for (unsigned i = 0; i < NumParts; ++i)
848 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000849 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000850 }
851 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000852
853 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000854 CallingConv::ID CallConv =
855 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000856 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
857 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000858
859 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000861 "LowerReturn didn't return a valid chain!");
862
863 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000864 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000865}
866
Dan Gohmanad62f532009-04-23 23:13:24 +0000867/// CopyToExportRegsIfNeeded - If the given value has virtual registers
868/// created for it, emit nodes to copy the value into the virtual
869/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000870void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000871 if (!V->use_empty()) {
872 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
873 if (VMI != FuncInfo.ValueMap.end())
874 CopyValueToVirtualRegister(V, VMI->second);
875 }
876}
877
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000878/// ExportFromCurrentBlock - If this condition isn't known to be exported from
879/// the current basic block, add it to ValueMap now so that we'll get a
880/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000881void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000882 // No need to export constants.
883 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000885 // Already exported?
886 if (FuncInfo.isExportedInst(V)) return;
887
888 unsigned Reg = FuncInfo.InitializeRegForValue(V);
889 CopyValueToVirtualRegister(V, Reg);
890}
891
Dan Gohman2048b852009-11-23 18:04:58 +0000892bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
893 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000894 // The operands of the setcc have to be in this block. We don't know
895 // how to export them from some other block.
896 if (Instruction *VI = dyn_cast<Instruction>(V)) {
897 // Can export from current BB.
898 if (VI->getParent() == FromBB)
899 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000900
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000901 // Is already exported, noop.
902 return FuncInfo.isExportedInst(V);
903 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000905 // If this is an argument, we can export it if the BB is the entry block or
906 // if it is already exported.
907 if (isa<Argument>(V)) {
908 if (FromBB == &FromBB->getParent()->getEntryBlock())
909 return true;
910
911 // Otherwise, can only export this if it is already exported.
912 return FuncInfo.isExportedInst(V);
913 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915 // Otherwise, constants can always be exported.
916 return true;
917}
918
919static bool InBlock(const Value *V, const BasicBlock *BB) {
920 if (const Instruction *I = dyn_cast<Instruction>(V))
921 return I->getParent() == BB;
922 return true;
923}
924
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000925/// getFCmpCondCode - Return the ISD condition code corresponding to
926/// the given LLVM IR floating-point condition code. This includes
927/// consideration of global floating-point math flags.
928///
929static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
930 ISD::CondCode FPC, FOC;
931 switch (Pred) {
932 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
933 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
934 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
935 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
936 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
937 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
938 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
939 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
940 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
941 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
942 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
943 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
944 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
945 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
946 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
947 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
948 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000949 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000950 FOC = FPC = ISD::SETFALSE;
951 break;
952 }
953 if (FiniteOnlyFPMath())
954 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000955 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000956 return FPC;
957}
958
959/// getICmpCondCode - Return the ISD condition code corresponding to
960/// the given LLVM IR integer condition code.
961///
962static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
963 switch (Pred) {
964 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
965 case ICmpInst::ICMP_NE: return ISD::SETNE;
966 case ICmpInst::ICMP_SLE: return ISD::SETLE;
967 case ICmpInst::ICMP_ULE: return ISD::SETULE;
968 case ICmpInst::ICMP_SGE: return ISD::SETGE;
969 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
970 case ICmpInst::ICMP_SLT: return ISD::SETLT;
971 case ICmpInst::ICMP_ULT: return ISD::SETULT;
972 case ICmpInst::ICMP_SGT: return ISD::SETGT;
973 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
974 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000975 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000976 return ISD::SETNE;
977 }
978}
979
Dan Gohmanc2277342008-10-17 21:16:08 +0000980/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
981/// This function emits a branch and is used at the leaves of an OR or an
982/// AND operator tree.
983///
984void
Dan Gohman2048b852009-11-23 18:04:58 +0000985SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
986 MachineBasicBlock *TBB,
987 MachineBasicBlock *FBB,
988 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000989 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000990
Dan Gohmanc2277342008-10-17 21:16:08 +0000991 // If the leaf of the tree is a comparison, merge the condition into
992 // the caseblock.
993 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
994 // The operands of the cmp have to be in this block. We don't know
995 // how to export them from some other block. If this is the first block
996 // of the sequence, no exporting is needed.
997 if (CurBB == CurMBB ||
998 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
999 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001000 ISD::CondCode Condition;
1001 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001002 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001003 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001004 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001005 } else {
1006 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001007 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001008 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001009
1010 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001011 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1012 SwitchCases.push_back(CB);
1013 return;
1014 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001015 }
1016
1017 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001018 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001019 NULL, TBB, FBB, CurBB);
1020 SwitchCases.push_back(CB);
1021}
1022
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001023/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001024void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1025 MachineBasicBlock *TBB,
1026 MachineBasicBlock *FBB,
1027 MachineBasicBlock *CurBB,
1028 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001029 // If this node is not part of the or/and tree, emit it as a branch.
1030 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001031 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001032 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1033 BOp->getParent() != CurBB->getBasicBlock() ||
1034 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1035 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1036 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001037 return;
1038 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001039
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001040 // Create TmpBB after CurBB.
1041 MachineFunction::iterator BBI = CurBB;
1042 MachineFunction &MF = DAG.getMachineFunction();
1043 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1044 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001046 if (Opc == Instruction::Or) {
1047 // Codegen X | Y as:
1048 // jmp_if_X TBB
1049 // jmp TmpBB
1050 // TmpBB:
1051 // jmp_if_Y TBB
1052 // jmp FBB
1053 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001055 // Emit the LHS condition.
1056 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001057
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001058 // Emit the RHS condition into TmpBB.
1059 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1060 } else {
1061 assert(Opc == Instruction::And && "Unknown merge op!");
1062 // Codegen X & Y as:
1063 // jmp_if_X TmpBB
1064 // jmp FBB
1065 // TmpBB:
1066 // jmp_if_Y TBB
1067 // jmp FBB
1068 //
1069 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001070
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001071 // Emit the LHS condition.
1072 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001074 // Emit the RHS condition into TmpBB.
1075 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1076 }
1077}
1078
1079/// If the set of cases should be emitted as a series of branches, return true.
1080/// If we should emit this as a bunch of and/or'd together conditions, return
1081/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082bool
Dan Gohman2048b852009-11-23 18:04:58 +00001083SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001084 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001086 // If this is two comparisons of the same values or'd or and'd together, they
1087 // will get folded into a single comparison, so don't emit two blocks.
1088 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1089 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1090 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1091 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1092 return false;
1093 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001095 return true;
1096}
1097
Dan Gohman2048b852009-11-23 18:04:58 +00001098void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001099 // Update machine-CFG edges.
1100 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1101
1102 // Figure out which block is immediately after the current one.
1103 MachineBasicBlock *NextBlock = 0;
1104 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001105 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001106 NextBlock = BBI;
1107
1108 if (I.isUnconditional()) {
1109 // Update machine-CFG edges.
1110 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001112 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001113 if (Succ0MBB != NextBlock) {
1114 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001115 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001116 DAG.getBasicBlock(Succ0MBB));
1117 DAG.setRoot(V);
1118
1119 if (DisableScheduling)
1120 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1121 }
1122
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001123 return;
1124 }
1125
1126 // If this condition is one of the special cases we handle, do special stuff
1127 // now.
1128 Value *CondVal = I.getCondition();
1129 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1130
1131 // If this is a series of conditions that are or'd or and'd together, emit
1132 // this as a sequence of branches instead of setcc's with and/or operations.
1133 // For example, instead of something like:
1134 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001135 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001136 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001137 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001138 // or C, F
1139 // jnz foo
1140 // Emit:
1141 // cmp A, B
1142 // je foo
1143 // cmp D, E
1144 // jle foo
1145 //
1146 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001147 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001148 (BOp->getOpcode() == Instruction::And ||
1149 BOp->getOpcode() == Instruction::Or)) {
1150 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1151 // If the compares in later blocks need to use values not currently
1152 // exported from this block, export them now. This block should always
1153 // be the first entry.
1154 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001156 // Allow some cases to be rejected.
1157 if (ShouldEmitAsBranches(SwitchCases)) {
1158 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1159 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1160 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1161 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001162
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001163 // Emit the branch for this block.
1164 visitSwitchCase(SwitchCases[0]);
1165 SwitchCases.erase(SwitchCases.begin());
1166 return;
1167 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001169 // Okay, we decided not to do this, remove any inserted MBB's and clear
1170 // SwitchCases.
1171 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001172 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001174 SwitchCases.clear();
1175 }
1176 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001178 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001179 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001180 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001181
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001182 // Use visitSwitchCase to actually insert the fast branch sequence for this
1183 // cond branch.
1184 visitSwitchCase(CB);
1185}
1186
1187/// visitSwitchCase - Emits the necessary code to represent a single node in
1188/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001189void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001190 SDValue Cond;
1191 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001192 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001193
1194 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001195 if (CB.CmpMHS == NULL) {
1196 // Fold "(X == true)" to X and "(X == false)" to !X to
1197 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001198 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001199 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001200 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001201 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001202 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001203 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001204 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001206 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 } else {
1208 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1209
Anton Korobeynikov23218582008-12-23 22:25:27 +00001210 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1211 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212
1213 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001214 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001215
1216 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001217 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001218 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001219 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001220 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001221 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001222 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 DAG.getConstant(High-Low, VT), ISD::SETULE);
1224 }
1225 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001226
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001227 // Update successor info
1228 CurMBB->addSuccessor(CB.TrueBB);
1229 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001230
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001231 // Set NextBlock to be the MBB immediately after the current one, if any.
1232 // This is used to avoid emitting unnecessary branches to the next block.
1233 MachineBasicBlock *NextBlock = 0;
1234 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001235 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001237
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001238 // If the lhs block is the next block, invert the condition so that we can
1239 // fall through to the lhs instead of the rhs block.
1240 if (CB.TrueBB == NextBlock) {
1241 std::swap(CB.TrueBB, CB.FalseBB);
1242 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001243 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001244 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001245
Dale Johannesenf5d97892009-02-04 01:48:28 +00001246 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001247 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001248 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001249
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001250 // If the branch was constant folded, fix up the CFG.
1251 if (BrCond.getOpcode() == ISD::BR) {
1252 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001253 } else {
1254 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001255 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001256 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001257
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001258 if (CB.FalseBB != NextBlock)
1259 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1260 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001261 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001262
1263 DAG.setRoot(BrCond);
1264
1265 if (DisableScheduling)
1266 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001267}
1268
1269/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001270void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001271 // Emit the code for the jump table
1272 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001273 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001274 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1275 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001276 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001277 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1278 MVT::Other, Index.getValue(1),
1279 Table, Index);
1280 DAG.setRoot(BrJumpTable);
1281
1282 if (DisableScheduling)
1283 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001284}
1285
1286/// visitJumpTableHeader - This function emits necessary code to produce index
1287/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001288void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1289 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001290 // Subtract the lowest switch case value from the value being switched on and
1291 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001292 // difference between smallest and largest cases.
1293 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001294 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001295 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001296 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001297
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001298 // The SDNode we just created, which holds the value being switched on minus
1299 // the the smallest case value, needs to be copied to a virtual register so it
1300 // can be used as an index into the jump table in a subsequent basic block.
1301 // This value may be smaller or larger than the target's pointer type, and
1302 // therefore require extension or truncating.
Duncan Sands3a66a682009-10-13 21:04:12 +00001303 SwitchOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001304
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001305 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001306 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1307 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001308 JT.Reg = JumpTableReg;
1309
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001310 // Emit the range check for the jump table, and branch to the default block
1311 // for the switch statement if the value being switched on exceeds the largest
1312 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001313 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
1314 TLI.getSetCCResultType(SUB.getValueType()), SUB,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001315 DAG.getConstant(JTH.Last-JTH.First,VT),
1316 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001317
1318 // Set NextBlock to be the MBB immediately after the current one, if any.
1319 // This is used to avoid emitting unnecessary branches to the next block.
1320 MachineBasicBlock *NextBlock = 0;
1321 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001322 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001323 NextBlock = BBI;
1324
Dale Johannesen66978ee2009-01-31 02:22:37 +00001325 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001326 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001327 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001328
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001329 if (JT.MBB != NextBlock)
1330 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1331 DAG.getBasicBlock(JT.MBB));
1332
1333 DAG.setRoot(BrCond);
1334
1335 if (DisableScheduling)
1336 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001337}
1338
1339/// visitBitTestHeader - This function emits necessary code to produce value
1340/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001341void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001342 // Subtract the minimum value
1343 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001344 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001345 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001346 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347
1348 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001349 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
1350 TLI.getSetCCResultType(SUB.getValueType()),
1351 SUB, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001352 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001353
Duncan Sands3a66a682009-10-13 21:04:12 +00001354 SDValue ShiftOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001355
Duncan Sands92abc622009-01-31 15:50:11 +00001356 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001357 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1358 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001359
1360 // Set NextBlock to be the MBB immediately after the current one, if any.
1361 // This is used to avoid emitting unnecessary branches to the next block.
1362 MachineBasicBlock *NextBlock = 0;
1363 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001364 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 NextBlock = BBI;
1366
1367 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1368
1369 CurMBB->addSuccessor(B.Default);
1370 CurMBB->addSuccessor(MBB);
1371
Dale Johannesen66978ee2009-01-31 02:22:37 +00001372 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001373 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001374 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001375
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001376 if (MBB != NextBlock)
1377 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1378 DAG.getBasicBlock(MBB));
1379
1380 DAG.setRoot(BrRange);
1381
1382 if (DisableScheduling)
1383 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001384}
1385
1386/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001387void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1388 unsigned Reg,
1389 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001390 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001391 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001392 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001393 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001394 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001395 DAG.getConstant(1, TLI.getPointerTy()),
1396 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001397
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001398 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001399 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001400 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001401 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001402 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1403 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001404 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001405 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406
1407 CurMBB->addSuccessor(B.TargetBB);
1408 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001409
Dale Johannesen66978ee2009-01-31 02:22:37 +00001410 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001411 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001412 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001413
1414 // Set NextBlock to be the MBB immediately after the current one, if any.
1415 // This is used to avoid emitting unnecessary branches to the next block.
1416 MachineBasicBlock *NextBlock = 0;
1417 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001418 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001419 NextBlock = BBI;
1420
Bill Wendling0777e922009-12-21 21:59:52 +00001421 if (NextMBB != NextBlock)
1422 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1423 DAG.getBasicBlock(NextMBB));
1424
1425 DAG.setRoot(BrAnd);
1426
1427 if (DisableScheduling)
1428 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001429}
1430
Dan Gohman2048b852009-11-23 18:04:58 +00001431void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001432 // Retrieve successors.
1433 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1434 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1435
Gabor Greifb67e6b32009-01-15 11:10:44 +00001436 const Value *Callee(I.getCalledValue());
1437 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001438 visitInlineAsm(&I);
1439 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001440 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001441
1442 // If the value of the invoke is used outside of its defining block, make it
1443 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001444 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445
1446 // Update successor info
1447 CurMBB->addSuccessor(Return);
1448 CurMBB->addSuccessor(LandingPad);
1449
1450 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001451 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1452 MVT::Other, getControlRoot(),
1453 DAG.getBasicBlock(Return));
1454 DAG.setRoot(Branch);
1455
1456 if (DisableScheduling)
1457 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458}
1459
Dan Gohman2048b852009-11-23 18:04:58 +00001460void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001461}
1462
1463/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1464/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001465bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1466 CaseRecVector& WorkList,
1467 Value* SV,
1468 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001469 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001470
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001471 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001472 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001474 return false;
1475
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001476 // Get the MachineFunction which holds the current MBB. This is used when
1477 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001478 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001479
1480 // Figure out which block is immediately after the current one.
1481 MachineBasicBlock *NextBlock = 0;
1482 MachineFunction::iterator BBI = CR.CaseBB;
1483
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001484 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485 NextBlock = BBI;
1486
1487 // TODO: If any two of the cases has the same destination, and if one value
1488 // is the same as the other, but has one bit unset that the other has set,
1489 // use bit manipulation to do two compares at once. For example:
1490 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001491
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001492 // Rearrange the case blocks so that the last one falls through if possible.
1493 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1494 // The last case block won't fall through into 'NextBlock' if we emit the
1495 // branches in this order. See if rearranging a case value would help.
1496 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1497 if (I->BB == NextBlock) {
1498 std::swap(*I, BackCase);
1499 break;
1500 }
1501 }
1502 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001503
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001504 // Create a CaseBlock record representing a conditional branch to
1505 // the Case's target mbb if the value being switched on SV is equal
1506 // to C.
1507 MachineBasicBlock *CurBlock = CR.CaseBB;
1508 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1509 MachineBasicBlock *FallThrough;
1510 if (I != E-1) {
1511 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1512 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001513
1514 // Put SV in a virtual register to make it available from the new blocks.
1515 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001516 } else {
1517 // If the last case doesn't match, go to the default block.
1518 FallThrough = Default;
1519 }
1520
1521 Value *RHS, *LHS, *MHS;
1522 ISD::CondCode CC;
1523 if (I->High == I->Low) {
1524 // This is just small small case range :) containing exactly 1 case
1525 CC = ISD::SETEQ;
1526 LHS = SV; RHS = I->High; MHS = NULL;
1527 } else {
1528 CC = ISD::SETLE;
1529 LHS = I->Low; MHS = SV; RHS = I->High;
1530 }
1531 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001532
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001533 // If emitting the first comparison, just call visitSwitchCase to emit the
1534 // code into the current block. Otherwise, push the CaseBlock onto the
1535 // vector to be later processed by SDISel, and insert the node's MBB
1536 // before the next MBB.
1537 if (CurBlock == CurMBB)
1538 visitSwitchCase(CB);
1539 else
1540 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001541
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542 CurBlock = FallThrough;
1543 }
1544
1545 return true;
1546}
1547
1548static inline bool areJTsAllowed(const TargetLowering &TLI) {
1549 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001550 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1551 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001553
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001554static APInt ComputeRange(const APInt &First, const APInt &Last) {
1555 APInt LastExt(Last), FirstExt(First);
1556 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1557 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1558 return (LastExt - FirstExt + 1ULL);
1559}
1560
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001561/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001562bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1563 CaseRecVector& WorkList,
1564 Value* SV,
1565 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001566 Case& FrontCase = *CR.Range.first;
1567 Case& BackCase = *(CR.Range.second-1);
1568
Chris Lattnere880efe2009-11-07 07:50:34 +00001569 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1570 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001571
Chris Lattnere880efe2009-11-07 07:50:34 +00001572 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001573 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1574 I!=E; ++I)
1575 TSize += I->size();
1576
Chris Lattnere880efe2009-11-07 07:50:34 +00001577 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001578 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001579
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001580 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001581 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001582 if (Density < 0.4)
1583 return false;
1584
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001585 DEBUG(errs() << "Lowering jump table\n"
1586 << "First entry: " << First << ". Last entry: " << Last << '\n'
1587 << "Range: " << Range
1588 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001589
1590 // Get the MachineFunction which holds the current MBB. This is used when
1591 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001592 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593
1594 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001595 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001596 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001597
1598 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1599
1600 // Create a new basic block to hold the code for loading the address
1601 // of the jump table, and jumping to it. Update successor information;
1602 // we will either branch to the default case for the switch, or the jump
1603 // table.
1604 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1605 CurMF->insert(BBI, JumpTableBB);
1606 CR.CaseBB->addSuccessor(Default);
1607 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001608
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609 // Build a vector of destination BBs, corresponding to each target
1610 // of the jump table. If the value of the jump table slot corresponds to
1611 // a case statement, push the case's BB onto the vector, otherwise, push
1612 // the default BB.
1613 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001614 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001615 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001616 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1617 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1618
1619 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 DestBBs.push_back(I->BB);
1621 if (TEI==High)
1622 ++I;
1623 } else {
1624 DestBBs.push_back(Default);
1625 }
1626 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001627
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001628 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001629 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1630 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001631 E = DestBBs.end(); I != E; ++I) {
1632 if (!SuccsHandled[(*I)->getNumber()]) {
1633 SuccsHandled[(*I)->getNumber()] = true;
1634 JumpTableBB->addSuccessor(*I);
1635 }
1636 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001637
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638 // Create a jump table index for this jump table, or return an existing
1639 // one.
1640 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001641
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001642 // Set the jump table information so that we can codegen it as a second
1643 // MachineBasicBlock
1644 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1645 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1646 if (CR.CaseBB == CurMBB)
1647 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001648
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001649 JTCases.push_back(JumpTableBlock(JTH, JT));
1650
1651 return true;
1652}
1653
1654/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1655/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001656bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1657 CaseRecVector& WorkList,
1658 Value* SV,
1659 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001660 // Get the MachineFunction which holds the current MBB. This is used when
1661 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001662 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001663
1664 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001665 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001666 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001667
1668 Case& FrontCase = *CR.Range.first;
1669 Case& BackCase = *(CR.Range.second-1);
1670 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1671
1672 // Size is the number of Cases represented by this range.
1673 unsigned Size = CR.Range.second - CR.Range.first;
1674
Chris Lattnere880efe2009-11-07 07:50:34 +00001675 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1676 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001677 double FMetric = 0;
1678 CaseItr Pivot = CR.Range.first + Size/2;
1679
1680 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1681 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001682 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001683 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1684 I!=E; ++I)
1685 TSize += I->size();
1686
Chris Lattnere880efe2009-11-07 07:50:34 +00001687 APInt LSize = FrontCase.size();
1688 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001689 DEBUG(errs() << "Selecting best pivot: \n"
1690 << "First: " << First << ", Last: " << Last <<'\n'
1691 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001692 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1693 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001694 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1695 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001696 APInt Range = ComputeRange(LEnd, RBegin);
1697 assert((Range - 2ULL).isNonNegative() &&
1698 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001699 double LDensity = (double)LSize.roundToDouble() /
1700 (LEnd - First + 1ULL).roundToDouble();
1701 double RDensity = (double)RSize.roundToDouble() /
1702 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001703 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001704 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001705 DEBUG(errs() <<"=>Step\n"
1706 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1707 << "LDensity: " << LDensity
1708 << ", RDensity: " << RDensity << '\n'
1709 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001710 if (FMetric < Metric) {
1711 Pivot = J;
1712 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001713 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 }
1715
1716 LSize += J->size();
1717 RSize -= J->size();
1718 }
1719 if (areJTsAllowed(TLI)) {
1720 // If our case is dense we *really* should handle it earlier!
1721 assert((FMetric > 0) && "Should handle dense range earlier!");
1722 } else {
1723 Pivot = CR.Range.first + Size/2;
1724 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 CaseRange LHSR(CR.Range.first, Pivot);
1727 CaseRange RHSR(Pivot, CR.Range.second);
1728 Constant *C = Pivot->Low;
1729 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001730
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001731 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001732 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001733 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001734 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001735 // Pivot's Value, then we can branch directly to the LHS's Target,
1736 // rather than creating a leaf node for it.
1737 if ((LHSR.second - LHSR.first) == 1 &&
1738 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001739 cast<ConstantInt>(C)->getValue() ==
1740 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741 TrueBB = LHSR.first->BB;
1742 } else {
1743 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1744 CurMF->insert(BBI, TrueBB);
1745 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001746
1747 // Put SV in a virtual register to make it available from the new blocks.
1748 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001749 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001750
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001751 // Similar to the optimization above, if the Value being switched on is
1752 // known to be less than the Constant CR.LT, and the current Case Value
1753 // is CR.LT - 1, then we can branch directly to the target block for
1754 // the current Case Value, rather than emitting a RHS leaf node for it.
1755 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001756 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1757 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001758 FalseBB = RHSR.first->BB;
1759 } else {
1760 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1761 CurMF->insert(BBI, FalseBB);
1762 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001763
1764 // Put SV in a virtual register to make it available from the new blocks.
1765 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001766 }
1767
1768 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001769 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001770 // Otherwise, branch to LHS.
1771 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1772
1773 if (CR.CaseBB == CurMBB)
1774 visitSwitchCase(CB);
1775 else
1776 SwitchCases.push_back(CB);
1777
1778 return true;
1779}
1780
1781/// handleBitTestsSwitchCase - if current case range has few destination and
1782/// range span less, than machine word bitwidth, encode case range into series
1783/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001784bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1785 CaseRecVector& WorkList,
1786 Value* SV,
1787 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001788 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001789 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001790
1791 Case& FrontCase = *CR.Range.first;
1792 Case& BackCase = *(CR.Range.second-1);
1793
1794 // Get the MachineFunction which holds the current MBB. This is used when
1795 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001796 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001797
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001798 // If target does not have legal shift left, do not emit bit tests at all.
1799 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1800 return false;
1801
Anton Korobeynikov23218582008-12-23 22:25:27 +00001802 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001803 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1804 I!=E; ++I) {
1805 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001806 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001807 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001808
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001809 // Count unique destinations
1810 SmallSet<MachineBasicBlock*, 4> Dests;
1811 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1812 Dests.insert(I->BB);
1813 if (Dests.size() > 3)
1814 // Don't bother the code below, if there are too much unique destinations
1815 return false;
1816 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001817 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1818 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001820 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001821 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1822 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001823 APInt cmpRange = maxValue - minValue;
1824
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001825 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1826 << "Low bound: " << minValue << '\n'
1827 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001828
1829 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001830 (!(Dests.size() == 1 && numCmps >= 3) &&
1831 !(Dests.size() == 2 && numCmps >= 5) &&
1832 !(Dests.size() >= 3 && numCmps >= 6)))
1833 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001834
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001835 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001836 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1837
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001838 // Optimize the case where all the case values fit in a
1839 // word without having to subtract minValue. In this case,
1840 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001841 if (minValue.isNonNegative() &&
1842 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1843 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001844 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001845 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001846 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001847
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001848 CaseBitsVector CasesBits;
1849 unsigned i, count = 0;
1850
1851 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1852 MachineBasicBlock* Dest = I->BB;
1853 for (i = 0; i < count; ++i)
1854 if (Dest == CasesBits[i].BB)
1855 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001856
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001857 if (i == count) {
1858 assert((count < 3) && "Too much destinations to test!");
1859 CasesBits.push_back(CaseBits(0, Dest, 0));
1860 count++;
1861 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001862
1863 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1864 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1865
1866 uint64_t lo = (lowValue - lowBound).getZExtValue();
1867 uint64_t hi = (highValue - lowBound).getZExtValue();
1868
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001869 for (uint64_t j = lo; j <= hi; j++) {
1870 CasesBits[i].Mask |= 1ULL << j;
1871 CasesBits[i].Bits++;
1872 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001873
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001874 }
1875 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001877 BitTestInfo BTC;
1878
1879 // Figure out which block is immediately after the current one.
1880 MachineFunction::iterator BBI = CR.CaseBB;
1881 ++BBI;
1882
1883 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1884
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001885 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001887 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
1888 << ", Bits: " << CasesBits[i].Bits
1889 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001890
1891 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1892 CurMF->insert(BBI, CaseBB);
1893 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1894 CaseBB,
1895 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001896
1897 // Put SV in a virtual register to make it available from the new blocks.
1898 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001899 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001900
1901 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001902 -1U, (CR.CaseBB == CurMBB),
1903 CR.CaseBB, Default, BTC);
1904
1905 if (CR.CaseBB == CurMBB)
1906 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001907
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001908 BitTestCases.push_back(BTB);
1909
1910 return true;
1911}
1912
1913
1914/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001915size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1916 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001917 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001918
1919 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001920 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1922 Cases.push_back(Case(SI.getSuccessorValue(i),
1923 SI.getSuccessorValue(i),
1924 SMBB));
1925 }
1926 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1927
1928 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001929 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001930 // Must recompute end() each iteration because it may be
1931 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001932 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1933 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1934 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001935 MachineBasicBlock* nextBB = J->BB;
1936 MachineBasicBlock* currentBB = I->BB;
1937
1938 // If the two neighboring cases go to the same destination, merge them
1939 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001940 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941 I->High = J->High;
1942 J = Cases.erase(J);
1943 } else {
1944 I = J++;
1945 }
1946 }
1947
1948 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1949 if (I->Low != I->High)
1950 // A range counts double, since it requires two compares.
1951 ++numCmps;
1952 }
1953
1954 return numCmps;
1955}
1956
Dan Gohman2048b852009-11-23 18:04:58 +00001957void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001958 // Figure out which block is immediately after the current one.
1959 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001960
1961 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1962
1963 // If there is only the default destination, branch to it if it is not the
1964 // next basic block. Otherwise, just fall through.
1965 if (SI.getNumOperands() == 2) {
1966 // Update machine-CFG edges.
1967
1968 // If this is not a fall-through branch, emit the branch.
1969 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00001970 if (Default != NextBlock) {
1971 SDValue Val = DAG.getNode(ISD::BR, getCurDebugLoc(),
1972 MVT::Other, getControlRoot(),
1973 DAG.getBasicBlock(Default));
1974 DAG.setRoot(Val);
1975
1976 if (DisableScheduling)
1977 DAG.AssignOrdering(Val.getNode(), SDNodeOrder);
1978 }
1979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001980 return;
1981 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001982
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001983 // If there are any non-default case statements, create a vector of Cases
1984 // representing each one, and sort the vector so that we can efficiently
1985 // create a binary search tree from them.
1986 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001987 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001988 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
1989 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001990 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001991
1992 // Get the Value to be switched on and default basic blocks, which will be
1993 // inserted into CaseBlock records, representing basic blocks in the binary
1994 // search tree.
1995 Value *SV = SI.getOperand(0);
1996
1997 // Push the initial CaseRec onto the worklist
1998 CaseRecVector WorkList;
1999 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2000
2001 while (!WorkList.empty()) {
2002 // Grab a record representing a case range to process off the worklist
2003 CaseRec CR = WorkList.back();
2004 WorkList.pop_back();
2005
2006 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2007 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002008
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002009 // If the range has few cases (two or less) emit a series of specific
2010 // tests.
2011 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2012 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002013
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002014 // If the switch has more than 5 blocks, and at least 40% dense, and the
2015 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002016 // lowering the switch to a binary tree of conditional branches.
2017 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2018 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002019
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002020 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2021 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2022 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2023 }
2024}
2025
Dan Gohman2048b852009-11-23 18:04:58 +00002026void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002027 // Update machine-CFG edges.
2028 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2029 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2030
Bill Wendling49fcff82009-12-21 22:30:11 +00002031 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2032 MVT::Other, getControlRoot(),
2033 getValue(I.getAddress()));
2034 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002035
Bill Wendling49fcff82009-12-21 22:30:11 +00002036 if (DisableScheduling)
2037 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2038}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039
Dan Gohman2048b852009-11-23 18:04:58 +00002040void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002041 // -0.0 - X --> fneg
2042 const Type *Ty = I.getType();
2043 if (isa<VectorType>(Ty)) {
2044 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2045 const VectorType *DestTy = cast<VectorType>(I.getType());
2046 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002047 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002048 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002049 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002050 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002051 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002052 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2053 Op2.getValueType(), Op2);
2054 setValue(&I, Res);
2055
2056 if (DisableScheduling)
2057 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2058
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002059 return;
2060 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002061 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002062 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002063
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002064 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002065 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002066 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002067 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2068 Op2.getValueType(), Op2);
2069 setValue(&I, Res);
2070
2071 if (DisableScheduling)
2072 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2073
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002074 return;
2075 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002076
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002077 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002078}
2079
Dan Gohman2048b852009-11-23 18:04:58 +00002080void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081 SDValue Op1 = getValue(I.getOperand(0));
2082 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002083 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2084 Op1.getValueType(), Op1, Op2);
2085 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002086
Bill Wendling49fcff82009-12-21 22:30:11 +00002087 if (DisableScheduling)
2088 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002089}
2090
Dan Gohman2048b852009-11-23 18:04:58 +00002091void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002092 SDValue Op1 = getValue(I.getOperand(0));
2093 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002094 if (!isa<VectorType>(I.getType()) &&
2095 Op2.getValueType() != TLI.getShiftAmountTy()) {
2096 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002097 EVT PTy = TLI.getPointerTy();
2098 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002099 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002100 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2101 TLI.getShiftAmountTy(), Op2);
2102 // If the operand is larger than the shift count type but the shift
2103 // count type has enough bits to represent any shift value, truncate
2104 // it now. This is a common case and it exposes the truncate to
2105 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002106 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002107 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2108 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2109 TLI.getShiftAmountTy(), Op2);
2110 // Otherwise we'll need to temporarily settle for some other
2111 // convenient type; type legalization will make adjustments as
2112 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002113 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002114 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002115 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002116 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002117 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002118 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002119 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002120
Bill Wendling49fcff82009-12-21 22:30:11 +00002121 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2122 Op1.getValueType(), Op1, Op2);
2123 setValue(&I, Res);
2124
2125 if (DisableScheduling)
2126 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127}
2128
Dan Gohman2048b852009-11-23 18:04:58 +00002129void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002130 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2131 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2132 predicate = IC->getPredicate();
2133 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2134 predicate = ICmpInst::Predicate(IC->getPredicate());
2135 SDValue Op1 = getValue(I.getOperand(0));
2136 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002137 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002138
Owen Andersone50ed302009-08-10 22:56:29 +00002139 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002140 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2141 setValue(&I, Res);
2142
2143 if (DisableScheduling)
2144 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002145}
2146
Dan Gohman2048b852009-11-23 18:04:58 +00002147void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002148 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2149 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2150 predicate = FC->getPredicate();
2151 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2152 predicate = FCmpInst::Predicate(FC->getPredicate());
2153 SDValue Op1 = getValue(I.getOperand(0));
2154 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002155 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002156 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002157 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2158 setValue(&I, Res);
2159
2160 if (DisableScheduling)
2161 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002162}
2163
Dan Gohman2048b852009-11-23 18:04:58 +00002164void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002165 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002166 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2167 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002168 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002169
Bill Wendling49fcff82009-12-21 22:30:11 +00002170 SmallVector<SDValue, 4> Values(NumValues);
2171 SDValue Cond = getValue(I.getOperand(0));
2172 SDValue TrueVal = getValue(I.getOperand(1));
2173 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002174
Bill Wendling49fcff82009-12-21 22:30:11 +00002175 for (unsigned i = 0; i != NumValues; ++i) {
2176 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2177 TrueVal.getNode()->getValueType(i), Cond,
2178 SDValue(TrueVal.getNode(),
2179 TrueVal.getResNo() + i),
2180 SDValue(FalseVal.getNode(),
2181 FalseVal.getResNo() + i));
2182
2183 if (DisableScheduling)
2184 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002185 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002186
Bill Wendling49fcff82009-12-21 22:30:11 +00002187 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2188 DAG.getVTList(&ValueVTs[0], NumValues),
2189 &Values[0], NumValues);
2190 setValue(&I, Res);
2191
2192 if (DisableScheduling)
2193 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2194}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002195
Dan Gohman2048b852009-11-23 18:04:58 +00002196void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002197 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2198 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002199 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002200 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2201 setValue(&I, Res);
2202
2203 if (DisableScheduling)
2204 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205}
2206
Dan Gohman2048b852009-11-23 18:04:58 +00002207void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2209 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2210 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002211 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002212 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2213 setValue(&I, Res);
2214
2215 if (DisableScheduling)
2216 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002217}
2218
Dan Gohman2048b852009-11-23 18:04:58 +00002219void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002220 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2221 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2222 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002223 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002224 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2225 setValue(&I, Res);
2226
2227 if (DisableScheduling)
2228 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002229}
2230
Dan Gohman2048b852009-11-23 18:04:58 +00002231void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002232 // FPTrunc is never a no-op cast, no need to check
2233 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002234 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002235 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2236 DestVT, N, DAG.getIntPtrConstant(0));
2237 setValue(&I, Res);
2238
2239 if (DisableScheduling)
2240 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241}
2242
Dan Gohman2048b852009-11-23 18:04:58 +00002243void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002244 // FPTrunc is never a no-op cast, no need to check
2245 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002246 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002247 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2248 setValue(&I, Res);
2249
2250 if (DisableScheduling)
2251 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252}
2253
Dan Gohman2048b852009-11-23 18:04:58 +00002254void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002255 // FPToUI is never a no-op cast, no need to check
2256 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002257 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002258 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2259 setValue(&I, Res);
2260
2261 if (DisableScheduling)
2262 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002263}
2264
Dan Gohman2048b852009-11-23 18:04:58 +00002265void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002266 // FPToSI is never a no-op cast, no need to check
2267 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002268 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002269 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2270 setValue(&I, Res);
2271
2272 if (DisableScheduling)
2273 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002274}
2275
Dan Gohman2048b852009-11-23 18:04:58 +00002276void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002277 // UIToFP is never a no-op cast, no need to check
2278 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002279 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002280 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2281 setValue(&I, Res);
2282
2283 if (DisableScheduling)
2284 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002285}
2286
Dan Gohman2048b852009-11-23 18:04:58 +00002287void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002288 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002289 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002290 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002291 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2292 setValue(&I, Res);
2293
2294 if (DisableScheduling)
2295 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002296}
2297
Dan Gohman2048b852009-11-23 18:04:58 +00002298void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002299 // What to do depends on the size of the integer and the size of the pointer.
2300 // We can either truncate, zero extend, or no-op, accordingly.
2301 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002302 EVT SrcVT = N.getValueType();
2303 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002304 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2305 setValue(&I, Res);
2306
2307 if (DisableScheduling)
2308 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002309}
2310
Dan Gohman2048b852009-11-23 18:04:58 +00002311void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002312 // What to do depends on the size of the integer and the size of the pointer.
2313 // We can either truncate, zero extend, or no-op, accordingly.
2314 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002315 EVT SrcVT = N.getValueType();
2316 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002317 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2318 setValue(&I, Res);
2319
2320 if (DisableScheduling)
2321 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002322}
2323
Dan Gohman2048b852009-11-23 18:04:58 +00002324void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002325 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002326 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002327
Bill Wendling49fcff82009-12-21 22:30:11 +00002328 // BitCast assures us that source and destination are the same size so this is
2329 // either a BIT_CONVERT or a no-op.
2330 if (DestVT != N.getValueType()) {
2331 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2332 DestVT, N); // convert types.
2333 setValue(&I, Res);
2334
2335 if (DisableScheduling)
2336 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2337 } else {
2338 setValue(&I, N); // noop cast.
2339 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002340}
2341
Dan Gohman2048b852009-11-23 18:04:58 +00002342void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002343 SDValue InVec = getValue(I.getOperand(0));
2344 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002345 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002346 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002347 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002348 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2349 TLI.getValueType(I.getType()),
2350 InVec, InVal, InIdx);
2351 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002352
Bill Wendling49fcff82009-12-21 22:30:11 +00002353 if (DisableScheduling)
2354 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002355}
2356
Dan Gohman2048b852009-11-23 18:04:58 +00002357void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002358 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002359 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002360 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002361 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002362 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2363 TLI.getValueType(I.getType()), InVec, InIdx);
2364 setValue(&I, Res);
2365
2366 if (DisableScheduling)
2367 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002368}
2369
Mon P Wangaeb06d22008-11-10 04:46:22 +00002370
2371// Utility for visitShuffleVector - Returns true if the mask is mask starting
2372// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002373static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2374 unsigned MaskNumElts = Mask.size();
2375 for (unsigned i = 0; i != MaskNumElts; ++i)
2376 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002377 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002378 return true;
2379}
2380
Dan Gohman2048b852009-11-23 18:04:58 +00002381void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002382 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002383 SDValue Src1 = getValue(I.getOperand(0));
2384 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002385
Nate Begeman9008ca62009-04-27 18:41:29 +00002386 // Convert the ConstantVector mask operand into an array of ints, with -1
2387 // representing undef values.
2388 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002389 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2390 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002391 unsigned MaskNumElts = MaskElts.size();
2392 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002393 if (isa<UndefValue>(MaskElts[i]))
2394 Mask.push_back(-1);
2395 else
2396 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2397 }
2398
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT VT = TLI.getValueType(I.getType());
2400 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002401 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002402
Mon P Wangc7849c22008-11-16 05:06:27 +00002403 if (SrcNumElts == MaskNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002404 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2405 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002406 return;
2407 }
2408
2409 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002410 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2411 // Mask is longer than the source vectors and is a multiple of the source
2412 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002413 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002414 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2415 // The shuffle is concatenating two vectors together.
Scott Michelfdc40a02009-02-17 22:15:04 +00002416 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002417 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002418 return;
2419 }
2420
Mon P Wangc7849c22008-11-16 05:06:27 +00002421 // Pad both vectors with undefs to make them the same length as the mask.
2422 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002423 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2424 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002425 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002426
Nate Begeman9008ca62009-04-27 18:41:29 +00002427 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2428 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002429 MOps1[0] = Src1;
2430 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002431
2432 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2433 getCurDebugLoc(), VT,
2434 &MOps1[0], NumConcat);
2435 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2436 getCurDebugLoc(), VT,
2437 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002438
Mon P Wangaeb06d22008-11-10 04:46:22 +00002439 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002440 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002441 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002442 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002443 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002444 MappedOps.push_back(Idx);
2445 else
2446 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002447 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002448 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2449 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002450 return;
2451 }
2452
Mon P Wangc7849c22008-11-16 05:06:27 +00002453 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002454 // Analyze the access pattern of the vector to see if we can extract
2455 // two subvectors and do the shuffle. The analysis is done by calculating
2456 // the range of elements the mask access on both vectors.
2457 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2458 int MaxRange[2] = {-1, -1};
2459
Nate Begeman5a5ca152009-04-29 05:20:52 +00002460 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002461 int Idx = Mask[i];
2462 int Input = 0;
2463 if (Idx < 0)
2464 continue;
2465
Nate Begeman5a5ca152009-04-29 05:20:52 +00002466 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002467 Input = 1;
2468 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002469 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002470 if (Idx > MaxRange[Input])
2471 MaxRange[Input] = Idx;
2472 if (Idx < MinRange[Input])
2473 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002474 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002475
Mon P Wangc7849c22008-11-16 05:06:27 +00002476 // Check if the access is smaller than the vector size and can we find
2477 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002478 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002479 int StartIdx[2]; // StartIdx to extract from
2480 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002481 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002482 RangeUse[Input] = 0; // Unused
2483 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002484 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002485 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002486 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002487 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002488 RangeUse[Input] = 1; // Extract from beginning of the vector
2489 StartIdx[Input] = 0;
2490 } else {
2491 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002492 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002493 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002494 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002495 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002496 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002497 }
2498
Bill Wendling636e2582009-08-21 18:16:06 +00002499 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002500 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002501 return;
2502 }
2503 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2504 // Extract appropriate subvector and generate a vector shuffle
2505 for (int Input=0; Input < 2; ++Input) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002506 SDValue& Src = Input == 0 ? Src1 : Src2;
Mon P Wangc7849c22008-11-16 05:06:27 +00002507 if (RangeUse[Input] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002508 Src = DAG.getUNDEF(VT);
Mon P Wangc7849c22008-11-16 05:06:27 +00002509 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002510 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002511 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002512 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002513 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002514 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002515 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002516 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002517 int Idx = Mask[i];
2518 if (Idx < 0)
2519 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002520 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002521 MappedOps.push_back(Idx - StartIdx[0]);
2522 else
2523 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002524 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002525 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2526 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002527 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002528 }
2529 }
2530
Mon P Wangc7849c22008-11-16 05:06:27 +00002531 // We can't use either concat vectors or extract subvectors so fall back to
2532 // replacing the shuffle with extract and build vector.
2533 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002534 EVT EltVT = VT.getVectorElementType();
2535 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002536 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002537 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002538 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002539 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002540 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002541 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002542 if (Idx < (int)SrcNumElts)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002543 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002544 EltVT, Src1, DAG.getConstant(Idx, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002545 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002546 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Scott Michelfdc40a02009-02-17 22:15:04 +00002547 EltVT, Src2,
Mon P Wangc7849c22008-11-16 05:06:27 +00002548 DAG.getConstant(Idx - SrcNumElts, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002549 }
2550 }
Evan Chenga87008d2009-02-25 22:49:59 +00002551 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2552 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002553}
2554
Dan Gohman2048b852009-11-23 18:04:58 +00002555void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002556 const Value *Op0 = I.getOperand(0);
2557 const Value *Op1 = I.getOperand(1);
2558 const Type *AggTy = I.getType();
2559 const Type *ValTy = Op1->getType();
2560 bool IntoUndef = isa<UndefValue>(Op0);
2561 bool FromUndef = isa<UndefValue>(Op1);
2562
2563 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2564 I.idx_begin(), I.idx_end());
2565
Owen Andersone50ed302009-08-10 22:56:29 +00002566 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002567 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002568 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002569 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2570
2571 unsigned NumAggValues = AggValueVTs.size();
2572 unsigned NumValValues = ValValueVTs.size();
2573 SmallVector<SDValue, 4> Values(NumAggValues);
2574
2575 SDValue Agg = getValue(Op0);
2576 SDValue Val = getValue(Op1);
2577 unsigned i = 0;
2578 // Copy the beginning value(s) from the original aggregate.
2579 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002580 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002581 SDValue(Agg.getNode(), Agg.getResNo() + i);
2582 // Copy values from the inserted value(s).
2583 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002584 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002585 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2586 // Copy remaining value(s) from the original aggregate.
2587 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002588 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002589 SDValue(Agg.getNode(), Agg.getResNo() + i);
2590
Scott Michelfdc40a02009-02-17 22:15:04 +00002591 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002592 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2593 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002594}
2595
Dan Gohman2048b852009-11-23 18:04:58 +00002596void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002597 const Value *Op0 = I.getOperand(0);
2598 const Type *AggTy = Op0->getType();
2599 const Type *ValTy = I.getType();
2600 bool OutOfUndef = isa<UndefValue>(Op0);
2601
2602 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2603 I.idx_begin(), I.idx_end());
2604
Owen Andersone50ed302009-08-10 22:56:29 +00002605 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002606 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2607
2608 unsigned NumValValues = ValValueVTs.size();
2609 SmallVector<SDValue, 4> Values(NumValValues);
2610
2611 SDValue Agg = getValue(Op0);
2612 // Copy out the selected value(s).
2613 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2614 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002615 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002616 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002617 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002618
Scott Michelfdc40a02009-02-17 22:15:04 +00002619 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002620 DAG.getVTList(&ValValueVTs[0], NumValValues),
2621 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002622}
2623
2624
Dan Gohman2048b852009-11-23 18:04:58 +00002625void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002626 SDValue N = getValue(I.getOperand(0));
2627 const Type *Ty = I.getOperand(0)->getType();
2628
2629 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2630 OI != E; ++OI) {
2631 Value *Idx = *OI;
2632 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2633 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2634 if (Field) {
2635 // N = N + Offset
2636 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002637 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002638 DAG.getIntPtrConstant(Offset));
2639 }
2640 Ty = StTy->getElementType(Field);
2641 } else {
2642 Ty = cast<SequentialType>(Ty)->getElementType();
2643
2644 // If this is a constant subscript, handle it quickly.
2645 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2646 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002647 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002648 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002649 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002650 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002651 unsigned PtrBits = PTy.getSizeInBits();
Evan Cheng65b52df2009-02-09 21:01:06 +00002652 if (PtrBits < 64) {
2653 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2654 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002655 DAG.getConstant(Offs, MVT::i64));
Evan Cheng65b52df2009-02-09 21:01:06 +00002656 } else
Evan Chengb1032a82009-02-09 20:54:38 +00002657 OffsVal = DAG.getIntPtrConstant(Offs);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002658 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002659 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002660 continue;
2661 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002663 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002664 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2665 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002666 SDValue IdxN = getValue(Idx);
2667
2668 // If the index is smaller or larger than intptr_t, truncate or extend
2669 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002670 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002671
2672 // If this is a multiply by a power of two, turn it into a shl
2673 // immediately. This is a very common case.
2674 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002675 if (ElementSize.isPowerOf2()) {
2676 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002677 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002678 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002679 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002680 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002681 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002682 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002683 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002684 }
2685 }
2686
Scott Michelfdc40a02009-02-17 22:15:04 +00002687 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002688 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002689 }
2690 }
2691 setValue(&I, N);
2692}
2693
Dan Gohman2048b852009-11-23 18:04:58 +00002694void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002695 // If this is a fixed sized alloca in the entry block of the function,
2696 // allocate it statically on the stack.
2697 if (FuncInfo.StaticAllocaMap.count(&I))
2698 return; // getValue will auto-populate this.
2699
2700 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002701 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002702 unsigned Align =
2703 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2704 I.getAlignment());
2705
2706 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002707
2708 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2709 AllocSize,
2710 DAG.getConstant(TySize, AllocSize.getValueType()));
2711
2712
2713
Owen Andersone50ed302009-08-10 22:56:29 +00002714 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002715 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002716
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002717 // Handle alignment. If the requested alignment is less than or equal to
2718 // the stack alignment, ignore it. If the size is greater than or equal to
2719 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2720 unsigned StackAlign =
2721 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2722 if (Align <= StackAlign)
2723 Align = 0;
2724
2725 // Round the size of the allocation up to the stack alignment size
2726 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002727 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002728 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002729 DAG.getIntPtrConstant(StackAlign-1));
2730 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002731 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002732 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002733 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2734
2735 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002736 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002737 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002738 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002739 setValue(&I, DSA);
2740 DAG.setRoot(DSA.getValue(1));
2741
2742 // Inform the Frame Information that we have just allocated a variable-sized
2743 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002744 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002745}
2746
Dan Gohman2048b852009-11-23 18:04:58 +00002747void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002748 const Value *SV = I.getOperand(0);
2749 SDValue Ptr = getValue(SV);
2750
2751 const Type *Ty = I.getType();
2752 bool isVolatile = I.isVolatile();
2753 unsigned Alignment = I.getAlignment();
2754
Owen Andersone50ed302009-08-10 22:56:29 +00002755 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002756 SmallVector<uint64_t, 4> Offsets;
2757 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2758 unsigned NumValues = ValueVTs.size();
2759 if (NumValues == 0)
2760 return;
2761
2762 SDValue Root;
2763 bool ConstantMemory = false;
2764 if (I.isVolatile())
2765 // Serialize volatile loads with other side effects.
2766 Root = getRoot();
2767 else if (AA->pointsToConstantMemory(SV)) {
2768 // Do not serialize (non-volatile) loads of constant memory with anything.
2769 Root = DAG.getEntryNode();
2770 ConstantMemory = true;
2771 } else {
2772 // Do not serialize non-volatile loads against each other.
2773 Root = DAG.getRoot();
2774 }
2775
2776 SmallVector<SDValue, 4> Values(NumValues);
2777 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002778 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002779 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002780 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Nate Begemane6798372009-09-15 00:13:12 +00002781 DAG.getNode(ISD::ADD, getCurDebugLoc(),
2782 PtrVT, Ptr,
2783 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002784 SV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002785 Values[i] = L;
2786 Chains[i] = L.getValue(1);
2787 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002788
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002789 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002790 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002791 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792 &Chains[0], NumValues);
2793 if (isVolatile)
2794 DAG.setRoot(Chain);
2795 else
2796 PendingLoads.push_back(Chain);
2797 }
2798
Scott Michelfdc40a02009-02-17 22:15:04 +00002799 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002800 DAG.getVTList(&ValueVTs[0], NumValues),
2801 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002802}
2803
2804
Dan Gohman2048b852009-11-23 18:04:58 +00002805void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002806 Value *SrcV = I.getOperand(0);
2807 Value *PtrV = I.getOperand(1);
2808
Owen Andersone50ed302009-08-10 22:56:29 +00002809 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002810 SmallVector<uint64_t, 4> Offsets;
2811 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2812 unsigned NumValues = ValueVTs.size();
2813 if (NumValues == 0)
2814 return;
2815
2816 // Get the lowered operands. Note that we do this after
2817 // checking if NumResults is zero, because with zero results
2818 // the operands won't have values in the map.
2819 SDValue Src = getValue(SrcV);
2820 SDValue Ptr = getValue(PtrV);
2821
2822 SDValue Root = getRoot();
2823 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002824 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 bool isVolatile = I.isVolatile();
2826 unsigned Alignment = I.getAlignment();
2827 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002828 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002829 SDValue(Src.getNode(), Src.getResNo() + i),
Scott Michelfdc40a02009-02-17 22:15:04 +00002830 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002831 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002833 PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002834
Scott Michelfdc40a02009-02-17 22:15:04 +00002835 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002836 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002837}
2838
2839/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2840/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00002841void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
2842 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843 bool HasChain = !I.doesNotAccessMemory();
2844 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2845
2846 // Build the operand list.
2847 SmallVector<SDValue, 8> Ops;
2848 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2849 if (OnlyLoad) {
2850 // We don't need to serialize loads against other loads.
2851 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002852 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002853 Ops.push_back(getRoot());
2854 }
2855 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002856
2857 // Info is set by getTgtMemInstrinsic
2858 TargetLowering::IntrinsicInfo Info;
2859 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2860
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002861 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002862 if (!IsTgtIntrinsic)
2863 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002864
2865 // Add all operands of the call to the operand list.
2866 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2867 SDValue Op = getValue(I.getOperand(i));
2868 assert(TLI.isTypeLegal(Op.getValueType()) &&
2869 "Intrinsic uses a non-legal type?");
2870 Ops.push_back(Op);
2871 }
2872
Owen Andersone50ed302009-08-10 22:56:29 +00002873 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002874 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2875#ifndef NDEBUG
2876 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2877 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2878 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002879 }
Bob Wilson8d919552009-07-31 22:41:21 +00002880#endif // NDEBUG
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002882 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002883
Bob Wilson8d919552009-07-31 22:41:21 +00002884 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002885
2886 // Create the node.
2887 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002888 if (IsTgtIntrinsic) {
2889 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002890 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002891 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002892 Info.memVT, Info.ptrVal, Info.offset,
2893 Info.align, Info.vol,
2894 Info.readMem, Info.writeMem);
2895 }
2896 else if (!HasChain)
Scott Michelfdc40a02009-02-17 22:15:04 +00002897 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002898 VTs, &Ops[0], Ops.size());
Owen Anderson1d0be152009-08-13 21:58:54 +00002899 else if (I.getType() != Type::getVoidTy(*DAG.getContext()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002900 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002901 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002902 else
Scott Michelfdc40a02009-02-17 22:15:04 +00002903 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002904 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002905
2906 if (HasChain) {
2907 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2908 if (OnlyLoad)
2909 PendingLoads.push_back(Chain);
2910 else
2911 DAG.setRoot(Chain);
2912 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002913 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002914 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002915 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002916 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002917 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002918 setValue(&I, Result);
2919 }
2920}
2921
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002922/// GetSignificand - Get the significand and build it into a floating-point
2923/// number with exponent of 1:
2924///
2925/// Op = (Op & 0x007fffff) | 0x3f800000;
2926///
2927/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002928static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002929GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002930 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2931 DAG.getConstant(0x007fffff, MVT::i32));
2932 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2933 DAG.getConstant(0x3f800000, MVT::i32));
2934 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002935}
2936
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002937/// GetExponent - Get the exponent:
2938///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002939/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002940///
2941/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002942static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002943GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
2944 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002945 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2946 DAG.getConstant(0x7f800000, MVT::i32));
2947 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002948 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2950 DAG.getConstant(127, MVT::i32));
2951 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002952}
2953
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002954/// getF32Constant - Get 32-bit floating point constant.
2955static SDValue
2956getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002958}
2959
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002960/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002961/// visitIntrinsicCall: I is a call instruction
2962/// Op is the associated NodeType for I
2963const char *
Dan Gohman2048b852009-11-23 18:04:58 +00002964SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002965 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002966 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002967 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002968 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002969 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002970 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002971 getValue(I.getOperand(2)),
2972 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002973 setValue(&I, L);
2974 DAG.setRoot(L.getValue(1));
2975 return 0;
2976}
2977
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002978// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002979const char *
Dan Gohman2048b852009-11-23 18:04:58 +00002980SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002981 SDValue Op1 = getValue(I.getOperand(1));
2982 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002983
Owen Anderson825b72b2009-08-11 20:47:22 +00002984 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00002985 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00002986
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002987 setValue(&I, Result);
2988 return 0;
2989}
Bill Wendling74c37652008-12-09 22:08:41 +00002990
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002991/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2992/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002993void
Dan Gohman2048b852009-11-23 18:04:58 +00002994SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002995 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002996 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002997
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002999 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3000 SDValue Op = getValue(I.getOperand(1));
3001
3002 // Put the exponent in the right bit position for later addition to the
3003 // final result:
3004 //
3005 // #define LOG2OFe 1.4426950f
3006 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003007 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003008 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003009 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003010
3011 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003012 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3013 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003014
3015 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003016 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003017 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003018
3019 if (LimitFloatPrecision <= 6) {
3020 // For floating-point precision of 6:
3021 //
3022 // TwoToFractionalPartOfX =
3023 // 0.997535578f +
3024 // (0.735607626f + 0.252464424f * x) * x;
3025 //
3026 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003027 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003028 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003030 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003031 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3032 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003033 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003034 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003035
3036 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003037 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003038 TwoToFracPartOfX, IntegerPartOfX);
3039
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003041 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3042 // For floating-point precision of 12:
3043 //
3044 // TwoToFractionalPartOfX =
3045 // 0.999892986f +
3046 // (0.696457318f +
3047 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3048 //
3049 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003050 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003051 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003052 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003053 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003054 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3055 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003056 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003057 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3058 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003059 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003060 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003061
3062 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003063 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003064 TwoToFracPartOfX, IntegerPartOfX);
3065
Owen Anderson825b72b2009-08-11 20:47:22 +00003066 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003067 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3068 // For floating-point precision of 18:
3069 //
3070 // TwoToFractionalPartOfX =
3071 // 0.999999982f +
3072 // (0.693148872f +
3073 // (0.240227044f +
3074 // (0.554906021e-1f +
3075 // (0.961591928e-2f +
3076 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3077 //
3078 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003079 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003080 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003082 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003083 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3084 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003085 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003086 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3087 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003088 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3090 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003091 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003092 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3093 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003094 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003095 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3096 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003097 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003098 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003099 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003100
3101 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003102 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003103 TwoToFracPartOfX, IntegerPartOfX);
3104
Owen Anderson825b72b2009-08-11 20:47:22 +00003105 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003106 }
3107 } else {
3108 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003109 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003110 getValue(I.getOperand(1)).getValueType(),
3111 getValue(I.getOperand(1)));
3112 }
3113
Dale Johannesen59e577f2008-09-05 18:38:42 +00003114 setValue(&I, result);
3115}
3116
Bill Wendling39150252008-09-09 20:39:27 +00003117/// visitLog - Lower a log intrinsic. Handles the special sequences for
3118/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003119void
Dan Gohman2048b852009-11-23 18:04:58 +00003120SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003121 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003122 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003123
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003125 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3126 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003127 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003128
3129 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003130 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003131 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003132 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003133
3134 // Get the significand and build it into a floating-point number with
3135 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003136 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003137
3138 if (LimitFloatPrecision <= 6) {
3139 // For floating-point precision of 6:
3140 //
3141 // LogofMantissa =
3142 // -1.1609546f +
3143 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003144 //
Bill Wendling39150252008-09-09 20:39:27 +00003145 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003146 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003147 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003149 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3151 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003152 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003153
Scott Michelfdc40a02009-02-17 22:15:04 +00003154 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003156 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3157 // For floating-point precision of 12:
3158 //
3159 // LogOfMantissa =
3160 // -1.7417939f +
3161 // (2.8212026f +
3162 // (-1.4699568f +
3163 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3164 //
3165 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003166 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003167 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003169 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003170 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3171 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003172 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003173 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3174 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003175 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003176 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3177 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003178 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003179
Scott Michelfdc40a02009-02-17 22:15:04 +00003180 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003181 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003182 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3183 // For floating-point precision of 18:
3184 //
3185 // LogOfMantissa =
3186 // -2.1072184f +
3187 // (4.2372794f +
3188 // (-3.7029485f +
3189 // (2.2781945f +
3190 // (-0.87823314f +
3191 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3192 //
3193 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003194 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003195 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003196 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003197 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3199 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003200 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003201 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3202 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003203 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003204 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3205 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003206 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003207 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3208 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003209 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003210 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3211 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003212 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003213
Scott Michelfdc40a02009-02-17 22:15:04 +00003214 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003215 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003216 }
3217 } else {
3218 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003219 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003220 getValue(I.getOperand(1)).getValueType(),
3221 getValue(I.getOperand(1)));
3222 }
3223
Dale Johannesen59e577f2008-09-05 18:38:42 +00003224 setValue(&I, result);
3225}
3226
Bill Wendling3eb59402008-09-09 00:28:24 +00003227/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3228/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003229void
Dan Gohman2048b852009-11-23 18:04:58 +00003230SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003231 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003232 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003233
Owen Anderson825b72b2009-08-11 20:47:22 +00003234 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003235 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3236 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003237 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003238
Bill Wendling39150252008-09-09 20:39:27 +00003239 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003240 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003241
3242 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003243 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003244 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003245
Bill Wendling3eb59402008-09-09 00:28:24 +00003246 // Different possible minimax approximations of significand in
3247 // floating-point for various degrees of accuracy over [1,2].
3248 if (LimitFloatPrecision <= 6) {
3249 // For floating-point precision of 6:
3250 //
3251 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3252 //
3253 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003254 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003255 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003256 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003257 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003258 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3259 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003260 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003261
Scott Michelfdc40a02009-02-17 22:15:04 +00003262 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003263 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003264 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3265 // For floating-point precision of 12:
3266 //
3267 // Log2ofMantissa =
3268 // -2.51285454f +
3269 // (4.07009056f +
3270 // (-2.12067489f +
3271 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003272 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003273 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003274 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003275 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003276 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003277 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003278 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3279 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003280 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003281 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3282 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003283 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003284 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3285 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003286 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003287
Scott Michelfdc40a02009-02-17 22:15:04 +00003288 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003290 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3291 // For floating-point precision of 18:
3292 //
3293 // Log2ofMantissa =
3294 // -3.0400495f +
3295 // (6.1129976f +
3296 // (-5.3420409f +
3297 // (3.2865683f +
3298 // (-1.2669343f +
3299 // (0.27515199f -
3300 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3301 //
3302 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003303 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003304 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003306 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003307 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3308 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003309 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003310 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3311 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003312 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3314 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003315 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3317 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003318 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3320 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003321 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003322
Scott Michelfdc40a02009-02-17 22:15:04 +00003323 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003324 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003325 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003326 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003327 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003328 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003329 getValue(I.getOperand(1)).getValueType(),
3330 getValue(I.getOperand(1)));
3331 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003332
Dale Johannesen59e577f2008-09-05 18:38:42 +00003333 setValue(&I, result);
3334}
3335
Bill Wendling3eb59402008-09-09 00:28:24 +00003336/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3337/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003338void
Dan Gohman2048b852009-11-23 18:04:58 +00003339SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003340 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003341 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003342
Owen Anderson825b72b2009-08-11 20:47:22 +00003343 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003344 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3345 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003347
Bill Wendling39150252008-09-09 20:39:27 +00003348 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003349 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003351 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003352
3353 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003354 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003355 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003356
3357 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003358 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003359 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003360 // Log10ofMantissa =
3361 // -0.50419619f +
3362 // (0.60948995f - 0.10380950f * x) * x;
3363 //
3364 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003366 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003368 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3370 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003371 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003372
Scott Michelfdc40a02009-02-17 22:15:04 +00003373 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003374 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003375 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3376 // For floating-point precision of 12:
3377 //
3378 // Log10ofMantissa =
3379 // -0.64831180f +
3380 // (0.91751397f +
3381 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3382 //
3383 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003384 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003385 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003386 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003387 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003388 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3389 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003390 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003391 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3392 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003393 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003394
Scott Michelfdc40a02009-02-17 22:15:04 +00003395 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003397 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003398 // For floating-point precision of 18:
3399 //
3400 // Log10ofMantissa =
3401 // -0.84299375f +
3402 // (1.5327582f +
3403 // (-1.0688956f +
3404 // (0.49102474f +
3405 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3406 //
3407 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003409 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003410 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003411 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003412 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3413 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003414 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003415 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3416 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003417 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003418 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3419 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003420 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3422 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003423 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003424
Scott Michelfdc40a02009-02-17 22:15:04 +00003425 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003426 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003427 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003428 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003429 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003430 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003431 getValue(I.getOperand(1)).getValueType(),
3432 getValue(I.getOperand(1)));
3433 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003434
Dale Johannesen59e577f2008-09-05 18:38:42 +00003435 setValue(&I, result);
3436}
3437
Bill Wendlinge10c8142008-09-09 22:39:21 +00003438/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3439/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003440void
Dan Gohman2048b852009-11-23 18:04:58 +00003441SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003442 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003443 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003444
Owen Anderson825b72b2009-08-11 20:47:22 +00003445 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003446 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3447 SDValue Op = getValue(I.getOperand(1));
3448
Owen Anderson825b72b2009-08-11 20:47:22 +00003449 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003450
3451 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003452 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3453 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003454
3455 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003457 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003458
3459 if (LimitFloatPrecision <= 6) {
3460 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003461 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003462 // TwoToFractionalPartOfX =
3463 // 0.997535578f +
3464 // (0.735607626f + 0.252464424f * x) * x;
3465 //
3466 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003468 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003470 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3472 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003473 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003474 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003475 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003477
Scott Michelfdc40a02009-02-17 22:15:04 +00003478 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003479 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003480 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3481 // For floating-point precision of 12:
3482 //
3483 // TwoToFractionalPartOfX =
3484 // 0.999892986f +
3485 // (0.696457318f +
3486 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3487 //
3488 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003489 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003490 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003492 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3494 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003495 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003496 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3497 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003498 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003499 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003500 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003501 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003502
Scott Michelfdc40a02009-02-17 22:15:04 +00003503 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003505 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3506 // For floating-point precision of 18:
3507 //
3508 // TwoToFractionalPartOfX =
3509 // 0.999999982f +
3510 // (0.693148872f +
3511 // (0.240227044f +
3512 // (0.554906021e-1f +
3513 // (0.961591928e-2f +
3514 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3515 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003516 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003517 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003518 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003519 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3521 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003522 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3524 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003525 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3527 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003528 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3530 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003531 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3533 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003534 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003536 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003538
Scott Michelfdc40a02009-02-17 22:15:04 +00003539 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003540 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003541 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003542 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003543 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003544 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003545 getValue(I.getOperand(1)).getValueType(),
3546 getValue(I.getOperand(1)));
3547 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003548
Dale Johannesen601d3c02008-09-05 01:48:15 +00003549 setValue(&I, result);
3550}
3551
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003552/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3553/// limited-precision mode with x == 10.0f.
3554void
Dan Gohman2048b852009-11-23 18:04:58 +00003555SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003556 SDValue result;
3557 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003558 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003559 bool IsExp10 = false;
3560
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 if (getValue(Val).getValueType() == MVT::f32 &&
3562 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003563 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3564 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3565 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3566 APFloat Ten(10.0f);
3567 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3568 }
3569 }
3570 }
3571
3572 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3573 SDValue Op = getValue(I.getOperand(2));
3574
3575 // Put the exponent in the right bit position for later addition to the
3576 // final result:
3577 //
3578 // #define LOG2OF10 3.3219281f
3579 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003580 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003581 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003582 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003583
3584 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003585 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3586 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003587
3588 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003589 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003590 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003591
3592 if (LimitFloatPrecision <= 6) {
3593 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003594 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003595 // twoToFractionalPartOfX =
3596 // 0.997535578f +
3597 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003598 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003599 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003601 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003602 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003603 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3605 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003606 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003607 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003608 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003610
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003611 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003612 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003613 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3614 // For floating-point precision of 12:
3615 //
3616 // TwoToFractionalPartOfX =
3617 // 0.999892986f +
3618 // (0.696457318f +
3619 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3620 //
3621 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003622 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003623 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003624 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003625 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003626 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3627 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003628 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003629 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3630 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003631 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003632 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003633 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003635
Scott Michelfdc40a02009-02-17 22:15:04 +00003636 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003637 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003638 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3639 // For floating-point precision of 18:
3640 //
3641 // TwoToFractionalPartOfX =
3642 // 0.999999982f +
3643 // (0.693148872f +
3644 // (0.240227044f +
3645 // (0.554906021e-1f +
3646 // (0.961591928e-2f +
3647 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3648 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003649 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003650 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003651 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003652 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003653 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3654 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003655 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003656 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3657 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003658 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003659 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3660 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003661 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003662 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3663 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003664 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003665 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3666 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003667 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003669 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003670 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003671
Scott Michelfdc40a02009-02-17 22:15:04 +00003672 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003673 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003674 }
3675 } else {
3676 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003677 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003678 getValue(I.getOperand(1)).getValueType(),
3679 getValue(I.getOperand(1)),
3680 getValue(I.getOperand(2)));
3681 }
3682
3683 setValue(&I, result);
3684}
3685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003686/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3687/// we want to emit this as a call to a named external function, return the name
3688/// otherwise lower it and return null.
3689const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003690SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003691 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003692 switch (Intrinsic) {
3693 default:
3694 // By default, turn this into a target intrinsic node.
3695 visitTargetIntrinsic(I, Intrinsic);
3696 return 0;
3697 case Intrinsic::vastart: visitVAStart(I); return 0;
3698 case Intrinsic::vaend: visitVAEnd(I); return 0;
3699 case Intrinsic::vacopy: visitVACopy(I); return 0;
3700 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003701 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003702 getValue(I.getOperand(1))));
3703 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003704 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003705 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003706 getValue(I.getOperand(1))));
3707 return 0;
3708 case Intrinsic::setjmp:
3709 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3710 break;
3711 case Intrinsic::longjmp:
3712 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3713 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003714 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003715 SDValue Op1 = getValue(I.getOperand(1));
3716 SDValue Op2 = getValue(I.getOperand(2));
3717 SDValue Op3 = getValue(I.getOperand(3));
3718 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003719 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003720 I.getOperand(1), 0, I.getOperand(2), 0));
3721 return 0;
3722 }
Chris Lattner824b9582008-11-21 16:42:48 +00003723 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003724 SDValue Op1 = getValue(I.getOperand(1));
3725 SDValue Op2 = getValue(I.getOperand(2));
3726 SDValue Op3 = getValue(I.getOperand(3));
3727 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003728 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003729 I.getOperand(1), 0));
3730 return 0;
3731 }
Chris Lattner824b9582008-11-21 16:42:48 +00003732 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003733 SDValue Op1 = getValue(I.getOperand(1));
3734 SDValue Op2 = getValue(I.getOperand(2));
3735 SDValue Op3 = getValue(I.getOperand(3));
3736 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3737
3738 // If the source and destination are known to not be aliases, we can
3739 // lower memmove as memcpy.
3740 uint64_t Size = -1ULL;
3741 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003742 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003743 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3744 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00003745 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003746 I.getOperand(1), 0, I.getOperand(2), 0));
3747 return 0;
3748 }
3749
Dale Johannesena04b7572009-02-03 23:04:43 +00003750 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003751 I.getOperand(1), 0, I.getOperand(2), 0));
3752 return 0;
3753 }
Devang Patel70d75ca2009-11-12 19:02:56 +00003754 case Intrinsic::dbg_stoppoint:
3755 case Intrinsic::dbg_region_start:
3756 case Intrinsic::dbg_region_end:
3757 case Intrinsic::dbg_func_start:
3758 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003759 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003760 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00003761 if (OptLevel != CodeGenOpt::None)
3762 // FIXME: Variable debug info is not supported here.
3763 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00003764 DwarfWriter *DW = DAG.getDwarfWriter();
3765 if (!DW)
3766 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003767 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3768 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
3769 return 0;
3770
Devang Patelac1ceb32009-10-09 22:42:28 +00003771 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00003772 Value *Address = DI.getAddress();
3773 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
3774 Address = BCI->getOperand(0);
3775 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
3776 // Don't handle byval struct arguments or VLAs, for example.
3777 if (!AI)
3778 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003779 DenseMap<const AllocaInst*, int>::iterator SI =
3780 FuncInfo.StaticAllocaMap.find(AI);
3781 if (SI == FuncInfo.StaticAllocaMap.end())
3782 return 0; // VLAs.
3783 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003784
Devang Patelac1ceb32009-10-09 22:42:28 +00003785 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00003786 if (MMI) {
3787 MetadataContext &TheMetadata =
3788 DI.getParent()->getContext().getMetadata();
3789 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
3790 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
3791 MMI->setVariableDbgInfo(Variable, FI, Dbg);
3792 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003793 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003794 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003795 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003796 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00003797 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003798 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003799 SDValue Ops[1];
3800 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003801 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003802 setValue(&I, Op);
3803 DAG.setRoot(Op.getValue(1));
3804 return 0;
3805 }
3806
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003807 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003808 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003809
Chris Lattner3a5815f2009-09-17 23:54:54 +00003810 if (CurMBB->isLandingPad())
3811 AddCatchInfo(I, MMI, CurMBB);
3812 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003813#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003814 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003815#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003816 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3817 unsigned Reg = TLI.getExceptionSelectorRegister();
3818 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003819 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003820
Chris Lattner3a5815f2009-09-17 23:54:54 +00003821 // Insert the EHSELECTION instruction.
3822 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3823 SDValue Ops[2];
3824 Ops[0] = getValue(I.getOperand(1));
3825 Ops[1] = getRoot();
3826 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
3827
3828 DAG.setRoot(Op.getValue(1));
3829
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003830 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003831 return 0;
3832 }
3833
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003834 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003835 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003836
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003837 if (MMI) {
3838 // Find the type id for the given typeinfo.
3839 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3840
3841 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003842 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003843 } else {
3844 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003845 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003846 }
3847
3848 return 0;
3849 }
3850
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003851 case Intrinsic::eh_return_i32:
3852 case Intrinsic::eh_return_i64:
3853 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003854 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003855 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003857 getControlRoot(),
3858 getValue(I.getOperand(1)),
3859 getValue(I.getOperand(2))));
3860 } else {
3861 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
3862 }
3863
3864 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003865 case Intrinsic::eh_unwind_init:
3866 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3867 MMI->setCallsUnwindInit(true);
3868 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003869
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003870 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003871
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003872 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00003873 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00003874 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
3875 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003876
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003877 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003878 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003879 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003880 TLI.getPointerTy()),
3881 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003882 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003883 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003884 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003885 TLI.getPointerTy(),
3886 DAG.getConstant(0,
3887 TLI.getPointerTy())),
3888 Offset));
3889 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003890 }
Mon P Wang77cdf302008-11-10 20:54:11 +00003891 case Intrinsic::convertff:
3892 case Intrinsic::convertfsi:
3893 case Intrinsic::convertfui:
3894 case Intrinsic::convertsif:
3895 case Intrinsic::convertuif:
3896 case Intrinsic::convertss:
3897 case Intrinsic::convertsu:
3898 case Intrinsic::convertus:
3899 case Intrinsic::convertuu: {
3900 ISD::CvtCode Code = ISD::CVT_INVALID;
3901 switch (Intrinsic) {
3902 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3903 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3904 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3905 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3906 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3907 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3908 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3909 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3910 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3911 }
Owen Andersone50ed302009-08-10 22:56:29 +00003912 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00003913 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00003914 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00003915 DAG.getValueType(DestVT),
3916 DAG.getValueType(getValue(Op1).getValueType()),
3917 getValue(I.getOperand(2)),
3918 getValue(I.getOperand(3)),
3919 Code));
3920 return 0;
3921 }
3922
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003923 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003924 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003925 getValue(I.getOperand(1)).getValueType(),
3926 getValue(I.getOperand(1))));
3927 return 0;
3928 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003929 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003930 getValue(I.getOperand(1)).getValueType(),
3931 getValue(I.getOperand(1)),
3932 getValue(I.getOperand(2))));
3933 return 0;
3934 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003935 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003936 getValue(I.getOperand(1)).getValueType(),
3937 getValue(I.getOperand(1))));
3938 return 0;
3939 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003940 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003941 getValue(I.getOperand(1)).getValueType(),
3942 getValue(I.getOperand(1))));
3943 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003944 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003945 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003946 return 0;
3947 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003948 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003949 return 0;
3950 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003951 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003952 return 0;
3953 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003954 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003955 return 0;
3956 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003957 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003958 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003959 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003960 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003961 return 0;
3962 case Intrinsic::pcmarker: {
3963 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003964 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003965 return 0;
3966 }
3967 case Intrinsic::readcyclecounter: {
3968 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003969 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003970 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00003971 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003972 setValue(&I, Tmp);
3973 DAG.setRoot(Tmp.getValue(1));
3974 return 0;
3975 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003976 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003977 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003978 getValue(I.getOperand(1)).getValueType(),
3979 getValue(I.getOperand(1))));
3980 return 0;
3981 case Intrinsic::cttz: {
3982 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003983 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003984 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003985 setValue(&I, result);
3986 return 0;
3987 }
3988 case Intrinsic::ctlz: {
3989 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003990 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003991 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003992 setValue(&I, result);
3993 return 0;
3994 }
3995 case Intrinsic::ctpop: {
3996 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003997 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003998 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003999 setValue(&I, result);
4000 return 0;
4001 }
4002 case Intrinsic::stacksave: {
4003 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004004 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004005 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004006 setValue(&I, Tmp);
4007 DAG.setRoot(Tmp.getValue(1));
4008 return 0;
4009 }
4010 case Intrinsic::stackrestore: {
4011 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00004012 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004013 return 0;
4014 }
Bill Wendling57344502008-11-18 11:01:33 +00004015 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004016 // Emit code into the DAG to store the stack guard onto the stack.
4017 MachineFunction &MF = DAG.getMachineFunction();
4018 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004019 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004020
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004021 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4022 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004023
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004024 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004025 MFI->setStackProtectorIndex(FI);
4026
4027 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4028
4029 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004030 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00004031 PseudoSourceValue::getFixedStack(FI),
4032 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004033 setValue(&I, Result);
4034 DAG.setRoot(Result);
4035 return 0;
4036 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004037 case Intrinsic::objectsize: {
4038 // If we don't know by now, we're never going to know.
4039 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4040
4041 assert(CI && "Non-constant type in __builtin_object_size?");
4042
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004043 SDValue Arg = getValue(I.getOperand(0));
4044 EVT Ty = Arg.getValueType();
4045
Eric Christopher7b5e6172009-10-27 00:52:25 +00004046 if (CI->getZExtValue() < 2)
Mike Stump70e5e682009-11-09 22:28:21 +00004047 setValue(&I, DAG.getConstant(-1ULL, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004048 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004049 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004050 return 0;
4051 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004052 case Intrinsic::var_annotation:
4053 // Discard annotate attributes
4054 return 0;
4055
4056 case Intrinsic::init_trampoline: {
4057 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4058
4059 SDValue Ops[6];
4060 Ops[0] = getRoot();
4061 Ops[1] = getValue(I.getOperand(1));
4062 Ops[2] = getValue(I.getOperand(2));
4063 Ops[3] = getValue(I.getOperand(3));
4064 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4065 Ops[5] = DAG.getSrcValue(F);
4066
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004067 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004068 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00004069 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004070
4071 setValue(&I, Tmp);
4072 DAG.setRoot(Tmp.getValue(1));
4073 return 0;
4074 }
4075
4076 case Intrinsic::gcroot:
4077 if (GFI) {
4078 Value *Alloca = I.getOperand(1);
4079 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004081 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4082 GFI->addStackRoot(FI->getIndex(), TypeMap);
4083 }
4084 return 0;
4085
4086 case Intrinsic::gcread:
4087 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004088 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004089 return 0;
4090
4091 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004092 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004093 return 0;
4094 }
4095
4096 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00004097 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004098 return 0;
4099 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004100
Bill Wendlingef375462008-11-21 02:38:44 +00004101 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004102 return implVisitAluOverflow(I, ISD::UADDO);
4103 case Intrinsic::sadd_with_overflow:
4104 return implVisitAluOverflow(I, ISD::SADDO);
4105 case Intrinsic::usub_with_overflow:
4106 return implVisitAluOverflow(I, ISD::USUBO);
4107 case Intrinsic::ssub_with_overflow:
4108 return implVisitAluOverflow(I, ISD::SSUBO);
4109 case Intrinsic::umul_with_overflow:
4110 return implVisitAluOverflow(I, ISD::UMULO);
4111 case Intrinsic::smul_with_overflow:
4112 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004113
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004114 case Intrinsic::prefetch: {
4115 SDValue Ops[4];
4116 Ops[0] = getRoot();
4117 Ops[1] = getValue(I.getOperand(1));
4118 Ops[2] = getValue(I.getOperand(2));
4119 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004120 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004121 return 0;
4122 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004124 case Intrinsic::memory_barrier: {
4125 SDValue Ops[6];
4126 Ops[0] = getRoot();
4127 for (int x = 1; x < 6; ++x)
4128 Ops[x] = getValue(I.getOperand(x));
4129
Owen Anderson825b72b2009-08-11 20:47:22 +00004130 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004131 return 0;
4132 }
4133 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004134 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004135 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004136 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004137 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4138 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004139 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004140 getValue(I.getOperand(2)),
4141 getValue(I.getOperand(3)),
4142 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004143 setValue(&I, L);
4144 DAG.setRoot(L.getValue(1));
4145 return 0;
4146 }
4147 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004148 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004149 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004150 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004151 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004152 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004153 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004154 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004155 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004156 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004157 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004158 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004159 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004160 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004161 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004162 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004163 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004164 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004165 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004166 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004167 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004168 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004169
4170 case Intrinsic::invariant_start:
4171 case Intrinsic::lifetime_start:
4172 // Discard region information.
4173 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
4174 return 0;
4175 case Intrinsic::invariant_end:
4176 case Intrinsic::lifetime_end:
4177 // Discard region information.
4178 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004179 }
4180}
4181
Dan Gohman98ca4f22009-08-05 01:29:28 +00004182/// Test if the given instruction is in a position to be optimized
4183/// with a tail-call. This roughly means that it's in a block with
4184/// a return and there's nothing that needs to be scheduled
4185/// between it and the return.
4186///
4187/// This function only tests target-independent requirements.
4188/// For target-dependent requirements, a target should override
4189/// TargetLowering::IsEligibleForTailCallOptimization.
4190///
4191static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004192isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004193 const TargetLowering &TLI) {
4194 const BasicBlock *ExitBB = I->getParent();
4195 const TerminatorInst *Term = ExitBB->getTerminator();
4196 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4197 const Function *F = ExitBB->getParent();
4198
4199 // The block must end in a return statement or an unreachable.
4200 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4201
4202 // If I will have a chain, make sure no other instruction that will have a
4203 // chain interposes between I and the return.
4204 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4205 !I->isSafeToSpeculativelyExecute())
4206 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4207 --BBI) {
4208 if (&*BBI == I)
4209 break;
4210 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4211 !BBI->isSafeToSpeculativelyExecute())
4212 return false;
4213 }
4214
4215 // If the block ends with a void return or unreachable, it doesn't matter
4216 // what the call's return type is.
4217 if (!Ret || Ret->getNumOperands() == 0) return true;
4218
Dan Gohmaned9bab32009-11-14 02:06:30 +00004219 // If the return value is undef, it doesn't matter what the call's
4220 // return type is.
4221 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4222
Dan Gohman98ca4f22009-08-05 01:29:28 +00004223 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004224 // the return. Ignore noalias because it doesn't affect the call sequence.
4225 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4226 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004227 return false;
4228
4229 // Otherwise, make sure the unmodified return value of I is the return value.
4230 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4231 U = dyn_cast<Instruction>(U->getOperand(0))) {
4232 if (!U)
4233 return false;
4234 if (!U->hasOneUse())
4235 return false;
4236 if (U == I)
4237 break;
4238 // Check for a truly no-op truncate.
4239 if (isa<TruncInst>(U) &&
4240 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4241 continue;
4242 // Check for a truly no-op bitcast.
4243 if (isa<BitCastInst>(U) &&
4244 (U->getOperand(0)->getType() == U->getType() ||
4245 (isa<PointerType>(U->getOperand(0)->getType()) &&
4246 isa<PointerType>(U->getType()))))
4247 continue;
4248 // Otherwise it's not a true no-op.
4249 return false;
4250 }
4251
4252 return true;
4253}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004254
Dan Gohman2048b852009-11-23 18:04:58 +00004255void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4256 bool isTailCall,
4257 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004258 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4259 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004260 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004261 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4262 unsigned BeginLabel = 0, EndLabel = 0;
4263
4264 TargetLowering::ArgListTy Args;
4265 TargetLowering::ArgListEntry Entry;
4266 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004267
4268 // Check whether the function can return without sret-demotion.
4269 SmallVector<EVT, 4> OutVTs;
4270 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4271 SmallVector<uint64_t, 4> Offsets;
4272 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4273 OutVTs, OutsFlags, TLI, &Offsets);
4274
4275
4276 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4277 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4278
4279 SDValue DemoteStackSlot;
4280
4281 if (!CanLowerReturn) {
4282 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4283 FTy->getReturnType());
4284 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4285 FTy->getReturnType());
4286 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004287 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004288 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4289
4290 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4291 Entry.Node = DemoteStackSlot;
4292 Entry.Ty = StackSlotPtrType;
4293 Entry.isSExt = false;
4294 Entry.isZExt = false;
4295 Entry.isInReg = false;
4296 Entry.isSRet = true;
4297 Entry.isNest = false;
4298 Entry.isByVal = false;
4299 Entry.Alignment = Align;
4300 Args.push_back(Entry);
4301 RetTy = Type::getVoidTy(FTy->getContext());
4302 }
4303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004304 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004305 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004306 SDValue ArgNode = getValue(*i);
4307 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4308
4309 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004310 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4311 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4312 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4313 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4314 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4315 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004316 Entry.Alignment = CS.getParamAlignment(attrInd);
4317 Args.push_back(Entry);
4318 }
4319
4320 if (LandingPad && MMI) {
4321 // Insert a label before the invoke call to mark the try range. This can be
4322 // used to detect deletion of the invoke via the MachineModuleInfo.
4323 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004324
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004325 // Both PendingLoads and PendingExports must be flushed here;
4326 // this call might not return.
4327 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004328 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4329 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004330 }
4331
Dan Gohman98ca4f22009-08-05 01:29:28 +00004332 // Check if target-independent constraints permit a tail call here.
4333 // Target-dependent constraints are checked within TLI.LowerCallTo.
4334 if (isTailCall &&
4335 !isInTailCallPosition(CS.getInstruction(),
4336 CS.getAttributes().getRetAttributes(),
4337 TLI))
4338 isTailCall = false;
4339
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004340 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004341 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004342 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004343 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004344 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004345 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004346 isTailCall,
4347 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004348 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004349 assert((isTailCall || Result.second.getNode()) &&
4350 "Non-null chain expected with non-tail call!");
4351 assert((Result.second.getNode() || !Result.first.getNode()) &&
4352 "Null value expected with tail call!");
4353 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004354 setValue(CS.getInstruction(), Result.first);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004355 else if (!CanLowerReturn && Result.second.getNode()) {
4356 // The instruction result is the result of loading from the
4357 // hidden sret parameter.
4358 SmallVector<EVT, 1> PVTs;
4359 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4360
4361 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4362 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4363 EVT PtrVT = PVTs[0];
4364 unsigned NumValues = OutVTs.size();
4365 SmallVector<SDValue, 4> Values(NumValues);
4366 SmallVector<SDValue, 4> Chains(NumValues);
4367
4368 for (unsigned i = 0; i < NumValues; ++i) {
4369 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
4370 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, DemoteStackSlot,
4371 DAG.getConstant(Offsets[i], PtrVT)),
4372 NULL, Offsets[i], false, 1);
4373 Values[i] = L;
4374 Chains[i] = L.getValue(1);
4375 }
4376 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4377 MVT::Other, &Chains[0], NumValues);
4378 PendingLoads.push_back(Chain);
4379
4380 setValue(CS.getInstruction(), DAG.getNode(ISD::MERGE_VALUES,
4381 getCurDebugLoc(), DAG.getVTList(&OutVTs[0], NumValues),
4382 &Values[0], NumValues));
4383 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00004384 // As a special case, a null chain means that a tail call has
4385 // been emitted and the DAG root is already updated.
4386 if (Result.second.getNode())
4387 DAG.setRoot(Result.second);
4388 else
4389 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004390
4391 if (LandingPad && MMI) {
4392 // Insert a label at the end of the invoke call to mark the try range. This
4393 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4394 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004395 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4396 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397
4398 // Inform MachineModuleInfo of range.
4399 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4400 }
4401}
4402
4403
Dan Gohman2048b852009-11-23 18:04:58 +00004404void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004405 const char *RenameFn = 0;
4406 if (Function *F = I.getCalledFunction()) {
4407 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004408 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4409 if (II) {
4410 if (unsigned IID = II->getIntrinsicID(F)) {
4411 RenameFn = visitIntrinsicCall(I, IID);
4412 if (!RenameFn)
4413 return;
4414 }
4415 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004416 if (unsigned IID = F->getIntrinsicID()) {
4417 RenameFn = visitIntrinsicCall(I, IID);
4418 if (!RenameFn)
4419 return;
4420 }
4421 }
4422
4423 // Check for well-known libc/libm calls. If the function is internal, it
4424 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004425 if (!F->hasLocalLinkage() && F->hasName()) {
4426 StringRef Name = F->getName();
4427 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004428 if (I.getNumOperands() == 3 && // Basic sanity checks.
4429 I.getOperand(1)->getType()->isFloatingPoint() &&
4430 I.getType() == I.getOperand(1)->getType() &&
4431 I.getType() == I.getOperand(2)->getType()) {
4432 SDValue LHS = getValue(I.getOperand(1));
4433 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004434 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004435 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004436 return;
4437 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004438 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004439 if (I.getNumOperands() == 2 && // Basic sanity checks.
4440 I.getOperand(1)->getType()->isFloatingPoint() &&
4441 I.getType() == I.getOperand(1)->getType()) {
4442 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004443 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004444 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004445 return;
4446 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004447 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004448 if (I.getNumOperands() == 2 && // Basic sanity checks.
4449 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004450 I.getType() == I.getOperand(1)->getType() &&
4451 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004452 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004453 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004454 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004455 return;
4456 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004457 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004458 if (I.getNumOperands() == 2 && // Basic sanity checks.
4459 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004460 I.getType() == I.getOperand(1)->getType() &&
4461 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004462 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004463 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004464 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004465 return;
4466 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004467 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4468 if (I.getNumOperands() == 2 && // Basic sanity checks.
4469 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004470 I.getType() == I.getOperand(1)->getType() &&
4471 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004472 SDValue Tmp = getValue(I.getOperand(1));
4473 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4474 Tmp.getValueType(), Tmp));
4475 return;
4476 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004477 }
4478 }
4479 } else if (isa<InlineAsm>(I.getOperand(0))) {
4480 visitInlineAsm(&I);
4481 return;
4482 }
4483
4484 SDValue Callee;
4485 if (!RenameFn)
4486 Callee = getValue(I.getOperand(0));
4487 else
Bill Wendling056292f2008-09-16 21:48:12 +00004488 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004489
Dan Gohman98ca4f22009-08-05 01:29:28 +00004490 // Check if we can potentially perform a tail call. More detailed
4491 // checking is be done within LowerCallTo, after more information
4492 // about the call is known.
4493 bool isTailCall = PerformTailCallOpt && I.isTailCall();
4494
4495 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004496}
4497
4498
4499/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004500/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004501/// Chain/Flag as the input and updates them for the output Chain/Flag.
4502/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004503SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004504 SDValue &Chain,
4505 SDValue *Flag) const {
4506 // Assemble the legal parts into the final values.
4507 SmallVector<SDValue, 4> Values(ValueVTs.size());
4508 SmallVector<SDValue, 8> Parts;
4509 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4510 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004511 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004512 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004513 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004514
4515 Parts.resize(NumRegs);
4516 for (unsigned i = 0; i != NumRegs; ++i) {
4517 SDValue P;
4518 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004519 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004520 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004521 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004522 *Flag = P.getValue(2);
4523 }
4524 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004525
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004526 // If the source register was virtual and if we know something about it,
4527 // add an assert node.
4528 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4529 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4530 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4531 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4532 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4533 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004534
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004535 unsigned RegSize = RegisterVT.getSizeInBits();
4536 unsigned NumSignBits = LOI.NumSignBits;
4537 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004538
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004539 // FIXME: We capture more information than the dag can represent. For
4540 // now, just use the tightest assertzext/assertsext possible.
4541 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004542 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004543 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004544 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004545 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004546 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004547 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004548 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004549 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004550 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004551 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004552 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004553 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004554 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004555 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004556 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004557 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004558 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004559
Owen Anderson825b72b2009-08-11 20:47:22 +00004560 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004561 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004562 RegisterVT, P, DAG.getValueType(FromVT));
4563
4564 }
4565 }
4566 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004567
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004568 Parts[i] = P;
4569 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004570
Scott Michelfdc40a02009-02-17 22:15:04 +00004571 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004572 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004573 Part += NumRegs;
4574 Parts.clear();
4575 }
4576
Dale Johannesen66978ee2009-01-31 02:22:37 +00004577 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004578 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4579 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004580}
4581
4582/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004583/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004584/// Chain/Flag as the input and updates them for the output Chain/Flag.
4585/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004586void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004587 SDValue &Chain, SDValue *Flag) const {
4588 // Get the list of the values's legal parts.
4589 unsigned NumRegs = Regs.size();
4590 SmallVector<SDValue, 8> Parts(NumRegs);
4591 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004592 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004593 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004594 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004595
Dale Johannesen66978ee2009-01-31 02:22:37 +00004596 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004597 &Parts[Part], NumParts, RegisterVT);
4598 Part += NumParts;
4599 }
4600
4601 // Copy the parts into the registers.
4602 SmallVector<SDValue, 8> Chains(NumRegs);
4603 for (unsigned i = 0; i != NumRegs; ++i) {
4604 SDValue Part;
4605 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004606 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004607 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004608 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004609 *Flag = Part.getValue(1);
4610 }
4611 Chains[i] = Part.getValue(0);
4612 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004613
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004614 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004615 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004616 // flagged to it. That is the CopyToReg nodes and the user are considered
4617 // a single scheduling unit. If we create a TokenFactor and return it as
4618 // chain, then the TokenFactor is both a predecessor (operand) of the
4619 // user as well as a successor (the TF operands are flagged to the user).
4620 // c1, f1 = CopyToReg
4621 // c2, f2 = CopyToReg
4622 // c3 = TokenFactor c1, c2
4623 // ...
4624 // = op c3, ..., f2
4625 Chain = Chains[NumRegs-1];
4626 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004627 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004628}
4629
4630/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004631/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004632/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00004633void RegsForValue::AddInlineAsmOperands(unsigned Code,
4634 bool HasMatching,unsigned MatchingIdx,
4635 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004636 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004637 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00004638 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
4639 unsigned Flag = Code | (Regs.size() << 3);
4640 if (HasMatching)
4641 Flag |= 0x80000000 | (MatchingIdx << 16);
4642 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004643 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004644 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004645 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004646 for (unsigned i = 0; i != NumRegs; ++i) {
4647 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004648 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004649 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650 }
4651}
4652
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004653/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004654/// i.e. it isn't a stack pointer or some other special register, return the
4655/// register class for the register. Otherwise, return null.
4656static const TargetRegisterClass *
4657isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4658 const TargetLowering &TLI,
4659 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004660 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004661 const TargetRegisterClass *FoundRC = 0;
4662 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4663 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004664 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004665
4666 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004667 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4669 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4670 I != E; ++I) {
4671 if (TLI.isTypeLegal(*I)) {
4672 // If we have already found this register in a different register class,
4673 // choose the one with the largest VT specified. For example, on
4674 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004675 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 ThisVT = *I;
4677 break;
4678 }
4679 }
4680 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004681
Owen Anderson825b72b2009-08-11 20:47:22 +00004682 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004683
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004684 // NOTE: This isn't ideal. In particular, this might allocate the
4685 // frame pointer in functions that need it (due to them not being taken
4686 // out of allocation, because a variable sized allocation hasn't been seen
4687 // yet). This is a slight code pessimization, but should still work.
4688 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4689 E = RC->allocation_order_end(MF); I != E; ++I)
4690 if (*I == Reg) {
4691 // We found a matching register class. Keep looking at others in case
4692 // we find one with larger registers that this physreg is also in.
4693 FoundRC = RC;
4694 FoundVT = ThisVT;
4695 break;
4696 }
4697 }
4698 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004699}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004700
4701
4702namespace llvm {
4703/// AsmOperandInfo - This contains information for each constraint that we are
4704/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004705class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004706 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004707public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004708 /// CallOperand - If this is the result output operand or a clobber
4709 /// this is null, otherwise it is the incoming operand to the CallInst.
4710 /// This gets modified as the asm is processed.
4711 SDValue CallOperand;
4712
4713 /// AssignedRegs - If this is a register or register class operand, this
4714 /// contains the set of register corresponding to the operand.
4715 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004716
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004717 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4718 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4719 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004720
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004721 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4722 /// busy in OutputRegs/InputRegs.
4723 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004724 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004725 std::set<unsigned> &InputRegs,
4726 const TargetRegisterInfo &TRI) const {
4727 if (isOutReg) {
4728 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4729 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4730 }
4731 if (isInReg) {
4732 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4733 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4734 }
4735 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004736
Owen Andersone50ed302009-08-10 22:56:29 +00004737 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004738 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004739 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00004740 EVT getCallOperandValEVT(LLVMContext &Context,
4741 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004742 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004743 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004744
Chris Lattner81249c92008-10-17 17:05:25 +00004745 if (isa<BasicBlock>(CallOperandVal))
4746 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004747
Chris Lattner81249c92008-10-17 17:05:25 +00004748 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004749
Chris Lattner81249c92008-10-17 17:05:25 +00004750 // If this is an indirect operand, the operand is a pointer to the
4751 // accessed type.
4752 if (isIndirect)
4753 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004754
Chris Lattner81249c92008-10-17 17:05:25 +00004755 // If OpTy is not a single value, it may be a struct/union that we
4756 // can tile with integers.
4757 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4758 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4759 switch (BitSize) {
4760 default: break;
4761 case 1:
4762 case 8:
4763 case 16:
4764 case 32:
4765 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004766 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004767 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004768 break;
4769 }
4770 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004771
Chris Lattner81249c92008-10-17 17:05:25 +00004772 return TLI.getValueType(OpTy, true);
4773 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004774
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004775private:
4776 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4777 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004778 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004779 const TargetRegisterInfo &TRI) {
4780 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4781 Regs.insert(Reg);
4782 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4783 for (; *Aliases; ++Aliases)
4784 Regs.insert(*Aliases);
4785 }
4786};
4787} // end llvm namespace.
4788
4789
4790/// GetRegistersForValue - Assign registers (virtual or physical) for the
4791/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004792/// register allocator to handle the assignment process. However, if the asm
4793/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004794/// allocation. This produces generally horrible, but correct, code.
4795///
4796/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797/// Input and OutputRegs are the set of already allocated physical registers.
4798///
Dan Gohman2048b852009-11-23 18:04:58 +00004799void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004800GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004801 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004803 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004804
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004805 // Compute whether this value requires an input register, an output register,
4806 // or both.
4807 bool isOutReg = false;
4808 bool isInReg = false;
4809 switch (OpInfo.Type) {
4810 case InlineAsm::isOutput:
4811 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004812
4813 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004814 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004815 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004816 break;
4817 case InlineAsm::isInput:
4818 isInReg = true;
4819 isOutReg = false;
4820 break;
4821 case InlineAsm::isClobber:
4822 isOutReg = true;
4823 isInReg = true;
4824 break;
4825 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004826
4827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 MachineFunction &MF = DAG.getMachineFunction();
4829 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004830
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004831 // If this is a constraint for a single physreg, or a constraint for a
4832 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004833 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004834 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4835 OpInfo.ConstraintVT);
4836
4837 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004838 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004839 // If this is a FP input in an integer register (or visa versa) insert a bit
4840 // cast of the input value. More generally, handle any case where the input
4841 // value disagrees with the register class we plan to stick this in.
4842 if (OpInfo.Type == InlineAsm::isInput &&
4843 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004844 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004845 // types are identical size, use a bitcast to convert (e.g. two differing
4846 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004847 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004848 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004849 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004850 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004851 OpInfo.ConstraintVT = RegVT;
4852 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4853 // If the input is a FP value and we want it in FP registers, do a
4854 // bitcast to the corresponding integer type. This turns an f64 value
4855 // into i64, which can be passed with two i32 values on a 32-bit
4856 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00004857 RegVT = EVT::getIntegerVT(Context,
4858 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004859 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004860 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004861 OpInfo.ConstraintVT = RegVT;
4862 }
4863 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004864
Owen Anderson23b9b192009-08-12 00:36:31 +00004865 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004866 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004867
Owen Andersone50ed302009-08-10 22:56:29 +00004868 EVT RegVT;
4869 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004870
4871 // If this is a constraint for a specific physical register, like {r17},
4872 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004873 if (unsigned AssignedReg = PhysReg.first) {
4874 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00004875 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004876 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004877
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004878 // Get the actual register value type. This is important, because the user
4879 // may have asked for (e.g.) the AX register in i32 type. We need to
4880 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004881 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004882
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004883 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004884 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004885
4886 // If this is an expanded reference, add the rest of the regs to Regs.
4887 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004888 TargetRegisterClass::iterator I = RC->begin();
4889 for (; *I != AssignedReg; ++I)
4890 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004891
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004892 // Already added the first reg.
4893 --NumRegs; ++I;
4894 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004895 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004896 Regs.push_back(*I);
4897 }
4898 }
4899 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4900 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4901 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4902 return;
4903 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004905 // Otherwise, if this was a reference to an LLVM register class, create vregs
4906 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00004907 if (const TargetRegisterClass *RC = PhysReg.second) {
4908 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00004909 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00004910 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004911
Evan Chengfb112882009-03-23 08:01:15 +00004912 // Create the appropriate number of virtual registers.
4913 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4914 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00004915 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004916
Evan Chengfb112882009-03-23 08:01:15 +00004917 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4918 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004919 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00004920
4921 // This is a reference to a register class that doesn't directly correspond
4922 // to an LLVM register class. Allocate NumRegs consecutive, available,
4923 // registers from the class.
4924 std::vector<unsigned> RegClassRegs
4925 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
4926 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004927
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004928 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4929 unsigned NumAllocated = 0;
4930 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
4931 unsigned Reg = RegClassRegs[i];
4932 // See if this register is available.
4933 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
4934 (isInReg && InputRegs.count(Reg))) { // Already used.
4935 // Make sure we find consecutive registers.
4936 NumAllocated = 0;
4937 continue;
4938 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004939
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004940 // Check to see if this register is allocatable (i.e. don't give out the
4941 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00004942 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
4943 if (!RC) { // Couldn't allocate this register.
4944 // Reset NumAllocated to make sure we return consecutive registers.
4945 NumAllocated = 0;
4946 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004947 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004948
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004949 // Okay, this register is good, we can use it.
4950 ++NumAllocated;
4951
4952 // If we allocated enough consecutive registers, succeed.
4953 if (NumAllocated == NumRegs) {
4954 unsigned RegStart = (i-NumAllocated)+1;
4955 unsigned RegEnd = i+1;
4956 // Mark all of the allocated registers used.
4957 for (unsigned i = RegStart; i != RegEnd; ++i)
4958 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004959
4960 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004961 OpInfo.ConstraintVT);
4962 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4963 return;
4964 }
4965 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004966
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004967 // Otherwise, we couldn't allocate enough registers for this.
4968}
4969
Evan Chengda43bcf2008-09-24 00:05:32 +00004970/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
4971/// processed uses a memory 'm' constraint.
4972static bool
4973hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00004974 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00004975 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
4976 InlineAsm::ConstraintInfo &CI = CInfos[i];
4977 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
4978 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
4979 if (CType == TargetLowering::C_Memory)
4980 return true;
4981 }
Chris Lattner6c147292009-04-30 00:48:50 +00004982
4983 // Indirect operand accesses access memory.
4984 if (CI.isIndirect)
4985 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00004986 }
4987
4988 return false;
4989}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004990
4991/// visitInlineAsm - Handle a call to an InlineAsm object.
4992///
Dan Gohman2048b852009-11-23 18:04:58 +00004993void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004994 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
4995
4996 /// ConstraintOperands - Information about all of the constraints.
4997 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004998
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004999 std::set<unsigned> OutputRegs, InputRegs;
5000
5001 // Do a prepass over the constraints, canonicalizing them, and building up the
5002 // ConstraintOperands list.
5003 std::vector<InlineAsm::ConstraintInfo>
5004 ConstraintInfos = IA->ParseConstraints();
5005
Evan Chengda43bcf2008-09-24 00:05:32 +00005006 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005007
5008 SDValue Chain, Flag;
5009
5010 // We won't need to flush pending loads if this asm doesn't touch
5011 // memory and is nonvolatile.
5012 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005013 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005014 else
5015 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005017 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5018 unsigned ResNo = 0; // ResNo - The result number of the next output.
5019 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5020 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5021 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005022
Owen Anderson825b72b2009-08-11 20:47:22 +00005023 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005024
5025 // Compute the value type for each operand.
5026 switch (OpInfo.Type) {
5027 case InlineAsm::isOutput:
5028 // Indirect outputs just consume an argument.
5029 if (OpInfo.isIndirect) {
5030 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5031 break;
5032 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005033
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005034 // The return value of the call is this value. As such, there is no
5035 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005036 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5037 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005038 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5039 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5040 } else {
5041 assert(ResNo == 0 && "Asm only has one result!");
5042 OpVT = TLI.getValueType(CS.getType());
5043 }
5044 ++ResNo;
5045 break;
5046 case InlineAsm::isInput:
5047 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5048 break;
5049 case InlineAsm::isClobber:
5050 // Nothing to do.
5051 break;
5052 }
5053
5054 // If this is an input or an indirect output, process the call argument.
5055 // BasicBlocks are labels, currently appearing only in asm's.
5056 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005057 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005058 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5059
Chris Lattner81249c92008-10-17 17:05:25 +00005060 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005061 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005062 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005063 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005064 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005065
Owen Anderson1d0be152009-08-13 21:58:54 +00005066 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005069 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005070 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005071
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005072 // Second pass over the constraints: compute which constraint option to use
5073 // and assign registers to constraints that want a specific physreg.
5074 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5075 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005076
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005077 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005078 // matching input. If their types mismatch, e.g. one is an integer, the
5079 // other is floating point, or their sizes are different, flag it as an
5080 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005081 if (OpInfo.hasMatchingInput()) {
5082 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5083 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005084 if ((OpInfo.ConstraintVT.isInteger() !=
5085 Input.ConstraintVT.isInteger()) ||
5086 (OpInfo.ConstraintVT.getSizeInBits() !=
5087 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005088 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005089 " with a matching output constraint of incompatible"
5090 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005091 }
5092 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005093 }
5094 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005095
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005096 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005097 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005099 // If this is a memory input, and if the operand is not indirect, do what we
5100 // need to to provide an address for the memory input.
5101 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5102 !OpInfo.isIndirect) {
5103 assert(OpInfo.Type == InlineAsm::isInput &&
5104 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005105
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005106 // Memory operands really want the address of the value. If we don't have
5107 // an indirect input, put it in the constpool if we can, otherwise spill
5108 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005110 // If the operand is a float, integer, or vector constant, spill to a
5111 // constant pool entry to get its address.
5112 Value *OpVal = OpInfo.CallOperandVal;
5113 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5114 isa<ConstantVector>(OpVal)) {
5115 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5116 TLI.getPointerTy());
5117 } else {
5118 // Otherwise, create a stack slot and emit a store to it before the
5119 // asm.
5120 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005121 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005122 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5123 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005124 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005125 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005126 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005127 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005128 OpInfo.CallOperand = StackSlot;
5129 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005130
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005131 // There is no longer a Value* corresponding to this operand.
5132 OpInfo.CallOperandVal = 0;
5133 // It is now an indirect operand.
5134 OpInfo.isIndirect = true;
5135 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005136
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005137 // If this constraint is for a specific register, allocate it before
5138 // anything else.
5139 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005140 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005141 }
5142 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005143
5144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005145 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005146 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005147 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5148 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005149
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005150 // C_Register operands have already been allocated, Other/Memory don't need
5151 // to be.
5152 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005153 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005154 }
5155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5157 std::vector<SDValue> AsmNodeOperands;
5158 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5159 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005160 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005161
5162
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005163 // Loop over all of the inputs, copying the operand values into the
5164 // appropriate registers and processing the output regs.
5165 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005167 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5168 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005170 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5171 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5172
5173 switch (OpInfo.Type) {
5174 case InlineAsm::isOutput: {
5175 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5176 OpInfo.ConstraintType != TargetLowering::C_Register) {
5177 // Memory output, or 'other' output (e.g. 'X' constraint).
5178 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5179
5180 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005181 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5182 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005183 TLI.getPointerTy()));
5184 AsmNodeOperands.push_back(OpInfo.CallOperand);
5185 break;
5186 }
5187
5188 // Otherwise, this is a register or register class output.
5189
5190 // Copy the output from the appropriate register. Find a register that
5191 // we can use.
5192 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005193 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005194 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005195 }
5196
5197 // If this is an indirect operand, store through the pointer after the
5198 // asm.
5199 if (OpInfo.isIndirect) {
5200 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5201 OpInfo.CallOperandVal));
5202 } else {
5203 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005204 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5205 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005206 // Concatenate this output onto the outputs list.
5207 RetValRegs.append(OpInfo.AssignedRegs);
5208 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005209
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005210 // Add information to the INLINEASM node to know that this register is
5211 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005212 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5213 6 /* EARLYCLOBBER REGDEF */ :
5214 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005215 false,
5216 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005217 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005218 break;
5219 }
5220 case InlineAsm::isInput: {
5221 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005222
Chris Lattner6bdcda32008-10-17 16:47:46 +00005223 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005224 // If this is required to match an output register we have already set,
5225 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005226 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005227
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005228 // Scan until we find the definition we already emitted of this operand.
5229 // When we find it, create a RegsForValue operand.
5230 unsigned CurOp = 2; // The first operand.
5231 for (; OperandNo; --OperandNo) {
5232 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005233 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005234 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005235 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5236 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5237 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005238 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005239 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 }
5241
Evan Cheng697cbbf2009-03-20 18:03:34 +00005242 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005243 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005244 if ((OpFlag & 7) == 2 /*REGDEF*/
5245 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5246 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005247 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005248 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005249 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005250 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005251 RegsForValue MatchedRegs;
5252 MatchedRegs.TLI = &TLI;
5253 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005254 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005255 MatchedRegs.RegVTs.push_back(RegVT);
5256 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005257 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005258 i != e; ++i)
5259 MatchedRegs.Regs.
5260 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005261
5262 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005263 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5264 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005265 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5266 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005267 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005268 break;
5269 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005270 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5271 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5272 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005273 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005274 // See InlineAsm.h isUseOperandTiedToDef.
5275 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005276 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005277 TLI.getPointerTy()));
5278 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5279 break;
5280 }
5281 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005282
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005283 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005284 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005285 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005286
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005287 std::vector<SDValue> Ops;
5288 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005289 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005290 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005291 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005292 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005293 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005294
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005295 // Add information to the INLINEASM node to know about this input.
5296 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005297 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005298 TLI.getPointerTy()));
5299 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5300 break;
5301 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5302 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5303 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5304 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005305
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005306 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005307 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5308 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005309 TLI.getPointerTy()));
5310 AsmNodeOperands.push_back(InOperandVal);
5311 break;
5312 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005313
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005314 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5315 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5316 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005317 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005318 "Don't know how to handle indirect register inputs yet!");
5319
5320 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005321 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005322 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005323 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005324 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325
Dale Johannesen66978ee2009-01-31 02:22:37 +00005326 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5327 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005328
Evan Cheng697cbbf2009-03-20 18:03:34 +00005329 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005330 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005331 break;
5332 }
5333 case InlineAsm::isClobber: {
5334 // Add the clobbered value to the operand list, so that the register
5335 // allocator is aware that the physreg got clobbered.
5336 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005337 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005338 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005339 break;
5340 }
5341 }
5342 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005344 // Finish up input operands.
5345 AsmNodeOperands[0] = Chain;
5346 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005347
Dale Johannesen66978ee2009-01-31 02:22:37 +00005348 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005349 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005350 &AsmNodeOperands[0], AsmNodeOperands.size());
5351 Flag = Chain.getValue(1);
5352
5353 // If this asm returns a register value, copy the result from that register
5354 // and set it as the value of the call.
5355 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005356 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005357 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005358
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005359 // FIXME: Why don't we do this for inline asms with MRVs?
5360 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005361 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005362
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005363 // If any of the results of the inline asm is a vector, it may have the
5364 // wrong width/num elts. This can happen for register classes that can
5365 // contain multiple different value types. The preg or vreg allocated may
5366 // not have the same VT as was expected. Convert it to the right type
5367 // with bit_convert.
5368 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005369 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005370 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005371
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005372 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005373 ResultType.isInteger() && Val.getValueType().isInteger()) {
5374 // If a result value was tied to an input value, the computed result may
5375 // have a wider width than the expected result. Extract the relevant
5376 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005377 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005378 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005379
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005380 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005381 }
Dan Gohman95915732008-10-18 01:03:45 +00005382
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005383 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005384 // Don't need to use this as a chain in this case.
5385 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5386 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005387 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005388
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005389 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005390
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005391 // Process indirect outputs, first output all of the flagged copies out of
5392 // physregs.
5393 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5394 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5395 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005396 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5397 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005398 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005399
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005401
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005402 // Emit the non-flagged stores from the physregs.
5403 SmallVector<SDValue, 8> OutChains;
5404 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005405 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005406 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005407 getValue(StoresToEmit[i].second),
5408 StoresToEmit[i].second, 0));
5409 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005410 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005411 &OutChains[0], OutChains.size());
5412 DAG.setRoot(Chain);
5413}
5414
Dan Gohman2048b852009-11-23 18:04:58 +00005415void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005416 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005417 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005418 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005419 DAG.getSrcValue(I.getOperand(1))));
5420}
5421
Dan Gohman2048b852009-11-23 18:04:58 +00005422void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005423 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5424 getRoot(), getValue(I.getOperand(0)),
5425 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005426 setValue(&I, V);
5427 DAG.setRoot(V.getValue(1));
5428}
5429
Dan Gohman2048b852009-11-23 18:04:58 +00005430void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005431 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005432 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005433 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005434 DAG.getSrcValue(I.getOperand(1))));
5435}
5436
Dan Gohman2048b852009-11-23 18:04:58 +00005437void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005438 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005439 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005440 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005441 getValue(I.getOperand(2)),
5442 DAG.getSrcValue(I.getOperand(1)),
5443 DAG.getSrcValue(I.getOperand(2))));
5444}
5445
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005446/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005447/// implementation, which just calls LowerCall.
5448/// FIXME: When all targets are
5449/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005450std::pair<SDValue, SDValue>
5451TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5452 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005453 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005454 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005455 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005456 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005457 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005458
Dan Gohman1937e2f2008-09-16 01:42:28 +00005459 assert((!isTailCall || PerformTailCallOpt) &&
5460 "isTailCall set when tail-call optimizations are disabled!");
5461
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005462 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005463 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005464 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005465 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005466 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5467 for (unsigned Value = 0, NumValues = ValueVTs.size();
5468 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005469 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005470 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005471 SDValue Op = SDValue(Args[i].Node.getNode(),
5472 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005473 ISD::ArgFlagsTy Flags;
5474 unsigned OriginalAlignment =
5475 getTargetData()->getABITypeAlignment(ArgTy);
5476
5477 if (Args[i].isZExt)
5478 Flags.setZExt();
5479 if (Args[i].isSExt)
5480 Flags.setSExt();
5481 if (Args[i].isInReg)
5482 Flags.setInReg();
5483 if (Args[i].isSRet)
5484 Flags.setSRet();
5485 if (Args[i].isByVal) {
5486 Flags.setByVal();
5487 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5488 const Type *ElementTy = Ty->getElementType();
5489 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005490 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005491 // For ByVal, alignment should come from FE. BE will guess if this
5492 // info is not there but there are cases it cannot get right.
5493 if (Args[i].Alignment)
5494 FrameAlign = Args[i].Alignment;
5495 Flags.setByValAlign(FrameAlign);
5496 Flags.setByValSize(FrameSize);
5497 }
5498 if (Args[i].isNest)
5499 Flags.setNest();
5500 Flags.setOrigAlign(OriginalAlignment);
5501
Owen Anderson23b9b192009-08-12 00:36:31 +00005502 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5503 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005504 SmallVector<SDValue, 4> Parts(NumParts);
5505 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5506
5507 if (Args[i].isSExt)
5508 ExtendKind = ISD::SIGN_EXTEND;
5509 else if (Args[i].isZExt)
5510 ExtendKind = ISD::ZERO_EXTEND;
5511
Dale Johannesen66978ee2009-01-31 02:22:37 +00005512 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005513
Dan Gohman98ca4f22009-08-05 01:29:28 +00005514 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005515 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005516 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5517 if (NumParts > 1 && j == 0)
5518 MyFlags.Flags.setSplit();
5519 else if (j != 0)
5520 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005521
Dan Gohman98ca4f22009-08-05 01:29:28 +00005522 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005523 }
5524 }
5525 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005526
Dan Gohman98ca4f22009-08-05 01:29:28 +00005527 // Handle the incoming return values from the call.
5528 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005529 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005530 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005531 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005532 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005533 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5534 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005535 for (unsigned i = 0; i != NumRegs; ++i) {
5536 ISD::InputArg MyFlags;
5537 MyFlags.VT = RegisterVT;
5538 MyFlags.Used = isReturnValueUsed;
5539 if (RetSExt)
5540 MyFlags.Flags.setSExt();
5541 if (RetZExt)
5542 MyFlags.Flags.setZExt();
5543 if (isInreg)
5544 MyFlags.Flags.setInReg();
5545 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005546 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005547 }
5548
Dan Gohman98ca4f22009-08-05 01:29:28 +00005549 // Check if target-dependent constraints permit a tail call here.
5550 // Target-independent constraints should be checked by the caller.
5551 if (isTailCall &&
5552 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
5553 isTailCall = false;
5554
5555 SmallVector<SDValue, 4> InVals;
5556 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
5557 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005558
5559 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005560 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005561 "LowerCall didn't return a valid chain!");
5562 assert((!isTailCall || InVals.empty()) &&
5563 "LowerCall emitted a return value for a tail call!");
5564 assert((isTailCall || InVals.size() == Ins.size()) &&
5565 "LowerCall didn't emit the correct number of values!");
5566 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5567 assert(InVals[i].getNode() &&
5568 "LowerCall emitted a null value!");
5569 assert(Ins[i].VT == InVals[i].getValueType() &&
5570 "LowerCall emitted a value with the wrong type!");
5571 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00005572
5573 // For a tail call, the return value is merely live-out and there aren't
5574 // any nodes in the DAG representing it. Return a special value to
5575 // indicate that a tail call has been emitted and no more Instructions
5576 // should be processed in the current block.
5577 if (isTailCall) {
5578 DAG.setRoot(Chain);
5579 return std::make_pair(SDValue(), SDValue());
5580 }
5581
5582 // Collect the legal value parts into potentially illegal values
5583 // that correspond to the original function's return values.
5584 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5585 if (RetSExt)
5586 AssertOp = ISD::AssertSext;
5587 else if (RetZExt)
5588 AssertOp = ISD::AssertZext;
5589 SmallVector<SDValue, 4> ReturnValues;
5590 unsigned CurReg = 0;
5591 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005592 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005593 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5594 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005595
5596 SDValue ReturnValue =
5597 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
5598 AssertOp);
5599 ReturnValues.push_back(ReturnValue);
5600 CurReg += NumRegs;
5601 }
5602
5603 // For a function returning void, there is no return value. We can't create
5604 // such a node, so we just return a null return value in that case. In
5605 // that case, nothing will actualy look at the value.
5606 if (ReturnValues.empty())
5607 return std::make_pair(SDValue(), Chain);
5608
5609 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5610 DAG.getVTList(&RetTys[0], RetTys.size()),
5611 &ReturnValues[0], ReturnValues.size());
5612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005613 return std::make_pair(Res, Chain);
5614}
5615
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005616void TargetLowering::LowerOperationWrapper(SDNode *N,
5617 SmallVectorImpl<SDValue> &Results,
5618 SelectionDAG &DAG) {
5619 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005620 if (Res.getNode())
5621 Results.push_back(Res);
5622}
5623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005624SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005625 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626 return SDValue();
5627}
5628
5629
Dan Gohman2048b852009-11-23 18:04:58 +00005630void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005631 SDValue Op = getValue(V);
5632 assert((Op.getOpcode() != ISD::CopyFromReg ||
5633 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5634 "Copy from a reg to the same reg!");
5635 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5636
Owen Anderson23b9b192009-08-12 00:36:31 +00005637 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005638 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005639 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005640 PendingExports.push_back(Chain);
5641}
5642
5643#include "llvm/CodeGen/SelectionDAGISel.h"
5644
Dan Gohman8c2b5252009-10-30 01:27:03 +00005645void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005646 // If this is the entry block, emit arguments.
5647 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005648 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005649 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005650 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005651 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005652 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005653
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005654 // Check whether the function can return without sret-demotion.
5655 SmallVector<EVT, 4> OutVTs;
5656 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005657 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
5658 OutVTs, OutsFlags, TLI);
5659 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5660
5661 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
5662 OutVTs, OutsFlags, DAG);
5663 if (!FLI.CanLowerReturn) {
5664 // Put in an sret pointer parameter before all the other parameters.
5665 SmallVector<EVT, 1> ValueVTs;
5666 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5667
5668 // NOTE: Assuming that a pointer will never break down to more than one VT
5669 // or one register.
5670 ISD::ArgFlagsTy Flags;
5671 Flags.setSRet();
5672 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5673 ISD::InputArg RetArg(Flags, RegisterVT, true);
5674 Ins.push_back(RetArg);
5675 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005676
Dan Gohman98ca4f22009-08-05 01:29:28 +00005677 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005678 unsigned Idx = 1;
5679 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5680 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005681 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005682 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5683 bool isArgValueUsed = !I->use_empty();
5684 for (unsigned Value = 0, NumValues = ValueVTs.size();
5685 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005686 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005687 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005688 ISD::ArgFlagsTy Flags;
5689 unsigned OriginalAlignment =
5690 TD->getABITypeAlignment(ArgTy);
5691
5692 if (F.paramHasAttr(Idx, Attribute::ZExt))
5693 Flags.setZExt();
5694 if (F.paramHasAttr(Idx, Attribute::SExt))
5695 Flags.setSExt();
5696 if (F.paramHasAttr(Idx, Attribute::InReg))
5697 Flags.setInReg();
5698 if (F.paramHasAttr(Idx, Attribute::StructRet))
5699 Flags.setSRet();
5700 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5701 Flags.setByVal();
5702 const PointerType *Ty = cast<PointerType>(I->getType());
5703 const Type *ElementTy = Ty->getElementType();
5704 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5705 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5706 // For ByVal, alignment should be passed from FE. BE will guess if
5707 // this info is not there but there are cases it cannot get right.
5708 if (F.getParamAlignment(Idx))
5709 FrameAlign = F.getParamAlignment(Idx);
5710 Flags.setByValAlign(FrameAlign);
5711 Flags.setByValSize(FrameSize);
5712 }
5713 if (F.paramHasAttr(Idx, Attribute::Nest))
5714 Flags.setNest();
5715 Flags.setOrigAlign(OriginalAlignment);
5716
Owen Anderson23b9b192009-08-12 00:36:31 +00005717 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5718 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005719 for (unsigned i = 0; i != NumRegs; ++i) {
5720 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5721 if (NumRegs > 1 && i == 0)
5722 MyFlags.Flags.setSplit();
5723 // if it isn't first piece, alignment must be 1
5724 else if (i > 0)
5725 MyFlags.Flags.setOrigAlign(1);
5726 Ins.push_back(MyFlags);
5727 }
5728 }
5729 }
5730
5731 // Call the target to set up the argument values.
5732 SmallVector<SDValue, 8> InVals;
5733 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5734 F.isVarArg(), Ins,
5735 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005736
5737 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005738 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005739 "LowerFormalArguments didn't return a valid chain!");
5740 assert(InVals.size() == Ins.size() &&
5741 "LowerFormalArguments didn't emit the correct number of values!");
5742 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5743 assert(InVals[i].getNode() &&
5744 "LowerFormalArguments emitted a null value!");
5745 assert(Ins[i].VT == InVals[i].getValueType() &&
5746 "LowerFormalArguments emitted a value with the wrong type!");
5747 });
5748
5749 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005750 DAG.setRoot(NewRoot);
5751
5752 // Set up the argument values.
5753 unsigned i = 0;
5754 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005755 if (!FLI.CanLowerReturn) {
5756 // Create a virtual register for the sret pointer, and put in a copy
5757 // from the sret argument into it.
5758 SmallVector<EVT, 1> ValueVTs;
5759 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5760 EVT VT = ValueVTs[0];
5761 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5762 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5763 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT,
5764 VT, AssertOp);
5765
Dan Gohman2048b852009-11-23 18:04:58 +00005766 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005767 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5768 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5769 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00005770 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005771 DAG.setRoot(NewRoot);
5772
5773 // i indexes lowered arguments. Bump it past the hidden sret argument.
5774 // Idx indexes LLVM arguments. Don't touch it.
5775 ++i;
5776 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005777 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5778 ++I, ++Idx) {
5779 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005780 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005781 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005782 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005783 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005784 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005785 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5786 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005787
5788 if (!I->use_empty()) {
5789 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5790 if (F.paramHasAttr(Idx, Attribute::SExt))
5791 AssertOp = ISD::AssertSext;
5792 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5793 AssertOp = ISD::AssertZext;
5794
5795 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
5796 PartVT, VT, AssertOp));
5797 }
5798 i += NumParts;
5799 }
5800 if (!I->use_empty()) {
Dan Gohman2048b852009-11-23 18:04:58 +00005801 SDB->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
5802 SDB->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005803 // If this argument is live outside of the entry block, insert a copy from
5804 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005805 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005806 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005807 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005808 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005809
5810 // Finally, if the target has anything special to do, allow it to do so.
5811 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00005812 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005813}
5814
5815/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5816/// ensure constants are generated when needed. Remember the virtual registers
5817/// that need to be added to the Machine PHI nodes as input. We cannot just
5818/// directly add them, because expansion might result in multiple MBB's for one
5819/// BB. As such, the start of the BB might correspond to a different MBB than
5820/// the end.
5821///
5822void
5823SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5824 TerminatorInst *TI = LLVMBB->getTerminator();
5825
5826 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5827
5828 // Check successor nodes' PHI nodes that expect a constant to be available
5829 // from this block.
5830 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5831 BasicBlock *SuccBB = TI->getSuccessor(succ);
5832 if (!isa<PHINode>(SuccBB->begin())) continue;
5833 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005834
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005835 // If this terminator has multiple identical successors (common for
5836 // switches), only handle each succ once.
5837 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005838
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005839 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5840 PHINode *PN;
5841
5842 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5843 // nodes and Machine PHI nodes, but the incoming operands have not been
5844 // emitted yet.
5845 for (BasicBlock::iterator I = SuccBB->begin();
5846 (PN = dyn_cast<PHINode>(I)); ++I) {
5847 // Ignore dead phi's.
5848 if (PN->use_empty()) continue;
5849
5850 unsigned Reg;
5851 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5852
5853 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00005854 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005855 if (RegOut == 0) {
5856 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00005857 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005858 }
5859 Reg = RegOut;
5860 } else {
5861 Reg = FuncInfo->ValueMap[PHIOp];
5862 if (Reg == 0) {
5863 assert(isa<AllocaInst>(PHIOp) &&
5864 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5865 "Didn't codegen value into a register!??");
5866 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00005867 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005868 }
5869 }
5870
5871 // Remember that this register needs to added to the machine PHI node as
5872 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00005873 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005874 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5875 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00005876 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00005877 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005878 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00005879 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005880 Reg += NumRegisters;
5881 }
5882 }
5883 }
Dan Gohman2048b852009-11-23 18:04:58 +00005884 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005885}
5886
Dan Gohman3df24e62008-09-03 23:12:08 +00005887/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5888/// supports legal types, and it emits MachineInstrs directly instead of
5889/// creating SelectionDAG nodes.
5890///
5891bool
5892SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5893 FastISel *F) {
5894 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005895
Dan Gohman3df24e62008-09-03 23:12:08 +00005896 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00005897 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00005898
5899 // Check successor nodes' PHI nodes that expect a constant to be available
5900 // from this block.
5901 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5902 BasicBlock *SuccBB = TI->getSuccessor(succ);
5903 if (!isa<PHINode>(SuccBB->begin())) continue;
5904 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005905
Dan Gohman3df24e62008-09-03 23:12:08 +00005906 // If this terminator has multiple identical successors (common for
5907 // switches), only handle each succ once.
5908 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005909
Dan Gohman3df24e62008-09-03 23:12:08 +00005910 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5911 PHINode *PN;
5912
5913 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5914 // nodes and Machine PHI nodes, but the incoming operands have not been
5915 // emitted yet.
5916 for (BasicBlock::iterator I = SuccBB->begin();
5917 (PN = dyn_cast<PHINode>(I)); ++I) {
5918 // Ignore dead phi's.
5919 if (PN->use_empty()) continue;
5920
5921 // Only handle legal types. Two interesting things to note here. First,
5922 // by bailing out early, we may leave behind some dead instructions,
5923 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
5924 // own moves. Second, this check is necessary becuase FastISel doesn't
5925 // use CreateRegForValue to create registers, so it always creates
5926 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00005927 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00005928 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
5929 // Promote MVT::i1.
5930 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00005931 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00005932 else {
Dan Gohman2048b852009-11-23 18:04:58 +00005933 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00005934 return false;
5935 }
Dan Gohman3df24e62008-09-03 23:12:08 +00005936 }
5937
5938 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5939
5940 unsigned Reg = F->getRegForValue(PHIOp);
5941 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00005942 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00005943 return false;
5944 }
Dan Gohman2048b852009-11-23 18:04:58 +00005945 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00005946 }
5947 }
5948
5949 return true;
5950}