blob: be0d6afb4bf19f16c9acd94b5200140a825d089b [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
589 SDNode *PrevNode = DAG.getRoot().getNode();
Daniel Dunbar819309e2009-12-16 20:10:05 +0000590
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000591 // Note: this doesn't use InstVisitor, because it has to work with
592 // ConstantExpr's in addition to instructions.
593 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000594 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000595 // Build the switch statement using the Instruction.def file.
596#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000597 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000598#include "llvm/Instruction.def"
599 }
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000600
601 if (DisableScheduling && DAG.getRoot().getNode() != PrevNode)
602 DAG.AssignOrdering(DAG.getRoot().getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000603}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000604
Dan Gohman2048b852009-11-23 18:04:58 +0000605SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000606 SDValue &N = NodeMap[V];
607 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000608
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000609 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000610 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000611
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000612 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000613 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000614
615 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
616 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000617
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000618 if (isa<ConstantPointerNull>(C))
619 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000620
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000621 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000622 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000623
Nate Begeman9008ca62009-04-27 18:41:29 +0000624 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000625 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000626
627 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
628 visit(CE->getOpcode(), *CE);
629 SDValue N1 = NodeMap[V];
630 assert(N1.getNode() && "visit didn't populate the ValueMap!");
631 return N1;
632 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000633
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000634 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
635 SmallVector<SDValue, 4> Constants;
636 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
637 OI != OE; ++OI) {
638 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000639 // If the operand is an empty aggregate, there are no values.
640 if (!Val) continue;
641 // Add each leaf value from the operand to the Constants list
642 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000643 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
644 Constants.push_back(SDValue(Val, i));
645 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000646 return DAG.getMergeValues(&Constants[0], Constants.size(),
647 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000648 }
649
650 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
651 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
652 "Unknown struct or array constant!");
653
Owen Andersone50ed302009-08-10 22:56:29 +0000654 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000655 ComputeValueVTs(TLI, C->getType(), ValueVTs);
656 unsigned NumElts = ValueVTs.size();
657 if (NumElts == 0)
658 return SDValue(); // empty struct
659 SmallVector<SDValue, 4> Constants(NumElts);
660 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000661 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000662 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000663 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000664 else if (EltVT.isFloatingPoint())
665 Constants[i] = DAG.getConstantFP(0, EltVT);
666 else
667 Constants[i] = DAG.getConstant(0, EltVT);
668 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000669 return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000670 }
671
Dan Gohman8c2b5252009-10-30 01:27:03 +0000672 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000673 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000674
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000675 const VectorType *VecTy = cast<VectorType>(V->getType());
676 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000677
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000678 // Now that we know the number and type of the elements, get that number of
679 // elements into the Ops array based on what kind of constant it is.
680 SmallVector<SDValue, 16> Ops;
681 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
682 for (unsigned i = 0; i != NumElements; ++i)
683 Ops.push_back(getValue(CP->getOperand(i)));
684 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000685 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000686 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000687
688 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000689 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000690 Op = DAG.getConstantFP(0, EltVT);
691 else
692 Op = DAG.getConstant(0, EltVT);
693 Ops.assign(NumElements, Op);
694 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000695
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000696 // Create a BUILD_VECTOR node.
Evan Chenga87008d2009-02-25 22:49:59 +0000697 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
698 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000699 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000700
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000701 // If this is a static alloca, generate it as the frameindex instead of
702 // computation.
703 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
704 DenseMap<const AllocaInst*, int>::iterator SI =
705 FuncInfo.StaticAllocaMap.find(AI);
706 if (SI != FuncInfo.StaticAllocaMap.end())
707 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
708 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000709
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000710 unsigned InReg = FuncInfo.ValueMap[V];
711 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000712
Owen Anderson23b9b192009-08-12 00:36:31 +0000713 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000714 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +0000715 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000716}
717
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000718/// Get the EVTs and ArgFlags collections that represent the return type
719/// of the given function. This does not require a DAG or a return value, and
720/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000721static void getReturnInfo(const Type* ReturnType,
722 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000723 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000724 TargetLowering &TLI,
725 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000726 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000727 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000728 unsigned NumValues = ValueVTs.size();
729 if ( NumValues == 0 ) return;
730
731 for (unsigned j = 0, f = NumValues; j != f; ++j) {
732 EVT VT = ValueVTs[j];
733 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000734
735 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000736 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000737 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000738 ExtendKind = ISD::ZERO_EXTEND;
739
740 // FIXME: C calling convention requires the return type to be promoted to
741 // at least 32-bit. But this is not necessary for non-C calling
742 // conventions. The frontend should mark functions whose return values
743 // require promoting with signext or zeroext attributes.
744 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000745 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000746 if (VT.bitsLT(MinVT))
747 VT = MinVT;
748 }
749
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000750 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
751 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000752 // 'inreg' on function refers to return value
753 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000754 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000755 Flags.setInReg();
756
757 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000758 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000759 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000760 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000761 Flags.setZExt();
762
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000763 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000764 OutVTs.push_back(PartVT);
765 OutFlags.push_back(Flags);
766 }
767 }
768}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000769
Dan Gohman2048b852009-11-23 18:04:58 +0000770void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000771 SDValue Chain = getControlRoot();
772 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000773 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
774
775 if (!FLI.CanLowerReturn) {
776 unsigned DemoteReg = FLI.DemoteRegister;
777 const Function *F = I.getParent()->getParent();
778
779 // Emit a store of the return value through the virtual register.
780 // Leave Outs empty so that LowerReturn won't try to load return
781 // registers the usual way.
782 SmallVector<EVT, 1> PtrValueVTs;
783 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
784 PtrValueVTs);
785
786 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
787 SDValue RetOp = getValue(I.getOperand(0));
788
Owen Andersone50ed302009-08-10 22:56:29 +0000789 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000790 SmallVector<uint64_t, 4> Offsets;
791 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000792 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000793
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000794 SmallVector<SDValue, 4> Chains(NumValues);
795 EVT PtrVT = PtrValueVTs[0];
796 for (unsigned i = 0; i != NumValues; ++i)
797 Chains[i] = DAG.getStore(Chain, getCurDebugLoc(),
798 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
799 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
800 DAG.getConstant(Offsets[i], PtrVT)),
801 NULL, Offsets[i], false, 0);
802 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
803 MVT::Other, &Chains[0], NumValues);
804 }
805 else {
806 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
807 SmallVector<EVT, 4> ValueVTs;
808 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
809 unsigned NumValues = ValueVTs.size();
810 if (NumValues == 0) continue;
811
812 SDValue RetOp = getValue(I.getOperand(i));
813 for (unsigned j = 0, f = NumValues; j != f; ++j) {
814 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000815
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000816 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000817
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000818 const Function *F = I.getParent()->getParent();
819 if (F->paramHasAttr(0, Attribute::SExt))
820 ExtendKind = ISD::SIGN_EXTEND;
821 else if (F->paramHasAttr(0, Attribute::ZExt))
822 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000823
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000824 // FIXME: C calling convention requires the return type to be promoted to
825 // at least 32-bit. But this is not necessary for non-C calling
826 // conventions. The frontend should mark functions whose return values
827 // require promoting with signext or zeroext attributes.
828 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
829 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
830 if (VT.bitsLT(MinVT))
831 VT = MinVT;
832 }
833
834 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
835 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
836 SmallVector<SDValue, 4> Parts(NumParts);
837 getCopyToParts(DAG, getCurDebugLoc(),
838 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
839 &Parts[0], NumParts, PartVT, ExtendKind);
840
841 // 'inreg' on function refers to return value
842 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
843 if (F->paramHasAttr(0, Attribute::InReg))
844 Flags.setInReg();
845
846 // Propagate extension type if any
847 if (F->paramHasAttr(0, Attribute::SExt))
848 Flags.setSExt();
849 else if (F->paramHasAttr(0, Attribute::ZExt))
850 Flags.setZExt();
851
852 for (unsigned i = 0; i < NumParts; ++i)
853 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000854 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000855 }
856 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000857
858 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000859 CallingConv::ID CallConv =
860 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000861 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
862 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000863
864 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000865 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000866 "LowerReturn didn't return a valid chain!");
867
868 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000869 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000870}
871
Dan Gohmanad62f532009-04-23 23:13:24 +0000872/// CopyToExportRegsIfNeeded - If the given value has virtual registers
873/// created for it, emit nodes to copy the value into the virtual
874/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000875void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000876 if (!V->use_empty()) {
877 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
878 if (VMI != FuncInfo.ValueMap.end())
879 CopyValueToVirtualRegister(V, VMI->second);
880 }
881}
882
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000883/// ExportFromCurrentBlock - If this condition isn't known to be exported from
884/// the current basic block, add it to ValueMap now so that we'll get a
885/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000886void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000887 // No need to export constants.
888 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000889
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000890 // Already exported?
891 if (FuncInfo.isExportedInst(V)) return;
892
893 unsigned Reg = FuncInfo.InitializeRegForValue(V);
894 CopyValueToVirtualRegister(V, Reg);
895}
896
Dan Gohman2048b852009-11-23 18:04:58 +0000897bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
898 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000899 // The operands of the setcc have to be in this block. We don't know
900 // how to export them from some other block.
901 if (Instruction *VI = dyn_cast<Instruction>(V)) {
902 // Can export from current BB.
903 if (VI->getParent() == FromBB)
904 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000905
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000906 // Is already exported, noop.
907 return FuncInfo.isExportedInst(V);
908 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000910 // If this is an argument, we can export it if the BB is the entry block or
911 // if it is already exported.
912 if (isa<Argument>(V)) {
913 if (FromBB == &FromBB->getParent()->getEntryBlock())
914 return true;
915
916 // Otherwise, can only export this if it is already exported.
917 return FuncInfo.isExportedInst(V);
918 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000919
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000920 // Otherwise, constants can always be exported.
921 return true;
922}
923
924static bool InBlock(const Value *V, const BasicBlock *BB) {
925 if (const Instruction *I = dyn_cast<Instruction>(V))
926 return I->getParent() == BB;
927 return true;
928}
929
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000930/// getFCmpCondCode - Return the ISD condition code corresponding to
931/// the given LLVM IR floating-point condition code. This includes
932/// consideration of global floating-point math flags.
933///
934static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
935 ISD::CondCode FPC, FOC;
936 switch (Pred) {
937 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
938 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
939 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
940 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
941 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
942 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
943 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
944 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
945 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
946 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
947 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
948 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
949 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
950 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
951 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
952 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
953 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000954 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000955 FOC = FPC = ISD::SETFALSE;
956 break;
957 }
958 if (FiniteOnlyFPMath())
959 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000960 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000961 return FPC;
962}
963
964/// getICmpCondCode - Return the ISD condition code corresponding to
965/// the given LLVM IR integer condition code.
966///
967static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
968 switch (Pred) {
969 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
970 case ICmpInst::ICMP_NE: return ISD::SETNE;
971 case ICmpInst::ICMP_SLE: return ISD::SETLE;
972 case ICmpInst::ICMP_ULE: return ISD::SETULE;
973 case ICmpInst::ICMP_SGE: return ISD::SETGE;
974 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
975 case ICmpInst::ICMP_SLT: return ISD::SETLT;
976 case ICmpInst::ICMP_ULT: return ISD::SETULT;
977 case ICmpInst::ICMP_SGT: return ISD::SETGT;
978 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
979 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000980 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000981 return ISD::SETNE;
982 }
983}
984
Dan Gohmanc2277342008-10-17 21:16:08 +0000985/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
986/// This function emits a branch and is used at the leaves of an OR or an
987/// AND operator tree.
988///
989void
Dan Gohman2048b852009-11-23 18:04:58 +0000990SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
991 MachineBasicBlock *TBB,
992 MachineBasicBlock *FBB,
993 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000994 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000995
Dan Gohmanc2277342008-10-17 21:16:08 +0000996 // If the leaf of the tree is a comparison, merge the condition into
997 // the caseblock.
998 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
999 // The operands of the cmp have to be in this block. We don't know
1000 // how to export them from some other block. If this is the first block
1001 // of the sequence, no exporting is needed.
1002 if (CurBB == CurMBB ||
1003 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1004 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001005 ISD::CondCode Condition;
1006 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001007 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001008 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001009 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001010 } else {
1011 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001012 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001013 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001014
1015 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001016 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1017 SwitchCases.push_back(CB);
1018 return;
1019 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001020 }
1021
1022 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001023 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001024 NULL, TBB, FBB, CurBB);
1025 SwitchCases.push_back(CB);
1026}
1027
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001028/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001029void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1030 MachineBasicBlock *TBB,
1031 MachineBasicBlock *FBB,
1032 MachineBasicBlock *CurBB,
1033 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001034 // If this node is not part of the or/and tree, emit it as a branch.
1035 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001036 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001037 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1038 BOp->getParent() != CurBB->getBasicBlock() ||
1039 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1040 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1041 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001042 return;
1043 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001044
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001045 // Create TmpBB after CurBB.
1046 MachineFunction::iterator BBI = CurBB;
1047 MachineFunction &MF = DAG.getMachineFunction();
1048 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1049 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001051 if (Opc == Instruction::Or) {
1052 // Codegen X | Y as:
1053 // jmp_if_X TBB
1054 // jmp TmpBB
1055 // TmpBB:
1056 // jmp_if_Y TBB
1057 // jmp FBB
1058 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001059
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001060 // Emit the LHS condition.
1061 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001062
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001063 // Emit the RHS condition into TmpBB.
1064 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1065 } else {
1066 assert(Opc == Instruction::And && "Unknown merge op!");
1067 // Codegen X & Y as:
1068 // jmp_if_X TmpBB
1069 // jmp FBB
1070 // TmpBB:
1071 // jmp_if_Y TBB
1072 // jmp FBB
1073 //
1074 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001075
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001076 // Emit the LHS condition.
1077 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001078
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001079 // Emit the RHS condition into TmpBB.
1080 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1081 }
1082}
1083
1084/// If the set of cases should be emitted as a series of branches, return true.
1085/// If we should emit this as a bunch of and/or'd together conditions, return
1086/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001087bool
Dan Gohman2048b852009-11-23 18:04:58 +00001088SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001089 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001091 // If this is two comparisons of the same values or'd or and'd together, they
1092 // will get folded into a single comparison, so don't emit two blocks.
1093 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1094 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1095 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1096 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1097 return false;
1098 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001099
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001100 return true;
1101}
1102
Dan Gohman2048b852009-11-23 18:04:58 +00001103void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001104 // Update machine-CFG edges.
1105 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1106
1107 // Figure out which block is immediately after the current one.
1108 MachineBasicBlock *NextBlock = 0;
1109 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001110 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001111 NextBlock = BBI;
1112
1113 if (I.isUnconditional()) {
1114 // Update machine-CFG edges.
1115 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001116
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001117 // If this is not a fall-through branch, emit the branch.
1118 if (Succ0MBB != NextBlock)
Scott Michelfdc40a02009-02-17 22:15:04 +00001119 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001120 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001121 DAG.getBasicBlock(Succ0MBB)));
1122 return;
1123 }
1124
1125 // If this condition is one of the special cases we handle, do special stuff
1126 // now.
1127 Value *CondVal = I.getCondition();
1128 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1129
1130 // If this is a series of conditions that are or'd or and'd together, emit
1131 // this as a sequence of branches instead of setcc's with and/or operations.
1132 // For example, instead of something like:
1133 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001134 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001135 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001136 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001137 // or C, F
1138 // jnz foo
1139 // Emit:
1140 // cmp A, B
1141 // je foo
1142 // cmp D, E
1143 // jle foo
1144 //
1145 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001146 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001147 (BOp->getOpcode() == Instruction::And ||
1148 BOp->getOpcode() == Instruction::Or)) {
1149 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1150 // If the compares in later blocks need to use values not currently
1151 // exported from this block, export them now. This block should always
1152 // be the first entry.
1153 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155 // Allow some cases to be rejected.
1156 if (ShouldEmitAsBranches(SwitchCases)) {
1157 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1158 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1159 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1160 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001161
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001162 // Emit the branch for this block.
1163 visitSwitchCase(SwitchCases[0]);
1164 SwitchCases.erase(SwitchCases.begin());
1165 return;
1166 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001167
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001168 // Okay, we decided not to do this, remove any inserted MBB's and clear
1169 // SwitchCases.
1170 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001171 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001172
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001173 SwitchCases.clear();
1174 }
1175 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001176
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001177 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001178 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001179 NULL, Succ0MBB, Succ1MBB, CurMBB);
1180 // Use visitSwitchCase to actually insert the fast branch sequence for this
1181 // cond branch.
1182 visitSwitchCase(CB);
1183}
1184
1185/// visitSwitchCase - Emits the necessary code to represent a single node in
1186/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001187void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001188 SDValue Cond;
1189 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001190 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001191
1192 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001193 if (CB.CmpMHS == NULL) {
1194 // Fold "(X == true)" to X and "(X == false)" to !X to
1195 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001196 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001197 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001198 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001199 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001200 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001201 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001202 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001203 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001204 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205 } else {
1206 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1207
Anton Korobeynikov23218582008-12-23 22:25:27 +00001208 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1209 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001210
1211 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001212 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001213
1214 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001216 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001217 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001218 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001219 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001220 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001221 DAG.getConstant(High-Low, VT), ISD::SETULE);
1222 }
1223 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001224
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001225 // Update successor info
1226 CurMBB->addSuccessor(CB.TrueBB);
1227 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001228
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001229 // Set NextBlock to be the MBB immediately after the current one, if any.
1230 // This is used to avoid emitting unnecessary branches to the next block.
1231 MachineBasicBlock *NextBlock = 0;
1232 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001233 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001234 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001235
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236 // If the lhs block is the next block, invert the condition so that we can
1237 // fall through to the lhs instead of the rhs block.
1238 if (CB.TrueBB == NextBlock) {
1239 std::swap(CB.TrueBB, CB.FalseBB);
1240 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001241 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001242 }
Dale Johannesenf5d97892009-02-04 01:48:28 +00001243 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001244 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001245 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001246
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001247 // If the branch was constant folded, fix up the CFG.
1248 if (BrCond.getOpcode() == ISD::BR) {
1249 CurMBB->removeSuccessor(CB.FalseBB);
1250 DAG.setRoot(BrCond);
1251 } else {
1252 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001253 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001254 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001255
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001256 if (CB.FalseBB == NextBlock)
1257 DAG.setRoot(BrCond);
1258 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001259 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001260 DAG.getBasicBlock(CB.FalseBB)));
1261 }
1262}
1263
1264/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001265void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001266 // Emit the code for the jump table
1267 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001268 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001269 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1270 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001271 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Scott Michelfdc40a02009-02-17 22:15:04 +00001272 DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001273 MVT::Other, Index.getValue(1),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001274 Table, Index));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275}
1276
1277/// visitJumpTableHeader - This function emits necessary code to produce index
1278/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001279void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1280 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001281 // Subtract the lowest switch case value from the value being switched on and
1282 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001283 // difference between smallest and largest cases.
1284 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001285 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001286 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001287 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001288
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001289 // The SDNode we just created, which holds the value being switched on minus
1290 // the the smallest case value, needs to be copied to a virtual register so it
1291 // can be used as an index into the jump table in a subsequent basic block.
1292 // This value may be smaller or larger than the target's pointer type, and
1293 // therefore require extension or truncating.
Duncan Sands3a66a682009-10-13 21:04:12 +00001294 SwitchOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001295
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001296 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001297 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1298 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001299 JT.Reg = JumpTableReg;
1300
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001301 // Emit the range check for the jump table, and branch to the default block
1302 // for the switch statement if the value being switched on exceeds the largest
1303 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001304 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
1305 TLI.getSetCCResultType(SUB.getValueType()), SUB,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001306 DAG.getConstant(JTH.Last-JTH.First,VT),
1307 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001308
1309 // Set NextBlock to be the MBB immediately after the current one, if any.
1310 // This is used to avoid emitting unnecessary branches to the next block.
1311 MachineBasicBlock *NextBlock = 0;
1312 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001313 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001314 NextBlock = BBI;
1315
Dale Johannesen66978ee2009-01-31 02:22:37 +00001316 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001318 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001319
1320 if (JT.MBB == NextBlock)
1321 DAG.setRoot(BrCond);
1322 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001324 DAG.getBasicBlock(JT.MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001325}
1326
1327/// visitBitTestHeader - This function emits necessary code to produce value
1328/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001329void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330 // Subtract the minimum value
1331 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001332 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001333 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001334 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001335
1336 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001337 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
1338 TLI.getSetCCResultType(SUB.getValueType()),
1339 SUB, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001340 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001341
Duncan Sands3a66a682009-10-13 21:04:12 +00001342 SDValue ShiftOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001343
Duncan Sands92abc622009-01-31 15:50:11 +00001344 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001345 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1346 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347
1348 // Set NextBlock to be the MBB immediately after the current one, if any.
1349 // This is used to avoid emitting unnecessary branches to the next block.
1350 MachineBasicBlock *NextBlock = 0;
1351 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001352 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001353 NextBlock = BBI;
1354
1355 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1356
1357 CurMBB->addSuccessor(B.Default);
1358 CurMBB->addSuccessor(MBB);
1359
Dale Johannesen66978ee2009-01-31 02:22:37 +00001360 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001361 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001362 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001363
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001364 if (MBB == NextBlock)
1365 DAG.setRoot(BrRange);
1366 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001367 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001368 DAG.getBasicBlock(MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001369}
1370
1371/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001372void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1373 unsigned Reg,
1374 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001375 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001376 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001377 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001378 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001379 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001380 DAG.getConstant(1, TLI.getPointerTy()),
1381 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001382
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001383 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001384 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001385 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001386 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001387 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1388 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001389 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001390 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001391
1392 CurMBB->addSuccessor(B.TargetBB);
1393 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001394
Dale Johannesen66978ee2009-01-31 02:22:37 +00001395 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001396 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001397 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001398
1399 // Set NextBlock to be the MBB immediately after the current one, if any.
1400 // This is used to avoid emitting unnecessary branches to the next block.
1401 MachineBasicBlock *NextBlock = 0;
1402 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001403 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001404 NextBlock = BBI;
1405
1406 if (NextMBB == NextBlock)
1407 DAG.setRoot(BrAnd);
1408 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001409 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001410 DAG.getBasicBlock(NextMBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001411}
1412
Dan Gohman2048b852009-11-23 18:04:58 +00001413void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001414 // Retrieve successors.
1415 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1416 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1417
Gabor Greifb67e6b32009-01-15 11:10:44 +00001418 const Value *Callee(I.getCalledValue());
1419 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001420 visitInlineAsm(&I);
1421 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001422 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001423
1424 // If the value of the invoke is used outside of its defining block, make it
1425 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001426 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001427
1428 // Update successor info
1429 CurMBB->addSuccessor(Return);
1430 CurMBB->addSuccessor(LandingPad);
1431
1432 // Drop into normal successor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001433 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001435 DAG.getBasicBlock(Return)));
1436}
1437
Dan Gohman2048b852009-11-23 18:04:58 +00001438void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001439}
1440
1441/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1442/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001443bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1444 CaseRecVector& WorkList,
1445 Value* SV,
1446 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001447 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001448
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001450 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001451 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001452 return false;
1453
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454 // Get the MachineFunction which holds the current MBB. This is used when
1455 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001456 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001457
1458 // Figure out which block is immediately after the current one.
1459 MachineBasicBlock *NextBlock = 0;
1460 MachineFunction::iterator BBI = CR.CaseBB;
1461
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001462 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001463 NextBlock = BBI;
1464
1465 // TODO: If any two of the cases has the same destination, and if one value
1466 // is the same as the other, but has one bit unset that the other has set,
1467 // use bit manipulation to do two compares at once. For example:
1468 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001469
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470 // Rearrange the case blocks so that the last one falls through if possible.
1471 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1472 // The last case block won't fall through into 'NextBlock' if we emit the
1473 // branches in this order. See if rearranging a case value would help.
1474 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1475 if (I->BB == NextBlock) {
1476 std::swap(*I, BackCase);
1477 break;
1478 }
1479 }
1480 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001481
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001482 // Create a CaseBlock record representing a conditional branch to
1483 // the Case's target mbb if the value being switched on SV is equal
1484 // to C.
1485 MachineBasicBlock *CurBlock = CR.CaseBB;
1486 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1487 MachineBasicBlock *FallThrough;
1488 if (I != E-1) {
1489 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1490 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001491
1492 // Put SV in a virtual register to make it available from the new blocks.
1493 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001494 } else {
1495 // If the last case doesn't match, go to the default block.
1496 FallThrough = Default;
1497 }
1498
1499 Value *RHS, *LHS, *MHS;
1500 ISD::CondCode CC;
1501 if (I->High == I->Low) {
1502 // This is just small small case range :) containing exactly 1 case
1503 CC = ISD::SETEQ;
1504 LHS = SV; RHS = I->High; MHS = NULL;
1505 } else {
1506 CC = ISD::SETLE;
1507 LHS = I->Low; MHS = SV; RHS = I->High;
1508 }
1509 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001510
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001511 // If emitting the first comparison, just call visitSwitchCase to emit the
1512 // code into the current block. Otherwise, push the CaseBlock onto the
1513 // vector to be later processed by SDISel, and insert the node's MBB
1514 // before the next MBB.
1515 if (CurBlock == CurMBB)
1516 visitSwitchCase(CB);
1517 else
1518 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001519
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001520 CurBlock = FallThrough;
1521 }
1522
1523 return true;
1524}
1525
1526static inline bool areJTsAllowed(const TargetLowering &TLI) {
1527 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001528 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1529 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001530}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001531
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001532static APInt ComputeRange(const APInt &First, const APInt &Last) {
1533 APInt LastExt(Last), FirstExt(First);
1534 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1535 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1536 return (LastExt - FirstExt + 1ULL);
1537}
1538
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001539/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001540bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1541 CaseRecVector& WorkList,
1542 Value* SV,
1543 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001544 Case& FrontCase = *CR.Range.first;
1545 Case& BackCase = *(CR.Range.second-1);
1546
Chris Lattnere880efe2009-11-07 07:50:34 +00001547 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1548 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001549
Chris Lattnere880efe2009-11-07 07:50:34 +00001550 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1552 I!=E; ++I)
1553 TSize += I->size();
1554
Chris Lattnere880efe2009-11-07 07:50:34 +00001555 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001556 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001557
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001558 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001559 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001560 if (Density < 0.4)
1561 return false;
1562
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001563 DEBUG(errs() << "Lowering jump table\n"
1564 << "First entry: " << First << ". Last entry: " << Last << '\n'
1565 << "Range: " << Range
1566 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567
1568 // Get the MachineFunction which holds the current MBB. This is used when
1569 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001570 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001571
1572 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001573 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001574 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001575
1576 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1577
1578 // Create a new basic block to hold the code for loading the address
1579 // of the jump table, and jumping to it. Update successor information;
1580 // we will either branch to the default case for the switch, or the jump
1581 // table.
1582 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1583 CurMF->insert(BBI, JumpTableBB);
1584 CR.CaseBB->addSuccessor(Default);
1585 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001586
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001587 // Build a vector of destination BBs, corresponding to each target
1588 // of the jump table. If the value of the jump table slot corresponds to
1589 // a case statement, push the case's BB onto the vector, otherwise, push
1590 // the default BB.
1591 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001592 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001594 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1595 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1596
1597 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001598 DestBBs.push_back(I->BB);
1599 if (TEI==High)
1600 ++I;
1601 } else {
1602 DestBBs.push_back(Default);
1603 }
1604 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001605
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001606 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001607 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1608 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609 E = DestBBs.end(); I != E; ++I) {
1610 if (!SuccsHandled[(*I)->getNumber()]) {
1611 SuccsHandled[(*I)->getNumber()] = true;
1612 JumpTableBB->addSuccessor(*I);
1613 }
1614 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001615
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001616 // Create a jump table index for this jump table, or return an existing
1617 // one.
1618 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001619
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 // Set the jump table information so that we can codegen it as a second
1621 // MachineBasicBlock
1622 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1623 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1624 if (CR.CaseBB == CurMBB)
1625 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001626
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001627 JTCases.push_back(JumpTableBlock(JTH, JT));
1628
1629 return true;
1630}
1631
1632/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1633/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001634bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1635 CaseRecVector& WorkList,
1636 Value* SV,
1637 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638 // Get the MachineFunction which holds the current MBB. This is used when
1639 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001640 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001641
1642 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001643 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001644 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001645
1646 Case& FrontCase = *CR.Range.first;
1647 Case& BackCase = *(CR.Range.second-1);
1648 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1649
1650 // Size is the number of Cases represented by this range.
1651 unsigned Size = CR.Range.second - CR.Range.first;
1652
Chris Lattnere880efe2009-11-07 07:50:34 +00001653 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1654 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001655 double FMetric = 0;
1656 CaseItr Pivot = CR.Range.first + Size/2;
1657
1658 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1659 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001660 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001661 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1662 I!=E; ++I)
1663 TSize += I->size();
1664
Chris Lattnere880efe2009-11-07 07:50:34 +00001665 APInt LSize = FrontCase.size();
1666 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001667 DEBUG(errs() << "Selecting best pivot: \n"
1668 << "First: " << First << ", Last: " << Last <<'\n'
1669 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001670 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1671 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001672 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1673 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001674 APInt Range = ComputeRange(LEnd, RBegin);
1675 assert((Range - 2ULL).isNonNegative() &&
1676 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001677 double LDensity = (double)LSize.roundToDouble() /
1678 (LEnd - First + 1ULL).roundToDouble();
1679 double RDensity = (double)RSize.roundToDouble() /
1680 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001681 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001682 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001683 DEBUG(errs() <<"=>Step\n"
1684 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1685 << "LDensity: " << LDensity
1686 << ", RDensity: " << RDensity << '\n'
1687 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001688 if (FMetric < Metric) {
1689 Pivot = J;
1690 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001691 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001692 }
1693
1694 LSize += J->size();
1695 RSize -= J->size();
1696 }
1697 if (areJTsAllowed(TLI)) {
1698 // If our case is dense we *really* should handle it earlier!
1699 assert((FMetric > 0) && "Should handle dense range earlier!");
1700 } else {
1701 Pivot = CR.Range.first + Size/2;
1702 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001703
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001704 CaseRange LHSR(CR.Range.first, Pivot);
1705 CaseRange RHSR(Pivot, CR.Range.second);
1706 Constant *C = Pivot->Low;
1707 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001708
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001709 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001710 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001711 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001712 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001713 // Pivot's Value, then we can branch directly to the LHS's Target,
1714 // rather than creating a leaf node for it.
1715 if ((LHSR.second - LHSR.first) == 1 &&
1716 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001717 cast<ConstantInt>(C)->getValue() ==
1718 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719 TrueBB = LHSR.first->BB;
1720 } else {
1721 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1722 CurMF->insert(BBI, TrueBB);
1723 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001724
1725 // Put SV in a virtual register to make it available from the new blocks.
1726 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001727 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001728
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001729 // Similar to the optimization above, if the Value being switched on is
1730 // known to be less than the Constant CR.LT, and the current Case Value
1731 // is CR.LT - 1, then we can branch directly to the target block for
1732 // the current Case Value, rather than emitting a RHS leaf node for it.
1733 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001734 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1735 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001736 FalseBB = RHSR.first->BB;
1737 } else {
1738 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1739 CurMF->insert(BBI, FalseBB);
1740 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001741
1742 // Put SV in a virtual register to make it available from the new blocks.
1743 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744 }
1745
1746 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001747 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748 // Otherwise, branch to LHS.
1749 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1750
1751 if (CR.CaseBB == CurMBB)
1752 visitSwitchCase(CB);
1753 else
1754 SwitchCases.push_back(CB);
1755
1756 return true;
1757}
1758
1759/// handleBitTestsSwitchCase - if current case range has few destination and
1760/// range span less, than machine word bitwidth, encode case range into series
1761/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001762bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1763 CaseRecVector& WorkList,
1764 Value* SV,
1765 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001766 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001767 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001768
1769 Case& FrontCase = *CR.Range.first;
1770 Case& BackCase = *(CR.Range.second-1);
1771
1772 // Get the MachineFunction which holds the current MBB. This is used when
1773 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001774 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001775
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001776 // If target does not have legal shift left, do not emit bit tests at all.
1777 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1778 return false;
1779
Anton Korobeynikov23218582008-12-23 22:25:27 +00001780 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1782 I!=E; ++I) {
1783 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001784 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001785 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001786
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787 // Count unique destinations
1788 SmallSet<MachineBasicBlock*, 4> Dests;
1789 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1790 Dests.insert(I->BB);
1791 if (Dests.size() > 3)
1792 // Don't bother the code below, if there are too much unique destinations
1793 return false;
1794 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001795 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1796 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001797
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001798 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001799 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1800 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001801 APInt cmpRange = maxValue - minValue;
1802
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001803 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1804 << "Low bound: " << minValue << '\n'
1805 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001806
1807 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 (!(Dests.size() == 1 && numCmps >= 3) &&
1809 !(Dests.size() == 2 && numCmps >= 5) &&
1810 !(Dests.size() >= 3 && numCmps >= 6)))
1811 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001812
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001813 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001814 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1815
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816 // Optimize the case where all the case values fit in a
1817 // word without having to subtract minValue. In this case,
1818 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001819 if (minValue.isNonNegative() &&
1820 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1821 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001822 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001823 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001824 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001825
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 CaseBitsVector CasesBits;
1827 unsigned i, count = 0;
1828
1829 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1830 MachineBasicBlock* Dest = I->BB;
1831 for (i = 0; i < count; ++i)
1832 if (Dest == CasesBits[i].BB)
1833 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001834
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001835 if (i == count) {
1836 assert((count < 3) && "Too much destinations to test!");
1837 CasesBits.push_back(CaseBits(0, Dest, 0));
1838 count++;
1839 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001840
1841 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1842 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1843
1844 uint64_t lo = (lowValue - lowBound).getZExtValue();
1845 uint64_t hi = (highValue - lowBound).getZExtValue();
1846
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001847 for (uint64_t j = lo; j <= hi; j++) {
1848 CasesBits[i].Mask |= 1ULL << j;
1849 CasesBits[i].Bits++;
1850 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001851
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001852 }
1853 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001854
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 BitTestInfo BTC;
1856
1857 // Figure out which block is immediately after the current one.
1858 MachineFunction::iterator BBI = CR.CaseBB;
1859 ++BBI;
1860
1861 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1862
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001863 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001864 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001865 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
1866 << ", Bits: " << CasesBits[i].Bits
1867 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001868
1869 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1870 CurMF->insert(BBI, CaseBB);
1871 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1872 CaseBB,
1873 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001874
1875 // Put SV in a virtual register to make it available from the new blocks.
1876 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001877 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001878
1879 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001880 -1U, (CR.CaseBB == CurMBB),
1881 CR.CaseBB, Default, BTC);
1882
1883 if (CR.CaseBB == CurMBB)
1884 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001885
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 BitTestCases.push_back(BTB);
1887
1888 return true;
1889}
1890
1891
1892/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001893size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1894 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001895 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001896
1897 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001898 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001899 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1900 Cases.push_back(Case(SI.getSuccessorValue(i),
1901 SI.getSuccessorValue(i),
1902 SMBB));
1903 }
1904 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1905
1906 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001907 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001908 // Must recompute end() each iteration because it may be
1909 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001910 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1911 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1912 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001913 MachineBasicBlock* nextBB = J->BB;
1914 MachineBasicBlock* currentBB = I->BB;
1915
1916 // If the two neighboring cases go to the same destination, merge them
1917 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001918 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919 I->High = J->High;
1920 J = Cases.erase(J);
1921 } else {
1922 I = J++;
1923 }
1924 }
1925
1926 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1927 if (I->Low != I->High)
1928 // A range counts double, since it requires two compares.
1929 ++numCmps;
1930 }
1931
1932 return numCmps;
1933}
1934
Dan Gohman2048b852009-11-23 18:04:58 +00001935void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001936 // Figure out which block is immediately after the current one.
1937 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001938
1939 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1940
1941 // If there is only the default destination, branch to it if it is not the
1942 // next basic block. Otherwise, just fall through.
1943 if (SI.getNumOperands() == 2) {
1944 // Update machine-CFG edges.
1945
1946 // If this is not a fall-through branch, emit the branch.
1947 CurMBB->addSuccessor(Default);
1948 if (Default != NextBlock)
Dale Johannesen66978ee2009-01-31 02:22:37 +00001949 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001950 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001951 DAG.getBasicBlock(Default)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001952 return;
1953 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001954
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955 // If there are any non-default case statements, create a vector of Cases
1956 // representing each one, and sort the vector so that we can efficiently
1957 // create a binary search tree from them.
1958 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001959 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001960 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
1961 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001962 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001963
1964 // Get the Value to be switched on and default basic blocks, which will be
1965 // inserted into CaseBlock records, representing basic blocks in the binary
1966 // search tree.
1967 Value *SV = SI.getOperand(0);
1968
1969 // Push the initial CaseRec onto the worklist
1970 CaseRecVector WorkList;
1971 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
1972
1973 while (!WorkList.empty()) {
1974 // Grab a record representing a case range to process off the worklist
1975 CaseRec CR = WorkList.back();
1976 WorkList.pop_back();
1977
1978 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
1979 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001980
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001981 // If the range has few cases (two or less) emit a series of specific
1982 // tests.
1983 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
1984 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001985
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001986 // If the switch has more than 5 blocks, and at least 40% dense, and the
1987 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001988 // lowering the switch to a binary tree of conditional branches.
1989 if (handleJTSwitchCase(CR, WorkList, SV, Default))
1990 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992 // Emit binary tree. We need to pick a pivot, and push left and right ranges
1993 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
1994 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
1995 }
1996}
1997
Dan Gohman2048b852009-11-23 18:04:58 +00001998void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00001999 // Update machine-CFG edges.
2000 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2001 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2002
Dan Gohman64825152009-10-27 21:56:26 +00002003 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2004 MVT::Other, getControlRoot(),
2005 getValue(I.getAddress())));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002006}
2007
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002008
Dan Gohman2048b852009-11-23 18:04:58 +00002009void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002010 // -0.0 - X --> fneg
2011 const Type *Ty = I.getType();
2012 if (isa<VectorType>(Ty)) {
2013 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2014 const VectorType *DestTy = cast<VectorType>(I.getType());
2015 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002016 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002017 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002018 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002019 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002020 SDValue Op2 = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002021 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002022 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002023 return;
2024 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002025 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002027 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002028 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002029 SDValue Op2 = getValue(I.getOperand(1));
2030 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2031 Op2.getValueType(), Op2));
2032 return;
2033 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002035 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002036}
2037
Dan Gohman2048b852009-11-23 18:04:58 +00002038void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039 SDValue Op1 = getValue(I.getOperand(0));
2040 SDValue Op2 = getValue(I.getOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002041
Scott Michelfdc40a02009-02-17 22:15:04 +00002042 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002043 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002044}
2045
Dan Gohman2048b852009-11-23 18:04:58 +00002046void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002047 SDValue Op1 = getValue(I.getOperand(0));
2048 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002049 if (!isa<VectorType>(I.getType()) &&
2050 Op2.getValueType() != TLI.getShiftAmountTy()) {
2051 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002052 EVT PTy = TLI.getPointerTy();
2053 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002054 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002055 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2056 TLI.getShiftAmountTy(), Op2);
2057 // If the operand is larger than the shift count type but the shift
2058 // count type has enough bits to represent any shift value, truncate
2059 // it now. This is a common case and it exposes the truncate to
2060 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002061 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002062 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2063 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2064 TLI.getShiftAmountTy(), Op2);
2065 // Otherwise we'll need to temporarily settle for some other
2066 // convenient type; type legalization will make adjustments as
2067 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002068 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002069 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002070 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002071 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002072 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002073 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002074 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002075
Scott Michelfdc40a02009-02-17 22:15:04 +00002076 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002077 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002078}
2079
Dan Gohman2048b852009-11-23 18:04:58 +00002080void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2082 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2083 predicate = IC->getPredicate();
2084 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2085 predicate = ICmpInst::Predicate(IC->getPredicate());
2086 SDValue Op1 = getValue(I.getOperand(0));
2087 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002088 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002089
Owen Andersone50ed302009-08-10 22:56:29 +00002090 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002091 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002092}
2093
Dan Gohman2048b852009-11-23 18:04:58 +00002094void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002095 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2096 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2097 predicate = FC->getPredicate();
2098 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2099 predicate = FCmpInst::Predicate(FC->getPredicate());
2100 SDValue Op1 = getValue(I.getOperand(0));
2101 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002102 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002103 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002104 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002105}
2106
Dan Gohman2048b852009-11-23 18:04:58 +00002107void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002108 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002109 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2110 unsigned NumValues = ValueVTs.size();
2111 if (NumValues != 0) {
2112 SmallVector<SDValue, 4> Values(NumValues);
2113 SDValue Cond = getValue(I.getOperand(0));
2114 SDValue TrueVal = getValue(I.getOperand(1));
2115 SDValue FalseVal = getValue(I.getOperand(2));
2116
2117 for (unsigned i = 0; i != NumValues; ++i)
Scott Michelfdc40a02009-02-17 22:15:04 +00002118 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Dan Gohmana4f9cc42009-12-11 19:50:50 +00002119 TrueVal.getNode()->getValueType(i), Cond,
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002120 SDValue(TrueVal.getNode(), TrueVal.getResNo() + i),
2121 SDValue(FalseVal.getNode(), FalseVal.getResNo() + i));
2122
Scott Michelfdc40a02009-02-17 22:15:04 +00002123 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002124 DAG.getVTList(&ValueVTs[0], NumValues),
2125 &Values[0], NumValues));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002126 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127}
2128
2129
Dan Gohman2048b852009-11-23 18:04:58 +00002130void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002131 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2132 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002133 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002134 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002135}
2136
Dan Gohman2048b852009-11-23 18:04:58 +00002137void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002138 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2139 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2140 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002141 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002142 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002143}
2144
Dan Gohman2048b852009-11-23 18:04:58 +00002145void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002146 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2147 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2148 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002149 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002150 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151}
2152
Dan Gohman2048b852009-11-23 18:04:58 +00002153void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002154 // FPTrunc is never a no-op cast, no need to check
2155 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002156 EVT DestVT = TLI.getValueType(I.getType());
Scott Michelfdc40a02009-02-17 22:15:04 +00002157 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002158 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002159}
2160
Dan Gohman2048b852009-11-23 18:04:58 +00002161void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002162 // FPTrunc is never a no-op cast, no need to check
2163 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002164 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002165 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166}
2167
Dan Gohman2048b852009-11-23 18:04:58 +00002168void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002169 // FPToUI is never a no-op cast, no need to check
2170 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002171 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002172 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002173}
2174
Dan Gohman2048b852009-11-23 18:04:58 +00002175void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002176 // FPToSI is never a no-op cast, no need to check
2177 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002178 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002179 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002180}
2181
Dan Gohman2048b852009-11-23 18:04:58 +00002182void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002183 // UIToFP is never a no-op cast, no need to check
2184 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002185 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002186 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187}
2188
Dan Gohman2048b852009-11-23 18:04:58 +00002189void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002190 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002191 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002192 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002193 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002194}
2195
Dan Gohman2048b852009-11-23 18:04:58 +00002196void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002197 // What to do depends on the size of the integer and the size of the pointer.
2198 // We can either truncate, zero extend, or no-op, accordingly.
2199 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002200 EVT SrcVT = N.getValueType();
2201 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002202 SDValue Result = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002203 setValue(&I, Result);
2204}
2205
Dan Gohman2048b852009-11-23 18:04:58 +00002206void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002207 // What to do depends on the size of the integer and the size of the pointer.
2208 // We can either truncate, zero extend, or no-op, accordingly.
2209 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002210 EVT SrcVT = N.getValueType();
2211 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002212 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002213}
2214
Dan Gohman2048b852009-11-23 18:04:58 +00002215void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002216 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002217 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002218
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002219 // BitCast assures us that source and destination are the same size so this
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002220 // is either a BIT_CONVERT or a no-op.
2221 if (DestVT != N.getValueType())
Scott Michelfdc40a02009-02-17 22:15:04 +00002222 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002223 DestVT, N)); // convert types
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 else
2225 setValue(&I, N); // noop cast.
2226}
2227
Dan Gohman2048b852009-11-23 18:04:58 +00002228void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002229 SDValue InVec = getValue(I.getOperand(0));
2230 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002231 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002232 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002233 getValue(I.getOperand(2)));
2234
Scott Michelfdc40a02009-02-17 22:15:04 +00002235 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002236 TLI.getValueType(I.getType()),
2237 InVec, InVal, InIdx));
2238}
2239
Dan Gohman2048b852009-11-23 18:04:58 +00002240void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002242 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002243 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002244 getValue(I.getOperand(1)));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002245 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002246 TLI.getValueType(I.getType()), InVec, InIdx));
2247}
2248
Mon P Wangaeb06d22008-11-10 04:46:22 +00002249
2250// Utility for visitShuffleVector - Returns true if the mask is mask starting
2251// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002252static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2253 unsigned MaskNumElts = Mask.size();
2254 for (unsigned i = 0; i != MaskNumElts; ++i)
2255 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002256 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002257 return true;
2258}
2259
Dan Gohman2048b852009-11-23 18:04:58 +00002260void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002261 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002262 SDValue Src1 = getValue(I.getOperand(0));
2263 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002264
Nate Begeman9008ca62009-04-27 18:41:29 +00002265 // Convert the ConstantVector mask operand into an array of ints, with -1
2266 // representing undef values.
2267 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002268 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2269 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002270 unsigned MaskNumElts = MaskElts.size();
2271 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002272 if (isa<UndefValue>(MaskElts[i]))
2273 Mask.push_back(-1);
2274 else
2275 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2276 }
2277
Owen Andersone50ed302009-08-10 22:56:29 +00002278 EVT VT = TLI.getValueType(I.getType());
2279 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002280 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002281
Mon P Wangc7849c22008-11-16 05:06:27 +00002282 if (SrcNumElts == MaskNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002283 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2284 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002285 return;
2286 }
2287
2288 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002289 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2290 // Mask is longer than the source vectors and is a multiple of the source
2291 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002292 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002293 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2294 // The shuffle is concatenating two vectors together.
Scott Michelfdc40a02009-02-17 22:15:04 +00002295 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002296 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002297 return;
2298 }
2299
Mon P Wangc7849c22008-11-16 05:06:27 +00002300 // Pad both vectors with undefs to make them the same length as the mask.
2301 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002302 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2303 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002304 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002305
Nate Begeman9008ca62009-04-27 18:41:29 +00002306 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2307 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002308 MOps1[0] = Src1;
2309 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002310
2311 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2312 getCurDebugLoc(), VT,
2313 &MOps1[0], NumConcat);
2314 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2315 getCurDebugLoc(), VT,
2316 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002317
Mon P Wangaeb06d22008-11-10 04:46:22 +00002318 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002319 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002320 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002321 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002322 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002323 MappedOps.push_back(Idx);
2324 else
2325 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002326 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002327 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2328 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002329 return;
2330 }
2331
Mon P Wangc7849c22008-11-16 05:06:27 +00002332 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002333 // Analyze the access pattern of the vector to see if we can extract
2334 // two subvectors and do the shuffle. The analysis is done by calculating
2335 // the range of elements the mask access on both vectors.
2336 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2337 int MaxRange[2] = {-1, -1};
2338
Nate Begeman5a5ca152009-04-29 05:20:52 +00002339 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002340 int Idx = Mask[i];
2341 int Input = 0;
2342 if (Idx < 0)
2343 continue;
2344
Nate Begeman5a5ca152009-04-29 05:20:52 +00002345 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002346 Input = 1;
2347 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002348 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002349 if (Idx > MaxRange[Input])
2350 MaxRange[Input] = Idx;
2351 if (Idx < MinRange[Input])
2352 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002353 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002354
Mon P Wangc7849c22008-11-16 05:06:27 +00002355 // Check if the access is smaller than the vector size and can we find
2356 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002357 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002358 int StartIdx[2]; // StartIdx to extract from
2359 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002360 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002361 RangeUse[Input] = 0; // Unused
2362 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002363 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002364 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002365 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002366 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002367 RangeUse[Input] = 1; // Extract from beginning of the vector
2368 StartIdx[Input] = 0;
2369 } else {
2370 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002371 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002372 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002373 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002374 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002375 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002376 }
2377
Bill Wendling636e2582009-08-21 18:16:06 +00002378 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002379 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002380 return;
2381 }
2382 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2383 // Extract appropriate subvector and generate a vector shuffle
2384 for (int Input=0; Input < 2; ++Input) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002385 SDValue& Src = Input == 0 ? Src1 : Src2;
Mon P Wangc7849c22008-11-16 05:06:27 +00002386 if (RangeUse[Input] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002387 Src = DAG.getUNDEF(VT);
Mon P Wangc7849c22008-11-16 05:06:27 +00002388 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002389 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002390 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002391 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002392 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002393 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002394 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002395 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002396 int Idx = Mask[i];
2397 if (Idx < 0)
2398 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002399 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002400 MappedOps.push_back(Idx - StartIdx[0]);
2401 else
2402 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002403 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002404 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2405 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002406 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002407 }
2408 }
2409
Mon P Wangc7849c22008-11-16 05:06:27 +00002410 // We can't use either concat vectors or extract subvectors so fall back to
2411 // replacing the shuffle with extract and build vector.
2412 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002413 EVT EltVT = VT.getVectorElementType();
2414 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002415 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002416 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002417 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002418 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002419 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002420 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002421 if (Idx < (int)SrcNumElts)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002422 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002423 EltVT, Src1, DAG.getConstant(Idx, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002424 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002425 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Scott Michelfdc40a02009-02-17 22:15:04 +00002426 EltVT, Src2,
Mon P Wangc7849c22008-11-16 05:06:27 +00002427 DAG.getConstant(Idx - SrcNumElts, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002428 }
2429 }
Evan Chenga87008d2009-02-25 22:49:59 +00002430 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2431 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002432}
2433
Dan Gohman2048b852009-11-23 18:04:58 +00002434void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002435 const Value *Op0 = I.getOperand(0);
2436 const Value *Op1 = I.getOperand(1);
2437 const Type *AggTy = I.getType();
2438 const Type *ValTy = Op1->getType();
2439 bool IntoUndef = isa<UndefValue>(Op0);
2440 bool FromUndef = isa<UndefValue>(Op1);
2441
2442 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2443 I.idx_begin(), I.idx_end());
2444
Owen Andersone50ed302009-08-10 22:56:29 +00002445 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002446 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002447 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002448 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2449
2450 unsigned NumAggValues = AggValueVTs.size();
2451 unsigned NumValValues = ValValueVTs.size();
2452 SmallVector<SDValue, 4> Values(NumAggValues);
2453
2454 SDValue Agg = getValue(Op0);
2455 SDValue Val = getValue(Op1);
2456 unsigned i = 0;
2457 // Copy the beginning value(s) from the original aggregate.
2458 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002459 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002460 SDValue(Agg.getNode(), Agg.getResNo() + i);
2461 // Copy values from the inserted value(s).
2462 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002463 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002464 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2465 // Copy remaining value(s) from the original aggregate.
2466 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002467 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002468 SDValue(Agg.getNode(), Agg.getResNo() + i);
2469
Scott Michelfdc40a02009-02-17 22:15:04 +00002470 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002471 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2472 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002473}
2474
Dan Gohman2048b852009-11-23 18:04:58 +00002475void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002476 const Value *Op0 = I.getOperand(0);
2477 const Type *AggTy = Op0->getType();
2478 const Type *ValTy = I.getType();
2479 bool OutOfUndef = isa<UndefValue>(Op0);
2480
2481 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2482 I.idx_begin(), I.idx_end());
2483
Owen Andersone50ed302009-08-10 22:56:29 +00002484 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002485 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2486
2487 unsigned NumValValues = ValValueVTs.size();
2488 SmallVector<SDValue, 4> Values(NumValValues);
2489
2490 SDValue Agg = getValue(Op0);
2491 // Copy out the selected value(s).
2492 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2493 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002494 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002495 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002496 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002497
Scott Michelfdc40a02009-02-17 22:15:04 +00002498 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002499 DAG.getVTList(&ValValueVTs[0], NumValValues),
2500 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002501}
2502
2503
Dan Gohman2048b852009-11-23 18:04:58 +00002504void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002505 SDValue N = getValue(I.getOperand(0));
2506 const Type *Ty = I.getOperand(0)->getType();
2507
2508 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2509 OI != E; ++OI) {
2510 Value *Idx = *OI;
2511 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2512 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2513 if (Field) {
2514 // N = N + Offset
2515 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002516 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002517 DAG.getIntPtrConstant(Offset));
2518 }
2519 Ty = StTy->getElementType(Field);
2520 } else {
2521 Ty = cast<SequentialType>(Ty)->getElementType();
2522
2523 // If this is a constant subscript, handle it quickly.
2524 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2525 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002526 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002527 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002528 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002529 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002530 unsigned PtrBits = PTy.getSizeInBits();
Evan Cheng65b52df2009-02-09 21:01:06 +00002531 if (PtrBits < 64) {
2532 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2533 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002534 DAG.getConstant(Offs, MVT::i64));
Evan Cheng65b52df2009-02-09 21:01:06 +00002535 } else
Evan Chengb1032a82009-02-09 20:54:38 +00002536 OffsVal = DAG.getIntPtrConstant(Offs);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002537 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002538 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002539 continue;
2540 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002541
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002542 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002543 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2544 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002545 SDValue IdxN = getValue(Idx);
2546
2547 // If the index is smaller or larger than intptr_t, truncate or extend
2548 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002549 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002550
2551 // If this is a multiply by a power of two, turn it into a shl
2552 // immediately. This is a very common case.
2553 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002554 if (ElementSize.isPowerOf2()) {
2555 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002556 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002557 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002558 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002559 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002560 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002561 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002562 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563 }
2564 }
2565
Scott Michelfdc40a02009-02-17 22:15:04 +00002566 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002567 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002568 }
2569 }
2570 setValue(&I, N);
2571}
2572
Dan Gohman2048b852009-11-23 18:04:58 +00002573void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002574 // If this is a fixed sized alloca in the entry block of the function,
2575 // allocate it statically on the stack.
2576 if (FuncInfo.StaticAllocaMap.count(&I))
2577 return; // getValue will auto-populate this.
2578
2579 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002580 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002581 unsigned Align =
2582 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2583 I.getAlignment());
2584
2585 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002586
2587 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2588 AllocSize,
2589 DAG.getConstant(TySize, AllocSize.getValueType()));
2590
2591
2592
Owen Andersone50ed302009-08-10 22:56:29 +00002593 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002594 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002595
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002596 // Handle alignment. If the requested alignment is less than or equal to
2597 // the stack alignment, ignore it. If the size is greater than or equal to
2598 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2599 unsigned StackAlign =
2600 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2601 if (Align <= StackAlign)
2602 Align = 0;
2603
2604 // Round the size of the allocation up to the stack alignment size
2605 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002606 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002607 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002608 DAG.getIntPtrConstant(StackAlign-1));
2609 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002610 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002611 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002612 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2613
2614 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002615 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002616 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002617 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002618 setValue(&I, DSA);
2619 DAG.setRoot(DSA.getValue(1));
2620
2621 // Inform the Frame Information that we have just allocated a variable-sized
2622 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002623 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002624}
2625
Dan Gohman2048b852009-11-23 18:04:58 +00002626void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002627 const Value *SV = I.getOperand(0);
2628 SDValue Ptr = getValue(SV);
2629
2630 const Type *Ty = I.getType();
2631 bool isVolatile = I.isVolatile();
2632 unsigned Alignment = I.getAlignment();
2633
Owen Andersone50ed302009-08-10 22:56:29 +00002634 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002635 SmallVector<uint64_t, 4> Offsets;
2636 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2637 unsigned NumValues = ValueVTs.size();
2638 if (NumValues == 0)
2639 return;
2640
2641 SDValue Root;
2642 bool ConstantMemory = false;
2643 if (I.isVolatile())
2644 // Serialize volatile loads with other side effects.
2645 Root = getRoot();
2646 else if (AA->pointsToConstantMemory(SV)) {
2647 // Do not serialize (non-volatile) loads of constant memory with anything.
2648 Root = DAG.getEntryNode();
2649 ConstantMemory = true;
2650 } else {
2651 // Do not serialize non-volatile loads against each other.
2652 Root = DAG.getRoot();
2653 }
2654
2655 SmallVector<SDValue, 4> Values(NumValues);
2656 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002657 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002658 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002659 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Nate Begemane6798372009-09-15 00:13:12 +00002660 DAG.getNode(ISD::ADD, getCurDebugLoc(),
2661 PtrVT, Ptr,
2662 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002663 SV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002664 Values[i] = L;
2665 Chains[i] = L.getValue(1);
2666 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002667
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002668 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002669 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002670 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002671 &Chains[0], NumValues);
2672 if (isVolatile)
2673 DAG.setRoot(Chain);
2674 else
2675 PendingLoads.push_back(Chain);
2676 }
2677
Scott Michelfdc40a02009-02-17 22:15:04 +00002678 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002679 DAG.getVTList(&ValueVTs[0], NumValues),
2680 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002681}
2682
2683
Dan Gohman2048b852009-11-23 18:04:58 +00002684void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002685 Value *SrcV = I.getOperand(0);
2686 Value *PtrV = I.getOperand(1);
2687
Owen Andersone50ed302009-08-10 22:56:29 +00002688 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002689 SmallVector<uint64_t, 4> Offsets;
2690 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2691 unsigned NumValues = ValueVTs.size();
2692 if (NumValues == 0)
2693 return;
2694
2695 // Get the lowered operands. Note that we do this after
2696 // checking if NumResults is zero, because with zero results
2697 // the operands won't have values in the map.
2698 SDValue Src = getValue(SrcV);
2699 SDValue Ptr = getValue(PtrV);
2700
2701 SDValue Root = getRoot();
2702 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002703 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002704 bool isVolatile = I.isVolatile();
2705 unsigned Alignment = I.getAlignment();
2706 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002707 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002708 SDValue(Src.getNode(), Src.getResNo() + i),
Scott Michelfdc40a02009-02-17 22:15:04 +00002709 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002710 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002712 PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002713
Scott Michelfdc40a02009-02-17 22:15:04 +00002714 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002715 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002716}
2717
2718/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2719/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00002720void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
2721 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002722 bool HasChain = !I.doesNotAccessMemory();
2723 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2724
2725 // Build the operand list.
2726 SmallVector<SDValue, 8> Ops;
2727 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2728 if (OnlyLoad) {
2729 // We don't need to serialize loads against other loads.
2730 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002731 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002732 Ops.push_back(getRoot());
2733 }
2734 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002735
2736 // Info is set by getTgtMemInstrinsic
2737 TargetLowering::IntrinsicInfo Info;
2738 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2739
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002740 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002741 if (!IsTgtIntrinsic)
2742 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002743
2744 // Add all operands of the call to the operand list.
2745 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2746 SDValue Op = getValue(I.getOperand(i));
2747 assert(TLI.isTypeLegal(Op.getValueType()) &&
2748 "Intrinsic uses a non-legal type?");
2749 Ops.push_back(Op);
2750 }
2751
Owen Andersone50ed302009-08-10 22:56:29 +00002752 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002753 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2754#ifndef NDEBUG
2755 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2756 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2757 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002758 }
Bob Wilson8d919552009-07-31 22:41:21 +00002759#endif // NDEBUG
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002760 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002761 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002762
Bob Wilson8d919552009-07-31 22:41:21 +00002763 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002764
2765 // Create the node.
2766 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002767 if (IsTgtIntrinsic) {
2768 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002769 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002770 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002771 Info.memVT, Info.ptrVal, Info.offset,
2772 Info.align, Info.vol,
2773 Info.readMem, Info.writeMem);
2774 }
2775 else if (!HasChain)
Scott Michelfdc40a02009-02-17 22:15:04 +00002776 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002777 VTs, &Ops[0], Ops.size());
Owen Anderson1d0be152009-08-13 21:58:54 +00002778 else if (I.getType() != Type::getVoidTy(*DAG.getContext()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002779 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002780 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002781 else
Scott Michelfdc40a02009-02-17 22:15:04 +00002782 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002783 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002784
2785 if (HasChain) {
2786 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2787 if (OnlyLoad)
2788 PendingLoads.push_back(Chain);
2789 else
2790 DAG.setRoot(Chain);
2791 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002792 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002793 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002794 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002795 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002796 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002797 setValue(&I, Result);
2798 }
2799}
2800
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002801/// GetSignificand - Get the significand and build it into a floating-point
2802/// number with exponent of 1:
2803///
2804/// Op = (Op & 0x007fffff) | 0x3f800000;
2805///
2806/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002807static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002808GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002809 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2810 DAG.getConstant(0x007fffff, MVT::i32));
2811 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2812 DAG.getConstant(0x3f800000, MVT::i32));
2813 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002814}
2815
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002816/// GetExponent - Get the exponent:
2817///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002818/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002819///
2820/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002821static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002822GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
2823 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002824 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2825 DAG.getConstant(0x7f800000, MVT::i32));
2826 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002827 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002828 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2829 DAG.getConstant(127, MVT::i32));
2830 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002831}
2832
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002833/// getF32Constant - Get 32-bit floating point constant.
2834static SDValue
2835getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002836 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002837}
2838
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002839/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002840/// visitIntrinsicCall: I is a call instruction
2841/// Op is the associated NodeType for I
2842const char *
Dan Gohman2048b852009-11-23 18:04:58 +00002843SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002844 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002845 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002846 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002847 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002848 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002849 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002850 getValue(I.getOperand(2)),
2851 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002852 setValue(&I, L);
2853 DAG.setRoot(L.getValue(1));
2854 return 0;
2855}
2856
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002857// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002858const char *
Dan Gohman2048b852009-11-23 18:04:58 +00002859SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002860 SDValue Op1 = getValue(I.getOperand(1));
2861 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002862
Owen Anderson825b72b2009-08-11 20:47:22 +00002863 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00002864 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00002865
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002866 setValue(&I, Result);
2867 return 0;
2868}
Bill Wendling74c37652008-12-09 22:08:41 +00002869
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002870/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2871/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002872void
Dan Gohman2048b852009-11-23 18:04:58 +00002873SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002874 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002875 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002876
Owen Anderson825b72b2009-08-11 20:47:22 +00002877 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002878 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2879 SDValue Op = getValue(I.getOperand(1));
2880
2881 // Put the exponent in the right bit position for later addition to the
2882 // final result:
2883 //
2884 // #define LOG2OFe 1.4426950f
2885 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002886 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002887 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002888 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002889
2890 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002891 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2892 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002893
2894 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002895 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002896 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002897
2898 if (LimitFloatPrecision <= 6) {
2899 // For floating-point precision of 6:
2900 //
2901 // TwoToFractionalPartOfX =
2902 // 0.997535578f +
2903 // (0.735607626f + 0.252464424f * x) * x;
2904 //
2905 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002906 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002907 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002908 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002909 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002910 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2911 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002912 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002913 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002914
2915 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002916 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002917 TwoToFracPartOfX, IntegerPartOfX);
2918
Owen Anderson825b72b2009-08-11 20:47:22 +00002919 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002920 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2921 // For floating-point precision of 12:
2922 //
2923 // TwoToFractionalPartOfX =
2924 // 0.999892986f +
2925 // (0.696457318f +
2926 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2927 //
2928 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002929 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002930 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002932 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002933 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2934 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002935 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00002936 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2937 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002938 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00002939 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002940
2941 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002942 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002943 TwoToFracPartOfX, IntegerPartOfX);
2944
Owen Anderson825b72b2009-08-11 20:47:22 +00002945 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002946 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2947 // For floating-point precision of 18:
2948 //
2949 // TwoToFractionalPartOfX =
2950 // 0.999999982f +
2951 // (0.693148872f +
2952 // (0.240227044f +
2953 // (0.554906021e-1f +
2954 // (0.961591928e-2f +
2955 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
2956 //
2957 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002958 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002959 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002961 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00002962 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2963 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002964 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00002965 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2966 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002967 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00002968 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
2969 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002970 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00002971 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
2972 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002973 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00002974 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
2975 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002976 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00002977 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002978 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002979
2980 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002981 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002982 TwoToFracPartOfX, IntegerPartOfX);
2983
Owen Anderson825b72b2009-08-11 20:47:22 +00002984 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002985 }
2986 } else {
2987 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002988 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002989 getValue(I.getOperand(1)).getValueType(),
2990 getValue(I.getOperand(1)));
2991 }
2992
Dale Johannesen59e577f2008-09-05 18:38:42 +00002993 setValue(&I, result);
2994}
2995
Bill Wendling39150252008-09-09 20:39:27 +00002996/// visitLog - Lower a log intrinsic. Handles the special sequences for
2997/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002998void
Dan Gohman2048b852009-11-23 18:04:58 +00002999SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003000 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003001 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003002
Owen Anderson825b72b2009-08-11 20:47:22 +00003003 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003004 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3005 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003006 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003007
3008 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003009 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003010 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003011 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003012
3013 // Get the significand and build it into a floating-point number with
3014 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003015 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003016
3017 if (LimitFloatPrecision <= 6) {
3018 // For floating-point precision of 6:
3019 //
3020 // LogofMantissa =
3021 // -1.1609546f +
3022 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003023 //
Bill Wendling39150252008-09-09 20:39:27 +00003024 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003025 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003026 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003027 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003028 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3030 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003031 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003032
Scott Michelfdc40a02009-02-17 22:15:04 +00003033 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003034 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003035 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3036 // For floating-point precision of 12:
3037 //
3038 // LogOfMantissa =
3039 // -1.7417939f +
3040 // (2.8212026f +
3041 // (-1.4699568f +
3042 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3043 //
3044 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003045 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003046 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003047 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003048 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003049 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3050 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003051 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003052 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3053 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003054 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003055 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3056 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003057 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003058
Scott Michelfdc40a02009-02-17 22:15:04 +00003059 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003060 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003061 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3062 // For floating-point precision of 18:
3063 //
3064 // LogOfMantissa =
3065 // -2.1072184f +
3066 // (4.2372794f +
3067 // (-3.7029485f +
3068 // (2.2781945f +
3069 // (-0.87823314f +
3070 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3071 //
3072 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003073 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003074 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003076 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003077 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3078 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003079 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003080 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3081 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003082 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003083 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3084 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003085 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003086 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3087 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003088 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3090 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003091 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003092
Scott Michelfdc40a02009-02-17 22:15:04 +00003093 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003094 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003095 }
3096 } else {
3097 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003098 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003099 getValue(I.getOperand(1)).getValueType(),
3100 getValue(I.getOperand(1)));
3101 }
3102
Dale Johannesen59e577f2008-09-05 18:38:42 +00003103 setValue(&I, result);
3104}
3105
Bill Wendling3eb59402008-09-09 00:28:24 +00003106/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3107/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003108void
Dan Gohman2048b852009-11-23 18:04:58 +00003109SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003110 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003111 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003112
Owen Anderson825b72b2009-08-11 20:47:22 +00003113 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003114 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3115 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003117
Bill Wendling39150252008-09-09 20:39:27 +00003118 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003119 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003120
3121 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003122 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003123 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003124
Bill Wendling3eb59402008-09-09 00:28:24 +00003125 // Different possible minimax approximations of significand in
3126 // floating-point for various degrees of accuracy over [1,2].
3127 if (LimitFloatPrecision <= 6) {
3128 // For floating-point precision of 6:
3129 //
3130 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3131 //
3132 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003133 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003134 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003136 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3138 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003139 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003140
Scott Michelfdc40a02009-02-17 22:15:04 +00003141 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003142 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003143 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3144 // For floating-point precision of 12:
3145 //
3146 // Log2ofMantissa =
3147 // -2.51285454f +
3148 // (4.07009056f +
3149 // (-2.12067489f +
3150 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003151 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003152 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003153 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003154 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003156 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003157 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3158 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003159 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003160 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3161 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003162 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003163 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3164 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003165 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003166
Scott Michelfdc40a02009-02-17 22:15:04 +00003167 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003168 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003169 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3170 // For floating-point precision of 18:
3171 //
3172 // Log2ofMantissa =
3173 // -3.0400495f +
3174 // (6.1129976f +
3175 // (-5.3420409f +
3176 // (3.2865683f +
3177 // (-1.2669343f +
3178 // (0.27515199f -
3179 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3180 //
3181 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003182 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003183 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003184 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003185 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003186 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3187 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003188 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003189 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3190 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003191 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3193 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003194 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3196 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003197 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3199 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003200 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003201
Scott Michelfdc40a02009-02-17 22:15:04 +00003202 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003203 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003204 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003205 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003206 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003207 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003208 getValue(I.getOperand(1)).getValueType(),
3209 getValue(I.getOperand(1)));
3210 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003211
Dale Johannesen59e577f2008-09-05 18:38:42 +00003212 setValue(&I, result);
3213}
3214
Bill Wendling3eb59402008-09-09 00:28:24 +00003215/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3216/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003217void
Dan Gohman2048b852009-11-23 18:04:58 +00003218SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003219 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003220 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003221
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003223 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3224 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003225 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003226
Bill Wendling39150252008-09-09 20:39:27 +00003227 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003228 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003230 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003231
3232 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003233 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003234 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003235
3236 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003237 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003238 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003239 // Log10ofMantissa =
3240 // -0.50419619f +
3241 // (0.60948995f - 0.10380950f * x) * x;
3242 //
3243 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003246 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003247 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3249 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003250 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003251
Scott Michelfdc40a02009-02-17 22:15:04 +00003252 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003253 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003254 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3255 // For floating-point precision of 12:
3256 //
3257 // Log10ofMantissa =
3258 // -0.64831180f +
3259 // (0.91751397f +
3260 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3261 //
3262 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003263 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003264 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003265 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003266 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003267 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3268 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003269 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003270 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3271 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003272 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003273
Scott Michelfdc40a02009-02-17 22:15:04 +00003274 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003276 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003277 // For floating-point precision of 18:
3278 //
3279 // Log10ofMantissa =
3280 // -0.84299375f +
3281 // (1.5327582f +
3282 // (-1.0688956f +
3283 // (0.49102474f +
3284 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3285 //
3286 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003287 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003288 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003290 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003291 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3292 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003293 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003294 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3295 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003296 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3298 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003299 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3301 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003302 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003303
Scott Michelfdc40a02009-02-17 22:15:04 +00003304 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003306 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003307 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003308 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003309 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003310 getValue(I.getOperand(1)).getValueType(),
3311 getValue(I.getOperand(1)));
3312 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003313
Dale Johannesen59e577f2008-09-05 18:38:42 +00003314 setValue(&I, result);
3315}
3316
Bill Wendlinge10c8142008-09-09 22:39:21 +00003317/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3318/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003319void
Dan Gohman2048b852009-11-23 18:04:58 +00003320SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003321 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003322 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003323
Owen Anderson825b72b2009-08-11 20:47:22 +00003324 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003325 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3326 SDValue Op = getValue(I.getOperand(1));
3327
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003329
3330 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3332 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003333
3334 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003336 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003337
3338 if (LimitFloatPrecision <= 6) {
3339 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003340 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003341 // TwoToFractionalPartOfX =
3342 // 0.997535578f +
3343 // (0.735607626f + 0.252464424f * x) * x;
3344 //
3345 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003347 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3351 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003352 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003354 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003356
Scott Michelfdc40a02009-02-17 22:15:04 +00003357 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003359 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3360 // For floating-point precision of 12:
3361 //
3362 // TwoToFractionalPartOfX =
3363 // 0.999892986f +
3364 // (0.696457318f +
3365 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3366 //
3367 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003368 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003369 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003371 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3373 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003374 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3376 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003377 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003378 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003379 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003380 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003381
Scott Michelfdc40a02009-02-17 22:15:04 +00003382 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003383 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003384 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3385 // For floating-point precision of 18:
3386 //
3387 // TwoToFractionalPartOfX =
3388 // 0.999999982f +
3389 // (0.693148872f +
3390 // (0.240227044f +
3391 // (0.554906021e-1f +
3392 // (0.961591928e-2f +
3393 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3394 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003395 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003396 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003398 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3400 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003401 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3403 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003404 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3406 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003407 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3409 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003410 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3412 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003413 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003415 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003416 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003417
Scott Michelfdc40a02009-02-17 22:15:04 +00003418 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003420 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003421 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003422 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003423 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003424 getValue(I.getOperand(1)).getValueType(),
3425 getValue(I.getOperand(1)));
3426 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003427
Dale Johannesen601d3c02008-09-05 01:48:15 +00003428 setValue(&I, result);
3429}
3430
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003431/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3432/// limited-precision mode with x == 10.0f.
3433void
Dan Gohman2048b852009-11-23 18:04:58 +00003434SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003435 SDValue result;
3436 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003437 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003438 bool IsExp10 = false;
3439
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 if (getValue(Val).getValueType() == MVT::f32 &&
3441 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003442 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3443 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3444 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3445 APFloat Ten(10.0f);
3446 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3447 }
3448 }
3449 }
3450
3451 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3452 SDValue Op = getValue(I.getOperand(2));
3453
3454 // Put the exponent in the right bit position for later addition to the
3455 // final result:
3456 //
3457 // #define LOG2OF10 3.3219281f
3458 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003460 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003461 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003462
3463 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003464 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3465 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003466
3467 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003468 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003469 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003470
3471 if (LimitFloatPrecision <= 6) {
3472 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003473 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003474 // twoToFractionalPartOfX =
3475 // 0.997535578f +
3476 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003477 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003478 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003479 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003480 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003481 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003482 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3484 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003485 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003487 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003488 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003489
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003490 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003492 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3493 // For floating-point precision of 12:
3494 //
3495 // TwoToFractionalPartOfX =
3496 // 0.999892986f +
3497 // (0.696457318f +
3498 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3499 //
3500 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003501 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003502 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003503 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003504 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003505 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3506 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003507 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3509 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003510 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003512 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003513 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003514
Scott Michelfdc40a02009-02-17 22:15:04 +00003515 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003516 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003517 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3518 // For floating-point precision of 18:
3519 //
3520 // TwoToFractionalPartOfX =
3521 // 0.999999982f +
3522 // (0.693148872f +
3523 // (0.240227044f +
3524 // (0.554906021e-1f +
3525 // (0.961591928e-2f +
3526 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3527 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003528 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003529 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003531 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3533 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003534 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3536 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003537 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3539 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003540 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3542 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003543 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3545 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003546 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003548 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003550
Scott Michelfdc40a02009-02-17 22:15:04 +00003551 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003553 }
3554 } else {
3555 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003556 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003557 getValue(I.getOperand(1)).getValueType(),
3558 getValue(I.getOperand(1)),
3559 getValue(I.getOperand(2)));
3560 }
3561
3562 setValue(&I, result);
3563}
3564
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003565/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3566/// we want to emit this as a call to a named external function, return the name
3567/// otherwise lower it and return null.
3568const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003569SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003570 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003571 switch (Intrinsic) {
3572 default:
3573 // By default, turn this into a target intrinsic node.
3574 visitTargetIntrinsic(I, Intrinsic);
3575 return 0;
3576 case Intrinsic::vastart: visitVAStart(I); return 0;
3577 case Intrinsic::vaend: visitVAEnd(I); return 0;
3578 case Intrinsic::vacopy: visitVACopy(I); return 0;
3579 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003580 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003581 getValue(I.getOperand(1))));
3582 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003583 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003584 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003585 getValue(I.getOperand(1))));
3586 return 0;
3587 case Intrinsic::setjmp:
3588 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3589 break;
3590 case Intrinsic::longjmp:
3591 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3592 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003593 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003594 SDValue Op1 = getValue(I.getOperand(1));
3595 SDValue Op2 = getValue(I.getOperand(2));
3596 SDValue Op3 = getValue(I.getOperand(3));
3597 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003598 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003599 I.getOperand(1), 0, I.getOperand(2), 0));
3600 return 0;
3601 }
Chris Lattner824b9582008-11-21 16:42:48 +00003602 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003603 SDValue Op1 = getValue(I.getOperand(1));
3604 SDValue Op2 = getValue(I.getOperand(2));
3605 SDValue Op3 = getValue(I.getOperand(3));
3606 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003607 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003608 I.getOperand(1), 0));
3609 return 0;
3610 }
Chris Lattner824b9582008-11-21 16:42:48 +00003611 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003612 SDValue Op1 = getValue(I.getOperand(1));
3613 SDValue Op2 = getValue(I.getOperand(2));
3614 SDValue Op3 = getValue(I.getOperand(3));
3615 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3616
3617 // If the source and destination are known to not be aliases, we can
3618 // lower memmove as memcpy.
3619 uint64_t Size = -1ULL;
3620 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003621 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003622 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3623 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00003624 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003625 I.getOperand(1), 0, I.getOperand(2), 0));
3626 return 0;
3627 }
3628
Dale Johannesena04b7572009-02-03 23:04:43 +00003629 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003630 I.getOperand(1), 0, I.getOperand(2), 0));
3631 return 0;
3632 }
Devang Patel70d75ca2009-11-12 19:02:56 +00003633 case Intrinsic::dbg_stoppoint:
3634 case Intrinsic::dbg_region_start:
3635 case Intrinsic::dbg_region_end:
3636 case Intrinsic::dbg_func_start:
3637 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003638 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003639 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00003640 if (OptLevel != CodeGenOpt::None)
3641 // FIXME: Variable debug info is not supported here.
3642 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00003643 DwarfWriter *DW = DAG.getDwarfWriter();
3644 if (!DW)
3645 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003646 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3647 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
3648 return 0;
3649
Devang Patelac1ceb32009-10-09 22:42:28 +00003650 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00003651 Value *Address = DI.getAddress();
3652 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
3653 Address = BCI->getOperand(0);
3654 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
3655 // Don't handle byval struct arguments or VLAs, for example.
3656 if (!AI)
3657 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003658 DenseMap<const AllocaInst*, int>::iterator SI =
3659 FuncInfo.StaticAllocaMap.find(AI);
3660 if (SI == FuncInfo.StaticAllocaMap.end())
3661 return 0; // VLAs.
3662 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003663
Devang Patelac1ceb32009-10-09 22:42:28 +00003664 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00003665 if (MMI) {
3666 MetadataContext &TheMetadata =
3667 DI.getParent()->getContext().getMetadata();
3668 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
3669 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
3670 MMI->setVariableDbgInfo(Variable, FI, Dbg);
3671 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003672 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003673 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003674 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003675 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00003676 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003677 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003678 SDValue Ops[1];
3679 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003680 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003681 setValue(&I, Op);
3682 DAG.setRoot(Op.getValue(1));
3683 return 0;
3684 }
3685
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003686 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003687 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003688
Chris Lattner3a5815f2009-09-17 23:54:54 +00003689 if (CurMBB->isLandingPad())
3690 AddCatchInfo(I, MMI, CurMBB);
3691 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003692#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003693 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003694#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003695 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3696 unsigned Reg = TLI.getExceptionSelectorRegister();
3697 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003698 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003699
Chris Lattner3a5815f2009-09-17 23:54:54 +00003700 // Insert the EHSELECTION instruction.
3701 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3702 SDValue Ops[2];
3703 Ops[0] = getValue(I.getOperand(1));
3704 Ops[1] = getRoot();
3705 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
3706
3707 DAG.setRoot(Op.getValue(1));
3708
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003709 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003710 return 0;
3711 }
3712
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003713 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003714 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003715
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003716 if (MMI) {
3717 // Find the type id for the given typeinfo.
3718 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3719
3720 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003721 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003722 } else {
3723 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003724 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003725 }
3726
3727 return 0;
3728 }
3729
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003730 case Intrinsic::eh_return_i32:
3731 case Intrinsic::eh_return_i64:
3732 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003733 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003734 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003735 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003736 getControlRoot(),
3737 getValue(I.getOperand(1)),
3738 getValue(I.getOperand(2))));
3739 } else {
3740 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
3741 }
3742
3743 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003744 case Intrinsic::eh_unwind_init:
3745 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3746 MMI->setCallsUnwindInit(true);
3747 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003748
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003749 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003750
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003751 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00003752 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00003753 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
3754 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003755
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003756 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003757 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003758 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003759 TLI.getPointerTy()),
3760 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003761 setValue(&I, DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003762 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003763 DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003764 TLI.getPointerTy(),
3765 DAG.getConstant(0,
3766 TLI.getPointerTy())),
3767 Offset));
3768 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003769 }
Mon P Wang77cdf302008-11-10 20:54:11 +00003770 case Intrinsic::convertff:
3771 case Intrinsic::convertfsi:
3772 case Intrinsic::convertfui:
3773 case Intrinsic::convertsif:
3774 case Intrinsic::convertuif:
3775 case Intrinsic::convertss:
3776 case Intrinsic::convertsu:
3777 case Intrinsic::convertus:
3778 case Intrinsic::convertuu: {
3779 ISD::CvtCode Code = ISD::CVT_INVALID;
3780 switch (Intrinsic) {
3781 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3782 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3783 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3784 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3785 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3786 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3787 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3788 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3789 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3790 }
Owen Andersone50ed302009-08-10 22:56:29 +00003791 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00003792 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00003793 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00003794 DAG.getValueType(DestVT),
3795 DAG.getValueType(getValue(Op1).getValueType()),
3796 getValue(I.getOperand(2)),
3797 getValue(I.getOperand(3)),
3798 Code));
3799 return 0;
3800 }
3801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003802 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003803 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003804 getValue(I.getOperand(1)).getValueType(),
3805 getValue(I.getOperand(1))));
3806 return 0;
3807 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003808 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003809 getValue(I.getOperand(1)).getValueType(),
3810 getValue(I.getOperand(1)),
3811 getValue(I.getOperand(2))));
3812 return 0;
3813 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003814 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003815 getValue(I.getOperand(1)).getValueType(),
3816 getValue(I.getOperand(1))));
3817 return 0;
3818 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003819 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003820 getValue(I.getOperand(1)).getValueType(),
3821 getValue(I.getOperand(1))));
3822 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003823 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003824 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003825 return 0;
3826 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003827 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003828 return 0;
3829 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003830 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003831 return 0;
3832 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003833 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003834 return 0;
3835 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003836 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003837 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003838 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003839 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003840 return 0;
3841 case Intrinsic::pcmarker: {
3842 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003843 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003844 return 0;
3845 }
3846 case Intrinsic::readcyclecounter: {
3847 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003848 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003849 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00003850 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003851 setValue(&I, Tmp);
3852 DAG.setRoot(Tmp.getValue(1));
3853 return 0;
3854 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003855 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003856 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003857 getValue(I.getOperand(1)).getValueType(),
3858 getValue(I.getOperand(1))));
3859 return 0;
3860 case Intrinsic::cttz: {
3861 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003862 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003863 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003864 setValue(&I, result);
3865 return 0;
3866 }
3867 case Intrinsic::ctlz: {
3868 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003869 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003870 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003871 setValue(&I, result);
3872 return 0;
3873 }
3874 case Intrinsic::ctpop: {
3875 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003876 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003877 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003878 setValue(&I, result);
3879 return 0;
3880 }
3881 case Intrinsic::stacksave: {
3882 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003883 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003884 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003885 setValue(&I, Tmp);
3886 DAG.setRoot(Tmp.getValue(1));
3887 return 0;
3888 }
3889 case Intrinsic::stackrestore: {
3890 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003891 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003892 return 0;
3893 }
Bill Wendling57344502008-11-18 11:01:33 +00003894 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00003895 // Emit code into the DAG to store the stack guard onto the stack.
3896 MachineFunction &MF = DAG.getMachineFunction();
3897 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003898 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00003899
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00003900 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
3901 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00003902
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00003903 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00003904 MFI->setStackProtectorIndex(FI);
3905
3906 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3907
3908 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003909 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00003910 PseudoSourceValue::getFixedStack(FI),
3911 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00003912 setValue(&I, Result);
3913 DAG.setRoot(Result);
3914 return 0;
3915 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00003916 case Intrinsic::objectsize: {
3917 // If we don't know by now, we're never going to know.
3918 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
3919
3920 assert(CI && "Non-constant type in __builtin_object_size?");
3921
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00003922 SDValue Arg = getValue(I.getOperand(0));
3923 EVT Ty = Arg.getValueType();
3924
Eric Christopher7b5e6172009-10-27 00:52:25 +00003925 if (CI->getZExtValue() < 2)
Mike Stump70e5e682009-11-09 22:28:21 +00003926 setValue(&I, DAG.getConstant(-1ULL, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00003927 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00003928 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00003929 return 0;
3930 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003931 case Intrinsic::var_annotation:
3932 // Discard annotate attributes
3933 return 0;
3934
3935 case Intrinsic::init_trampoline: {
3936 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
3937
3938 SDValue Ops[6];
3939 Ops[0] = getRoot();
3940 Ops[1] = getValue(I.getOperand(1));
3941 Ops[2] = getValue(I.getOperand(2));
3942 Ops[3] = getValue(I.getOperand(3));
3943 Ops[4] = DAG.getSrcValue(I.getOperand(1));
3944 Ops[5] = DAG.getSrcValue(F);
3945
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003946 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003947 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00003948 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003949
3950 setValue(&I, Tmp);
3951 DAG.setRoot(Tmp.getValue(1));
3952 return 0;
3953 }
3954
3955 case Intrinsic::gcroot:
3956 if (GFI) {
3957 Value *Alloca = I.getOperand(1);
3958 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003959
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003960 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
3961 GFI->addStackRoot(FI->getIndex(), TypeMap);
3962 }
3963 return 0;
3964
3965 case Intrinsic::gcread:
3966 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00003967 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003968 return 0;
3969
3970 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00003971 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003972 return 0;
3973 }
3974
3975 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00003976 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003977 return 0;
3978 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00003979
Bill Wendlingef375462008-11-21 02:38:44 +00003980 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00003981 return implVisitAluOverflow(I, ISD::UADDO);
3982 case Intrinsic::sadd_with_overflow:
3983 return implVisitAluOverflow(I, ISD::SADDO);
3984 case Intrinsic::usub_with_overflow:
3985 return implVisitAluOverflow(I, ISD::USUBO);
3986 case Intrinsic::ssub_with_overflow:
3987 return implVisitAluOverflow(I, ISD::SSUBO);
3988 case Intrinsic::umul_with_overflow:
3989 return implVisitAluOverflow(I, ISD::UMULO);
3990 case Intrinsic::smul_with_overflow:
3991 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00003992
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003993 case Intrinsic::prefetch: {
3994 SDValue Ops[4];
3995 Ops[0] = getRoot();
3996 Ops[1] = getValue(I.getOperand(1));
3997 Ops[2] = getValue(I.getOperand(2));
3998 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003999 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004000 return 0;
4001 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004003 case Intrinsic::memory_barrier: {
4004 SDValue Ops[6];
4005 Ops[0] = getRoot();
4006 for (int x = 1; x < 6; ++x)
4007 Ops[x] = getValue(I.getOperand(x));
4008
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004010 return 0;
4011 }
4012 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004013 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004014 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004015 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004016 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4017 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004018 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004019 getValue(I.getOperand(2)),
4020 getValue(I.getOperand(3)),
4021 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004022 setValue(&I, L);
4023 DAG.setRoot(L.getValue(1));
4024 return 0;
4025 }
4026 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004027 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004028 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004029 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004030 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004031 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004032 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004033 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004034 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004035 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004036 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004037 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004038 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004039 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004040 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004041 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004042 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004043 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004044 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004045 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004046 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004047 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004048
4049 case Intrinsic::invariant_start:
4050 case Intrinsic::lifetime_start:
4051 // Discard region information.
4052 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
4053 return 0;
4054 case Intrinsic::invariant_end:
4055 case Intrinsic::lifetime_end:
4056 // Discard region information.
4057 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004058 }
4059}
4060
Dan Gohman98ca4f22009-08-05 01:29:28 +00004061/// Test if the given instruction is in a position to be optimized
4062/// with a tail-call. This roughly means that it's in a block with
4063/// a return and there's nothing that needs to be scheduled
4064/// between it and the return.
4065///
4066/// This function only tests target-independent requirements.
4067/// For target-dependent requirements, a target should override
4068/// TargetLowering::IsEligibleForTailCallOptimization.
4069///
4070static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004071isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004072 const TargetLowering &TLI) {
4073 const BasicBlock *ExitBB = I->getParent();
4074 const TerminatorInst *Term = ExitBB->getTerminator();
4075 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4076 const Function *F = ExitBB->getParent();
4077
4078 // The block must end in a return statement or an unreachable.
4079 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4080
4081 // If I will have a chain, make sure no other instruction that will have a
4082 // chain interposes between I and the return.
4083 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4084 !I->isSafeToSpeculativelyExecute())
4085 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4086 --BBI) {
4087 if (&*BBI == I)
4088 break;
4089 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4090 !BBI->isSafeToSpeculativelyExecute())
4091 return false;
4092 }
4093
4094 // If the block ends with a void return or unreachable, it doesn't matter
4095 // what the call's return type is.
4096 if (!Ret || Ret->getNumOperands() == 0) return true;
4097
Dan Gohmaned9bab32009-11-14 02:06:30 +00004098 // If the return value is undef, it doesn't matter what the call's
4099 // return type is.
4100 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4101
Dan Gohman98ca4f22009-08-05 01:29:28 +00004102 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004103 // the return. Ignore noalias because it doesn't affect the call sequence.
4104 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4105 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004106 return false;
4107
4108 // Otherwise, make sure the unmodified return value of I is the return value.
4109 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4110 U = dyn_cast<Instruction>(U->getOperand(0))) {
4111 if (!U)
4112 return false;
4113 if (!U->hasOneUse())
4114 return false;
4115 if (U == I)
4116 break;
4117 // Check for a truly no-op truncate.
4118 if (isa<TruncInst>(U) &&
4119 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4120 continue;
4121 // Check for a truly no-op bitcast.
4122 if (isa<BitCastInst>(U) &&
4123 (U->getOperand(0)->getType() == U->getType() ||
4124 (isa<PointerType>(U->getOperand(0)->getType()) &&
4125 isa<PointerType>(U->getType()))))
4126 continue;
4127 // Otherwise it's not a true no-op.
4128 return false;
4129 }
4130
4131 return true;
4132}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004133
Dan Gohman2048b852009-11-23 18:04:58 +00004134void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4135 bool isTailCall,
4136 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004137 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4138 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004139 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004140 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4141 unsigned BeginLabel = 0, EndLabel = 0;
4142
4143 TargetLowering::ArgListTy Args;
4144 TargetLowering::ArgListEntry Entry;
4145 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004146
4147 // Check whether the function can return without sret-demotion.
4148 SmallVector<EVT, 4> OutVTs;
4149 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4150 SmallVector<uint64_t, 4> Offsets;
4151 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4152 OutVTs, OutsFlags, TLI, &Offsets);
4153
4154
4155 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4156 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4157
4158 SDValue DemoteStackSlot;
4159
4160 if (!CanLowerReturn) {
4161 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4162 FTy->getReturnType());
4163 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4164 FTy->getReturnType());
4165 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004166 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004167 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4168
4169 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4170 Entry.Node = DemoteStackSlot;
4171 Entry.Ty = StackSlotPtrType;
4172 Entry.isSExt = false;
4173 Entry.isZExt = false;
4174 Entry.isInReg = false;
4175 Entry.isSRet = true;
4176 Entry.isNest = false;
4177 Entry.isByVal = false;
4178 Entry.Alignment = Align;
4179 Args.push_back(Entry);
4180 RetTy = Type::getVoidTy(FTy->getContext());
4181 }
4182
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004183 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004184 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004185 SDValue ArgNode = getValue(*i);
4186 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4187
4188 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004189 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4190 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4191 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4192 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4193 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4194 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004195 Entry.Alignment = CS.getParamAlignment(attrInd);
4196 Args.push_back(Entry);
4197 }
4198
4199 if (LandingPad && MMI) {
4200 // Insert a label before the invoke call to mark the try range. This can be
4201 // used to detect deletion of the invoke via the MachineModuleInfo.
4202 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004203
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004204 // Both PendingLoads and PendingExports must be flushed here;
4205 // this call might not return.
4206 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004207 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4208 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004209 }
4210
Dan Gohman98ca4f22009-08-05 01:29:28 +00004211 // Check if target-independent constraints permit a tail call here.
4212 // Target-dependent constraints are checked within TLI.LowerCallTo.
4213 if (isTailCall &&
4214 !isInTailCallPosition(CS.getInstruction(),
4215 CS.getAttributes().getRetAttributes(),
4216 TLI))
4217 isTailCall = false;
4218
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004219 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004220 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004221 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004222 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004223 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004224 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004225 isTailCall,
4226 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004227 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004228 assert((isTailCall || Result.second.getNode()) &&
4229 "Non-null chain expected with non-tail call!");
4230 assert((Result.second.getNode() || !Result.first.getNode()) &&
4231 "Null value expected with tail call!");
4232 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004233 setValue(CS.getInstruction(), Result.first);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004234 else if (!CanLowerReturn && Result.second.getNode()) {
4235 // The instruction result is the result of loading from the
4236 // hidden sret parameter.
4237 SmallVector<EVT, 1> PVTs;
4238 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4239
4240 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4241 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4242 EVT PtrVT = PVTs[0];
4243 unsigned NumValues = OutVTs.size();
4244 SmallVector<SDValue, 4> Values(NumValues);
4245 SmallVector<SDValue, 4> Chains(NumValues);
4246
4247 for (unsigned i = 0; i < NumValues; ++i) {
4248 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
4249 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, DemoteStackSlot,
4250 DAG.getConstant(Offsets[i], PtrVT)),
4251 NULL, Offsets[i], false, 1);
4252 Values[i] = L;
4253 Chains[i] = L.getValue(1);
4254 }
4255 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4256 MVT::Other, &Chains[0], NumValues);
4257 PendingLoads.push_back(Chain);
4258
4259 setValue(CS.getInstruction(), DAG.getNode(ISD::MERGE_VALUES,
4260 getCurDebugLoc(), DAG.getVTList(&OutVTs[0], NumValues),
4261 &Values[0], NumValues));
4262 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00004263 // As a special case, a null chain means that a tail call has
4264 // been emitted and the DAG root is already updated.
4265 if (Result.second.getNode())
4266 DAG.setRoot(Result.second);
4267 else
4268 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004269
4270 if (LandingPad && MMI) {
4271 // Insert a label at the end of the invoke call to mark the try range. This
4272 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4273 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004274 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4275 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004276
4277 // Inform MachineModuleInfo of range.
4278 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4279 }
4280}
4281
4282
Dan Gohman2048b852009-11-23 18:04:58 +00004283void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004284 const char *RenameFn = 0;
4285 if (Function *F = I.getCalledFunction()) {
4286 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004287 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4288 if (II) {
4289 if (unsigned IID = II->getIntrinsicID(F)) {
4290 RenameFn = visitIntrinsicCall(I, IID);
4291 if (!RenameFn)
4292 return;
4293 }
4294 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004295 if (unsigned IID = F->getIntrinsicID()) {
4296 RenameFn = visitIntrinsicCall(I, IID);
4297 if (!RenameFn)
4298 return;
4299 }
4300 }
4301
4302 // Check for well-known libc/libm calls. If the function is internal, it
4303 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004304 if (!F->hasLocalLinkage() && F->hasName()) {
4305 StringRef Name = F->getName();
4306 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004307 if (I.getNumOperands() == 3 && // Basic sanity checks.
4308 I.getOperand(1)->getType()->isFloatingPoint() &&
4309 I.getType() == I.getOperand(1)->getType() &&
4310 I.getType() == I.getOperand(2)->getType()) {
4311 SDValue LHS = getValue(I.getOperand(1));
4312 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004313 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004314 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004315 return;
4316 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004317 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004318 if (I.getNumOperands() == 2 && // Basic sanity checks.
4319 I.getOperand(1)->getType()->isFloatingPoint() &&
4320 I.getType() == I.getOperand(1)->getType()) {
4321 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004322 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004323 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004324 return;
4325 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004326 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004327 if (I.getNumOperands() == 2 && // Basic sanity checks.
4328 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004329 I.getType() == I.getOperand(1)->getType() &&
4330 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004331 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004332 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004333 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004334 return;
4335 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004336 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004337 if (I.getNumOperands() == 2 && // Basic sanity checks.
4338 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004339 I.getType() == I.getOperand(1)->getType() &&
4340 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004341 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004342 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004343 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004344 return;
4345 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004346 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4347 if (I.getNumOperands() == 2 && // Basic sanity checks.
4348 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004349 I.getType() == I.getOperand(1)->getType() &&
4350 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004351 SDValue Tmp = getValue(I.getOperand(1));
4352 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4353 Tmp.getValueType(), Tmp));
4354 return;
4355 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004356 }
4357 }
4358 } else if (isa<InlineAsm>(I.getOperand(0))) {
4359 visitInlineAsm(&I);
4360 return;
4361 }
4362
4363 SDValue Callee;
4364 if (!RenameFn)
4365 Callee = getValue(I.getOperand(0));
4366 else
Bill Wendling056292f2008-09-16 21:48:12 +00004367 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004368
Dan Gohman98ca4f22009-08-05 01:29:28 +00004369 // Check if we can potentially perform a tail call. More detailed
4370 // checking is be done within LowerCallTo, after more information
4371 // about the call is known.
4372 bool isTailCall = PerformTailCallOpt && I.isTailCall();
4373
4374 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004375}
4376
4377
4378/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004379/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004380/// Chain/Flag as the input and updates them for the output Chain/Flag.
4381/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004382SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004383 SDValue &Chain,
4384 SDValue *Flag) const {
4385 // Assemble the legal parts into the final values.
4386 SmallVector<SDValue, 4> Values(ValueVTs.size());
4387 SmallVector<SDValue, 8> Parts;
4388 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4389 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004390 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004391 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004392 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004393
4394 Parts.resize(NumRegs);
4395 for (unsigned i = 0; i != NumRegs; ++i) {
4396 SDValue P;
4397 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004398 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004399 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004400 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004401 *Flag = P.getValue(2);
4402 }
4403 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004404
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004405 // If the source register was virtual and if we know something about it,
4406 // add an assert node.
4407 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4408 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4409 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4410 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4411 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4412 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004413
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004414 unsigned RegSize = RegisterVT.getSizeInBits();
4415 unsigned NumSignBits = LOI.NumSignBits;
4416 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004417
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004418 // FIXME: We capture more information than the dag can represent. For
4419 // now, just use the tightest assertzext/assertsext possible.
4420 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004421 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004422 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004423 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004424 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004425 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004426 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004427 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004428 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004429 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004430 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004431 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004432 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004433 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004434 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004435 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004436 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004437 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004438
Owen Anderson825b72b2009-08-11 20:47:22 +00004439 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004440 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004441 RegisterVT, P, DAG.getValueType(FromVT));
4442
4443 }
4444 }
4445 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004446
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004447 Parts[i] = P;
4448 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004449
Scott Michelfdc40a02009-02-17 22:15:04 +00004450 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004451 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004452 Part += NumRegs;
4453 Parts.clear();
4454 }
4455
Dale Johannesen66978ee2009-01-31 02:22:37 +00004456 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004457 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4458 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004459}
4460
4461/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004462/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004463/// Chain/Flag as the input and updates them for the output Chain/Flag.
4464/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004465void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004466 SDValue &Chain, SDValue *Flag) const {
4467 // Get the list of the values's legal parts.
4468 unsigned NumRegs = Regs.size();
4469 SmallVector<SDValue, 8> Parts(NumRegs);
4470 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004471 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004472 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004473 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004474
Dale Johannesen66978ee2009-01-31 02:22:37 +00004475 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004476 &Parts[Part], NumParts, RegisterVT);
4477 Part += NumParts;
4478 }
4479
4480 // Copy the parts into the registers.
4481 SmallVector<SDValue, 8> Chains(NumRegs);
4482 for (unsigned i = 0; i != NumRegs; ++i) {
4483 SDValue Part;
4484 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004485 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004486 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004487 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004488 *Flag = Part.getValue(1);
4489 }
4490 Chains[i] = Part.getValue(0);
4491 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004492
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004493 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004494 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004495 // flagged to it. That is the CopyToReg nodes and the user are considered
4496 // a single scheduling unit. If we create a TokenFactor and return it as
4497 // chain, then the TokenFactor is both a predecessor (operand) of the
4498 // user as well as a successor (the TF operands are flagged to the user).
4499 // c1, f1 = CopyToReg
4500 // c2, f2 = CopyToReg
4501 // c3 = TokenFactor c1, c2
4502 // ...
4503 // = op c3, ..., f2
4504 Chain = Chains[NumRegs-1];
4505 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004506 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004507}
4508
4509/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004510/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004511/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00004512void RegsForValue::AddInlineAsmOperands(unsigned Code,
4513 bool HasMatching,unsigned MatchingIdx,
4514 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004515 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004516 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00004517 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
4518 unsigned Flag = Code | (Regs.size() << 3);
4519 if (HasMatching)
4520 Flag |= 0x80000000 | (MatchingIdx << 16);
4521 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004522 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004523 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004524 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004525 for (unsigned i = 0; i != NumRegs; ++i) {
4526 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004527 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004528 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004529 }
4530}
4531
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004532/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004533/// i.e. it isn't a stack pointer or some other special register, return the
4534/// register class for the register. Otherwise, return null.
4535static const TargetRegisterClass *
4536isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4537 const TargetLowering &TLI,
4538 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004539 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004540 const TargetRegisterClass *FoundRC = 0;
4541 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4542 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004543 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004544
4545 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004546 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004547 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4548 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4549 I != E; ++I) {
4550 if (TLI.isTypeLegal(*I)) {
4551 // If we have already found this register in a different register class,
4552 // choose the one with the largest VT specified. For example, on
4553 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004554 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004555 ThisVT = *I;
4556 break;
4557 }
4558 }
4559 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004560
Owen Anderson825b72b2009-08-11 20:47:22 +00004561 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004562
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004563 // NOTE: This isn't ideal. In particular, this might allocate the
4564 // frame pointer in functions that need it (due to them not being taken
4565 // out of allocation, because a variable sized allocation hasn't been seen
4566 // yet). This is a slight code pessimization, but should still work.
4567 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4568 E = RC->allocation_order_end(MF); I != E; ++I)
4569 if (*I == Reg) {
4570 // We found a matching register class. Keep looking at others in case
4571 // we find one with larger registers that this physreg is also in.
4572 FoundRC = RC;
4573 FoundVT = ThisVT;
4574 break;
4575 }
4576 }
4577 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004578}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004579
4580
4581namespace llvm {
4582/// AsmOperandInfo - This contains information for each constraint that we are
4583/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004584class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004585 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004586public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004587 /// CallOperand - If this is the result output operand or a clobber
4588 /// this is null, otherwise it is the incoming operand to the CallInst.
4589 /// This gets modified as the asm is processed.
4590 SDValue CallOperand;
4591
4592 /// AssignedRegs - If this is a register or register class operand, this
4593 /// contains the set of register corresponding to the operand.
4594 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004595
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4597 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4598 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004599
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004600 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4601 /// busy in OutputRegs/InputRegs.
4602 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004603 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004604 std::set<unsigned> &InputRegs,
4605 const TargetRegisterInfo &TRI) const {
4606 if (isOutReg) {
4607 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4608 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4609 }
4610 if (isInReg) {
4611 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4612 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4613 }
4614 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004615
Owen Andersone50ed302009-08-10 22:56:29 +00004616 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004617 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004618 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00004619 EVT getCallOperandValEVT(LLVMContext &Context,
4620 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004621 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004622 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004623
Chris Lattner81249c92008-10-17 17:05:25 +00004624 if (isa<BasicBlock>(CallOperandVal))
4625 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004626
Chris Lattner81249c92008-10-17 17:05:25 +00004627 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004628
Chris Lattner81249c92008-10-17 17:05:25 +00004629 // If this is an indirect operand, the operand is a pointer to the
4630 // accessed type.
4631 if (isIndirect)
4632 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004633
Chris Lattner81249c92008-10-17 17:05:25 +00004634 // If OpTy is not a single value, it may be a struct/union that we
4635 // can tile with integers.
4636 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4637 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4638 switch (BitSize) {
4639 default: break;
4640 case 1:
4641 case 8:
4642 case 16:
4643 case 32:
4644 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004645 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004646 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004647 break;
4648 }
4649 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004650
Chris Lattner81249c92008-10-17 17:05:25 +00004651 return TLI.getValueType(OpTy, true);
4652 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004653
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004654private:
4655 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4656 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004657 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004658 const TargetRegisterInfo &TRI) {
4659 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4660 Regs.insert(Reg);
4661 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4662 for (; *Aliases; ++Aliases)
4663 Regs.insert(*Aliases);
4664 }
4665};
4666} // end llvm namespace.
4667
4668
4669/// GetRegistersForValue - Assign registers (virtual or physical) for the
4670/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004671/// register allocator to handle the assignment process. However, if the asm
4672/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004673/// allocation. This produces generally horrible, but correct, code.
4674///
4675/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676/// Input and OutputRegs are the set of already allocated physical registers.
4677///
Dan Gohman2048b852009-11-23 18:04:58 +00004678void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004679GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004680 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004681 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004682 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004683
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004684 // Compute whether this value requires an input register, an output register,
4685 // or both.
4686 bool isOutReg = false;
4687 bool isInReg = false;
4688 switch (OpInfo.Type) {
4689 case InlineAsm::isOutput:
4690 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004691
4692 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004693 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004694 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004695 break;
4696 case InlineAsm::isInput:
4697 isInReg = true;
4698 isOutReg = false;
4699 break;
4700 case InlineAsm::isClobber:
4701 isOutReg = true;
4702 isInReg = true;
4703 break;
4704 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004705
4706
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004707 MachineFunction &MF = DAG.getMachineFunction();
4708 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004709
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004710 // If this is a constraint for a single physreg, or a constraint for a
4711 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004712 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004713 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4714 OpInfo.ConstraintVT);
4715
4716 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004717 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004718 // If this is a FP input in an integer register (or visa versa) insert a bit
4719 // cast of the input value. More generally, handle any case where the input
4720 // value disagrees with the register class we plan to stick this in.
4721 if (OpInfo.Type == InlineAsm::isInput &&
4722 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004723 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004724 // types are identical size, use a bitcast to convert (e.g. two differing
4725 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004726 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004727 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004728 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004729 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004730 OpInfo.ConstraintVT = RegVT;
4731 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4732 // If the input is a FP value and we want it in FP registers, do a
4733 // bitcast to the corresponding integer type. This turns an f64 value
4734 // into i64, which can be passed with two i32 values on a 32-bit
4735 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00004736 RegVT = EVT::getIntegerVT(Context,
4737 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004738 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004739 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004740 OpInfo.ConstraintVT = RegVT;
4741 }
4742 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004743
Owen Anderson23b9b192009-08-12 00:36:31 +00004744 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004745 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004746
Owen Andersone50ed302009-08-10 22:56:29 +00004747 EVT RegVT;
4748 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004749
4750 // If this is a constraint for a specific physical register, like {r17},
4751 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004752 if (unsigned AssignedReg = PhysReg.first) {
4753 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00004754 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004755 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004756
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757 // Get the actual register value type. This is important, because the user
4758 // may have asked for (e.g.) the AX register in i32 type. We need to
4759 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004760 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004761
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004762 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004763 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004764
4765 // If this is an expanded reference, add the rest of the regs to Regs.
4766 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004767 TargetRegisterClass::iterator I = RC->begin();
4768 for (; *I != AssignedReg; ++I)
4769 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004770
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004771 // Already added the first reg.
4772 --NumRegs; ++I;
4773 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004774 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004775 Regs.push_back(*I);
4776 }
4777 }
4778 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4779 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4780 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4781 return;
4782 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004783
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004784 // Otherwise, if this was a reference to an LLVM register class, create vregs
4785 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00004786 if (const TargetRegisterClass *RC = PhysReg.second) {
4787 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00004788 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00004789 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790
Evan Chengfb112882009-03-23 08:01:15 +00004791 // Create the appropriate number of virtual registers.
4792 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4793 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00004794 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004795
Evan Chengfb112882009-03-23 08:01:15 +00004796 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4797 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004798 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00004799
4800 // This is a reference to a register class that doesn't directly correspond
4801 // to an LLVM register class. Allocate NumRegs consecutive, available,
4802 // registers from the class.
4803 std::vector<unsigned> RegClassRegs
4804 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
4805 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004806
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004807 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4808 unsigned NumAllocated = 0;
4809 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
4810 unsigned Reg = RegClassRegs[i];
4811 // See if this register is available.
4812 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
4813 (isInReg && InputRegs.count(Reg))) { // Already used.
4814 // Make sure we find consecutive registers.
4815 NumAllocated = 0;
4816 continue;
4817 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004818
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004819 // Check to see if this register is allocatable (i.e. don't give out the
4820 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00004821 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
4822 if (!RC) { // Couldn't allocate this register.
4823 // Reset NumAllocated to make sure we return consecutive registers.
4824 NumAllocated = 0;
4825 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004826 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 // Okay, this register is good, we can use it.
4829 ++NumAllocated;
4830
4831 // If we allocated enough consecutive registers, succeed.
4832 if (NumAllocated == NumRegs) {
4833 unsigned RegStart = (i-NumAllocated)+1;
4834 unsigned RegEnd = i+1;
4835 // Mark all of the allocated registers used.
4836 for (unsigned i = RegStart; i != RegEnd; ++i)
4837 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004838
4839 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004840 OpInfo.ConstraintVT);
4841 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4842 return;
4843 }
4844 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004845
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004846 // Otherwise, we couldn't allocate enough registers for this.
4847}
4848
Evan Chengda43bcf2008-09-24 00:05:32 +00004849/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
4850/// processed uses a memory 'm' constraint.
4851static bool
4852hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00004853 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00004854 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
4855 InlineAsm::ConstraintInfo &CI = CInfos[i];
4856 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
4857 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
4858 if (CType == TargetLowering::C_Memory)
4859 return true;
4860 }
Chris Lattner6c147292009-04-30 00:48:50 +00004861
4862 // Indirect operand accesses access memory.
4863 if (CI.isIndirect)
4864 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00004865 }
4866
4867 return false;
4868}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004869
4870/// visitInlineAsm - Handle a call to an InlineAsm object.
4871///
Dan Gohman2048b852009-11-23 18:04:58 +00004872void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004873 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
4874
4875 /// ConstraintOperands - Information about all of the constraints.
4876 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004877
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004878 std::set<unsigned> OutputRegs, InputRegs;
4879
4880 // Do a prepass over the constraints, canonicalizing them, and building up the
4881 // ConstraintOperands list.
4882 std::vector<InlineAsm::ConstraintInfo>
4883 ConstraintInfos = IA->ParseConstraints();
4884
Evan Chengda43bcf2008-09-24 00:05:32 +00004885 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00004886
4887 SDValue Chain, Flag;
4888
4889 // We won't need to flush pending loads if this asm doesn't touch
4890 // memory and is nonvolatile.
4891 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00004892 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00004893 else
4894 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004895
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004896 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
4897 unsigned ResNo = 0; // ResNo - The result number of the next output.
4898 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4899 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
4900 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004901
Owen Anderson825b72b2009-08-11 20:47:22 +00004902 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004903
4904 // Compute the value type for each operand.
4905 switch (OpInfo.Type) {
4906 case InlineAsm::isOutput:
4907 // Indirect outputs just consume an argument.
4908 if (OpInfo.isIndirect) {
4909 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4910 break;
4911 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004912
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004913 // The return value of the call is this value. As such, there is no
4914 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00004915 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
4916 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004917 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
4918 OpVT = TLI.getValueType(STy->getElementType(ResNo));
4919 } else {
4920 assert(ResNo == 0 && "Asm only has one result!");
4921 OpVT = TLI.getValueType(CS.getType());
4922 }
4923 ++ResNo;
4924 break;
4925 case InlineAsm::isInput:
4926 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4927 break;
4928 case InlineAsm::isClobber:
4929 // Nothing to do.
4930 break;
4931 }
4932
4933 // If this is an input or an indirect output, process the call argument.
4934 // BasicBlocks are labels, currently appearing only in asm's.
4935 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00004936 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00004937 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
4938
Chris Lattner81249c92008-10-17 17:05:25 +00004939 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004940 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00004941 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004942 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004943 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004944
Owen Anderson1d0be152009-08-13 21:58:54 +00004945 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004946 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004947
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004948 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004949 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004950
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004951 // Second pass over the constraints: compute which constraint option to use
4952 // and assign registers to constraints that want a specific physreg.
4953 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4954 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004955
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004956 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00004957 // matching input. If their types mismatch, e.g. one is an integer, the
4958 // other is floating point, or their sizes are different, flag it as an
4959 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004960 if (OpInfo.hasMatchingInput()) {
4961 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
4962 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00004963 if ((OpInfo.ConstraintVT.isInteger() !=
4964 Input.ConstraintVT.isInteger()) ||
4965 (OpInfo.ConstraintVT.getSizeInBits() !=
4966 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00004967 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00004968 " with a matching output constraint of incompatible"
4969 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00004970 }
4971 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004972 }
4973 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004974
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004975 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00004976 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004977
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004978 // If this is a memory input, and if the operand is not indirect, do what we
4979 // need to to provide an address for the memory input.
4980 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
4981 !OpInfo.isIndirect) {
4982 assert(OpInfo.Type == InlineAsm::isInput &&
4983 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004985 // Memory operands really want the address of the value. If we don't have
4986 // an indirect input, put it in the constpool if we can, otherwise spill
4987 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004988
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004989 // If the operand is a float, integer, or vector constant, spill to a
4990 // constant pool entry to get its address.
4991 Value *OpVal = OpInfo.CallOperandVal;
4992 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
4993 isa<ConstantVector>(OpVal)) {
4994 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
4995 TLI.getPointerTy());
4996 } else {
4997 // Otherwise, create a stack slot and emit a store to it before the
4998 // asm.
4999 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005000 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005001 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5002 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005003 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005004 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005005 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005006 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005007 OpInfo.CallOperand = StackSlot;
5008 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005009
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005010 // There is no longer a Value* corresponding to this operand.
5011 OpInfo.CallOperandVal = 0;
5012 // It is now an indirect operand.
5013 OpInfo.isIndirect = true;
5014 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005015
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005016 // If this constraint is for a specific register, allocate it before
5017 // anything else.
5018 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005019 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005020 }
5021 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005022
5023
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005024 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005025 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005026 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5027 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005028
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005029 // C_Register operands have already been allocated, Other/Memory don't need
5030 // to be.
5031 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005032 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005033 }
5034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005035 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5036 std::vector<SDValue> AsmNodeOperands;
5037 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5038 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005039 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005040
5041
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005042 // Loop over all of the inputs, copying the operand values into the
5043 // appropriate registers and processing the output regs.
5044 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005046 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5047 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005048
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005049 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5050 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5051
5052 switch (OpInfo.Type) {
5053 case InlineAsm::isOutput: {
5054 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5055 OpInfo.ConstraintType != TargetLowering::C_Register) {
5056 // Memory output, or 'other' output (e.g. 'X' constraint).
5057 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5058
5059 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005060 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5061 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005062 TLI.getPointerTy()));
5063 AsmNodeOperands.push_back(OpInfo.CallOperand);
5064 break;
5065 }
5066
5067 // Otherwise, this is a register or register class output.
5068
5069 // Copy the output from the appropriate register. Find a register that
5070 // we can use.
5071 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005072 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005073 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005074 }
5075
5076 // If this is an indirect operand, store through the pointer after the
5077 // asm.
5078 if (OpInfo.isIndirect) {
5079 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5080 OpInfo.CallOperandVal));
5081 } else {
5082 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005083 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5084 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005085 // Concatenate this output onto the outputs list.
5086 RetValRegs.append(OpInfo.AssignedRegs);
5087 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005088
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005089 // Add information to the INLINEASM node to know that this register is
5090 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005091 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5092 6 /* EARLYCLOBBER REGDEF */ :
5093 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005094 false,
5095 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005096 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005097 break;
5098 }
5099 case InlineAsm::isInput: {
5100 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005101
Chris Lattner6bdcda32008-10-17 16:47:46 +00005102 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005103 // If this is required to match an output register we have already set,
5104 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005105 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005106
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005107 // Scan until we find the definition we already emitted of this operand.
5108 // When we find it, create a RegsForValue operand.
5109 unsigned CurOp = 2; // The first operand.
5110 for (; OperandNo; --OperandNo) {
5111 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005112 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005113 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005114 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5115 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5116 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005117 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005118 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005119 }
5120
Evan Cheng697cbbf2009-03-20 18:03:34 +00005121 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005122 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005123 if ((OpFlag & 7) == 2 /*REGDEF*/
5124 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5125 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005126 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005127 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005128 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005129 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005130 RegsForValue MatchedRegs;
5131 MatchedRegs.TLI = &TLI;
5132 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005133 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005134 MatchedRegs.RegVTs.push_back(RegVT);
5135 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005136 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005137 i != e; ++i)
5138 MatchedRegs.Regs.
5139 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005140
5141 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005142 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5143 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005144 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5145 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005146 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005147 break;
5148 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005149 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5150 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5151 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005152 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005153 // See InlineAsm.h isUseOperandTiedToDef.
5154 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005155 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156 TLI.getPointerTy()));
5157 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5158 break;
5159 }
5160 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005161
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005162 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005163 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005164 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005166 std::vector<SDValue> Ops;
5167 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005168 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005169 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005170 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005171 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005172 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005174 // Add information to the INLINEASM node to know about this input.
5175 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005176 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005177 TLI.getPointerTy()));
5178 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5179 break;
5180 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5181 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5182 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5183 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005184
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005185 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005186 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5187 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005188 TLI.getPointerTy()));
5189 AsmNodeOperands.push_back(InOperandVal);
5190 break;
5191 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005192
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005193 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5194 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5195 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005196 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005197 "Don't know how to handle indirect register inputs yet!");
5198
5199 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005200 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005201 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005202 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005203 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005204
Dale Johannesen66978ee2009-01-31 02:22:37 +00005205 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5206 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005207
Evan Cheng697cbbf2009-03-20 18:03:34 +00005208 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005209 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005210 break;
5211 }
5212 case InlineAsm::isClobber: {
5213 // Add the clobbered value to the operand list, so that the register
5214 // allocator is aware that the physreg got clobbered.
5215 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005216 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005217 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005218 break;
5219 }
5220 }
5221 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005223 // Finish up input operands.
5224 AsmNodeOperands[0] = Chain;
5225 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005226
Dale Johannesen66978ee2009-01-31 02:22:37 +00005227 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005228 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005229 &AsmNodeOperands[0], AsmNodeOperands.size());
5230 Flag = Chain.getValue(1);
5231
5232 // If this asm returns a register value, copy the result from that register
5233 // and set it as the value of the call.
5234 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005235 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005236 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005237
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005238 // FIXME: Why don't we do this for inline asms with MRVs?
5239 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005240 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005241
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005242 // If any of the results of the inline asm is a vector, it may have the
5243 // wrong width/num elts. This can happen for register classes that can
5244 // contain multiple different value types. The preg or vreg allocated may
5245 // not have the same VT as was expected. Convert it to the right type
5246 // with bit_convert.
5247 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005248 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005249 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005250
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005251 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005252 ResultType.isInteger() && Val.getValueType().isInteger()) {
5253 // If a result value was tied to an input value, the computed result may
5254 // have a wider width than the expected result. Extract the relevant
5255 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005256 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005257 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005258
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005259 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005260 }
Dan Gohman95915732008-10-18 01:03:45 +00005261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005263 // Don't need to use this as a chain in this case.
5264 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5265 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005266 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005267
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005268 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005270 // Process indirect outputs, first output all of the flagged copies out of
5271 // physregs.
5272 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5273 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5274 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005275 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5276 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005277 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005278
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005279 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005280
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005281 // Emit the non-flagged stores from the physregs.
5282 SmallVector<SDValue, 8> OutChains;
5283 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005284 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005285 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005286 getValue(StoresToEmit[i].second),
5287 StoresToEmit[i].second, 0));
5288 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005289 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005290 &OutChains[0], OutChains.size());
5291 DAG.setRoot(Chain);
5292}
5293
Dan Gohman2048b852009-11-23 18:04:58 +00005294void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005295 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005296 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005297 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005298 DAG.getSrcValue(I.getOperand(1))));
5299}
5300
Dan Gohman2048b852009-11-23 18:04:58 +00005301void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005302 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5303 getRoot(), getValue(I.getOperand(0)),
5304 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 setValue(&I, V);
5306 DAG.setRoot(V.getValue(1));
5307}
5308
Dan Gohman2048b852009-11-23 18:04:58 +00005309void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005310 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005311 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005312 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313 DAG.getSrcValue(I.getOperand(1))));
5314}
5315
Dan Gohman2048b852009-11-23 18:04:58 +00005316void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005317 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005318 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005319 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005320 getValue(I.getOperand(2)),
5321 DAG.getSrcValue(I.getOperand(1)),
5322 DAG.getSrcValue(I.getOperand(2))));
5323}
5324
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005326/// implementation, which just calls LowerCall.
5327/// FIXME: When all targets are
5328/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005329std::pair<SDValue, SDValue>
5330TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5331 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005332 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005333 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005334 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005336 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005337
Dan Gohman1937e2f2008-09-16 01:42:28 +00005338 assert((!isTailCall || PerformTailCallOpt) &&
5339 "isTailCall set when tail-call optimizations are disabled!");
5340
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005341 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005342 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005343 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005344 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005345 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5346 for (unsigned Value = 0, NumValues = ValueVTs.size();
5347 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005348 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005349 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005350 SDValue Op = SDValue(Args[i].Node.getNode(),
5351 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352 ISD::ArgFlagsTy Flags;
5353 unsigned OriginalAlignment =
5354 getTargetData()->getABITypeAlignment(ArgTy);
5355
5356 if (Args[i].isZExt)
5357 Flags.setZExt();
5358 if (Args[i].isSExt)
5359 Flags.setSExt();
5360 if (Args[i].isInReg)
5361 Flags.setInReg();
5362 if (Args[i].isSRet)
5363 Flags.setSRet();
5364 if (Args[i].isByVal) {
5365 Flags.setByVal();
5366 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5367 const Type *ElementTy = Ty->getElementType();
5368 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005369 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005370 // For ByVal, alignment should come from FE. BE will guess if this
5371 // info is not there but there are cases it cannot get right.
5372 if (Args[i].Alignment)
5373 FrameAlign = Args[i].Alignment;
5374 Flags.setByValAlign(FrameAlign);
5375 Flags.setByValSize(FrameSize);
5376 }
5377 if (Args[i].isNest)
5378 Flags.setNest();
5379 Flags.setOrigAlign(OriginalAlignment);
5380
Owen Anderson23b9b192009-08-12 00:36:31 +00005381 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5382 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005383 SmallVector<SDValue, 4> Parts(NumParts);
5384 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5385
5386 if (Args[i].isSExt)
5387 ExtendKind = ISD::SIGN_EXTEND;
5388 else if (Args[i].isZExt)
5389 ExtendKind = ISD::ZERO_EXTEND;
5390
Dale Johannesen66978ee2009-01-31 02:22:37 +00005391 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005392
Dan Gohman98ca4f22009-08-05 01:29:28 +00005393 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005394 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005395 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5396 if (NumParts > 1 && j == 0)
5397 MyFlags.Flags.setSplit();
5398 else if (j != 0)
5399 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400
Dan Gohman98ca4f22009-08-05 01:29:28 +00005401 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005402 }
5403 }
5404 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005405
Dan Gohman98ca4f22009-08-05 01:29:28 +00005406 // Handle the incoming return values from the call.
5407 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005408 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005409 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005410 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005411 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005412 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5413 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005414 for (unsigned i = 0; i != NumRegs; ++i) {
5415 ISD::InputArg MyFlags;
5416 MyFlags.VT = RegisterVT;
5417 MyFlags.Used = isReturnValueUsed;
5418 if (RetSExt)
5419 MyFlags.Flags.setSExt();
5420 if (RetZExt)
5421 MyFlags.Flags.setZExt();
5422 if (isInreg)
5423 MyFlags.Flags.setInReg();
5424 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005425 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005426 }
5427
Dan Gohman98ca4f22009-08-05 01:29:28 +00005428 // Check if target-dependent constraints permit a tail call here.
5429 // Target-independent constraints should be checked by the caller.
5430 if (isTailCall &&
5431 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
5432 isTailCall = false;
5433
5434 SmallVector<SDValue, 4> InVals;
5435 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
5436 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005437
5438 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005439 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005440 "LowerCall didn't return a valid chain!");
5441 assert((!isTailCall || InVals.empty()) &&
5442 "LowerCall emitted a return value for a tail call!");
5443 assert((isTailCall || InVals.size() == Ins.size()) &&
5444 "LowerCall didn't emit the correct number of values!");
5445 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5446 assert(InVals[i].getNode() &&
5447 "LowerCall emitted a null value!");
5448 assert(Ins[i].VT == InVals[i].getValueType() &&
5449 "LowerCall emitted a value with the wrong type!");
5450 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00005451
5452 // For a tail call, the return value is merely live-out and there aren't
5453 // any nodes in the DAG representing it. Return a special value to
5454 // indicate that a tail call has been emitted and no more Instructions
5455 // should be processed in the current block.
5456 if (isTailCall) {
5457 DAG.setRoot(Chain);
5458 return std::make_pair(SDValue(), SDValue());
5459 }
5460
5461 // Collect the legal value parts into potentially illegal values
5462 // that correspond to the original function's return values.
5463 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5464 if (RetSExt)
5465 AssertOp = ISD::AssertSext;
5466 else if (RetZExt)
5467 AssertOp = ISD::AssertZext;
5468 SmallVector<SDValue, 4> ReturnValues;
5469 unsigned CurReg = 0;
5470 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005471 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005472 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5473 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005474
5475 SDValue ReturnValue =
5476 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
5477 AssertOp);
5478 ReturnValues.push_back(ReturnValue);
5479 CurReg += NumRegs;
5480 }
5481
5482 // For a function returning void, there is no return value. We can't create
5483 // such a node, so we just return a null return value in that case. In
5484 // that case, nothing will actualy look at the value.
5485 if (ReturnValues.empty())
5486 return std::make_pair(SDValue(), Chain);
5487
5488 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5489 DAG.getVTList(&RetTys[0], RetTys.size()),
5490 &ReturnValues[0], ReturnValues.size());
5491
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005492 return std::make_pair(Res, Chain);
5493}
5494
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005495void TargetLowering::LowerOperationWrapper(SDNode *N,
5496 SmallVectorImpl<SDValue> &Results,
5497 SelectionDAG &DAG) {
5498 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005499 if (Res.getNode())
5500 Results.push_back(Res);
5501}
5502
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005503SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005504 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505 return SDValue();
5506}
5507
5508
Dan Gohman2048b852009-11-23 18:04:58 +00005509void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005510 SDValue Op = getValue(V);
5511 assert((Op.getOpcode() != ISD::CopyFromReg ||
5512 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5513 "Copy from a reg to the same reg!");
5514 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5515
Owen Anderson23b9b192009-08-12 00:36:31 +00005516 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005517 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005518 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005519 PendingExports.push_back(Chain);
5520}
5521
5522#include "llvm/CodeGen/SelectionDAGISel.h"
5523
Dan Gohman8c2b5252009-10-30 01:27:03 +00005524void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005525 // If this is the entry block, emit arguments.
5526 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005527 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005528 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005529 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005530 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005531 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005532
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005533 // Check whether the function can return without sret-demotion.
5534 SmallVector<EVT, 4> OutVTs;
5535 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005536 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
5537 OutVTs, OutsFlags, TLI);
5538 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5539
5540 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
5541 OutVTs, OutsFlags, DAG);
5542 if (!FLI.CanLowerReturn) {
5543 // Put in an sret pointer parameter before all the other parameters.
5544 SmallVector<EVT, 1> ValueVTs;
5545 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5546
5547 // NOTE: Assuming that a pointer will never break down to more than one VT
5548 // or one register.
5549 ISD::ArgFlagsTy Flags;
5550 Flags.setSRet();
5551 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5552 ISD::InputArg RetArg(Flags, RegisterVT, true);
5553 Ins.push_back(RetArg);
5554 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005555
Dan Gohman98ca4f22009-08-05 01:29:28 +00005556 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005557 unsigned Idx = 1;
5558 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5559 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005560 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005561 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5562 bool isArgValueUsed = !I->use_empty();
5563 for (unsigned Value = 0, NumValues = ValueVTs.size();
5564 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005565 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005566 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005567 ISD::ArgFlagsTy Flags;
5568 unsigned OriginalAlignment =
5569 TD->getABITypeAlignment(ArgTy);
5570
5571 if (F.paramHasAttr(Idx, Attribute::ZExt))
5572 Flags.setZExt();
5573 if (F.paramHasAttr(Idx, Attribute::SExt))
5574 Flags.setSExt();
5575 if (F.paramHasAttr(Idx, Attribute::InReg))
5576 Flags.setInReg();
5577 if (F.paramHasAttr(Idx, Attribute::StructRet))
5578 Flags.setSRet();
5579 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5580 Flags.setByVal();
5581 const PointerType *Ty = cast<PointerType>(I->getType());
5582 const Type *ElementTy = Ty->getElementType();
5583 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5584 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5585 // For ByVal, alignment should be passed from FE. BE will guess if
5586 // this info is not there but there are cases it cannot get right.
5587 if (F.getParamAlignment(Idx))
5588 FrameAlign = F.getParamAlignment(Idx);
5589 Flags.setByValAlign(FrameAlign);
5590 Flags.setByValSize(FrameSize);
5591 }
5592 if (F.paramHasAttr(Idx, Attribute::Nest))
5593 Flags.setNest();
5594 Flags.setOrigAlign(OriginalAlignment);
5595
Owen Anderson23b9b192009-08-12 00:36:31 +00005596 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5597 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005598 for (unsigned i = 0; i != NumRegs; ++i) {
5599 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5600 if (NumRegs > 1 && i == 0)
5601 MyFlags.Flags.setSplit();
5602 // if it isn't first piece, alignment must be 1
5603 else if (i > 0)
5604 MyFlags.Flags.setOrigAlign(1);
5605 Ins.push_back(MyFlags);
5606 }
5607 }
5608 }
5609
5610 // Call the target to set up the argument values.
5611 SmallVector<SDValue, 8> InVals;
5612 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5613 F.isVarArg(), Ins,
5614 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005615
5616 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005617 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005618 "LowerFormalArguments didn't return a valid chain!");
5619 assert(InVals.size() == Ins.size() &&
5620 "LowerFormalArguments didn't emit the correct number of values!");
5621 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5622 assert(InVals[i].getNode() &&
5623 "LowerFormalArguments emitted a null value!");
5624 assert(Ins[i].VT == InVals[i].getValueType() &&
5625 "LowerFormalArguments emitted a value with the wrong type!");
5626 });
5627
5628 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005629 DAG.setRoot(NewRoot);
5630
5631 // Set up the argument values.
5632 unsigned i = 0;
5633 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005634 if (!FLI.CanLowerReturn) {
5635 // Create a virtual register for the sret pointer, and put in a copy
5636 // from the sret argument into it.
5637 SmallVector<EVT, 1> ValueVTs;
5638 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5639 EVT VT = ValueVTs[0];
5640 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5641 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5642 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT,
5643 VT, AssertOp);
5644
Dan Gohman2048b852009-11-23 18:04:58 +00005645 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005646 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5647 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5648 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00005649 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005650 DAG.setRoot(NewRoot);
5651
5652 // i indexes lowered arguments. Bump it past the hidden sret argument.
5653 // Idx indexes LLVM arguments. Don't touch it.
5654 ++i;
5655 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005656 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5657 ++I, ++Idx) {
5658 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005659 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005660 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005661 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005662 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005663 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005664 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5665 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005666
5667 if (!I->use_empty()) {
5668 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5669 if (F.paramHasAttr(Idx, Attribute::SExt))
5670 AssertOp = ISD::AssertSext;
5671 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5672 AssertOp = ISD::AssertZext;
5673
5674 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
5675 PartVT, VT, AssertOp));
5676 }
5677 i += NumParts;
5678 }
5679 if (!I->use_empty()) {
Dan Gohman2048b852009-11-23 18:04:58 +00005680 SDB->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
5681 SDB->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005682 // If this argument is live outside of the entry block, insert a copy from
5683 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005684 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005685 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005686 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005687 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005688
5689 // Finally, if the target has anything special to do, allow it to do so.
5690 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00005691 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005692}
5693
5694/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5695/// ensure constants are generated when needed. Remember the virtual registers
5696/// that need to be added to the Machine PHI nodes as input. We cannot just
5697/// directly add them, because expansion might result in multiple MBB's for one
5698/// BB. As such, the start of the BB might correspond to a different MBB than
5699/// the end.
5700///
5701void
5702SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5703 TerminatorInst *TI = LLVMBB->getTerminator();
5704
5705 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5706
5707 // Check successor nodes' PHI nodes that expect a constant to be available
5708 // from this block.
5709 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5710 BasicBlock *SuccBB = TI->getSuccessor(succ);
5711 if (!isa<PHINode>(SuccBB->begin())) continue;
5712 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005713
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005714 // If this terminator has multiple identical successors (common for
5715 // switches), only handle each succ once.
5716 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005718 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5719 PHINode *PN;
5720
5721 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5722 // nodes and Machine PHI nodes, but the incoming operands have not been
5723 // emitted yet.
5724 for (BasicBlock::iterator I = SuccBB->begin();
5725 (PN = dyn_cast<PHINode>(I)); ++I) {
5726 // Ignore dead phi's.
5727 if (PN->use_empty()) continue;
5728
5729 unsigned Reg;
5730 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5731
5732 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00005733 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005734 if (RegOut == 0) {
5735 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00005736 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005737 }
5738 Reg = RegOut;
5739 } else {
5740 Reg = FuncInfo->ValueMap[PHIOp];
5741 if (Reg == 0) {
5742 assert(isa<AllocaInst>(PHIOp) &&
5743 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5744 "Didn't codegen value into a register!??");
5745 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00005746 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005747 }
5748 }
5749
5750 // Remember that this register needs to added to the machine PHI node as
5751 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00005752 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005753 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5754 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00005755 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00005756 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005757 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00005758 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005759 Reg += NumRegisters;
5760 }
5761 }
5762 }
Dan Gohman2048b852009-11-23 18:04:58 +00005763 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005764}
5765
Dan Gohman3df24e62008-09-03 23:12:08 +00005766/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5767/// supports legal types, and it emits MachineInstrs directly instead of
5768/// creating SelectionDAG nodes.
5769///
5770bool
5771SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5772 FastISel *F) {
5773 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005774
Dan Gohman3df24e62008-09-03 23:12:08 +00005775 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00005776 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00005777
5778 // Check successor nodes' PHI nodes that expect a constant to be available
5779 // from this block.
5780 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5781 BasicBlock *SuccBB = TI->getSuccessor(succ);
5782 if (!isa<PHINode>(SuccBB->begin())) continue;
5783 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005784
Dan Gohman3df24e62008-09-03 23:12:08 +00005785 // If this terminator has multiple identical successors (common for
5786 // switches), only handle each succ once.
5787 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005788
Dan Gohman3df24e62008-09-03 23:12:08 +00005789 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5790 PHINode *PN;
5791
5792 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5793 // nodes and Machine PHI nodes, but the incoming operands have not been
5794 // emitted yet.
5795 for (BasicBlock::iterator I = SuccBB->begin();
5796 (PN = dyn_cast<PHINode>(I)); ++I) {
5797 // Ignore dead phi's.
5798 if (PN->use_empty()) continue;
5799
5800 // Only handle legal types. Two interesting things to note here. First,
5801 // by bailing out early, we may leave behind some dead instructions,
5802 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
5803 // own moves. Second, this check is necessary becuase FastISel doesn't
5804 // use CreateRegForValue to create registers, so it always creates
5805 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00005806 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00005807 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
5808 // Promote MVT::i1.
5809 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00005810 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00005811 else {
Dan Gohman2048b852009-11-23 18:04:58 +00005812 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00005813 return false;
5814 }
Dan Gohman3df24e62008-09-03 23:12:08 +00005815 }
5816
5817 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5818
5819 unsigned Reg = F->getRegForValue(PHIOp);
5820 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00005821 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00005822 return false;
5823 }
Dan Gohman2048b852009-11-23 18:04:58 +00005824 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00005825 }
5826 }
5827
5828 return true;
5829}