blob: 077548514cead239ee6d86aeebb11dd926d422b1 [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) {
Daniel Dunbar819309e2009-12-16 20:10:05 +0000586 // Tell the DAG that we're processing a new instruction.
587 DAG.NewInst();
588
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000589 // Note: this doesn't use InstVisitor, because it has to work with
590 // ConstantExpr's in addition to instructions.
591 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000592 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000593 // Build the switch statement using the Instruction.def file.
594#define HANDLE_INST(NUM, OPCODE, CLASS) \
595 case Instruction::OPCODE:return visit##OPCODE((CLASS&)I);
596#include "llvm/Instruction.def"
597 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000598}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000599
Dan Gohman2048b852009-11-23 18:04:58 +0000600SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000601 SDValue &N = NodeMap[V];
602 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000603
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000604 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000605 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000606
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000607 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000608 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000609
610 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
611 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000613 if (isa<ConstantPointerNull>(C))
614 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000615
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000616 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000617 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000618
Nate Begeman9008ca62009-04-27 18:41:29 +0000619 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000620 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000621
622 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
623 visit(CE->getOpcode(), *CE);
624 SDValue N1 = NodeMap[V];
625 assert(N1.getNode() && "visit didn't populate the ValueMap!");
626 return N1;
627 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000628
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000629 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
630 SmallVector<SDValue, 4> Constants;
631 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
632 OI != OE; ++OI) {
633 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000634 // If the operand is an empty aggregate, there are no values.
635 if (!Val) continue;
636 // Add each leaf value from the operand to the Constants list
637 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000638 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
639 Constants.push_back(SDValue(Val, i));
640 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000641 return DAG.getMergeValues(&Constants[0], Constants.size(),
642 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000643 }
644
645 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
646 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
647 "Unknown struct or array constant!");
648
Owen Andersone50ed302009-08-10 22:56:29 +0000649 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000650 ComputeValueVTs(TLI, C->getType(), ValueVTs);
651 unsigned NumElts = ValueVTs.size();
652 if (NumElts == 0)
653 return SDValue(); // empty struct
654 SmallVector<SDValue, 4> Constants(NumElts);
655 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000656 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000657 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000658 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000659 else if (EltVT.isFloatingPoint())
660 Constants[i] = DAG.getConstantFP(0, EltVT);
661 else
662 Constants[i] = DAG.getConstant(0, EltVT);
663 }
Dale Johannesen4be0bdf2009-02-05 00:20:09 +0000664 return DAG.getMergeValues(&Constants[0], NumElts, getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000665 }
666
Dan Gohman8c2b5252009-10-30 01:27:03 +0000667 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000668 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000669
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000670 const VectorType *VecTy = cast<VectorType>(V->getType());
671 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000672
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000673 // Now that we know the number and type of the elements, get that number of
674 // elements into the Ops array based on what kind of constant it is.
675 SmallVector<SDValue, 16> Ops;
676 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
677 for (unsigned i = 0; i != NumElements; ++i)
678 Ops.push_back(getValue(CP->getOperand(i)));
679 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000680 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000681 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682
683 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000684 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000685 Op = DAG.getConstantFP(0, EltVT);
686 else
687 Op = DAG.getConstant(0, EltVT);
688 Ops.assign(NumElements, Op);
689 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000691 // Create a BUILD_VECTOR node.
Evan Chenga87008d2009-02-25 22:49:59 +0000692 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
693 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000694 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000695
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000696 // If this is a static alloca, generate it as the frameindex instead of
697 // computation.
698 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
699 DenseMap<const AllocaInst*, int>::iterator SI =
700 FuncInfo.StaticAllocaMap.find(AI);
701 if (SI != FuncInfo.StaticAllocaMap.end())
702 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
703 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000705 unsigned InReg = FuncInfo.ValueMap[V];
706 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000707
Owen Anderson23b9b192009-08-12 00:36:31 +0000708 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000709 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +0000710 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000711}
712
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000713/// Get the EVTs and ArgFlags collections that represent the return type
714/// of the given function. This does not require a DAG or a return value, and
715/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000716static void getReturnInfo(const Type* ReturnType,
717 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000718 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000719 TargetLowering &TLI,
720 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000721 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000722 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000723 unsigned NumValues = ValueVTs.size();
724 if ( NumValues == 0 ) return;
725
726 for (unsigned j = 0, f = NumValues; j != f; ++j) {
727 EVT VT = ValueVTs[j];
728 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000729
730 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000731 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000732 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000733 ExtendKind = ISD::ZERO_EXTEND;
734
735 // FIXME: C calling convention requires the return type to be promoted to
736 // at least 32-bit. But this is not necessary for non-C calling
737 // conventions. The frontend should mark functions whose return values
738 // require promoting with signext or zeroext attributes.
739 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000740 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000741 if (VT.bitsLT(MinVT))
742 VT = MinVT;
743 }
744
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000745 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
746 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000747 // 'inreg' on function refers to return value
748 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000749 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000750 Flags.setInReg();
751
752 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000753 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000754 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000755 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000756 Flags.setZExt();
757
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000758 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000759 OutVTs.push_back(PartVT);
760 OutFlags.push_back(Flags);
761 }
762 }
763}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000764
Dan Gohman2048b852009-11-23 18:04:58 +0000765void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000766 SDValue Chain = getControlRoot();
767 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000768 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
769
770 if (!FLI.CanLowerReturn) {
771 unsigned DemoteReg = FLI.DemoteRegister;
772 const Function *F = I.getParent()->getParent();
773
774 // Emit a store of the return value through the virtual register.
775 // Leave Outs empty so that LowerReturn won't try to load return
776 // registers the usual way.
777 SmallVector<EVT, 1> PtrValueVTs;
778 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
779 PtrValueVTs);
780
781 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
782 SDValue RetOp = getValue(I.getOperand(0));
783
Owen Andersone50ed302009-08-10 22:56:29 +0000784 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000785 SmallVector<uint64_t, 4> Offsets;
786 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000787 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000788
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000789 SmallVector<SDValue, 4> Chains(NumValues);
790 EVT PtrVT = PtrValueVTs[0];
791 for (unsigned i = 0; i != NumValues; ++i)
792 Chains[i] = DAG.getStore(Chain, getCurDebugLoc(),
793 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
794 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
795 DAG.getConstant(Offsets[i], PtrVT)),
796 NULL, Offsets[i], false, 0);
797 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
798 MVT::Other, &Chains[0], NumValues);
799 }
800 else {
801 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
802 SmallVector<EVT, 4> ValueVTs;
803 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
804 unsigned NumValues = ValueVTs.size();
805 if (NumValues == 0) continue;
806
807 SDValue RetOp = getValue(I.getOperand(i));
808 for (unsigned j = 0, f = NumValues; j != f; ++j) {
809 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000810
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000811 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000812
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000813 const Function *F = I.getParent()->getParent();
814 if (F->paramHasAttr(0, Attribute::SExt))
815 ExtendKind = ISD::SIGN_EXTEND;
816 else if (F->paramHasAttr(0, Attribute::ZExt))
817 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000818
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000819 // FIXME: C calling convention requires the return type to be promoted to
820 // at least 32-bit. But this is not necessary for non-C calling
821 // conventions. The frontend should mark functions whose return values
822 // require promoting with signext or zeroext attributes.
823 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
824 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
825 if (VT.bitsLT(MinVT))
826 VT = MinVT;
827 }
828
829 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
830 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
831 SmallVector<SDValue, 4> Parts(NumParts);
832 getCopyToParts(DAG, getCurDebugLoc(),
833 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
834 &Parts[0], NumParts, PartVT, ExtendKind);
835
836 // 'inreg' on function refers to return value
837 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
838 if (F->paramHasAttr(0, Attribute::InReg))
839 Flags.setInReg();
840
841 // Propagate extension type if any
842 if (F->paramHasAttr(0, Attribute::SExt))
843 Flags.setSExt();
844 else if (F->paramHasAttr(0, Attribute::ZExt))
845 Flags.setZExt();
846
847 for (unsigned i = 0; i < NumParts; ++i)
848 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000849 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000850 }
851 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000852
853 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000854 CallingConv::ID CallConv =
855 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000856 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
857 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000858
859 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000860 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000861 "LowerReturn didn't return a valid chain!");
862
863 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000864 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000865}
866
Dan Gohmanad62f532009-04-23 23:13:24 +0000867/// CopyToExportRegsIfNeeded - If the given value has virtual registers
868/// created for it, emit nodes to copy the value into the virtual
869/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000870void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000871 if (!V->use_empty()) {
872 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
873 if (VMI != FuncInfo.ValueMap.end())
874 CopyValueToVirtualRegister(V, VMI->second);
875 }
876}
877
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000878/// ExportFromCurrentBlock - If this condition isn't known to be exported from
879/// the current basic block, add it to ValueMap now so that we'll get a
880/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000881void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000882 // No need to export constants.
883 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000884
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000885 // Already exported?
886 if (FuncInfo.isExportedInst(V)) return;
887
888 unsigned Reg = FuncInfo.InitializeRegForValue(V);
889 CopyValueToVirtualRegister(V, Reg);
890}
891
Dan Gohman2048b852009-11-23 18:04:58 +0000892bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
893 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000894 // The operands of the setcc have to be in this block. We don't know
895 // how to export them from some other block.
896 if (Instruction *VI = dyn_cast<Instruction>(V)) {
897 // Can export from current BB.
898 if (VI->getParent() == FromBB)
899 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000900
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000901 // Is already exported, noop.
902 return FuncInfo.isExportedInst(V);
903 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000905 // If this is an argument, we can export it if the BB is the entry block or
906 // if it is already exported.
907 if (isa<Argument>(V)) {
908 if (FromBB == &FromBB->getParent()->getEntryBlock())
909 return true;
910
911 // Otherwise, can only export this if it is already exported.
912 return FuncInfo.isExportedInst(V);
913 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915 // Otherwise, constants can always be exported.
916 return true;
917}
918
919static bool InBlock(const Value *V, const BasicBlock *BB) {
920 if (const Instruction *I = dyn_cast<Instruction>(V))
921 return I->getParent() == BB;
922 return true;
923}
924
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000925/// getFCmpCondCode - Return the ISD condition code corresponding to
926/// the given LLVM IR floating-point condition code. This includes
927/// consideration of global floating-point math flags.
928///
929static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
930 ISD::CondCode FPC, FOC;
931 switch (Pred) {
932 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
933 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
934 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
935 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
936 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
937 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
938 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
939 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
940 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
941 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
942 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
943 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
944 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
945 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
946 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
947 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
948 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000949 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000950 FOC = FPC = ISD::SETFALSE;
951 break;
952 }
953 if (FiniteOnlyFPMath())
954 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000955 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000956 return FPC;
957}
958
959/// getICmpCondCode - Return the ISD condition code corresponding to
960/// the given LLVM IR integer condition code.
961///
962static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
963 switch (Pred) {
964 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
965 case ICmpInst::ICMP_NE: return ISD::SETNE;
966 case ICmpInst::ICMP_SLE: return ISD::SETLE;
967 case ICmpInst::ICMP_ULE: return ISD::SETULE;
968 case ICmpInst::ICMP_SGE: return ISD::SETGE;
969 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
970 case ICmpInst::ICMP_SLT: return ISD::SETLT;
971 case ICmpInst::ICMP_ULT: return ISD::SETULT;
972 case ICmpInst::ICMP_SGT: return ISD::SETGT;
973 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
974 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000975 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000976 return ISD::SETNE;
977 }
978}
979
Dan Gohmanc2277342008-10-17 21:16:08 +0000980/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
981/// This function emits a branch and is used at the leaves of an OR or an
982/// AND operator tree.
983///
984void
Dan Gohman2048b852009-11-23 18:04:58 +0000985SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
986 MachineBasicBlock *TBB,
987 MachineBasicBlock *FBB,
988 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000989 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000990
Dan Gohmanc2277342008-10-17 21:16:08 +0000991 // If the leaf of the tree is a comparison, merge the condition into
992 // the caseblock.
993 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
994 // The operands of the cmp have to be in this block. We don't know
995 // how to export them from some other block. If this is the first block
996 // of the sequence, no exporting is needed.
997 if (CurBB == CurMBB ||
998 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
999 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001000 ISD::CondCode Condition;
1001 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001002 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001003 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001004 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001005 } else {
1006 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001007 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001008 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001009
1010 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001011 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1012 SwitchCases.push_back(CB);
1013 return;
1014 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001015 }
1016
1017 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001018 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001019 NULL, TBB, FBB, CurBB);
1020 SwitchCases.push_back(CB);
1021}
1022
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001023/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001024void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1025 MachineBasicBlock *TBB,
1026 MachineBasicBlock *FBB,
1027 MachineBasicBlock *CurBB,
1028 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001029 // If this node is not part of the or/and tree, emit it as a branch.
1030 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001031 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001032 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1033 BOp->getParent() != CurBB->getBasicBlock() ||
1034 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1035 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1036 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001037 return;
1038 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001039
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001040 // Create TmpBB after CurBB.
1041 MachineFunction::iterator BBI = CurBB;
1042 MachineFunction &MF = DAG.getMachineFunction();
1043 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1044 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001045
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001046 if (Opc == Instruction::Or) {
1047 // Codegen X | Y as:
1048 // jmp_if_X TBB
1049 // jmp TmpBB
1050 // TmpBB:
1051 // jmp_if_Y TBB
1052 // jmp FBB
1053 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001055 // Emit the LHS condition.
1056 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001057
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001058 // Emit the RHS condition into TmpBB.
1059 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1060 } else {
1061 assert(Opc == Instruction::And && "Unknown merge op!");
1062 // Codegen X & Y as:
1063 // jmp_if_X TmpBB
1064 // jmp FBB
1065 // TmpBB:
1066 // jmp_if_Y TBB
1067 // jmp FBB
1068 //
1069 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001070
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001071 // Emit the LHS condition.
1072 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001074 // Emit the RHS condition into TmpBB.
1075 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1076 }
1077}
1078
1079/// If the set of cases should be emitted as a series of branches, return true.
1080/// If we should emit this as a bunch of and/or'd together conditions, return
1081/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001082bool
Dan Gohman2048b852009-11-23 18:04:58 +00001083SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001084 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001086 // If this is two comparisons of the same values or'd or and'd together, they
1087 // will get folded into a single comparison, so don't emit two blocks.
1088 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1089 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1090 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1091 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1092 return false;
1093 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001094
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001095 return true;
1096}
1097
Dan Gohman2048b852009-11-23 18:04:58 +00001098void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001099 // Update machine-CFG edges.
1100 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1101
1102 // Figure out which block is immediately after the current one.
1103 MachineBasicBlock *NextBlock = 0;
1104 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001105 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001106 NextBlock = BBI;
1107
1108 if (I.isUnconditional()) {
1109 // Update machine-CFG edges.
1110 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001111
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001112 // If this is not a fall-through branch, emit the branch.
1113 if (Succ0MBB != NextBlock)
Scott Michelfdc40a02009-02-17 22:15:04 +00001114 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001115 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001116 DAG.getBasicBlock(Succ0MBB)));
1117 return;
1118 }
1119
1120 // If this condition is one of the special cases we handle, do special stuff
1121 // now.
1122 Value *CondVal = I.getCondition();
1123 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1124
1125 // If this is a series of conditions that are or'd or and'd together, emit
1126 // this as a sequence of branches instead of setcc's with and/or operations.
1127 // For example, instead of something like:
1128 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001129 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001130 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001131 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001132 // or C, F
1133 // jnz foo
1134 // Emit:
1135 // cmp A, B
1136 // je foo
1137 // cmp D, E
1138 // jle foo
1139 //
1140 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001141 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001142 (BOp->getOpcode() == Instruction::And ||
1143 BOp->getOpcode() == Instruction::Or)) {
1144 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1145 // If the compares in later blocks need to use values not currently
1146 // exported from this block, export them now. This block should always
1147 // be the first entry.
1148 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001149
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001150 // Allow some cases to be rejected.
1151 if (ShouldEmitAsBranches(SwitchCases)) {
1152 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1153 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1154 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1155 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001157 // Emit the branch for this block.
1158 visitSwitchCase(SwitchCases[0]);
1159 SwitchCases.erase(SwitchCases.begin());
1160 return;
1161 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001162
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001163 // Okay, we decided not to do this, remove any inserted MBB's and clear
1164 // SwitchCases.
1165 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001166 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001167
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001168 SwitchCases.clear();
1169 }
1170 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001171
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001173 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001174 NULL, Succ0MBB, Succ1MBB, CurMBB);
1175 // Use visitSwitchCase to actually insert the fast branch sequence for this
1176 // cond branch.
1177 visitSwitchCase(CB);
1178}
1179
1180/// visitSwitchCase - Emits the necessary code to represent a single node in
1181/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001182void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001183 SDValue Cond;
1184 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001185 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001186
1187 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001188 if (CB.CmpMHS == NULL) {
1189 // Fold "(X == true)" to X and "(X == false)" to !X to
1190 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001191 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001192 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001193 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001194 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001195 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001196 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001197 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001198 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001199 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001200 } else {
1201 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1202
Anton Korobeynikov23218582008-12-23 22:25:27 +00001203 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1204 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205
1206 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001207 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001208
1209 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001210 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001211 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001213 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001214 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001216 DAG.getConstant(High-Low, VT), ISD::SETULE);
1217 }
1218 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001220 // Update successor info
1221 CurMBB->addSuccessor(CB.TrueBB);
1222 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001223
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001224 // Set NextBlock to be the MBB immediately after the current one, if any.
1225 // This is used to avoid emitting unnecessary branches to the next block.
1226 MachineBasicBlock *NextBlock = 0;
1227 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001228 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001229 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001230
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001231 // If the lhs block is the next block, invert the condition so that we can
1232 // fall through to the lhs instead of the rhs block.
1233 if (CB.TrueBB == NextBlock) {
1234 std::swap(CB.TrueBB, CB.FalseBB);
1235 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001236 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001237 }
Dale Johannesenf5d97892009-02-04 01:48:28 +00001238 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001240 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001241
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001242 // If the branch was constant folded, fix up the CFG.
1243 if (BrCond.getOpcode() == ISD::BR) {
1244 CurMBB->removeSuccessor(CB.FalseBB);
1245 DAG.setRoot(BrCond);
1246 } else {
1247 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001248 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001249 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001250
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001251 if (CB.FalseBB == NextBlock)
1252 DAG.setRoot(BrCond);
1253 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001254 DAG.setRoot(DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001255 DAG.getBasicBlock(CB.FalseBB)));
1256 }
1257}
1258
1259/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001260void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001261 // Emit the code for the jump table
1262 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001263 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001264 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1265 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001266 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Scott Michelfdc40a02009-02-17 22:15:04 +00001267 DAG.setRoot(DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001268 MVT::Other, Index.getValue(1),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001269 Table, Index));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001270}
1271
1272/// visitJumpTableHeader - This function emits necessary code to produce index
1273/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001274void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1275 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001276 // Subtract the lowest switch case value from the value being switched on and
1277 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 // difference between smallest and largest cases.
1279 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001280 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001281 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001282 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001283
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001284 // The SDNode we just created, which holds the value being switched on minus
1285 // the the smallest case value, needs to be copied to a virtual register so it
1286 // can be used as an index into the jump table in a subsequent basic block.
1287 // This value may be smaller or larger than the target's pointer type, and
1288 // therefore require extension or truncating.
Duncan Sands3a66a682009-10-13 21:04:12 +00001289 SwitchOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001290
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001292 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1293 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001294 JT.Reg = JumpTableReg;
1295
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001296 // Emit the range check for the jump table, and branch to the default block
1297 // for the switch statement if the value being switched on exceeds the largest
1298 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001299 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
1300 TLI.getSetCCResultType(SUB.getValueType()), SUB,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001301 DAG.getConstant(JTH.Last-JTH.First,VT),
1302 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303
1304 // Set NextBlock to be the MBB immediately after the current one, if any.
1305 // This is used to avoid emitting unnecessary branches to the next block.
1306 MachineBasicBlock *NextBlock = 0;
1307 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001308 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001309 NextBlock = BBI;
1310
Dale Johannesen66978ee2009-01-31 02:22:37 +00001311 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001312 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001313 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001314
1315 if (JT.MBB == NextBlock)
1316 DAG.setRoot(BrCond);
1317 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001319 DAG.getBasicBlock(JT.MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320}
1321
1322/// visitBitTestHeader - This function emits necessary code to produce value
1323/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001324void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001325 // Subtract the minimum value
1326 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001327 EVT VT = SwitchOp.getValueType();
Dale Johannesen66978ee2009-01-31 02:22:37 +00001328 SDValue SUB = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001329 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330
1331 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001332 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
1333 TLI.getSetCCResultType(SUB.getValueType()),
1334 SUB, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001335 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001336
Duncan Sands3a66a682009-10-13 21:04:12 +00001337 SDValue ShiftOp = DAG.getZExtOrTrunc(SUB, getCurDebugLoc(), TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001338
Duncan Sands92abc622009-01-31 15:50:11 +00001339 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001340 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1341 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001342
1343 // Set NextBlock to be the MBB immediately after the current one, if any.
1344 // This is used to avoid emitting unnecessary branches to the next block.
1345 MachineBasicBlock *NextBlock = 0;
1346 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001347 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001348 NextBlock = BBI;
1349
1350 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1351
1352 CurMBB->addSuccessor(B.Default);
1353 CurMBB->addSuccessor(MBB);
1354
Dale Johannesen66978ee2009-01-31 02:22:37 +00001355 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001356 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001357 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001358
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001359 if (MBB == NextBlock)
1360 DAG.setRoot(BrRange);
1361 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001362 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001363 DAG.getBasicBlock(MBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001364}
1365
1366/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001367void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1368 unsigned Reg,
1369 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001370 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001371 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001372 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001373 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001374 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001375 DAG.getConstant(1, TLI.getPointerTy()),
1376 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001377
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001378 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001379 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001380 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001381 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001382 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1383 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001384 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001385 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386
1387 CurMBB->addSuccessor(B.TargetBB);
1388 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001389
Dale Johannesen66978ee2009-01-31 02:22:37 +00001390 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001391 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001392 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001393
1394 // Set NextBlock to be the MBB immediately after the current one, if any.
1395 // This is used to avoid emitting unnecessary branches to the next block.
1396 MachineBasicBlock *NextBlock = 0;
1397 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001398 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001399 NextBlock = BBI;
1400
1401 if (NextMBB == NextBlock)
1402 DAG.setRoot(BrAnd);
1403 else
Owen Anderson825b72b2009-08-11 20:47:22 +00001404 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001405 DAG.getBasicBlock(NextMBB)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406}
1407
Dan Gohman2048b852009-11-23 18:04:58 +00001408void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001409 // Retrieve successors.
1410 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1411 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1412
Gabor Greifb67e6b32009-01-15 11:10:44 +00001413 const Value *Callee(I.getCalledValue());
1414 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001415 visitInlineAsm(&I);
1416 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001417 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001418
1419 // If the value of the invoke is used outside of its defining block, make it
1420 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001421 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001422
1423 // Update successor info
1424 CurMBB->addSuccessor(Return);
1425 CurMBB->addSuccessor(LandingPad);
1426
1427 // Drop into normal successor.
Scott Michelfdc40a02009-02-17 22:15:04 +00001428 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001429 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430 DAG.getBasicBlock(Return)));
1431}
1432
Dan Gohman2048b852009-11-23 18:04:58 +00001433void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001434}
1435
1436/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1437/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001438bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1439 CaseRecVector& WorkList,
1440 Value* SV,
1441 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001443
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001444 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001445 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001446 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001447 return false;
1448
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449 // Get the MachineFunction which holds the current MBB. This is used when
1450 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001451 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001452
1453 // Figure out which block is immediately after the current one.
1454 MachineBasicBlock *NextBlock = 0;
1455 MachineFunction::iterator BBI = CR.CaseBB;
1456
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001457 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458 NextBlock = BBI;
1459
1460 // TODO: If any two of the cases has the same destination, and if one value
1461 // is the same as the other, but has one bit unset that the other has set,
1462 // use bit manipulation to do two compares at once. For example:
1463 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001464
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001465 // Rearrange the case blocks so that the last one falls through if possible.
1466 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1467 // The last case block won't fall through into 'NextBlock' if we emit the
1468 // branches in this order. See if rearranging a case value would help.
1469 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1470 if (I->BB == NextBlock) {
1471 std::swap(*I, BackCase);
1472 break;
1473 }
1474 }
1475 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001476
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001477 // Create a CaseBlock record representing a conditional branch to
1478 // the Case's target mbb if the value being switched on SV is equal
1479 // to C.
1480 MachineBasicBlock *CurBlock = CR.CaseBB;
1481 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1482 MachineBasicBlock *FallThrough;
1483 if (I != E-1) {
1484 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1485 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001486
1487 // Put SV in a virtual register to make it available from the new blocks.
1488 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001489 } else {
1490 // If the last case doesn't match, go to the default block.
1491 FallThrough = Default;
1492 }
1493
1494 Value *RHS, *LHS, *MHS;
1495 ISD::CondCode CC;
1496 if (I->High == I->Low) {
1497 // This is just small small case range :) containing exactly 1 case
1498 CC = ISD::SETEQ;
1499 LHS = SV; RHS = I->High; MHS = NULL;
1500 } else {
1501 CC = ISD::SETLE;
1502 LHS = I->Low; MHS = SV; RHS = I->High;
1503 }
1504 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001506 // If emitting the first comparison, just call visitSwitchCase to emit the
1507 // code into the current block. Otherwise, push the CaseBlock onto the
1508 // vector to be later processed by SDISel, and insert the node's MBB
1509 // before the next MBB.
1510 if (CurBlock == CurMBB)
1511 visitSwitchCase(CB);
1512 else
1513 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001514
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001515 CurBlock = FallThrough;
1516 }
1517
1518 return true;
1519}
1520
1521static inline bool areJTsAllowed(const TargetLowering &TLI) {
1522 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001523 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1524 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001525}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001526
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001527static APInt ComputeRange(const APInt &First, const APInt &Last) {
1528 APInt LastExt(Last), FirstExt(First);
1529 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1530 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1531 return (LastExt - FirstExt + 1ULL);
1532}
1533
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001534/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001535bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1536 CaseRecVector& WorkList,
1537 Value* SV,
1538 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001539 Case& FrontCase = *CR.Range.first;
1540 Case& BackCase = *(CR.Range.second-1);
1541
Chris Lattnere880efe2009-11-07 07:50:34 +00001542 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1543 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001544
Chris Lattnere880efe2009-11-07 07:50:34 +00001545 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1547 I!=E; ++I)
1548 TSize += I->size();
1549
Chris Lattnere880efe2009-11-07 07:50:34 +00001550 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001552
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001553 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001554 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001555 if (Density < 0.4)
1556 return false;
1557
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001558 DEBUG(errs() << "Lowering jump table\n"
1559 << "First entry: " << First << ". Last entry: " << Last << '\n'
1560 << "Range: " << Range
1561 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562
1563 // Get the MachineFunction which holds the current MBB. This is used when
1564 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001565 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001566
1567 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001568 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001569 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001570
1571 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1572
1573 // Create a new basic block to hold the code for loading the address
1574 // of the jump table, and jumping to it. Update successor information;
1575 // we will either branch to the default case for the switch, or the jump
1576 // table.
1577 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1578 CurMF->insert(BBI, JumpTableBB);
1579 CR.CaseBB->addSuccessor(Default);
1580 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001581
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001582 // Build a vector of destination BBs, corresponding to each target
1583 // of the jump table. If the value of the jump table slot corresponds to
1584 // a case statement, push the case's BB onto the vector, otherwise, push
1585 // the default BB.
1586 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001587 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001588 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001589 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1590 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1591
1592 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593 DestBBs.push_back(I->BB);
1594 if (TEI==High)
1595 ++I;
1596 } else {
1597 DestBBs.push_back(Default);
1598 }
1599 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001600
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001601 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001602 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1603 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001604 E = DestBBs.end(); I != E; ++I) {
1605 if (!SuccsHandled[(*I)->getNumber()]) {
1606 SuccsHandled[(*I)->getNumber()] = true;
1607 JumpTableBB->addSuccessor(*I);
1608 }
1609 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001610
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001611 // Create a jump table index for this jump table, or return an existing
1612 // one.
1613 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001614
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001615 // Set the jump table information so that we can codegen it as a second
1616 // MachineBasicBlock
1617 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1618 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1619 if (CR.CaseBB == CurMBB)
1620 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001621
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622 JTCases.push_back(JumpTableBlock(JTH, JT));
1623
1624 return true;
1625}
1626
1627/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1628/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001629bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1630 CaseRecVector& WorkList,
1631 Value* SV,
1632 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001633 // Get the MachineFunction which holds the current MBB. This is used when
1634 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001635 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001636
1637 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001638 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001639 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640
1641 Case& FrontCase = *CR.Range.first;
1642 Case& BackCase = *(CR.Range.second-1);
1643 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1644
1645 // Size is the number of Cases represented by this range.
1646 unsigned Size = CR.Range.second - CR.Range.first;
1647
Chris Lattnere880efe2009-11-07 07:50:34 +00001648 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1649 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001650 double FMetric = 0;
1651 CaseItr Pivot = CR.Range.first + Size/2;
1652
1653 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1654 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001655 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001656 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1657 I!=E; ++I)
1658 TSize += I->size();
1659
Chris Lattnere880efe2009-11-07 07:50:34 +00001660 APInt LSize = FrontCase.size();
1661 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001662 DEBUG(errs() << "Selecting best pivot: \n"
1663 << "First: " << First << ", Last: " << Last <<'\n'
1664 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001665 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1666 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001667 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1668 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001669 APInt Range = ComputeRange(LEnd, RBegin);
1670 assert((Range - 2ULL).isNonNegative() &&
1671 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001672 double LDensity = (double)LSize.roundToDouble() /
1673 (LEnd - First + 1ULL).roundToDouble();
1674 double RDensity = (double)RSize.roundToDouble() /
1675 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001676 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001677 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001678 DEBUG(errs() <<"=>Step\n"
1679 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1680 << "LDensity: " << LDensity
1681 << ", RDensity: " << RDensity << '\n'
1682 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001683 if (FMetric < Metric) {
1684 Pivot = J;
1685 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001686 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001687 }
1688
1689 LSize += J->size();
1690 RSize -= J->size();
1691 }
1692 if (areJTsAllowed(TLI)) {
1693 // If our case is dense we *really* should handle it earlier!
1694 assert((FMetric > 0) && "Should handle dense range earlier!");
1695 } else {
1696 Pivot = CR.Range.first + Size/2;
1697 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001698
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001699 CaseRange LHSR(CR.Range.first, Pivot);
1700 CaseRange RHSR(Pivot, CR.Range.second);
1701 Constant *C = Pivot->Low;
1702 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001703
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001704 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001705 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001706 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001707 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001708 // Pivot's Value, then we can branch directly to the LHS's Target,
1709 // rather than creating a leaf node for it.
1710 if ((LHSR.second - LHSR.first) == 1 &&
1711 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001712 cast<ConstantInt>(C)->getValue() ==
1713 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 TrueBB = LHSR.first->BB;
1715 } else {
1716 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1717 CurMF->insert(BBI, TrueBB);
1718 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001719
1720 // Put SV in a virtual register to make it available from the new blocks.
1721 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001722 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001723
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001724 // Similar to the optimization above, if the Value being switched on is
1725 // known to be less than the Constant CR.LT, and the current Case Value
1726 // is CR.LT - 1, then we can branch directly to the target block for
1727 // the current Case Value, rather than emitting a RHS leaf node for it.
1728 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001729 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1730 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001731 FalseBB = RHSR.first->BB;
1732 } else {
1733 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1734 CurMF->insert(BBI, FalseBB);
1735 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001736
1737 // Put SV in a virtual register to make it available from the new blocks.
1738 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001739 }
1740
1741 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001742 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001743 // Otherwise, branch to LHS.
1744 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1745
1746 if (CR.CaseBB == CurMBB)
1747 visitSwitchCase(CB);
1748 else
1749 SwitchCases.push_back(CB);
1750
1751 return true;
1752}
1753
1754/// handleBitTestsSwitchCase - if current case range has few destination and
1755/// range span less, than machine word bitwidth, encode case range into series
1756/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001757bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1758 CaseRecVector& WorkList,
1759 Value* SV,
1760 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001761 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001762 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001763
1764 Case& FrontCase = *CR.Range.first;
1765 Case& BackCase = *(CR.Range.second-1);
1766
1767 // Get the MachineFunction which holds the current MBB. This is used when
1768 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001769 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001770
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001771 // If target does not have legal shift left, do not emit bit tests at all.
1772 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1773 return false;
1774
Anton Korobeynikov23218582008-12-23 22:25:27 +00001775 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001776 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1777 I!=E; ++I) {
1778 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001779 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001780 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001781
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001782 // Count unique destinations
1783 SmallSet<MachineBasicBlock*, 4> Dests;
1784 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1785 Dests.insert(I->BB);
1786 if (Dests.size() > 3)
1787 // Don't bother the code below, if there are too much unique destinations
1788 return false;
1789 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001790 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1791 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001792
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001793 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001794 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1795 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001796 APInt cmpRange = maxValue - minValue;
1797
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001798 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1799 << "Low bound: " << minValue << '\n'
1800 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001801
1802 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001803 (!(Dests.size() == 1 && numCmps >= 3) &&
1804 !(Dests.size() == 2 && numCmps >= 5) &&
1805 !(Dests.size() >= 3 && numCmps >= 6)))
1806 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001807
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001808 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001809 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1810
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001811 // Optimize the case where all the case values fit in a
1812 // word without having to subtract minValue. In this case,
1813 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001814 if (minValue.isNonNegative() &&
1815 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1816 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001817 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001818 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001819 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001821 CaseBitsVector CasesBits;
1822 unsigned i, count = 0;
1823
1824 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1825 MachineBasicBlock* Dest = I->BB;
1826 for (i = 0; i < count; ++i)
1827 if (Dest == CasesBits[i].BB)
1828 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001829
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001830 if (i == count) {
1831 assert((count < 3) && "Too much destinations to test!");
1832 CasesBits.push_back(CaseBits(0, Dest, 0));
1833 count++;
1834 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001835
1836 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1837 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1838
1839 uint64_t lo = (lowValue - lowBound).getZExtValue();
1840 uint64_t hi = (highValue - lowBound).getZExtValue();
1841
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001842 for (uint64_t j = lo; j <= hi; j++) {
1843 CasesBits[i].Mask |= 1ULL << j;
1844 CasesBits[i].Bits++;
1845 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001846
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001847 }
1848 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001849
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001850 BitTestInfo BTC;
1851
1852 // Figure out which block is immediately after the current one.
1853 MachineFunction::iterator BBI = CR.CaseBB;
1854 ++BBI;
1855
1856 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1857
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001858 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001859 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001860 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
1861 << ", Bits: " << CasesBits[i].Bits
1862 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863
1864 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1865 CurMF->insert(BBI, CaseBB);
1866 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1867 CaseBB,
1868 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001869
1870 // Put SV in a virtual register to make it available from the new blocks.
1871 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001872 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001873
1874 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001875 -1U, (CR.CaseBB == CurMBB),
1876 CR.CaseBB, Default, BTC);
1877
1878 if (CR.CaseBB == CurMBB)
1879 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001880
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001881 BitTestCases.push_back(BTB);
1882
1883 return true;
1884}
1885
1886
1887/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001888size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1889 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001890 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001891
1892 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001893 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001894 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1895 Cases.push_back(Case(SI.getSuccessorValue(i),
1896 SI.getSuccessorValue(i),
1897 SMBB));
1898 }
1899 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1900
1901 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001902 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903 // Must recompute end() each iteration because it may be
1904 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001905 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1906 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1907 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001908 MachineBasicBlock* nextBB = J->BB;
1909 MachineBasicBlock* currentBB = I->BB;
1910
1911 // If the two neighboring cases go to the same destination, merge them
1912 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001913 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001914 I->High = J->High;
1915 J = Cases.erase(J);
1916 } else {
1917 I = J++;
1918 }
1919 }
1920
1921 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1922 if (I->Low != I->High)
1923 // A range counts double, since it requires two compares.
1924 ++numCmps;
1925 }
1926
1927 return numCmps;
1928}
1929
Dan Gohman2048b852009-11-23 18:04:58 +00001930void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001931 // Figure out which block is immediately after the current one.
1932 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001933
1934 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1935
1936 // If there is only the default destination, branch to it if it is not the
1937 // next basic block. Otherwise, just fall through.
1938 if (SI.getNumOperands() == 2) {
1939 // Update machine-CFG edges.
1940
1941 // If this is not a fall-through branch, emit the branch.
1942 CurMBB->addSuccessor(Default);
1943 if (Default != NextBlock)
Dale Johannesen66978ee2009-01-31 02:22:37 +00001944 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001945 MVT::Other, getControlRoot(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946 DAG.getBasicBlock(Default)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001947 return;
1948 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001949
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001950 // If there are any non-default case statements, create a vector of Cases
1951 // representing each one, and sort the vector so that we can efficiently
1952 // create a binary search tree from them.
1953 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001954 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001955 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
1956 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001957 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001958
1959 // Get the Value to be switched on and default basic blocks, which will be
1960 // inserted into CaseBlock records, representing basic blocks in the binary
1961 // search tree.
1962 Value *SV = SI.getOperand(0);
1963
1964 // Push the initial CaseRec onto the worklist
1965 CaseRecVector WorkList;
1966 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
1967
1968 while (!WorkList.empty()) {
1969 // Grab a record representing a case range to process off the worklist
1970 CaseRec CR = WorkList.back();
1971 WorkList.pop_back();
1972
1973 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
1974 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001975
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001976 // If the range has few cases (two or less) emit a series of specific
1977 // tests.
1978 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
1979 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001980
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001981 // If the switch has more than 5 blocks, and at least 40% dense, and the
1982 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001983 // lowering the switch to a binary tree of conditional branches.
1984 if (handleJTSwitchCase(CR, WorkList, SV, Default))
1985 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001986
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001987 // Emit binary tree. We need to pick a pivot, and push left and right ranges
1988 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
1989 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
1990 }
1991}
1992
Dan Gohman2048b852009-11-23 18:04:58 +00001993void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00001994 // Update machine-CFG edges.
1995 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
1996 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
1997
Dan Gohman64825152009-10-27 21:56:26 +00001998 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
1999 MVT::Other, getControlRoot(),
2000 getValue(I.getAddress())));
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002001}
2002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002003
Dan Gohman2048b852009-11-23 18:04:58 +00002004void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002005 // -0.0 - X --> fneg
2006 const Type *Ty = I.getType();
2007 if (isa<VectorType>(Ty)) {
2008 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2009 const VectorType *DestTy = cast<VectorType>(I.getType());
2010 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002011 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002012 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002013 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002014 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002015 SDValue Op2 = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002016 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002017 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002018 return;
2019 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002020 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002022 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002023 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002024 SDValue Op2 = getValue(I.getOperand(1));
2025 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2026 Op2.getValueType(), Op2));
2027 return;
2028 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002029
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002030 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002031}
2032
Dan Gohman2048b852009-11-23 18:04:58 +00002033void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 SDValue Op1 = getValue(I.getOperand(0));
2035 SDValue Op2 = getValue(I.getOperand(1));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002036
Scott Michelfdc40a02009-02-17 22:15:04 +00002037 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002038 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039}
2040
Dan Gohman2048b852009-11-23 18:04:58 +00002041void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002042 SDValue Op1 = getValue(I.getOperand(0));
2043 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002044 if (!isa<VectorType>(I.getType()) &&
2045 Op2.getValueType() != TLI.getShiftAmountTy()) {
2046 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002047 EVT PTy = TLI.getPointerTy();
2048 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002049 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002050 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2051 TLI.getShiftAmountTy(), Op2);
2052 // If the operand is larger than the shift count type but the shift
2053 // count type has enough bits to represent any shift value, truncate
2054 // it now. This is a common case and it exposes the truncate to
2055 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002056 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002057 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2058 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2059 TLI.getShiftAmountTy(), Op2);
2060 // Otherwise we'll need to temporarily settle for some other
2061 // convenient type; type legalization will make adjustments as
2062 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002063 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002064 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002065 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002066 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002067 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002068 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002069 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002070
Scott Michelfdc40a02009-02-17 22:15:04 +00002071 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002072 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002073}
2074
Dan Gohman2048b852009-11-23 18:04:58 +00002075void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002076 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2077 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2078 predicate = IC->getPredicate();
2079 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2080 predicate = ICmpInst::Predicate(IC->getPredicate());
2081 SDValue Op1 = getValue(I.getOperand(0));
2082 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002083 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002084
Owen Andersone50ed302009-08-10 22:56:29 +00002085 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002086 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002087}
2088
Dan Gohman2048b852009-11-23 18:04:58 +00002089void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002090 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2091 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2092 predicate = FC->getPredicate();
2093 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2094 predicate = FCmpInst::Predicate(FC->getPredicate());
2095 SDValue Op1 = getValue(I.getOperand(0));
2096 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002097 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002098 EVT DestVT = TLI.getValueType(I.getType());
Chris Lattner9800e842009-07-07 22:41:32 +00002099 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002100}
2101
Dan Gohman2048b852009-11-23 18:04:58 +00002102void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002103 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002104 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2105 unsigned NumValues = ValueVTs.size();
2106 if (NumValues != 0) {
2107 SmallVector<SDValue, 4> Values(NumValues);
2108 SDValue Cond = getValue(I.getOperand(0));
2109 SDValue TrueVal = getValue(I.getOperand(1));
2110 SDValue FalseVal = getValue(I.getOperand(2));
2111
2112 for (unsigned i = 0; i != NumValues; ++i)
Scott Michelfdc40a02009-02-17 22:15:04 +00002113 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Dan Gohmana4f9cc42009-12-11 19:50:50 +00002114 TrueVal.getNode()->getValueType(i), Cond,
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002115 SDValue(TrueVal.getNode(), TrueVal.getResNo() + i),
2116 SDValue(FalseVal.getNode(), FalseVal.getResNo() + i));
2117
Scott Michelfdc40a02009-02-17 22:15:04 +00002118 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002119 DAG.getVTList(&ValueVTs[0], NumValues),
2120 &Values[0], NumValues));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002121 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002122}
2123
2124
Dan Gohman2048b852009-11-23 18:04:58 +00002125void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002126 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2127 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002128 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002129 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002130}
2131
Dan Gohman2048b852009-11-23 18:04:58 +00002132void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2134 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2135 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002136 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002137 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002138}
2139
Dan Gohman2048b852009-11-23 18:04:58 +00002140void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2142 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2143 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002144 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002145 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002146}
2147
Dan Gohman2048b852009-11-23 18:04:58 +00002148void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002149 // FPTrunc is never a no-op cast, no need to check
2150 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002151 EVT DestVT = TLI.getValueType(I.getType());
Scott Michelfdc40a02009-02-17 22:15:04 +00002152 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002153 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002154}
2155
Dan Gohman2048b852009-11-23 18:04:58 +00002156void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002157 // FPTrunc is never a no-op cast, no need to check
2158 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002159 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002160 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002161}
2162
Dan Gohman2048b852009-11-23 18:04:58 +00002163void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002164 // FPToUI is never a no-op cast, no need to check
2165 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002166 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002167 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002168}
2169
Dan Gohman2048b852009-11-23 18:04:58 +00002170void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002171 // FPToSI is never a no-op cast, no need to check
2172 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002173 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002174 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002175}
2176
Dan Gohman2048b852009-11-23 18:04:58 +00002177void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002178 // UIToFP is never a no-op cast, no need to check
2179 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002180 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002181 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002182}
2183
Dan Gohman2048b852009-11-23 18:04:58 +00002184void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002185 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002186 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002187 EVT DestVT = TLI.getValueType(I.getType());
Dale Johannesen66978ee2009-01-31 02:22:37 +00002188 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002189}
2190
Dan Gohman2048b852009-11-23 18:04:58 +00002191void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002192 // What to do depends on the size of the integer and the size of the pointer.
2193 // We can either truncate, zero extend, or no-op, accordingly.
2194 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002195 EVT SrcVT = N.getValueType();
2196 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002197 SDValue Result = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002198 setValue(&I, Result);
2199}
2200
Dan Gohman2048b852009-11-23 18:04:58 +00002201void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002202 // What to do depends on the size of the integer and the size of the pointer.
2203 // We can either truncate, zero extend, or no-op, accordingly.
2204 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002205 EVT SrcVT = N.getValueType();
2206 EVT DestVT = TLI.getValueType(I.getType());
Duncan Sands3a66a682009-10-13 21:04:12 +00002207 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208}
2209
Dan Gohman2048b852009-11-23 18:04:58 +00002210void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002211 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002212 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002213
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002214 // BitCast assures us that source and destination are the same size so this
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002215 // is either a BIT_CONVERT or a no-op.
2216 if (DestVT != N.getValueType())
Scott Michelfdc40a02009-02-17 22:15:04 +00002217 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002218 DestVT, N)); // convert types
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002219 else
2220 setValue(&I, N); // noop cast.
2221}
2222
Dan Gohman2048b852009-11-23 18:04:58 +00002223void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 SDValue InVec = getValue(I.getOperand(0));
2225 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002226 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002227 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228 getValue(I.getOperand(2)));
2229
Scott Michelfdc40a02009-02-17 22:15:04 +00002230 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002231 TLI.getValueType(I.getType()),
2232 InVec, InVal, InIdx));
2233}
2234
Dan Gohman2048b852009-11-23 18:04:58 +00002235void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002236 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002237 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002238 TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239 getValue(I.getOperand(1)));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002240 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002241 TLI.getValueType(I.getType()), InVec, InIdx));
2242}
2243
Mon P Wangaeb06d22008-11-10 04:46:22 +00002244
2245// Utility for visitShuffleVector - Returns true if the mask is mask starting
2246// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002247static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2248 unsigned MaskNumElts = Mask.size();
2249 for (unsigned i = 0; i != MaskNumElts; ++i)
2250 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002251 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002252 return true;
2253}
2254
Dan Gohman2048b852009-11-23 18:04:58 +00002255void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002256 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002257 SDValue Src1 = getValue(I.getOperand(0));
2258 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002259
Nate Begeman9008ca62009-04-27 18:41:29 +00002260 // Convert the ConstantVector mask operand into an array of ints, with -1
2261 // representing undef values.
2262 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002263 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2264 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002265 unsigned MaskNumElts = MaskElts.size();
2266 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002267 if (isa<UndefValue>(MaskElts[i]))
2268 Mask.push_back(-1);
2269 else
2270 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2271 }
2272
Owen Andersone50ed302009-08-10 22:56:29 +00002273 EVT VT = TLI.getValueType(I.getType());
2274 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002275 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002276
Mon P Wangc7849c22008-11-16 05:06:27 +00002277 if (SrcNumElts == MaskNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002278 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2279 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002280 return;
2281 }
2282
2283 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002284 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2285 // Mask is longer than the source vectors and is a multiple of the source
2286 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002287 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002288 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2289 // The shuffle is concatenating two vectors together.
Scott Michelfdc40a02009-02-17 22:15:04 +00002290 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002291 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002292 return;
2293 }
2294
Mon P Wangc7849c22008-11-16 05:06:27 +00002295 // Pad both vectors with undefs to make them the same length as the mask.
2296 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002297 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2298 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002299 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002300
Nate Begeman9008ca62009-04-27 18:41:29 +00002301 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2302 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002303 MOps1[0] = Src1;
2304 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002305
2306 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2307 getCurDebugLoc(), VT,
2308 &MOps1[0], NumConcat);
2309 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2310 getCurDebugLoc(), VT,
2311 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002312
Mon P Wangaeb06d22008-11-10 04:46:22 +00002313 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002314 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002315 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002316 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002317 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002318 MappedOps.push_back(Idx);
2319 else
2320 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002321 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002322 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2323 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002324 return;
2325 }
2326
Mon P Wangc7849c22008-11-16 05:06:27 +00002327 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002328 // Analyze the access pattern of the vector to see if we can extract
2329 // two subvectors and do the shuffle. The analysis is done by calculating
2330 // the range of elements the mask access on both vectors.
2331 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2332 int MaxRange[2] = {-1, -1};
2333
Nate Begeman5a5ca152009-04-29 05:20:52 +00002334 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002335 int Idx = Mask[i];
2336 int Input = 0;
2337 if (Idx < 0)
2338 continue;
2339
Nate Begeman5a5ca152009-04-29 05:20:52 +00002340 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002341 Input = 1;
2342 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002343 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002344 if (Idx > MaxRange[Input])
2345 MaxRange[Input] = Idx;
2346 if (Idx < MinRange[Input])
2347 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002348 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002349
Mon P Wangc7849c22008-11-16 05:06:27 +00002350 // Check if the access is smaller than the vector size and can we find
2351 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002352 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002353 int StartIdx[2]; // StartIdx to extract from
2354 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002355 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002356 RangeUse[Input] = 0; // Unused
2357 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002358 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002359 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002360 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002361 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002362 RangeUse[Input] = 1; // Extract from beginning of the vector
2363 StartIdx[Input] = 0;
2364 } else {
2365 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002366 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002367 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002368 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002369 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002370 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002371 }
2372
Bill Wendling636e2582009-08-21 18:16:06 +00002373 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002374 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002375 return;
2376 }
2377 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2378 // Extract appropriate subvector and generate a vector shuffle
2379 for (int Input=0; Input < 2; ++Input) {
Mon P Wang230e4fa2008-11-21 04:25:21 +00002380 SDValue& Src = Input == 0 ? Src1 : Src2;
Mon P Wangc7849c22008-11-16 05:06:27 +00002381 if (RangeUse[Input] == 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002382 Src = DAG.getUNDEF(VT);
Mon P Wangc7849c22008-11-16 05:06:27 +00002383 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002384 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002385 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002386 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002387 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002388 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002389 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002390 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002391 int Idx = Mask[i];
2392 if (Idx < 0)
2393 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002394 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002395 MappedOps.push_back(Idx - StartIdx[0]);
2396 else
2397 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002398 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002399 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2400 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002401 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002402 }
2403 }
2404
Mon P Wangc7849c22008-11-16 05:06:27 +00002405 // We can't use either concat vectors or extract subvectors so fall back to
2406 // replacing the shuffle with extract and build vector.
2407 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002408 EVT EltVT = VT.getVectorElementType();
2409 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002410 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002411 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002412 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002413 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002414 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002415 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002416 if (Idx < (int)SrcNumElts)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002417 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002418 EltVT, Src1, DAG.getConstant(Idx, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002419 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002420 Ops.push_back(DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
Scott Michelfdc40a02009-02-17 22:15:04 +00002421 EltVT, Src2,
Mon P Wangc7849c22008-11-16 05:06:27 +00002422 DAG.getConstant(Idx - SrcNumElts, PtrVT)));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002423 }
2424 }
Evan Chenga87008d2009-02-25 22:49:59 +00002425 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2426 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002427}
2428
Dan Gohman2048b852009-11-23 18:04:58 +00002429void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002430 const Value *Op0 = I.getOperand(0);
2431 const Value *Op1 = I.getOperand(1);
2432 const Type *AggTy = I.getType();
2433 const Type *ValTy = Op1->getType();
2434 bool IntoUndef = isa<UndefValue>(Op0);
2435 bool FromUndef = isa<UndefValue>(Op1);
2436
2437 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2438 I.idx_begin(), I.idx_end());
2439
Owen Andersone50ed302009-08-10 22:56:29 +00002440 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002441 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002442 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002443 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2444
2445 unsigned NumAggValues = AggValueVTs.size();
2446 unsigned NumValValues = ValValueVTs.size();
2447 SmallVector<SDValue, 4> Values(NumAggValues);
2448
2449 SDValue Agg = getValue(Op0);
2450 SDValue Val = getValue(Op1);
2451 unsigned i = 0;
2452 // Copy the beginning value(s) from the original aggregate.
2453 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002454 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002455 SDValue(Agg.getNode(), Agg.getResNo() + i);
2456 // Copy values from the inserted value(s).
2457 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002458 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002459 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2460 // Copy remaining value(s) from the original aggregate.
2461 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002462 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002463 SDValue(Agg.getNode(), Agg.getResNo() + i);
2464
Scott Michelfdc40a02009-02-17 22:15:04 +00002465 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002466 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2467 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002468}
2469
Dan Gohman2048b852009-11-23 18:04:58 +00002470void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002471 const Value *Op0 = I.getOperand(0);
2472 const Type *AggTy = Op0->getType();
2473 const Type *ValTy = I.getType();
2474 bool OutOfUndef = isa<UndefValue>(Op0);
2475
2476 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2477 I.idx_begin(), I.idx_end());
2478
Owen Andersone50ed302009-08-10 22:56:29 +00002479 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002480 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2481
2482 unsigned NumValValues = ValValueVTs.size();
2483 SmallVector<SDValue, 4> Values(NumValValues);
2484
2485 SDValue Agg = getValue(Op0);
2486 // Copy out the selected value(s).
2487 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2488 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002489 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002490 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002491 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492
Scott Michelfdc40a02009-02-17 22:15:04 +00002493 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002494 DAG.getVTList(&ValValueVTs[0], NumValValues),
2495 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002496}
2497
2498
Dan Gohman2048b852009-11-23 18:04:58 +00002499void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002500 SDValue N = getValue(I.getOperand(0));
2501 const Type *Ty = I.getOperand(0)->getType();
2502
2503 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2504 OI != E; ++OI) {
2505 Value *Idx = *OI;
2506 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2507 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2508 if (Field) {
2509 // N = N + Offset
2510 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002511 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002512 DAG.getIntPtrConstant(Offset));
2513 }
2514 Ty = StTy->getElementType(Field);
2515 } else {
2516 Ty = cast<SequentialType>(Ty)->getElementType();
2517
2518 // If this is a constant subscript, handle it quickly.
2519 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2520 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002521 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002522 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002523 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002524 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002525 unsigned PtrBits = PTy.getSizeInBits();
Evan Cheng65b52df2009-02-09 21:01:06 +00002526 if (PtrBits < 64) {
2527 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2528 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002529 DAG.getConstant(Offs, MVT::i64));
Evan Cheng65b52df2009-02-09 21:01:06 +00002530 } else
Evan Chengb1032a82009-02-09 20:54:38 +00002531 OffsVal = DAG.getIntPtrConstant(Offs);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002532 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002533 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002534 continue;
2535 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002536
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002537 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002538 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2539 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002540 SDValue IdxN = getValue(Idx);
2541
2542 // If the index is smaller or larger than intptr_t, truncate or extend
2543 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002544 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002545
2546 // If this is a multiply by a power of two, turn it into a shl
2547 // immediately. This is a very common case.
2548 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002549 if (ElementSize.isPowerOf2()) {
2550 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002551 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002552 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002553 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002554 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002555 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002556 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002557 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002558 }
2559 }
2560
Scott Michelfdc40a02009-02-17 22:15:04 +00002561 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002562 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563 }
2564 }
2565 setValue(&I, N);
2566}
2567
Dan Gohman2048b852009-11-23 18:04:58 +00002568void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002569 // If this is a fixed sized alloca in the entry block of the function,
2570 // allocate it statically on the stack.
2571 if (FuncInfo.StaticAllocaMap.count(&I))
2572 return; // getValue will auto-populate this.
2573
2574 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002575 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002576 unsigned Align =
2577 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2578 I.getAlignment());
2579
2580 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002581
2582 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2583 AllocSize,
2584 DAG.getConstant(TySize, AllocSize.getValueType()));
2585
2586
2587
Owen Andersone50ed302009-08-10 22:56:29 +00002588 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002589 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002590
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002591 // Handle alignment. If the requested alignment is less than or equal to
2592 // the stack alignment, ignore it. If the size is greater than or equal to
2593 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2594 unsigned StackAlign =
2595 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2596 if (Align <= StackAlign)
2597 Align = 0;
2598
2599 // Round the size of the allocation up to the stack alignment size
2600 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002601 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002602 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002603 DAG.getIntPtrConstant(StackAlign-1));
2604 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002605 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002606 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002607 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2608
2609 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002610 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002611 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002612 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002613 setValue(&I, DSA);
2614 DAG.setRoot(DSA.getValue(1));
2615
2616 // Inform the Frame Information that we have just allocated a variable-sized
2617 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002618 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002619}
2620
Dan Gohman2048b852009-11-23 18:04:58 +00002621void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002622 const Value *SV = I.getOperand(0);
2623 SDValue Ptr = getValue(SV);
2624
2625 const Type *Ty = I.getType();
2626 bool isVolatile = I.isVolatile();
2627 unsigned Alignment = I.getAlignment();
2628
Owen Andersone50ed302009-08-10 22:56:29 +00002629 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002630 SmallVector<uint64_t, 4> Offsets;
2631 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2632 unsigned NumValues = ValueVTs.size();
2633 if (NumValues == 0)
2634 return;
2635
2636 SDValue Root;
2637 bool ConstantMemory = false;
2638 if (I.isVolatile())
2639 // Serialize volatile loads with other side effects.
2640 Root = getRoot();
2641 else if (AA->pointsToConstantMemory(SV)) {
2642 // Do not serialize (non-volatile) loads of constant memory with anything.
2643 Root = DAG.getEntryNode();
2644 ConstantMemory = true;
2645 } else {
2646 // Do not serialize non-volatile loads against each other.
2647 Root = DAG.getRoot();
2648 }
2649
2650 SmallVector<SDValue, 4> Values(NumValues);
2651 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002652 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002653 for (unsigned i = 0; i != NumValues; ++i) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00002654 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Nate Begemane6798372009-09-15 00:13:12 +00002655 DAG.getNode(ISD::ADD, getCurDebugLoc(),
2656 PtrVT, Ptr,
2657 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002658 SV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002659 Values[i] = L;
2660 Chains[i] = L.getValue(1);
2661 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002663 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002664 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002665 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002666 &Chains[0], NumValues);
2667 if (isVolatile)
2668 DAG.setRoot(Chain);
2669 else
2670 PendingLoads.push_back(Chain);
2671 }
2672
Scott Michelfdc40a02009-02-17 22:15:04 +00002673 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00002674 DAG.getVTList(&ValueVTs[0], NumValues),
2675 &Values[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002676}
2677
2678
Dan Gohman2048b852009-11-23 18:04:58 +00002679void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002680 Value *SrcV = I.getOperand(0);
2681 Value *PtrV = I.getOperand(1);
2682
Owen Andersone50ed302009-08-10 22:56:29 +00002683 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002684 SmallVector<uint64_t, 4> Offsets;
2685 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2686 unsigned NumValues = ValueVTs.size();
2687 if (NumValues == 0)
2688 return;
2689
2690 // Get the lowered operands. Note that we do this after
2691 // checking if NumResults is zero, because with zero results
2692 // the operands won't have values in the map.
2693 SDValue Src = getValue(SrcV);
2694 SDValue Ptr = getValue(PtrV);
2695
2696 SDValue Root = getRoot();
2697 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002698 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002699 bool isVolatile = I.isVolatile();
2700 unsigned Alignment = I.getAlignment();
2701 for (unsigned i = 0; i != NumValues; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00002702 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002703 SDValue(Src.getNode(), Src.getResNo() + i),
Scott Michelfdc40a02009-02-17 22:15:04 +00002704 DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002705 PtrVT, Ptr,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002706 DAG.getConstant(Offsets[i], PtrVT)),
Nate Begeman101b25c2009-09-15 19:05:41 +00002707 PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002708
Scott Michelfdc40a02009-02-17 22:15:04 +00002709 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002710 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711}
2712
2713/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2714/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00002715void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
2716 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002717 bool HasChain = !I.doesNotAccessMemory();
2718 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2719
2720 // Build the operand list.
2721 SmallVector<SDValue, 8> Ops;
2722 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2723 if (OnlyLoad) {
2724 // We don't need to serialize loads against other loads.
2725 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002726 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002727 Ops.push_back(getRoot());
2728 }
2729 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002730
2731 // Info is set by getTgtMemInstrinsic
2732 TargetLowering::IntrinsicInfo Info;
2733 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2734
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002735 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002736 if (!IsTgtIntrinsic)
2737 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002738
2739 // Add all operands of the call to the operand list.
2740 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
2741 SDValue Op = getValue(I.getOperand(i));
2742 assert(TLI.isTypeLegal(Op.getValueType()) &&
2743 "Intrinsic uses a non-legal type?");
2744 Ops.push_back(Op);
2745 }
2746
Owen Andersone50ed302009-08-10 22:56:29 +00002747 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002748 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2749#ifndef NDEBUG
2750 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2751 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2752 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002753 }
Bob Wilson8d919552009-07-31 22:41:21 +00002754#endif // NDEBUG
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002755 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002756 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002757
Bob Wilson8d919552009-07-31 22:41:21 +00002758 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002759
2760 // Create the node.
2761 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002762 if (IsTgtIntrinsic) {
2763 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002764 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002765 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002766 Info.memVT, Info.ptrVal, Info.offset,
2767 Info.align, Info.vol,
2768 Info.readMem, Info.writeMem);
2769 }
2770 else if (!HasChain)
Scott Michelfdc40a02009-02-17 22:15:04 +00002771 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002772 VTs, &Ops[0], Ops.size());
Owen Anderson1d0be152009-08-13 21:58:54 +00002773 else if (I.getType() != Type::getVoidTy(*DAG.getContext()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002774 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002775 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002776 else
Scott Michelfdc40a02009-02-17 22:15:04 +00002777 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002778 VTs, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002779
2780 if (HasChain) {
2781 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2782 if (OnlyLoad)
2783 PendingLoads.push_back(Chain);
2784 else
2785 DAG.setRoot(Chain);
2786 }
Owen Anderson1d0be152009-08-13 21:58:54 +00002787 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002788 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002789 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002790 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002791 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792 setValue(&I, Result);
2793 }
2794}
2795
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002796/// GetSignificand - Get the significand and build it into a floating-point
2797/// number with exponent of 1:
2798///
2799/// Op = (Op & 0x007fffff) | 0x3f800000;
2800///
2801/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002802static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002803GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002804 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2805 DAG.getConstant(0x007fffff, MVT::i32));
2806 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2807 DAG.getConstant(0x3f800000, MVT::i32));
2808 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002809}
2810
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002811/// GetExponent - Get the exponent:
2812///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002813/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002814///
2815/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002816static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002817GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
2818 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002819 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2820 DAG.getConstant(0x7f800000, MVT::i32));
2821 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002822 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002823 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2824 DAG.getConstant(127, MVT::i32));
2825 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002826}
2827
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002828/// getF32Constant - Get 32-bit floating point constant.
2829static SDValue
2830getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002831 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002832}
2833
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002834/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002835/// visitIntrinsicCall: I is a call instruction
2836/// Op is the associated NodeType for I
2837const char *
Dan Gohman2048b852009-11-23 18:04:58 +00002838SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002839 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002840 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002841 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002842 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002843 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002844 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002845 getValue(I.getOperand(2)),
2846 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847 setValue(&I, L);
2848 DAG.setRoot(L.getValue(1));
2849 return 0;
2850}
2851
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002852// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002853const char *
Dan Gohman2048b852009-11-23 18:04:58 +00002854SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002855 SDValue Op1 = getValue(I.getOperand(1));
2856 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002857
Owen Anderson825b72b2009-08-11 20:47:22 +00002858 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00002859 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00002860
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002861 setValue(&I, Result);
2862 return 0;
2863}
Bill Wendling74c37652008-12-09 22:08:41 +00002864
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002865/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2866/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002867void
Dan Gohman2048b852009-11-23 18:04:58 +00002868SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002869 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002870 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002871
Owen Anderson825b72b2009-08-11 20:47:22 +00002872 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002873 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
2874 SDValue Op = getValue(I.getOperand(1));
2875
2876 // Put the exponent in the right bit position for later addition to the
2877 // final result:
2878 //
2879 // #define LOG2OFe 1.4426950f
2880 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002881 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002882 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002883 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002884
2885 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002886 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2887 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002888
2889 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002890 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002891 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002892
2893 if (LimitFloatPrecision <= 6) {
2894 // For floating-point precision of 6:
2895 //
2896 // TwoToFractionalPartOfX =
2897 // 0.997535578f +
2898 // (0.735607626f + 0.252464424f * x) * x;
2899 //
2900 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002901 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002902 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002903 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002904 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002905 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2906 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002907 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002908 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002909
2910 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002911 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002912 TwoToFracPartOfX, IntegerPartOfX);
2913
Owen Anderson825b72b2009-08-11 20:47:22 +00002914 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002915 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2916 // For floating-point precision of 12:
2917 //
2918 // TwoToFractionalPartOfX =
2919 // 0.999892986f +
2920 // (0.696457318f +
2921 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2922 //
2923 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002924 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002925 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002926 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002927 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002928 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2929 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002930 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00002931 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2932 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002933 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00002934 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002935
2936 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002937 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002938 TwoToFracPartOfX, IntegerPartOfX);
2939
Owen Anderson825b72b2009-08-11 20:47:22 +00002940 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002941 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2942 // For floating-point precision of 18:
2943 //
2944 // TwoToFractionalPartOfX =
2945 // 0.999999982f +
2946 // (0.693148872f +
2947 // (0.240227044f +
2948 // (0.554906021e-1f +
2949 // (0.961591928e-2f +
2950 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
2951 //
2952 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002953 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002954 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002955 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002956 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00002957 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2958 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002959 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00002960 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2961 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002962 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00002963 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
2964 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002965 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00002966 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
2967 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002968 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00002969 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
2970 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002971 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00002972 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00002973 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002974
2975 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002976 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002977 TwoToFracPartOfX, IntegerPartOfX);
2978
Owen Anderson825b72b2009-08-11 20:47:22 +00002979 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002980 }
2981 } else {
2982 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002983 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002984 getValue(I.getOperand(1)).getValueType(),
2985 getValue(I.getOperand(1)));
2986 }
2987
Dale Johannesen59e577f2008-09-05 18:38:42 +00002988 setValue(&I, result);
2989}
2990
Bill Wendling39150252008-09-09 20:39:27 +00002991/// visitLog - Lower a log intrinsic. Handles the special sequences for
2992/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002993void
Dan Gohman2048b852009-11-23 18:04:58 +00002994SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002995 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002996 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00002997
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00002999 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3000 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003001 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003002
3003 // Scale the exponent by log(2) [0.69314718f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003004 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003005 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003006 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003007
3008 // Get the significand and build it into a floating-point number with
3009 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003010 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003011
3012 if (LimitFloatPrecision <= 6) {
3013 // For floating-point precision of 6:
3014 //
3015 // LogofMantissa =
3016 // -1.1609546f +
3017 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003018 //
Bill Wendling39150252008-09-09 20:39:27 +00003019 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003020 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003021 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003022 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003023 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003024 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3025 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003026 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003027
Scott Michelfdc40a02009-02-17 22:15:04 +00003028 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003029 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003030 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3031 // For floating-point precision of 12:
3032 //
3033 // LogOfMantissa =
3034 // -1.7417939f +
3035 // (2.8212026f +
3036 // (-1.4699568f +
3037 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3038 //
3039 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003041 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003042 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003043 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003044 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3045 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003046 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003047 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3048 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003049 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003050 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3051 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003052 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003053
Scott Michelfdc40a02009-02-17 22:15:04 +00003054 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003055 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003056 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3057 // For floating-point precision of 18:
3058 //
3059 // LogOfMantissa =
3060 // -2.1072184f +
3061 // (4.2372794f +
3062 // (-3.7029485f +
3063 // (2.2781945f +
3064 // (-0.87823314f +
3065 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3066 //
3067 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003068 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003069 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003070 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003071 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003072 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3073 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003074 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3076 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003077 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003078 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3079 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003080 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003081 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3082 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003083 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003084 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3085 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003086 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003087
Scott Michelfdc40a02009-02-17 22:15:04 +00003088 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003090 }
3091 } else {
3092 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003093 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003094 getValue(I.getOperand(1)).getValueType(),
3095 getValue(I.getOperand(1)));
3096 }
3097
Dale Johannesen59e577f2008-09-05 18:38:42 +00003098 setValue(&I, result);
3099}
3100
Bill Wendling3eb59402008-09-09 00:28:24 +00003101/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3102/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003103void
Dan Gohman2048b852009-11-23 18:04:58 +00003104SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003105 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003106 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003107
Owen Anderson825b72b2009-08-11 20:47:22 +00003108 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003109 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3110 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003111 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003112
Bill Wendling39150252008-09-09 20:39:27 +00003113 // Get the exponent.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003114 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003115
3116 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003117 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003118 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003119
Bill Wendling3eb59402008-09-09 00:28:24 +00003120 // Different possible minimax approximations of significand in
3121 // floating-point for various degrees of accuracy over [1,2].
3122 if (LimitFloatPrecision <= 6) {
3123 // For floating-point precision of 6:
3124 //
3125 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3126 //
3127 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003128 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003129 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003130 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003131 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003132 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3133 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003134 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003135
Scott Michelfdc40a02009-02-17 22:15:04 +00003136 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003137 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003138 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3139 // For floating-point precision of 12:
3140 //
3141 // Log2ofMantissa =
3142 // -2.51285454f +
3143 // (4.07009056f +
3144 // (-2.12067489f +
3145 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003146 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003147 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003148 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003149 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003151 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003152 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3153 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003154 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003155 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3156 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003157 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003158 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3159 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003160 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003161
Scott Michelfdc40a02009-02-17 22:15:04 +00003162 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003163 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003164 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3165 // For floating-point precision of 18:
3166 //
3167 // Log2ofMantissa =
3168 // -3.0400495f +
3169 // (6.1129976f +
3170 // (-5.3420409f +
3171 // (3.2865683f +
3172 // (-1.2669343f +
3173 // (0.27515199f -
3174 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3175 //
3176 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003177 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003178 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003179 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003180 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003181 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3182 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003183 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003184 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3185 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003186 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3188 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003189 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003190 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3191 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003192 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003193 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3194 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003195 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003196
Scott Michelfdc40a02009-02-17 22:15:04 +00003197 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003199 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003200 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003201 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003202 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003203 getValue(I.getOperand(1)).getValueType(),
3204 getValue(I.getOperand(1)));
3205 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003206
Dale Johannesen59e577f2008-09-05 18:38:42 +00003207 setValue(&I, result);
3208}
3209
Bill Wendling3eb59402008-09-09 00:28:24 +00003210/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3211/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003212void
Dan Gohman2048b852009-11-23 18:04:58 +00003213SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003214 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003215 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003216
Owen Anderson825b72b2009-08-11 20:47:22 +00003217 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003218 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3219 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003220 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003221
Bill Wendling39150252008-09-09 20:39:27 +00003222 // Scale the exponent by log10(2) [0.30102999f].
Dale Johannesen66978ee2009-01-31 02:22:37 +00003223 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003224 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003225 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003226
3227 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003228 // exponent of 1.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003229 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003230
3231 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003232 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003233 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003234 // Log10ofMantissa =
3235 // -0.50419619f +
3236 // (0.60948995f - 0.10380950f * x) * x;
3237 //
3238 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003239 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003240 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003241 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003242 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003243 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3244 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003246
Scott Michelfdc40a02009-02-17 22:15:04 +00003247 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003249 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3250 // For floating-point precision of 12:
3251 //
3252 // Log10ofMantissa =
3253 // -0.64831180f +
3254 // (0.91751397f +
3255 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3256 //
3257 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003258 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003259 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003260 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003261 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003262 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3263 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003264 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003265 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3266 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003267 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003268
Scott Michelfdc40a02009-02-17 22:15:04 +00003269 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003270 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003271 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003272 // For floating-point precision of 18:
3273 //
3274 // Log10ofMantissa =
3275 // -0.84299375f +
3276 // (1.5327582f +
3277 // (-1.0688956f +
3278 // (0.49102474f +
3279 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3280 //
3281 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003282 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003283 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003284 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003285 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003286 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3287 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003288 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3290 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003291 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003292 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3293 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003294 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003295 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3296 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003297 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003298
Scott Michelfdc40a02009-02-17 22:15:04 +00003299 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003301 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003302 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003303 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003304 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003305 getValue(I.getOperand(1)).getValueType(),
3306 getValue(I.getOperand(1)));
3307 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003308
Dale Johannesen59e577f2008-09-05 18:38:42 +00003309 setValue(&I, result);
3310}
3311
Bill Wendlinge10c8142008-09-09 22:39:21 +00003312/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3313/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003314void
Dan Gohman2048b852009-11-23 18:04:58 +00003315SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003316 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003317 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003318
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003320 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3321 SDValue Op = getValue(I.getOperand(1));
3322
Owen Anderson825b72b2009-08-11 20:47:22 +00003323 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003324
3325 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003326 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3327 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003328
3329 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003331 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003332
3333 if (LimitFloatPrecision <= 6) {
3334 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003335 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003336 // TwoToFractionalPartOfX =
3337 // 0.997535578f +
3338 // (0.735607626f + 0.252464424f * x) * x;
3339 //
3340 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003342 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003343 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003344 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3346 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003347 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003349 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003350 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003351
Scott Michelfdc40a02009-02-17 22:15:04 +00003352 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003354 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3355 // For floating-point precision of 12:
3356 //
3357 // TwoToFractionalPartOfX =
3358 // 0.999892986f +
3359 // (0.696457318f +
3360 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3361 //
3362 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003363 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003364 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003366 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3368 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003369 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3371 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003372 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003374 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003376
Scott Michelfdc40a02009-02-17 22:15:04 +00003377 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003378 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003379 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3380 // For floating-point precision of 18:
3381 //
3382 // TwoToFractionalPartOfX =
3383 // 0.999999982f +
3384 // (0.693148872f +
3385 // (0.240227044f +
3386 // (0.554906021e-1f +
3387 // (0.961591928e-2f +
3388 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3389 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003390 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003391 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003392 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003393 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003394 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3395 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003396 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003397 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3398 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003399 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3401 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003402 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3404 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003405 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3407 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003408 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003409 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003410 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003412
Scott Michelfdc40a02009-02-17 22:15:04 +00003413 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003415 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003416 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003417 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003418 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003419 getValue(I.getOperand(1)).getValueType(),
3420 getValue(I.getOperand(1)));
3421 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003422
Dale Johannesen601d3c02008-09-05 01:48:15 +00003423 setValue(&I, result);
3424}
3425
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003426/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3427/// limited-precision mode with x == 10.0f.
3428void
Dan Gohman2048b852009-11-23 18:04:58 +00003429SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003430 SDValue result;
3431 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003432 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003433 bool IsExp10 = false;
3434
Owen Anderson825b72b2009-08-11 20:47:22 +00003435 if (getValue(Val).getValueType() == MVT::f32 &&
3436 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003437 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3438 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3439 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3440 APFloat Ten(10.0f);
3441 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3442 }
3443 }
3444 }
3445
3446 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3447 SDValue Op = getValue(I.getOperand(2));
3448
3449 // Put the exponent in the right bit position for later addition to the
3450 // final result:
3451 //
3452 // #define LOG2OF10 3.3219281f
3453 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003454 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003455 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003456 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003457
3458 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003459 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3460 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003461
3462 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003463 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003464 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003465
3466 if (LimitFloatPrecision <= 6) {
3467 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003468 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003469 // twoToFractionalPartOfX =
3470 // 0.997535578f +
3471 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003472 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003473 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003474 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003475 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003477 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003478 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3479 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003480 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003481 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003482 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003484
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003485 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003487 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3488 // For floating-point precision of 12:
3489 //
3490 // TwoToFractionalPartOfX =
3491 // 0.999892986f +
3492 // (0.696457318f +
3493 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3494 //
3495 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003496 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003497 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003499 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003500 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3501 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003502 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003503 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3504 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003505 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003506 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003507 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003509
Scott Michelfdc40a02009-02-17 22:15:04 +00003510 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003512 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3513 // For floating-point precision of 18:
3514 //
3515 // TwoToFractionalPartOfX =
3516 // 0.999999982f +
3517 // (0.693148872f +
3518 // (0.240227044f +
3519 // (0.554906021e-1f +
3520 // (0.961591928e-2f +
3521 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3522 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003524 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003526 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3528 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003529 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3531 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003532 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3534 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003535 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003536 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3537 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003538 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3540 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003541 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003543 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003544 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003545
Scott Michelfdc40a02009-02-17 22:15:04 +00003546 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003547 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003548 }
3549 } else {
3550 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003551 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003552 getValue(I.getOperand(1)).getValueType(),
3553 getValue(I.getOperand(1)),
3554 getValue(I.getOperand(2)));
3555 }
3556
3557 setValue(&I, result);
3558}
3559
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003560/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3561/// we want to emit this as a call to a named external function, return the name
3562/// otherwise lower it and return null.
3563const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003564SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003565 DebugLoc dl = getCurDebugLoc();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003566 switch (Intrinsic) {
3567 default:
3568 // By default, turn this into a target intrinsic node.
3569 visitTargetIntrinsic(I, Intrinsic);
3570 return 0;
3571 case Intrinsic::vastart: visitVAStart(I); return 0;
3572 case Intrinsic::vaend: visitVAEnd(I); return 0;
3573 case Intrinsic::vacopy: visitVACopy(I); return 0;
3574 case Intrinsic::returnaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003575 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003576 getValue(I.getOperand(1))));
3577 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003578 case Intrinsic::frameaddress:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003579 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003580 getValue(I.getOperand(1))));
3581 return 0;
3582 case Intrinsic::setjmp:
3583 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
3584 break;
3585 case Intrinsic::longjmp:
3586 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
3587 break;
Chris Lattner824b9582008-11-21 16:42:48 +00003588 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003589 SDValue Op1 = getValue(I.getOperand(1));
3590 SDValue Op2 = getValue(I.getOperand(2));
3591 SDValue Op3 = getValue(I.getOperand(3));
3592 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003593 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003594 I.getOperand(1), 0, I.getOperand(2), 0));
3595 return 0;
3596 }
Chris Lattner824b9582008-11-21 16:42:48 +00003597 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003598 SDValue Op1 = getValue(I.getOperand(1));
3599 SDValue Op2 = getValue(I.getOperand(2));
3600 SDValue Op3 = getValue(I.getOperand(3));
3601 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Dale Johannesena04b7572009-02-03 23:04:43 +00003602 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003603 I.getOperand(1), 0));
3604 return 0;
3605 }
Chris Lattner824b9582008-11-21 16:42:48 +00003606 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003607 SDValue Op1 = getValue(I.getOperand(1));
3608 SDValue Op2 = getValue(I.getOperand(2));
3609 SDValue Op3 = getValue(I.getOperand(3));
3610 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3611
3612 // If the source and destination are known to not be aliases, we can
3613 // lower memmove as memcpy.
3614 uint64_t Size = -1ULL;
3615 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003616 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003617 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
3618 AliasAnalysis::NoAlias) {
Dale Johannesena04b7572009-02-03 23:04:43 +00003619 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003620 I.getOperand(1), 0, I.getOperand(2), 0));
3621 return 0;
3622 }
3623
Dale Johannesena04b7572009-02-03 23:04:43 +00003624 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003625 I.getOperand(1), 0, I.getOperand(2), 0));
3626 return 0;
3627 }
Devang Patel70d75ca2009-11-12 19:02:56 +00003628 case Intrinsic::dbg_stoppoint:
3629 case Intrinsic::dbg_region_start:
3630 case Intrinsic::dbg_region_end:
3631 case Intrinsic::dbg_func_start:
3632 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003633 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003634 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00003635 if (OptLevel != CodeGenOpt::None)
3636 // FIXME: Variable debug info is not supported here.
3637 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00003638 DwarfWriter *DW = DAG.getDwarfWriter();
3639 if (!DW)
3640 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00003641 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
3642 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
3643 return 0;
3644
Devang Patelac1ceb32009-10-09 22:42:28 +00003645 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00003646 Value *Address = DI.getAddress();
3647 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
3648 Address = BCI->getOperand(0);
3649 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
3650 // Don't handle byval struct arguments or VLAs, for example.
3651 if (!AI)
3652 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003653 DenseMap<const AllocaInst*, int>::iterator SI =
3654 FuncInfo.StaticAllocaMap.find(AI);
3655 if (SI == FuncInfo.StaticAllocaMap.end())
3656 return 0; // VLAs.
3657 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003658
Devang Patelac1ceb32009-10-09 22:42:28 +00003659 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00003660 if (MMI) {
3661 MetadataContext &TheMetadata =
3662 DI.getParent()->getContext().getMetadata();
3663 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
3664 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
3665 MMI->setVariableDbgInfo(Variable, FI, Dbg);
3666 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003667 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003668 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003669 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003670 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00003671 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003672 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003673 SDValue Ops[1];
3674 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003675 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003676 setValue(&I, Op);
3677 DAG.setRoot(Op.getValue(1));
3678 return 0;
3679 }
3680
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003681 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003682 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003683
Chris Lattner3a5815f2009-09-17 23:54:54 +00003684 if (CurMBB->isLandingPad())
3685 AddCatchInfo(I, MMI, CurMBB);
3686 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003687#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003688 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003689#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003690 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3691 unsigned Reg = TLI.getExceptionSelectorRegister();
3692 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003693 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003694
Chris Lattner3a5815f2009-09-17 23:54:54 +00003695 // Insert the EHSELECTION instruction.
3696 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3697 SDValue Ops[2];
3698 Ops[0] = getValue(I.getOperand(1));
3699 Ops[1] = getRoot();
3700 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
3701
3702 DAG.setRoot(Op.getValue(1));
3703
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003704 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003705 return 0;
3706 }
3707
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003708 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003709 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003710
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003711 if (MMI) {
3712 // Find the type id for the given typeinfo.
3713 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
3714
3715 unsigned TypeID = MMI->getTypeIDFor(GV);
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003716 setValue(&I, DAG.getConstant(TypeID, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003717 } else {
3718 // Return something different to eh_selector.
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003719 setValue(&I, DAG.getConstant(1, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003720 }
3721
3722 return 0;
3723 }
3724
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003725 case Intrinsic::eh_return_i32:
3726 case Intrinsic::eh_return_i64:
3727 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003728 MMI->setCallsEHReturn(true);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003729 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003730 MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003731 getControlRoot(),
3732 getValue(I.getOperand(1)),
3733 getValue(I.getOperand(2))));
3734 } else {
3735 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
3736 }
3737
3738 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003739 case Intrinsic::eh_unwind_init:
3740 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
3741 MMI->setCallsUnwindInit(true);
3742 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003743
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003744 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003745
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003746 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00003747 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00003748 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
3749 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003750
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003751 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003752 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003753 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003754 TLI.getPointerTy()),
3755 CfaArg);
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003756 setValue(&I, 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::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003759 TLI.getPointerTy(),
3760 DAG.getConstant(0,
3761 TLI.getPointerTy())),
3762 Offset));
3763 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003764 }
Mon P Wang77cdf302008-11-10 20:54:11 +00003765 case Intrinsic::convertff:
3766 case Intrinsic::convertfsi:
3767 case Intrinsic::convertfui:
3768 case Intrinsic::convertsif:
3769 case Intrinsic::convertuif:
3770 case Intrinsic::convertss:
3771 case Intrinsic::convertsu:
3772 case Intrinsic::convertus:
3773 case Intrinsic::convertuu: {
3774 ISD::CvtCode Code = ISD::CVT_INVALID;
3775 switch (Intrinsic) {
3776 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3777 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3778 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3779 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3780 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3781 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3782 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3783 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3784 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3785 }
Owen Andersone50ed302009-08-10 22:56:29 +00003786 EVT DestVT = TLI.getValueType(I.getType());
Mon P Wang77cdf302008-11-10 20:54:11 +00003787 Value* Op1 = I.getOperand(1);
Dale Johannesena04b7572009-02-03 23:04:43 +00003788 setValue(&I, DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
Mon P Wang77cdf302008-11-10 20:54:11 +00003789 DAG.getValueType(DestVT),
3790 DAG.getValueType(getValue(Op1).getValueType()),
3791 getValue(I.getOperand(2)),
3792 getValue(I.getOperand(3)),
3793 Code));
3794 return 0;
3795 }
3796
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003797 case Intrinsic::sqrt:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003798 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003799 getValue(I.getOperand(1)).getValueType(),
3800 getValue(I.getOperand(1))));
3801 return 0;
3802 case Intrinsic::powi:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003803 setValue(&I, DAG.getNode(ISD::FPOWI, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003804 getValue(I.getOperand(1)).getValueType(),
3805 getValue(I.getOperand(1)),
3806 getValue(I.getOperand(2))));
3807 return 0;
3808 case Intrinsic::sin:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003809 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003810 getValue(I.getOperand(1)).getValueType(),
3811 getValue(I.getOperand(1))));
3812 return 0;
3813 case Intrinsic::cos:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003814 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003815 getValue(I.getOperand(1)).getValueType(),
3816 getValue(I.getOperand(1))));
3817 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003818 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003819 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003820 return 0;
3821 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003822 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003823 return 0;
3824 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003825 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003826 return 0;
3827 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003828 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003829 return 0;
3830 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003831 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003832 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003833 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003834 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003835 return 0;
3836 case Intrinsic::pcmarker: {
3837 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003838 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003839 return 0;
3840 }
3841 case Intrinsic::readcyclecounter: {
3842 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003843 SDValue Tmp = DAG.getNode(ISD::READCYCLECOUNTER, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003844 DAG.getVTList(MVT::i64, MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00003845 &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003846 setValue(&I, Tmp);
3847 DAG.setRoot(Tmp.getValue(1));
3848 return 0;
3849 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003850 case Intrinsic::bswap:
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003851 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003852 getValue(I.getOperand(1)).getValueType(),
3853 getValue(I.getOperand(1))));
3854 return 0;
3855 case Intrinsic::cttz: {
3856 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003857 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003858 SDValue result = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003859 setValue(&I, result);
3860 return 0;
3861 }
3862 case Intrinsic::ctlz: {
3863 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003864 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003865 SDValue result = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003866 setValue(&I, result);
3867 return 0;
3868 }
3869 case Intrinsic::ctpop: {
3870 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00003871 EVT Ty = Arg.getValueType();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003872 SDValue result = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003873 setValue(&I, result);
3874 return 0;
3875 }
3876 case Intrinsic::stacksave: {
3877 SDValue Op = getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003878 SDValue Tmp = DAG.getNode(ISD::STACKSAVE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003879 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003880 setValue(&I, Tmp);
3881 DAG.setRoot(Tmp.getValue(1));
3882 return 0;
3883 }
3884 case Intrinsic::stackrestore: {
3885 SDValue Tmp = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003886 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003887 return 0;
3888 }
Bill Wendling57344502008-11-18 11:01:33 +00003889 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00003890 // Emit code into the DAG to store the stack guard onto the stack.
3891 MachineFunction &MF = DAG.getMachineFunction();
3892 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00003893 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00003894
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00003895 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
3896 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00003897
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00003898 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00003899 MFI->setStackProtectorIndex(FI);
3900
3901 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
3902
3903 // Store the stack protector onto the stack.
Dale Johannesen66978ee2009-01-31 02:22:37 +00003904 SDValue Result = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
Evan Chengff89dcb2009-10-18 18:16:27 +00003905 PseudoSourceValue::getFixedStack(FI),
3906 0, true);
Bill Wendlingb2a42982008-11-06 02:29:10 +00003907 setValue(&I, Result);
3908 DAG.setRoot(Result);
3909 return 0;
3910 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00003911 case Intrinsic::objectsize: {
3912 // If we don't know by now, we're never going to know.
3913 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
3914
3915 assert(CI && "Non-constant type in __builtin_object_size?");
3916
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00003917 SDValue Arg = getValue(I.getOperand(0));
3918 EVT Ty = Arg.getValueType();
3919
Eric Christopher7b5e6172009-10-27 00:52:25 +00003920 if (CI->getZExtValue() < 2)
Mike Stump70e5e682009-11-09 22:28:21 +00003921 setValue(&I, DAG.getConstant(-1ULL, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00003922 else
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00003923 setValue(&I, DAG.getConstant(0, Ty));
Eric Christopher7b5e6172009-10-27 00:52:25 +00003924 return 0;
3925 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003926 case Intrinsic::var_annotation:
3927 // Discard annotate attributes
3928 return 0;
3929
3930 case Intrinsic::init_trampoline: {
3931 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
3932
3933 SDValue Ops[6];
3934 Ops[0] = getRoot();
3935 Ops[1] = getValue(I.getOperand(1));
3936 Ops[2] = getValue(I.getOperand(2));
3937 Ops[3] = getValue(I.getOperand(3));
3938 Ops[4] = DAG.getSrcValue(I.getOperand(1));
3939 Ops[5] = DAG.getSrcValue(F);
3940
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003941 SDValue Tmp = DAG.getNode(ISD::TRAMPOLINE, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
Dan Gohmanfc166572009-04-09 23:54:40 +00003943 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003944
3945 setValue(&I, Tmp);
3946 DAG.setRoot(Tmp.getValue(1));
3947 return 0;
3948 }
3949
3950 case Intrinsic::gcroot:
3951 if (GFI) {
3952 Value *Alloca = I.getOperand(1);
3953 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003954
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003955 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
3956 GFI->addStackRoot(FI->getIndex(), TypeMap);
3957 }
3958 return 0;
3959
3960 case Intrinsic::gcread:
3961 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00003962 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003963 return 0;
3964
3965 case Intrinsic::flt_rounds: {
Owen Anderson825b72b2009-08-11 20:47:22 +00003966 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003967 return 0;
3968 }
3969
3970 case Intrinsic::trap: {
Owen Anderson825b72b2009-08-11 20:47:22 +00003971 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003972 return 0;
3973 }
Bill Wendling7cdc3c82008-11-21 02:03:52 +00003974
Bill Wendlingef375462008-11-21 02:38:44 +00003975 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00003976 return implVisitAluOverflow(I, ISD::UADDO);
3977 case Intrinsic::sadd_with_overflow:
3978 return implVisitAluOverflow(I, ISD::SADDO);
3979 case Intrinsic::usub_with_overflow:
3980 return implVisitAluOverflow(I, ISD::USUBO);
3981 case Intrinsic::ssub_with_overflow:
3982 return implVisitAluOverflow(I, ISD::SSUBO);
3983 case Intrinsic::umul_with_overflow:
3984 return implVisitAluOverflow(I, ISD::UMULO);
3985 case Intrinsic::smul_with_overflow:
3986 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00003987
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003988 case Intrinsic::prefetch: {
3989 SDValue Ops[4];
3990 Ops[0] = getRoot();
3991 Ops[1] = getValue(I.getOperand(1));
3992 Ops[2] = getValue(I.getOperand(2));
3993 Ops[3] = getValue(I.getOperand(3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003994 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003995 return 0;
3996 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003997
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003998 case Intrinsic::memory_barrier: {
3999 SDValue Ops[6];
4000 Ops[0] = getRoot();
4001 for (int x = 1; x < 6; ++x)
4002 Ops[x] = getValue(I.getOperand(x));
4003
Owen Anderson825b72b2009-08-11 20:47:22 +00004004 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004005 return 0;
4006 }
4007 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004008 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004009 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004010 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004011 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4012 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004013 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004014 getValue(I.getOperand(2)),
4015 getValue(I.getOperand(3)),
4016 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004017 setValue(&I, L);
4018 DAG.setRoot(L.getValue(1));
4019 return 0;
4020 }
4021 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004022 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004023 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004024 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004025 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004026 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004027 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004028 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004029 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004030 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004031 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004032 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004033 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004034 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004035 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004036 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004037 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004038 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004039 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004040 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004041 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004042 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004043
4044 case Intrinsic::invariant_start:
4045 case Intrinsic::lifetime_start:
4046 // Discard region information.
4047 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
4048 return 0;
4049 case Intrinsic::invariant_end:
4050 case Intrinsic::lifetime_end:
4051 // Discard region information.
4052 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004053 }
4054}
4055
Dan Gohman98ca4f22009-08-05 01:29:28 +00004056/// Test if the given instruction is in a position to be optimized
4057/// with a tail-call. This roughly means that it's in a block with
4058/// a return and there's nothing that needs to be scheduled
4059/// between it and the return.
4060///
4061/// This function only tests target-independent requirements.
4062/// For target-dependent requirements, a target should override
4063/// TargetLowering::IsEligibleForTailCallOptimization.
4064///
4065static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004066isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004067 const TargetLowering &TLI) {
4068 const BasicBlock *ExitBB = I->getParent();
4069 const TerminatorInst *Term = ExitBB->getTerminator();
4070 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4071 const Function *F = ExitBB->getParent();
4072
4073 // The block must end in a return statement or an unreachable.
4074 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4075
4076 // If I will have a chain, make sure no other instruction that will have a
4077 // chain interposes between I and the return.
4078 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4079 !I->isSafeToSpeculativelyExecute())
4080 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4081 --BBI) {
4082 if (&*BBI == I)
4083 break;
4084 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4085 !BBI->isSafeToSpeculativelyExecute())
4086 return false;
4087 }
4088
4089 // If the block ends with a void return or unreachable, it doesn't matter
4090 // what the call's return type is.
4091 if (!Ret || Ret->getNumOperands() == 0) return true;
4092
Dan Gohmaned9bab32009-11-14 02:06:30 +00004093 // If the return value is undef, it doesn't matter what the call's
4094 // return type is.
4095 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4096
Dan Gohman98ca4f22009-08-05 01:29:28 +00004097 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004098 // the return. Ignore noalias because it doesn't affect the call sequence.
4099 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4100 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004101 return false;
4102
4103 // Otherwise, make sure the unmodified return value of I is the return value.
4104 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4105 U = dyn_cast<Instruction>(U->getOperand(0))) {
4106 if (!U)
4107 return false;
4108 if (!U->hasOneUse())
4109 return false;
4110 if (U == I)
4111 break;
4112 // Check for a truly no-op truncate.
4113 if (isa<TruncInst>(U) &&
4114 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4115 continue;
4116 // Check for a truly no-op bitcast.
4117 if (isa<BitCastInst>(U) &&
4118 (U->getOperand(0)->getType() == U->getType() ||
4119 (isa<PointerType>(U->getOperand(0)->getType()) &&
4120 isa<PointerType>(U->getType()))))
4121 continue;
4122 // Otherwise it's not a true no-op.
4123 return false;
4124 }
4125
4126 return true;
4127}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004128
Dan Gohman2048b852009-11-23 18:04:58 +00004129void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4130 bool isTailCall,
4131 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004132 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4133 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004134 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004135 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4136 unsigned BeginLabel = 0, EndLabel = 0;
4137
4138 TargetLowering::ArgListTy Args;
4139 TargetLowering::ArgListEntry Entry;
4140 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004141
4142 // Check whether the function can return without sret-demotion.
4143 SmallVector<EVT, 4> OutVTs;
4144 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4145 SmallVector<uint64_t, 4> Offsets;
4146 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
4147 OutVTs, OutsFlags, TLI, &Offsets);
4148
4149
4150 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4151 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4152
4153 SDValue DemoteStackSlot;
4154
4155 if (!CanLowerReturn) {
4156 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4157 FTy->getReturnType());
4158 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4159 FTy->getReturnType());
4160 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004161 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004162 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4163
4164 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4165 Entry.Node = DemoteStackSlot;
4166 Entry.Ty = StackSlotPtrType;
4167 Entry.isSExt = false;
4168 Entry.isZExt = false;
4169 Entry.isInReg = false;
4170 Entry.isSRet = true;
4171 Entry.isNest = false;
4172 Entry.isByVal = false;
4173 Entry.Alignment = Align;
4174 Args.push_back(Entry);
4175 RetTy = Type::getVoidTy(FTy->getContext());
4176 }
4177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004178 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004179 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004180 SDValue ArgNode = getValue(*i);
4181 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4182
4183 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004184 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4185 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4186 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4187 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4188 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4189 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004190 Entry.Alignment = CS.getParamAlignment(attrInd);
4191 Args.push_back(Entry);
4192 }
4193
4194 if (LandingPad && MMI) {
4195 // Insert a label before the invoke call to mark the try range. This can be
4196 // used to detect deletion of the invoke via the MachineModuleInfo.
4197 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004198
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004199 // Both PendingLoads and PendingExports must be flushed here;
4200 // this call might not return.
4201 (void)getRoot();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004202 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4203 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004204 }
4205
Dan Gohman98ca4f22009-08-05 01:29:28 +00004206 // Check if target-independent constraints permit a tail call here.
4207 // Target-dependent constraints are checked within TLI.LowerCallTo.
4208 if (isTailCall &&
4209 !isInTailCallPosition(CS.getInstruction(),
4210 CS.getAttributes().getRetAttributes(),
4211 TLI))
4212 isTailCall = false;
4213
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004214 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004215 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004216 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004217 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004218 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004219 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004220 isTailCall,
4221 !CS.getInstruction()->use_empty(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004222 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004223 assert((isTailCall || Result.second.getNode()) &&
4224 "Non-null chain expected with non-tail call!");
4225 assert((Result.second.getNode() || !Result.first.getNode()) &&
4226 "Null value expected with tail call!");
4227 if (Result.first.getNode())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004228 setValue(CS.getInstruction(), Result.first);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004229 else if (!CanLowerReturn && Result.second.getNode()) {
4230 // The instruction result is the result of loading from the
4231 // hidden sret parameter.
4232 SmallVector<EVT, 1> PVTs;
4233 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4234
4235 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4236 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4237 EVT PtrVT = PVTs[0];
4238 unsigned NumValues = OutVTs.size();
4239 SmallVector<SDValue, 4> Values(NumValues);
4240 SmallVector<SDValue, 4> Chains(NumValues);
4241
4242 for (unsigned i = 0; i < NumValues; ++i) {
4243 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
4244 DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, DemoteStackSlot,
4245 DAG.getConstant(Offsets[i], PtrVT)),
4246 NULL, Offsets[i], false, 1);
4247 Values[i] = L;
4248 Chains[i] = L.getValue(1);
4249 }
4250 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4251 MVT::Other, &Chains[0], NumValues);
4252 PendingLoads.push_back(Chain);
4253
4254 setValue(CS.getInstruction(), DAG.getNode(ISD::MERGE_VALUES,
4255 getCurDebugLoc(), DAG.getVTList(&OutVTs[0], NumValues),
4256 &Values[0], NumValues));
4257 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00004258 // As a special case, a null chain means that a tail call has
4259 // been emitted and the DAG root is already updated.
4260 if (Result.second.getNode())
4261 DAG.setRoot(Result.second);
4262 else
4263 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004264
4265 if (LandingPad && MMI) {
4266 // Insert a label at the end of the invoke call to mark the try range. This
4267 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4268 EndLabel = MMI->NextLabelID();
Dale Johannesen8ad9b432009-02-04 01:17:06 +00004269 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4270 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004271
4272 // Inform MachineModuleInfo of range.
4273 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4274 }
4275}
4276
4277
Dan Gohman2048b852009-11-23 18:04:58 +00004278void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004279 const char *RenameFn = 0;
4280 if (Function *F = I.getCalledFunction()) {
4281 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00004282 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
4283 if (II) {
4284 if (unsigned IID = II->getIntrinsicID(F)) {
4285 RenameFn = visitIntrinsicCall(I, IID);
4286 if (!RenameFn)
4287 return;
4288 }
4289 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004290 if (unsigned IID = F->getIntrinsicID()) {
4291 RenameFn = visitIntrinsicCall(I, IID);
4292 if (!RenameFn)
4293 return;
4294 }
4295 }
4296
4297 // Check for well-known libc/libm calls. If the function is internal, it
4298 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004299 if (!F->hasLocalLinkage() && F->hasName()) {
4300 StringRef Name = F->getName();
4301 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004302 if (I.getNumOperands() == 3 && // Basic sanity checks.
4303 I.getOperand(1)->getType()->isFloatingPoint() &&
4304 I.getType() == I.getOperand(1)->getType() &&
4305 I.getType() == I.getOperand(2)->getType()) {
4306 SDValue LHS = getValue(I.getOperand(1));
4307 SDValue RHS = getValue(I.getOperand(2));
Scott Michelfdc40a02009-02-17 22:15:04 +00004308 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004309 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004310 return;
4311 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004312 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004313 if (I.getNumOperands() == 2 && // Basic sanity checks.
4314 I.getOperand(1)->getType()->isFloatingPoint() &&
4315 I.getType() == I.getOperand(1)->getType()) {
4316 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004317 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004318 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004319 return;
4320 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004321 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004322 if (I.getNumOperands() == 2 && // Basic sanity checks.
4323 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004324 I.getType() == I.getOperand(1)->getType() &&
4325 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004326 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004327 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004328 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004329 return;
4330 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004331 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004332 if (I.getNumOperands() == 2 && // Basic sanity checks.
4333 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004334 I.getType() == I.getOperand(1)->getType() &&
4335 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004336 SDValue Tmp = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00004337 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004338 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004339 return;
4340 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004341 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4342 if (I.getNumOperands() == 2 && // Basic sanity checks.
4343 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004344 I.getType() == I.getOperand(1)->getType() &&
4345 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004346 SDValue Tmp = getValue(I.getOperand(1));
4347 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4348 Tmp.getValueType(), Tmp));
4349 return;
4350 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004351 }
4352 }
4353 } else if (isa<InlineAsm>(I.getOperand(0))) {
4354 visitInlineAsm(&I);
4355 return;
4356 }
4357
4358 SDValue Callee;
4359 if (!RenameFn)
4360 Callee = getValue(I.getOperand(0));
4361 else
Bill Wendling056292f2008-09-16 21:48:12 +00004362 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004363
Dan Gohman98ca4f22009-08-05 01:29:28 +00004364 // Check if we can potentially perform a tail call. More detailed
4365 // checking is be done within LowerCallTo, after more information
4366 // about the call is known.
4367 bool isTailCall = PerformTailCallOpt && I.isTailCall();
4368
4369 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004370}
4371
4372
4373/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004374/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004375/// Chain/Flag as the input and updates them for the output Chain/Flag.
4376/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004377SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004378 SDValue &Chain,
4379 SDValue *Flag) const {
4380 // Assemble the legal parts into the final values.
4381 SmallVector<SDValue, 4> Values(ValueVTs.size());
4382 SmallVector<SDValue, 8> Parts;
4383 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4384 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004385 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004386 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004387 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004388
4389 Parts.resize(NumRegs);
4390 for (unsigned i = 0; i != NumRegs; ++i) {
4391 SDValue P;
4392 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004393 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004394 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004395 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004396 *Flag = P.getValue(2);
4397 }
4398 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004399
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004400 // If the source register was virtual and if we know something about it,
4401 // add an assert node.
4402 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4403 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4404 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4405 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4406 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4407 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004408
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004409 unsigned RegSize = RegisterVT.getSizeInBits();
4410 unsigned NumSignBits = LOI.NumSignBits;
4411 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004412
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004413 // FIXME: We capture more information than the dag can represent. For
4414 // now, just use the tightest assertzext/assertsext possible.
4415 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004416 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004417 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004418 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004419 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004420 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004421 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004422 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004423 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004424 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004425 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004426 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004427 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004428 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004429 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004430 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004431 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004432 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004433
Owen Anderson825b72b2009-08-11 20:47:22 +00004434 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004435 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004436 RegisterVT, P, DAG.getValueType(FromVT));
4437
4438 }
4439 }
4440 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004441
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004442 Parts[i] = P;
4443 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004444
Scott Michelfdc40a02009-02-17 22:15:04 +00004445 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004446 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004447 Part += NumRegs;
4448 Parts.clear();
4449 }
4450
Dale Johannesen66978ee2009-01-31 02:22:37 +00004451 return DAG.getNode(ISD::MERGE_VALUES, dl,
Duncan Sandsaaffa052008-12-01 11:41:29 +00004452 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4453 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004454}
4455
4456/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004457/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004458/// Chain/Flag as the input and updates them for the output Chain/Flag.
4459/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004460void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004461 SDValue &Chain, SDValue *Flag) const {
4462 // Get the list of the values's legal parts.
4463 unsigned NumRegs = Regs.size();
4464 SmallVector<SDValue, 8> Parts(NumRegs);
4465 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004466 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004467 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004468 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004469
Dale Johannesen66978ee2009-01-31 02:22:37 +00004470 getCopyToParts(DAG, dl, Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004471 &Parts[Part], NumParts, RegisterVT);
4472 Part += NumParts;
4473 }
4474
4475 // Copy the parts into the registers.
4476 SmallVector<SDValue, 8> Chains(NumRegs);
4477 for (unsigned i = 0; i != NumRegs; ++i) {
4478 SDValue Part;
4479 if (Flag == 0)
Dale Johannesena04b7572009-02-03 23:04:43 +00004480 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004481 else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004482 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004483 *Flag = Part.getValue(1);
4484 }
4485 Chains[i] = Part.getValue(0);
4486 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004487
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004488 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004489 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004490 // flagged to it. That is the CopyToReg nodes and the user are considered
4491 // a single scheduling unit. If we create a TokenFactor and return it as
4492 // chain, then the TokenFactor is both a predecessor (operand) of the
4493 // user as well as a successor (the TF operands are flagged to the user).
4494 // c1, f1 = CopyToReg
4495 // c2, f2 = CopyToReg
4496 // c3 = TokenFactor c1, c2
4497 // ...
4498 // = op c3, ..., f2
4499 Chain = Chains[NumRegs-1];
4500 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004501 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004502}
4503
4504/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004505/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004506/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00004507void RegsForValue::AddInlineAsmOperands(unsigned Code,
4508 bool HasMatching,unsigned MatchingIdx,
4509 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004510 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00004511 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00004512 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
4513 unsigned Flag = Code | (Regs.size() << 3);
4514 if (HasMatching)
4515 Flag |= 0x80000000 | (MatchingIdx << 16);
4516 Ops.push_back(DAG.getTargetConstant(Flag, IntPtrTy));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004517 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004518 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004519 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004520 for (unsigned i = 0; i != NumRegs; ++i) {
4521 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004522 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004523 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004524 }
4525}
4526
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004527/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004528/// i.e. it isn't a stack pointer or some other special register, return the
4529/// register class for the register. Otherwise, return null.
4530static const TargetRegisterClass *
4531isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4532 const TargetLowering &TLI,
4533 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004534 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004535 const TargetRegisterClass *FoundRC = 0;
4536 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4537 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004538 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004539
4540 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004541 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004542 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4543 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4544 I != E; ++I) {
4545 if (TLI.isTypeLegal(*I)) {
4546 // If we have already found this register in a different register class,
4547 // choose the one with the largest VT specified. For example, on
4548 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004549 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004550 ThisVT = *I;
4551 break;
4552 }
4553 }
4554 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004555
Owen Anderson825b72b2009-08-11 20:47:22 +00004556 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004557
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004558 // NOTE: This isn't ideal. In particular, this might allocate the
4559 // frame pointer in functions that need it (due to them not being taken
4560 // out of allocation, because a variable sized allocation hasn't been seen
4561 // yet). This is a slight code pessimization, but should still work.
4562 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4563 E = RC->allocation_order_end(MF); I != E; ++I)
4564 if (*I == Reg) {
4565 // We found a matching register class. Keep looking at others in case
4566 // we find one with larger registers that this physreg is also in.
4567 FoundRC = RC;
4568 FoundVT = ThisVT;
4569 break;
4570 }
4571 }
4572 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004573}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004574
4575
4576namespace llvm {
4577/// AsmOperandInfo - This contains information for each constraint that we are
4578/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004579class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004580 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004581public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004582 /// CallOperand - If this is the result output operand or a clobber
4583 /// this is null, otherwise it is the incoming operand to the CallInst.
4584 /// This gets modified as the asm is processed.
4585 SDValue CallOperand;
4586
4587 /// AssignedRegs - If this is a register or register class operand, this
4588 /// contains the set of register corresponding to the operand.
4589 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004590
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004591 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4592 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4593 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004594
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004595 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4596 /// busy in OutputRegs/InputRegs.
4597 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004598 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004599 std::set<unsigned> &InputRegs,
4600 const TargetRegisterInfo &TRI) const {
4601 if (isOutReg) {
4602 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4603 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4604 }
4605 if (isInReg) {
4606 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4607 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4608 }
4609 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004610
Owen Andersone50ed302009-08-10 22:56:29 +00004611 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004612 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004613 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00004614 EVT getCallOperandValEVT(LLVMContext &Context,
4615 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004616 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004617 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004618
Chris Lattner81249c92008-10-17 17:05:25 +00004619 if (isa<BasicBlock>(CallOperandVal))
4620 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004621
Chris Lattner81249c92008-10-17 17:05:25 +00004622 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004623
Chris Lattner81249c92008-10-17 17:05:25 +00004624 // If this is an indirect operand, the operand is a pointer to the
4625 // accessed type.
4626 if (isIndirect)
4627 OpTy = cast<PointerType>(OpTy)->getElementType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004628
Chris Lattner81249c92008-10-17 17:05:25 +00004629 // If OpTy is not a single value, it may be a struct/union that we
4630 // can tile with integers.
4631 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4632 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4633 switch (BitSize) {
4634 default: break;
4635 case 1:
4636 case 8:
4637 case 16:
4638 case 32:
4639 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004640 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004641 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004642 break;
4643 }
4644 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004645
Chris Lattner81249c92008-10-17 17:05:25 +00004646 return TLI.getValueType(OpTy, true);
4647 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004648
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004649private:
4650 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4651 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004652 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004653 const TargetRegisterInfo &TRI) {
4654 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4655 Regs.insert(Reg);
4656 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4657 for (; *Aliases; ++Aliases)
4658 Regs.insert(*Aliases);
4659 }
4660};
4661} // end llvm namespace.
4662
4663
4664/// GetRegistersForValue - Assign registers (virtual or physical) for the
4665/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004666/// register allocator to handle the assignment process. However, if the asm
4667/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004668/// allocation. This produces generally horrible, but correct, code.
4669///
4670/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004671/// Input and OutputRegs are the set of already allocated physical registers.
4672///
Dan Gohman2048b852009-11-23 18:04:58 +00004673void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004674GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004675 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004677 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004678
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004679 // Compute whether this value requires an input register, an output register,
4680 // or both.
4681 bool isOutReg = false;
4682 bool isInReg = false;
4683 switch (OpInfo.Type) {
4684 case InlineAsm::isOutput:
4685 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004686
4687 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004688 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004689 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004690 break;
4691 case InlineAsm::isInput:
4692 isInReg = true;
4693 isOutReg = false;
4694 break;
4695 case InlineAsm::isClobber:
4696 isOutReg = true;
4697 isInReg = true;
4698 break;
4699 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004700
4701
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004702 MachineFunction &MF = DAG.getMachineFunction();
4703 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004705 // If this is a constraint for a single physreg, or a constraint for a
4706 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004707 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004708 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4709 OpInfo.ConstraintVT);
4710
4711 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004712 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004713 // If this is a FP input in an integer register (or visa versa) insert a bit
4714 // cast of the input value. More generally, handle any case where the input
4715 // value disagrees with the register class we plan to stick this in.
4716 if (OpInfo.Type == InlineAsm::isInput &&
4717 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004718 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004719 // types are identical size, use a bitcast to convert (e.g. two differing
4720 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004721 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004722 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004723 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004724 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004725 OpInfo.ConstraintVT = RegVT;
4726 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4727 // If the input is a FP value and we want it in FP registers, do a
4728 // bitcast to the corresponding integer type. This turns an f64 value
4729 // into i64, which can be passed with two i32 values on a 32-bit
4730 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00004731 RegVT = EVT::getIntegerVT(Context,
4732 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004733 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004734 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004735 OpInfo.ConstraintVT = RegVT;
4736 }
4737 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004738
Owen Anderson23b9b192009-08-12 00:36:31 +00004739 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004740 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004741
Owen Andersone50ed302009-08-10 22:56:29 +00004742 EVT RegVT;
4743 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004744
4745 // If this is a constraint for a specific physical register, like {r17},
4746 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004747 if (unsigned AssignedReg = PhysReg.first) {
4748 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00004749 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004750 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004751
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004752 // Get the actual register value type. This is important, because the user
4753 // may have asked for (e.g.) the AX register in i32 type. We need to
4754 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004755 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004756
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004758 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004759
4760 // If this is an expanded reference, add the rest of the regs to Regs.
4761 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004762 TargetRegisterClass::iterator I = RC->begin();
4763 for (; *I != AssignedReg; ++I)
4764 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004766 // Already added the first reg.
4767 --NumRegs; ++I;
4768 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004769 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004770 Regs.push_back(*I);
4771 }
4772 }
4773 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4774 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4775 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4776 return;
4777 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004778
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004779 // Otherwise, if this was a reference to an LLVM register class, create vregs
4780 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00004781 if (const TargetRegisterClass *RC = PhysReg.second) {
4782 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00004783 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00004784 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004785
Evan Chengfb112882009-03-23 08:01:15 +00004786 // Create the appropriate number of virtual registers.
4787 MachineRegisterInfo &RegInfo = MF.getRegInfo();
4788 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00004789 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004790
Evan Chengfb112882009-03-23 08:01:15 +00004791 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
4792 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004793 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00004794
4795 // This is a reference to a register class that doesn't directly correspond
4796 // to an LLVM register class. Allocate NumRegs consecutive, available,
4797 // registers from the class.
4798 std::vector<unsigned> RegClassRegs
4799 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
4800 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
4803 unsigned NumAllocated = 0;
4804 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
4805 unsigned Reg = RegClassRegs[i];
4806 // See if this register is available.
4807 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
4808 (isInReg && InputRegs.count(Reg))) { // Already used.
4809 // Make sure we find consecutive registers.
4810 NumAllocated = 0;
4811 continue;
4812 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004814 // Check to see if this register is allocatable (i.e. don't give out the
4815 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00004816 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
4817 if (!RC) { // Couldn't allocate this register.
4818 // Reset NumAllocated to make sure we return consecutive registers.
4819 NumAllocated = 0;
4820 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004821 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004822
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004823 // Okay, this register is good, we can use it.
4824 ++NumAllocated;
4825
4826 // If we allocated enough consecutive registers, succeed.
4827 if (NumAllocated == NumRegs) {
4828 unsigned RegStart = (i-NumAllocated)+1;
4829 unsigned RegEnd = i+1;
4830 // Mark all of the allocated registers used.
4831 for (unsigned i = RegStart; i != RegEnd; ++i)
4832 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004833
4834 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004835 OpInfo.ConstraintVT);
4836 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
4837 return;
4838 }
4839 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004840
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004841 // Otherwise, we couldn't allocate enough registers for this.
4842}
4843
Evan Chengda43bcf2008-09-24 00:05:32 +00004844/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
4845/// processed uses a memory 'm' constraint.
4846static bool
4847hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00004848 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00004849 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
4850 InlineAsm::ConstraintInfo &CI = CInfos[i];
4851 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
4852 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
4853 if (CType == TargetLowering::C_Memory)
4854 return true;
4855 }
Chris Lattner6c147292009-04-30 00:48:50 +00004856
4857 // Indirect operand accesses access memory.
4858 if (CI.isIndirect)
4859 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00004860 }
4861
4862 return false;
4863}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004864
4865/// visitInlineAsm - Handle a call to an InlineAsm object.
4866///
Dan Gohman2048b852009-11-23 18:04:58 +00004867void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004868 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
4869
4870 /// ConstraintOperands - Information about all of the constraints.
4871 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004873 std::set<unsigned> OutputRegs, InputRegs;
4874
4875 // Do a prepass over the constraints, canonicalizing them, and building up the
4876 // ConstraintOperands list.
4877 std::vector<InlineAsm::ConstraintInfo>
4878 ConstraintInfos = IA->ParseConstraints();
4879
Evan Chengda43bcf2008-09-24 00:05:32 +00004880 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00004881
4882 SDValue Chain, Flag;
4883
4884 // We won't need to flush pending loads if this asm doesn't touch
4885 // memory and is nonvolatile.
4886 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00004887 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00004888 else
4889 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004890
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004891 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
4892 unsigned ResNo = 0; // ResNo - The result number of the next output.
4893 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4894 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
4895 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004896
Owen Anderson825b72b2009-08-11 20:47:22 +00004897 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004898
4899 // Compute the value type for each operand.
4900 switch (OpInfo.Type) {
4901 case InlineAsm::isOutput:
4902 // Indirect outputs just consume an argument.
4903 if (OpInfo.isIndirect) {
4904 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4905 break;
4906 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004907
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004908 // The return value of the call is this value. As such, there is no
4909 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00004910 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
4911 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004912 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
4913 OpVT = TLI.getValueType(STy->getElementType(ResNo));
4914 } else {
4915 assert(ResNo == 0 && "Asm only has one result!");
4916 OpVT = TLI.getValueType(CS.getType());
4917 }
4918 ++ResNo;
4919 break;
4920 case InlineAsm::isInput:
4921 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
4922 break;
4923 case InlineAsm::isClobber:
4924 // Nothing to do.
4925 break;
4926 }
4927
4928 // If this is an input or an indirect output, process the call argument.
4929 // BasicBlocks are labels, currently appearing only in asm's.
4930 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00004931 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00004932 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
4933
Chris Lattner81249c92008-10-17 17:05:25 +00004934 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00004936 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004937 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004938 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004939
Owen Anderson1d0be152009-08-13 21:58:54 +00004940 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004941 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004943 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004944 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004945
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004946 // Second pass over the constraints: compute which constraint option to use
4947 // and assign registers to constraints that want a specific physreg.
4948 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
4949 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004950
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004951 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00004952 // matching input. If their types mismatch, e.g. one is an integer, the
4953 // other is floating point, or their sizes are different, flag it as an
4954 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004955 if (OpInfo.hasMatchingInput()) {
4956 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
4957 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00004958 if ((OpInfo.ConstraintVT.isInteger() !=
4959 Input.ConstraintVT.isInteger()) ||
4960 (OpInfo.ConstraintVT.getSizeInBits() !=
4961 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00004962 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00004963 " with a matching output constraint of incompatible"
4964 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00004965 }
4966 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00004967 }
4968 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004969
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004970 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00004971 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004972
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004973 // If this is a memory input, and if the operand is not indirect, do what we
4974 // need to to provide an address for the memory input.
4975 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
4976 !OpInfo.isIndirect) {
4977 assert(OpInfo.Type == InlineAsm::isInput &&
4978 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004980 // Memory operands really want the address of the value. If we don't have
4981 // an indirect input, put it in the constpool if we can, otherwise spill
4982 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004983
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004984 // If the operand is a float, integer, or vector constant, spill to a
4985 // constant pool entry to get its address.
4986 Value *OpVal = OpInfo.CallOperandVal;
4987 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
4988 isa<ConstantVector>(OpVal)) {
4989 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
4990 TLI.getPointerTy());
4991 } else {
4992 // Otherwise, create a stack slot and emit a store to it before the
4993 // asm.
4994 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00004995 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004996 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
4997 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004998 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004999 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005000 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005001 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005002 OpInfo.CallOperand = StackSlot;
5003 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005005 // There is no longer a Value* corresponding to this operand.
5006 OpInfo.CallOperandVal = 0;
5007 // It is now an indirect operand.
5008 OpInfo.isIndirect = true;
5009 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005010
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005011 // If this constraint is for a specific register, allocate it before
5012 // anything else.
5013 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005014 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005015 }
5016 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005017
5018
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005019 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005020 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005021 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5022 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005023
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005024 // C_Register operands have already been allocated, Other/Memory don't need
5025 // to be.
5026 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005027 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005028 }
5029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005030 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5031 std::vector<SDValue> AsmNodeOperands;
5032 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5033 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005034 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005035
5036
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005037 // Loop over all of the inputs, copying the operand values into the
5038 // appropriate registers and processing the output regs.
5039 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005041 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5042 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005043
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005044 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5045 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5046
5047 switch (OpInfo.Type) {
5048 case InlineAsm::isOutput: {
5049 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5050 OpInfo.ConstraintType != TargetLowering::C_Register) {
5051 // Memory output, or 'other' output (e.g. 'X' constraint).
5052 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5053
5054 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005055 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5056 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005057 TLI.getPointerTy()));
5058 AsmNodeOperands.push_back(OpInfo.CallOperand);
5059 break;
5060 }
5061
5062 // Otherwise, this is a register or register class output.
5063
5064 // Copy the output from the appropriate register. Find a register that
5065 // we can use.
5066 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005067 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005068 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005069 }
5070
5071 // If this is an indirect operand, store through the pointer after the
5072 // asm.
5073 if (OpInfo.isIndirect) {
5074 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5075 OpInfo.CallOperandVal));
5076 } else {
5077 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005078 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5079 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005080 // Concatenate this output onto the outputs list.
5081 RetValRegs.append(OpInfo.AssignedRegs);
5082 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005083
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005084 // Add information to the INLINEASM node to know that this register is
5085 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005086 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5087 6 /* EARLYCLOBBER REGDEF */ :
5088 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005089 false,
5090 0,
Dale Johannesen913d3df2008-09-12 17:49:03 +00005091 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005092 break;
5093 }
5094 case InlineAsm::isInput: {
5095 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005096
Chris Lattner6bdcda32008-10-17 16:47:46 +00005097 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005098 // If this is required to match an output register we have already set,
5099 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005100 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005102 // Scan until we find the definition we already emitted of this operand.
5103 // When we find it, create a RegsForValue operand.
5104 unsigned CurOp = 2; // The first operand.
5105 for (; OperandNo; --OperandNo) {
5106 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005107 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005108 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005109 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5110 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5111 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005112 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005113 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005114 }
5115
Evan Cheng697cbbf2009-03-20 18:03:34 +00005116 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005117 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005118 if ((OpFlag & 7) == 2 /*REGDEF*/
5119 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5120 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005121 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005122 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005123 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005124 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005125 RegsForValue MatchedRegs;
5126 MatchedRegs.TLI = &TLI;
5127 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005128 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005129 MatchedRegs.RegVTs.push_back(RegVT);
5130 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005131 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005132 i != e; ++i)
5133 MatchedRegs.Regs.
5134 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005135
5136 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005137 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5138 Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005139 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5140 true, OpInfo.getMatchedOperand(),
Evan Cheng697cbbf2009-03-20 18:03:34 +00005141 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005142 break;
5143 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005144 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5145 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5146 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005147 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005148 // See InlineAsm.h isUseOperandTiedToDef.
5149 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005150 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005151 TLI.getPointerTy()));
5152 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5153 break;
5154 }
5155 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005157 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005158 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005159 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005160
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005161 std::vector<SDValue> Ops;
5162 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005163 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005164 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005165 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005166 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005167 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005169 // Add information to the INLINEASM node to know about this input.
5170 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005171 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005172 TLI.getPointerTy()));
5173 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5174 break;
5175 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5176 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5177 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5178 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005180 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005181 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5182 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005183 TLI.getPointerTy()));
5184 AsmNodeOperands.push_back(InOperandVal);
5185 break;
5186 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005187
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005188 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5189 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5190 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005191 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005192 "Don't know how to handle indirect register inputs yet!");
5193
5194 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005195 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005196 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005197 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005198 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005199
Dale Johannesen66978ee2009-01-31 02:22:37 +00005200 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
5201 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005202
Evan Cheng697cbbf2009-03-20 18:03:34 +00005203 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Dale Johannesen86b49f82008-09-24 01:07:17 +00005204 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005205 break;
5206 }
5207 case InlineAsm::isClobber: {
5208 // Add the clobbered value to the operand list, so that the register
5209 // allocator is aware that the physreg got clobbered.
5210 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005211 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Evan Cheng697cbbf2009-03-20 18:03:34 +00005212 false, 0, DAG,AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005213 break;
5214 }
5215 }
5216 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005217
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005218 // Finish up input operands.
5219 AsmNodeOperands[0] = Chain;
5220 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005221
Dale Johannesen66978ee2009-01-31 02:22:37 +00005222 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005223 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005224 &AsmNodeOperands[0], AsmNodeOperands.size());
5225 Flag = Chain.getValue(1);
5226
5227 // If this asm returns a register value, copy the result from that register
5228 // and set it as the value of the call.
5229 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005230 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005231 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005232
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005233 // FIXME: Why don't we do this for inline asms with MRVs?
5234 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005235 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005236
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005237 // If any of the results of the inline asm is a vector, it may have the
5238 // wrong width/num elts. This can happen for register classes that can
5239 // contain multiple different value types. The preg or vreg allocated may
5240 // not have the same VT as was expected. Convert it to the right type
5241 // with bit_convert.
5242 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005243 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005244 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005245
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005246 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005247 ResultType.isInteger() && Val.getValueType().isInteger()) {
5248 // If a result value was tied to an input value, the computed result may
5249 // have a wider width than the expected result. Extract the relevant
5250 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005251 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005252 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005253
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005254 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005255 }
Dan Gohman95915732008-10-18 01:03:45 +00005256
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005257 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005258 // Don't need to use this as a chain in this case.
5259 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5260 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005263 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005264
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005265 // Process indirect outputs, first output all of the flagged copies out of
5266 // physregs.
5267 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5268 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
5269 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005270 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
5271 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005272 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00005273
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005274 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005275
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005276 // Emit the non-flagged stores from the physregs.
5277 SmallVector<SDValue, 8> OutChains;
5278 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i)
Dale Johannesen66978ee2009-01-31 02:22:37 +00005279 OutChains.push_back(DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005280 StoresToEmit[i].first,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005281 getValue(StoresToEmit[i].second),
5282 StoresToEmit[i].second, 0));
5283 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005284 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005285 &OutChains[0], OutChains.size());
5286 DAG.setRoot(Chain);
5287}
5288
Dan Gohman2048b852009-11-23 18:04:58 +00005289void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005290 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005291 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005292 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005293 DAG.getSrcValue(I.getOperand(1))));
5294}
5295
Dan Gohman2048b852009-11-23 18:04:58 +00005296void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005297 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5298 getRoot(), getValue(I.getOperand(0)),
5299 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005300 setValue(&I, V);
5301 DAG.setRoot(V.getValue(1));
5302}
5303
Dan Gohman2048b852009-11-23 18:04:58 +00005304void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005305 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005306 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005307 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005308 DAG.getSrcValue(I.getOperand(1))));
5309}
5310
Dan Gohman2048b852009-11-23 18:04:58 +00005311void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005312 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005313 MVT::Other, getRoot(),
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005314 getValue(I.getOperand(1)),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005315 getValue(I.getOperand(2)),
5316 DAG.getSrcValue(I.getOperand(1)),
5317 DAG.getSrcValue(I.getOperand(2))));
5318}
5319
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005320/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005321/// implementation, which just calls LowerCall.
5322/// FIXME: When all targets are
5323/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005324std::pair<SDValue, SDValue>
5325TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5326 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005327 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005328 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005329 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005330 SDValue Callee,
Dale Johannesen7d2ad622009-01-30 23:10:59 +00005331 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005332
Dan Gohman1937e2f2008-09-16 01:42:28 +00005333 assert((!isTailCall || PerformTailCallOpt) &&
5334 "isTailCall set when tail-call optimizations are disabled!");
5335
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005337 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005338 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005339 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005340 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5341 for (unsigned Value = 0, NumValues = ValueVTs.size();
5342 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005343 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005344 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005345 SDValue Op = SDValue(Args[i].Node.getNode(),
5346 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005347 ISD::ArgFlagsTy Flags;
5348 unsigned OriginalAlignment =
5349 getTargetData()->getABITypeAlignment(ArgTy);
5350
5351 if (Args[i].isZExt)
5352 Flags.setZExt();
5353 if (Args[i].isSExt)
5354 Flags.setSExt();
5355 if (Args[i].isInReg)
5356 Flags.setInReg();
5357 if (Args[i].isSRet)
5358 Flags.setSRet();
5359 if (Args[i].isByVal) {
5360 Flags.setByVal();
5361 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5362 const Type *ElementTy = Ty->getElementType();
5363 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005364 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005365 // For ByVal, alignment should come from FE. BE will guess if this
5366 // info is not there but there are cases it cannot get right.
5367 if (Args[i].Alignment)
5368 FrameAlign = Args[i].Alignment;
5369 Flags.setByValAlign(FrameAlign);
5370 Flags.setByValSize(FrameSize);
5371 }
5372 if (Args[i].isNest)
5373 Flags.setNest();
5374 Flags.setOrigAlign(OriginalAlignment);
5375
Owen Anderson23b9b192009-08-12 00:36:31 +00005376 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5377 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005378 SmallVector<SDValue, 4> Parts(NumParts);
5379 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5380
5381 if (Args[i].isSExt)
5382 ExtendKind = ISD::SIGN_EXTEND;
5383 else if (Args[i].isZExt)
5384 ExtendKind = ISD::ZERO_EXTEND;
5385
Dale Johannesen66978ee2009-01-31 02:22:37 +00005386 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts, PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005387
Dan Gohman98ca4f22009-08-05 01:29:28 +00005388 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005389 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005390 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5391 if (NumParts > 1 && j == 0)
5392 MyFlags.Flags.setSplit();
5393 else if (j != 0)
5394 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005395
Dan Gohman98ca4f22009-08-05 01:29:28 +00005396 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005397 }
5398 }
5399 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005400
Dan Gohman98ca4f22009-08-05 01:29:28 +00005401 // Handle the incoming return values from the call.
5402 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005403 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005404 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005405 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005406 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005407 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5408 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005409 for (unsigned i = 0; i != NumRegs; ++i) {
5410 ISD::InputArg MyFlags;
5411 MyFlags.VT = RegisterVT;
5412 MyFlags.Used = isReturnValueUsed;
5413 if (RetSExt)
5414 MyFlags.Flags.setSExt();
5415 if (RetZExt)
5416 MyFlags.Flags.setZExt();
5417 if (isInreg)
5418 MyFlags.Flags.setInReg();
5419 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005420 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005421 }
5422
Dan Gohman98ca4f22009-08-05 01:29:28 +00005423 // Check if target-dependent constraints permit a tail call here.
5424 // Target-independent constraints should be checked by the caller.
5425 if (isTailCall &&
5426 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
5427 isTailCall = false;
5428
5429 SmallVector<SDValue, 4> InVals;
5430 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
5431 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005432
5433 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005434 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005435 "LowerCall didn't return a valid chain!");
5436 assert((!isTailCall || InVals.empty()) &&
5437 "LowerCall emitted a return value for a tail call!");
5438 assert((isTailCall || InVals.size() == Ins.size()) &&
5439 "LowerCall didn't emit the correct number of values!");
5440 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5441 assert(InVals[i].getNode() &&
5442 "LowerCall emitted a null value!");
5443 assert(Ins[i].VT == InVals[i].getValueType() &&
5444 "LowerCall emitted a value with the wrong type!");
5445 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00005446
5447 // For a tail call, the return value is merely live-out and there aren't
5448 // any nodes in the DAG representing it. Return a special value to
5449 // indicate that a tail call has been emitted and no more Instructions
5450 // should be processed in the current block.
5451 if (isTailCall) {
5452 DAG.setRoot(Chain);
5453 return std::make_pair(SDValue(), SDValue());
5454 }
5455
5456 // Collect the legal value parts into potentially illegal values
5457 // that correspond to the original function's return values.
5458 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5459 if (RetSExt)
5460 AssertOp = ISD::AssertSext;
5461 else if (RetZExt)
5462 AssertOp = ISD::AssertZext;
5463 SmallVector<SDValue, 4> ReturnValues;
5464 unsigned CurReg = 0;
5465 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005466 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005467 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5468 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005469
5470 SDValue ReturnValue =
5471 getCopyFromParts(DAG, dl, &InVals[CurReg], NumRegs, RegisterVT, VT,
5472 AssertOp);
5473 ReturnValues.push_back(ReturnValue);
5474 CurReg += NumRegs;
5475 }
5476
5477 // For a function returning void, there is no return value. We can't create
5478 // such a node, so we just return a null return value in that case. In
5479 // that case, nothing will actualy look at the value.
5480 if (ReturnValues.empty())
5481 return std::make_pair(SDValue(), Chain);
5482
5483 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5484 DAG.getVTList(&RetTys[0], RetTys.size()),
5485 &ReturnValues[0], ReturnValues.size());
5486
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005487 return std::make_pair(Res, Chain);
5488}
5489
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005490void TargetLowering::LowerOperationWrapper(SDNode *N,
5491 SmallVectorImpl<SDValue> &Results,
5492 SelectionDAG &DAG) {
5493 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005494 if (Res.getNode())
5495 Results.push_back(Res);
5496}
5497
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005498SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005499 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005500 return SDValue();
5501}
5502
5503
Dan Gohman2048b852009-11-23 18:04:58 +00005504void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505 SDValue Op = getValue(V);
5506 assert((Op.getOpcode() != ISD::CopyFromReg ||
5507 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5508 "Copy from a reg to the same reg!");
5509 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5510
Owen Anderson23b9b192009-08-12 00:36:31 +00005511 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005512 SDValue Chain = DAG.getEntryNode();
Dale Johannesen66978ee2009-01-31 02:22:37 +00005513 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005514 PendingExports.push_back(Chain);
5515}
5516
5517#include "llvm/CodeGen/SelectionDAGISel.h"
5518
Dan Gohman8c2b5252009-10-30 01:27:03 +00005519void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005520 // If this is the entry block, emit arguments.
5521 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005522 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005523 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005524 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005525 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005526 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005527
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005528 // Check whether the function can return without sret-demotion.
5529 SmallVector<EVT, 4> OutVTs;
5530 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005531 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
5532 OutVTs, OutsFlags, TLI);
5533 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5534
5535 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
5536 OutVTs, OutsFlags, DAG);
5537 if (!FLI.CanLowerReturn) {
5538 // Put in an sret pointer parameter before all the other parameters.
5539 SmallVector<EVT, 1> ValueVTs;
5540 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5541
5542 // NOTE: Assuming that a pointer will never break down to more than one VT
5543 // or one register.
5544 ISD::ArgFlagsTy Flags;
5545 Flags.setSRet();
5546 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5547 ISD::InputArg RetArg(Flags, RegisterVT, true);
5548 Ins.push_back(RetArg);
5549 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005550
Dan Gohman98ca4f22009-08-05 01:29:28 +00005551 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005552 unsigned Idx = 1;
5553 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
5554 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005555 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005556 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5557 bool isArgValueUsed = !I->use_empty();
5558 for (unsigned Value = 0, NumValues = ValueVTs.size();
5559 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005560 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005561 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005562 ISD::ArgFlagsTy Flags;
5563 unsigned OriginalAlignment =
5564 TD->getABITypeAlignment(ArgTy);
5565
5566 if (F.paramHasAttr(Idx, Attribute::ZExt))
5567 Flags.setZExt();
5568 if (F.paramHasAttr(Idx, Attribute::SExt))
5569 Flags.setSExt();
5570 if (F.paramHasAttr(Idx, Attribute::InReg))
5571 Flags.setInReg();
5572 if (F.paramHasAttr(Idx, Attribute::StructRet))
5573 Flags.setSRet();
5574 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5575 Flags.setByVal();
5576 const PointerType *Ty = cast<PointerType>(I->getType());
5577 const Type *ElementTy = Ty->getElementType();
5578 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5579 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5580 // For ByVal, alignment should be passed from FE. BE will guess if
5581 // this info is not there but there are cases it cannot get right.
5582 if (F.getParamAlignment(Idx))
5583 FrameAlign = F.getParamAlignment(Idx);
5584 Flags.setByValAlign(FrameAlign);
5585 Flags.setByValSize(FrameSize);
5586 }
5587 if (F.paramHasAttr(Idx, Attribute::Nest))
5588 Flags.setNest();
5589 Flags.setOrigAlign(OriginalAlignment);
5590
Owen Anderson23b9b192009-08-12 00:36:31 +00005591 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5592 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005593 for (unsigned i = 0; i != NumRegs; ++i) {
5594 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5595 if (NumRegs > 1 && i == 0)
5596 MyFlags.Flags.setSplit();
5597 // if it isn't first piece, alignment must be 1
5598 else if (i > 0)
5599 MyFlags.Flags.setOrigAlign(1);
5600 Ins.push_back(MyFlags);
5601 }
5602 }
5603 }
5604
5605 // Call the target to set up the argument values.
5606 SmallVector<SDValue, 8> InVals;
5607 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5608 F.isVarArg(), Ins,
5609 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005610
5611 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005612 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005613 "LowerFormalArguments didn't return a valid chain!");
5614 assert(InVals.size() == Ins.size() &&
5615 "LowerFormalArguments didn't emit the correct number of values!");
5616 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5617 assert(InVals[i].getNode() &&
5618 "LowerFormalArguments emitted a null value!");
5619 assert(Ins[i].VT == InVals[i].getValueType() &&
5620 "LowerFormalArguments emitted a value with the wrong type!");
5621 });
5622
5623 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005624 DAG.setRoot(NewRoot);
5625
5626 // Set up the argument values.
5627 unsigned i = 0;
5628 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005629 if (!FLI.CanLowerReturn) {
5630 // Create a virtual register for the sret pointer, and put in a copy
5631 // from the sret argument into it.
5632 SmallVector<EVT, 1> ValueVTs;
5633 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5634 EVT VT = ValueVTs[0];
5635 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5636 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5637 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1, RegVT,
5638 VT, AssertOp);
5639
Dan Gohman2048b852009-11-23 18:04:58 +00005640 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005641 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5642 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5643 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00005644 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005645 DAG.setRoot(NewRoot);
5646
5647 // i indexes lowered arguments. Bump it past the hidden sret argument.
5648 // Idx indexes LLVM arguments. Don't touch it.
5649 ++i;
5650 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005651 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
5652 ++I, ++Idx) {
5653 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005654 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005655 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005656 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005657 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005658 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005659 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5660 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005661
5662 if (!I->use_empty()) {
5663 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5664 if (F.paramHasAttr(Idx, Attribute::SExt))
5665 AssertOp = ISD::AssertSext;
5666 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5667 AssertOp = ISD::AssertZext;
5668
5669 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i], NumParts,
5670 PartVT, VT, AssertOp));
5671 }
5672 i += NumParts;
5673 }
5674 if (!I->use_empty()) {
Dan Gohman2048b852009-11-23 18:04:58 +00005675 SDB->setValue(I, DAG.getMergeValues(&ArgValues[0], NumValues,
5676 SDB->getCurDebugLoc()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005677 // If this argument is live outside of the entry block, insert a copy from
5678 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005679 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005680 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005681 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00005682 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005683
5684 // Finally, if the target has anything special to do, allow it to do so.
5685 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00005686 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005687}
5688
5689/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5690/// ensure constants are generated when needed. Remember the virtual registers
5691/// that need to be added to the Machine PHI nodes as input. We cannot just
5692/// directly add them, because expansion might result in multiple MBB's for one
5693/// BB. As such, the start of the BB might correspond to a different MBB than
5694/// the end.
5695///
5696void
5697SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
5698 TerminatorInst *TI = LLVMBB->getTerminator();
5699
5700 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5701
5702 // Check successor nodes' PHI nodes that expect a constant to be available
5703 // from this block.
5704 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5705 BasicBlock *SuccBB = TI->getSuccessor(succ);
5706 if (!isa<PHINode>(SuccBB->begin())) continue;
5707 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005708
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709 // If this terminator has multiple identical successors (common for
5710 // switches), only handle each succ once.
5711 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005712
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005713 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5714 PHINode *PN;
5715
5716 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5717 // nodes and Machine PHI nodes, but the incoming operands have not been
5718 // emitted yet.
5719 for (BasicBlock::iterator I = SuccBB->begin();
5720 (PN = dyn_cast<PHINode>(I)); ++I) {
5721 // Ignore dead phi's.
5722 if (PN->use_empty()) continue;
5723
5724 unsigned Reg;
5725 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5726
5727 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00005728 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005729 if (RegOut == 0) {
5730 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00005731 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005732 }
5733 Reg = RegOut;
5734 } else {
5735 Reg = FuncInfo->ValueMap[PHIOp];
5736 if (Reg == 0) {
5737 assert(isa<AllocaInst>(PHIOp) &&
5738 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5739 "Didn't codegen value into a register!??");
5740 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00005741 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005742 }
5743 }
5744
5745 // Remember that this register needs to added to the machine PHI node as
5746 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00005747 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005748 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5749 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00005750 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00005751 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005752 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00005753 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005754 Reg += NumRegisters;
5755 }
5756 }
5757 }
Dan Gohman2048b852009-11-23 18:04:58 +00005758 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005759}
5760
Dan Gohman3df24e62008-09-03 23:12:08 +00005761/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5762/// supports legal types, and it emits MachineInstrs directly instead of
5763/// creating SelectionDAG nodes.
5764///
5765bool
5766SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
5767 FastISel *F) {
5768 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005769
Dan Gohman3df24e62008-09-03 23:12:08 +00005770 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00005771 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00005772
5773 // Check successor nodes' PHI nodes that expect a constant to be available
5774 // from this block.
5775 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
5776 BasicBlock *SuccBB = TI->getSuccessor(succ);
5777 if (!isa<PHINode>(SuccBB->begin())) continue;
5778 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005779
Dan Gohman3df24e62008-09-03 23:12:08 +00005780 // If this terminator has multiple identical successors (common for
5781 // switches), only handle each succ once.
5782 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005783
Dan Gohman3df24e62008-09-03 23:12:08 +00005784 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
5785 PHINode *PN;
5786
5787 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5788 // nodes and Machine PHI nodes, but the incoming operands have not been
5789 // emitted yet.
5790 for (BasicBlock::iterator I = SuccBB->begin();
5791 (PN = dyn_cast<PHINode>(I)); ++I) {
5792 // Ignore dead phi's.
5793 if (PN->use_empty()) continue;
5794
5795 // Only handle legal types. Two interesting things to note here. First,
5796 // by bailing out early, we may leave behind some dead instructions,
5797 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
5798 // own moves. Second, this check is necessary becuase FastISel doesn't
5799 // use CreateRegForValue to create registers, so it always creates
5800 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00005801 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00005802 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
5803 // Promote MVT::i1.
5804 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00005805 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00005806 else {
Dan Gohman2048b852009-11-23 18:04:58 +00005807 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00005808 return false;
5809 }
Dan Gohman3df24e62008-09-03 23:12:08 +00005810 }
5811
5812 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
5813
5814 unsigned Reg = F->getRegForValue(PHIOp);
5815 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00005816 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00005817 return false;
5818 }
Dan Gohman2048b852009-11-23 18:04:58 +00005819 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00005820 }
5821 }
5822
5823 return true;
5824}