blob: 68a2e7b7c2749f31b76c19df87ad8540ea75f621 [file] [log] [blame]
Dan Gohman2048b852009-11-23 18:04:58 +00001//===-- SelectionDAGBuilder.cpp - Selection-DAG building ------------------===//
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This implements routines for translating from LLVM IR into SelectionDAG IR.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "isel"
Dan Gohman2048b852009-11-23 18:04:58 +000015#include "SelectionDAGBuilder.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000016#include "FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000017#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000018#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000019#include "llvm/Analysis/AliasAnalysis.h"
20#include "llvm/Constants.h"
21#include "llvm/CallingConv.h"
22#include "llvm/DerivedTypes.h"
23#include "llvm/Function.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/InlineAsm.h"
26#include "llvm/Instructions.h"
27#include "llvm/Intrinsics.h"
28#include "llvm/IntrinsicInst.h"
Devang Patel53bb5c92009-11-10 23:06:00 +000029#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000030#include "llvm/Module.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000031#include "llvm/CodeGen/FastISel.h"
32#include "llvm/CodeGen/GCStrategy.h"
33#include "llvm/CodeGen/GCMetadata.h"
34#include "llvm/CodeGen/MachineFunction.h"
35#include "llvm/CodeGen/MachineFrameInfo.h"
36#include "llvm/CodeGen/MachineInstrBuilder.h"
37#include "llvm/CodeGen/MachineJumpTableInfo.h"
38#include "llvm/CodeGen/MachineModuleInfo.h"
39#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000040#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000041#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000042#include "llvm/CodeGen/DwarfWriter.h"
43#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000044#include "llvm/Target/TargetRegisterInfo.h"
45#include "llvm/Target/TargetData.h"
46#include "llvm/Target/TargetFrameInfo.h"
47#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000048#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000049#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000050#include "llvm/Target/TargetOptions.h"
51#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000052#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000053#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000054#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000055#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000056#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000057#include <algorithm>
58using namespace llvm;
59
Dale Johannesen601d3c02008-09-05 01:48:15 +000060/// LimitFloatPrecision - Generate low-precision inline sequences for
61/// some float libcalls (6, 8 or 12 bits).
62static unsigned LimitFloatPrecision;
63
64static cl::opt<unsigned, true>
65LimitFPPrecision("limit-float-precision",
66 cl::desc("Generate low-precision inline sequences "
67 "for some float libcalls"),
68 cl::location(LimitFloatPrecision),
69 cl::init(0));
70
Dan Gohmanf9bd4502009-11-23 17:46:23 +000071namespace {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000072 /// RegsForValue - This struct represents the registers (physical or virtual)
73 /// that a particular set of values is assigned, and the type information about
74 /// the value. The most common situation is to represent one value at a time,
75 /// but struct or array values are handled element-wise as multiple values.
76 /// The splitting of aggregates is performed recursively, so that we never
77 /// have aggregate-typed registers. The values at this point do not necessarily
78 /// have legal types, so each value may require one or more registers of some
79 /// legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000080 ///
Dan Gohmanf9bd4502009-11-23 17:46:23 +000081 struct RegsForValue {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000082 /// TLI - The TargetLowering object.
83 ///
84 const TargetLowering *TLI;
85
86 /// ValueVTs - The value types of the values, which may not be legal, and
87 /// may need be promoted or synthesized from one or more registers.
88 ///
Owen Andersone50ed302009-08-10 22:56:29 +000089 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000091 /// RegVTs - The value types of the registers. This is the same size as
92 /// ValueVTs and it records, for each value, what the type of the assigned
93 /// register or registers are. (Individual values are never synthesized
94 /// from more than one type of register.)
95 ///
96 /// With virtual registers, the contents of RegVTs is redundant with TLI's
97 /// getRegisterType member function, however when with physical registers
98 /// it is necessary to have a separate record of the types.
99 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000100 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000102 /// Regs - This list holds the registers assigned to the values.
103 /// Each legal or promoted value requires one register, and each
104 /// expanded value requires multiple registers.
105 ///
106 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000108 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000109
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000110 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000111 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000112 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000113 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
114 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000115 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000116 const SmallVector<EVT, 4> &regvts,
117 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000118 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000119 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000120 unsigned Reg, const Type *Ty) : TLI(&tli) {
121 ComputeValueVTs(tli, Ty, ValueVTs);
122
123 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000124 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000125 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
126 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000127 for (unsigned i = 0; i != NumRegs; ++i)
128 Regs.push_back(Reg + i);
129 RegVTs.push_back(RegisterVT);
130 Reg += NumRegs;
131 }
132 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000134 /// append - Add the specified values to this one.
135 void append(const RegsForValue &RHS) {
136 TLI = RHS.TLI;
137 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
138 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
139 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
140 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000141
142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000143 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000144 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000145 /// Chain/Flag as the input and updates them for the output Chain/Flag.
146 /// If the Flag pointer is NULL, no flag is used.
Bill Wendlingec72e322009-12-22 01:11:43 +0000147 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl, unsigned Order,
148 SDValue &Chain, SDValue *Flag) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000149
150 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000151 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000152 /// Chain/Flag as the input and updates them for the output Chain/Flag.
153 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000154 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +0000155 unsigned Order, SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000158 /// operand list. This adds the code marker, matching input operand index
159 /// (if applicable), and includes the number of values added into it.
160 void AddInlineAsmOperands(unsigned Code,
161 bool HasMatching, unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +0000162 SelectionDAG &DAG, unsigned Order,
163 std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000164 };
165}
166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000167/// getCopyFromParts - Create a value that contains the specified legal parts
168/// combined into the value they represent. If the parts combine to a type
169/// larger then ValueVT then AssertOp can be used to specify whether the extra
170/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
171/// (ISD::AssertSext).
Bill Wendling3ea3c242009-12-22 02:10:19 +0000172static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl, unsigned Order,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000173 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000174 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000175 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000176 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000177 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000178 SDValue Val = Parts[0];
Bill Wendling3ea3c242009-12-22 02:10:19 +0000179 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000180
181 if (NumParts > 1) {
182 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000183 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000184 unsigned PartBits = PartVT.getSizeInBits();
185 unsigned ValueBits = ValueVT.getSizeInBits();
186
187 // Assemble the power of 2 part.
188 unsigned RoundParts = NumParts & (NumParts - 1) ?
189 1 << Log2_32(NumParts) : NumParts;
190 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000191 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000192 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000193 SDValue Lo, Hi;
194
Owen Anderson23b9b192009-08-12 00:36:31 +0000195 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000196
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000197 if (RoundParts > 2) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000198 Lo = getCopyFromParts(DAG, dl, Order, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000199 PartVT, HalfVT);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000200 Hi = getCopyFromParts(DAG, dl, Order, Parts + RoundParts / 2,
201 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000202 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000203 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
204 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000205 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000206
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000207 if (TLI.isBigEndian())
208 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000209
Dale Johannesen66978ee2009-01-31 02:22:37 +0000210 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000211
Bill Wendling3ea3c242009-12-22 02:10:19 +0000212 if (DisableScheduling) {
213 DAG.AssignOrdering(Lo.getNode(), Order);
214 DAG.AssignOrdering(Hi.getNode(), Order);
215 DAG.AssignOrdering(Val.getNode(), Order);
216 }
217
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000218 if (RoundParts < NumParts) {
219 // Assemble the trailing non-power-of-2 part.
220 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000221 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000222 Hi = getCopyFromParts(DAG, dl, Order,
223 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000224
225 // Combine the round and odd parts.
226 Lo = Val;
227 if (TLI.isBigEndian())
228 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000229 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000230 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000231 if (DisableScheduling) DAG.AssignOrdering(Hi.getNode(), Order);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000232 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000233 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000234 TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000235 if (DisableScheduling) DAG.AssignOrdering(Hi.getNode(), Order);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000236 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000237 if (DisableScheduling) DAG.AssignOrdering(Lo.getNode(), Order);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000238 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000239 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000240 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000241 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000242 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000243 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000244 unsigned NumIntermediates;
245 unsigned NumRegs =
Owen Anderson23b9b192009-08-12 00:36:31 +0000246 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
247 NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000248 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
249 NumParts = NumRegs; // Silence a compiler warning.
250 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
251 assert(RegisterVT == Parts[0].getValueType() &&
252 "Part type doesn't match part!");
253
254 // Assemble the parts into intermediate operands.
255 SmallVector<SDValue, 8> Ops(NumIntermediates);
256 if (NumIntermediates == NumParts) {
257 // If the register was not expanded, truncate or copy the value,
258 // as appropriate.
259 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000260 Ops[i] = getCopyFromParts(DAG, dl, Order, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000261 PartVT, IntermediateVT);
262 } else if (NumParts > 0) {
263 // If the intermediate type was expanded, build the intermediate operands
264 // from the parts.
265 assert(NumParts % NumIntermediates == 0 &&
266 "Must expand into a divisible number of parts!");
267 unsigned Factor = NumParts / NumIntermediates;
268 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000269 Ops[i] = getCopyFromParts(DAG, dl, Order, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000270 PartVT, IntermediateVT);
271 }
272
273 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the intermediate
274 // operands.
275 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000276 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000277 ValueVT, &Ops[0], NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000278 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000279 } else if (PartVT.isFloatingPoint()) {
280 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000282 "Unexpected split");
283 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
285 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000286 if (TLI.isBigEndian())
287 std::swap(Lo, Hi);
288 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000289
290 if (DisableScheduling) {
291 DAG.AssignOrdering(Hi.getNode(), Order);
292 DAG.AssignOrdering(Lo.getNode(), Order);
293 DAG.AssignOrdering(Val.getNode(), Order);
294 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000295 } else {
296 // FP split into integer parts (soft fp)
297 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
298 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000299 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling3ea3c242009-12-22 02:10:19 +0000300 Val = getCopyFromParts(DAG, dl, Order, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000301 }
302 }
303
304 // There is now one part, held in Val. Correct it to match ValueVT.
305 PartVT = Val.getValueType();
306
307 if (PartVT == ValueVT)
308 return Val;
309
310 if (PartVT.isVector()) {
311 assert(ValueVT.isVector() && "Unknown vector conversion!");
Bill Wendling3ea3c242009-12-22 02:10:19 +0000312 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
313 if (DisableScheduling)
314 DAG.AssignOrdering(Res.getNode(), Order);
315 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000316 }
317
318 if (ValueVT.isVector()) {
319 assert(ValueVT.getVectorElementType() == PartVT &&
320 ValueVT.getVectorNumElements() == 1 &&
321 "Only trivial scalar-to-vector conversions should get here!");
Bill Wendling3ea3c242009-12-22 02:10:19 +0000322 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
323 if (DisableScheduling)
324 DAG.AssignOrdering(Res.getNode(), Order);
325 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000326 }
327
328 if (PartVT.isInteger() &&
329 ValueVT.isInteger()) {
330 if (ValueVT.bitsLT(PartVT)) {
331 // For a truncate, see if we have any information to
332 // indicate whether the truncated bits will always be
333 // zero or sign-extension.
334 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000335 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000336 DAG.getValueType(ValueVT));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000337 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
338 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
339 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
340 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 } else {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000342 Val = DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
343 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
344 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000345 }
346 }
347
348 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000349 if (ValueVT.bitsLT(Val.getValueType())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000350 // FP_ROUND's are always exact here.
Bill Wendling3ea3c242009-12-22 02:10:19 +0000351 Val = DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
352 DAG.getIntPtrConstant(1));
353 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
354 return Val;
355 }
356
357 Val = DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
358 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
359 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000360 }
361
Bill Wendling3ea3c242009-12-22 02:10:19 +0000362 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
363 Val = DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
364 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
365 return Val;
366 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000367
Torok Edwinc23197a2009-07-14 16:55:14 +0000368 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000369 return SDValue();
370}
371
372/// getCopyToParts - Create a series of nodes that contain the specified value
373/// split into legal parts. If the parts contain more bits than Val, then, for
374/// integers, ExtendKind can be used to specify how to generate the extra bits.
Bill Wendling3ea3c242009-12-22 02:10:19 +0000375static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, unsigned Order,
376 SDValue Val, SDValue *Parts, unsigned NumParts,
377 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000378 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000379 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000380 EVT PtrVT = TLI.getPointerTy();
381 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000382 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000383 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000384 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
385
386 if (!NumParts)
387 return;
388
389 if (!ValueVT.isVector()) {
390 if (PartVT == ValueVT) {
391 assert(NumParts == 1 && "No-op copy with multiple parts!");
392 Parts[0] = Val;
393 return;
394 }
395
396 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
397 // If the parts cover more bits than the value has, promote the value.
398 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
399 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000400 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000401 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000402 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000403 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000404 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000405 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000406 }
407 } else if (PartBits == ValueVT.getSizeInBits()) {
408 // Different types of the same size.
409 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000410 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000411 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
412 // If the parts cover less bits than value has, truncate the value.
413 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000414 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000415 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000416 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000417 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000418 }
419 }
420
Bill Wendling3ea3c242009-12-22 02:10:19 +0000421 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
422
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000423 // The value may have changed - recompute ValueVT.
424 ValueVT = Val.getValueType();
425 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
426 "Failed to tile the value with PartVT!");
427
428 if (NumParts == 1) {
429 assert(PartVT == ValueVT && "Type conversion failed!");
430 Parts[0] = Val;
431 return;
432 }
433
434 // Expand the value into multiple parts.
435 if (NumParts & (NumParts - 1)) {
436 // The number of parts is not a power of 2. Split off and copy the tail.
437 assert(PartVT.isInteger() && ValueVT.isInteger() &&
438 "Do not know what to expand to!");
439 unsigned RoundParts = 1 << Log2_32(NumParts);
440 unsigned RoundBits = RoundParts * PartBits;
441 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000442 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000443 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000444 TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000445 getCopyToParts(DAG, dl, Order, OddVal, Parts + RoundParts,
446 OddParts, PartVT);
447
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000448 if (TLI.isBigEndian())
449 // The odd parts were reversed by getCopyToParts - unreverse them.
450 std::reverse(Parts + RoundParts, Parts + NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000451
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000452 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000453 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000454 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000455
456 if (DisableScheduling) {
457 DAG.AssignOrdering(OddVal.getNode(), Order);
458 DAG.AssignOrdering(Val.getNode(), Order);
459 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000460 }
461
462 // The number of parts is a power of 2. Repeatedly bisect the value using
463 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000464 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson23b9b192009-08-12 00:36:31 +0000465 EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000466 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000467
468 if (DisableScheduling)
469 DAG.AssignOrdering(Parts[0].getNode(), Order);
470
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000471 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
472 for (unsigned i = 0; i < NumParts; i += StepSize) {
473 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000474 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000475 SDValue &Part0 = Parts[i];
476 SDValue &Part1 = Parts[i+StepSize/2];
477
Scott Michelfdc40a02009-02-17 22:15:04 +0000478 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000479 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000480 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000481 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000482 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000483 DAG.getConstant(0, PtrVT));
484
Bill Wendling3ea3c242009-12-22 02:10:19 +0000485 if (DisableScheduling) {
486 DAG.AssignOrdering(Part0.getNode(), Order);
487 DAG.AssignOrdering(Part1.getNode(), Order);
488 }
489
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000490 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000491 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000492 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000493 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000494 PartVT, Part1);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000495 if (DisableScheduling) {
496 DAG.AssignOrdering(Part0.getNode(), Order);
497 DAG.AssignOrdering(Part1.getNode(), Order);
498 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000499 }
500 }
501 }
502
503 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000504 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000505
506 return;
507 }
508
509 // Vector ValueVT.
510 if (NumParts == 1) {
511 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000512 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000513 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000514 } else {
515 assert(ValueVT.getVectorElementType() == PartVT &&
516 ValueVT.getVectorNumElements() == 1 &&
517 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000518 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000519 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000520 DAG.getConstant(0, PtrVT));
521 }
522 }
523
Bill Wendling3ea3c242009-12-22 02:10:19 +0000524 if (DisableScheduling)
525 DAG.AssignOrdering(Val.getNode(), Order);
526
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000527 Parts[0] = Val;
528 return;
529 }
530
531 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000532 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000533 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000534 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
535 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000536 unsigned NumElements = ValueVT.getVectorNumElements();
537
538 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
539 NumParts = NumRegs; // Silence a compiler warning.
540 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
541
542 // Split the vector into intermediate operands.
543 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000544 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000545 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000546 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000547 IntermediateVT, Val,
548 DAG.getConstant(i * (NumElements / NumIntermediates),
549 PtrVT));
550 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000551 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000552 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000553 DAG.getConstant(i, PtrVT));
554
Bill Wendling3ea3c242009-12-22 02:10:19 +0000555 if (DisableScheduling)
556 DAG.AssignOrdering(Ops[i].getNode(), Order);
557 }
558
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000559 // Split the intermediate operands into legal parts.
560 if (NumParts == NumIntermediates) {
561 // If the register was not expanded, promote or copy the value,
562 // as appropriate.
563 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000564 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000565 } else if (NumParts > 0) {
566 // If the intermediate type was expanded, split each the value into
567 // legal parts.
568 assert(NumParts % NumIntermediates == 0 &&
569 "Must expand into a divisible number of parts!");
570 unsigned Factor = NumParts / NumIntermediates;
571 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000572 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000573 }
574}
575
576
Dan Gohman2048b852009-11-23 18:04:58 +0000577void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000578 AA = &aa;
579 GFI = gfi;
580 TD = DAG.getTarget().getTargetData();
581}
582
583/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000584/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000585/// for a new block. This doesn't clear out information about
586/// additional blocks that are needed to complete switch lowering
587/// or PHI node updating; that information is cleared out as it is
588/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000589void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000590 NodeMap.clear();
591 PendingLoads.clear();
592 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000593 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000594 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000595 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000596 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000597}
598
599/// getRoot - Return the current virtual root of the Selection DAG,
600/// flushing any PendingLoad items. This must be done before emitting
601/// a store or any other node that may need to be ordered after any
602/// prior load instructions.
603///
Dan Gohman2048b852009-11-23 18:04:58 +0000604SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000605 if (PendingLoads.empty())
606 return DAG.getRoot();
607
608 if (PendingLoads.size() == 1) {
609 SDValue Root = PendingLoads[0];
610 DAG.setRoot(Root);
611 PendingLoads.clear();
612 return Root;
613 }
614
615 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000617 &PendingLoads[0], PendingLoads.size());
618 PendingLoads.clear();
619 DAG.setRoot(Root);
620 return Root;
621}
622
623/// getControlRoot - Similar to getRoot, but instead of flushing all the
624/// PendingLoad items, flush all the PendingExports items. It is necessary
625/// to do this before emitting a terminator instruction.
626///
Dan Gohman2048b852009-11-23 18:04:58 +0000627SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000628 SDValue Root = DAG.getRoot();
629
630 if (PendingExports.empty())
631 return Root;
632
633 // Turn all of the CopyToReg chains into one factored node.
634 if (Root.getOpcode() != ISD::EntryToken) {
635 unsigned i = 0, e = PendingExports.size();
636 for (; i != e; ++i) {
637 assert(PendingExports[i].getNode()->getNumOperands() > 1);
638 if (PendingExports[i].getNode()->getOperand(0) == Root)
639 break; // Don't add the root if we already indirectly depend on it.
640 }
641
642 if (i == e)
643 PendingExports.push_back(Root);
644 }
645
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000647 &PendingExports[0],
648 PendingExports.size());
649 PendingExports.clear();
650 DAG.setRoot(Root);
651 return Root;
652}
653
Dan Gohman2048b852009-11-23 18:04:58 +0000654void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000655 visit(I.getOpcode(), I);
656}
657
Dan Gohman2048b852009-11-23 18:04:58 +0000658void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000659 // We're processing a new instruction.
660 ++SDNodeOrder;
661
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000662 // Note: this doesn't use InstVisitor, because it has to work with
663 // ConstantExpr's in addition to instructions.
664 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000665 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000666 // Build the switch statement using the Instruction.def file.
667#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000668 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000669#include "llvm/Instruction.def"
670 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000671}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000672
Dan Gohman2048b852009-11-23 18:04:58 +0000673SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000674 SDValue &N = NodeMap[V];
675 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000676
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000677 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000678 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000679
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000680 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000681 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682
683 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
684 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000686 if (isa<ConstantPointerNull>(C))
687 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000688
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000689 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000690 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000691
Nate Begeman9008ca62009-04-27 18:41:29 +0000692 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000693 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000694
695 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
696 visit(CE->getOpcode(), *CE);
697 SDValue N1 = NodeMap[V];
698 assert(N1.getNode() && "visit didn't populate the ValueMap!");
699 return N1;
700 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000701
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000702 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
703 SmallVector<SDValue, 4> Constants;
704 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
705 OI != OE; ++OI) {
706 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000707 // If the operand is an empty aggregate, there are no values.
708 if (!Val) continue;
709 // Add each leaf value from the operand to the Constants list
710 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000711 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
712 Constants.push_back(SDValue(Val, i));
713 }
Bill Wendling87710f02009-12-21 23:47:40 +0000714
715 SDValue Res = DAG.getMergeValues(&Constants[0], Constants.size(),
716 getCurDebugLoc());
717 if (DisableScheduling)
718 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
719 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000720 }
721
722 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
723 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
724 "Unknown struct or array constant!");
725
Owen Andersone50ed302009-08-10 22:56:29 +0000726 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000727 ComputeValueVTs(TLI, C->getType(), ValueVTs);
728 unsigned NumElts = ValueVTs.size();
729 if (NumElts == 0)
730 return SDValue(); // empty struct
731 SmallVector<SDValue, 4> Constants(NumElts);
732 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000733 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000734 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000735 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000736 else if (EltVT.isFloatingPoint())
737 Constants[i] = DAG.getConstantFP(0, EltVT);
738 else
739 Constants[i] = DAG.getConstant(0, EltVT);
740 }
Bill Wendling87710f02009-12-21 23:47:40 +0000741
742 SDValue Res = DAG.getMergeValues(&Constants[0], NumElts,
743 getCurDebugLoc());
744 if (DisableScheduling)
745 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
746 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000747 }
748
Dan Gohman8c2b5252009-10-30 01:27:03 +0000749 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000750 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000751
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000752 const VectorType *VecTy = cast<VectorType>(V->getType());
753 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000754
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000755 // Now that we know the number and type of the elements, get that number of
756 // elements into the Ops array based on what kind of constant it is.
757 SmallVector<SDValue, 16> Ops;
758 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
759 for (unsigned i = 0; i != NumElements; ++i)
760 Ops.push_back(getValue(CP->getOperand(i)));
761 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000762 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000763 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000764
765 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000766 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000767 Op = DAG.getConstantFP(0, EltVT);
768 else
769 Op = DAG.getConstant(0, EltVT);
770 Ops.assign(NumElements, Op);
771 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000772
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000773 // Create a BUILD_VECTOR node.
Bill Wendling87710f02009-12-21 23:47:40 +0000774 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
775 VT, &Ops[0], Ops.size());
776 if (DisableScheduling)
777 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
778
779 return NodeMap[V] = Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000780 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000781
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000782 // If this is a static alloca, generate it as the frameindex instead of
783 // computation.
784 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
785 DenseMap<const AllocaInst*, int>::iterator SI =
786 FuncInfo.StaticAllocaMap.find(AI);
787 if (SI != FuncInfo.StaticAllocaMap.end())
788 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
789 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000790
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000791 unsigned InReg = FuncInfo.ValueMap[V];
792 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000793
Owen Anderson23b9b192009-08-12 00:36:31 +0000794 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000795 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +0000796 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(),
797 SDNodeOrder, Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000798}
799
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000800/// Get the EVTs and ArgFlags collections that represent the return type
801/// of the given function. This does not require a DAG or a return value, and
802/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000803static void getReturnInfo(const Type* ReturnType,
804 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000805 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000806 TargetLowering &TLI,
807 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000808 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000809 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000810 unsigned NumValues = ValueVTs.size();
811 if ( NumValues == 0 ) return;
812
813 for (unsigned j = 0, f = NumValues; j != f; ++j) {
814 EVT VT = ValueVTs[j];
815 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000816
817 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000818 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000819 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000820 ExtendKind = ISD::ZERO_EXTEND;
821
822 // FIXME: C calling convention requires the return type to be promoted to
823 // at least 32-bit. But this is not necessary for non-C calling
824 // conventions. The frontend should mark functions whose return values
825 // require promoting with signext or zeroext attributes.
826 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000827 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000828 if (VT.bitsLT(MinVT))
829 VT = MinVT;
830 }
831
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000832 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
833 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000834 // 'inreg' on function refers to return value
835 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000836 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000837 Flags.setInReg();
838
839 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000840 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000841 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000842 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000843 Flags.setZExt();
844
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000845 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000846 OutVTs.push_back(PartVT);
847 OutFlags.push_back(Flags);
848 }
849 }
850}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000851
Dan Gohman2048b852009-11-23 18:04:58 +0000852void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000853 SDValue Chain = getControlRoot();
854 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000855 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
856
857 if (!FLI.CanLowerReturn) {
858 unsigned DemoteReg = FLI.DemoteRegister;
859 const Function *F = I.getParent()->getParent();
860
861 // Emit a store of the return value through the virtual register.
862 // Leave Outs empty so that LowerReturn won't try to load return
863 // registers the usual way.
864 SmallVector<EVT, 1> PtrValueVTs;
865 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
866 PtrValueVTs);
867
868 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
869 SDValue RetOp = getValue(I.getOperand(0));
870
Owen Andersone50ed302009-08-10 22:56:29 +0000871 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000872 SmallVector<uint64_t, 4> Offsets;
873 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000874 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000875
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000876 SmallVector<SDValue, 4> Chains(NumValues);
877 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000878 for (unsigned i = 0; i != NumValues; ++i) {
879 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
880 DAG.getConstant(Offsets[i], PtrVT));
881 Chains[i] =
882 DAG.getStore(Chain, getCurDebugLoc(),
883 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
884 Add, NULL, Offsets[i], false, 0);
885
886 if (DisableScheduling) {
887 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
888 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
889 }
890 }
891
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000892 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
893 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000894
895 if (DisableScheduling)
896 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
897 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000898 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
899 SmallVector<EVT, 4> ValueVTs;
900 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
901 unsigned NumValues = ValueVTs.size();
902 if (NumValues == 0) continue;
903
904 SDValue RetOp = getValue(I.getOperand(i));
905 for (unsigned j = 0, f = NumValues; j != f; ++j) {
906 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000907
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000908 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000909
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000910 const Function *F = I.getParent()->getParent();
911 if (F->paramHasAttr(0, Attribute::SExt))
912 ExtendKind = ISD::SIGN_EXTEND;
913 else if (F->paramHasAttr(0, Attribute::ZExt))
914 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000916 // FIXME: C calling convention requires the return type to be promoted to
917 // at least 32-bit. But this is not necessary for non-C calling
918 // conventions. The frontend should mark functions whose return values
919 // require promoting with signext or zeroext attributes.
920 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
921 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
922 if (VT.bitsLT(MinVT))
923 VT = MinVT;
924 }
925
926 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
927 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
928 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000929 getCopyToParts(DAG, getCurDebugLoc(), SDNodeOrder,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000930 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
931 &Parts[0], NumParts, PartVT, ExtendKind);
932
933 // 'inreg' on function refers to return value
934 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
935 if (F->paramHasAttr(0, Attribute::InReg))
936 Flags.setInReg();
937
938 // Propagate extension type if any
939 if (F->paramHasAttr(0, Attribute::SExt))
940 Flags.setSExt();
941 else if (F->paramHasAttr(0, Attribute::ZExt))
942 Flags.setZExt();
943
944 for (unsigned i = 0; i < NumParts; ++i)
945 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000946 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000947 }
948 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000949
950 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000951 CallingConv::ID CallConv =
952 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000953 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
954 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000955
956 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000958 "LowerReturn didn't return a valid chain!");
959
960 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000961 DAG.setRoot(Chain);
Bill Wendling87710f02009-12-21 23:47:40 +0000962
963 if (DisableScheduling)
964 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000965}
966
Dan Gohmanad62f532009-04-23 23:13:24 +0000967/// CopyToExportRegsIfNeeded - If the given value has virtual registers
968/// created for it, emit nodes to copy the value into the virtual
969/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000970void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000971 if (!V->use_empty()) {
972 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
973 if (VMI != FuncInfo.ValueMap.end())
974 CopyValueToVirtualRegister(V, VMI->second);
975 }
976}
977
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000978/// ExportFromCurrentBlock - If this condition isn't known to be exported from
979/// the current basic block, add it to ValueMap now so that we'll get a
980/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000981void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000982 // No need to export constants.
983 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000985 // Already exported?
986 if (FuncInfo.isExportedInst(V)) return;
987
988 unsigned Reg = FuncInfo.InitializeRegForValue(V);
989 CopyValueToVirtualRegister(V, Reg);
990}
991
Dan Gohman2048b852009-11-23 18:04:58 +0000992bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
993 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000994 // The operands of the setcc have to be in this block. We don't know
995 // how to export them from some other block.
996 if (Instruction *VI = dyn_cast<Instruction>(V)) {
997 // Can export from current BB.
998 if (VI->getParent() == FromBB)
999 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001000
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001001 // Is already exported, noop.
1002 return FuncInfo.isExportedInst(V);
1003 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001005 // If this is an argument, we can export it if the BB is the entry block or
1006 // if it is already exported.
1007 if (isa<Argument>(V)) {
1008 if (FromBB == &FromBB->getParent()->getEntryBlock())
1009 return true;
1010
1011 // Otherwise, can only export this if it is already exported.
1012 return FuncInfo.isExportedInst(V);
1013 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001015 // Otherwise, constants can always be exported.
1016 return true;
1017}
1018
1019static bool InBlock(const Value *V, const BasicBlock *BB) {
1020 if (const Instruction *I = dyn_cast<Instruction>(V))
1021 return I->getParent() == BB;
1022 return true;
1023}
1024
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001025/// getFCmpCondCode - Return the ISD condition code corresponding to
1026/// the given LLVM IR floating-point condition code. This includes
1027/// consideration of global floating-point math flags.
1028///
1029static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1030 ISD::CondCode FPC, FOC;
1031 switch (Pred) {
1032 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1033 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1034 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1035 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1036 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1037 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1038 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1039 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1040 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1041 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1042 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1043 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1044 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1045 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1046 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1047 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1048 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001049 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001050 FOC = FPC = ISD::SETFALSE;
1051 break;
1052 }
1053 if (FiniteOnlyFPMath())
1054 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001055 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001056 return FPC;
1057}
1058
1059/// getICmpCondCode - Return the ISD condition code corresponding to
1060/// the given LLVM IR integer condition code.
1061///
1062static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1063 switch (Pred) {
1064 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1065 case ICmpInst::ICMP_NE: return ISD::SETNE;
1066 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1067 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1068 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1069 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1070 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1071 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1072 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1073 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1074 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001075 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001076 return ISD::SETNE;
1077 }
1078}
1079
Dan Gohmanc2277342008-10-17 21:16:08 +00001080/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1081/// This function emits a branch and is used at the leaves of an OR or an
1082/// AND operator tree.
1083///
1084void
Dan Gohman2048b852009-11-23 18:04:58 +00001085SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1086 MachineBasicBlock *TBB,
1087 MachineBasicBlock *FBB,
1088 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001089 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001090
Dan Gohmanc2277342008-10-17 21:16:08 +00001091 // If the leaf of the tree is a comparison, merge the condition into
1092 // the caseblock.
1093 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1094 // The operands of the cmp have to be in this block. We don't know
1095 // how to export them from some other block. If this is the first block
1096 // of the sequence, no exporting is needed.
1097 if (CurBB == CurMBB ||
1098 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1099 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001100 ISD::CondCode Condition;
1101 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001102 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001103 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001104 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001105 } else {
1106 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001107 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001108 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001109
1110 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001111 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1112 SwitchCases.push_back(CB);
1113 return;
1114 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001115 }
1116
1117 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001118 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001119 NULL, TBB, FBB, CurBB);
1120 SwitchCases.push_back(CB);
1121}
1122
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001123/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001124void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1125 MachineBasicBlock *TBB,
1126 MachineBasicBlock *FBB,
1127 MachineBasicBlock *CurBB,
1128 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001129 // If this node is not part of the or/and tree, emit it as a branch.
1130 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001131 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001132 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1133 BOp->getParent() != CurBB->getBasicBlock() ||
1134 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1135 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1136 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001137 return;
1138 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001139
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001140 // Create TmpBB after CurBB.
1141 MachineFunction::iterator BBI = CurBB;
1142 MachineFunction &MF = DAG.getMachineFunction();
1143 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1144 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001146 if (Opc == Instruction::Or) {
1147 // Codegen X | Y as:
1148 // jmp_if_X TBB
1149 // jmp TmpBB
1150 // TmpBB:
1151 // jmp_if_Y TBB
1152 // jmp FBB
1153 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155 // Emit the LHS condition.
1156 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 // Emit the RHS condition into TmpBB.
1159 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1160 } else {
1161 assert(Opc == Instruction::And && "Unknown merge op!");
1162 // Codegen X & Y as:
1163 // jmp_if_X TmpBB
1164 // jmp FBB
1165 // TmpBB:
1166 // jmp_if_Y TBB
1167 // jmp FBB
1168 //
1169 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001170
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001171 // Emit the LHS condition.
1172 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001174 // Emit the RHS condition into TmpBB.
1175 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1176 }
1177}
1178
1179/// If the set of cases should be emitted as a series of branches, return true.
1180/// If we should emit this as a bunch of and/or'd together conditions, return
1181/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001182bool
Dan Gohman2048b852009-11-23 18:04:58 +00001183SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001184 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001186 // If this is two comparisons of the same values or'd or and'd together, they
1187 // will get folded into a single comparison, so don't emit two blocks.
1188 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1189 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1190 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1191 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1192 return false;
1193 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001195 return true;
1196}
1197
Dan Gohman2048b852009-11-23 18:04:58 +00001198void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001199 // Update machine-CFG edges.
1200 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1201
1202 // Figure out which block is immediately after the current one.
1203 MachineBasicBlock *NextBlock = 0;
1204 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001205 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001206 NextBlock = BBI;
1207
1208 if (I.isUnconditional()) {
1209 // Update machine-CFG edges.
1210 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001211
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001213 if (Succ0MBB != NextBlock) {
1214 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001216 DAG.getBasicBlock(Succ0MBB));
1217 DAG.setRoot(V);
1218
1219 if (DisableScheduling)
1220 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1221 }
1222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 return;
1224 }
1225
1226 // If this condition is one of the special cases we handle, do special stuff
1227 // now.
1228 Value *CondVal = I.getCondition();
1229 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1230
1231 // If this is a series of conditions that are or'd or and'd together, emit
1232 // this as a sequence of branches instead of setcc's with and/or operations.
1233 // For example, instead of something like:
1234 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001235 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001237 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001238 // or C, F
1239 // jnz foo
1240 // Emit:
1241 // cmp A, B
1242 // je foo
1243 // cmp D, E
1244 // jle foo
1245 //
1246 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001247 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001248 (BOp->getOpcode() == Instruction::And ||
1249 BOp->getOpcode() == Instruction::Or)) {
1250 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1251 // If the compares in later blocks need to use values not currently
1252 // exported from this block, export them now. This block should always
1253 // be the first entry.
1254 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001255
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001256 // Allow some cases to be rejected.
1257 if (ShouldEmitAsBranches(SwitchCases)) {
1258 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1259 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1260 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1261 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 // Emit the branch for this block.
1264 visitSwitchCase(SwitchCases[0]);
1265 SwitchCases.erase(SwitchCases.begin());
1266 return;
1267 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001268
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001269 // Okay, we decided not to do this, remove any inserted MBB's and clear
1270 // SwitchCases.
1271 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001272 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001273
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001274 SwitchCases.clear();
1275 }
1276 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001279 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001280 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001282 // Use visitSwitchCase to actually insert the fast branch sequence for this
1283 // cond branch.
1284 visitSwitchCase(CB);
1285}
1286
1287/// visitSwitchCase - Emits the necessary code to represent a single node in
1288/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001289void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001290 SDValue Cond;
1291 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001292 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001293
1294 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001295 if (CB.CmpMHS == NULL) {
1296 // Fold "(X == true)" to X and "(X == false)" to !X to
1297 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001298 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001299 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001300 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001301 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001302 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001304 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001305 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001306 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001307 } else {
1308 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1309
Anton Korobeynikov23218582008-12-23 22:25:27 +00001310 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1311 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001312
1313 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001314 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315
1316 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001318 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001319 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001320 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001321 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001322 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001323 DAG.getConstant(High-Low, VT), ISD::SETULE);
1324 }
1325 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001326
Bill Wendling87710f02009-12-21 23:47:40 +00001327 if (DisableScheduling)
1328 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330 // Update successor info
1331 CurMBB->addSuccessor(CB.TrueBB);
1332 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 // Set NextBlock to be the MBB immediately after the current one, if any.
1335 // This is used to avoid emitting unnecessary branches to the next block.
1336 MachineBasicBlock *NextBlock = 0;
1337 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001338 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001339 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001340
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001341 // If the lhs block is the next block, invert the condition so that we can
1342 // fall through to the lhs instead of the rhs block.
1343 if (CB.TrueBB == NextBlock) {
1344 std::swap(CB.TrueBB, CB.FalseBB);
1345 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001346 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001347
1348 if (DisableScheduling)
1349 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001350 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001351
Dale Johannesenf5d97892009-02-04 01:48:28 +00001352 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001353 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001354 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001355
Bill Wendling87710f02009-12-21 23:47:40 +00001356 if (DisableScheduling)
1357 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1358
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001359 // If the branch was constant folded, fix up the CFG.
1360 if (BrCond.getOpcode() == ISD::BR) {
1361 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001362 } else {
1363 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001364 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001366
Bill Wendling87710f02009-12-21 23:47:40 +00001367 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001368 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1369 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001370
1371 if (DisableScheduling)
1372 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1373 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001374 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001375
1376 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377}
1378
1379/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001380void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001381 // Emit the code for the jump table
1382 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001383 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001384 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1385 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001387 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1388 MVT::Other, Index.getValue(1),
1389 Table, Index);
1390 DAG.setRoot(BrJumpTable);
1391
Bill Wendling87710f02009-12-21 23:47:40 +00001392 if (DisableScheduling) {
1393 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1394 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001395 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001396 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001397}
1398
1399/// visitJumpTableHeader - This function emits necessary code to produce index
1400/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001401void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1402 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001403 // Subtract the lowest switch case value from the value being switched on and
1404 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001405 // difference between smallest and largest cases.
1406 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001407 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001408 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001409 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001410
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001411 // The SDNode we just created, which holds the value being switched on minus
1412 // the the smallest case value, needs to be copied to a virtual register so it
1413 // can be used as an index into the jump table in a subsequent basic block.
1414 // This value may be smaller or larger than the target's pointer type, and
1415 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001416 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001417
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001418 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001419 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1420 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421 JT.Reg = JumpTableReg;
1422
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001423 // Emit the range check for the jump table, and branch to the default block
1424 // for the switch statement if the value being switched on exceeds the largest
1425 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001426 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001427 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001428 DAG.getConstant(JTH.Last-JTH.First,VT),
1429 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430
Bill Wendling87710f02009-12-21 23:47:40 +00001431 if (DisableScheduling) {
1432 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1433 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1434 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1435 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1436 }
1437
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001438 // Set NextBlock to be the MBB immediately after the current one, if any.
1439 // This is used to avoid emitting unnecessary branches to the next block.
1440 MachineBasicBlock *NextBlock = 0;
1441 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001442
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001443 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001444 NextBlock = BBI;
1445
Dale Johannesen66978ee2009-01-31 02:22:37 +00001446 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001447 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001448 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449
Bill Wendling87710f02009-12-21 23:47:40 +00001450 if (DisableScheduling)
1451 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1452
1453 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001454 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1455 DAG.getBasicBlock(JT.MBB));
1456
Bill Wendling87710f02009-12-21 23:47:40 +00001457 if (DisableScheduling)
1458 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1459 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001460
Bill Wendling87710f02009-12-21 23:47:40 +00001461 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001462}
1463
1464/// visitBitTestHeader - This function emits necessary code to produce value
1465/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001466void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001467 // Subtract the minimum value
1468 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001469 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001470 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001471 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001472
1473 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001474 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001475 TLI.getSetCCResultType(Sub.getValueType()),
1476 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001477 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001478
Bill Wendling87710f02009-12-21 23:47:40 +00001479 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1480 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001481
Duncan Sands92abc622009-01-31 15:50:11 +00001482 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001483 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1484 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485
Bill Wendling87710f02009-12-21 23:47:40 +00001486 if (DisableScheduling) {
1487 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1488 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1489 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1490 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1491 }
1492
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001493 // Set NextBlock to be the MBB immediately after the current one, if any.
1494 // This is used to avoid emitting unnecessary branches to the next block.
1495 MachineBasicBlock *NextBlock = 0;
1496 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001497 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001498 NextBlock = BBI;
1499
1500 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1501
1502 CurMBB->addSuccessor(B.Default);
1503 CurMBB->addSuccessor(MBB);
1504
Dale Johannesen66978ee2009-01-31 02:22:37 +00001505 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001506 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001507 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001508
Bill Wendling87710f02009-12-21 23:47:40 +00001509 if (DisableScheduling)
1510 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1511
1512 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001513 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1514 DAG.getBasicBlock(MBB));
1515
Bill Wendling87710f02009-12-21 23:47:40 +00001516 if (DisableScheduling)
1517 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1518 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001519
Bill Wendling87710f02009-12-21 23:47:40 +00001520 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001521}
1522
1523/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001524void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1525 unsigned Reg,
1526 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001527 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001528 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001529 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001530 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001531 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001532 DAG.getConstant(1, TLI.getPointerTy()),
1533 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001534
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001535 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001536 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001537 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001538 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001539 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1540 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001541 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001542 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001543
Bill Wendling87710f02009-12-21 23:47:40 +00001544 if (DisableScheduling) {
1545 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1546 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1547 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1548 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1549 }
1550
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 CurMBB->addSuccessor(B.TargetBB);
1552 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001553
Dale Johannesen66978ee2009-01-31 02:22:37 +00001554 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001555 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001556 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001557
Bill Wendling87710f02009-12-21 23:47:40 +00001558 if (DisableScheduling)
1559 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1560
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001561 // Set NextBlock to be the MBB immediately after the current one, if any.
1562 // This is used to avoid emitting unnecessary branches to the next block.
1563 MachineBasicBlock *NextBlock = 0;
1564 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001565 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001566 NextBlock = BBI;
1567
Bill Wendling87710f02009-12-21 23:47:40 +00001568 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001569 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1570 DAG.getBasicBlock(NextMBB));
1571
Bill Wendling87710f02009-12-21 23:47:40 +00001572 if (DisableScheduling)
1573 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1574 }
Bill Wendling0777e922009-12-21 21:59:52 +00001575
Bill Wendling87710f02009-12-21 23:47:40 +00001576 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001577}
1578
Dan Gohman2048b852009-11-23 18:04:58 +00001579void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 // Retrieve successors.
1581 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1582 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1583
Gabor Greifb67e6b32009-01-15 11:10:44 +00001584 const Value *Callee(I.getCalledValue());
1585 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001586 visitInlineAsm(&I);
1587 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001588 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001589
1590 // If the value of the invoke is used outside of its defining block, make it
1591 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001592 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593
1594 // Update successor info
1595 CurMBB->addSuccessor(Return);
1596 CurMBB->addSuccessor(LandingPad);
1597
1598 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001599 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1600 MVT::Other, getControlRoot(),
1601 DAG.getBasicBlock(Return));
1602 DAG.setRoot(Branch);
1603
1604 if (DisableScheduling)
1605 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001606}
1607
Dan Gohman2048b852009-11-23 18:04:58 +00001608void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609}
1610
1611/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1612/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001613bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1614 CaseRecVector& WorkList,
1615 Value* SV,
1616 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001617 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001620 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001621 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001622 return false;
1623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624 // Get the MachineFunction which holds the current MBB. This is used when
1625 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001626 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001627
1628 // Figure out which block is immediately after the current one.
1629 MachineBasicBlock *NextBlock = 0;
1630 MachineFunction::iterator BBI = CR.CaseBB;
1631
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001632 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001633 NextBlock = BBI;
1634
1635 // TODO: If any two of the cases has the same destination, and if one value
1636 // is the same as the other, but has one bit unset that the other has set,
1637 // use bit manipulation to do two compares at once. For example:
1638 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001639
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640 // Rearrange the case blocks so that the last one falls through if possible.
1641 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1642 // The last case block won't fall through into 'NextBlock' if we emit the
1643 // branches in this order. See if rearranging a case value would help.
1644 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1645 if (I->BB == NextBlock) {
1646 std::swap(*I, BackCase);
1647 break;
1648 }
1649 }
1650 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001652 // Create a CaseBlock record representing a conditional branch to
1653 // the Case's target mbb if the value being switched on SV is equal
1654 // to C.
1655 MachineBasicBlock *CurBlock = CR.CaseBB;
1656 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1657 MachineBasicBlock *FallThrough;
1658 if (I != E-1) {
1659 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1660 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001661
1662 // Put SV in a virtual register to make it available from the new blocks.
1663 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001664 } else {
1665 // If the last case doesn't match, go to the default block.
1666 FallThrough = Default;
1667 }
1668
1669 Value *RHS, *LHS, *MHS;
1670 ISD::CondCode CC;
1671 if (I->High == I->Low) {
1672 // This is just small small case range :) containing exactly 1 case
1673 CC = ISD::SETEQ;
1674 LHS = SV; RHS = I->High; MHS = NULL;
1675 } else {
1676 CC = ISD::SETLE;
1677 LHS = I->Low; MHS = SV; RHS = I->High;
1678 }
1679 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681 // If emitting the first comparison, just call visitSwitchCase to emit the
1682 // code into the current block. Otherwise, push the CaseBlock onto the
1683 // vector to be later processed by SDISel, and insert the node's MBB
1684 // before the next MBB.
1685 if (CurBlock == CurMBB)
1686 visitSwitchCase(CB);
1687 else
1688 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001689
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001690 CurBlock = FallThrough;
1691 }
1692
1693 return true;
1694}
1695
1696static inline bool areJTsAllowed(const TargetLowering &TLI) {
1697 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1699 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001700}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001701
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001702static APInt ComputeRange(const APInt &First, const APInt &Last) {
1703 APInt LastExt(Last), FirstExt(First);
1704 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1705 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1706 return (LastExt - FirstExt + 1ULL);
1707}
1708
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001709/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001710bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1711 CaseRecVector& WorkList,
1712 Value* SV,
1713 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 Case& FrontCase = *CR.Range.first;
1715 Case& BackCase = *(CR.Range.second-1);
1716
Chris Lattnere880efe2009-11-07 07:50:34 +00001717 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1718 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719
Chris Lattnere880efe2009-11-07 07:50:34 +00001720 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001721 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1722 I!=E; ++I)
1723 TSize += I->size();
1724
Chris Lattnere880efe2009-11-07 07:50:34 +00001725 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001727
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001728 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001729 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001730 if (Density < 0.4)
1731 return false;
1732
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001733 DEBUG(errs() << "Lowering jump table\n"
1734 << "First entry: " << First << ". Last entry: " << Last << '\n'
1735 << "Range: " << Range
1736 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737
1738 // Get the MachineFunction which holds the current MBB. This is used when
1739 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001740 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741
1742 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001743 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001744 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001745
1746 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1747
1748 // Create a new basic block to hold the code for loading the address
1749 // of the jump table, and jumping to it. Update successor information;
1750 // we will either branch to the default case for the switch, or the jump
1751 // table.
1752 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1753 CurMF->insert(BBI, JumpTableBB);
1754 CR.CaseBB->addSuccessor(Default);
1755 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001756
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001757 // Build a vector of destination BBs, corresponding to each target
1758 // of the jump table. If the value of the jump table slot corresponds to
1759 // a case statement, push the case's BB onto the vector, otherwise, push
1760 // the default BB.
1761 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001762 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001763 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001764 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1765 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1766
1767 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001768 DestBBs.push_back(I->BB);
1769 if (TEI==High)
1770 ++I;
1771 } else {
1772 DestBBs.push_back(Default);
1773 }
1774 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001775
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001776 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001777 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1778 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001779 E = DestBBs.end(); I != E; ++I) {
1780 if (!SuccsHandled[(*I)->getNumber()]) {
1781 SuccsHandled[(*I)->getNumber()] = true;
1782 JumpTableBB->addSuccessor(*I);
1783 }
1784 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001785
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001786 // Create a jump table index for this jump table, or return an existing
1787 // one.
1788 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001789
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001790 // Set the jump table information so that we can codegen it as a second
1791 // MachineBasicBlock
1792 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1793 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1794 if (CR.CaseBB == CurMBB)
1795 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001796
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001797 JTCases.push_back(JumpTableBlock(JTH, JT));
1798
1799 return true;
1800}
1801
1802/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1803/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001804bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1805 CaseRecVector& WorkList,
1806 Value* SV,
1807 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 // Get the MachineFunction which holds the current MBB. This is used when
1809 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001810 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001811
1812 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001813 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001814 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001815
1816 Case& FrontCase = *CR.Range.first;
1817 Case& BackCase = *(CR.Range.second-1);
1818 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1819
1820 // Size is the number of Cases represented by this range.
1821 unsigned Size = CR.Range.second - CR.Range.first;
1822
Chris Lattnere880efe2009-11-07 07:50:34 +00001823 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1824 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001825 double FMetric = 0;
1826 CaseItr Pivot = CR.Range.first + Size/2;
1827
1828 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1829 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001830 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001831 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1832 I!=E; ++I)
1833 TSize += I->size();
1834
Chris Lattnere880efe2009-11-07 07:50:34 +00001835 APInt LSize = FrontCase.size();
1836 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001837 DEBUG(errs() << "Selecting best pivot: \n"
1838 << "First: " << First << ", Last: " << Last <<'\n'
1839 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001840 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1841 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001842 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1843 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001844 APInt Range = ComputeRange(LEnd, RBegin);
1845 assert((Range - 2ULL).isNonNegative() &&
1846 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001847 double LDensity = (double)LSize.roundToDouble() /
1848 (LEnd - First + 1ULL).roundToDouble();
1849 double RDensity = (double)RSize.roundToDouble() /
1850 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001851 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001852 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001853 DEBUG(errs() <<"=>Step\n"
1854 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1855 << "LDensity: " << LDensity
1856 << ", RDensity: " << RDensity << '\n'
1857 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001858 if (FMetric < Metric) {
1859 Pivot = J;
1860 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001861 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001862 }
1863
1864 LSize += J->size();
1865 RSize -= J->size();
1866 }
1867 if (areJTsAllowed(TLI)) {
1868 // If our case is dense we *really* should handle it earlier!
1869 assert((FMetric > 0) && "Should handle dense range earlier!");
1870 } else {
1871 Pivot = CR.Range.first + Size/2;
1872 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001873
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001874 CaseRange LHSR(CR.Range.first, Pivot);
1875 CaseRange RHSR(Pivot, CR.Range.second);
1876 Constant *C = Pivot->Low;
1877 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001878
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001879 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001880 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001881 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001882 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001883 // Pivot's Value, then we can branch directly to the LHS's Target,
1884 // rather than creating a leaf node for it.
1885 if ((LHSR.second - LHSR.first) == 1 &&
1886 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001887 cast<ConstantInt>(C)->getValue() ==
1888 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001889 TrueBB = LHSR.first->BB;
1890 } else {
1891 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1892 CurMF->insert(BBI, TrueBB);
1893 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001894
1895 // Put SV in a virtual register to make it available from the new blocks.
1896 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001897 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001898
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001899 // Similar to the optimization above, if the Value being switched on is
1900 // known to be less than the Constant CR.LT, and the current Case Value
1901 // is CR.LT - 1, then we can branch directly to the target block for
1902 // the current Case Value, rather than emitting a RHS leaf node for it.
1903 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001904 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1905 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001906 FalseBB = RHSR.first->BB;
1907 } else {
1908 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1909 CurMF->insert(BBI, FalseBB);
1910 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001911
1912 // Put SV in a virtual register to make it available from the new blocks.
1913 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001914 }
1915
1916 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001917 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001918 // Otherwise, branch to LHS.
1919 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1920
1921 if (CR.CaseBB == CurMBB)
1922 visitSwitchCase(CB);
1923 else
1924 SwitchCases.push_back(CB);
1925
1926 return true;
1927}
1928
1929/// handleBitTestsSwitchCase - if current case range has few destination and
1930/// range span less, than machine word bitwidth, encode case range into series
1931/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001932bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1933 CaseRecVector& WorkList,
1934 Value* SV,
1935 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001936 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001937 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001938
1939 Case& FrontCase = *CR.Range.first;
1940 Case& BackCase = *(CR.Range.second-1);
1941
1942 // Get the MachineFunction which holds the current MBB. This is used when
1943 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001944 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001946 // If target does not have legal shift left, do not emit bit tests at all.
1947 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1948 return false;
1949
Anton Korobeynikov23218582008-12-23 22:25:27 +00001950 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001951 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1952 I!=E; ++I) {
1953 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001954 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001956
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001957 // Count unique destinations
1958 SmallSet<MachineBasicBlock*, 4> Dests;
1959 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1960 Dests.insert(I->BB);
1961 if (Dests.size() > 3)
1962 // Don't bother the code below, if there are too much unique destinations
1963 return false;
1964 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001965 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1966 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001967
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001968 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001969 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1970 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001971 APInt cmpRange = maxValue - minValue;
1972
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001973 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1974 << "Low bound: " << minValue << '\n'
1975 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001976
1977 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001978 (!(Dests.size() == 1 && numCmps >= 3) &&
1979 !(Dests.size() == 2 && numCmps >= 5) &&
1980 !(Dests.size() >= 3 && numCmps >= 6)))
1981 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001982
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001983 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001984 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1985
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001986 // Optimize the case where all the case values fit in a
1987 // word without having to subtract minValue. In this case,
1988 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001989 if (minValue.isNonNegative() &&
1990 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1991 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001993 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001994 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001995
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001996 CaseBitsVector CasesBits;
1997 unsigned i, count = 0;
1998
1999 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2000 MachineBasicBlock* Dest = I->BB;
2001 for (i = 0; i < count; ++i)
2002 if (Dest == CasesBits[i].BB)
2003 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002005 if (i == count) {
2006 assert((count < 3) && "Too much destinations to test!");
2007 CasesBits.push_back(CaseBits(0, Dest, 0));
2008 count++;
2009 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002010
2011 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2012 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2013
2014 uint64_t lo = (lowValue - lowBound).getZExtValue();
2015 uint64_t hi = (highValue - lowBound).getZExtValue();
2016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 for (uint64_t j = lo; j <= hi; j++) {
2018 CasesBits[i].Mask |= 1ULL << j;
2019 CasesBits[i].Bits++;
2020 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002021
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002022 }
2023 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002024
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 BitTestInfo BTC;
2026
2027 // Figure out which block is immediately after the current one.
2028 MachineFunction::iterator BBI = CR.CaseBB;
2029 ++BBI;
2030
2031 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2032
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002033 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002035 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
2036 << ", Bits: " << CasesBits[i].Bits
2037 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002038
2039 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2040 CurMF->insert(BBI, CaseBB);
2041 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2042 CaseBB,
2043 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002044
2045 // Put SV in a virtual register to make it available from the new blocks.
2046 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002047 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002048
2049 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002050 -1U, (CR.CaseBB == CurMBB),
2051 CR.CaseBB, Default, BTC);
2052
2053 if (CR.CaseBB == CurMBB)
2054 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002056 BitTestCases.push_back(BTB);
2057
2058 return true;
2059}
2060
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002061/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002062size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2063 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002064 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002065
2066 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002067 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002068 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2069 Cases.push_back(Case(SI.getSuccessorValue(i),
2070 SI.getSuccessorValue(i),
2071 SMBB));
2072 }
2073 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2074
2075 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002076 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002077 // Must recompute end() each iteration because it may be
2078 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002079 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2080 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2081 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002082 MachineBasicBlock* nextBB = J->BB;
2083 MachineBasicBlock* currentBB = I->BB;
2084
2085 // If the two neighboring cases go to the same destination, merge them
2086 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002087 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002088 I->High = J->High;
2089 J = Cases.erase(J);
2090 } else {
2091 I = J++;
2092 }
2093 }
2094
2095 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2096 if (I->Low != I->High)
2097 // A range counts double, since it requires two compares.
2098 ++numCmps;
2099 }
2100
2101 return numCmps;
2102}
2103
Dan Gohman2048b852009-11-23 18:04:58 +00002104void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002105 // Figure out which block is immediately after the current one.
2106 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002107 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2108
2109 // If there is only the default destination, branch to it if it is not the
2110 // next basic block. Otherwise, just fall through.
2111 if (SI.getNumOperands() == 2) {
2112 // Update machine-CFG edges.
2113
2114 // If this is not a fall-through branch, emit the branch.
2115 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002116 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002117 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002118 MVT::Other, getControlRoot(),
2119 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002120 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002121
2122 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002123 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002124 }
2125
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002126 return;
2127 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129 // If there are any non-default case statements, create a vector of Cases
2130 // representing each one, and sort the vector so that we can efficiently
2131 // create a binary search tree from them.
2132 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002133 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002134 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2135 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002136 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002137
2138 // Get the Value to be switched on and default basic blocks, which will be
2139 // inserted into CaseBlock records, representing basic blocks in the binary
2140 // search tree.
2141 Value *SV = SI.getOperand(0);
2142
2143 // Push the initial CaseRec onto the worklist
2144 CaseRecVector WorkList;
2145 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2146
2147 while (!WorkList.empty()) {
2148 // Grab a record representing a case range to process off the worklist
2149 CaseRec CR = WorkList.back();
2150 WorkList.pop_back();
2151
2152 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2153 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002155 // If the range has few cases (two or less) emit a series of specific
2156 // tests.
2157 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2158 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002159
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002160 // If the switch has more than 5 blocks, and at least 40% dense, and the
2161 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002162 // lowering the switch to a binary tree of conditional branches.
2163 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2164 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2167 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2168 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2169 }
2170}
2171
Dan Gohman2048b852009-11-23 18:04:58 +00002172void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002173 // Update machine-CFG edges.
2174 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2175 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2176
Bill Wendling49fcff82009-12-21 22:30:11 +00002177 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2178 MVT::Other, getControlRoot(),
2179 getValue(I.getAddress()));
2180 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002181
Bill Wendling49fcff82009-12-21 22:30:11 +00002182 if (DisableScheduling)
2183 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2184}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002185
Dan Gohman2048b852009-11-23 18:04:58 +00002186void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187 // -0.0 - X --> fneg
2188 const Type *Ty = I.getType();
2189 if (isa<VectorType>(Ty)) {
2190 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2191 const VectorType *DestTy = cast<VectorType>(I.getType());
2192 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002193 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002194 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002195 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002196 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002197 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002198 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2199 Op2.getValueType(), Op2);
2200 setValue(&I, Res);
2201
2202 if (DisableScheduling)
2203 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205 return;
2206 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002207 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002209
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002210 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002211 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002212 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002213 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2214 Op2.getValueType(), Op2);
2215 setValue(&I, Res);
2216
2217 if (DisableScheduling)
2218 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2219
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002220 return;
2221 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002223 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224}
2225
Dan Gohman2048b852009-11-23 18:04:58 +00002226void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002227 SDValue Op1 = getValue(I.getOperand(0));
2228 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002229 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2230 Op1.getValueType(), Op1, Op2);
2231 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002232
Bill Wendling49fcff82009-12-21 22:30:11 +00002233 if (DisableScheduling)
2234 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235}
2236
Dan Gohman2048b852009-11-23 18:04:58 +00002237void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002238 SDValue Op1 = getValue(I.getOperand(0));
2239 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002240 if (!isa<VectorType>(I.getType()) &&
2241 Op2.getValueType() != TLI.getShiftAmountTy()) {
2242 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002243 EVT PTy = TLI.getPointerTy();
2244 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002245 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002246 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2247 TLI.getShiftAmountTy(), Op2);
2248 // If the operand is larger than the shift count type but the shift
2249 // count type has enough bits to represent any shift value, truncate
2250 // it now. This is a common case and it exposes the truncate to
2251 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002252 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002253 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2254 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2255 TLI.getShiftAmountTy(), Op2);
2256 // Otherwise we'll need to temporarily settle for some other
2257 // convenient type; type legalization will make adjustments as
2258 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002259 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002260 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002261 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002262 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002263 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002264 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002265 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002266
Bill Wendling49fcff82009-12-21 22:30:11 +00002267 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2268 Op1.getValueType(), Op1, Op2);
2269 setValue(&I, Res);
2270
Bill Wendling87710f02009-12-21 23:47:40 +00002271 if (DisableScheduling) {
2272 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2273 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002274 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002275 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002276}
2277
Dan Gohman2048b852009-11-23 18:04:58 +00002278void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002279 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2280 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2281 predicate = IC->getPredicate();
2282 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2283 predicate = ICmpInst::Predicate(IC->getPredicate());
2284 SDValue Op1 = getValue(I.getOperand(0));
2285 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002286 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002287
Owen Andersone50ed302009-08-10 22:56:29 +00002288 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002289 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2290 setValue(&I, Res);
2291
2292 if (DisableScheduling)
2293 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002294}
2295
Dan Gohman2048b852009-11-23 18:04:58 +00002296void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002297 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2298 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2299 predicate = FC->getPredicate();
2300 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2301 predicate = FCmpInst::Predicate(FC->getPredicate());
2302 SDValue Op1 = getValue(I.getOperand(0));
2303 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002304 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002305 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002306 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2307 setValue(&I, Res);
2308
2309 if (DisableScheduling)
2310 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002311}
2312
Dan Gohman2048b852009-11-23 18:04:58 +00002313void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002314 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002315 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2316 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002317 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002318
Bill Wendling49fcff82009-12-21 22:30:11 +00002319 SmallVector<SDValue, 4> Values(NumValues);
2320 SDValue Cond = getValue(I.getOperand(0));
2321 SDValue TrueVal = getValue(I.getOperand(1));
2322 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002323
Bill Wendling49fcff82009-12-21 22:30:11 +00002324 for (unsigned i = 0; i != NumValues; ++i) {
2325 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2326 TrueVal.getNode()->getValueType(i), Cond,
2327 SDValue(TrueVal.getNode(),
2328 TrueVal.getResNo() + i),
2329 SDValue(FalseVal.getNode(),
2330 FalseVal.getResNo() + i));
2331
2332 if (DisableScheduling)
2333 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002334 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002335
Bill Wendling49fcff82009-12-21 22:30:11 +00002336 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2337 DAG.getVTList(&ValueVTs[0], NumValues),
2338 &Values[0], NumValues);
2339 setValue(&I, Res);
2340
2341 if (DisableScheduling)
2342 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2343}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002344
Dan Gohman2048b852009-11-23 18:04:58 +00002345void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002346 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2347 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002348 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002349 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2350 setValue(&I, Res);
2351
2352 if (DisableScheduling)
2353 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002354}
2355
Dan Gohman2048b852009-11-23 18:04:58 +00002356void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002357 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2358 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2359 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002360 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002361 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2362 setValue(&I, Res);
2363
2364 if (DisableScheduling)
2365 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002366}
2367
Dan Gohman2048b852009-11-23 18:04:58 +00002368void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002369 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2370 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2371 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002372 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002373 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2374 setValue(&I, Res);
2375
2376 if (DisableScheduling)
2377 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002378}
2379
Dan Gohman2048b852009-11-23 18:04:58 +00002380void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002381 // FPTrunc is never a no-op cast, no need to check
2382 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002383 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002384 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2385 DestVT, N, DAG.getIntPtrConstant(0));
2386 setValue(&I, Res);
2387
2388 if (DisableScheduling)
2389 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002390}
2391
Dan Gohman2048b852009-11-23 18:04:58 +00002392void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002393 // FPTrunc is never a no-op cast, no need to check
2394 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002395 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002396 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2397 setValue(&I, Res);
2398
2399 if (DisableScheduling)
2400 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002401}
2402
Dan Gohman2048b852009-11-23 18:04:58 +00002403void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002404 // FPToUI is never a no-op cast, no need to check
2405 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002406 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002407 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2408 setValue(&I, Res);
2409
2410 if (DisableScheduling)
2411 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002412}
2413
Dan Gohman2048b852009-11-23 18:04:58 +00002414void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002415 // FPToSI is never a no-op cast, no need to check
2416 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002417 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002418 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2419 setValue(&I, Res);
2420
2421 if (DisableScheduling)
2422 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002423}
2424
Dan Gohman2048b852009-11-23 18:04:58 +00002425void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002426 // UIToFP is never a no-op cast, no need to check
2427 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002428 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002429 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2430 setValue(&I, Res);
2431
2432 if (DisableScheduling)
2433 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002434}
2435
Dan Gohman2048b852009-11-23 18:04:58 +00002436void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002437 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002438 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002439 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002440 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2441 setValue(&I, Res);
2442
2443 if (DisableScheduling)
2444 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002445}
2446
Dan Gohman2048b852009-11-23 18:04:58 +00002447void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002448 // What to do depends on the size of the integer and the size of the pointer.
2449 // We can either truncate, zero extend, or no-op, accordingly.
2450 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002451 EVT SrcVT = N.getValueType();
2452 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002453 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2454 setValue(&I, Res);
2455
2456 if (DisableScheduling)
2457 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002458}
2459
Dan Gohman2048b852009-11-23 18:04:58 +00002460void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002461 // What to do depends on the size of the integer and the size of the pointer.
2462 // We can either truncate, zero extend, or no-op, accordingly.
2463 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002464 EVT SrcVT = N.getValueType();
2465 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002466 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2467 setValue(&I, Res);
2468
2469 if (DisableScheduling)
2470 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002471}
2472
Dan Gohman2048b852009-11-23 18:04:58 +00002473void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002474 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002475 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002476
Bill Wendling49fcff82009-12-21 22:30:11 +00002477 // BitCast assures us that source and destination are the same size so this is
2478 // either a BIT_CONVERT or a no-op.
2479 if (DestVT != N.getValueType()) {
2480 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2481 DestVT, N); // convert types.
2482 setValue(&I, Res);
2483
2484 if (DisableScheduling)
2485 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2486 } else {
2487 setValue(&I, N); // noop cast.
2488 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002489}
2490
Dan Gohman2048b852009-11-23 18:04:58 +00002491void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492 SDValue InVec = getValue(I.getOperand(0));
2493 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002494 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002495 TLI.getPointerTy(),
2496 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002497 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2498 TLI.getValueType(I.getType()),
2499 InVec, InVal, InIdx);
2500 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002501
Bill Wendling87710f02009-12-21 23:47:40 +00002502 if (DisableScheduling) {
2503 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002504 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002505 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002506}
2507
Dan Gohman2048b852009-11-23 18:04:58 +00002508void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002509 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002510 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002511 TLI.getPointerTy(),
2512 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002513 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2514 TLI.getValueType(I.getType()), InVec, InIdx);
2515 setValue(&I, Res);
2516
Bill Wendling87710f02009-12-21 23:47:40 +00002517 if (DisableScheduling) {
2518 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002519 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002520 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002521}
2522
Mon P Wangaeb06d22008-11-10 04:46:22 +00002523
2524// Utility for visitShuffleVector - Returns true if the mask is mask starting
2525// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002526static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2527 unsigned MaskNumElts = Mask.size();
2528 for (unsigned i = 0; i != MaskNumElts; ++i)
2529 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002530 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002531 return true;
2532}
2533
Dan Gohman2048b852009-11-23 18:04:58 +00002534void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002535 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002536 SDValue Src1 = getValue(I.getOperand(0));
2537 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002538
Nate Begeman9008ca62009-04-27 18:41:29 +00002539 // Convert the ConstantVector mask operand into an array of ints, with -1
2540 // representing undef values.
2541 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002542 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2543 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002544 unsigned MaskNumElts = MaskElts.size();
2545 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002546 if (isa<UndefValue>(MaskElts[i]))
2547 Mask.push_back(-1);
2548 else
2549 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2550 }
2551
Owen Andersone50ed302009-08-10 22:56:29 +00002552 EVT VT = TLI.getValueType(I.getType());
2553 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002554 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002555
Mon P Wangc7849c22008-11-16 05:06:27 +00002556 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002557 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2558 &Mask[0]);
2559 setValue(&I, Res);
2560
2561 if (DisableScheduling)
2562 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2563
Mon P Wangaeb06d22008-11-10 04:46:22 +00002564 return;
2565 }
2566
2567 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002568 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2569 // Mask is longer than the source vectors and is a multiple of the source
2570 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002571 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002572 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2573 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002574 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2575 VT, Src1, Src2);
2576 setValue(&I, Res);
2577
2578 if (DisableScheduling)
2579 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2580
Mon P Wangaeb06d22008-11-10 04:46:22 +00002581 return;
2582 }
2583
Mon P Wangc7849c22008-11-16 05:06:27 +00002584 // Pad both vectors with undefs to make them the same length as the mask.
2585 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002586 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2587 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002588 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002589
Nate Begeman9008ca62009-04-27 18:41:29 +00002590 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2591 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002592 MOps1[0] = Src1;
2593 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002594
2595 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2596 getCurDebugLoc(), VT,
2597 &MOps1[0], NumConcat);
2598 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2599 getCurDebugLoc(), VT,
2600 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002601
Mon P Wangaeb06d22008-11-10 04:46:22 +00002602 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002603 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002604 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002605 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002606 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002607 MappedOps.push_back(Idx);
2608 else
2609 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002610 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002611
2612 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2613 &MappedOps[0]);
2614 setValue(&I, Res);
2615
Bill Wendlinge1a90422009-12-21 23:10:19 +00002616 if (DisableScheduling) {
2617 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2618 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002619 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002620 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002621
Mon P Wangaeb06d22008-11-10 04:46:22 +00002622 return;
2623 }
2624
Mon P Wangc7849c22008-11-16 05:06:27 +00002625 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002626 // Analyze the access pattern of the vector to see if we can extract
2627 // two subvectors and do the shuffle. The analysis is done by calculating
2628 // the range of elements the mask access on both vectors.
2629 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2630 int MaxRange[2] = {-1, -1};
2631
Nate Begeman5a5ca152009-04-29 05:20:52 +00002632 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002633 int Idx = Mask[i];
2634 int Input = 0;
2635 if (Idx < 0)
2636 continue;
2637
Nate Begeman5a5ca152009-04-29 05:20:52 +00002638 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002639 Input = 1;
2640 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002641 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002642 if (Idx > MaxRange[Input])
2643 MaxRange[Input] = Idx;
2644 if (Idx < MinRange[Input])
2645 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002646 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002647
Mon P Wangc7849c22008-11-16 05:06:27 +00002648 // Check if the access is smaller than the vector size and can we find
2649 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002650 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002651 int StartIdx[2]; // StartIdx to extract from
2652 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002653 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002654 RangeUse[Input] = 0; // Unused
2655 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002656 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002657 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002658 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002659 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002660 RangeUse[Input] = 1; // Extract from beginning of the vector
2661 StartIdx[Input] = 0;
2662 } else {
2663 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002664 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002665 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002666 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002667 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002668 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002669 }
2670
Bill Wendling636e2582009-08-21 18:16:06 +00002671 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002672 SDValue Res = DAG.getUNDEF(VT);
2673 setValue(&I, Res); // Vectors are not used.
2674
2675 if (DisableScheduling)
2676 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2677
Mon P Wangc7849c22008-11-16 05:06:27 +00002678 return;
2679 }
2680 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2681 // Extract appropriate subvector and generate a vector shuffle
2682 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002683 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002684 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002685 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002686 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002687 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002688 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002689
2690 if (DisableScheduling)
2691 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002692 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002693
Mon P Wangc7849c22008-11-16 05:06:27 +00002694 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002695 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002696 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002697 int Idx = Mask[i];
2698 if (Idx < 0)
2699 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002700 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002701 MappedOps.push_back(Idx - StartIdx[0]);
2702 else
2703 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002704 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002705
2706 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2707 &MappedOps[0]);
2708 setValue(&I, Res);
2709
2710 if (DisableScheduling)
2711 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2712
Mon P Wangc7849c22008-11-16 05:06:27 +00002713 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002714 }
2715 }
2716
Mon P Wangc7849c22008-11-16 05:06:27 +00002717 // We can't use either concat vectors or extract subvectors so fall back to
2718 // replacing the shuffle with extract and build vector.
2719 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002720 EVT EltVT = VT.getVectorElementType();
2721 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002722 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002723 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002724 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002725 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002726 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002727 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002728 SDValue Res;
2729
Nate Begeman5a5ca152009-04-29 05:20:52 +00002730 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002731 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2732 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002733 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002734 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2735 EltVT, Src2,
2736 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2737
2738 Ops.push_back(Res);
2739
2740 if (DisableScheduling)
2741 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002742 }
2743 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002744
2745 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2746 VT, &Ops[0], Ops.size());
2747 setValue(&I, Res);
2748
2749 if (DisableScheduling)
2750 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002751}
2752
Dan Gohman2048b852009-11-23 18:04:58 +00002753void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002754 const Value *Op0 = I.getOperand(0);
2755 const Value *Op1 = I.getOperand(1);
2756 const Type *AggTy = I.getType();
2757 const Type *ValTy = Op1->getType();
2758 bool IntoUndef = isa<UndefValue>(Op0);
2759 bool FromUndef = isa<UndefValue>(Op1);
2760
2761 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2762 I.idx_begin(), I.idx_end());
2763
Owen Andersone50ed302009-08-10 22:56:29 +00002764 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002765 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002766 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002767 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2768
2769 unsigned NumAggValues = AggValueVTs.size();
2770 unsigned NumValValues = ValValueVTs.size();
2771 SmallVector<SDValue, 4> Values(NumAggValues);
2772
2773 SDValue Agg = getValue(Op0);
2774 SDValue Val = getValue(Op1);
2775 unsigned i = 0;
2776 // Copy the beginning value(s) from the original aggregate.
2777 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002778 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002779 SDValue(Agg.getNode(), Agg.getResNo() + i);
2780 // Copy values from the inserted value(s).
2781 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002782 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002783 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2784 // Copy remaining value(s) from the original aggregate.
2785 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002786 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002787 SDValue(Agg.getNode(), Agg.getResNo() + i);
2788
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002789 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2790 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2791 &Values[0], NumAggValues);
2792 setValue(&I, Res);
2793
2794 if (DisableScheduling)
2795 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796}
2797
Dan Gohman2048b852009-11-23 18:04:58 +00002798void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002799 const Value *Op0 = I.getOperand(0);
2800 const Type *AggTy = Op0->getType();
2801 const Type *ValTy = I.getType();
2802 bool OutOfUndef = isa<UndefValue>(Op0);
2803
2804 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2805 I.idx_begin(), I.idx_end());
2806
Owen Andersone50ed302009-08-10 22:56:29 +00002807 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2809
2810 unsigned NumValValues = ValValueVTs.size();
2811 SmallVector<SDValue, 4> Values(NumValValues);
2812
2813 SDValue Agg = getValue(Op0);
2814 // Copy out the selected value(s).
2815 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2816 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002817 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002818 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002819 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002820
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002821 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2822 DAG.getVTList(&ValValueVTs[0], NumValValues),
2823 &Values[0], NumValValues);
2824 setValue(&I, Res);
2825
2826 if (DisableScheduling)
2827 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002828}
2829
Dan Gohman2048b852009-11-23 18:04:58 +00002830void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002831 SDValue N = getValue(I.getOperand(0));
2832 const Type *Ty = I.getOperand(0)->getType();
2833
2834 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2835 OI != E; ++OI) {
2836 Value *Idx = *OI;
2837 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2838 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2839 if (Field) {
2840 // N = N + Offset
2841 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002842 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002844
2845 if (DisableScheduling)
2846 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002848
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002849 Ty = StTy->getElementType(Field);
2850 } else {
2851 Ty = cast<SequentialType>(Ty)->getElementType();
2852
2853 // If this is a constant subscript, handle it quickly.
2854 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2855 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002856 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002857 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002858 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002859 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002860 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002861 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002862 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2863 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002864 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002865 else
Evan Chengb1032a82009-02-09 20:54:38 +00002866 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002867
Dale Johannesen66978ee2009-01-31 02:22:37 +00002868 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002869 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002870
2871 if (DisableScheduling) {
2872 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2873 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2874 }
2875
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002876 continue;
2877 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002878
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002879 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002880 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2881 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002882 SDValue IdxN = getValue(Idx);
2883
2884 // If the index is smaller or larger than intptr_t, truncate or extend
2885 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002886 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002887
2888 // If this is a multiply by a power of two, turn it into a shl
2889 // immediately. This is a very common case.
2890 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002891 if (ElementSize.isPowerOf2()) {
2892 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002893 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002894 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002895 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002896 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002897 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002898 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002899 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002901
2902 if (DisableScheduling)
2903 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002904 }
2905
Scott Michelfdc40a02009-02-17 22:15:04 +00002906 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002907 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002908
2909 if (DisableScheduling)
2910 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002911 }
2912 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002914 setValue(&I, N);
2915}
2916
Dan Gohman2048b852009-11-23 18:04:58 +00002917void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002918 // If this is a fixed sized alloca in the entry block of the function,
2919 // allocate it statically on the stack.
2920 if (FuncInfo.StaticAllocaMap.count(&I))
2921 return; // getValue will auto-populate this.
2922
2923 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002924 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002925 unsigned Align =
2926 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2927 I.getAlignment());
2928
2929 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002930
2931 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2932 AllocSize,
2933 DAG.getConstant(TySize, AllocSize.getValueType()));
2934
Bill Wendling856ff412009-12-22 00:12:37 +00002935 if (DisableScheduling)
2936 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Chris Lattner0b18e592009-03-17 19:36:00 +00002937
Owen Andersone50ed302009-08-10 22:56:29 +00002938 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002939 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002940
Bill Wendling856ff412009-12-22 00:12:37 +00002941 if (DisableScheduling)
2942 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2943
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002944 // Handle alignment. If the requested alignment is less than or equal to
2945 // the stack alignment, ignore it. If the size is greater than or equal to
2946 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2947 unsigned StackAlign =
2948 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2949 if (Align <= StackAlign)
2950 Align = 0;
2951
2952 // Round the size of the allocation up to the stack alignment size
2953 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002954 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002955 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002956 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002957 if (DisableScheduling)
2958 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2959
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002960 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002961 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002962 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002963 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002964 if (DisableScheduling)
2965 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002966
2967 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002968 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002969 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002970 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002971 setValue(&I, DSA);
2972 DAG.setRoot(DSA.getValue(1));
2973
Bill Wendling856ff412009-12-22 00:12:37 +00002974 if (DisableScheduling)
2975 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
2976
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002977 // Inform the Frame Information that we have just allocated a variable-sized
2978 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002979 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002980}
2981
Dan Gohman2048b852009-11-23 18:04:58 +00002982void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002983 const Value *SV = I.getOperand(0);
2984 SDValue Ptr = getValue(SV);
2985
2986 const Type *Ty = I.getType();
2987 bool isVolatile = I.isVolatile();
2988 unsigned Alignment = I.getAlignment();
2989
Owen Andersone50ed302009-08-10 22:56:29 +00002990 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002991 SmallVector<uint64_t, 4> Offsets;
2992 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2993 unsigned NumValues = ValueVTs.size();
2994 if (NumValues == 0)
2995 return;
2996
2997 SDValue Root;
2998 bool ConstantMemory = false;
2999 if (I.isVolatile())
3000 // Serialize volatile loads with other side effects.
3001 Root = getRoot();
3002 else if (AA->pointsToConstantMemory(SV)) {
3003 // Do not serialize (non-volatile) loads of constant memory with anything.
3004 Root = DAG.getEntryNode();
3005 ConstantMemory = true;
3006 } else {
3007 // Do not serialize non-volatile loads against each other.
3008 Root = DAG.getRoot();
3009 }
3010
3011 SmallVector<SDValue, 4> Values(NumValues);
3012 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003013 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003014 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00003015 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
3016 PtrVT, Ptr,
3017 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003018 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00003019 A, SV, Offsets[i], isVolatile, Alignment);
3020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003021 Values[i] = L;
3022 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00003023
3024 if (DisableScheduling) {
3025 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
3026 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3027 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003028 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003030 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003031 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003032 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003033 if (isVolatile)
3034 DAG.setRoot(Chain);
3035 else
3036 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00003037
3038 if (DisableScheduling)
3039 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003040 }
3041
Bill Wendling856ff412009-12-22 00:12:37 +00003042 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
3043 DAG.getVTList(&ValueVTs[0], NumValues),
3044 &Values[0], NumValues);
3045 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003046
Bill Wendling856ff412009-12-22 00:12:37 +00003047 if (DisableScheduling)
3048 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
3049}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003050
Dan Gohman2048b852009-11-23 18:04:58 +00003051void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003052 Value *SrcV = I.getOperand(0);
3053 Value *PtrV = I.getOperand(1);
3054
Owen Andersone50ed302009-08-10 22:56:29 +00003055 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003056 SmallVector<uint64_t, 4> Offsets;
3057 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3058 unsigned NumValues = ValueVTs.size();
3059 if (NumValues == 0)
3060 return;
3061
3062 // Get the lowered operands. Note that we do this after
3063 // checking if NumResults is zero, because with zero results
3064 // the operands won't have values in the map.
3065 SDValue Src = getValue(SrcV);
3066 SDValue Ptr = getValue(PtrV);
3067
3068 SDValue Root = getRoot();
3069 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003070 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003071 bool isVolatile = I.isVolatile();
3072 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003073
3074 for (unsigned i = 0; i != NumValues; ++i) {
3075 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3076 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003077 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003078 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003079 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003080
Bill Wendling856ff412009-12-22 00:12:37 +00003081 if (DisableScheduling) {
3082 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3083 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3084 }
3085 }
3086
3087 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3088 MVT::Other, &Chains[0], NumValues);
3089 DAG.setRoot(Res);
3090
3091 if (DisableScheduling)
3092 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003093}
3094
3095/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3096/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003097void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3098 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003099 bool HasChain = !I.doesNotAccessMemory();
3100 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3101
3102 // Build the operand list.
3103 SmallVector<SDValue, 8> Ops;
3104 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3105 if (OnlyLoad) {
3106 // We don't need to serialize loads against other loads.
3107 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003108 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003109 Ops.push_back(getRoot());
3110 }
3111 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003112
3113 // Info is set by getTgtMemInstrinsic
3114 TargetLowering::IntrinsicInfo Info;
3115 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3116
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003117 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003118 if (!IsTgtIntrinsic)
3119 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003120
3121 // Add all operands of the call to the operand list.
3122 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3123 SDValue Op = getValue(I.getOperand(i));
3124 assert(TLI.isTypeLegal(Op.getValueType()) &&
3125 "Intrinsic uses a non-legal type?");
3126 Ops.push_back(Op);
3127 }
3128
Owen Andersone50ed302009-08-10 22:56:29 +00003129 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003130 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3131#ifndef NDEBUG
3132 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3133 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3134 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003135 }
Bob Wilson8d919552009-07-31 22:41:21 +00003136#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003138 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003140
Bob Wilson8d919552009-07-31 22:41:21 +00003141 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003142
3143 // Create the node.
3144 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003145 if (IsTgtIntrinsic) {
3146 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003147 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003148 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003149 Info.memVT, Info.ptrVal, Info.offset,
3150 Info.align, Info.vol,
3151 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003152 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003153 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003154 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003155 } else if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003156 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003157 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003158 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003159 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003160 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003161 }
3162
3163 if (DisableScheduling)
3164 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003165
3166 if (HasChain) {
3167 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3168 if (OnlyLoad)
3169 PendingLoads.push_back(Chain);
3170 else
3171 DAG.setRoot(Chain);
3172 }
Bill Wendling856ff412009-12-22 00:12:37 +00003173
Owen Anderson1d0be152009-08-13 21:58:54 +00003174 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003175 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003176 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003177 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003178
3179 if (DisableScheduling)
3180 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003181 }
Bill Wendling856ff412009-12-22 00:12:37 +00003182
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003183 setValue(&I, Result);
3184 }
3185}
3186
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003187/// GetSignificand - Get the significand and build it into a floating-point
3188/// number with exponent of 1:
3189///
3190/// Op = (Op & 0x007fffff) | 0x3f800000;
3191///
3192/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003193static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003194GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3196 DAG.getConstant(0x007fffff, MVT::i32));
3197 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3198 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003199 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3200
3201 if (DisableScheduling) {
3202 DAG.AssignOrdering(t1.getNode(), Order);
3203 DAG.AssignOrdering(t2.getNode(), Order);
3204 DAG.AssignOrdering(Res.getNode(), Order);
3205 }
3206
3207 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003208}
3209
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003210/// GetExponent - Get the exponent:
3211///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003212/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003213///
3214/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003215static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003216GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003217 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003218 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3219 DAG.getConstant(0x7f800000, MVT::i32));
3220 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003221 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3223 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003224 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3225
3226 if (DisableScheduling) {
3227 DAG.AssignOrdering(t0.getNode(), Order);
3228 DAG.AssignOrdering(t1.getNode(), Order);
3229 DAG.AssignOrdering(t2.getNode(), Order);
3230 DAG.AssignOrdering(Res.getNode(), Order);
3231 }
3232
3233 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003234}
3235
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003236/// getF32Constant - Get 32-bit floating point constant.
3237static SDValue
3238getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003239 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003240}
3241
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003242/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003243/// visitIntrinsicCall: I is a call instruction
3244/// Op is the associated NodeType for I
3245const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003246SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003247 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003248 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003249 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003250 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003251 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003252 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003253 getValue(I.getOperand(2)),
3254 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003255 setValue(&I, L);
3256 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003257
3258 if (DisableScheduling)
3259 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003261 return 0;
3262}
3263
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003264// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003265const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003266SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003267 SDValue Op1 = getValue(I.getOperand(1));
3268 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003269
Owen Anderson825b72b2009-08-11 20:47:22 +00003270 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003271 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003272
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003273 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003274
3275 if (DisableScheduling)
3276 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3277
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003278 return 0;
3279}
Bill Wendling74c37652008-12-09 22:08:41 +00003280
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003281/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3282/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003283void
Dan Gohman2048b852009-11-23 18:04:58 +00003284SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003285 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003286 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003287
Owen Anderson825b72b2009-08-11 20:47:22 +00003288 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003289 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3290 SDValue Op = getValue(I.getOperand(1));
3291
3292 // Put the exponent in the right bit position for later addition to the
3293 // final result:
3294 //
3295 // #define LOG2OFe 1.4426950f
3296 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003298 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003299 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003300
3301 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3303 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003304
Bill Wendling856ff412009-12-22 00:12:37 +00003305 if (DisableScheduling) {
3306 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3307 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3308 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3309 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3310 }
3311
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003312 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003314 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003315
Bill Wendling856ff412009-12-22 00:12:37 +00003316 if (DisableScheduling)
3317 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3318
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003319 if (LimitFloatPrecision <= 6) {
3320 // For floating-point precision of 6:
3321 //
3322 // TwoToFractionalPartOfX =
3323 // 0.997535578f +
3324 // (0.735607626f + 0.252464424f * x) * x;
3325 //
3326 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003328 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003330 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3332 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003333 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003335
3336 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003338 TwoToFracPartOfX, IntegerPartOfX);
3339
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003341
3342 if (DisableScheduling) {
3343 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3344 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3345 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3346 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3347 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3348 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3349 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3350 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003351 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3352 // For floating-point precision of 12:
3353 //
3354 // TwoToFractionalPartOfX =
3355 // 0.999892986f +
3356 // (0.696457318f +
3357 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3358 //
3359 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003361 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003363 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3365 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003366 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3368 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003369 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003371
3372 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003374 TwoToFracPartOfX, IntegerPartOfX);
3375
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003377
3378 if (DisableScheduling) {
3379 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3380 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3381 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3382 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3383 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3384 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3385 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3386 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3387 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3388 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003389 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3390 // For floating-point precision of 18:
3391 //
3392 // TwoToFractionalPartOfX =
3393 // 0.999999982f +
3394 // (0.693148872f +
3395 // (0.240227044f +
3396 // (0.554906021e-1f +
3397 // (0.961591928e-2f +
3398 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3399 //
3400 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003402 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003404 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3406 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003407 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3409 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003410 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3412 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003413 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3415 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003416 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003417 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3418 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003419 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003420 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003422
3423 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003425 TwoToFracPartOfX, IntegerPartOfX);
3426
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003428
3429 if (DisableScheduling) {
3430 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3431 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3432 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3433 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3434 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3435 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3436 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3437 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3438 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3439 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3440 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3441 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3442 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3443 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3444 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3445 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003446 }
3447 } else {
3448 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003449 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003450 getValue(I.getOperand(1)).getValueType(),
3451 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003452 if (DisableScheduling)
3453 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003454 }
3455
Dale Johannesen59e577f2008-09-05 18:38:42 +00003456 setValue(&I, result);
3457}
3458
Bill Wendling39150252008-09-09 20:39:27 +00003459/// visitLog - Lower a log intrinsic. Handles the special sequences for
3460/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003461void
Dan Gohman2048b852009-11-23 18:04:58 +00003462SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003463 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003464 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003465
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003467 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3468 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003470
Bill Wendling856ff412009-12-22 00:12:37 +00003471 if (DisableScheduling)
3472 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3473
Bill Wendling39150252008-09-09 20:39:27 +00003474 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003475 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003477 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003478
Bill Wendling856ff412009-12-22 00:12:37 +00003479 if (DisableScheduling)
3480 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3481
Bill Wendling39150252008-09-09 20:39:27 +00003482 // Get the significand and build it into a floating-point number with
3483 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003484 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003485
3486 if (LimitFloatPrecision <= 6) {
3487 // For floating-point precision of 6:
3488 //
3489 // LogofMantissa =
3490 // -1.1609546f +
3491 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003492 //
Bill Wendling39150252008-09-09 20:39:27 +00003493 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003494 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003495 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003496 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003497 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3499 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003500 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003501
Scott Michelfdc40a02009-02-17 22:15:04 +00003502 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003503 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003504
3505 if (DisableScheduling) {
3506 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3507 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3508 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3509 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3510 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3511 }
Bill Wendling39150252008-09-09 20:39:27 +00003512 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3513 // For floating-point precision of 12:
3514 //
3515 // LogOfMantissa =
3516 // -1.7417939f +
3517 // (2.8212026f +
3518 // (-1.4699568f +
3519 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3520 //
3521 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003523 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003525 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3527 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003528 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3530 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003531 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3533 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003534 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003535
Scott Michelfdc40a02009-02-17 22:15:04 +00003536 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003538
3539 if (DisableScheduling) {
3540 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3541 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3542 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3543 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3544 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3545 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3546 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3547 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3548 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3549 }
Bill Wendling39150252008-09-09 20:39:27 +00003550 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3551 // For floating-point precision of 18:
3552 //
3553 // LogOfMantissa =
3554 // -2.1072184f +
3555 // (4.2372794f +
3556 // (-3.7029485f +
3557 // (2.2781945f +
3558 // (-0.87823314f +
3559 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3560 //
3561 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003565 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003566 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3567 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003568 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3570 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003571 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3573 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003574 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003575 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3576 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003577 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3579 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003580 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003581
Scott Michelfdc40a02009-02-17 22:15:04 +00003582 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003584
3585 if (DisableScheduling) {
3586 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3587 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3588 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3589 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3590 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3591 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3592 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3593 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3594 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3595 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3596 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3597 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3598 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3599 }
Bill Wendling39150252008-09-09 20:39:27 +00003600 }
3601 } else {
3602 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003603 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003604 getValue(I.getOperand(1)).getValueType(),
3605 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003606
3607 if (DisableScheduling)
3608 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003609 }
3610
Dale Johannesen59e577f2008-09-05 18:38:42 +00003611 setValue(&I, result);
3612}
3613
Bill Wendling3eb59402008-09-09 00:28:24 +00003614/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3615/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003616void
Dan Gohman2048b852009-11-23 18:04:58 +00003617SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003618 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003619 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003620
Owen Anderson825b72b2009-08-11 20:47:22 +00003621 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003622 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3623 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003624 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003625
Bill Wendling856ff412009-12-22 00:12:37 +00003626 if (DisableScheduling)
3627 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3628
Bill Wendling39150252008-09-09 20:39:27 +00003629 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003630 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3631
3632 if (DisableScheduling)
3633 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003634
3635 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003636 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003637 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003638
Bill Wendling3eb59402008-09-09 00:28:24 +00003639 // Different possible minimax approximations of significand in
3640 // floating-point for various degrees of accuracy over [1,2].
3641 if (LimitFloatPrecision <= 6) {
3642 // For floating-point precision of 6:
3643 //
3644 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3645 //
3646 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003648 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003649 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003650 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003651 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3652 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003653 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003654
Scott Michelfdc40a02009-02-17 22:15:04 +00003655 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003656 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003657
3658 if (DisableScheduling) {
3659 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3660 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3661 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3662 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3663 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3664 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003665 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3666 // For floating-point precision of 12:
3667 //
3668 // Log2ofMantissa =
3669 // -2.51285454f +
3670 // (4.07009056f +
3671 // (-2.12067489f +
3672 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003673 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003674 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003675 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003676 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003677 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003678 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003679 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3680 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003681 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003682 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3683 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003684 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003685 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3686 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003687 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003688
Scott Michelfdc40a02009-02-17 22:15:04 +00003689 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003690 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003691
3692 if (DisableScheduling) {
3693 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3694 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3695 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3696 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3697 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3698 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3699 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3700 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3701 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3702 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003703 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3704 // For floating-point precision of 18:
3705 //
3706 // Log2ofMantissa =
3707 // -3.0400495f +
3708 // (6.1129976f +
3709 // (-5.3420409f +
3710 // (3.2865683f +
3711 // (-1.2669343f +
3712 // (0.27515199f -
3713 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3714 //
3715 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003717 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003718 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003719 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003720 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3721 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003722 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003723 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3724 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003725 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003726 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3727 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003728 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003729 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3730 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003731 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3733 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003734 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003735
Scott Michelfdc40a02009-02-17 22:15:04 +00003736 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003738
3739 if (DisableScheduling) {
3740 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3741 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3742 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3743 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3744 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3745 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3746 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3747 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3748 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3749 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3750 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3751 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3752 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3753 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003754 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003755 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003756 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003757 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003758 getValue(I.getOperand(1)).getValueType(),
3759 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003760
3761 if (DisableScheduling)
3762 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003763 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003764
Dale Johannesen59e577f2008-09-05 18:38:42 +00003765 setValue(&I, result);
3766}
3767
Bill Wendling3eb59402008-09-09 00:28:24 +00003768/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3769/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003770void
Dan Gohman2048b852009-11-23 18:04:58 +00003771SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003772 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003773 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003774
Owen Anderson825b72b2009-08-11 20:47:22 +00003775 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003776 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3777 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003778 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003779
Bill Wendling856ff412009-12-22 00:12:37 +00003780 if (DisableScheduling)
3781 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3782
Bill Wendling39150252008-09-09 20:39:27 +00003783 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003784 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003785 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003786 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003787
Bill Wendling856ff412009-12-22 00:12:37 +00003788 if (DisableScheduling)
3789 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3790
Bill Wendling3eb59402008-09-09 00:28:24 +00003791 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003792 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003793 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003794
3795 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003796 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003797 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003798 // Log10ofMantissa =
3799 // -0.50419619f +
3800 // (0.60948995f - 0.10380950f * x) * x;
3801 //
3802 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003803 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003804 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003806 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003807 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3808 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003809 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003810
Scott Michelfdc40a02009-02-17 22:15:04 +00003811 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003812 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003813
3814 if (DisableScheduling) {
3815 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3816 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3817 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3818 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3819 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3820 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003821 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3822 // For floating-point precision of 12:
3823 //
3824 // Log10ofMantissa =
3825 // -0.64831180f +
3826 // (0.91751397f +
3827 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3828 //
3829 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003830 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003831 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003832 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003833 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003834 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3835 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003836 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003837 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3838 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003839 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003840
Scott Michelfdc40a02009-02-17 22:15:04 +00003841 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003842 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003843
3844 if (DisableScheduling) {
3845 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3846 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3847 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3848 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3849 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3850 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3851 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3852 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003853 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003854 // For floating-point precision of 18:
3855 //
3856 // Log10ofMantissa =
3857 // -0.84299375f +
3858 // (1.5327582f +
3859 // (-1.0688956f +
3860 // (0.49102474f +
3861 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3862 //
3863 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003864 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003865 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003866 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003867 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3869 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003870 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3872 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003873 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003874 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3875 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003876 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003877 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3878 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003879 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003880
Scott Michelfdc40a02009-02-17 22:15:04 +00003881 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003883
3884 if (DisableScheduling) {
3885 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3886 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3887 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3888 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3889 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3890 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3891 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3892 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3893 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3894 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3895 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3896 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003897 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003898 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003899 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003900 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003901 getValue(I.getOperand(1)).getValueType(),
3902 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003903
3904 if (DisableScheduling)
3905 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003906 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003907
Dale Johannesen59e577f2008-09-05 18:38:42 +00003908 setValue(&I, result);
3909}
3910
Bill Wendlinge10c8142008-09-09 22:39:21 +00003911/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3912/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003913void
Dan Gohman2048b852009-11-23 18:04:58 +00003914SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003915 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003916 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003917
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003919 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3920 SDValue Op = getValue(I.getOperand(1));
3921
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003923
Bill Wendling856ff412009-12-22 00:12:37 +00003924 if (DisableScheduling)
3925 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3926
Bill Wendlinge10c8142008-09-09 22:39:21 +00003927 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003928 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3929 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003930
3931 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003932 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003933 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003934
Bill Wendling856ff412009-12-22 00:12:37 +00003935 if (DisableScheduling) {
3936 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3937 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3938 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3939 }
3940
Bill Wendlinge10c8142008-09-09 22:39:21 +00003941 if (LimitFloatPrecision <= 6) {
3942 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003943 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003944 // TwoToFractionalPartOfX =
3945 // 0.997535578f +
3946 // (0.735607626f + 0.252464424f * x) * x;
3947 //
3948 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003950 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003952 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003953 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3954 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003955 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003957 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003959
Scott Michelfdc40a02009-02-17 22:15:04 +00003960 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003962
3963 if (DisableScheduling) {
3964 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3965 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3966 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3967 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3968 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3969 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3970 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3971 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003972 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3973 // For floating-point precision of 12:
3974 //
3975 // TwoToFractionalPartOfX =
3976 // 0.999892986f +
3977 // (0.696457318f +
3978 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3979 //
3980 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003981 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003982 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003983 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003984 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003985 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3986 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003987 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003988 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3989 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003990 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003991 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003992 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003993 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003994
Scott Michelfdc40a02009-02-17 22:15:04 +00003995 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003996 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003997
3998 if (DisableScheduling) {
3999 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4000 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4001 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4002 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4003 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4004 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4005 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4006 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4007 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4008 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004009 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4010 // For floating-point precision of 18:
4011 //
4012 // TwoToFractionalPartOfX =
4013 // 0.999999982f +
4014 // (0.693148872f +
4015 // (0.240227044f +
4016 // (0.554906021e-1f +
4017 // (0.961591928e-2f +
4018 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4019 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004020 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004021 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004023 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004024 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4025 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004026 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004027 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4028 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004029 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004030 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4031 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004032 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4034 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004035 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4037 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004038 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004040 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004041 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004042
Scott Michelfdc40a02009-02-17 22:15:04 +00004043 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004044 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004045
4046 if (DisableScheduling) {
4047 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4048 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4049 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4050 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4051 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4052 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4053 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4054 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4055 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4056 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4057 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4058 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4059 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4060 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4061 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4062 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004063 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00004064 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00004065 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004066 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00004067 getValue(I.getOperand(1)).getValueType(),
4068 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00004069
4070 if (DisableScheduling)
4071 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004072 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004073
Dale Johannesen601d3c02008-09-05 01:48:15 +00004074 setValue(&I, result);
4075}
4076
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004077/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4078/// limited-precision mode with x == 10.0f.
4079void
Dan Gohman2048b852009-11-23 18:04:58 +00004080SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004081 SDValue result;
4082 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004083 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004084 bool IsExp10 = false;
4085
Owen Anderson825b72b2009-08-11 20:47:22 +00004086 if (getValue(Val).getValueType() == MVT::f32 &&
4087 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004088 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4089 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4090 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4091 APFloat Ten(10.0f);
4092 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4093 }
4094 }
4095 }
4096
4097 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4098 SDValue Op = getValue(I.getOperand(2));
4099
4100 // Put the exponent in the right bit position for later addition to the
4101 // final result:
4102 //
4103 // #define LOG2OF10 3.3219281f
4104 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004105 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004106 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004107 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004108
4109 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4111 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004112
Bill Wendling856ff412009-12-22 00:12:37 +00004113 if (DisableScheduling) {
4114 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4115 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4116 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4117 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4118 }
4119
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004120 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004121 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004122 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004123
Bill Wendling856ff412009-12-22 00:12:37 +00004124 if (DisableScheduling)
4125 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4126
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004127 if (LimitFloatPrecision <= 6) {
4128 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004129 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004130 // twoToFractionalPartOfX =
4131 // 0.997535578f +
4132 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004133 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004134 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004136 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004138 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4140 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004141 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004143 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004145
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004146 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004148
4149 if (DisableScheduling) {
4150 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4151 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4152 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4153 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4154 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4155 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4156 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4157 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004158 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4159 // For floating-point precision of 12:
4160 //
4161 // TwoToFractionalPartOfX =
4162 // 0.999892986f +
4163 // (0.696457318f +
4164 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4165 //
4166 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004167 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004168 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004170 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004171 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4172 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004173 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4175 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004176 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004178 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004180
Scott Michelfdc40a02009-02-17 22:15:04 +00004181 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004183
4184 if (DisableScheduling) {
4185 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4186 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4187 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4188 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4189 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4190 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4191 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4192 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4193 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4194 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004195 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4196 // For floating-point precision of 18:
4197 //
4198 // TwoToFractionalPartOfX =
4199 // 0.999999982f +
4200 // (0.693148872f +
4201 // (0.240227044f +
4202 // (0.554906021e-1f +
4203 // (0.961591928e-2f +
4204 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4205 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004206 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004207 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004208 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004209 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004210 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4211 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004212 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4214 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004215 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4217 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004218 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004219 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4220 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004221 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004222 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4223 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004224 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004226 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004228
Scott Michelfdc40a02009-02-17 22:15:04 +00004229 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004231
4232 if (DisableScheduling) {
4233 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4234 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4235 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4236 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4237 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4238 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4239 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4240 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4241 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4242 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4243 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4244 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4245 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4246 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4247 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4248 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004249 }
4250 } else {
4251 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004252 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004253 getValue(I.getOperand(1)).getValueType(),
4254 getValue(I.getOperand(1)),
4255 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004256
4257 if (DisableScheduling)
4258 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004259 }
4260
4261 setValue(&I, result);
4262}
4263
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004264/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4265/// we want to emit this as a call to a named external function, return the name
4266/// otherwise lower it and return null.
4267const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004268SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004269 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004270 SDValue Res;
4271
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004272 switch (Intrinsic) {
4273 default:
4274 // By default, turn this into a target intrinsic node.
4275 visitTargetIntrinsic(I, Intrinsic);
4276 return 0;
4277 case Intrinsic::vastart: visitVAStart(I); return 0;
4278 case Intrinsic::vaend: visitVAEnd(I); return 0;
4279 case Intrinsic::vacopy: visitVACopy(I); return 0;
4280 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004281 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4282 getValue(I.getOperand(1)));
4283 setValue(&I, Res);
4284 if (DisableScheduling)
4285 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004286 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004287 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004288 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4289 getValue(I.getOperand(1)));
4290 setValue(&I, Res);
4291 if (DisableScheduling)
4292 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004293 return 0;
4294 case Intrinsic::setjmp:
4295 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004296 case Intrinsic::longjmp:
4297 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004298 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004299 SDValue Op1 = getValue(I.getOperand(1));
4300 SDValue Op2 = getValue(I.getOperand(2));
4301 SDValue Op3 = getValue(I.getOperand(3));
4302 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004303 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4304 I.getOperand(1), 0, I.getOperand(2), 0);
4305 DAG.setRoot(Res);
4306 if (DisableScheduling)
4307 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004308 return 0;
4309 }
Chris Lattner824b9582008-11-21 16:42:48 +00004310 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004311 SDValue Op1 = getValue(I.getOperand(1));
4312 SDValue Op2 = getValue(I.getOperand(2));
4313 SDValue Op3 = getValue(I.getOperand(3));
4314 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004315 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4316 I.getOperand(1), 0);
4317 DAG.setRoot(Res);
4318 if (DisableScheduling)
4319 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004320 return 0;
4321 }
Chris Lattner824b9582008-11-21 16:42:48 +00004322 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004323 SDValue Op1 = getValue(I.getOperand(1));
4324 SDValue Op2 = getValue(I.getOperand(2));
4325 SDValue Op3 = getValue(I.getOperand(3));
4326 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4327
4328 // If the source and destination are known to not be aliases, we can
4329 // lower memmove as memcpy.
4330 uint64_t Size = -1ULL;
4331 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004332 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004333 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4334 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004335 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4336 I.getOperand(1), 0, I.getOperand(2), 0);
4337 DAG.setRoot(Res);
4338 if (DisableScheduling)
4339 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004340 return 0;
4341 }
4342
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004343 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4344 I.getOperand(1), 0, I.getOperand(2), 0);
4345 DAG.setRoot(Res);
4346 if (DisableScheduling)
4347 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004348 return 0;
4349 }
Devang Patel70d75ca2009-11-12 19:02:56 +00004350 case Intrinsic::dbg_stoppoint:
4351 case Intrinsic::dbg_region_start:
4352 case Intrinsic::dbg_region_end:
4353 case Intrinsic::dbg_func_start:
4354 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004355 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004356 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00004357 if (OptLevel != CodeGenOpt::None)
4358 // FIXME: Variable debug info is not supported here.
4359 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004360 DwarfWriter *DW = DAG.getDwarfWriter();
4361 if (!DW)
4362 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004363 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
4364 if (!isValidDebugInfoIntrinsic(DI, CodeGenOpt::None))
4365 return 0;
4366
Devang Patelac1ceb32009-10-09 22:42:28 +00004367 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004368 Value *Address = DI.getAddress();
4369 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4370 Address = BCI->getOperand(0);
4371 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4372 // Don't handle byval struct arguments or VLAs, for example.
4373 if (!AI)
4374 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004375 DenseMap<const AllocaInst*, int>::iterator SI =
4376 FuncInfo.StaticAllocaMap.find(AI);
4377 if (SI == FuncInfo.StaticAllocaMap.end())
4378 return 0; // VLAs.
4379 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004380
Devang Patelac1ceb32009-10-09 22:42:28 +00004381 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Devang Patel53bb5c92009-11-10 23:06:00 +00004382 if (MMI) {
4383 MetadataContext &TheMetadata =
4384 DI.getParent()->getContext().getMetadata();
4385 unsigned MDDbgKind = TheMetadata.getMDKind("dbg");
4386 MDNode *Dbg = TheMetadata.getMD(MDDbgKind, &DI);
4387 MMI->setVariableDbgInfo(Variable, FI, Dbg);
4388 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004389 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004390 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004391 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004392 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004393 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004394 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004395 SDValue Ops[1];
4396 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004397 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004398 setValue(&I, Op);
4399 DAG.setRoot(Op.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004400 if (DisableScheduling)
4401 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004402 return 0;
4403 }
4404
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004405 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004407
Chris Lattner3a5815f2009-09-17 23:54:54 +00004408 if (CurMBB->isLandingPad())
4409 AddCatchInfo(I, MMI, CurMBB);
4410 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004411#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004412 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004413#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004414 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4415 unsigned Reg = TLI.getExceptionSelectorRegister();
4416 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004417 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004418
Chris Lattner3a5815f2009-09-17 23:54:54 +00004419 // Insert the EHSELECTION instruction.
4420 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4421 SDValue Ops[2];
4422 Ops[0] = getValue(I.getOperand(1));
4423 Ops[1] = getRoot();
4424 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4425
4426 DAG.setRoot(Op.getValue(1));
4427
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004428 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4429 setValue(&I, Res);
4430 if (DisableScheduling) {
4431 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4432 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4433 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004434 return 0;
4435 }
4436
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004437 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004438 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004439
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004440 if (MMI) {
4441 // Find the type id for the given typeinfo.
4442 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004443 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004444 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004445 } else {
4446 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004447 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004448 }
4449
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004450 setValue(&I, Res);
4451 if (DisableScheduling)
4452 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004453 return 0;
4454 }
4455
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004456 case Intrinsic::eh_return_i32:
4457 case Intrinsic::eh_return_i64:
4458 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004459 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004460 Res = DAG.getNode(ISD::EH_RETURN, dl,
4461 MVT::Other,
4462 getControlRoot(),
4463 getValue(I.getOperand(1)),
4464 getValue(I.getOperand(2)));
4465 DAG.setRoot(Res);
4466 if (DisableScheduling)
4467 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004468 } else {
4469 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4470 }
4471
4472 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004473 case Intrinsic::eh_unwind_init:
4474 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4475 MMI->setCallsUnwindInit(true);
4476 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004477 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004478 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004479 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004480 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4481 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004482 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004483 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004484 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004485 TLI.getPointerTy()),
4486 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004487 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004488 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004489 DAG.getConstant(0, TLI.getPointerTy()));
4490 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4491 FA, Offset);
4492 setValue(&I, Res);
4493 if (DisableScheduling) {
4494 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4495 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4496 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4497 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4498 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004499 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004500 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004501 case Intrinsic::convertff:
4502 case Intrinsic::convertfsi:
4503 case Intrinsic::convertfui:
4504 case Intrinsic::convertsif:
4505 case Intrinsic::convertuif:
4506 case Intrinsic::convertss:
4507 case Intrinsic::convertsu:
4508 case Intrinsic::convertus:
4509 case Intrinsic::convertuu: {
4510 ISD::CvtCode Code = ISD::CVT_INVALID;
4511 switch (Intrinsic) {
4512 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4513 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4514 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4515 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4516 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4517 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4518 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4519 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4520 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4521 }
Owen Andersone50ed302009-08-10 22:56:29 +00004522 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004523 Value *Op1 = I.getOperand(1);
4524 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4525 DAG.getValueType(DestVT),
4526 DAG.getValueType(getValue(Op1).getValueType()),
4527 getValue(I.getOperand(2)),
4528 getValue(I.getOperand(3)),
4529 Code);
4530 setValue(&I, Res);
4531 if (DisableScheduling)
4532 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004533 return 0;
4534 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004535 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004536 Res = DAG.getNode(ISD::FSQRT, dl,
4537 getValue(I.getOperand(1)).getValueType(),
4538 getValue(I.getOperand(1)));
4539 setValue(&I, Res);
4540 if (DisableScheduling)
4541 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004542 return 0;
4543 case Intrinsic::powi:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004544 Res = DAG.getNode(ISD::FPOWI, dl,
4545 getValue(I.getOperand(1)).getValueType(),
4546 getValue(I.getOperand(1)),
4547 getValue(I.getOperand(2)));
4548 setValue(&I, Res);
4549 if (DisableScheduling)
4550 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004551 return 0;
4552 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004553 Res = DAG.getNode(ISD::FSIN, dl,
4554 getValue(I.getOperand(1)).getValueType(),
4555 getValue(I.getOperand(1)));
4556 setValue(&I, Res);
4557 if (DisableScheduling)
4558 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004559 return 0;
4560 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004561 Res = DAG.getNode(ISD::FCOS, dl,
4562 getValue(I.getOperand(1)).getValueType(),
4563 getValue(I.getOperand(1)));
4564 setValue(&I, Res);
4565 if (DisableScheduling)
4566 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004567 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004568 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004569 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004570 return 0;
4571 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004572 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004573 return 0;
4574 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004575 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004576 return 0;
4577 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004578 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004579 return 0;
4580 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004581 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004582 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004583 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004584 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004585 return 0;
4586 case Intrinsic::pcmarker: {
4587 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004588 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4589 DAG.setRoot(Res);
4590 if (DisableScheduling)
4591 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004592 return 0;
4593 }
4594 case Intrinsic::readcyclecounter: {
4595 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004596 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4597 DAG.getVTList(MVT::i64, MVT::Other),
4598 &Op, 1);
4599 setValue(&I, Res);
4600 DAG.setRoot(Res.getValue(1));
4601 if (DisableScheduling)
4602 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004603 return 0;
4604 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004605 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004606 Res = DAG.getNode(ISD::BSWAP, dl,
4607 getValue(I.getOperand(1)).getValueType(),
4608 getValue(I.getOperand(1)));
4609 setValue(&I, Res);
4610 if (DisableScheduling)
4611 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004612 return 0;
4613 case Intrinsic::cttz: {
4614 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004615 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004616 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4617 setValue(&I, Res);
4618 if (DisableScheduling)
4619 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004620 return 0;
4621 }
4622 case Intrinsic::ctlz: {
4623 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004624 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004625 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4626 setValue(&I, Res);
4627 if (DisableScheduling)
4628 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004629 return 0;
4630 }
4631 case Intrinsic::ctpop: {
4632 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004633 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004634 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4635 setValue(&I, Res);
4636 if (DisableScheduling)
4637 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004638 return 0;
4639 }
4640 case Intrinsic::stacksave: {
4641 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004642 Res = DAG.getNode(ISD::STACKSAVE, dl,
4643 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4644 setValue(&I, Res);
4645 DAG.setRoot(Res.getValue(1));
4646 if (DisableScheduling)
4647 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004648 return 0;
4649 }
4650 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004651 Res = getValue(I.getOperand(1));
4652 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4653 DAG.setRoot(Res);
4654 if (DisableScheduling)
4655 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 return 0;
4657 }
Bill Wendling57344502008-11-18 11:01:33 +00004658 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004659 // Emit code into the DAG to store the stack guard onto the stack.
4660 MachineFunction &MF = DAG.getMachineFunction();
4661 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004662 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004663
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004664 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4665 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004666
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004667 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004668 MFI->setStackProtectorIndex(FI);
4669
4670 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4671
4672 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004673 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4674 PseudoSourceValue::getFixedStack(FI),
4675 0, true);
4676 setValue(&I, Res);
4677 DAG.setRoot(Res);
4678 if (DisableScheduling)
4679 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004680 return 0;
4681 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004682 case Intrinsic::objectsize: {
4683 // If we don't know by now, we're never going to know.
4684 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4685
4686 assert(CI && "Non-constant type in __builtin_object_size?");
4687
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004688 SDValue Arg = getValue(I.getOperand(0));
4689 EVT Ty = Arg.getValueType();
4690
Eric Christopher7b5e6172009-10-27 00:52:25 +00004691 if (CI->getZExtValue() < 2)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004692 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004693 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004694 Res = DAG.getConstant(0, Ty);
4695
4696 setValue(&I, Res);
4697 if (DisableScheduling)
4698 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004699 return 0;
4700 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004701 case Intrinsic::var_annotation:
4702 // Discard annotate attributes
4703 return 0;
4704
4705 case Intrinsic::init_trampoline: {
4706 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4707
4708 SDValue Ops[6];
4709 Ops[0] = getRoot();
4710 Ops[1] = getValue(I.getOperand(1));
4711 Ops[2] = getValue(I.getOperand(2));
4712 Ops[3] = getValue(I.getOperand(3));
4713 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4714 Ops[5] = DAG.getSrcValue(F);
4715
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004716 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4717 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4718 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004719
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004720 setValue(&I, Res);
4721 DAG.setRoot(Res.getValue(1));
4722 if (DisableScheduling)
4723 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004724 return 0;
4725 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004726 case Intrinsic::gcroot:
4727 if (GFI) {
4728 Value *Alloca = I.getOperand(1);
4729 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004730
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004731 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4732 GFI->addStackRoot(FI->getIndex(), TypeMap);
4733 }
4734 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004735 case Intrinsic::gcread:
4736 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004737 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004738 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004739 case Intrinsic::flt_rounds:
4740 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4741 setValue(&I, Res);
4742 if (DisableScheduling)
4743 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004744 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004745 case Intrinsic::trap:
4746 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4747 DAG.setRoot(Res);
4748 if (DisableScheduling)
4749 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004750 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004751 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004752 return implVisitAluOverflow(I, ISD::UADDO);
4753 case Intrinsic::sadd_with_overflow:
4754 return implVisitAluOverflow(I, ISD::SADDO);
4755 case Intrinsic::usub_with_overflow:
4756 return implVisitAluOverflow(I, ISD::USUBO);
4757 case Intrinsic::ssub_with_overflow:
4758 return implVisitAluOverflow(I, ISD::SSUBO);
4759 case Intrinsic::umul_with_overflow:
4760 return implVisitAluOverflow(I, ISD::UMULO);
4761 case Intrinsic::smul_with_overflow:
4762 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004763
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004764 case Intrinsic::prefetch: {
4765 SDValue Ops[4];
4766 Ops[0] = getRoot();
4767 Ops[1] = getValue(I.getOperand(1));
4768 Ops[2] = getValue(I.getOperand(2));
4769 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004770 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4771 DAG.setRoot(Res);
4772 if (DisableScheduling)
4773 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004774 return 0;
4775 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004776
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004777 case Intrinsic::memory_barrier: {
4778 SDValue Ops[6];
4779 Ops[0] = getRoot();
4780 for (int x = 1; x < 6; ++x)
4781 Ops[x] = getValue(I.getOperand(x));
4782
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004783 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4784 DAG.setRoot(Res);
4785 if (DisableScheduling)
4786 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004787 return 0;
4788 }
4789 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004790 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004791 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004792 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004793 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4794 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004795 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004796 getValue(I.getOperand(2)),
4797 getValue(I.getOperand(3)),
4798 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004799 setValue(&I, L);
4800 DAG.setRoot(L.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004801 if (DisableScheduling)
4802 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004803 return 0;
4804 }
4805 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004806 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004807 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004808 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004809 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004810 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004811 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004812 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004813 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004814 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004815 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004816 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004817 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004818 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004819 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004820 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004821 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004822 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004823 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004824 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004825 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004826 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004827
4828 case Intrinsic::invariant_start:
4829 case Intrinsic::lifetime_start:
4830 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004831 Res = DAG.getUNDEF(TLI.getPointerTy());
4832 setValue(&I, Res);
4833 if (DisableScheduling)
4834 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004835 return 0;
4836 case Intrinsic::invariant_end:
4837 case Intrinsic::lifetime_end:
4838 // Discard region information.
4839 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004840 }
4841}
4842
Dan Gohman98ca4f22009-08-05 01:29:28 +00004843/// Test if the given instruction is in a position to be optimized
4844/// with a tail-call. This roughly means that it's in a block with
4845/// a return and there's nothing that needs to be scheduled
4846/// between it and the return.
4847///
4848/// This function only tests target-independent requirements.
4849/// For target-dependent requirements, a target should override
4850/// TargetLowering::IsEligibleForTailCallOptimization.
4851///
4852static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004853isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004854 const TargetLowering &TLI) {
4855 const BasicBlock *ExitBB = I->getParent();
4856 const TerminatorInst *Term = ExitBB->getTerminator();
4857 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4858 const Function *F = ExitBB->getParent();
4859
4860 // The block must end in a return statement or an unreachable.
4861 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4862
4863 // If I will have a chain, make sure no other instruction that will have a
4864 // chain interposes between I and the return.
4865 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4866 !I->isSafeToSpeculativelyExecute())
4867 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4868 --BBI) {
4869 if (&*BBI == I)
4870 break;
4871 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4872 !BBI->isSafeToSpeculativelyExecute())
4873 return false;
4874 }
4875
4876 // If the block ends with a void return or unreachable, it doesn't matter
4877 // what the call's return type is.
4878 if (!Ret || Ret->getNumOperands() == 0) return true;
4879
Dan Gohmaned9bab32009-11-14 02:06:30 +00004880 // If the return value is undef, it doesn't matter what the call's
4881 // return type is.
4882 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4883
Dan Gohman98ca4f22009-08-05 01:29:28 +00004884 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004885 // the return. Ignore noalias because it doesn't affect the call sequence.
4886 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4887 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004888 return false;
4889
4890 // Otherwise, make sure the unmodified return value of I is the return value.
4891 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4892 U = dyn_cast<Instruction>(U->getOperand(0))) {
4893 if (!U)
4894 return false;
4895 if (!U->hasOneUse())
4896 return false;
4897 if (U == I)
4898 break;
4899 // Check for a truly no-op truncate.
4900 if (isa<TruncInst>(U) &&
4901 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4902 continue;
4903 // Check for a truly no-op bitcast.
4904 if (isa<BitCastInst>(U) &&
4905 (U->getOperand(0)->getType() == U->getType() ||
4906 (isa<PointerType>(U->getOperand(0)->getType()) &&
4907 isa<PointerType>(U->getType()))))
4908 continue;
4909 // Otherwise it's not a true no-op.
4910 return false;
4911 }
4912
4913 return true;
4914}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004915
Dan Gohman2048b852009-11-23 18:04:58 +00004916void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4917 bool isTailCall,
4918 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004919 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4920 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004921 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004922 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4923 unsigned BeginLabel = 0, EndLabel = 0;
4924
4925 TargetLowering::ArgListTy Args;
4926 TargetLowering::ArgListEntry Entry;
4927 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004928
4929 // Check whether the function can return without sret-demotion.
4930 SmallVector<EVT, 4> OutVTs;
4931 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4932 SmallVector<uint64_t, 4> Offsets;
4933 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004934 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004935
4936 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4937 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4938
4939 SDValue DemoteStackSlot;
4940
4941 if (!CanLowerReturn) {
4942 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4943 FTy->getReturnType());
4944 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4945 FTy->getReturnType());
4946 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004947 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004948 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4949
4950 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4951 Entry.Node = DemoteStackSlot;
4952 Entry.Ty = StackSlotPtrType;
4953 Entry.isSExt = false;
4954 Entry.isZExt = false;
4955 Entry.isInReg = false;
4956 Entry.isSRet = true;
4957 Entry.isNest = false;
4958 Entry.isByVal = false;
4959 Entry.Alignment = Align;
4960 Args.push_back(Entry);
4961 RetTy = Type::getVoidTy(FTy->getContext());
4962 }
4963
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004964 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004965 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004966 SDValue ArgNode = getValue(*i);
4967 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4968
4969 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004970 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4971 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4972 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4973 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4974 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4975 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004976 Entry.Alignment = CS.getParamAlignment(attrInd);
4977 Args.push_back(Entry);
4978 }
4979
4980 if (LandingPad && MMI) {
4981 // Insert a label before the invoke call to mark the try range. This can be
4982 // used to detect deletion of the invoke via the MachineModuleInfo.
4983 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004985 // Both PendingLoads and PendingExports must be flushed here;
4986 // this call might not return.
4987 (void)getRoot();
Bill Wendlinge80ae832009-12-22 00:50:32 +00004988 SDValue Label = DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4989 getControlRoot(), BeginLabel);
4990 DAG.setRoot(Label);
4991 if (DisableScheduling)
4992 DAG.AssignOrdering(Label.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004993 }
4994
Dan Gohman98ca4f22009-08-05 01:29:28 +00004995 // Check if target-independent constraints permit a tail call here.
4996 // Target-dependent constraints are checked within TLI.LowerCallTo.
4997 if (isTailCall &&
4998 !isInTailCallPosition(CS.getInstruction(),
4999 CS.getAttributes().getRetAttributes(),
5000 TLI))
5001 isTailCall = false;
5002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005003 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005004 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00005005 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005006 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005007 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005008 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00005009 isTailCall,
5010 !CS.getInstruction()->use_empty(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005011 Callee, Args, DAG, getCurDebugLoc(), SDNodeOrder);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005012 assert((isTailCall || Result.second.getNode()) &&
5013 "Non-null chain expected with non-tail call!");
5014 assert((Result.second.getNode() || !Result.first.getNode()) &&
5015 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005016 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005017 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005018 if (DisableScheduling)
5019 DAG.AssignOrdering(Result.first.getNode(), SDNodeOrder);
5020 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005021 // The instruction result is the result of loading from the
5022 // hidden sret parameter.
5023 SmallVector<EVT, 1> PVTs;
5024 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
5025
5026 ComputeValueVTs(TLI, PtrRetTy, PVTs);
5027 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5028 EVT PtrVT = PVTs[0];
5029 unsigned NumValues = OutVTs.size();
5030 SmallVector<SDValue, 4> Values(NumValues);
5031 SmallVector<SDValue, 4> Chains(NumValues);
5032
5033 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00005034 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
5035 DemoteStackSlot,
5036 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005037 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005038 Add, NULL, Offsets[i], false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005039 Values[i] = L;
5040 Chains[i] = L.getValue(1);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005041
5042 if (DisableScheduling) {
5043 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
5044 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
5045 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005046 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005047
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005048 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
5049 MVT::Other, &Chains[0], NumValues);
5050 PendingLoads.push_back(Chain);
5051
Bill Wendlinge80ae832009-12-22 00:50:32 +00005052 SDValue MV = DAG.getNode(ISD::MERGE_VALUES,
5053 getCurDebugLoc(),
5054 DAG.getVTList(&OutVTs[0], NumValues),
5055 &Values[0], NumValues);
5056 setValue(CS.getInstruction(), MV);
5057
5058 if (DisableScheduling) {
5059 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
5060 DAG.AssignOrdering(MV.getNode(), SDNodeOrder);
5061 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005062 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005063
5064 // As a special case, a null chain means that a tail call has been emitted and
5065 // the DAG root is already updated.
5066 if (Result.second.getNode()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005067 DAG.setRoot(Result.second);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005068 if (DisableScheduling)
5069 DAG.AssignOrdering(Result.second.getNode(), SDNodeOrder);
5070 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005071 HasTailCall = true;
Bill Wendlinge80ae832009-12-22 00:50:32 +00005072 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005073
5074 if (LandingPad && MMI) {
5075 // Insert a label at the end of the invoke call to mark the try range. This
5076 // can be used to detect deletion of the invoke via the MachineModuleInfo.
5077 EndLabel = MMI->NextLabelID();
Bill Wendlinge80ae832009-12-22 00:50:32 +00005078 SDValue Label = DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5079 getRoot(), EndLabel);
5080 DAG.setRoot(Label);
5081
5082 if (DisableScheduling)
5083 DAG.AssignOrdering(Label.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005084
5085 // Inform MachineModuleInfo of range.
5086 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
5087 }
5088}
5089
Dan Gohman2048b852009-11-23 18:04:58 +00005090void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005091 const char *RenameFn = 0;
5092 if (Function *F = I.getCalledFunction()) {
5093 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005094 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
5095 if (II) {
5096 if (unsigned IID = II->getIntrinsicID(F)) {
5097 RenameFn = visitIntrinsicCall(I, IID);
5098 if (!RenameFn)
5099 return;
5100 }
5101 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005102 if (unsigned IID = F->getIntrinsicID()) {
5103 RenameFn = visitIntrinsicCall(I, IID);
5104 if (!RenameFn)
5105 return;
5106 }
5107 }
5108
5109 // Check for well-known libc/libm calls. If the function is internal, it
5110 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005111 if (!F->hasLocalLinkage() && F->hasName()) {
5112 StringRef Name = F->getName();
5113 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005114 if (I.getNumOperands() == 3 && // Basic sanity checks.
5115 I.getOperand(1)->getType()->isFloatingPoint() &&
5116 I.getType() == I.getOperand(1)->getType() &&
5117 I.getType() == I.getOperand(2)->getType()) {
5118 SDValue LHS = getValue(I.getOperand(1));
5119 SDValue RHS = getValue(I.getOperand(2));
Bill Wendlingec72e322009-12-22 01:11:43 +00005120 SDValue Res = DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5121 LHS.getValueType(), LHS, RHS);
5122 setValue(&I, Res);
5123 if (DisableScheduling)
5124 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005125 return;
5126 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005127 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005128 if (I.getNumOperands() == 2 && // Basic sanity checks.
5129 I.getOperand(1)->getType()->isFloatingPoint() &&
5130 I.getType() == I.getOperand(1)->getType()) {
5131 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingec72e322009-12-22 01:11:43 +00005132 SDValue Res = DAG.getNode(ISD::FABS, getCurDebugLoc(),
5133 Tmp.getValueType(), Tmp);
5134 setValue(&I, Res);
5135 if (DisableScheduling)
5136 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005137 return;
5138 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005139 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005140 if (I.getNumOperands() == 2 && // Basic sanity checks.
5141 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005142 I.getType() == I.getOperand(1)->getType() &&
5143 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005144 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingec72e322009-12-22 01:11:43 +00005145 SDValue Res = DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5146 Tmp.getValueType(), Tmp);
5147 setValue(&I, Res);
5148 if (DisableScheduling)
5149 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005150 return;
5151 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005152 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005153 if (I.getNumOperands() == 2 && // Basic sanity checks.
5154 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005155 I.getType() == I.getOperand(1)->getType() &&
5156 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005157 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingec72e322009-12-22 01:11:43 +00005158 SDValue Res = DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5159 Tmp.getValueType(), Tmp);
5160 setValue(&I, Res);
5161 if (DisableScheduling)
5162 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005163 return;
5164 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005165 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5166 if (I.getNumOperands() == 2 && // Basic sanity checks.
5167 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005168 I.getType() == I.getOperand(1)->getType() &&
5169 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005170 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingec72e322009-12-22 01:11:43 +00005171 SDValue Res = DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5172 Tmp.getValueType(), Tmp);
5173 setValue(&I, Res);
5174 if (DisableScheduling)
5175 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005176 return;
5177 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005178 }
5179 }
5180 } else if (isa<InlineAsm>(I.getOperand(0))) {
5181 visitInlineAsm(&I);
5182 return;
5183 }
5184
5185 SDValue Callee;
5186 if (!RenameFn)
5187 Callee = getValue(I.getOperand(0));
5188 else
Bill Wendling056292f2008-09-16 21:48:12 +00005189 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005190
Bill Wendlingec72e322009-12-22 01:11:43 +00005191 if (DisableScheduling)
5192 DAG.AssignOrdering(Callee.getNode(), SDNodeOrder);
5193
Dan Gohman98ca4f22009-08-05 01:29:28 +00005194 // Check if we can potentially perform a tail call. More detailed
5195 // checking is be done within LowerCallTo, after more information
5196 // about the call is known.
5197 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5198
5199 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005200}
5201
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005202/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005203/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005204/// Chain/Flag as the input and updates them for the output Chain/Flag.
5205/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005206SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005207 unsigned Order, SDValue &Chain,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005208 SDValue *Flag) const {
5209 // Assemble the legal parts into the final values.
5210 SmallVector<SDValue, 4> Values(ValueVTs.size());
5211 SmallVector<SDValue, 8> Parts;
5212 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5213 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005214 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005215 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005216 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005217
5218 Parts.resize(NumRegs);
5219 for (unsigned i = 0; i != NumRegs; ++i) {
5220 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00005221 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005222 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005223 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005224 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005225 *Flag = P.getValue(2);
5226 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005227
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005228 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005229
Bill Wendlingec72e322009-12-22 01:11:43 +00005230 if (DisableScheduling)
5231 DAG.AssignOrdering(P.getNode(), Order);
5232
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005233 // If the source register was virtual and if we know something about it,
5234 // add an assert node.
5235 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5236 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5237 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5238 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5239 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5240 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005241
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005242 unsigned RegSize = RegisterVT.getSizeInBits();
5243 unsigned NumSignBits = LOI.NumSignBits;
5244 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005245
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005246 // FIXME: We capture more information than the dag can represent. For
5247 // now, just use the tightest assertzext/assertsext possible.
5248 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005249 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005250 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005251 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005252 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005253 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005254 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005255 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005256 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005257 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005258 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005259 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005260 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005261 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005263 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005264 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005265 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005266
Owen Anderson825b72b2009-08-11 20:47:22 +00005267 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005268 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005269 RegisterVT, P, DAG.getValueType(FromVT));
5270
Bill Wendlingec72e322009-12-22 01:11:43 +00005271 if (DisableScheduling)
5272 DAG.AssignOrdering(P.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005273 }
5274 }
5275 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005276
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005277 Parts[i] = P;
5278 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005279
Bill Wendling3ea3c242009-12-22 02:10:19 +00005280 Values[Value] = getCopyFromParts(DAG, dl, Order, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005281 NumRegs, RegisterVT, ValueVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005282 if (DisableScheduling)
5283 DAG.AssignOrdering(Values[Value].getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005284 Part += NumRegs;
5285 Parts.clear();
5286 }
5287
Bill Wendlingec72e322009-12-22 01:11:43 +00005288 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5289 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5290 &Values[0], ValueVTs.size());
5291 if (DisableScheduling)
5292 DAG.AssignOrdering(Res.getNode(), Order);
5293 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005294}
5295
5296/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005297/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005298/// Chain/Flag as the input and updates them for the output Chain/Flag.
5299/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005300void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005301 unsigned Order, SDValue &Chain,
5302 SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005303 // Get the list of the values's legal parts.
5304 unsigned NumRegs = Regs.size();
5305 SmallVector<SDValue, 8> Parts(NumRegs);
5306 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005307 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005308 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005309 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005310
Bill Wendling3ea3c242009-12-22 02:10:19 +00005311 getCopyToParts(DAG, dl, Order,
5312 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313 &Parts[Part], NumParts, RegisterVT);
5314 Part += NumParts;
5315 }
5316
5317 // Copy the parts into the registers.
5318 SmallVector<SDValue, 8> Chains(NumRegs);
5319 for (unsigned i = 0; i != NumRegs; ++i) {
5320 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00005321 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005322 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00005323 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005324 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325 *Flag = Part.getValue(1);
5326 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005327
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005328 Chains[i] = Part.getValue(0);
Bill Wendlingec72e322009-12-22 01:11:43 +00005329
5330 if (DisableScheduling)
5331 DAG.AssignOrdering(Part.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005332 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005334 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005335 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 // flagged to it. That is the CopyToReg nodes and the user are considered
5337 // a single scheduling unit. If we create a TokenFactor and return it as
5338 // chain, then the TokenFactor is both a predecessor (operand) of the
5339 // user as well as a successor (the TF operands are flagged to the user).
5340 // c1, f1 = CopyToReg
5341 // c2, f2 = CopyToReg
5342 // c3 = TokenFactor c1, c2
5343 // ...
5344 // = op c3, ..., f2
5345 Chain = Chains[NumRegs-1];
5346 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005347 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Bill Wendlingec72e322009-12-22 01:11:43 +00005348
5349 if (DisableScheduling)
5350 DAG.AssignOrdering(Chain.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005351}
5352
5353/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005354/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005355/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005356void RegsForValue::AddInlineAsmOperands(unsigned Code,
5357 bool HasMatching,unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +00005358 SelectionDAG &DAG, unsigned Order,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005359 std::vector<SDValue> &Ops) const {
Owen Andersone50ed302009-08-10 22:56:29 +00005360 EVT IntPtrTy = DAG.getTargetLoweringInfo().getPointerTy();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005361 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5362 unsigned Flag = Code | (Regs.size() << 3);
5363 if (HasMatching)
5364 Flag |= 0x80000000 | (MatchingIdx << 16);
Bill Wendling651ad132009-12-22 01:25:10 +00005365
5366 SDValue Res = DAG.getTargetConstant(Flag, IntPtrTy);
5367 Ops.push_back(Res);
5368
5369 if (DisableScheduling)
5370 DAG.AssignOrdering(Res.getNode(), Order);
5371
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005372 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005373 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005374 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005375 for (unsigned i = 0; i != NumRegs; ++i) {
5376 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling651ad132009-12-22 01:25:10 +00005377 SDValue Res = DAG.getRegister(Regs[Reg++], RegisterVT);
5378 Ops.push_back(Res);
5379
5380 if (DisableScheduling)
5381 DAG.AssignOrdering(Res.getNode(), Order);
Chris Lattner58f15c42008-10-17 16:21:11 +00005382 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005383 }
5384}
5385
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005386/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005387/// i.e. it isn't a stack pointer or some other special register, return the
5388/// register class for the register. Otherwise, return null.
5389static const TargetRegisterClass *
5390isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5391 const TargetLowering &TLI,
5392 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005393 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005394 const TargetRegisterClass *FoundRC = 0;
5395 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5396 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005397 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005398
5399 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005400 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005401 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5402 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5403 I != E; ++I) {
5404 if (TLI.isTypeLegal(*I)) {
5405 // If we have already found this register in a different register class,
5406 // choose the one with the largest VT specified. For example, on
5407 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005408 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005409 ThisVT = *I;
5410 break;
5411 }
5412 }
5413 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005414
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005416
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417 // NOTE: This isn't ideal. In particular, this might allocate the
5418 // frame pointer in functions that need it (due to them not being taken
5419 // out of allocation, because a variable sized allocation hasn't been seen
5420 // yet). This is a slight code pessimization, but should still work.
5421 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5422 E = RC->allocation_order_end(MF); I != E; ++I)
5423 if (*I == Reg) {
5424 // We found a matching register class. Keep looking at others in case
5425 // we find one with larger registers that this physreg is also in.
5426 FoundRC = RC;
5427 FoundVT = ThisVT;
5428 break;
5429 }
5430 }
5431 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005432}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005433
5434
5435namespace llvm {
5436/// AsmOperandInfo - This contains information for each constraint that we are
5437/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005438class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005439 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005440public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005441 /// CallOperand - If this is the result output operand or a clobber
5442 /// this is null, otherwise it is the incoming operand to the CallInst.
5443 /// This gets modified as the asm is processed.
5444 SDValue CallOperand;
5445
5446 /// AssignedRegs - If this is a register or register class operand, this
5447 /// contains the set of register corresponding to the operand.
5448 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005449
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005450 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5451 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5452 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005453
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005454 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5455 /// busy in OutputRegs/InputRegs.
5456 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005457 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005458 std::set<unsigned> &InputRegs,
5459 const TargetRegisterInfo &TRI) const {
5460 if (isOutReg) {
5461 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5462 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5463 }
5464 if (isInReg) {
5465 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5466 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5467 }
5468 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005469
Owen Andersone50ed302009-08-10 22:56:29 +00005470 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005471 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005472 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00005473 EVT getCallOperandValEVT(LLVMContext &Context,
5474 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005475 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005476 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005477
Chris Lattner81249c92008-10-17 17:05:25 +00005478 if (isa<BasicBlock>(CallOperandVal))
5479 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005480
Chris Lattner81249c92008-10-17 17:05:25 +00005481 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005482
Chris Lattner81249c92008-10-17 17:05:25 +00005483 // If this is an indirect operand, the operand is a pointer to the
5484 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005485 if (isIndirect) {
5486 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5487 if (!PtrTy)
5488 llvm_report_error("Indirect operand for inline asm not a pointer!");
5489 OpTy = PtrTy->getElementType();
5490 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005491
Chris Lattner81249c92008-10-17 17:05:25 +00005492 // If OpTy is not a single value, it may be a struct/union that we
5493 // can tile with integers.
5494 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5495 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5496 switch (BitSize) {
5497 default: break;
5498 case 1:
5499 case 8:
5500 case 16:
5501 case 32:
5502 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005503 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005504 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005505 break;
5506 }
5507 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005508
Chris Lattner81249c92008-10-17 17:05:25 +00005509 return TLI.getValueType(OpTy, true);
5510 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005511
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005512private:
5513 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5514 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005515 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005516 const TargetRegisterInfo &TRI) {
5517 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5518 Regs.insert(Reg);
5519 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5520 for (; *Aliases; ++Aliases)
5521 Regs.insert(*Aliases);
5522 }
5523};
5524} // end llvm namespace.
5525
5526
5527/// GetRegistersForValue - Assign registers (virtual or physical) for the
5528/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005529/// register allocator to handle the assignment process. However, if the asm
5530/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005531/// allocation. This produces generally horrible, but correct, code.
5532///
5533/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005534/// Input and OutputRegs are the set of already allocated physical registers.
5535///
Dan Gohman2048b852009-11-23 18:04:58 +00005536void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005537GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005538 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005539 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005540 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005541
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005542 // Compute whether this value requires an input register, an output register,
5543 // or both.
5544 bool isOutReg = false;
5545 bool isInReg = false;
5546 switch (OpInfo.Type) {
5547 case InlineAsm::isOutput:
5548 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005549
5550 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005551 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005552 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005553 break;
5554 case InlineAsm::isInput:
5555 isInReg = true;
5556 isOutReg = false;
5557 break;
5558 case InlineAsm::isClobber:
5559 isOutReg = true;
5560 isInReg = true;
5561 break;
5562 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005563
5564
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005565 MachineFunction &MF = DAG.getMachineFunction();
5566 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005567
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005568 // If this is a constraint for a single physreg, or a constraint for a
5569 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005570 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005571 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5572 OpInfo.ConstraintVT);
5573
5574 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005575 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005576 // If this is a FP input in an integer register (or visa versa) insert a bit
5577 // cast of the input value. More generally, handle any case where the input
5578 // value disagrees with the register class we plan to stick this in.
5579 if (OpInfo.Type == InlineAsm::isInput &&
5580 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005581 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005582 // types are identical size, use a bitcast to convert (e.g. two differing
5583 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005584 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005585 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005586 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005587 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005588 OpInfo.ConstraintVT = RegVT;
5589 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5590 // If the input is a FP value and we want it in FP registers, do a
5591 // bitcast to the corresponding integer type. This turns an f64 value
5592 // into i64, which can be passed with two i32 values on a 32-bit
5593 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005594 RegVT = EVT::getIntegerVT(Context,
5595 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005596 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005597 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005598 OpInfo.ConstraintVT = RegVT;
5599 }
Bill Wendling651ad132009-12-22 01:25:10 +00005600
5601 if (DisableScheduling)
5602 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Chris Lattner01426e12008-10-21 00:45:36 +00005603 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005604
Owen Anderson23b9b192009-08-12 00:36:31 +00005605 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005606 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005607
Owen Andersone50ed302009-08-10 22:56:29 +00005608 EVT RegVT;
5609 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005610
5611 // If this is a constraint for a specific physical register, like {r17},
5612 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005613 if (unsigned AssignedReg = PhysReg.first) {
5614 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005615 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005616 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005617
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005618 // Get the actual register value type. This is important, because the user
5619 // may have asked for (e.g.) the AX register in i32 type. We need to
5620 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005621 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005622
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005623 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005624 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005625
5626 // If this is an expanded reference, add the rest of the regs to Regs.
5627 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005628 TargetRegisterClass::iterator I = RC->begin();
5629 for (; *I != AssignedReg; ++I)
5630 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005631
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005632 // Already added the first reg.
5633 --NumRegs; ++I;
5634 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005635 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005636 Regs.push_back(*I);
5637 }
5638 }
Bill Wendling651ad132009-12-22 01:25:10 +00005639
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005640 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5641 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5642 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5643 return;
5644 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005645
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005646 // Otherwise, if this was a reference to an LLVM register class, create vregs
5647 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005648 if (const TargetRegisterClass *RC = PhysReg.second) {
5649 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005650 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005651 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652
Evan Chengfb112882009-03-23 08:01:15 +00005653 // Create the appropriate number of virtual registers.
5654 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5655 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005656 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005657
Evan Chengfb112882009-03-23 08:01:15 +00005658 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5659 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005660 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005661
5662 // This is a reference to a register class that doesn't directly correspond
5663 // to an LLVM register class. Allocate NumRegs consecutive, available,
5664 // registers from the class.
5665 std::vector<unsigned> RegClassRegs
5666 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5667 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005668
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005669 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5670 unsigned NumAllocated = 0;
5671 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5672 unsigned Reg = RegClassRegs[i];
5673 // See if this register is available.
5674 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5675 (isInReg && InputRegs.count(Reg))) { // Already used.
5676 // Make sure we find consecutive registers.
5677 NumAllocated = 0;
5678 continue;
5679 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005681 // Check to see if this register is allocatable (i.e. don't give out the
5682 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005683 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5684 if (!RC) { // Couldn't allocate this register.
5685 // Reset NumAllocated to make sure we return consecutive registers.
5686 NumAllocated = 0;
5687 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005688 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005689
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005690 // Okay, this register is good, we can use it.
5691 ++NumAllocated;
5692
5693 // If we allocated enough consecutive registers, succeed.
5694 if (NumAllocated == NumRegs) {
5695 unsigned RegStart = (i-NumAllocated)+1;
5696 unsigned RegEnd = i+1;
5697 // Mark all of the allocated registers used.
5698 for (unsigned i = RegStart; i != RegEnd; ++i)
5699 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005700
5701 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005702 OpInfo.ConstraintVT);
5703 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5704 return;
5705 }
5706 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005707
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005708 // Otherwise, we couldn't allocate enough registers for this.
5709}
5710
Evan Chengda43bcf2008-09-24 00:05:32 +00005711/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5712/// processed uses a memory 'm' constraint.
5713static bool
5714hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005715 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005716 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5717 InlineAsm::ConstraintInfo &CI = CInfos[i];
5718 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5719 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5720 if (CType == TargetLowering::C_Memory)
5721 return true;
5722 }
Chris Lattner6c147292009-04-30 00:48:50 +00005723
5724 // Indirect operand accesses access memory.
5725 if (CI.isIndirect)
5726 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005727 }
5728
5729 return false;
5730}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005731
5732/// visitInlineAsm - Handle a call to an InlineAsm object.
5733///
Dan Gohman2048b852009-11-23 18:04:58 +00005734void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005735 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5736
5737 /// ConstraintOperands - Information about all of the constraints.
5738 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005739
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005740 std::set<unsigned> OutputRegs, InputRegs;
5741
5742 // Do a prepass over the constraints, canonicalizing them, and building up the
5743 // ConstraintOperands list.
5744 std::vector<InlineAsm::ConstraintInfo>
5745 ConstraintInfos = IA->ParseConstraints();
5746
Evan Chengda43bcf2008-09-24 00:05:32 +00005747 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005748
5749 SDValue Chain, Flag;
5750
5751 // We won't need to flush pending loads if this asm doesn't touch
5752 // memory and is nonvolatile.
5753 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005754 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005755 else
5756 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005757
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005758 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5759 unsigned ResNo = 0; // ResNo - The result number of the next output.
5760 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5761 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5762 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005763
Owen Anderson825b72b2009-08-11 20:47:22 +00005764 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005765
5766 // Compute the value type for each operand.
5767 switch (OpInfo.Type) {
5768 case InlineAsm::isOutput:
5769 // Indirect outputs just consume an argument.
5770 if (OpInfo.isIndirect) {
5771 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5772 break;
5773 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005774
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005775 // The return value of the call is this value. As such, there is no
5776 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005777 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5778 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005779 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5780 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5781 } else {
5782 assert(ResNo == 0 && "Asm only has one result!");
5783 OpVT = TLI.getValueType(CS.getType());
5784 }
5785 ++ResNo;
5786 break;
5787 case InlineAsm::isInput:
5788 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5789 break;
5790 case InlineAsm::isClobber:
5791 // Nothing to do.
5792 break;
5793 }
5794
5795 // If this is an input or an indirect output, process the call argument.
5796 // BasicBlocks are labels, currently appearing only in asm's.
5797 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005798 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005799 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5800
Chris Lattner81249c92008-10-17 17:05:25 +00005801 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005803 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005804 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005805 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005806
Owen Anderson1d0be152009-08-13 21:58:54 +00005807 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005809
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005810 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005811 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005812
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005813 // Second pass over the constraints: compute which constraint option to use
5814 // and assign registers to constraints that want a specific physreg.
5815 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5816 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005817
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005818 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005819 // matching input. If their types mismatch, e.g. one is an integer, the
5820 // other is floating point, or their sizes are different, flag it as an
5821 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005822 if (OpInfo.hasMatchingInput()) {
5823 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5824 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005825 if ((OpInfo.ConstraintVT.isInteger() !=
5826 Input.ConstraintVT.isInteger()) ||
5827 (OpInfo.ConstraintVT.getSizeInBits() !=
5828 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005829 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005830 " with a matching output constraint of incompatible"
5831 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005832 }
5833 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005834 }
5835 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005836
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005837 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005838 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005839
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005840 // If this is a memory input, and if the operand is not indirect, do what we
5841 // need to to provide an address for the memory input.
5842 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5843 !OpInfo.isIndirect) {
5844 assert(OpInfo.Type == InlineAsm::isInput &&
5845 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005846
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005847 // Memory operands really want the address of the value. If we don't have
5848 // an indirect input, put it in the constpool if we can, otherwise spill
5849 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005850
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005851 // If the operand is a float, integer, or vector constant, spill to a
5852 // constant pool entry to get its address.
5853 Value *OpVal = OpInfo.CallOperandVal;
5854 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5855 isa<ConstantVector>(OpVal)) {
5856 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5857 TLI.getPointerTy());
5858 } else {
5859 // Otherwise, create a stack slot and emit a store to it before the
5860 // asm.
5861 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005862 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005863 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5864 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005865 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005866 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005867 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005868 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005869 OpInfo.CallOperand = StackSlot;
Bill Wendling651ad132009-12-22 01:25:10 +00005870 if (DisableScheduling)
5871 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005872 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005873
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005874 // There is no longer a Value* corresponding to this operand.
5875 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005877 // It is now an indirect operand.
5878 OpInfo.isIndirect = true;
Bill Wendling651ad132009-12-22 01:25:10 +00005879
5880 if (DisableScheduling)
5881 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005882 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005883
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005884 // If this constraint is for a specific register, allocate it before
5885 // anything else.
5886 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005887 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005888 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005889
Bill Wendling651ad132009-12-22 01:25:10 +00005890 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005891
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005892 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005893 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005894 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5895 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005896
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005897 // C_Register operands have already been allocated, Other/Memory don't need
5898 // to be.
5899 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005900 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005901 }
5902
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005903 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5904 std::vector<SDValue> AsmNodeOperands;
5905 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5906 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00005907 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005908
5909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005910 // Loop over all of the inputs, copying the operand values into the
5911 // appropriate registers and processing the output regs.
5912 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005914 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5915 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005916
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005917 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5918 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5919
5920 switch (OpInfo.Type) {
5921 case InlineAsm::isOutput: {
5922 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5923 OpInfo.ConstraintType != TargetLowering::C_Register) {
5924 // Memory output, or 'other' output (e.g. 'X' constraint).
5925 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5926
5927 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005928 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5929 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005930 TLI.getPointerTy()));
5931 AsmNodeOperands.push_back(OpInfo.CallOperand);
5932 break;
5933 }
5934
5935 // Otherwise, this is a register or register class output.
5936
5937 // Copy the output from the appropriate register. Find a register that
5938 // we can use.
5939 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005940 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005941 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005942 }
5943
5944 // If this is an indirect operand, store through the pointer after the
5945 // asm.
5946 if (OpInfo.isIndirect) {
5947 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5948 OpInfo.CallOperandVal));
5949 } else {
5950 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00005951 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5952 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005953 // Concatenate this output onto the outputs list.
5954 RetValRegs.append(OpInfo.AssignedRegs);
5955 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005956
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005957 // Add information to the INLINEASM node to know that this register is
5958 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005959 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5960 6 /* EARLYCLOBBER REGDEF */ :
5961 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005962 false,
5963 0,
Bill Wendling651ad132009-12-22 01:25:10 +00005964 DAG, SDNodeOrder,
5965 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005966 break;
5967 }
5968 case InlineAsm::isInput: {
5969 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005970
Chris Lattner6bdcda32008-10-17 16:47:46 +00005971 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005972 // If this is required to match an output register we have already set,
5973 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005974 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005975
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005976 // Scan until we find the definition we already emitted of this operand.
5977 // When we find it, create a RegsForValue operand.
5978 unsigned CurOp = 2; // The first operand.
5979 for (; OperandNo; --OperandNo) {
5980 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005981 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005982 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005983 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5984 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5985 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005986 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005987 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005988 }
5989
Evan Cheng697cbbf2009-03-20 18:03:34 +00005990 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005991 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005992 if ((OpFlag & 7) == 2 /*REGDEF*/
5993 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5994 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005995 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005996 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005997 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005998 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005999 RegsForValue MatchedRegs;
6000 MatchedRegs.TLI = &TLI;
6001 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00006002 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006003 MatchedRegs.RegVTs.push_back(RegVT);
6004 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006005 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00006006 i != e; ++i)
6007 MatchedRegs.Regs.
6008 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006009
6010 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00006011 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006012 SDNodeOrder, Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00006013 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
6014 true, OpInfo.getMatchedOperand(),
Bill Wendling651ad132009-12-22 01:25:10 +00006015 DAG, SDNodeOrder, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006016 break;
6017 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006018 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
6019 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
6020 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006021 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00006022 // See InlineAsm.h isUseOperandTiedToDef.
6023 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00006024 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006025 TLI.getPointerTy()));
6026 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6027 break;
6028 }
6029 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006031 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006032 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006033 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006035 std::vector<SDValue> Ops;
6036 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00006037 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006038 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006039 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00006040 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006041 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006042
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006043 // Add information to the INLINEASM node to know about this input.
6044 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006045 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006046 TLI.getPointerTy()));
6047 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6048 break;
6049 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
6050 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
6051 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6052 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006054 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006055 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6056 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006057 TLI.getPointerTy()));
6058 AsmNodeOperands.push_back(InOperandVal);
6059 break;
6060 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006061
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006062 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6063 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6064 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006065 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006066 "Don't know how to handle indirect register inputs yet!");
6067
6068 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00006069 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006070 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006071 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00006072 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006073
Dale Johannesen66978ee2009-01-31 02:22:37 +00006074 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006075 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006076
Evan Cheng697cbbf2009-03-20 18:03:34 +00006077 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006078 DAG, SDNodeOrder,
6079 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006080 break;
6081 }
6082 case InlineAsm::isClobber: {
6083 // Add the clobbered value to the operand list, so that the register
6084 // allocator is aware that the physreg got clobbered.
6085 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00006086 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Bill Wendling651ad132009-12-22 01:25:10 +00006087 false, 0, DAG, SDNodeOrder,
6088 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006089 break;
6090 }
6091 }
6092 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006093
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006094 // Finish up input operands.
6095 AsmNodeOperands[0] = Chain;
6096 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006097
Dale Johannesen66978ee2009-01-31 02:22:37 +00006098 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006099 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006100 &AsmNodeOperands[0], AsmNodeOperands.size());
6101 Flag = Chain.getValue(1);
6102
Bill Wendling651ad132009-12-22 01:25:10 +00006103 if (DisableScheduling)
6104 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
6105
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006106 // If this asm returns a register value, copy the result from that register
6107 // and set it as the value of the call.
6108 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006109 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006110 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006111
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006112 // FIXME: Why don't we do this for inline asms with MRVs?
6113 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006114 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006115
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006116 // If any of the results of the inline asm is a vector, it may have the
6117 // wrong width/num elts. This can happen for register classes that can
6118 // contain multiple different value types. The preg or vreg allocated may
6119 // not have the same VT as was expected. Convert it to the right type
6120 // with bit_convert.
6121 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006122 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006123 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006124
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006125 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006126 ResultType.isInteger() && Val.getValueType().isInteger()) {
6127 // If a result value was tied to an input value, the computed result may
6128 // have a wider width than the expected result. Extract the relevant
6129 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006130 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006131 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006132
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006133 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Bill Wendling651ad132009-12-22 01:25:10 +00006134
6135 if (DisableScheduling)
6136 DAG.AssignOrdering(Val.getNode(), SDNodeOrder);
Chris Lattner0c526442008-10-17 17:52:49 +00006137 }
Dan Gohman95915732008-10-18 01:03:45 +00006138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006139 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006140 // Don't need to use this as a chain in this case.
6141 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6142 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006143 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006147 // Process indirect outputs, first output all of the flagged copies out of
6148 // physregs.
6149 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6150 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
6151 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00006152 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006153 SDNodeOrder, Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006154 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00006155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006156 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006158 // Emit the non-flagged stores from the physregs.
6159 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006160 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6161 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6162 StoresToEmit[i].first,
6163 getValue(StoresToEmit[i].second),
6164 StoresToEmit[i].second, 0);
6165 OutChains.push_back(Val);
6166 if (DisableScheduling)
6167 DAG.AssignOrdering(Val.getNode(), SDNodeOrder);
6168 }
6169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006170 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006171 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006172 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006173
6174 if (DisableScheduling)
6175 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
6176
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006177 DAG.setRoot(Chain);
6178}
6179
Dan Gohman2048b852009-11-23 18:04:58 +00006180void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Bill Wendling651ad132009-12-22 01:25:10 +00006181 SDValue Res = DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6182 MVT::Other, getRoot(),
6183 getValue(I.getOperand(1)),
6184 DAG.getSrcValue(I.getOperand(1)));
6185 DAG.setRoot(Res);
6186 if (DisableScheduling)
6187 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006188}
6189
Dan Gohman2048b852009-11-23 18:04:58 +00006190void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006191 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6192 getRoot(), getValue(I.getOperand(0)),
6193 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006194 setValue(&I, V);
6195 DAG.setRoot(V.getValue(1));
Bill Wendling651ad132009-12-22 01:25:10 +00006196 if (DisableScheduling)
6197 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006198}
6199
Dan Gohman2048b852009-11-23 18:04:58 +00006200void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Bill Wendling651ad132009-12-22 01:25:10 +00006201 SDValue Res = DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6202 MVT::Other, getRoot(),
6203 getValue(I.getOperand(1)),
6204 DAG.getSrcValue(I.getOperand(1)));
6205 DAG.setRoot(Res);
6206 if (DisableScheduling)
6207 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006208}
6209
Dan Gohman2048b852009-11-23 18:04:58 +00006210void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Bill Wendling651ad132009-12-22 01:25:10 +00006211 SDValue Res = DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6212 MVT::Other, getRoot(),
6213 getValue(I.getOperand(1)),
6214 getValue(I.getOperand(2)),
6215 DAG.getSrcValue(I.getOperand(1)),
6216 DAG.getSrcValue(I.getOperand(2)));
6217 DAG.setRoot(Res);
6218 if (DisableScheduling)
6219 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006220}
6221
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006222/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006223/// implementation, which just calls LowerCall.
6224/// FIXME: When all targets are
6225/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006226std::pair<SDValue, SDValue>
6227TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6228 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006229 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006230 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006231 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006232 SDValue Callee,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006233 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl,
6234 unsigned Order) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00006235 assert((!isTailCall || PerformTailCallOpt) &&
6236 "isTailCall set when tail-call optimizations are disabled!");
6237
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006238 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006239 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006240 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006241 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006242 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6243 for (unsigned Value = 0, NumValues = ValueVTs.size();
6244 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006245 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006246 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006247 SDValue Op = SDValue(Args[i].Node.getNode(),
6248 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006249 ISD::ArgFlagsTy Flags;
6250 unsigned OriginalAlignment =
6251 getTargetData()->getABITypeAlignment(ArgTy);
6252
6253 if (Args[i].isZExt)
6254 Flags.setZExt();
6255 if (Args[i].isSExt)
6256 Flags.setSExt();
6257 if (Args[i].isInReg)
6258 Flags.setInReg();
6259 if (Args[i].isSRet)
6260 Flags.setSRet();
6261 if (Args[i].isByVal) {
6262 Flags.setByVal();
6263 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6264 const Type *ElementTy = Ty->getElementType();
6265 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006266 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006267 // For ByVal, alignment should come from FE. BE will guess if this
6268 // info is not there but there are cases it cannot get right.
6269 if (Args[i].Alignment)
6270 FrameAlign = Args[i].Alignment;
6271 Flags.setByValAlign(FrameAlign);
6272 Flags.setByValSize(FrameSize);
6273 }
6274 if (Args[i].isNest)
6275 Flags.setNest();
6276 Flags.setOrigAlign(OriginalAlignment);
6277
Owen Anderson23b9b192009-08-12 00:36:31 +00006278 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6279 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006280 SmallVector<SDValue, 4> Parts(NumParts);
6281 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6282
6283 if (Args[i].isSExt)
6284 ExtendKind = ISD::SIGN_EXTEND;
6285 else if (Args[i].isZExt)
6286 ExtendKind = ISD::ZERO_EXTEND;
6287
Bill Wendling3ea3c242009-12-22 02:10:19 +00006288 getCopyToParts(DAG, dl, Order, Op, &Parts[0], NumParts,
6289 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006290
Dan Gohman98ca4f22009-08-05 01:29:28 +00006291 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006292 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006293 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6294 if (NumParts > 1 && j == 0)
6295 MyFlags.Flags.setSplit();
6296 else if (j != 0)
6297 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006298
Dan Gohman98ca4f22009-08-05 01:29:28 +00006299 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006300 }
6301 }
6302 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006303
Dan Gohman98ca4f22009-08-05 01:29:28 +00006304 // Handle the incoming return values from the call.
6305 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006306 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006307 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006308 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006309 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006310 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6311 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006312 for (unsigned i = 0; i != NumRegs; ++i) {
6313 ISD::InputArg MyFlags;
6314 MyFlags.VT = RegisterVT;
6315 MyFlags.Used = isReturnValueUsed;
6316 if (RetSExt)
6317 MyFlags.Flags.setSExt();
6318 if (RetZExt)
6319 MyFlags.Flags.setZExt();
6320 if (isInreg)
6321 MyFlags.Flags.setInReg();
6322 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006323 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006324 }
6325
Dan Gohman98ca4f22009-08-05 01:29:28 +00006326 // Check if target-dependent constraints permit a tail call here.
6327 // Target-independent constraints should be checked by the caller.
6328 if (isTailCall &&
6329 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6330 isTailCall = false;
6331
6332 SmallVector<SDValue, 4> InVals;
6333 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6334 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006335
6336 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006337 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006338 "LowerCall didn't return a valid chain!");
6339 assert((!isTailCall || InVals.empty()) &&
6340 "LowerCall emitted a return value for a tail call!");
6341 assert((isTailCall || InVals.size() == Ins.size()) &&
6342 "LowerCall didn't emit the correct number of values!");
6343 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6344 assert(InVals[i].getNode() &&
6345 "LowerCall emitted a null value!");
6346 assert(Ins[i].VT == InVals[i].getValueType() &&
6347 "LowerCall emitted a value with the wrong type!");
6348 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006349
Bill Wendling3ea3c242009-12-22 02:10:19 +00006350 if (DisableScheduling)
6351 DAG.AssignOrdering(Chain.getNode(), Order);
6352
Dan Gohman98ca4f22009-08-05 01:29:28 +00006353 // For a tail call, the return value is merely live-out and there aren't
6354 // any nodes in the DAG representing it. Return a special value to
6355 // indicate that a tail call has been emitted and no more Instructions
6356 // should be processed in the current block.
6357 if (isTailCall) {
6358 DAG.setRoot(Chain);
6359 return std::make_pair(SDValue(), SDValue());
6360 }
6361
6362 // Collect the legal value parts into potentially illegal values
6363 // that correspond to the original function's return values.
6364 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6365 if (RetSExt)
6366 AssertOp = ISD::AssertSext;
6367 else if (RetZExt)
6368 AssertOp = ISD::AssertZext;
6369 SmallVector<SDValue, 4> ReturnValues;
6370 unsigned CurReg = 0;
6371 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006372 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006373 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6374 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006375
6376 SDValue ReturnValue =
Bill Wendling3ea3c242009-12-22 02:10:19 +00006377 getCopyFromParts(DAG, dl, Order, &InVals[CurReg], NumRegs,
6378 RegisterVT, VT, AssertOp);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006379 ReturnValues.push_back(ReturnValue);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006380 if (DisableScheduling)
6381 DAG.AssignOrdering(ReturnValue.getNode(), Order);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006382 CurReg += NumRegs;
6383 }
6384
6385 // For a function returning void, there is no return value. We can't create
6386 // such a node, so we just return a null return value in that case. In
6387 // that case, nothing will actualy look at the value.
6388 if (ReturnValues.empty())
6389 return std::make_pair(SDValue(), Chain);
6390
6391 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6392 DAG.getVTList(&RetTys[0], RetTys.size()),
6393 &ReturnValues[0], ReturnValues.size());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006394 if (DisableScheduling)
6395 DAG.AssignOrdering(Res.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006396 return std::make_pair(Res, Chain);
6397}
6398
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006399void TargetLowering::LowerOperationWrapper(SDNode *N,
6400 SmallVectorImpl<SDValue> &Results,
6401 SelectionDAG &DAG) {
6402 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006403 if (Res.getNode())
6404 Results.push_back(Res);
6405}
6406
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006407SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006408 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006409 return SDValue();
6410}
6411
Dan Gohman2048b852009-11-23 18:04:58 +00006412void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006413 SDValue Op = getValue(V);
6414 assert((Op.getOpcode() != ISD::CopyFromReg ||
6415 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6416 "Copy from a reg to the same reg!");
6417 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6418
Owen Anderson23b9b192009-08-12 00:36:31 +00006419 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006420 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +00006421 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006422 PendingExports.push_back(Chain);
6423}
6424
6425#include "llvm/CodeGen/SelectionDAGISel.h"
6426
Dan Gohman8c2b5252009-10-30 01:27:03 +00006427void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006428 // If this is the entry block, emit arguments.
6429 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006430 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006431 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006432 DebugLoc dl = SDB->getCurDebugLoc();
Bill Wendling3ea3c242009-12-22 02:10:19 +00006433 unsigned Order = SDB->getSDNodeOrder();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006434 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006435 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006436
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006437 // Check whether the function can return without sret-demotion.
6438 SmallVector<EVT, 4> OutVTs;
6439 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006440 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
6441 OutVTs, OutsFlags, TLI);
6442 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6443
6444 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00006445 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006446 if (!FLI.CanLowerReturn) {
6447 // Put in an sret pointer parameter before all the other parameters.
6448 SmallVector<EVT, 1> ValueVTs;
6449 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6450
6451 // NOTE: Assuming that a pointer will never break down to more than one VT
6452 // or one register.
6453 ISD::ArgFlagsTy Flags;
6454 Flags.setSRet();
6455 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6456 ISD::InputArg RetArg(Flags, RegisterVT, true);
6457 Ins.push_back(RetArg);
6458 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006459
Dan Gohman98ca4f22009-08-05 01:29:28 +00006460 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006461 unsigned Idx = 1;
6462 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6463 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006464 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006465 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6466 bool isArgValueUsed = !I->use_empty();
6467 for (unsigned Value = 0, NumValues = ValueVTs.size();
6468 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006469 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006470 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006471 ISD::ArgFlagsTy Flags;
6472 unsigned OriginalAlignment =
6473 TD->getABITypeAlignment(ArgTy);
6474
6475 if (F.paramHasAttr(Idx, Attribute::ZExt))
6476 Flags.setZExt();
6477 if (F.paramHasAttr(Idx, Attribute::SExt))
6478 Flags.setSExt();
6479 if (F.paramHasAttr(Idx, Attribute::InReg))
6480 Flags.setInReg();
6481 if (F.paramHasAttr(Idx, Attribute::StructRet))
6482 Flags.setSRet();
6483 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6484 Flags.setByVal();
6485 const PointerType *Ty = cast<PointerType>(I->getType());
6486 const Type *ElementTy = Ty->getElementType();
6487 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6488 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6489 // For ByVal, alignment should be passed from FE. BE will guess if
6490 // this info is not there but there are cases it cannot get right.
6491 if (F.getParamAlignment(Idx))
6492 FrameAlign = F.getParamAlignment(Idx);
6493 Flags.setByValAlign(FrameAlign);
6494 Flags.setByValSize(FrameSize);
6495 }
6496 if (F.paramHasAttr(Idx, Attribute::Nest))
6497 Flags.setNest();
6498 Flags.setOrigAlign(OriginalAlignment);
6499
Owen Anderson23b9b192009-08-12 00:36:31 +00006500 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6501 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006502 for (unsigned i = 0; i != NumRegs; ++i) {
6503 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6504 if (NumRegs > 1 && i == 0)
6505 MyFlags.Flags.setSplit();
6506 // if it isn't first piece, alignment must be 1
6507 else if (i > 0)
6508 MyFlags.Flags.setOrigAlign(1);
6509 Ins.push_back(MyFlags);
6510 }
6511 }
6512 }
6513
6514 // Call the target to set up the argument values.
6515 SmallVector<SDValue, 8> InVals;
6516 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6517 F.isVarArg(), Ins,
6518 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006519
6520 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006521 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006522 "LowerFormalArguments didn't return a valid chain!");
6523 assert(InVals.size() == Ins.size() &&
6524 "LowerFormalArguments didn't emit the correct number of values!");
6525 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6526 assert(InVals[i].getNode() &&
6527 "LowerFormalArguments emitted a null value!");
6528 assert(Ins[i].VT == InVals[i].getValueType() &&
6529 "LowerFormalArguments emitted a value with the wrong type!");
6530 });
6531
Bill Wendling3ea3c242009-12-22 02:10:19 +00006532 if (DisableScheduling)
6533 DAG.AssignOrdering(NewRoot.getNode(), Order);
6534
Dan Gohman5e866062009-08-06 15:37:27 +00006535 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006536 DAG.setRoot(NewRoot);
6537
6538 // Set up the argument values.
6539 unsigned i = 0;
6540 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006541 if (!FLI.CanLowerReturn) {
6542 // Create a virtual register for the sret pointer, and put in a copy
6543 // from the sret argument into it.
6544 SmallVector<EVT, 1> ValueVTs;
6545 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6546 EVT VT = ValueVTs[0];
6547 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6548 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling3ea3c242009-12-22 02:10:19 +00006549 SDValue ArgValue = getCopyFromParts(DAG, dl, Order, &InVals[0], 1,
6550 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006551
Dan Gohman2048b852009-11-23 18:04:58 +00006552 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006553 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6554 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6555 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00006556 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006557 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006558 if (DisableScheduling)
6559 DAG.AssignOrdering(NewRoot.getNode(), Order);
6560
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006561 // i indexes lowered arguments. Bump it past the hidden sret argument.
6562 // Idx indexes LLVM arguments. Don't touch it.
6563 ++i;
6564 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006565
Dan Gohman98ca4f22009-08-05 01:29:28 +00006566 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6567 ++I, ++Idx) {
6568 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006569 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006570 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006571 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006572 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006573 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006574 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6575 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006576
6577 if (!I->use_empty()) {
6578 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6579 if (F.paramHasAttr(Idx, Attribute::SExt))
6580 AssertOp = ISD::AssertSext;
6581 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6582 AssertOp = ISD::AssertZext;
6583
Bill Wendling3ea3c242009-12-22 02:10:19 +00006584 ArgValues.push_back(getCopyFromParts(DAG, dl, Order, &InVals[i],
6585 NumParts, PartVT, VT,
6586 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006587 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006588
Dan Gohman98ca4f22009-08-05 01:29:28 +00006589 i += NumParts;
6590 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006591
Dan Gohman98ca4f22009-08-05 01:29:28 +00006592 if (!I->use_empty()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +00006593 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6594 SDB->getCurDebugLoc());
6595 SDB->setValue(I, Res);
6596
6597 if (DisableScheduling)
6598 DAG.AssignOrdering(Res.getNode(), Order);
6599
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006600 // If this argument is live outside of the entry block, insert a copy from
6601 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006602 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006603 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006604 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006605
Dan Gohman98ca4f22009-08-05 01:29:28 +00006606 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006607
6608 // Finally, if the target has anything special to do, allow it to do so.
6609 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006610 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006611}
6612
6613/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6614/// ensure constants are generated when needed. Remember the virtual registers
6615/// that need to be added to the Machine PHI nodes as input. We cannot just
6616/// directly add them, because expansion might result in multiple MBB's for one
6617/// BB. As such, the start of the BB might correspond to a different MBB than
6618/// the end.
6619///
6620void
6621SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6622 TerminatorInst *TI = LLVMBB->getTerminator();
6623
6624 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6625
6626 // Check successor nodes' PHI nodes that expect a constant to be available
6627 // from this block.
6628 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6629 BasicBlock *SuccBB = TI->getSuccessor(succ);
6630 if (!isa<PHINode>(SuccBB->begin())) continue;
6631 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006632
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006633 // If this terminator has multiple identical successors (common for
6634 // switches), only handle each succ once.
6635 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006636
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006637 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6638 PHINode *PN;
6639
6640 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6641 // nodes and Machine PHI nodes, but the incoming operands have not been
6642 // emitted yet.
6643 for (BasicBlock::iterator I = SuccBB->begin();
6644 (PN = dyn_cast<PHINode>(I)); ++I) {
6645 // Ignore dead phi's.
6646 if (PN->use_empty()) continue;
6647
6648 unsigned Reg;
6649 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6650
6651 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006652 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006653 if (RegOut == 0) {
6654 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006655 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006656 }
6657 Reg = RegOut;
6658 } else {
6659 Reg = FuncInfo->ValueMap[PHIOp];
6660 if (Reg == 0) {
6661 assert(isa<AllocaInst>(PHIOp) &&
6662 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6663 "Didn't codegen value into a register!??");
6664 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006665 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006666 }
6667 }
6668
6669 // Remember that this register needs to added to the machine PHI node as
6670 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006671 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006672 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6673 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006674 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006675 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006676 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006677 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006678 Reg += NumRegisters;
6679 }
6680 }
6681 }
Dan Gohman2048b852009-11-23 18:04:58 +00006682 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006683}
6684
Dan Gohman3df24e62008-09-03 23:12:08 +00006685/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6686/// supports legal types, and it emits MachineInstrs directly instead of
6687/// creating SelectionDAG nodes.
6688///
6689bool
6690SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6691 FastISel *F) {
6692 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006693
Dan Gohman3df24e62008-09-03 23:12:08 +00006694 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006695 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006696
6697 // Check successor nodes' PHI nodes that expect a constant to be available
6698 // from this block.
6699 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6700 BasicBlock *SuccBB = TI->getSuccessor(succ);
6701 if (!isa<PHINode>(SuccBB->begin())) continue;
6702 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006703
Dan Gohman3df24e62008-09-03 23:12:08 +00006704 // If this terminator has multiple identical successors (common for
6705 // switches), only handle each succ once.
6706 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006707
Dan Gohman3df24e62008-09-03 23:12:08 +00006708 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6709 PHINode *PN;
6710
6711 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6712 // nodes and Machine PHI nodes, but the incoming operands have not been
6713 // emitted yet.
6714 for (BasicBlock::iterator I = SuccBB->begin();
6715 (PN = dyn_cast<PHINode>(I)); ++I) {
6716 // Ignore dead phi's.
6717 if (PN->use_empty()) continue;
6718
6719 // Only handle legal types. Two interesting things to note here. First,
6720 // by bailing out early, we may leave behind some dead instructions,
6721 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6722 // own moves. Second, this check is necessary becuase FastISel doesn't
6723 // use CreateRegForValue to create registers, so it always creates
6724 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006725 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006726 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6727 // Promote MVT::i1.
6728 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006729 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006730 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006731 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006732 return false;
6733 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006734 }
6735
6736 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6737
6738 unsigned Reg = F->getRegForValue(PHIOp);
6739 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006740 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006741 return false;
6742 }
Dan Gohman2048b852009-11-23 18:04:58 +00006743 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006744 }
6745 }
6746
6747 return true;
6748}