blob: db127f260ffd3701d310522514c76940d2474cfc [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"
Chris Lattner8047d9a2009-12-24 00:37:38 +000020#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000021#include "llvm/Constants.h"
22#include "llvm/CallingConv.h"
23#include "llvm/DerivedTypes.h"
24#include "llvm/Function.h"
25#include "llvm/GlobalVariable.h"
26#include "llvm/InlineAsm.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/IntrinsicInst.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,
Chris Lattnerf031e8a2010-01-01 03:32:16 +0000465 EVT::getIntegerVT(*DAG.getContext(),
466 ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000467 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000468
469 if (DisableScheduling)
470 DAG.AssignOrdering(Parts[0].getNode(), Order);
471
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000472 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
473 for (unsigned i = 0; i < NumParts; i += StepSize) {
474 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000475 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000476 SDValue &Part0 = Parts[i];
477 SDValue &Part1 = Parts[i+StepSize/2];
478
Scott Michelfdc40a02009-02-17 22:15:04 +0000479 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000480 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000481 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000482 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000483 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000484 DAG.getConstant(0, PtrVT));
485
Bill Wendling3ea3c242009-12-22 02:10:19 +0000486 if (DisableScheduling) {
487 DAG.AssignOrdering(Part0.getNode(), Order);
488 DAG.AssignOrdering(Part1.getNode(), Order);
489 }
490
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000491 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000492 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000493 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000494 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000495 PartVT, Part1);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000496 if (DisableScheduling) {
497 DAG.AssignOrdering(Part0.getNode(), Order);
498 DAG.AssignOrdering(Part1.getNode(), Order);
499 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000500 }
501 }
502 }
503
504 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000505 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000506
507 return;
508 }
509
510 // Vector ValueVT.
511 if (NumParts == 1) {
512 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000513 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000514 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000515 } else {
516 assert(ValueVT.getVectorElementType() == PartVT &&
517 ValueVT.getVectorNumElements() == 1 &&
518 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000519 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000520 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000521 DAG.getConstant(0, PtrVT));
522 }
523 }
524
Bill Wendling3ea3c242009-12-22 02:10:19 +0000525 if (DisableScheduling)
526 DAG.AssignOrdering(Val.getNode(), Order);
527
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000528 Parts[0] = Val;
529 return;
530 }
531
532 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000533 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000534 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000535 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
536 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000537 unsigned NumElements = ValueVT.getVectorNumElements();
538
539 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
540 NumParts = NumRegs; // Silence a compiler warning.
541 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
542
543 // Split the vector into intermediate operands.
544 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000545 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000546 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000547 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000548 IntermediateVT, Val,
549 DAG.getConstant(i * (NumElements / NumIntermediates),
550 PtrVT));
551 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000552 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000553 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000554 DAG.getConstant(i, PtrVT));
555
Bill Wendling3ea3c242009-12-22 02:10:19 +0000556 if (DisableScheduling)
557 DAG.AssignOrdering(Ops[i].getNode(), Order);
558 }
559
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000560 // Split the intermediate operands into legal parts.
561 if (NumParts == NumIntermediates) {
562 // If the register was not expanded, promote or copy the value,
563 // as appropriate.
564 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000565 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000566 } else if (NumParts > 0) {
567 // If the intermediate type was expanded, split each the value into
568 // legal parts.
569 assert(NumParts % NumIntermediates == 0 &&
570 "Must expand into a divisible number of parts!");
571 unsigned Factor = NumParts / NumIntermediates;
572 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000573 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000574 }
575}
576
577
Dan Gohman2048b852009-11-23 18:04:58 +0000578void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000579 AA = &aa;
580 GFI = gfi;
581 TD = DAG.getTarget().getTargetData();
582}
583
584/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000585/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000586/// for a new block. This doesn't clear out information about
587/// additional blocks that are needed to complete switch lowering
588/// or PHI node updating; that information is cleared out as it is
589/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000590void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000591 NodeMap.clear();
592 PendingLoads.clear();
593 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000594 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000595 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000596 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000597 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000598}
599
600/// getRoot - Return the current virtual root of the Selection DAG,
601/// flushing any PendingLoad items. This must be done before emitting
602/// a store or any other node that may need to be ordered after any
603/// prior load instructions.
604///
Dan Gohman2048b852009-11-23 18:04:58 +0000605SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000606 if (PendingLoads.empty())
607 return DAG.getRoot();
608
609 if (PendingLoads.size() == 1) {
610 SDValue Root = PendingLoads[0];
611 DAG.setRoot(Root);
612 PendingLoads.clear();
613 return Root;
614 }
615
616 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000617 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000618 &PendingLoads[0], PendingLoads.size());
619 PendingLoads.clear();
620 DAG.setRoot(Root);
621 return Root;
622}
623
624/// getControlRoot - Similar to getRoot, but instead of flushing all the
625/// PendingLoad items, flush all the PendingExports items. It is necessary
626/// to do this before emitting a terminator instruction.
627///
Dan Gohman2048b852009-11-23 18:04:58 +0000628SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000629 SDValue Root = DAG.getRoot();
630
631 if (PendingExports.empty())
632 return Root;
633
634 // Turn all of the CopyToReg chains into one factored node.
635 if (Root.getOpcode() != ISD::EntryToken) {
636 unsigned i = 0, e = PendingExports.size();
637 for (; i != e; ++i) {
638 assert(PendingExports[i].getNode()->getNumOperands() > 1);
639 if (PendingExports[i].getNode()->getOperand(0) == Root)
640 break; // Don't add the root if we already indirectly depend on it.
641 }
642
643 if (i == e)
644 PendingExports.push_back(Root);
645 }
646
Owen Anderson825b72b2009-08-11 20:47:22 +0000647 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000648 &PendingExports[0],
649 PendingExports.size());
650 PendingExports.clear();
651 DAG.setRoot(Root);
652 return Root;
653}
654
Dan Gohman2048b852009-11-23 18:04:58 +0000655void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000656 visit(I.getOpcode(), I);
657}
658
Dan Gohman2048b852009-11-23 18:04:58 +0000659void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000660 // We're processing a new instruction.
661 ++SDNodeOrder;
662
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000663 // Note: this doesn't use InstVisitor, because it has to work with
664 // ConstantExpr's in addition to instructions.
665 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000666 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000667 // Build the switch statement using the Instruction.def file.
668#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000669 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000670#include "llvm/Instruction.def"
671 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000672}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000673
Dan Gohman2048b852009-11-23 18:04:58 +0000674SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000675 SDValue &N = NodeMap[V];
676 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000677
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000678 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000679 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000681 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000682 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000683
684 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
685 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000686
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000687 if (isa<ConstantPointerNull>(C))
688 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000689
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000690 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000691 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000692
Nate Begeman9008ca62009-04-27 18:41:29 +0000693 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000694 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000695
696 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
697 visit(CE->getOpcode(), *CE);
698 SDValue N1 = NodeMap[V];
699 assert(N1.getNode() && "visit didn't populate the ValueMap!");
700 return N1;
701 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000702
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000703 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
704 SmallVector<SDValue, 4> Constants;
705 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
706 OI != OE; ++OI) {
707 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000708 // If the operand is an empty aggregate, there are no values.
709 if (!Val) continue;
710 // Add each leaf value from the operand to the Constants list
711 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000712 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
713 Constants.push_back(SDValue(Val, i));
714 }
Bill Wendling87710f02009-12-21 23:47:40 +0000715
716 SDValue Res = DAG.getMergeValues(&Constants[0], Constants.size(),
717 getCurDebugLoc());
718 if (DisableScheduling)
719 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
720 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000721 }
722
723 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
724 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
725 "Unknown struct or array constant!");
726
Owen Andersone50ed302009-08-10 22:56:29 +0000727 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000728 ComputeValueVTs(TLI, C->getType(), ValueVTs);
729 unsigned NumElts = ValueVTs.size();
730 if (NumElts == 0)
731 return SDValue(); // empty struct
732 SmallVector<SDValue, 4> Constants(NumElts);
733 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000734 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000735 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000736 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000737 else if (EltVT.isFloatingPoint())
738 Constants[i] = DAG.getConstantFP(0, EltVT);
739 else
740 Constants[i] = DAG.getConstant(0, EltVT);
741 }
Bill Wendling87710f02009-12-21 23:47:40 +0000742
743 SDValue Res = DAG.getMergeValues(&Constants[0], NumElts,
744 getCurDebugLoc());
745 if (DisableScheduling)
746 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
747 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000748 }
749
Dan Gohman8c2b5252009-10-30 01:27:03 +0000750 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000751 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000752
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000753 const VectorType *VecTy = cast<VectorType>(V->getType());
754 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000755
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000756 // Now that we know the number and type of the elements, get that number of
757 // elements into the Ops array based on what kind of constant it is.
758 SmallVector<SDValue, 16> Ops;
759 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
760 for (unsigned i = 0; i != NumElements; ++i)
761 Ops.push_back(getValue(CP->getOperand(i)));
762 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000763 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000764 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000765
766 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000767 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000768 Op = DAG.getConstantFP(0, EltVT);
769 else
770 Op = DAG.getConstant(0, EltVT);
771 Ops.assign(NumElements, Op);
772 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000773
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000774 // Create a BUILD_VECTOR node.
Bill Wendling87710f02009-12-21 23:47:40 +0000775 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
776 VT, &Ops[0], Ops.size());
777 if (DisableScheduling)
778 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
779
780 return NodeMap[V] = Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000781 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000782
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000783 // If this is a static alloca, generate it as the frameindex instead of
784 // computation.
785 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
786 DenseMap<const AllocaInst*, int>::iterator SI =
787 FuncInfo.StaticAllocaMap.find(AI);
788 if (SI != FuncInfo.StaticAllocaMap.end())
789 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
790 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000791
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000792 unsigned InReg = FuncInfo.ValueMap[V];
793 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000794
Owen Anderson23b9b192009-08-12 00:36:31 +0000795 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000796 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +0000797 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(),
798 SDNodeOrder, Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000799}
800
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000801/// Get the EVTs and ArgFlags collections that represent the return type
802/// of the given function. This does not require a DAG or a return value, and
803/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000804static void getReturnInfo(const Type* ReturnType,
805 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000806 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000807 TargetLowering &TLI,
808 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000809 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000810 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000811 unsigned NumValues = ValueVTs.size();
812 if ( NumValues == 0 ) return;
813
814 for (unsigned j = 0, f = NumValues; j != f; ++j) {
815 EVT VT = ValueVTs[j];
816 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000817
818 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000819 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000820 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000821 ExtendKind = ISD::ZERO_EXTEND;
822
823 // FIXME: C calling convention requires the return type to be promoted to
824 // at least 32-bit. But this is not necessary for non-C calling
825 // conventions. The frontend should mark functions whose return values
826 // require promoting with signext or zeroext attributes.
827 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000828 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000829 if (VT.bitsLT(MinVT))
830 VT = MinVT;
831 }
832
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000833 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
834 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000835 // 'inreg' on function refers to return value
836 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000837 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000838 Flags.setInReg();
839
840 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000841 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000842 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000843 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000844 Flags.setZExt();
845
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000846 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000847 OutVTs.push_back(PartVT);
848 OutFlags.push_back(Flags);
849 }
850 }
851}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000852
Dan Gohman2048b852009-11-23 18:04:58 +0000853void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000854 SDValue Chain = getControlRoot();
855 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000856 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
857
858 if (!FLI.CanLowerReturn) {
859 unsigned DemoteReg = FLI.DemoteRegister;
860 const Function *F = I.getParent()->getParent();
861
862 // Emit a store of the return value through the virtual register.
863 // Leave Outs empty so that LowerReturn won't try to load return
864 // registers the usual way.
865 SmallVector<EVT, 1> PtrValueVTs;
866 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
867 PtrValueVTs);
868
869 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
870 SDValue RetOp = getValue(I.getOperand(0));
871
Owen Andersone50ed302009-08-10 22:56:29 +0000872 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000873 SmallVector<uint64_t, 4> Offsets;
874 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000875 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000876
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000877 SmallVector<SDValue, 4> Chains(NumValues);
878 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000879 for (unsigned i = 0; i != NumValues; ++i) {
880 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
881 DAG.getConstant(Offsets[i], PtrVT));
882 Chains[i] =
883 DAG.getStore(Chain, getCurDebugLoc(),
884 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
885 Add, NULL, Offsets[i], false, 0);
886
887 if (DisableScheduling) {
888 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
889 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
890 }
891 }
892
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000893 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
894 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000895
896 if (DisableScheduling)
897 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
898 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000899 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
900 SmallVector<EVT, 4> ValueVTs;
901 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
902 unsigned NumValues = ValueVTs.size();
903 if (NumValues == 0) continue;
904
905 SDValue RetOp = getValue(I.getOperand(i));
906 for (unsigned j = 0, f = NumValues; j != f; ++j) {
907 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000908
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000909 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000910
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000911 const Function *F = I.getParent()->getParent();
912 if (F->paramHasAttr(0, Attribute::SExt))
913 ExtendKind = ISD::SIGN_EXTEND;
914 else if (F->paramHasAttr(0, Attribute::ZExt))
915 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000916
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000917 // FIXME: C calling convention requires the return type to be promoted to
918 // at least 32-bit. But this is not necessary for non-C calling
919 // conventions. The frontend should mark functions whose return values
920 // require promoting with signext or zeroext attributes.
921 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
922 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
923 if (VT.bitsLT(MinVT))
924 VT = MinVT;
925 }
926
927 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
928 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
929 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000930 getCopyToParts(DAG, getCurDebugLoc(), SDNodeOrder,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000931 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
932 &Parts[0], NumParts, PartVT, ExtendKind);
933
934 // 'inreg' on function refers to return value
935 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
936 if (F->paramHasAttr(0, Attribute::InReg))
937 Flags.setInReg();
938
939 // Propagate extension type if any
940 if (F->paramHasAttr(0, Attribute::SExt))
941 Flags.setSExt();
942 else if (F->paramHasAttr(0, Attribute::ZExt))
943 Flags.setZExt();
944
945 for (unsigned i = 0; i < NumParts; ++i)
946 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000947 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000948 }
949 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000950
951 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000952 CallingConv::ID CallConv =
953 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000954 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
955 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000956
957 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000958 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000959 "LowerReturn didn't return a valid chain!");
960
961 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000962 DAG.setRoot(Chain);
Bill Wendling87710f02009-12-21 23:47:40 +0000963
964 if (DisableScheduling)
965 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000966}
967
Dan Gohmanad62f532009-04-23 23:13:24 +0000968/// CopyToExportRegsIfNeeded - If the given value has virtual registers
969/// created for it, emit nodes to copy the value into the virtual
970/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000971void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000972 if (!V->use_empty()) {
973 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
974 if (VMI != FuncInfo.ValueMap.end())
975 CopyValueToVirtualRegister(V, VMI->second);
976 }
977}
978
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000979/// ExportFromCurrentBlock - If this condition isn't known to be exported from
980/// the current basic block, add it to ValueMap now so that we'll get a
981/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000982void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000983 // No need to export constants.
984 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000985
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000986 // Already exported?
987 if (FuncInfo.isExportedInst(V)) return;
988
989 unsigned Reg = FuncInfo.InitializeRegForValue(V);
990 CopyValueToVirtualRegister(V, Reg);
991}
992
Dan Gohman2048b852009-11-23 18:04:58 +0000993bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
994 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000995 // The operands of the setcc have to be in this block. We don't know
996 // how to export them from some other block.
997 if (Instruction *VI = dyn_cast<Instruction>(V)) {
998 // Can export from current BB.
999 if (VI->getParent() == FromBB)
1000 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001001
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001002 // Is already exported, noop.
1003 return FuncInfo.isExportedInst(V);
1004 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001005
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001006 // If this is an argument, we can export it if the BB is the entry block or
1007 // if it is already exported.
1008 if (isa<Argument>(V)) {
1009 if (FromBB == &FromBB->getParent()->getEntryBlock())
1010 return true;
1011
1012 // Otherwise, can only export this if it is already exported.
1013 return FuncInfo.isExportedInst(V);
1014 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001015
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001016 // Otherwise, constants can always be exported.
1017 return true;
1018}
1019
1020static bool InBlock(const Value *V, const BasicBlock *BB) {
1021 if (const Instruction *I = dyn_cast<Instruction>(V))
1022 return I->getParent() == BB;
1023 return true;
1024}
1025
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001026/// getFCmpCondCode - Return the ISD condition code corresponding to
1027/// the given LLVM IR floating-point condition code. This includes
1028/// consideration of global floating-point math flags.
1029///
1030static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1031 ISD::CondCode FPC, FOC;
1032 switch (Pred) {
1033 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1034 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1035 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1036 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1037 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1038 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1039 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1040 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1041 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1042 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1043 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1044 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1045 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1046 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1047 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1048 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1049 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001050 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001051 FOC = FPC = ISD::SETFALSE;
1052 break;
1053 }
1054 if (FiniteOnlyFPMath())
1055 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001056 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001057 return FPC;
1058}
1059
1060/// getICmpCondCode - Return the ISD condition code corresponding to
1061/// the given LLVM IR integer condition code.
1062///
1063static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1064 switch (Pred) {
1065 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1066 case ICmpInst::ICMP_NE: return ISD::SETNE;
1067 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1068 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1069 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1070 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1071 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1072 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1073 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1074 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1075 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001076 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001077 return ISD::SETNE;
1078 }
1079}
1080
Dan Gohmanc2277342008-10-17 21:16:08 +00001081/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1082/// This function emits a branch and is used at the leaves of an OR or an
1083/// AND operator tree.
1084///
1085void
Dan Gohman2048b852009-11-23 18:04:58 +00001086SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1087 MachineBasicBlock *TBB,
1088 MachineBasicBlock *FBB,
1089 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001090 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001091
Dan Gohmanc2277342008-10-17 21:16:08 +00001092 // If the leaf of the tree is a comparison, merge the condition into
1093 // the caseblock.
1094 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1095 // The operands of the cmp have to be in this block. We don't know
1096 // how to export them from some other block. If this is the first block
1097 // of the sequence, no exporting is needed.
1098 if (CurBB == CurMBB ||
1099 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1100 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001101 ISD::CondCode Condition;
1102 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001103 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001104 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001105 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001106 } else {
1107 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001108 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001109 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001110
1111 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001112 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1113 SwitchCases.push_back(CB);
1114 return;
1115 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001116 }
1117
1118 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001119 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001120 NULL, TBB, FBB, CurBB);
1121 SwitchCases.push_back(CB);
1122}
1123
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001124/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001125void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1126 MachineBasicBlock *TBB,
1127 MachineBasicBlock *FBB,
1128 MachineBasicBlock *CurBB,
1129 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001130 // If this node is not part of the or/and tree, emit it as a branch.
1131 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001132 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001133 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1134 BOp->getParent() != CurBB->getBasicBlock() ||
1135 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1136 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1137 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001138 return;
1139 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001140
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001141 // Create TmpBB after CurBB.
1142 MachineFunction::iterator BBI = CurBB;
1143 MachineFunction &MF = DAG.getMachineFunction();
1144 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1145 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001146
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001147 if (Opc == Instruction::Or) {
1148 // Codegen X | Y as:
1149 // jmp_if_X TBB
1150 // jmp TmpBB
1151 // TmpBB:
1152 // jmp_if_Y TBB
1153 // jmp FBB
1154 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001156 // Emit the LHS condition.
1157 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001158
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001159 // Emit the RHS condition into TmpBB.
1160 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1161 } else {
1162 assert(Opc == Instruction::And && "Unknown merge op!");
1163 // Codegen X & Y as:
1164 // jmp_if_X TmpBB
1165 // jmp FBB
1166 // TmpBB:
1167 // jmp_if_Y TBB
1168 // jmp FBB
1169 //
1170 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001171
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001172 // Emit the LHS condition.
1173 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001174
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001175 // Emit the RHS condition into TmpBB.
1176 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1177 }
1178}
1179
1180/// If the set of cases should be emitted as a series of branches, return true.
1181/// If we should emit this as a bunch of and/or'd together conditions, return
1182/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001183bool
Dan Gohman2048b852009-11-23 18:04:58 +00001184SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001185 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001186
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001187 // If this is two comparisons of the same values or'd or and'd together, they
1188 // will get folded into a single comparison, so don't emit two blocks.
1189 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1190 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1191 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1192 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1193 return false;
1194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001196 return true;
1197}
1198
Dan Gohman2048b852009-11-23 18:04:58 +00001199void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001200 // Update machine-CFG edges.
1201 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1202
1203 // Figure out which block is immediately after the current one.
1204 MachineBasicBlock *NextBlock = 0;
1205 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001206 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 NextBlock = BBI;
1208
1209 if (I.isUnconditional()) {
1210 // Update machine-CFG edges.
1211 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001212
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001213 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001214 if (Succ0MBB != NextBlock) {
1215 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001216 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001217 DAG.getBasicBlock(Succ0MBB));
1218 DAG.setRoot(V);
1219
1220 if (DisableScheduling)
1221 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1222 }
1223
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001224 return;
1225 }
1226
1227 // If this condition is one of the special cases we handle, do special stuff
1228 // now.
1229 Value *CondVal = I.getCondition();
1230 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1231
1232 // If this is a series of conditions that are or'd or and'd together, emit
1233 // this as a sequence of branches instead of setcc's with and/or operations.
1234 // For example, instead of something like:
1235 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001236 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001237 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001238 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239 // or C, F
1240 // jnz foo
1241 // Emit:
1242 // cmp A, B
1243 // je foo
1244 // cmp D, E
1245 // jle foo
1246 //
1247 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001248 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001249 (BOp->getOpcode() == Instruction::And ||
1250 BOp->getOpcode() == Instruction::Or)) {
1251 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1252 // If the compares in later blocks need to use values not currently
1253 // exported from this block, export them now. This block should always
1254 // be the first entry.
1255 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001256
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001257 // Allow some cases to be rejected.
1258 if (ShouldEmitAsBranches(SwitchCases)) {
1259 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1260 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1261 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1262 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001263
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001264 // Emit the branch for this block.
1265 visitSwitchCase(SwitchCases[0]);
1266 SwitchCases.erase(SwitchCases.begin());
1267 return;
1268 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001270 // Okay, we decided not to do this, remove any inserted MBB's and clear
1271 // SwitchCases.
1272 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001273 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001274
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275 SwitchCases.clear();
1276 }
1277 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001278
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001279 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001280 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001281 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001282
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001283 // Use visitSwitchCase to actually insert the fast branch sequence for this
1284 // cond branch.
1285 visitSwitchCase(CB);
1286}
1287
1288/// visitSwitchCase - Emits the necessary code to represent a single node in
1289/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001290void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001291 SDValue Cond;
1292 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001293 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001294
1295 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001296 if (CB.CmpMHS == NULL) {
1297 // Fold "(X == true)" to X and "(X == false)" to !X to
1298 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001299 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001300 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001301 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001302 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001303 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001304 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001305 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001306 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001307 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001308 } else {
1309 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1310
Anton Korobeynikov23218582008-12-23 22:25:27 +00001311 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1312 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001313
1314 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001315 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001316
1317 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001318 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001319 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001321 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001322 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001324 DAG.getConstant(High-Low, VT), ISD::SETULE);
1325 }
1326 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001327
Bill Wendling87710f02009-12-21 23:47:40 +00001328 if (DisableScheduling)
1329 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1330
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331 // Update successor info
1332 CurMBB->addSuccessor(CB.TrueBB);
1333 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001335 // Set NextBlock to be the MBB immediately after the current one, if any.
1336 // This is used to avoid emitting unnecessary branches to the next block.
1337 MachineBasicBlock *NextBlock = 0;
1338 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001339 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001340 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001341
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001342 // If the lhs block is the next block, invert the condition so that we can
1343 // fall through to the lhs instead of the rhs block.
1344 if (CB.TrueBB == NextBlock) {
1345 std::swap(CB.TrueBB, CB.FalseBB);
1346 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001347 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001348
1349 if (DisableScheduling)
1350 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001351 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001352
Dale Johannesenf5d97892009-02-04 01:48:28 +00001353 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001354 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001355 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001356
Bill Wendling87710f02009-12-21 23:47:40 +00001357 if (DisableScheduling)
1358 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1359
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001360 // If the branch was constant folded, fix up the CFG.
1361 if (BrCond.getOpcode() == ISD::BR) {
1362 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001363 } else {
1364 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001365 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001366 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001367
Bill Wendling87710f02009-12-21 23:47:40 +00001368 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001369 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1370 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001371
1372 if (DisableScheduling)
1373 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1374 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001375 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001376
1377 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001378}
1379
1380/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001381void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001382 // Emit the code for the jump table
1383 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001384 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001385 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1386 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001387 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001388 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1389 MVT::Other, Index.getValue(1),
1390 Table, Index);
1391 DAG.setRoot(BrJumpTable);
1392
Bill Wendling87710f02009-12-21 23:47:40 +00001393 if (DisableScheduling) {
1394 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1395 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001396 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001397 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001398}
1399
1400/// visitJumpTableHeader - This function emits necessary code to produce index
1401/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001402void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1403 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001404 // Subtract the lowest switch case value from the value being switched on and
1405 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001406 // difference between smallest and largest cases.
1407 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001408 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001409 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001410 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001411
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001412 // The SDNode we just created, which holds the value being switched on minus
1413 // the the smallest case value, needs to be copied to a virtual register so it
1414 // can be used as an index into the jump table in a subsequent basic block.
1415 // This value may be smaller or larger than the target's pointer type, and
1416 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001417 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001418
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001419 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001420 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1421 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001422 JT.Reg = JumpTableReg;
1423
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001424 // Emit the range check for the jump table, and branch to the default block
1425 // for the switch statement if the value being switched on exceeds the largest
1426 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001427 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001428 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001429 DAG.getConstant(JTH.Last-JTH.First,VT),
1430 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001431
Bill Wendling87710f02009-12-21 23:47:40 +00001432 if (DisableScheduling) {
1433 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1434 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1435 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1436 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1437 }
1438
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001439 // Set NextBlock to be the MBB immediately after the current one, if any.
1440 // This is used to avoid emitting unnecessary branches to the next block.
1441 MachineBasicBlock *NextBlock = 0;
1442 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001443
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001444 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445 NextBlock = BBI;
1446
Dale Johannesen66978ee2009-01-31 02:22:37 +00001447 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001448 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001449 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001450
Bill Wendling87710f02009-12-21 23:47:40 +00001451 if (DisableScheduling)
1452 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1453
1454 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001455 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1456 DAG.getBasicBlock(JT.MBB));
1457
Bill Wendling87710f02009-12-21 23:47:40 +00001458 if (DisableScheduling)
1459 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1460 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001461
Bill Wendling87710f02009-12-21 23:47:40 +00001462 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001463}
1464
1465/// visitBitTestHeader - This function emits necessary code to produce value
1466/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001467void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001468 // Subtract the minimum value
1469 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001470 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001471 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001472 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473
1474 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001475 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001476 TLI.getSetCCResultType(Sub.getValueType()),
1477 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001478 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001479
Bill Wendling87710f02009-12-21 23:47:40 +00001480 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1481 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001482
Duncan Sands92abc622009-01-31 15:50:11 +00001483 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001484 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1485 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486
Bill Wendling87710f02009-12-21 23:47:40 +00001487 if (DisableScheduling) {
1488 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1489 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1490 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1491 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1492 }
1493
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001494 // Set NextBlock to be the MBB immediately after the current one, if any.
1495 // This is used to avoid emitting unnecessary branches to the next block.
1496 MachineBasicBlock *NextBlock = 0;
1497 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001498 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001499 NextBlock = BBI;
1500
1501 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1502
1503 CurMBB->addSuccessor(B.Default);
1504 CurMBB->addSuccessor(MBB);
1505
Dale Johannesen66978ee2009-01-31 02:22:37 +00001506 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001507 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001508 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001509
Bill Wendling87710f02009-12-21 23:47:40 +00001510 if (DisableScheduling)
1511 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1512
1513 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001514 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1515 DAG.getBasicBlock(MBB));
1516
Bill Wendling87710f02009-12-21 23:47:40 +00001517 if (DisableScheduling)
1518 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1519 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001520
Bill Wendling87710f02009-12-21 23:47:40 +00001521 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001522}
1523
1524/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001525void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1526 unsigned Reg,
1527 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001528 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001529 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001530 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001531 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001532 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001533 DAG.getConstant(1, TLI.getPointerTy()),
1534 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001535
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001536 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001537 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001538 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001539 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001540 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1541 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001542 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001543 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001544
Bill Wendling87710f02009-12-21 23:47:40 +00001545 if (DisableScheduling) {
1546 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1547 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1548 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1549 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1550 }
1551
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001552 CurMBB->addSuccessor(B.TargetBB);
1553 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001554
Dale Johannesen66978ee2009-01-31 02:22:37 +00001555 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001556 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001557 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001558
Bill Wendling87710f02009-12-21 23:47:40 +00001559 if (DisableScheduling)
1560 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1561
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001562 // Set NextBlock to be the MBB immediately after the current one, if any.
1563 // This is used to avoid emitting unnecessary branches to the next block.
1564 MachineBasicBlock *NextBlock = 0;
1565 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001566 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567 NextBlock = BBI;
1568
Bill Wendling87710f02009-12-21 23:47:40 +00001569 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001570 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1571 DAG.getBasicBlock(NextMBB));
1572
Bill Wendling87710f02009-12-21 23:47:40 +00001573 if (DisableScheduling)
1574 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1575 }
Bill Wendling0777e922009-12-21 21:59:52 +00001576
Bill Wendling87710f02009-12-21 23:47:40 +00001577 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001578}
1579
Dan Gohman2048b852009-11-23 18:04:58 +00001580void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001581 // Retrieve successors.
1582 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1583 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1584
Gabor Greifb67e6b32009-01-15 11:10:44 +00001585 const Value *Callee(I.getCalledValue());
1586 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001587 visitInlineAsm(&I);
1588 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001589 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001590
1591 // If the value of the invoke is used outside of its defining block, make it
1592 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001593 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001594
1595 // Update successor info
1596 CurMBB->addSuccessor(Return);
1597 CurMBB->addSuccessor(LandingPad);
1598
1599 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001600 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1601 MVT::Other, getControlRoot(),
1602 DAG.getBasicBlock(Return));
1603 DAG.setRoot(Branch);
1604
1605 if (DisableScheduling)
1606 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001607}
1608
Dan Gohman2048b852009-11-23 18:04:58 +00001609void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610}
1611
1612/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1613/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001614bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1615 CaseRecVector& WorkList,
1616 Value* SV,
1617 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001618 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001619
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001621 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001623 return false;
1624
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001625 // Get the MachineFunction which holds the current MBB. This is used when
1626 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001627 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001628
1629 // Figure out which block is immediately after the current one.
1630 MachineBasicBlock *NextBlock = 0;
1631 MachineFunction::iterator BBI = CR.CaseBB;
1632
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001633 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001634 NextBlock = BBI;
1635
1636 // TODO: If any two of the cases has the same destination, and if one value
1637 // is the same as the other, but has one bit unset that the other has set,
1638 // use bit manipulation to do two compares at once. For example:
1639 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001640
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001641 // Rearrange the case blocks so that the last one falls through if possible.
1642 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1643 // The last case block won't fall through into 'NextBlock' if we emit the
1644 // branches in this order. See if rearranging a case value would help.
1645 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1646 if (I->BB == NextBlock) {
1647 std::swap(*I, BackCase);
1648 break;
1649 }
1650 }
1651 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001652
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653 // Create a CaseBlock record representing a conditional branch to
1654 // the Case's target mbb if the value being switched on SV is equal
1655 // to C.
1656 MachineBasicBlock *CurBlock = CR.CaseBB;
1657 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1658 MachineBasicBlock *FallThrough;
1659 if (I != E-1) {
1660 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1661 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001662
1663 // Put SV in a virtual register to make it available from the new blocks.
1664 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001665 } else {
1666 // If the last case doesn't match, go to the default block.
1667 FallThrough = Default;
1668 }
1669
1670 Value *RHS, *LHS, *MHS;
1671 ISD::CondCode CC;
1672 if (I->High == I->Low) {
1673 // This is just small small case range :) containing exactly 1 case
1674 CC = ISD::SETEQ;
1675 LHS = SV; RHS = I->High; MHS = NULL;
1676 } else {
1677 CC = ISD::SETLE;
1678 LHS = I->Low; MHS = SV; RHS = I->High;
1679 }
1680 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001681
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001682 // If emitting the first comparison, just call visitSwitchCase to emit the
1683 // code into the current block. Otherwise, push the CaseBlock onto the
1684 // vector to be later processed by SDISel, and insert the node's MBB
1685 // before the next MBB.
1686 if (CurBlock == CurMBB)
1687 visitSwitchCase(CB);
1688 else
1689 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001691 CurBlock = FallThrough;
1692 }
1693
1694 return true;
1695}
1696
1697static inline bool areJTsAllowed(const TargetLowering &TLI) {
1698 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001699 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1700 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001701}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001702
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001703static APInt ComputeRange(const APInt &First, const APInt &Last) {
1704 APInt LastExt(Last), FirstExt(First);
1705 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1706 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1707 return (LastExt - FirstExt + 1ULL);
1708}
1709
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001710/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001711bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1712 CaseRecVector& WorkList,
1713 Value* SV,
1714 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001715 Case& FrontCase = *CR.Range.first;
1716 Case& BackCase = *(CR.Range.second-1);
1717
Chris Lattnere880efe2009-11-07 07:50:34 +00001718 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1719 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001720
Chris Lattnere880efe2009-11-07 07:50:34 +00001721 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001722 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1723 I!=E; ++I)
1724 TSize += I->size();
1725
Chris Lattnere880efe2009-11-07 07:50:34 +00001726 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001727 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001728
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001729 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001730 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001731 if (Density < 0.4)
1732 return false;
1733
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001734 DEBUG(errs() << "Lowering jump table\n"
1735 << "First entry: " << First << ". Last entry: " << Last << '\n'
1736 << "Range: " << Range
1737 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001738
1739 // Get the MachineFunction which holds the current MBB. This is used when
1740 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001741 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001742
1743 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001745 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001746
1747 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1748
1749 // Create a new basic block to hold the code for loading the address
1750 // of the jump table, and jumping to it. Update successor information;
1751 // we will either branch to the default case for the switch, or the jump
1752 // table.
1753 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1754 CurMF->insert(BBI, JumpTableBB);
1755 CR.CaseBB->addSuccessor(Default);
1756 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001757
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001758 // Build a vector of destination BBs, corresponding to each target
1759 // of the jump table. If the value of the jump table slot corresponds to
1760 // a case statement, push the case's BB onto the vector, otherwise, push
1761 // the default BB.
1762 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001763 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001764 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001765 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1766 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1767
1768 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001769 DestBBs.push_back(I->BB);
1770 if (TEI==High)
1771 ++I;
1772 } else {
1773 DestBBs.push_back(Default);
1774 }
1775 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001776
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001777 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001778 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1779 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001780 E = DestBBs.end(); I != E; ++I) {
1781 if (!SuccsHandled[(*I)->getNumber()]) {
1782 SuccsHandled[(*I)->getNumber()] = true;
1783 JumpTableBB->addSuccessor(*I);
1784 }
1785 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001786
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787 // Create a jump table index for this jump table, or return an existing
1788 // one.
1789 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001790
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791 // Set the jump table information so that we can codegen it as a second
1792 // MachineBasicBlock
1793 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1794 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1795 if (CR.CaseBB == CurMBB)
1796 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001797
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001798 JTCases.push_back(JumpTableBlock(JTH, JT));
1799
1800 return true;
1801}
1802
1803/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1804/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001805bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1806 CaseRecVector& WorkList,
1807 Value* SV,
1808 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001809 // Get the MachineFunction which holds the current MBB. This is used when
1810 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001811 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001812
1813 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001814 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001815 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816
1817 Case& FrontCase = *CR.Range.first;
1818 Case& BackCase = *(CR.Range.second-1);
1819 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1820
1821 // Size is the number of Cases represented by this range.
1822 unsigned Size = CR.Range.second - CR.Range.first;
1823
Chris Lattnere880efe2009-11-07 07:50:34 +00001824 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1825 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 double FMetric = 0;
1827 CaseItr Pivot = CR.Range.first + Size/2;
1828
1829 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1830 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001831 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001832 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1833 I!=E; ++I)
1834 TSize += I->size();
1835
Chris Lattnere880efe2009-11-07 07:50:34 +00001836 APInt LSize = FrontCase.size();
1837 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001838 DEBUG(errs() << "Selecting best pivot: \n"
1839 << "First: " << First << ", Last: " << Last <<'\n'
1840 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001841 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1842 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001843 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1844 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001845 APInt Range = ComputeRange(LEnd, RBegin);
1846 assert((Range - 2ULL).isNonNegative() &&
1847 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001848 double LDensity = (double)LSize.roundToDouble() /
1849 (LEnd - First + 1ULL).roundToDouble();
1850 double RDensity = (double)RSize.roundToDouble() /
1851 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001852 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001853 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001854 DEBUG(errs() <<"=>Step\n"
1855 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1856 << "LDensity: " << LDensity
1857 << ", RDensity: " << RDensity << '\n'
1858 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001859 if (FMetric < Metric) {
1860 Pivot = J;
1861 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001862 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 }
1864
1865 LSize += J->size();
1866 RSize -= J->size();
1867 }
1868 if (areJTsAllowed(TLI)) {
1869 // If our case is dense we *really* should handle it earlier!
1870 assert((FMetric > 0) && "Should handle dense range earlier!");
1871 } else {
1872 Pivot = CR.Range.first + Size/2;
1873 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001874
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001875 CaseRange LHSR(CR.Range.first, Pivot);
1876 CaseRange RHSR(Pivot, CR.Range.second);
1877 Constant *C = Pivot->Low;
1878 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001879
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001880 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001881 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001883 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001884 // Pivot's Value, then we can branch directly to the LHS's Target,
1885 // rather than creating a leaf node for it.
1886 if ((LHSR.second - LHSR.first) == 1 &&
1887 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001888 cast<ConstantInt>(C)->getValue() ==
1889 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001890 TrueBB = LHSR.first->BB;
1891 } else {
1892 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1893 CurMF->insert(BBI, TrueBB);
1894 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001895
1896 // Put SV in a virtual register to make it available from the new blocks.
1897 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001898 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001899
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001900 // Similar to the optimization above, if the Value being switched on is
1901 // known to be less than the Constant CR.LT, and the current Case Value
1902 // is CR.LT - 1, then we can branch directly to the target block for
1903 // the current Case Value, rather than emitting a RHS leaf node for it.
1904 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001905 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1906 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001907 FalseBB = RHSR.first->BB;
1908 } else {
1909 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1910 CurMF->insert(BBI, FalseBB);
1911 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001912
1913 // Put SV in a virtual register to make it available from the new blocks.
1914 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001915 }
1916
1917 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001918 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919 // Otherwise, branch to LHS.
1920 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1921
1922 if (CR.CaseBB == CurMBB)
1923 visitSwitchCase(CB);
1924 else
1925 SwitchCases.push_back(CB);
1926
1927 return true;
1928}
1929
1930/// handleBitTestsSwitchCase - if current case range has few destination and
1931/// range span less, than machine word bitwidth, encode case range into series
1932/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001933bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1934 CaseRecVector& WorkList,
1935 Value* SV,
1936 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001937 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001938 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001939
1940 Case& FrontCase = *CR.Range.first;
1941 Case& BackCase = *(CR.Range.second-1);
1942
1943 // Get the MachineFunction which holds the current MBB. This is used when
1944 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001945 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001946
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001947 // If target does not have legal shift left, do not emit bit tests at all.
1948 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1949 return false;
1950
Anton Korobeynikov23218582008-12-23 22:25:27 +00001951 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001952 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1953 I!=E; ++I) {
1954 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001955 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001956 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001957
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001958 // Count unique destinations
1959 SmallSet<MachineBasicBlock*, 4> Dests;
1960 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1961 Dests.insert(I->BB);
1962 if (Dests.size() > 3)
1963 // Don't bother the code below, if there are too much unique destinations
1964 return false;
1965 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001966 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1967 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001968
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001969 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001970 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1971 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001972 APInt cmpRange = maxValue - minValue;
1973
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001974 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1975 << "Low bound: " << minValue << '\n'
1976 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001977
1978 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001979 (!(Dests.size() == 1 && numCmps >= 3) &&
1980 !(Dests.size() == 2 && numCmps >= 5) &&
1981 !(Dests.size() >= 3 && numCmps >= 6)))
1982 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001983
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001984 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001985 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1986
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001987 // Optimize the case where all the case values fit in a
1988 // word without having to subtract minValue. In this case,
1989 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001990 if (minValue.isNonNegative() &&
1991 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1992 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001993 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001994 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001995 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001996
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001997 CaseBitsVector CasesBits;
1998 unsigned i, count = 0;
1999
2000 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2001 MachineBasicBlock* Dest = I->BB;
2002 for (i = 0; i < count; ++i)
2003 if (Dest == CasesBits[i].BB)
2004 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002005
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002006 if (i == count) {
2007 assert((count < 3) && "Too much destinations to test!");
2008 CasesBits.push_back(CaseBits(0, Dest, 0));
2009 count++;
2010 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002011
2012 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2013 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2014
2015 uint64_t lo = (lowValue - lowBound).getZExtValue();
2016 uint64_t hi = (highValue - lowBound).getZExtValue();
2017
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002018 for (uint64_t j = lo; j <= hi; j++) {
2019 CasesBits[i].Mask |= 1ULL << j;
2020 CasesBits[i].Bits++;
2021 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002022
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002023 }
2024 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002025
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026 BitTestInfo BTC;
2027
2028 // Figure out which block is immediately after the current one.
2029 MachineFunction::iterator BBI = CR.CaseBB;
2030 ++BBI;
2031
2032 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2033
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002034 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002035 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002036 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
2037 << ", Bits: " << CasesBits[i].Bits
2038 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002039
2040 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2041 CurMF->insert(BBI, CaseBB);
2042 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2043 CaseBB,
2044 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002045
2046 // Put SV in a virtual register to make it available from the new blocks.
2047 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002048 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002049
2050 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002051 -1U, (CR.CaseBB == CurMBB),
2052 CR.CaseBB, Default, BTC);
2053
2054 if (CR.CaseBB == CurMBB)
2055 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002057 BitTestCases.push_back(BTB);
2058
2059 return true;
2060}
2061
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002062/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002063size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2064 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002065 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002066
2067 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002068 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002069 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2070 Cases.push_back(Case(SI.getSuccessorValue(i),
2071 SI.getSuccessorValue(i),
2072 SMBB));
2073 }
2074 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2075
2076 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002077 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002078 // Must recompute end() each iteration because it may be
2079 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002080 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2081 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2082 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002083 MachineBasicBlock* nextBB = J->BB;
2084 MachineBasicBlock* currentBB = I->BB;
2085
2086 // If the two neighboring cases go to the same destination, merge them
2087 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002088 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002089 I->High = J->High;
2090 J = Cases.erase(J);
2091 } else {
2092 I = J++;
2093 }
2094 }
2095
2096 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2097 if (I->Low != I->High)
2098 // A range counts double, since it requires two compares.
2099 ++numCmps;
2100 }
2101
2102 return numCmps;
2103}
2104
Dan Gohman2048b852009-11-23 18:04:58 +00002105void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002106 // Figure out which block is immediately after the current one.
2107 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002108 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2109
2110 // If there is only the default destination, branch to it if it is not the
2111 // next basic block. Otherwise, just fall through.
2112 if (SI.getNumOperands() == 2) {
2113 // Update machine-CFG edges.
2114
2115 // If this is not a fall-through branch, emit the branch.
2116 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002117 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002118 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002119 MVT::Other, getControlRoot(),
2120 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002121 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002122
2123 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002124 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002125 }
2126
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002127 return;
2128 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002130 // If there are any non-default case statements, create a vector of Cases
2131 // representing each one, and sort the vector so that we can efficiently
2132 // create a binary search tree from them.
2133 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002134 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002135 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2136 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002137 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002138
2139 // Get the Value to be switched on and default basic blocks, which will be
2140 // inserted into CaseBlock records, representing basic blocks in the binary
2141 // search tree.
2142 Value *SV = SI.getOperand(0);
2143
2144 // Push the initial CaseRec onto the worklist
2145 CaseRecVector WorkList;
2146 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2147
2148 while (!WorkList.empty()) {
2149 // Grab a record representing a case range to process off the worklist
2150 CaseRec CR = WorkList.back();
2151 WorkList.pop_back();
2152
2153 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2154 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002156 // If the range has few cases (two or less) emit a series of specific
2157 // tests.
2158 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2159 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002160
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002161 // If the switch has more than 5 blocks, and at least 40% dense, and the
2162 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002163 // lowering the switch to a binary tree of conditional branches.
2164 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2165 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002167 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2168 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2169 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2170 }
2171}
2172
Dan Gohman2048b852009-11-23 18:04:58 +00002173void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002174 // Update machine-CFG edges.
2175 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2176 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2177
Bill Wendling49fcff82009-12-21 22:30:11 +00002178 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2179 MVT::Other, getControlRoot(),
2180 getValue(I.getAddress()));
2181 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002182
Bill Wendling49fcff82009-12-21 22:30:11 +00002183 if (DisableScheduling)
2184 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2185}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002186
Dan Gohman2048b852009-11-23 18:04:58 +00002187void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002188 // -0.0 - X --> fneg
2189 const Type *Ty = I.getType();
2190 if (isa<VectorType>(Ty)) {
2191 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2192 const VectorType *DestTy = cast<VectorType>(I.getType());
2193 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002194 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002195 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002196 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002197 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002198 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002199 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2200 Op2.getValueType(), Op2);
2201 setValue(&I, Res);
2202
2203 if (DisableScheduling)
2204 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2205
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002206 return;
2207 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002208 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002209 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002210
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002211 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002212 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002213 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002214 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2215 Op2.getValueType(), Op2);
2216 setValue(&I, Res);
2217
2218 if (DisableScheduling)
2219 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2220
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002221 return;
2222 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002223
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002224 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002225}
2226
Dan Gohman2048b852009-11-23 18:04:58 +00002227void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228 SDValue Op1 = getValue(I.getOperand(0));
2229 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002230 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2231 Op1.getValueType(), Op1, Op2);
2232 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002233
Bill Wendling49fcff82009-12-21 22:30:11 +00002234 if (DisableScheduling)
2235 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002236}
2237
Dan Gohman2048b852009-11-23 18:04:58 +00002238void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239 SDValue Op1 = getValue(I.getOperand(0));
2240 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002241 if (!isa<VectorType>(I.getType()) &&
2242 Op2.getValueType() != TLI.getShiftAmountTy()) {
2243 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002244 EVT PTy = TLI.getPointerTy();
2245 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002246 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002247 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2248 TLI.getShiftAmountTy(), Op2);
2249 // If the operand is larger than the shift count type but the shift
2250 // count type has enough bits to represent any shift value, truncate
2251 // it now. This is a common case and it exposes the truncate to
2252 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002253 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002254 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2255 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2256 TLI.getShiftAmountTy(), Op2);
2257 // Otherwise we'll need to temporarily settle for some other
2258 // convenient type; type legalization will make adjustments as
2259 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002260 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002261 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002262 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002263 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002264 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002265 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002266 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002267
Bill Wendling49fcff82009-12-21 22:30:11 +00002268 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2269 Op1.getValueType(), Op1, Op2);
2270 setValue(&I, Res);
2271
Bill Wendling87710f02009-12-21 23:47:40 +00002272 if (DisableScheduling) {
2273 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2274 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002275 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002276 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002277}
2278
Dan Gohman2048b852009-11-23 18:04:58 +00002279void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002280 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2281 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2282 predicate = IC->getPredicate();
2283 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2284 predicate = ICmpInst::Predicate(IC->getPredicate());
2285 SDValue Op1 = getValue(I.getOperand(0));
2286 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002287 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002288
Owen Andersone50ed302009-08-10 22:56:29 +00002289 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002290 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2291 setValue(&I, Res);
2292
2293 if (DisableScheduling)
2294 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295}
2296
Dan Gohman2048b852009-11-23 18:04:58 +00002297void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002298 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2299 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2300 predicate = FC->getPredicate();
2301 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2302 predicate = FCmpInst::Predicate(FC->getPredicate());
2303 SDValue Op1 = getValue(I.getOperand(0));
2304 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002305 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002306 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002307 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2308 setValue(&I, Res);
2309
2310 if (DisableScheduling)
2311 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002312}
2313
Dan Gohman2048b852009-11-23 18:04:58 +00002314void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002315 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002316 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2317 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002318 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002319
Bill Wendling49fcff82009-12-21 22:30:11 +00002320 SmallVector<SDValue, 4> Values(NumValues);
2321 SDValue Cond = getValue(I.getOperand(0));
2322 SDValue TrueVal = getValue(I.getOperand(1));
2323 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002324
Bill Wendling49fcff82009-12-21 22:30:11 +00002325 for (unsigned i = 0; i != NumValues; ++i) {
2326 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2327 TrueVal.getNode()->getValueType(i), Cond,
2328 SDValue(TrueVal.getNode(),
2329 TrueVal.getResNo() + i),
2330 SDValue(FalseVal.getNode(),
2331 FalseVal.getResNo() + i));
2332
2333 if (DisableScheduling)
2334 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002335 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002336
Bill Wendling49fcff82009-12-21 22:30:11 +00002337 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2338 DAG.getVTList(&ValueVTs[0], NumValues),
2339 &Values[0], NumValues);
2340 setValue(&I, Res);
2341
2342 if (DisableScheduling)
2343 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2344}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002345
Dan Gohman2048b852009-11-23 18:04:58 +00002346void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002347 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2348 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002349 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002350 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2351 setValue(&I, Res);
2352
2353 if (DisableScheduling)
2354 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002355}
2356
Dan Gohman2048b852009-11-23 18:04:58 +00002357void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002358 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2359 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2360 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002361 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002362 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2363 setValue(&I, Res);
2364
2365 if (DisableScheduling)
2366 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002367}
2368
Dan Gohman2048b852009-11-23 18:04:58 +00002369void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002370 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2371 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2372 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002373 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002374 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2375 setValue(&I, Res);
2376
2377 if (DisableScheduling)
2378 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002379}
2380
Dan Gohman2048b852009-11-23 18:04:58 +00002381void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002382 // FPTrunc is never a no-op cast, no need to check
2383 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002384 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002385 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2386 DestVT, N, DAG.getIntPtrConstant(0));
2387 setValue(&I, Res);
2388
2389 if (DisableScheduling)
2390 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002391}
2392
Dan Gohman2048b852009-11-23 18:04:58 +00002393void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002394 // FPTrunc is never a no-op cast, no need to check
2395 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002396 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002397 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2398 setValue(&I, Res);
2399
2400 if (DisableScheduling)
2401 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002402}
2403
Dan Gohman2048b852009-11-23 18:04:58 +00002404void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002405 // FPToUI is never a no-op cast, no need to check
2406 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002407 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002408 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2409 setValue(&I, Res);
2410
2411 if (DisableScheduling)
2412 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002413}
2414
Dan Gohman2048b852009-11-23 18:04:58 +00002415void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002416 // FPToSI is never a no-op cast, no need to check
2417 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002418 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002419 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2420 setValue(&I, Res);
2421
2422 if (DisableScheduling)
2423 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002424}
2425
Dan Gohman2048b852009-11-23 18:04:58 +00002426void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002427 // UIToFP is never a no-op cast, no need to check
2428 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002429 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002430 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2431 setValue(&I, Res);
2432
2433 if (DisableScheduling)
2434 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002435}
2436
Dan Gohman2048b852009-11-23 18:04:58 +00002437void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002438 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002439 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002440 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002441 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2442 setValue(&I, Res);
2443
2444 if (DisableScheduling)
2445 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002446}
2447
Dan Gohman2048b852009-11-23 18:04:58 +00002448void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002449 // What to do depends on the size of the integer and the size of the pointer.
2450 // We can either truncate, zero extend, or no-op, accordingly.
2451 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002452 EVT SrcVT = N.getValueType();
2453 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002454 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2455 setValue(&I, Res);
2456
2457 if (DisableScheduling)
2458 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002459}
2460
Dan Gohman2048b852009-11-23 18:04:58 +00002461void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002462 // What to do depends on the size of the integer and the size of the pointer.
2463 // We can either truncate, zero extend, or no-op, accordingly.
2464 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002465 EVT SrcVT = N.getValueType();
2466 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002467 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2468 setValue(&I, Res);
2469
2470 if (DisableScheduling)
2471 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002472}
2473
Dan Gohman2048b852009-11-23 18:04:58 +00002474void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002475 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002476 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002477
Bill Wendling49fcff82009-12-21 22:30:11 +00002478 // BitCast assures us that source and destination are the same size so this is
2479 // either a BIT_CONVERT or a no-op.
2480 if (DestVT != N.getValueType()) {
2481 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2482 DestVT, N); // convert types.
2483 setValue(&I, Res);
2484
2485 if (DisableScheduling)
2486 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2487 } else {
2488 setValue(&I, N); // noop cast.
2489 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002490}
2491
Dan Gohman2048b852009-11-23 18:04:58 +00002492void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002493 SDValue InVec = getValue(I.getOperand(0));
2494 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002495 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002496 TLI.getPointerTy(),
2497 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002498 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2499 TLI.getValueType(I.getType()),
2500 InVec, InVal, InIdx);
2501 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002502
Bill Wendling87710f02009-12-21 23:47:40 +00002503 if (DisableScheduling) {
2504 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002505 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002506 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002507}
2508
Dan Gohman2048b852009-11-23 18:04:58 +00002509void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002510 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002511 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002512 TLI.getPointerTy(),
2513 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002514 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2515 TLI.getValueType(I.getType()), InVec, InIdx);
2516 setValue(&I, Res);
2517
Bill Wendling87710f02009-12-21 23:47:40 +00002518 if (DisableScheduling) {
2519 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002520 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002521 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002522}
2523
Mon P Wangaeb06d22008-11-10 04:46:22 +00002524
2525// Utility for visitShuffleVector - Returns true if the mask is mask starting
2526// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002527static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2528 unsigned MaskNumElts = Mask.size();
2529 for (unsigned i = 0; i != MaskNumElts; ++i)
2530 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002531 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002532 return true;
2533}
2534
Dan Gohman2048b852009-11-23 18:04:58 +00002535void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002536 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002537 SDValue Src1 = getValue(I.getOperand(0));
2538 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002539
Nate Begeman9008ca62009-04-27 18:41:29 +00002540 // Convert the ConstantVector mask operand into an array of ints, with -1
2541 // representing undef values.
2542 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002543 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2544 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002545 unsigned MaskNumElts = MaskElts.size();
2546 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002547 if (isa<UndefValue>(MaskElts[i]))
2548 Mask.push_back(-1);
2549 else
2550 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2551 }
2552
Owen Andersone50ed302009-08-10 22:56:29 +00002553 EVT VT = TLI.getValueType(I.getType());
2554 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002555 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002556
Mon P Wangc7849c22008-11-16 05:06:27 +00002557 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002558 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2559 &Mask[0]);
2560 setValue(&I, Res);
2561
2562 if (DisableScheduling)
2563 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2564
Mon P Wangaeb06d22008-11-10 04:46:22 +00002565 return;
2566 }
2567
2568 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002569 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2570 // Mask is longer than the source vectors and is a multiple of the source
2571 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002572 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002573 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2574 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002575 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2576 VT, Src1, Src2);
2577 setValue(&I, Res);
2578
2579 if (DisableScheduling)
2580 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2581
Mon P Wangaeb06d22008-11-10 04:46:22 +00002582 return;
2583 }
2584
Mon P Wangc7849c22008-11-16 05:06:27 +00002585 // Pad both vectors with undefs to make them the same length as the mask.
2586 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002587 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2588 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002589 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002590
Nate Begeman9008ca62009-04-27 18:41:29 +00002591 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2592 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002593 MOps1[0] = Src1;
2594 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002595
2596 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2597 getCurDebugLoc(), VT,
2598 &MOps1[0], NumConcat);
2599 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2600 getCurDebugLoc(), VT,
2601 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002602
Mon P Wangaeb06d22008-11-10 04:46:22 +00002603 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002604 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002605 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002606 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002607 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002608 MappedOps.push_back(Idx);
2609 else
2610 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002611 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002612
2613 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2614 &MappedOps[0]);
2615 setValue(&I, Res);
2616
Bill Wendlinge1a90422009-12-21 23:10:19 +00002617 if (DisableScheduling) {
2618 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2619 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002620 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002621 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002622
Mon P Wangaeb06d22008-11-10 04:46:22 +00002623 return;
2624 }
2625
Mon P Wangc7849c22008-11-16 05:06:27 +00002626 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002627 // Analyze the access pattern of the vector to see if we can extract
2628 // two subvectors and do the shuffle. The analysis is done by calculating
2629 // the range of elements the mask access on both vectors.
2630 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2631 int MaxRange[2] = {-1, -1};
2632
Nate Begeman5a5ca152009-04-29 05:20:52 +00002633 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002634 int Idx = Mask[i];
2635 int Input = 0;
2636 if (Idx < 0)
2637 continue;
2638
Nate Begeman5a5ca152009-04-29 05:20:52 +00002639 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002640 Input = 1;
2641 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002642 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002643 if (Idx > MaxRange[Input])
2644 MaxRange[Input] = Idx;
2645 if (Idx < MinRange[Input])
2646 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002647 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002648
Mon P Wangc7849c22008-11-16 05:06:27 +00002649 // Check if the access is smaller than the vector size and can we find
2650 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002651 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002652 int StartIdx[2]; // StartIdx to extract from
2653 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002654 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002655 RangeUse[Input] = 0; // Unused
2656 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002657 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002658 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002659 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002660 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002661 RangeUse[Input] = 1; // Extract from beginning of the vector
2662 StartIdx[Input] = 0;
2663 } else {
2664 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002665 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002666 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002667 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002668 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002669 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002670 }
2671
Bill Wendling636e2582009-08-21 18:16:06 +00002672 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002673 SDValue Res = DAG.getUNDEF(VT);
2674 setValue(&I, Res); // Vectors are not used.
2675
2676 if (DisableScheduling)
2677 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2678
Mon P Wangc7849c22008-11-16 05:06:27 +00002679 return;
2680 }
2681 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2682 // Extract appropriate subvector and generate a vector shuffle
2683 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002684 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002685 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002686 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002687 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002688 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002689 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002690
2691 if (DisableScheduling)
2692 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002693 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002694
Mon P Wangc7849c22008-11-16 05:06:27 +00002695 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002696 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002697 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002698 int Idx = Mask[i];
2699 if (Idx < 0)
2700 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002701 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002702 MappedOps.push_back(Idx - StartIdx[0]);
2703 else
2704 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002705 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002706
2707 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2708 &MappedOps[0]);
2709 setValue(&I, Res);
2710
2711 if (DisableScheduling)
2712 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2713
Mon P Wangc7849c22008-11-16 05:06:27 +00002714 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002715 }
2716 }
2717
Mon P Wangc7849c22008-11-16 05:06:27 +00002718 // We can't use either concat vectors or extract subvectors so fall back to
2719 // replacing the shuffle with extract and build vector.
2720 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002721 EVT EltVT = VT.getVectorElementType();
2722 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002723 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002724 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002725 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002726 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002727 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002728 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002729 SDValue Res;
2730
Nate Begeman5a5ca152009-04-29 05:20:52 +00002731 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002732 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2733 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002734 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002735 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2736 EltVT, Src2,
2737 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2738
2739 Ops.push_back(Res);
2740
2741 if (DisableScheduling)
2742 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002743 }
2744 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002745
2746 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2747 VT, &Ops[0], Ops.size());
2748 setValue(&I, Res);
2749
2750 if (DisableScheduling)
2751 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002752}
2753
Dan Gohman2048b852009-11-23 18:04:58 +00002754void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002755 const Value *Op0 = I.getOperand(0);
2756 const Value *Op1 = I.getOperand(1);
2757 const Type *AggTy = I.getType();
2758 const Type *ValTy = Op1->getType();
2759 bool IntoUndef = isa<UndefValue>(Op0);
2760 bool FromUndef = isa<UndefValue>(Op1);
2761
2762 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2763 I.idx_begin(), I.idx_end());
2764
Owen Andersone50ed302009-08-10 22:56:29 +00002765 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002766 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002767 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002768 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2769
2770 unsigned NumAggValues = AggValueVTs.size();
2771 unsigned NumValValues = ValValueVTs.size();
2772 SmallVector<SDValue, 4> Values(NumAggValues);
2773
2774 SDValue Agg = getValue(Op0);
2775 SDValue Val = getValue(Op1);
2776 unsigned i = 0;
2777 // Copy the beginning value(s) from the original aggregate.
2778 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002779 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002780 SDValue(Agg.getNode(), Agg.getResNo() + i);
2781 // Copy values from the inserted value(s).
2782 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002783 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002784 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2785 // Copy remaining value(s) from the original aggregate.
2786 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002787 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002788 SDValue(Agg.getNode(), Agg.getResNo() + i);
2789
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002790 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2791 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2792 &Values[0], NumAggValues);
2793 setValue(&I, Res);
2794
2795 if (DisableScheduling)
2796 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002797}
2798
Dan Gohman2048b852009-11-23 18:04:58 +00002799void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002800 const Value *Op0 = I.getOperand(0);
2801 const Type *AggTy = Op0->getType();
2802 const Type *ValTy = I.getType();
2803 bool OutOfUndef = isa<UndefValue>(Op0);
2804
2805 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2806 I.idx_begin(), I.idx_end());
2807
Owen Andersone50ed302009-08-10 22:56:29 +00002808 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002809 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2810
2811 unsigned NumValValues = ValValueVTs.size();
2812 SmallVector<SDValue, 4> Values(NumValValues);
2813
2814 SDValue Agg = getValue(Op0);
2815 // Copy out the selected value(s).
2816 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2817 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002818 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002819 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002820 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002821
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002822 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2823 DAG.getVTList(&ValValueVTs[0], NumValValues),
2824 &Values[0], NumValValues);
2825 setValue(&I, Res);
2826
2827 if (DisableScheduling)
2828 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002829}
2830
Dan Gohman2048b852009-11-23 18:04:58 +00002831void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002832 SDValue N = getValue(I.getOperand(0));
2833 const Type *Ty = I.getOperand(0)->getType();
2834
2835 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2836 OI != E; ++OI) {
2837 Value *Idx = *OI;
2838 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2839 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2840 if (Field) {
2841 // N = N + Offset
2842 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002843 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002844 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002845
2846 if (DisableScheduling)
2847 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002849
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002850 Ty = StTy->getElementType(Field);
2851 } else {
2852 Ty = cast<SequentialType>(Ty)->getElementType();
2853
2854 // If this is a constant subscript, handle it quickly.
2855 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2856 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002857 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002858 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002859 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002860 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002861 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002862 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002863 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2864 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002865 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002866 else
Evan Chengb1032a82009-02-09 20:54:38 +00002867 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002868
Dale Johannesen66978ee2009-01-31 02:22:37 +00002869 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002870 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002871
2872 if (DisableScheduling) {
2873 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2874 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2875 }
2876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002877 continue;
2878 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002879
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002880 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002881 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2882 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002883 SDValue IdxN = getValue(Idx);
2884
2885 // If the index is smaller or larger than intptr_t, truncate or extend
2886 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002887 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002888
2889 // If this is a multiply by a power of two, turn it into a shl
2890 // immediately. This is a very common case.
2891 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002892 if (ElementSize.isPowerOf2()) {
2893 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002894 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002895 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002896 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002897 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002898 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002899 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002900 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002901 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002902
2903 if (DisableScheduling)
2904 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002905 }
2906
Scott Michelfdc40a02009-02-17 22:15:04 +00002907 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002908 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002909
2910 if (DisableScheduling)
2911 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002912 }
2913 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002914
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002915 setValue(&I, N);
2916}
2917
Dan Gohman2048b852009-11-23 18:04:58 +00002918void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002919 // If this is a fixed sized alloca in the entry block of the function,
2920 // allocate it statically on the stack.
2921 if (FuncInfo.StaticAllocaMap.count(&I))
2922 return; // getValue will auto-populate this.
2923
2924 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002925 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002926 unsigned Align =
2927 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2928 I.getAlignment());
2929
2930 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002931
2932 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2933 AllocSize,
2934 DAG.getConstant(TySize, AllocSize.getValueType()));
2935
Bill Wendling856ff412009-12-22 00:12:37 +00002936 if (DisableScheduling)
2937 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Chris Lattner0b18e592009-03-17 19:36:00 +00002938
Owen Andersone50ed302009-08-10 22:56:29 +00002939 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002940 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002941
Bill Wendling856ff412009-12-22 00:12:37 +00002942 if (DisableScheduling)
2943 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002945 // Handle alignment. If the requested alignment is less than or equal to
2946 // the stack alignment, ignore it. If the size is greater than or equal to
2947 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2948 unsigned StackAlign =
2949 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2950 if (Align <= StackAlign)
2951 Align = 0;
2952
2953 // Round the size of the allocation up to the stack alignment size
2954 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002955 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002956 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002957 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002958 if (DisableScheduling)
2959 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2960
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002961 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002962 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002963 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002964 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002965 if (DisableScheduling)
2966 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002967
2968 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002969 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002970 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002971 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002972 setValue(&I, DSA);
2973 DAG.setRoot(DSA.getValue(1));
2974
Bill Wendling856ff412009-12-22 00:12:37 +00002975 if (DisableScheduling)
2976 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
2977
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002978 // Inform the Frame Information that we have just allocated a variable-sized
2979 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002980 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002981}
2982
Dan Gohman2048b852009-11-23 18:04:58 +00002983void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002984 const Value *SV = I.getOperand(0);
2985 SDValue Ptr = getValue(SV);
2986
2987 const Type *Ty = I.getType();
2988 bool isVolatile = I.isVolatile();
2989 unsigned Alignment = I.getAlignment();
2990
Owen Andersone50ed302009-08-10 22:56:29 +00002991 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002992 SmallVector<uint64_t, 4> Offsets;
2993 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2994 unsigned NumValues = ValueVTs.size();
2995 if (NumValues == 0)
2996 return;
2997
2998 SDValue Root;
2999 bool ConstantMemory = false;
3000 if (I.isVolatile())
3001 // Serialize volatile loads with other side effects.
3002 Root = getRoot();
3003 else if (AA->pointsToConstantMemory(SV)) {
3004 // Do not serialize (non-volatile) loads of constant memory with anything.
3005 Root = DAG.getEntryNode();
3006 ConstantMemory = true;
3007 } else {
3008 // Do not serialize non-volatile loads against each other.
3009 Root = DAG.getRoot();
3010 }
3011
3012 SmallVector<SDValue, 4> Values(NumValues);
3013 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003014 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003015 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00003016 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
3017 PtrVT, Ptr,
3018 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003019 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00003020 A, SV, Offsets[i], isVolatile, Alignment);
3021
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003022 Values[i] = L;
3023 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00003024
3025 if (DisableScheduling) {
3026 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
3027 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3028 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003029 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003031 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003032 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003033 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003034 if (isVolatile)
3035 DAG.setRoot(Chain);
3036 else
3037 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00003038
3039 if (DisableScheduling)
3040 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003041 }
3042
Bill Wendling856ff412009-12-22 00:12:37 +00003043 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
3044 DAG.getVTList(&ValueVTs[0], NumValues),
3045 &Values[0], NumValues);
3046 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003047
Bill Wendling856ff412009-12-22 00:12:37 +00003048 if (DisableScheduling)
3049 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
3050}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003051
Dan Gohman2048b852009-11-23 18:04:58 +00003052void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003053 Value *SrcV = I.getOperand(0);
3054 Value *PtrV = I.getOperand(1);
3055
Owen Andersone50ed302009-08-10 22:56:29 +00003056 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003057 SmallVector<uint64_t, 4> Offsets;
3058 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3059 unsigned NumValues = ValueVTs.size();
3060 if (NumValues == 0)
3061 return;
3062
3063 // Get the lowered operands. Note that we do this after
3064 // checking if NumResults is zero, because with zero results
3065 // the operands won't have values in the map.
3066 SDValue Src = getValue(SrcV);
3067 SDValue Ptr = getValue(PtrV);
3068
3069 SDValue Root = getRoot();
3070 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003071 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003072 bool isVolatile = I.isVolatile();
3073 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003074
3075 for (unsigned i = 0; i != NumValues; ++i) {
3076 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3077 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003078 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003079 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003080 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003081
Bill Wendling856ff412009-12-22 00:12:37 +00003082 if (DisableScheduling) {
3083 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3084 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3085 }
3086 }
3087
3088 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3089 MVT::Other, &Chains[0], NumValues);
3090 DAG.setRoot(Res);
3091
3092 if (DisableScheduling)
3093 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003094}
3095
3096/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3097/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003098void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3099 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003100 bool HasChain = !I.doesNotAccessMemory();
3101 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3102
3103 // Build the operand list.
3104 SmallVector<SDValue, 8> Ops;
3105 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3106 if (OnlyLoad) {
3107 // We don't need to serialize loads against other loads.
3108 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003109 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003110 Ops.push_back(getRoot());
3111 }
3112 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003113
3114 // Info is set by getTgtMemInstrinsic
3115 TargetLowering::IntrinsicInfo Info;
3116 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3117
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003118 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003119 if (!IsTgtIntrinsic)
3120 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003121
3122 // Add all operands of the call to the operand list.
3123 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3124 SDValue Op = getValue(I.getOperand(i));
3125 assert(TLI.isTypeLegal(Op.getValueType()) &&
3126 "Intrinsic uses a non-legal type?");
3127 Ops.push_back(Op);
3128 }
3129
Owen Andersone50ed302009-08-10 22:56:29 +00003130 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003131 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3132#ifndef NDEBUG
3133 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3134 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3135 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003136 }
Bob Wilson8d919552009-07-31 22:41:21 +00003137#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003139 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003140 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003141
Bob Wilson8d919552009-07-31 22:41:21 +00003142 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003143
3144 // Create the node.
3145 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003146 if (IsTgtIntrinsic) {
3147 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003148 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003149 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003150 Info.memVT, Info.ptrVal, Info.offset,
3151 Info.align, Info.vol,
3152 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003153 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003154 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003155 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003156 } else if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003157 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003158 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003159 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003160 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003161 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003162 }
3163
3164 if (DisableScheduling)
3165 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003166
3167 if (HasChain) {
3168 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3169 if (OnlyLoad)
3170 PendingLoads.push_back(Chain);
3171 else
3172 DAG.setRoot(Chain);
3173 }
Bill Wendling856ff412009-12-22 00:12:37 +00003174
Owen Anderson1d0be152009-08-13 21:58:54 +00003175 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003176 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003177 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003178 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003179
3180 if (DisableScheduling)
3181 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003182 }
Bill Wendling856ff412009-12-22 00:12:37 +00003183
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003184 setValue(&I, Result);
3185 }
3186}
3187
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003188/// GetSignificand - Get the significand and build it into a floating-point
3189/// number with exponent of 1:
3190///
3191/// Op = (Op & 0x007fffff) | 0x3f800000;
3192///
3193/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003194static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003195GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003196 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3197 DAG.getConstant(0x007fffff, MVT::i32));
3198 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3199 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003200 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3201
3202 if (DisableScheduling) {
3203 DAG.AssignOrdering(t1.getNode(), Order);
3204 DAG.AssignOrdering(t2.getNode(), Order);
3205 DAG.AssignOrdering(Res.getNode(), Order);
3206 }
3207
3208 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003209}
3210
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003211/// GetExponent - Get the exponent:
3212///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003213/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003214///
3215/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003216static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003217GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003218 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003219 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3220 DAG.getConstant(0x7f800000, MVT::i32));
3221 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003222 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003223 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3224 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003225 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3226
3227 if (DisableScheduling) {
3228 DAG.AssignOrdering(t0.getNode(), Order);
3229 DAG.AssignOrdering(t1.getNode(), Order);
3230 DAG.AssignOrdering(t2.getNode(), Order);
3231 DAG.AssignOrdering(Res.getNode(), Order);
3232 }
3233
3234 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003235}
3236
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003237/// getF32Constant - Get 32-bit floating point constant.
3238static SDValue
3239getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003240 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003241}
3242
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003243/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003244/// visitIntrinsicCall: I is a call instruction
3245/// Op is the associated NodeType for I
3246const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003247SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003248 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003249 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003250 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003251 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003252 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003253 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003254 getValue(I.getOperand(2)),
3255 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003256 setValue(&I, L);
3257 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003258
3259 if (DisableScheduling)
3260 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3261
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003262 return 0;
3263}
3264
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003265// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003266const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003267SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003268 SDValue Op1 = getValue(I.getOperand(1));
3269 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003270
Owen Anderson825b72b2009-08-11 20:47:22 +00003271 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003272 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003273
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003274 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003275
3276 if (DisableScheduling)
3277 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3278
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003279 return 0;
3280}
Bill Wendling74c37652008-12-09 22:08:41 +00003281
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003282/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3283/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003284void
Dan Gohman2048b852009-11-23 18:04:58 +00003285SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003286 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003287 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003288
Owen Anderson825b72b2009-08-11 20:47:22 +00003289 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003290 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3291 SDValue Op = getValue(I.getOperand(1));
3292
3293 // Put the exponent in the right bit position for later addition to the
3294 // final result:
3295 //
3296 // #define LOG2OFe 1.4426950f
3297 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003298 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003299 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003301
3302 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003303 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3304 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003305
Bill Wendling856ff412009-12-22 00:12:37 +00003306 if (DisableScheduling) {
3307 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3308 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3309 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3310 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3311 }
3312
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003313 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003315 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003316
Bill Wendling856ff412009-12-22 00:12:37 +00003317 if (DisableScheduling)
3318 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3319
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003320 if (LimitFloatPrecision <= 6) {
3321 // For floating-point precision of 6:
3322 //
3323 // TwoToFractionalPartOfX =
3324 // 0.997535578f +
3325 // (0.735607626f + 0.252464424f * x) * x;
3326 //
3327 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003329 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003331 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3333 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003334 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003336
3337 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003338 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003339 TwoToFracPartOfX, IntegerPartOfX);
3340
Owen Anderson825b72b2009-08-11 20:47:22 +00003341 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003342
3343 if (DisableScheduling) {
3344 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3345 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3346 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3347 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3348 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3349 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3350 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3351 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003352 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3353 // For floating-point precision of 12:
3354 //
3355 // TwoToFractionalPartOfX =
3356 // 0.999892986f +
3357 // (0.696457318f +
3358 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3359 //
3360 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003362 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003363 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003364 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3366 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003367 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003368 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3369 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003370 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003371 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003372
3373 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003374 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003375 TwoToFracPartOfX, IntegerPartOfX);
3376
Owen Anderson825b72b2009-08-11 20:47:22 +00003377 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003378
3379 if (DisableScheduling) {
3380 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3381 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3382 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3383 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3384 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3385 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3386 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3387 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3388 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3389 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003390 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3391 // For floating-point precision of 18:
3392 //
3393 // TwoToFractionalPartOfX =
3394 // 0.999999982f +
3395 // (0.693148872f +
3396 // (0.240227044f +
3397 // (0.554906021e-1f +
3398 // (0.961591928e-2f +
3399 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3400 //
3401 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003403 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003404 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003405 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3407 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003408 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003409 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3410 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003411 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003412 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3413 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003414 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003415 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3416 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003417 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003418 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3419 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003420 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003421 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003423
3424 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003426 TwoToFracPartOfX, IntegerPartOfX);
3427
Owen Anderson825b72b2009-08-11 20:47:22 +00003428 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003429
3430 if (DisableScheduling) {
3431 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3432 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3433 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3434 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3435 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3436 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3437 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3438 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3439 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3440 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3441 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3442 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3443 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3444 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3445 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3446 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003447 }
3448 } else {
3449 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003450 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003451 getValue(I.getOperand(1)).getValueType(),
3452 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003453 if (DisableScheduling)
3454 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003455 }
3456
Dale Johannesen59e577f2008-09-05 18:38:42 +00003457 setValue(&I, result);
3458}
3459
Bill Wendling39150252008-09-09 20:39:27 +00003460/// visitLog - Lower a log intrinsic. Handles the special sequences for
3461/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003462void
Dan Gohman2048b852009-11-23 18:04:58 +00003463SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003464 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003465 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003466
Owen Anderson825b72b2009-08-11 20:47:22 +00003467 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003468 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3469 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003470 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003471
Bill Wendling856ff412009-12-22 00:12:37 +00003472 if (DisableScheduling)
3473 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3474
Bill Wendling39150252008-09-09 20:39:27 +00003475 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003476 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003477 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003478 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003479
Bill Wendling856ff412009-12-22 00:12:37 +00003480 if (DisableScheduling)
3481 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3482
Bill Wendling39150252008-09-09 20:39:27 +00003483 // Get the significand and build it into a floating-point number with
3484 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003485 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003486
3487 if (LimitFloatPrecision <= 6) {
3488 // For floating-point precision of 6:
3489 //
3490 // LogofMantissa =
3491 // -1.1609546f +
3492 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003493 //
Bill Wendling39150252008-09-09 20:39:27 +00003494 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003496 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003497 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003498 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003499 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3500 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003501 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003502
Scott Michelfdc40a02009-02-17 22:15:04 +00003503 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003504 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003505
3506 if (DisableScheduling) {
3507 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3508 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3509 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3510 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3511 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3512 }
Bill Wendling39150252008-09-09 20:39:27 +00003513 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3514 // For floating-point precision of 12:
3515 //
3516 // LogOfMantissa =
3517 // -1.7417939f +
3518 // (2.8212026f +
3519 // (-1.4699568f +
3520 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3521 //
3522 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003523 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003524 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003525 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003526 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3528 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003529 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003530 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3531 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003532 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3534 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003535 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003536
Scott Michelfdc40a02009-02-17 22:15:04 +00003537 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003539
3540 if (DisableScheduling) {
3541 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3542 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3543 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3544 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3545 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3546 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3547 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3548 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3549 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3550 }
Bill Wendling39150252008-09-09 20:39:27 +00003551 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3552 // For floating-point precision of 18:
3553 //
3554 // LogOfMantissa =
3555 // -2.1072184f +
3556 // (4.2372794f +
3557 // (-3.7029485f +
3558 // (2.2781945f +
3559 // (-0.87823314f +
3560 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3561 //
3562 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003564 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3568 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003569 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003570 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3571 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003572 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003573 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3574 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003575 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003576 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3577 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003578 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3580 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003581 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003582
Scott Michelfdc40a02009-02-17 22:15:04 +00003583 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003584 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003585
3586 if (DisableScheduling) {
3587 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3588 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3589 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3590 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3591 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3592 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3593 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3594 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3595 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3596 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3597 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3598 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3599 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3600 }
Bill Wendling39150252008-09-09 20:39:27 +00003601 }
3602 } else {
3603 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003604 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003605 getValue(I.getOperand(1)).getValueType(),
3606 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003607
3608 if (DisableScheduling)
3609 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003610 }
3611
Dale Johannesen59e577f2008-09-05 18:38:42 +00003612 setValue(&I, result);
3613}
3614
Bill Wendling3eb59402008-09-09 00:28:24 +00003615/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3616/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003617void
Dan Gohman2048b852009-11-23 18:04:58 +00003618SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003619 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003620 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003621
Owen Anderson825b72b2009-08-11 20:47:22 +00003622 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003623 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3624 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003625 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003626
Bill Wendling856ff412009-12-22 00:12:37 +00003627 if (DisableScheduling)
3628 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3629
Bill Wendling39150252008-09-09 20:39:27 +00003630 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003631 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3632
3633 if (DisableScheduling)
3634 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003635
3636 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003637 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003638 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003639
Bill Wendling3eb59402008-09-09 00:28:24 +00003640 // Different possible minimax approximations of significand in
3641 // floating-point for various degrees of accuracy over [1,2].
3642 if (LimitFloatPrecision <= 6) {
3643 // For floating-point precision of 6:
3644 //
3645 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3646 //
3647 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003648 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003649 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003650 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003651 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003652 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3653 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003654 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003655
Scott Michelfdc40a02009-02-17 22:15:04 +00003656 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003657 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003658
3659 if (DisableScheduling) {
3660 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3661 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3662 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3663 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3664 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3665 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003666 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3667 // For floating-point precision of 12:
3668 //
3669 // Log2ofMantissa =
3670 // -2.51285454f +
3671 // (4.07009056f +
3672 // (-2.12067489f +
3673 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003674 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003675 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003676 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003677 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003678 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003679 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3681 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003682 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003683 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3684 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003685 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003686 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3687 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003688 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003689
Scott Michelfdc40a02009-02-17 22:15:04 +00003690 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003691 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003692
3693 if (DisableScheduling) {
3694 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3695 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3696 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3697 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3698 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3699 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3700 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3701 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3702 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3703 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003704 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3705 // For floating-point precision of 18:
3706 //
3707 // Log2ofMantissa =
3708 // -3.0400495f +
3709 // (6.1129976f +
3710 // (-5.3420409f +
3711 // (3.2865683f +
3712 // (-1.2669343f +
3713 // (0.27515199f -
3714 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3715 //
3716 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003717 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003718 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003719 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003720 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003721 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3722 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003723 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003724 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3725 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003726 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003727 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3728 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003729 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003730 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3731 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003732 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003733 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3734 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003735 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003736
Scott Michelfdc40a02009-02-17 22:15:04 +00003737 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003738 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003739
3740 if (DisableScheduling) {
3741 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3742 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3743 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3744 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3745 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3746 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3747 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3748 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3749 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3750 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3751 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3752 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3753 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3754 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003755 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003756 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003757 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003758 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003759 getValue(I.getOperand(1)).getValueType(),
3760 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003761
3762 if (DisableScheduling)
3763 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003764 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003765
Dale Johannesen59e577f2008-09-05 18:38:42 +00003766 setValue(&I, result);
3767}
3768
Bill Wendling3eb59402008-09-09 00:28:24 +00003769/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3770/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003771void
Dan Gohman2048b852009-11-23 18:04:58 +00003772SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003773 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003774 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003775
Owen Anderson825b72b2009-08-11 20:47:22 +00003776 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003777 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3778 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003779 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003780
Bill Wendling856ff412009-12-22 00:12:37 +00003781 if (DisableScheduling)
3782 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3783
Bill Wendling39150252008-09-09 20:39:27 +00003784 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003785 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003786 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003787 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003788
Bill Wendling856ff412009-12-22 00:12:37 +00003789 if (DisableScheduling)
3790 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3791
Bill Wendling3eb59402008-09-09 00:28:24 +00003792 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003793 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003794 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003795
3796 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003797 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003798 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003799 // Log10ofMantissa =
3800 // -0.50419619f +
3801 // (0.60948995f - 0.10380950f * x) * x;
3802 //
3803 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003804 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003805 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003806 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003807 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003808 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3809 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003810 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003811
Scott Michelfdc40a02009-02-17 22:15:04 +00003812 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003813 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003814
3815 if (DisableScheduling) {
3816 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3817 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3818 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3819 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3820 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3821 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003822 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3823 // For floating-point precision of 12:
3824 //
3825 // Log10ofMantissa =
3826 // -0.64831180f +
3827 // (0.91751397f +
3828 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3829 //
3830 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003831 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003832 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003833 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003834 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003835 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3836 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003837 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003838 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3839 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003840 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003841
Scott Michelfdc40a02009-02-17 22:15:04 +00003842 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003843 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003844
3845 if (DisableScheduling) {
3846 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3847 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3848 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3849 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3850 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3851 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3852 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3853 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003854 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003855 // For floating-point precision of 18:
3856 //
3857 // Log10ofMantissa =
3858 // -0.84299375f +
3859 // (1.5327582f +
3860 // (-1.0688956f +
3861 // (0.49102474f +
3862 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3863 //
3864 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003865 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003866 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003867 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003868 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003869 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3870 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003871 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003872 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3873 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003874 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003875 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3876 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003877 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003878 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3879 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003880 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003881
Scott Michelfdc40a02009-02-17 22:15:04 +00003882 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003883 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003884
3885 if (DisableScheduling) {
3886 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3887 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3888 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3889 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3890 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3891 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3892 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3893 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3894 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3895 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3896 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3897 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003898 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003899 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003900 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003901 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003902 getValue(I.getOperand(1)).getValueType(),
3903 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003904
3905 if (DisableScheduling)
3906 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003907 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003908
Dale Johannesen59e577f2008-09-05 18:38:42 +00003909 setValue(&I, result);
3910}
3911
Bill Wendlinge10c8142008-09-09 22:39:21 +00003912/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3913/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003914void
Dan Gohman2048b852009-11-23 18:04:58 +00003915SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003916 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003917 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003918
Owen Anderson825b72b2009-08-11 20:47:22 +00003919 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003920 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3921 SDValue Op = getValue(I.getOperand(1));
3922
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003924
Bill Wendling856ff412009-12-22 00:12:37 +00003925 if (DisableScheduling)
3926 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3927
Bill Wendlinge10c8142008-09-09 22:39:21 +00003928 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003929 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3930 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003931
3932 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003933 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003934 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003935
Bill Wendling856ff412009-12-22 00:12:37 +00003936 if (DisableScheduling) {
3937 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3938 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3939 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3940 }
3941
Bill Wendlinge10c8142008-09-09 22:39:21 +00003942 if (LimitFloatPrecision <= 6) {
3943 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003944 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003945 // TwoToFractionalPartOfX =
3946 // 0.997535578f +
3947 // (0.735607626f + 0.252464424f * x) * x;
3948 //
3949 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003950 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003951 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003952 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003953 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3955 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003956 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003957 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003958 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003959 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003960
Scott Michelfdc40a02009-02-17 22:15:04 +00003961 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003962 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003963
3964 if (DisableScheduling) {
3965 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3966 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3967 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3968 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3969 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3970 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3971 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3972 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003973 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3974 // For floating-point precision of 12:
3975 //
3976 // TwoToFractionalPartOfX =
3977 // 0.999892986f +
3978 // (0.696457318f +
3979 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3980 //
3981 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003982 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003983 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003985 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003986 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3987 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003988 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003989 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3990 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003991 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003992 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003993 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003994 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003995
Scott Michelfdc40a02009-02-17 22:15:04 +00003996 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003997 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003998
3999 if (DisableScheduling) {
4000 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4001 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4002 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4003 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4004 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4005 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4006 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4007 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4008 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4009 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004010 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4011 // For floating-point precision of 18:
4012 //
4013 // TwoToFractionalPartOfX =
4014 // 0.999999982f +
4015 // (0.693148872f +
4016 // (0.240227044f +
4017 // (0.554906021e-1f +
4018 // (0.961591928e-2f +
4019 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4020 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004021 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004022 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004023 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004024 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004025 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4026 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004027 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004028 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4029 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004030 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004031 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4032 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004033 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004034 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4035 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004036 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004037 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4038 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004039 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004040 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004041 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004042 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004043
Scott Michelfdc40a02009-02-17 22:15:04 +00004044 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004045 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004046
4047 if (DisableScheduling) {
4048 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4049 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4050 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4051 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4052 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4053 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4054 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4055 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4056 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4057 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4058 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4059 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4060 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4061 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4062 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4063 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004064 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00004065 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00004066 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004067 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00004068 getValue(I.getOperand(1)).getValueType(),
4069 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00004070
4071 if (DisableScheduling)
4072 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004073 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004074
Dale Johannesen601d3c02008-09-05 01:48:15 +00004075 setValue(&I, result);
4076}
4077
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004078/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4079/// limited-precision mode with x == 10.0f.
4080void
Dan Gohman2048b852009-11-23 18:04:58 +00004081SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004082 SDValue result;
4083 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004084 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004085 bool IsExp10 = false;
4086
Owen Anderson825b72b2009-08-11 20:47:22 +00004087 if (getValue(Val).getValueType() == MVT::f32 &&
4088 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004089 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4090 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4091 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4092 APFloat Ten(10.0f);
4093 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4094 }
4095 }
4096 }
4097
4098 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4099 SDValue Op = getValue(I.getOperand(2));
4100
4101 // Put the exponent in the right bit position for later addition to the
4102 // final result:
4103 //
4104 // #define LOG2OF10 3.3219281f
4105 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004106 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004107 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004108 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004109
4110 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004111 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4112 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004113
Bill Wendling856ff412009-12-22 00:12:37 +00004114 if (DisableScheduling) {
4115 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4116 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4117 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4118 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4119 }
4120
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004121 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004122 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004123 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004124
Bill Wendling856ff412009-12-22 00:12:37 +00004125 if (DisableScheduling)
4126 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4127
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004128 if (LimitFloatPrecision <= 6) {
4129 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004130 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004131 // twoToFractionalPartOfX =
4132 // 0.997535578f +
4133 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004134 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004135 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004136 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004137 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004139 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004140 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4141 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004142 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004143 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004144 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004145 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004146
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004147 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004148 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004149
4150 if (DisableScheduling) {
4151 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4152 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4153 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4154 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4155 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4156 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4157 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4158 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004159 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4160 // For floating-point precision of 12:
4161 //
4162 // TwoToFractionalPartOfX =
4163 // 0.999892986f +
4164 // (0.696457318f +
4165 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4166 //
4167 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004168 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004169 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004171 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4173 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004174 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004175 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4176 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004177 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004178 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004179 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004180 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004181
Scott Michelfdc40a02009-02-17 22:15:04 +00004182 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004183 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004184
4185 if (DisableScheduling) {
4186 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4187 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4188 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4189 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4190 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4191 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4192 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4193 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4194 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4195 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004196 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4197 // For floating-point precision of 18:
4198 //
4199 // TwoToFractionalPartOfX =
4200 // 0.999999982f +
4201 // (0.693148872f +
4202 // (0.240227044f +
4203 // (0.554906021e-1f +
4204 // (0.961591928e-2f +
4205 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4206 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004207 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004208 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004209 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004210 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004211 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4212 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004213 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004214 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4215 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004216 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004217 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4218 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004219 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004220 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4221 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004222 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004223 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4224 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004225 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004226 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004227 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004228 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004229
Scott Michelfdc40a02009-02-17 22:15:04 +00004230 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004231 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004232
4233 if (DisableScheduling) {
4234 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4235 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4236 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4237 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4238 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4239 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4240 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4241 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4242 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4243 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4244 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4245 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4246 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4247 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4248 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4249 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004250 }
4251 } else {
4252 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004253 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004254 getValue(I.getOperand(1)).getValueType(),
4255 getValue(I.getOperand(1)),
4256 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004257
4258 if (DisableScheduling)
4259 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004260 }
4261
4262 setValue(&I, result);
4263}
4264
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004265
4266/// ExpandPowI - Expand a llvm.powi intrinsic.
4267static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
4268 SelectionDAG &DAG) {
4269 // If RHS is a constant, we can expand this out to a multiplication tree,
4270 // otherwise we end up lowering to a call to __powidf2 (for example). When
4271 // optimizing for size, we only want to do this if the expansion would produce
4272 // a small number of multiplies, otherwise we do the full expansion.
4273 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4274 // Get the exponent as a positive value.
4275 unsigned Val = RHSC->getSExtValue();
4276 if ((int)Val < 0) Val = -Val;
4277
4278 // powi(x, 0) -> 1.0
4279 if (Val == 0)
4280 return DAG.getConstantFP(1.0, LHS.getValueType());
4281
4282 Function *F = DAG.getMachineFunction().getFunction();
4283 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
4284 // If optimizing for size, don't insert too many multiplies. This
4285 // inserts up to 5 multiplies.
4286 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4287 // We use the simple binary decomposition method to generate the multiply
4288 // sequence. There are more optimal ways to do this (for example,
4289 // powi(x,15) generates one more multiply than it should), but this has
4290 // the benefit of being both really simple and much better than a libcall.
4291 SDValue Res; // Logically starts equal to 1.0
4292 SDValue CurSquare = LHS;
4293 while (Val) {
4294 if (Val & 1)
4295 if (Res.getNode())
4296 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4297 else
4298 Res = CurSquare; // 1.0*CurSquare.
4299
4300 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4301 CurSquare, CurSquare);
4302 Val >>= 1;
4303 }
4304
4305 // If the original was negative, invert the result, producing 1/(x*x*x).
4306 if (RHSC->getSExtValue() < 0)
4307 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4308 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4309 return Res;
4310 }
4311 }
4312
4313 // Otherwise, expand to a libcall.
4314 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4315}
4316
4317
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004318/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4319/// we want to emit this as a call to a named external function, return the name
4320/// otherwise lower it and return null.
4321const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004322SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004323 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004324 SDValue Res;
4325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004326 switch (Intrinsic) {
4327 default:
4328 // By default, turn this into a target intrinsic node.
4329 visitTargetIntrinsic(I, Intrinsic);
4330 return 0;
4331 case Intrinsic::vastart: visitVAStart(I); return 0;
4332 case Intrinsic::vaend: visitVAEnd(I); return 0;
4333 case Intrinsic::vacopy: visitVACopy(I); return 0;
4334 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004335 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4336 getValue(I.getOperand(1)));
4337 setValue(&I, Res);
4338 if (DisableScheduling)
4339 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004340 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004341 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004342 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4343 getValue(I.getOperand(1)));
4344 setValue(&I, Res);
4345 if (DisableScheduling)
4346 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004347 return 0;
4348 case Intrinsic::setjmp:
4349 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004350 case Intrinsic::longjmp:
4351 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004352 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004353 SDValue Op1 = getValue(I.getOperand(1));
4354 SDValue Op2 = getValue(I.getOperand(2));
4355 SDValue Op3 = getValue(I.getOperand(3));
4356 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004357 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4358 I.getOperand(1), 0, I.getOperand(2), 0);
4359 DAG.setRoot(Res);
4360 if (DisableScheduling)
4361 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004362 return 0;
4363 }
Chris Lattner824b9582008-11-21 16:42:48 +00004364 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004365 SDValue Op1 = getValue(I.getOperand(1));
4366 SDValue Op2 = getValue(I.getOperand(2));
4367 SDValue Op3 = getValue(I.getOperand(3));
4368 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004369 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4370 I.getOperand(1), 0);
4371 DAG.setRoot(Res);
4372 if (DisableScheduling)
4373 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004374 return 0;
4375 }
Chris Lattner824b9582008-11-21 16:42:48 +00004376 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004377 SDValue Op1 = getValue(I.getOperand(1));
4378 SDValue Op2 = getValue(I.getOperand(2));
4379 SDValue Op3 = getValue(I.getOperand(3));
4380 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4381
4382 // If the source and destination are known to not be aliases, we can
4383 // lower memmove as memcpy.
4384 uint64_t Size = -1ULL;
4385 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004386 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004387 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4388 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004389 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4390 I.getOperand(1), 0, I.getOperand(2), 0);
4391 DAG.setRoot(Res);
4392 if (DisableScheduling)
4393 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004394 return 0;
4395 }
4396
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004397 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4398 I.getOperand(1), 0, I.getOperand(2), 0);
4399 DAG.setRoot(Res);
4400 if (DisableScheduling)
4401 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004402 return 0;
4403 }
Devang Patel70d75ca2009-11-12 19:02:56 +00004404 case Intrinsic::dbg_stoppoint:
4405 case Intrinsic::dbg_region_start:
4406 case Intrinsic::dbg_region_end:
4407 case Intrinsic::dbg_func_start:
4408 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004409 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004410 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00004411 if (OptLevel != CodeGenOpt::None)
4412 // FIXME: Variable debug info is not supported here.
4413 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004414 DwarfWriter *DW = DAG.getDwarfWriter();
4415 if (!DW)
4416 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004417 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00004418 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00004419 return 0;
4420
Devang Patelac1ceb32009-10-09 22:42:28 +00004421 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004422 Value *Address = DI.getAddress();
4423 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4424 Address = BCI->getOperand(0);
4425 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4426 // Don't handle byval struct arguments or VLAs, for example.
4427 if (!AI)
4428 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004429 DenseMap<const AllocaInst*, int>::iterator SI =
4430 FuncInfo.StaticAllocaMap.find(AI);
4431 if (SI == FuncInfo.StaticAllocaMap.end())
4432 return 0; // VLAs.
4433 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004434
Chris Lattner3990b122009-12-28 23:41:32 +00004435 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo())
4436 if (MDNode *Dbg = DI.getMetadata("dbg"))
Chris Lattner0eb41982009-12-28 20:45:51 +00004437 MMI->setVariableDbgInfo(Variable, FI, Dbg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004438 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004439 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004440 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004441 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004442 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004443 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004444 SDValue Ops[1];
4445 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004446 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004447 setValue(&I, Op);
4448 DAG.setRoot(Op.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004449 if (DisableScheduling)
4450 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004451 return 0;
4452 }
4453
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004454 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004455 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004456
Chris Lattner3a5815f2009-09-17 23:54:54 +00004457 if (CurMBB->isLandingPad())
4458 AddCatchInfo(I, MMI, CurMBB);
4459 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004460#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004461 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004462#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004463 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4464 unsigned Reg = TLI.getExceptionSelectorRegister();
4465 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004466 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004467
Chris Lattner3a5815f2009-09-17 23:54:54 +00004468 // Insert the EHSELECTION instruction.
4469 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4470 SDValue Ops[2];
4471 Ops[0] = getValue(I.getOperand(1));
4472 Ops[1] = getRoot();
4473 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4474
4475 DAG.setRoot(Op.getValue(1));
4476
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004477 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4478 setValue(&I, Res);
4479 if (DisableScheduling) {
4480 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4481 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4482 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004483 return 0;
4484 }
4485
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004486 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004487 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004488
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004489 if (MMI) {
4490 // Find the type id for the given typeinfo.
4491 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004492 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004493 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004494 } else {
4495 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004496 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004497 }
4498
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004499 setValue(&I, Res);
4500 if (DisableScheduling)
4501 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004502 return 0;
4503 }
4504
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004505 case Intrinsic::eh_return_i32:
4506 case Intrinsic::eh_return_i64:
4507 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004508 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004509 Res = DAG.getNode(ISD::EH_RETURN, dl,
4510 MVT::Other,
4511 getControlRoot(),
4512 getValue(I.getOperand(1)),
4513 getValue(I.getOperand(2)));
4514 DAG.setRoot(Res);
4515 if (DisableScheduling)
4516 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004517 } else {
4518 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4519 }
4520
4521 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004522 case Intrinsic::eh_unwind_init:
4523 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4524 MMI->setCallsUnwindInit(true);
4525 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004526 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004527 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004528 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004529 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4530 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004531 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004532 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004533 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004534 TLI.getPointerTy()),
4535 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004536 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004537 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004538 DAG.getConstant(0, TLI.getPointerTy()));
4539 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4540 FA, Offset);
4541 setValue(&I, Res);
4542 if (DisableScheduling) {
4543 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4544 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4545 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4546 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4547 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004548 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004549 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004550 case Intrinsic::convertff:
4551 case Intrinsic::convertfsi:
4552 case Intrinsic::convertfui:
4553 case Intrinsic::convertsif:
4554 case Intrinsic::convertuif:
4555 case Intrinsic::convertss:
4556 case Intrinsic::convertsu:
4557 case Intrinsic::convertus:
4558 case Intrinsic::convertuu: {
4559 ISD::CvtCode Code = ISD::CVT_INVALID;
4560 switch (Intrinsic) {
4561 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4562 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4563 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4564 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4565 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4566 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4567 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4568 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4569 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4570 }
Owen Andersone50ed302009-08-10 22:56:29 +00004571 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004572 Value *Op1 = I.getOperand(1);
4573 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4574 DAG.getValueType(DestVT),
4575 DAG.getValueType(getValue(Op1).getValueType()),
4576 getValue(I.getOperand(2)),
4577 getValue(I.getOperand(3)),
4578 Code);
4579 setValue(&I, Res);
4580 if (DisableScheduling)
4581 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004582 return 0;
4583 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004584 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004585 Res = DAG.getNode(ISD::FSQRT, dl,
4586 getValue(I.getOperand(1)).getValueType(),
4587 getValue(I.getOperand(1)));
4588 setValue(&I, Res);
4589 if (DisableScheduling)
4590 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004591 return 0;
4592 case Intrinsic::powi:
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004593 Res = ExpandPowI(dl, getValue(I.getOperand(1)), getValue(I.getOperand(2)),
4594 DAG);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004595 setValue(&I, Res);
4596 if (DisableScheduling)
4597 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004598 return 0;
4599 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004600 Res = DAG.getNode(ISD::FSIN, dl,
4601 getValue(I.getOperand(1)).getValueType(),
4602 getValue(I.getOperand(1)));
4603 setValue(&I, Res);
4604 if (DisableScheduling)
4605 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004606 return 0;
4607 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004608 Res = DAG.getNode(ISD::FCOS, dl,
4609 getValue(I.getOperand(1)).getValueType(),
4610 getValue(I.getOperand(1)));
4611 setValue(&I, Res);
4612 if (DisableScheduling)
4613 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004614 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004615 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004616 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004617 return 0;
4618 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004619 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004620 return 0;
4621 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004622 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004623 return 0;
4624 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004625 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004626 return 0;
4627 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004628 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004629 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004630 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004631 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004632 return 0;
4633 case Intrinsic::pcmarker: {
4634 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004635 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4636 DAG.setRoot(Res);
4637 if (DisableScheduling)
4638 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004639 return 0;
4640 }
4641 case Intrinsic::readcyclecounter: {
4642 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004643 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4644 DAG.getVTList(MVT::i64, MVT::Other),
4645 &Op, 1);
4646 setValue(&I, Res);
4647 DAG.setRoot(Res.getValue(1));
4648 if (DisableScheduling)
4649 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650 return 0;
4651 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004652 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004653 Res = DAG.getNode(ISD::BSWAP, dl,
4654 getValue(I.getOperand(1)).getValueType(),
4655 getValue(I.getOperand(1)));
4656 setValue(&I, Res);
4657 if (DisableScheduling)
4658 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004659 return 0;
4660 case Intrinsic::cttz: {
4661 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004662 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004663 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4664 setValue(&I, Res);
4665 if (DisableScheduling)
4666 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004667 return 0;
4668 }
4669 case Intrinsic::ctlz: {
4670 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004671 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004672 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4673 setValue(&I, Res);
4674 if (DisableScheduling)
4675 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 return 0;
4677 }
4678 case Intrinsic::ctpop: {
4679 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004680 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004681 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4682 setValue(&I, Res);
4683 if (DisableScheduling)
4684 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004685 return 0;
4686 }
4687 case Intrinsic::stacksave: {
4688 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004689 Res = DAG.getNode(ISD::STACKSAVE, dl,
4690 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4691 setValue(&I, Res);
4692 DAG.setRoot(Res.getValue(1));
4693 if (DisableScheduling)
4694 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004695 return 0;
4696 }
4697 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004698 Res = getValue(I.getOperand(1));
4699 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4700 DAG.setRoot(Res);
4701 if (DisableScheduling)
4702 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004703 return 0;
4704 }
Bill Wendling57344502008-11-18 11:01:33 +00004705 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004706 // Emit code into the DAG to store the stack guard onto the stack.
4707 MachineFunction &MF = DAG.getMachineFunction();
4708 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004709 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004710
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004711 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4712 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004713
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004714 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004715 MFI->setStackProtectorIndex(FI);
4716
4717 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4718
4719 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004720 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4721 PseudoSourceValue::getFixedStack(FI),
4722 0, true);
4723 setValue(&I, Res);
4724 DAG.setRoot(Res);
4725 if (DisableScheduling)
4726 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004727 return 0;
4728 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004729 case Intrinsic::objectsize: {
4730 // If we don't know by now, we're never going to know.
4731 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4732
4733 assert(CI && "Non-constant type in __builtin_object_size?");
4734
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004735 SDValue Arg = getValue(I.getOperand(0));
4736 EVT Ty = Arg.getValueType();
4737
Eric Christopherd060b252009-12-23 02:51:48 +00004738 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004739 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004740 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004741 Res = DAG.getConstant(0, Ty);
4742
4743 setValue(&I, Res);
4744 if (DisableScheduling)
4745 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004746 return 0;
4747 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004748 case Intrinsic::var_annotation:
4749 // Discard annotate attributes
4750 return 0;
4751
4752 case Intrinsic::init_trampoline: {
4753 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4754
4755 SDValue Ops[6];
4756 Ops[0] = getRoot();
4757 Ops[1] = getValue(I.getOperand(1));
4758 Ops[2] = getValue(I.getOperand(2));
4759 Ops[3] = getValue(I.getOperand(3));
4760 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4761 Ops[5] = DAG.getSrcValue(F);
4762
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004763 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4764 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4765 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004766
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004767 setValue(&I, Res);
4768 DAG.setRoot(Res.getValue(1));
4769 if (DisableScheduling)
4770 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004771 return 0;
4772 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004773 case Intrinsic::gcroot:
4774 if (GFI) {
4775 Value *Alloca = I.getOperand(1);
4776 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004777
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004778 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4779 GFI->addStackRoot(FI->getIndex(), TypeMap);
4780 }
4781 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004782 case Intrinsic::gcread:
4783 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004784 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004785 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004786 case Intrinsic::flt_rounds:
4787 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4788 setValue(&I, Res);
4789 if (DisableScheduling)
4790 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004791 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004792 case Intrinsic::trap:
4793 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4794 DAG.setRoot(Res);
4795 if (DisableScheduling)
4796 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004797 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004798 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004799 return implVisitAluOverflow(I, ISD::UADDO);
4800 case Intrinsic::sadd_with_overflow:
4801 return implVisitAluOverflow(I, ISD::SADDO);
4802 case Intrinsic::usub_with_overflow:
4803 return implVisitAluOverflow(I, ISD::USUBO);
4804 case Intrinsic::ssub_with_overflow:
4805 return implVisitAluOverflow(I, ISD::SSUBO);
4806 case Intrinsic::umul_with_overflow:
4807 return implVisitAluOverflow(I, ISD::UMULO);
4808 case Intrinsic::smul_with_overflow:
4809 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004810
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004811 case Intrinsic::prefetch: {
4812 SDValue Ops[4];
4813 Ops[0] = getRoot();
4814 Ops[1] = getValue(I.getOperand(1));
4815 Ops[2] = getValue(I.getOperand(2));
4816 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004817 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4818 DAG.setRoot(Res);
4819 if (DisableScheduling)
4820 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004821 return 0;
4822 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004823
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004824 case Intrinsic::memory_barrier: {
4825 SDValue Ops[6];
4826 Ops[0] = getRoot();
4827 for (int x = 1; x < 6; ++x)
4828 Ops[x] = getValue(I.getOperand(x));
4829
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004830 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4831 DAG.setRoot(Res);
4832 if (DisableScheduling)
4833 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004834 return 0;
4835 }
4836 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004837 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004838 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004839 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004840 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4841 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004842 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004843 getValue(I.getOperand(2)),
4844 getValue(I.getOperand(3)),
4845 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004846 setValue(&I, L);
4847 DAG.setRoot(L.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004848 if (DisableScheduling)
4849 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004850 return 0;
4851 }
4852 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004853 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004855 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004856 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004857 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004858 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004859 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004860 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004861 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004862 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004863 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004864 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004865 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004866 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004867 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004868 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004869 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004870 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004871 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004872 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004873 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004874
4875 case Intrinsic::invariant_start:
4876 case Intrinsic::lifetime_start:
4877 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004878 Res = DAG.getUNDEF(TLI.getPointerTy());
4879 setValue(&I, Res);
4880 if (DisableScheduling)
4881 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004882 return 0;
4883 case Intrinsic::invariant_end:
4884 case Intrinsic::lifetime_end:
4885 // Discard region information.
4886 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004887 }
4888}
4889
Dan Gohman98ca4f22009-08-05 01:29:28 +00004890/// Test if the given instruction is in a position to be optimized
4891/// with a tail-call. This roughly means that it's in a block with
4892/// a return and there's nothing that needs to be scheduled
4893/// between it and the return.
4894///
4895/// This function only tests target-independent requirements.
4896/// For target-dependent requirements, a target should override
4897/// TargetLowering::IsEligibleForTailCallOptimization.
4898///
4899static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004900isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004901 const TargetLowering &TLI) {
4902 const BasicBlock *ExitBB = I->getParent();
4903 const TerminatorInst *Term = ExitBB->getTerminator();
4904 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4905 const Function *F = ExitBB->getParent();
4906
4907 // The block must end in a return statement or an unreachable.
4908 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4909
4910 // If I will have a chain, make sure no other instruction that will have a
4911 // chain interposes between I and the return.
4912 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4913 !I->isSafeToSpeculativelyExecute())
4914 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4915 --BBI) {
4916 if (&*BBI == I)
4917 break;
4918 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4919 !BBI->isSafeToSpeculativelyExecute())
4920 return false;
4921 }
4922
4923 // If the block ends with a void return or unreachable, it doesn't matter
4924 // what the call's return type is.
4925 if (!Ret || Ret->getNumOperands() == 0) return true;
4926
Dan Gohmaned9bab32009-11-14 02:06:30 +00004927 // If the return value is undef, it doesn't matter what the call's
4928 // return type is.
4929 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4930
Dan Gohman98ca4f22009-08-05 01:29:28 +00004931 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004932 // the return. Ignore noalias because it doesn't affect the call sequence.
4933 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4934 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004935 return false;
4936
4937 // Otherwise, make sure the unmodified return value of I is the return value.
4938 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4939 U = dyn_cast<Instruction>(U->getOperand(0))) {
4940 if (!U)
4941 return false;
4942 if (!U->hasOneUse())
4943 return false;
4944 if (U == I)
4945 break;
4946 // Check for a truly no-op truncate.
4947 if (isa<TruncInst>(U) &&
4948 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4949 continue;
4950 // Check for a truly no-op bitcast.
4951 if (isa<BitCastInst>(U) &&
4952 (U->getOperand(0)->getType() == U->getType() ||
4953 (isa<PointerType>(U->getOperand(0)->getType()) &&
4954 isa<PointerType>(U->getType()))))
4955 continue;
4956 // Otherwise it's not a true no-op.
4957 return false;
4958 }
4959
4960 return true;
4961}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004962
Dan Gohman2048b852009-11-23 18:04:58 +00004963void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4964 bool isTailCall,
4965 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004966 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4967 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004968 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004969 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4970 unsigned BeginLabel = 0, EndLabel = 0;
4971
4972 TargetLowering::ArgListTy Args;
4973 TargetLowering::ArgListEntry Entry;
4974 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004975
4976 // Check whether the function can return without sret-demotion.
4977 SmallVector<EVT, 4> OutVTs;
4978 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4979 SmallVector<uint64_t, 4> Offsets;
4980 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004981 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004982
4983 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4984 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4985
4986 SDValue DemoteStackSlot;
4987
4988 if (!CanLowerReturn) {
4989 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4990 FTy->getReturnType());
4991 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4992 FTy->getReturnType());
4993 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004994 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004995 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4996
4997 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4998 Entry.Node = DemoteStackSlot;
4999 Entry.Ty = StackSlotPtrType;
5000 Entry.isSExt = false;
5001 Entry.isZExt = false;
5002 Entry.isInReg = false;
5003 Entry.isSRet = true;
5004 Entry.isNest = false;
5005 Entry.isByVal = false;
5006 Entry.Alignment = Align;
5007 Args.push_back(Entry);
5008 RetTy = Type::getVoidTy(FTy->getContext());
5009 }
5010
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005011 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005012 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005013 SDValue ArgNode = getValue(*i);
5014 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
5015
5016 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00005017 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5018 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5019 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5020 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5021 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5022 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005023 Entry.Alignment = CS.getParamAlignment(attrInd);
5024 Args.push_back(Entry);
5025 }
5026
5027 if (LandingPad && MMI) {
5028 // Insert a label before the invoke call to mark the try range. This can be
5029 // used to detect deletion of the invoke via the MachineModuleInfo.
5030 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005031
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005032 // Both PendingLoads and PendingExports must be flushed here;
5033 // this call might not return.
5034 (void)getRoot();
Bill Wendling0d580132009-12-23 01:28:19 +00005035 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5036 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005037 }
5038
Dan Gohman98ca4f22009-08-05 01:29:28 +00005039 // Check if target-independent constraints permit a tail call here.
5040 // Target-dependent constraints are checked within TLI.LowerCallTo.
5041 if (isTailCall &&
5042 !isInTailCallPosition(CS.getInstruction(),
5043 CS.getAttributes().getRetAttributes(),
5044 TLI))
5045 isTailCall = false;
5046
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005047 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005048 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00005049 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005050 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005051 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005052 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00005053 isTailCall,
5054 !CS.getInstruction()->use_empty(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005055 Callee, Args, DAG, getCurDebugLoc(), SDNodeOrder);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005056 assert((isTailCall || Result.second.getNode()) &&
5057 "Non-null chain expected with non-tail call!");
5058 assert((Result.second.getNode() || !Result.first.getNode()) &&
5059 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005060 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005061 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005062 if (DisableScheduling)
5063 DAG.AssignOrdering(Result.first.getNode(), SDNodeOrder);
5064 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005065 // The instruction result is the result of loading from the
5066 // hidden sret parameter.
5067 SmallVector<EVT, 1> PVTs;
5068 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
5069
5070 ComputeValueVTs(TLI, PtrRetTy, PVTs);
5071 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5072 EVT PtrVT = PVTs[0];
5073 unsigned NumValues = OutVTs.size();
5074 SmallVector<SDValue, 4> Values(NumValues);
5075 SmallVector<SDValue, 4> Chains(NumValues);
5076
5077 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00005078 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
5079 DemoteStackSlot,
5080 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005081 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005082 Add, NULL, Offsets[i], false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005083 Values[i] = L;
5084 Chains[i] = L.getValue(1);
5085 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005086
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005087 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
5088 MVT::Other, &Chains[0], NumValues);
5089 PendingLoads.push_back(Chain);
5090
Bill Wendlinge80ae832009-12-22 00:50:32 +00005091 SDValue MV = DAG.getNode(ISD::MERGE_VALUES,
5092 getCurDebugLoc(),
5093 DAG.getVTList(&OutVTs[0], NumValues),
5094 &Values[0], NumValues);
5095 setValue(CS.getInstruction(), MV);
5096
5097 if (DisableScheduling) {
5098 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
5099 DAG.AssignOrdering(MV.getNode(), SDNodeOrder);
5100 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005101 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005102
5103 // As a special case, a null chain means that a tail call has been emitted and
5104 // the DAG root is already updated.
5105 if (Result.second.getNode()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005106 DAG.setRoot(Result.second);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005107 if (DisableScheduling)
5108 DAG.AssignOrdering(Result.second.getNode(), SDNodeOrder);
5109 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005110 HasTailCall = true;
Bill Wendlinge80ae832009-12-22 00:50:32 +00005111 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005112
5113 if (LandingPad && MMI) {
5114 // Insert a label at the end of the invoke call to mark the try range. This
5115 // can be used to detect deletion of the invoke via the MachineModuleInfo.
5116 EndLabel = MMI->NextLabelID();
Bill Wendling0d580132009-12-23 01:28:19 +00005117 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5118 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005119
5120 // Inform MachineModuleInfo of range.
5121 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
5122 }
5123}
5124
Chris Lattner8047d9a2009-12-24 00:37:38 +00005125/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5126/// value is equal or not-equal to zero.
5127static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
5128 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
5129 UI != E; ++UI) {
5130 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
5131 if (IC->isEquality())
5132 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
5133 if (C->isNullValue())
5134 continue;
5135 // Unknown instruction.
5136 return false;
5137 }
5138 return true;
5139}
5140
Chris Lattner04b091a2009-12-24 01:07:17 +00005141static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005142 SelectionDAGBuilder &Builder) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005143
5144 // Check to see if this load can be trivially constant folded, e.g. if the
5145 // input is from a string literal.
5146 if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
5147 // Cast pointer to the type we really want to load.
5148 LoadInput = ConstantExpr::getBitCast(LoadInput,
5149 PointerType::getUnqual(LoadTy));
5150
5151 if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD))
5152 return Builder.getValue(LoadCst);
5153 }
5154
5155 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5156 // still constant memory, the input chain can be the entry node.
5157 SDValue Root;
5158 bool ConstantMemory = false;
5159
5160 // Do not serialize (non-volatile) loads of constant memory with anything.
5161 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5162 Root = Builder.DAG.getEntryNode();
5163 ConstantMemory = true;
5164 } else {
5165 // Do not serialize non-volatile loads against each other.
5166 Root = Builder.DAG.getRoot();
5167 }
5168
5169 SDValue Ptr = Builder.getValue(PtrVal);
5170 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
5171 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
5172 false /*volatile*/, 1 /* align=1 */);
5173
5174 if (!ConstantMemory)
5175 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5176 return LoadVal;
5177}
5178
5179
5180/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5181/// If so, return true and lower it, otherwise return false and it will be
5182/// lowered like a normal call.
5183bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) {
5184 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
5185 if (I.getNumOperands() != 4)
5186 return false;
5187
5188 Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
5189 if (!isa<PointerType>(LHS->getType()) || !isa<PointerType>(RHS->getType()) ||
5190 !isa<IntegerType>(I.getOperand(3)->getType()) ||
5191 !isa<IntegerType>(I.getType()))
5192 return false;
5193
5194 ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
5195
5196 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5197 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005198 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5199 bool ActuallyDoIt = true;
5200 MVT LoadVT;
5201 const Type *LoadTy;
5202 switch (Size->getZExtValue()) {
5203 default:
5204 LoadVT = MVT::Other;
5205 LoadTy = 0;
5206 ActuallyDoIt = false;
5207 break;
5208 case 2:
5209 LoadVT = MVT::i16;
5210 LoadTy = Type::getInt16Ty(Size->getContext());
5211 break;
5212 case 4:
5213 LoadVT = MVT::i32;
5214 LoadTy = Type::getInt32Ty(Size->getContext());
5215 break;
5216 case 8:
5217 LoadVT = MVT::i64;
5218 LoadTy = Type::getInt64Ty(Size->getContext());
5219 break;
5220 /*
5221 case 16:
5222 LoadVT = MVT::v4i32;
5223 LoadTy = Type::getInt32Ty(Size->getContext());
5224 LoadTy = VectorType::get(LoadTy, 4);
5225 break;
5226 */
5227 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005228
Chris Lattner04b091a2009-12-24 01:07:17 +00005229 // This turns into unaligned loads. We only do this if the target natively
5230 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5231 // we'll only produce a small number of byte loads.
5232
5233 // Require that we can find a legal MVT, and only do this if the target
5234 // supports unaligned loads of that type. Expanding into byte loads would
5235 // bloat the code.
5236 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5237 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5238 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5239 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5240 ActuallyDoIt = false;
5241 }
5242
5243 if (ActuallyDoIt) {
5244 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5245 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
5246
5247 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
5248 ISD::SETNE);
5249 EVT CallVT = TLI.getValueType(I.getType(), true);
5250 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
5251 return true;
5252 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005253 }
5254
5255
5256 return false;
5257}
5258
5259
Dan Gohman2048b852009-11-23 18:04:58 +00005260void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005261 const char *RenameFn = 0;
5262 if (Function *F = I.getCalledFunction()) {
5263 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005264 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
5265 if (II) {
5266 if (unsigned IID = II->getIntrinsicID(F)) {
5267 RenameFn = visitIntrinsicCall(I, IID);
5268 if (!RenameFn)
5269 return;
5270 }
5271 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005272 if (unsigned IID = F->getIntrinsicID()) {
5273 RenameFn = visitIntrinsicCall(I, IID);
5274 if (!RenameFn)
5275 return;
5276 }
5277 }
5278
5279 // Check for well-known libc/libm calls. If the function is internal, it
5280 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005281 if (!F->hasLocalLinkage() && F->hasName()) {
5282 StringRef Name = F->getName();
5283 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005284 if (I.getNumOperands() == 3 && // Basic sanity checks.
5285 I.getOperand(1)->getType()->isFloatingPoint() &&
5286 I.getType() == I.getOperand(1)->getType() &&
5287 I.getType() == I.getOperand(2)->getType()) {
5288 SDValue LHS = getValue(I.getOperand(1));
5289 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00005290 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5291 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005292 return;
5293 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005294 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005295 if (I.getNumOperands() == 2 && // Basic sanity checks.
5296 I.getOperand(1)->getType()->isFloatingPoint() &&
5297 I.getType() == I.getOperand(1)->getType()) {
5298 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005299 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
5300 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005301 return;
5302 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005303 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005304 if (I.getNumOperands() == 2 && // Basic sanity checks.
5305 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005306 I.getType() == I.getOperand(1)->getType() &&
5307 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005308 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005309 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5310 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005311 return;
5312 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005313 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005314 if (I.getNumOperands() == 2 && // Basic sanity checks.
5315 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005316 I.getType() == I.getOperand(1)->getType() &&
5317 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005318 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005319 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5320 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005321 return;
5322 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005323 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5324 if (I.getNumOperands() == 2 && // Basic sanity checks.
5325 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005326 I.getType() == I.getOperand(1)->getType() &&
5327 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005328 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005329 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5330 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005331 return;
5332 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005333 } else if (Name == "memcmp") {
5334 if (visitMemCmpCall(I))
5335 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 }
5337 }
5338 } else if (isa<InlineAsm>(I.getOperand(0))) {
5339 visitInlineAsm(&I);
5340 return;
5341 }
5342
5343 SDValue Callee;
5344 if (!RenameFn)
5345 Callee = getValue(I.getOperand(0));
5346 else
Bill Wendling056292f2008-09-16 21:48:12 +00005347 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005348
Bill Wendling0d580132009-12-23 01:28:19 +00005349 // Check if we can potentially perform a tail call. More detailed checking is
5350 // be done within LowerCallTo, after more information about the call is known.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005351 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5352
5353 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005354}
5355
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005356/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005357/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005358/// Chain/Flag as the input and updates them for the output Chain/Flag.
5359/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005360SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005361 unsigned Order, SDValue &Chain,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005362 SDValue *Flag) const {
5363 // Assemble the legal parts into the final values.
5364 SmallVector<SDValue, 4> Values(ValueVTs.size());
5365 SmallVector<SDValue, 8> Parts;
5366 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5367 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005368 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005369 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005370 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005371
5372 Parts.resize(NumRegs);
5373 for (unsigned i = 0; i != NumRegs; ++i) {
5374 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00005375 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005376 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005377 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005378 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005379 *Flag = P.getValue(2);
5380 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005381
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005382 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005383
Bill Wendlingec72e322009-12-22 01:11:43 +00005384 if (DisableScheduling)
5385 DAG.AssignOrdering(P.getNode(), Order);
5386
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005387 // If the source register was virtual and if we know something about it,
5388 // add an assert node.
5389 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5390 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5391 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5392 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5393 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5394 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005395
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005396 unsigned RegSize = RegisterVT.getSizeInBits();
5397 unsigned NumSignBits = LOI.NumSignBits;
5398 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005399
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400 // FIXME: We capture more information than the dag can represent. For
5401 // now, just use the tightest assertzext/assertsext possible.
5402 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005403 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005404 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005405 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005406 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005407 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005408 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005409 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005410 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005411 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005412 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005413 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005414 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005416 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005417 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005418 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005419 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005420
Owen Anderson825b72b2009-08-11 20:47:22 +00005421 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005422 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005423 RegisterVT, P, DAG.getValueType(FromVT));
5424
Bill Wendlingec72e322009-12-22 01:11:43 +00005425 if (DisableScheduling)
5426 DAG.AssignOrdering(P.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005427 }
5428 }
5429 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005430
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005431 Parts[i] = P;
5432 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005433
Bill Wendling3ea3c242009-12-22 02:10:19 +00005434 Values[Value] = getCopyFromParts(DAG, dl, Order, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005435 NumRegs, RegisterVT, ValueVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005436 if (DisableScheduling)
5437 DAG.AssignOrdering(Values[Value].getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005438 Part += NumRegs;
5439 Parts.clear();
5440 }
5441
Bill Wendlingec72e322009-12-22 01:11:43 +00005442 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5443 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5444 &Values[0], ValueVTs.size());
5445 if (DisableScheduling)
5446 DAG.AssignOrdering(Res.getNode(), Order);
5447 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005448}
5449
5450/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005451/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005452/// Chain/Flag as the input and updates them for the output Chain/Flag.
5453/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005454void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005455 unsigned Order, SDValue &Chain,
5456 SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005457 // Get the list of the values's legal parts.
5458 unsigned NumRegs = Regs.size();
5459 SmallVector<SDValue, 8> Parts(NumRegs);
5460 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005461 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005462 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005463 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005464
Bill Wendling3ea3c242009-12-22 02:10:19 +00005465 getCopyToParts(DAG, dl, Order,
5466 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005467 &Parts[Part], NumParts, RegisterVT);
5468 Part += NumParts;
5469 }
5470
5471 // Copy the parts into the registers.
5472 SmallVector<SDValue, 8> Chains(NumRegs);
5473 for (unsigned i = 0; i != NumRegs; ++i) {
5474 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00005475 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005476 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00005477 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005478 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005479 *Flag = Part.getValue(1);
5480 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005481
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005482 Chains[i] = Part.getValue(0);
Bill Wendlingec72e322009-12-22 01:11:43 +00005483
5484 if (DisableScheduling)
5485 DAG.AssignOrdering(Part.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005486 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005487
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005488 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005489 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005490 // flagged to it. That is the CopyToReg nodes and the user are considered
5491 // a single scheduling unit. If we create a TokenFactor and return it as
5492 // chain, then the TokenFactor is both a predecessor (operand) of the
5493 // user as well as a successor (the TF operands are flagged to the user).
5494 // c1, f1 = CopyToReg
5495 // c2, f2 = CopyToReg
5496 // c3 = TokenFactor c1, c2
5497 // ...
5498 // = op c3, ..., f2
5499 Chain = Chains[NumRegs-1];
5500 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005501 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Bill Wendlingec72e322009-12-22 01:11:43 +00005502
5503 if (DisableScheduling)
5504 DAG.AssignOrdering(Chain.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505}
5506
5507/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005508/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005509/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005510void RegsForValue::AddInlineAsmOperands(unsigned Code,
5511 bool HasMatching,unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +00005512 SelectionDAG &DAG, unsigned Order,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005513 std::vector<SDValue> &Ops) const {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005514 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5515 unsigned Flag = Code | (Regs.size() << 3);
5516 if (HasMatching)
5517 Flag |= 0x80000000 | (MatchingIdx << 16);
Dale Johannesen99499332009-12-23 07:32:51 +00005518 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00005519 Ops.push_back(Res);
5520
5521 if (DisableScheduling)
5522 DAG.AssignOrdering(Res.getNode(), Order);
5523
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005524 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005525 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005526 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005527 for (unsigned i = 0; i != NumRegs; ++i) {
5528 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling651ad132009-12-22 01:25:10 +00005529 SDValue Res = DAG.getRegister(Regs[Reg++], RegisterVT);
5530 Ops.push_back(Res);
5531
5532 if (DisableScheduling)
5533 DAG.AssignOrdering(Res.getNode(), Order);
Chris Lattner58f15c42008-10-17 16:21:11 +00005534 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005535 }
5536}
5537
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005538/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005539/// i.e. it isn't a stack pointer or some other special register, return the
5540/// register class for the register. Otherwise, return null.
5541static const TargetRegisterClass *
5542isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5543 const TargetLowering &TLI,
5544 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005545 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005546 const TargetRegisterClass *FoundRC = 0;
5547 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5548 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005550
5551 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005552 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005553 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5554 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5555 I != E; ++I) {
5556 if (TLI.isTypeLegal(*I)) {
5557 // If we have already found this register in a different register class,
5558 // choose the one with the largest VT specified. For example, on
5559 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005560 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005561 ThisVT = *I;
5562 break;
5563 }
5564 }
5565 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005566
Owen Anderson825b72b2009-08-11 20:47:22 +00005567 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005568
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005569 // NOTE: This isn't ideal. In particular, this might allocate the
5570 // frame pointer in functions that need it (due to them not being taken
5571 // out of allocation, because a variable sized allocation hasn't been seen
5572 // yet). This is a slight code pessimization, but should still work.
5573 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5574 E = RC->allocation_order_end(MF); I != E; ++I)
5575 if (*I == Reg) {
5576 // We found a matching register class. Keep looking at others in case
5577 // we find one with larger registers that this physreg is also in.
5578 FoundRC = RC;
5579 FoundVT = ThisVT;
5580 break;
5581 }
5582 }
5583 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005584}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005585
5586
5587namespace llvm {
5588/// AsmOperandInfo - This contains information for each constraint that we are
5589/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005590class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005591 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005592public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005593 /// CallOperand - If this is the result output operand or a clobber
5594 /// this is null, otherwise it is the incoming operand to the CallInst.
5595 /// This gets modified as the asm is processed.
5596 SDValue CallOperand;
5597
5598 /// AssignedRegs - If this is a register or register class operand, this
5599 /// contains the set of register corresponding to the operand.
5600 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005601
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005602 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5603 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5604 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005605
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005606 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5607 /// busy in OutputRegs/InputRegs.
5608 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005609 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005610 std::set<unsigned> &InputRegs,
5611 const TargetRegisterInfo &TRI) const {
5612 if (isOutReg) {
5613 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5614 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5615 }
5616 if (isInReg) {
5617 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5618 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5619 }
5620 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005621
Owen Andersone50ed302009-08-10 22:56:29 +00005622 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005623 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005624 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00005625 EVT getCallOperandValEVT(LLVMContext &Context,
5626 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005627 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005628 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005629
Chris Lattner81249c92008-10-17 17:05:25 +00005630 if (isa<BasicBlock>(CallOperandVal))
5631 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005632
Chris Lattner81249c92008-10-17 17:05:25 +00005633 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005634
Chris Lattner81249c92008-10-17 17:05:25 +00005635 // If this is an indirect operand, the operand is a pointer to the
5636 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005637 if (isIndirect) {
5638 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5639 if (!PtrTy)
5640 llvm_report_error("Indirect operand for inline asm not a pointer!");
5641 OpTy = PtrTy->getElementType();
5642 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005643
Chris Lattner81249c92008-10-17 17:05:25 +00005644 // If OpTy is not a single value, it may be a struct/union that we
5645 // can tile with integers.
5646 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5647 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5648 switch (BitSize) {
5649 default: break;
5650 case 1:
5651 case 8:
5652 case 16:
5653 case 32:
5654 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005655 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005656 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005657 break;
5658 }
5659 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005660
Chris Lattner81249c92008-10-17 17:05:25 +00005661 return TLI.getValueType(OpTy, true);
5662 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005664private:
5665 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5666 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005667 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005668 const TargetRegisterInfo &TRI) {
5669 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5670 Regs.insert(Reg);
5671 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5672 for (; *Aliases; ++Aliases)
5673 Regs.insert(*Aliases);
5674 }
5675};
5676} // end llvm namespace.
5677
5678
5679/// GetRegistersForValue - Assign registers (virtual or physical) for the
5680/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005681/// register allocator to handle the assignment process. However, if the asm
5682/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005683/// allocation. This produces generally horrible, but correct, code.
5684///
5685/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005686/// Input and OutputRegs are the set of already allocated physical registers.
5687///
Dan Gohman2048b852009-11-23 18:04:58 +00005688void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005689GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005690 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005691 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005692 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005693
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005694 // Compute whether this value requires an input register, an output register,
5695 // or both.
5696 bool isOutReg = false;
5697 bool isInReg = false;
5698 switch (OpInfo.Type) {
5699 case InlineAsm::isOutput:
5700 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005701
5702 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005703 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005704 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005705 break;
5706 case InlineAsm::isInput:
5707 isInReg = true;
5708 isOutReg = false;
5709 break;
5710 case InlineAsm::isClobber:
5711 isOutReg = true;
5712 isInReg = true;
5713 break;
5714 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005715
5716
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005717 MachineFunction &MF = DAG.getMachineFunction();
5718 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005719
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005720 // If this is a constraint for a single physreg, or a constraint for a
5721 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005722 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005723 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5724 OpInfo.ConstraintVT);
5725
5726 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005727 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005728 // If this is a FP input in an integer register (or visa versa) insert a bit
5729 // cast of the input value. More generally, handle any case where the input
5730 // value disagrees with the register class we plan to stick this in.
5731 if (OpInfo.Type == InlineAsm::isInput &&
5732 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005733 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005734 // types are identical size, use a bitcast to convert (e.g. two differing
5735 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005736 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005737 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005738 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005739 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005740 OpInfo.ConstraintVT = RegVT;
5741 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5742 // If the input is a FP value and we want it in FP registers, do a
5743 // bitcast to the corresponding integer type. This turns an f64 value
5744 // into i64, which can be passed with two i32 values on a 32-bit
5745 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005746 RegVT = EVT::getIntegerVT(Context,
5747 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005748 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005749 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005750 OpInfo.ConstraintVT = RegVT;
5751 }
Bill Wendling651ad132009-12-22 01:25:10 +00005752
5753 if (DisableScheduling)
5754 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Chris Lattner01426e12008-10-21 00:45:36 +00005755 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005756
Owen Anderson23b9b192009-08-12 00:36:31 +00005757 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005758 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005759
Owen Andersone50ed302009-08-10 22:56:29 +00005760 EVT RegVT;
5761 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005762
5763 // If this is a constraint for a specific physical register, like {r17},
5764 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005765 if (unsigned AssignedReg = PhysReg.first) {
5766 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005767 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005768 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005769
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005770 // Get the actual register value type. This is important, because the user
5771 // may have asked for (e.g.) the AX register in i32 type. We need to
5772 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005773 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005774
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005775 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005776 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005777
5778 // If this is an expanded reference, add the rest of the regs to Regs.
5779 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005780 TargetRegisterClass::iterator I = RC->begin();
5781 for (; *I != AssignedReg; ++I)
5782 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005783
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005784 // Already added the first reg.
5785 --NumRegs; ++I;
5786 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005787 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005788 Regs.push_back(*I);
5789 }
5790 }
Bill Wendling651ad132009-12-22 01:25:10 +00005791
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005792 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5793 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5794 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5795 return;
5796 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005797
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005798 // Otherwise, if this was a reference to an LLVM register class, create vregs
5799 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005800 if (const TargetRegisterClass *RC = PhysReg.second) {
5801 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005802 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005803 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005804
Evan Chengfb112882009-03-23 08:01:15 +00005805 // Create the appropriate number of virtual registers.
5806 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5807 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005808 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005809
Evan Chengfb112882009-03-23 08:01:15 +00005810 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5811 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005812 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005813
5814 // This is a reference to a register class that doesn't directly correspond
5815 // to an LLVM register class. Allocate NumRegs consecutive, available,
5816 // registers from the class.
5817 std::vector<unsigned> RegClassRegs
5818 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5819 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005821 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5822 unsigned NumAllocated = 0;
5823 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5824 unsigned Reg = RegClassRegs[i];
5825 // See if this register is available.
5826 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5827 (isInReg && InputRegs.count(Reg))) { // Already used.
5828 // Make sure we find consecutive registers.
5829 NumAllocated = 0;
5830 continue;
5831 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005832
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005833 // Check to see if this register is allocatable (i.e. don't give out the
5834 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005835 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5836 if (!RC) { // Couldn't allocate this register.
5837 // Reset NumAllocated to make sure we return consecutive registers.
5838 NumAllocated = 0;
5839 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005840 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005841
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005842 // Okay, this register is good, we can use it.
5843 ++NumAllocated;
5844
5845 // If we allocated enough consecutive registers, succeed.
5846 if (NumAllocated == NumRegs) {
5847 unsigned RegStart = (i-NumAllocated)+1;
5848 unsigned RegEnd = i+1;
5849 // Mark all of the allocated registers used.
5850 for (unsigned i = RegStart; i != RegEnd; ++i)
5851 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005852
5853 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005854 OpInfo.ConstraintVT);
5855 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5856 return;
5857 }
5858 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005859
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005860 // Otherwise, we couldn't allocate enough registers for this.
5861}
5862
Evan Chengda43bcf2008-09-24 00:05:32 +00005863/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5864/// processed uses a memory 'm' constraint.
5865static bool
5866hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005867 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005868 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5869 InlineAsm::ConstraintInfo &CI = CInfos[i];
5870 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5871 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5872 if (CType == TargetLowering::C_Memory)
5873 return true;
5874 }
Chris Lattner6c147292009-04-30 00:48:50 +00005875
5876 // Indirect operand accesses access memory.
5877 if (CI.isIndirect)
5878 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005879 }
5880
5881 return false;
5882}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005883
5884/// visitInlineAsm - Handle a call to an InlineAsm object.
5885///
Dan Gohman2048b852009-11-23 18:04:58 +00005886void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005887 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5888
5889 /// ConstraintOperands - Information about all of the constraints.
5890 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005891
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005892 std::set<unsigned> OutputRegs, InputRegs;
5893
5894 // Do a prepass over the constraints, canonicalizing them, and building up the
5895 // ConstraintOperands list.
5896 std::vector<InlineAsm::ConstraintInfo>
5897 ConstraintInfos = IA->ParseConstraints();
5898
Evan Chengda43bcf2008-09-24 00:05:32 +00005899 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005900
5901 SDValue Chain, Flag;
5902
5903 // We won't need to flush pending loads if this asm doesn't touch
5904 // memory and is nonvolatile.
5905 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005906 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005907 else
5908 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005910 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5911 unsigned ResNo = 0; // ResNo - The result number of the next output.
5912 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5913 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5914 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005915
Owen Anderson825b72b2009-08-11 20:47:22 +00005916 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005917
5918 // Compute the value type for each operand.
5919 switch (OpInfo.Type) {
5920 case InlineAsm::isOutput:
5921 // Indirect outputs just consume an argument.
5922 if (OpInfo.isIndirect) {
5923 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5924 break;
5925 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005926
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005927 // The return value of the call is this value. As such, there is no
5928 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005929 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5930 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005931 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5932 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5933 } else {
5934 assert(ResNo == 0 && "Asm only has one result!");
5935 OpVT = TLI.getValueType(CS.getType());
5936 }
5937 ++ResNo;
5938 break;
5939 case InlineAsm::isInput:
5940 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5941 break;
5942 case InlineAsm::isClobber:
5943 // Nothing to do.
5944 break;
5945 }
5946
5947 // If this is an input or an indirect output, process the call argument.
5948 // BasicBlocks are labels, currently appearing only in asm's.
5949 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005950 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005951 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5952
Chris Lattner81249c92008-10-17 17:05:25 +00005953 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005954 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005955 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005956 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005957 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005958
Owen Anderson1d0be152009-08-13 21:58:54 +00005959 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005960 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005961
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005962 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005963 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005964
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005965 // Second pass over the constraints: compute which constraint option to use
5966 // and assign registers to constraints that want a specific physreg.
5967 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5968 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005969
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005970 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005971 // matching input. If their types mismatch, e.g. one is an integer, the
5972 // other is floating point, or their sizes are different, flag it as an
5973 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005974 if (OpInfo.hasMatchingInput()) {
5975 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5976 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005977 if ((OpInfo.ConstraintVT.isInteger() !=
5978 Input.ConstraintVT.isInteger()) ||
5979 (OpInfo.ConstraintVT.getSizeInBits() !=
5980 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005981 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005982 " with a matching output constraint of incompatible"
5983 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005984 }
5985 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005986 }
5987 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005988
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005989 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005990 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005992 // If this is a memory input, and if the operand is not indirect, do what we
5993 // need to to provide an address for the memory input.
5994 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5995 !OpInfo.isIndirect) {
5996 assert(OpInfo.Type == InlineAsm::isInput &&
5997 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005998
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005999 // Memory operands really want the address of the value. If we don't have
6000 // an indirect input, put it in the constpool if we can, otherwise spill
6001 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003 // If the operand is a float, integer, or vector constant, spill to a
6004 // constant pool entry to get its address.
6005 Value *OpVal = OpInfo.CallOperandVal;
6006 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
6007 isa<ConstantVector>(OpVal)) {
6008 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
6009 TLI.getPointerTy());
6010 } else {
6011 // Otherwise, create a stack slot and emit a store to it before the
6012 // asm.
6013 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00006014 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006015 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
6016 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006017 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006018 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00006019 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006020 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006021 OpInfo.CallOperand = StackSlot;
6022 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006023
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006024 // There is no longer a Value* corresponding to this operand.
6025 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006026
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006027 // It is now an indirect operand.
6028 OpInfo.isIndirect = true;
6029 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006031 // If this constraint is for a specific register, allocate it before
6032 // anything else.
6033 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00006034 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006035 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006036
Bill Wendling651ad132009-12-22 01:25:10 +00006037 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006038
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006039 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006040 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006041 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6042 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006043
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006044 // C_Register operands have already been allocated, Other/Memory don't need
6045 // to be.
6046 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00006047 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006048 }
6049
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006050 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6051 std::vector<SDValue> AsmNodeOperands;
6052 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6053 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00006054 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006055
6056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006057 // Loop over all of the inputs, copying the operand values into the
6058 // appropriate registers and processing the output regs.
6059 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006060
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006061 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6062 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006063
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006064 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6065 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6066
6067 switch (OpInfo.Type) {
6068 case InlineAsm::isOutput: {
6069 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6070 OpInfo.ConstraintType != TargetLowering::C_Register) {
6071 // Memory output, or 'other' output (e.g. 'X' constraint).
6072 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6073
6074 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006075 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6076 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006077 TLI.getPointerTy()));
6078 AsmNodeOperands.push_back(OpInfo.CallOperand);
6079 break;
6080 }
6081
6082 // Otherwise, this is a register or register class output.
6083
6084 // Copy the output from the appropriate register. Find a register that
6085 // we can use.
6086 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006087 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006088 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006089 }
6090
6091 // If this is an indirect operand, store through the pointer after the
6092 // asm.
6093 if (OpInfo.isIndirect) {
6094 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6095 OpInfo.CallOperandVal));
6096 } else {
6097 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00006098 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
6099 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006100 // Concatenate this output onto the outputs list.
6101 RetValRegs.append(OpInfo.AssignedRegs);
6102 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006104 // Add information to the INLINEASM node to know that this register is
6105 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00006106 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
6107 6 /* EARLYCLOBBER REGDEF */ :
6108 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00006109 false,
6110 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006111 DAG, SDNodeOrder,
6112 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006113 break;
6114 }
6115 case InlineAsm::isInput: {
6116 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006117
Chris Lattner6bdcda32008-10-17 16:47:46 +00006118 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006119 // If this is required to match an output register we have already set,
6120 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006121 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006122
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006123 // Scan until we find the definition we already emitted of this operand.
6124 // When we find it, create a RegsForValue operand.
6125 unsigned CurOp = 2; // The first operand.
6126 for (; OperandNo; --OperandNo) {
6127 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006128 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006129 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006130 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
6131 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
6132 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006133 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006134 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006135 }
6136
Evan Cheng697cbbf2009-03-20 18:03:34 +00006137 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006138 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006139 if ((OpFlag & 7) == 2 /*REGDEF*/
6140 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
6141 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00006142 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006143 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00006144 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00006145 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006146 RegsForValue MatchedRegs;
6147 MatchedRegs.TLI = &TLI;
6148 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00006149 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006150 MatchedRegs.RegVTs.push_back(RegVT);
6151 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006152 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00006153 i != e; ++i)
6154 MatchedRegs.Regs.
6155 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006156
6157 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00006158 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006159 SDNodeOrder, Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00006160 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
6161 true, OpInfo.getMatchedOperand(),
Bill Wendling651ad132009-12-22 01:25:10 +00006162 DAG, SDNodeOrder, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006163 break;
6164 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006165 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
6166 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
6167 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006168 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00006169 // See InlineAsm.h isUseOperandTiedToDef.
6170 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00006171 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006172 TLI.getPointerTy()));
6173 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6174 break;
6175 }
6176 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006177
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006178 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006179 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006180 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006181
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006182 std::vector<SDValue> Ops;
6183 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00006184 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006185 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006186 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00006187 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006188 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006189
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006190 // Add information to the INLINEASM node to know about this input.
6191 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006192 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006193 TLI.getPointerTy()));
6194 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6195 break;
6196 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
6197 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
6198 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6199 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006200
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006201 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006202 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6203 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006204 TLI.getPointerTy()));
6205 AsmNodeOperands.push_back(InOperandVal);
6206 break;
6207 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006208
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006209 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6210 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6211 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006212 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006213 "Don't know how to handle indirect register inputs yet!");
6214
6215 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00006216 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006217 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006218 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00006219 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006220
Dale Johannesen66978ee2009-01-31 02:22:37 +00006221 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006222 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006223
Evan Cheng697cbbf2009-03-20 18:03:34 +00006224 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006225 DAG, SDNodeOrder,
6226 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006227 break;
6228 }
6229 case InlineAsm::isClobber: {
6230 // Add the clobbered value to the operand list, so that the register
6231 // allocator is aware that the physreg got clobbered.
6232 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00006233 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Bill Wendling651ad132009-12-22 01:25:10 +00006234 false, 0, DAG, SDNodeOrder,
6235 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006236 break;
6237 }
6238 }
6239 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006240
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006241 // Finish up input operands.
6242 AsmNodeOperands[0] = Chain;
6243 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006244
Dale Johannesen66978ee2009-01-31 02:22:37 +00006245 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006246 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006247 &AsmNodeOperands[0], AsmNodeOperands.size());
6248 Flag = Chain.getValue(1);
6249
6250 // If this asm returns a register value, copy the result from that register
6251 // and set it as the value of the call.
6252 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006253 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006254 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006255
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006256 // FIXME: Why don't we do this for inline asms with MRVs?
6257 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006258 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006259
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006260 // If any of the results of the inline asm is a vector, it may have the
6261 // wrong width/num elts. This can happen for register classes that can
6262 // contain multiple different value types. The preg or vreg allocated may
6263 // not have the same VT as was expected. Convert it to the right type
6264 // with bit_convert.
6265 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006266 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006267 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006268
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006269 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006270 ResultType.isInteger() && Val.getValueType().isInteger()) {
6271 // If a result value was tied to an input value, the computed result may
6272 // have a wider width than the expected result. Extract the relevant
6273 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006274 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006275 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006276
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006277 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006278 }
Dan Gohman95915732008-10-18 01:03:45 +00006279
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006280 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006281 // Don't need to use this as a chain in this case.
6282 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6283 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006284 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006285
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006286 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006287
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006288 // Process indirect outputs, first output all of the flagged copies out of
6289 // physregs.
6290 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6291 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
6292 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00006293 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006294 SDNodeOrder, Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006295 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00006296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006297 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006298
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006299 // Emit the non-flagged stores from the physregs.
6300 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006301 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6302 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6303 StoresToEmit[i].first,
6304 getValue(StoresToEmit[i].second),
6305 StoresToEmit[i].second, 0);
6306 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006307 }
6308
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006309 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006310 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006311 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006312
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006313 DAG.setRoot(Chain);
6314}
6315
Dan Gohman2048b852009-11-23 18:04:58 +00006316void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006317 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6318 MVT::Other, getRoot(),
6319 getValue(I.getOperand(1)),
6320 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006321}
6322
Dan Gohman2048b852009-11-23 18:04:58 +00006323void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006324 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6325 getRoot(), getValue(I.getOperand(0)),
6326 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006327 setValue(&I, V);
6328 DAG.setRoot(V.getValue(1));
6329}
6330
Dan Gohman2048b852009-11-23 18:04:58 +00006331void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006332 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6333 MVT::Other, getRoot(),
6334 getValue(I.getOperand(1)),
6335 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006336}
6337
Dan Gohman2048b852009-11-23 18:04:58 +00006338void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006339 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6340 MVT::Other, getRoot(),
6341 getValue(I.getOperand(1)),
6342 getValue(I.getOperand(2)),
6343 DAG.getSrcValue(I.getOperand(1)),
6344 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006345}
6346
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006347/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006348/// implementation, which just calls LowerCall.
6349/// FIXME: When all targets are
6350/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006351std::pair<SDValue, SDValue>
6352TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6353 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006354 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006355 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006356 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006357 SDValue Callee,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006358 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl,
6359 unsigned Order) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00006360 assert((!isTailCall || PerformTailCallOpt) &&
6361 "isTailCall set when tail-call optimizations are disabled!");
6362
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006363 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006364 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006365 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006366 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006367 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6368 for (unsigned Value = 0, NumValues = ValueVTs.size();
6369 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006370 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006371 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006372 SDValue Op = SDValue(Args[i].Node.getNode(),
6373 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006374 ISD::ArgFlagsTy Flags;
6375 unsigned OriginalAlignment =
6376 getTargetData()->getABITypeAlignment(ArgTy);
6377
6378 if (Args[i].isZExt)
6379 Flags.setZExt();
6380 if (Args[i].isSExt)
6381 Flags.setSExt();
6382 if (Args[i].isInReg)
6383 Flags.setInReg();
6384 if (Args[i].isSRet)
6385 Flags.setSRet();
6386 if (Args[i].isByVal) {
6387 Flags.setByVal();
6388 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6389 const Type *ElementTy = Ty->getElementType();
6390 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006391 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006392 // For ByVal, alignment should come from FE. BE will guess if this
6393 // info is not there but there are cases it cannot get right.
6394 if (Args[i].Alignment)
6395 FrameAlign = Args[i].Alignment;
6396 Flags.setByValAlign(FrameAlign);
6397 Flags.setByValSize(FrameSize);
6398 }
6399 if (Args[i].isNest)
6400 Flags.setNest();
6401 Flags.setOrigAlign(OriginalAlignment);
6402
Owen Anderson23b9b192009-08-12 00:36:31 +00006403 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6404 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006405 SmallVector<SDValue, 4> Parts(NumParts);
6406 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6407
6408 if (Args[i].isSExt)
6409 ExtendKind = ISD::SIGN_EXTEND;
6410 else if (Args[i].isZExt)
6411 ExtendKind = ISD::ZERO_EXTEND;
6412
Bill Wendling3ea3c242009-12-22 02:10:19 +00006413 getCopyToParts(DAG, dl, Order, Op, &Parts[0], NumParts,
6414 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006415
Dan Gohman98ca4f22009-08-05 01:29:28 +00006416 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006417 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006418 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6419 if (NumParts > 1 && j == 0)
6420 MyFlags.Flags.setSplit();
6421 else if (j != 0)
6422 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006423
Dan Gohman98ca4f22009-08-05 01:29:28 +00006424 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006425 }
6426 }
6427 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006428
Dan Gohman98ca4f22009-08-05 01:29:28 +00006429 // Handle the incoming return values from the call.
6430 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006431 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006432 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006433 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006434 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006435 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6436 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006437 for (unsigned i = 0; i != NumRegs; ++i) {
6438 ISD::InputArg MyFlags;
6439 MyFlags.VT = RegisterVT;
6440 MyFlags.Used = isReturnValueUsed;
6441 if (RetSExt)
6442 MyFlags.Flags.setSExt();
6443 if (RetZExt)
6444 MyFlags.Flags.setZExt();
6445 if (isInreg)
6446 MyFlags.Flags.setInReg();
6447 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006448 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006449 }
6450
Dan Gohman98ca4f22009-08-05 01:29:28 +00006451 // Check if target-dependent constraints permit a tail call here.
6452 // Target-independent constraints should be checked by the caller.
6453 if (isTailCall &&
6454 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6455 isTailCall = false;
6456
6457 SmallVector<SDValue, 4> InVals;
6458 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6459 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006460
6461 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006462 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006463 "LowerCall didn't return a valid chain!");
6464 assert((!isTailCall || InVals.empty()) &&
6465 "LowerCall emitted a return value for a tail call!");
6466 assert((isTailCall || InVals.size() == Ins.size()) &&
6467 "LowerCall didn't emit the correct number of values!");
6468 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6469 assert(InVals[i].getNode() &&
6470 "LowerCall emitted a null value!");
6471 assert(Ins[i].VT == InVals[i].getValueType() &&
6472 "LowerCall emitted a value with the wrong type!");
6473 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006474
Bill Wendling3ea3c242009-12-22 02:10:19 +00006475 if (DisableScheduling)
6476 DAG.AssignOrdering(Chain.getNode(), Order);
6477
Dan Gohman98ca4f22009-08-05 01:29:28 +00006478 // For a tail call, the return value is merely live-out and there aren't
6479 // any nodes in the DAG representing it. Return a special value to
6480 // indicate that a tail call has been emitted and no more Instructions
6481 // should be processed in the current block.
6482 if (isTailCall) {
6483 DAG.setRoot(Chain);
6484 return std::make_pair(SDValue(), SDValue());
6485 }
6486
6487 // Collect the legal value parts into potentially illegal values
6488 // that correspond to the original function's return values.
6489 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6490 if (RetSExt)
6491 AssertOp = ISD::AssertSext;
6492 else if (RetZExt)
6493 AssertOp = ISD::AssertZext;
6494 SmallVector<SDValue, 4> ReturnValues;
6495 unsigned CurReg = 0;
6496 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006497 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006498 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6499 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006500
6501 SDValue ReturnValue =
Bill Wendling3ea3c242009-12-22 02:10:19 +00006502 getCopyFromParts(DAG, dl, Order, &InVals[CurReg], NumRegs,
6503 RegisterVT, VT, AssertOp);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006504 ReturnValues.push_back(ReturnValue);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006505 if (DisableScheduling)
6506 DAG.AssignOrdering(ReturnValue.getNode(), Order);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006507 CurReg += NumRegs;
6508 }
6509
6510 // For a function returning void, there is no return value. We can't create
6511 // such a node, so we just return a null return value in that case. In
6512 // that case, nothing will actualy look at the value.
6513 if (ReturnValues.empty())
6514 return std::make_pair(SDValue(), Chain);
6515
6516 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6517 DAG.getVTList(&RetTys[0], RetTys.size()),
6518 &ReturnValues[0], ReturnValues.size());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006519 if (DisableScheduling)
6520 DAG.AssignOrdering(Res.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006521 return std::make_pair(Res, Chain);
6522}
6523
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006524void TargetLowering::LowerOperationWrapper(SDNode *N,
6525 SmallVectorImpl<SDValue> &Results,
6526 SelectionDAG &DAG) {
6527 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006528 if (Res.getNode())
6529 Results.push_back(Res);
6530}
6531
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006532SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006533 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006534 return SDValue();
6535}
6536
Dan Gohman2048b852009-11-23 18:04:58 +00006537void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006538 SDValue Op = getValue(V);
6539 assert((Op.getOpcode() != ISD::CopyFromReg ||
6540 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6541 "Copy from a reg to the same reg!");
6542 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6543
Owen Anderson23b9b192009-08-12 00:36:31 +00006544 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006545 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +00006546 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006547 PendingExports.push_back(Chain);
6548}
6549
6550#include "llvm/CodeGen/SelectionDAGISel.h"
6551
Dan Gohman8c2b5252009-10-30 01:27:03 +00006552void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006553 // If this is the entry block, emit arguments.
6554 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006555 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006556 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006557 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006558 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006559 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006560
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006561 // Check whether the function can return without sret-demotion.
6562 SmallVector<EVT, 4> OutVTs;
6563 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006564 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
6565 OutVTs, OutsFlags, TLI);
6566 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6567
6568 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00006569 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006570 if (!FLI.CanLowerReturn) {
6571 // Put in an sret pointer parameter before all the other parameters.
6572 SmallVector<EVT, 1> ValueVTs;
6573 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6574
6575 // NOTE: Assuming that a pointer will never break down to more than one VT
6576 // or one register.
6577 ISD::ArgFlagsTy Flags;
6578 Flags.setSRet();
6579 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6580 ISD::InputArg RetArg(Flags, RegisterVT, true);
6581 Ins.push_back(RetArg);
6582 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006583
Dan Gohman98ca4f22009-08-05 01:29:28 +00006584 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006585 unsigned Idx = 1;
6586 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6587 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006588 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006589 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6590 bool isArgValueUsed = !I->use_empty();
6591 for (unsigned Value = 0, NumValues = ValueVTs.size();
6592 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006593 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006594 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006595 ISD::ArgFlagsTy Flags;
6596 unsigned OriginalAlignment =
6597 TD->getABITypeAlignment(ArgTy);
6598
6599 if (F.paramHasAttr(Idx, Attribute::ZExt))
6600 Flags.setZExt();
6601 if (F.paramHasAttr(Idx, Attribute::SExt))
6602 Flags.setSExt();
6603 if (F.paramHasAttr(Idx, Attribute::InReg))
6604 Flags.setInReg();
6605 if (F.paramHasAttr(Idx, Attribute::StructRet))
6606 Flags.setSRet();
6607 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6608 Flags.setByVal();
6609 const PointerType *Ty = cast<PointerType>(I->getType());
6610 const Type *ElementTy = Ty->getElementType();
6611 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6612 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6613 // For ByVal, alignment should be passed from FE. BE will guess if
6614 // this info is not there but there are cases it cannot get right.
6615 if (F.getParamAlignment(Idx))
6616 FrameAlign = F.getParamAlignment(Idx);
6617 Flags.setByValAlign(FrameAlign);
6618 Flags.setByValSize(FrameSize);
6619 }
6620 if (F.paramHasAttr(Idx, Attribute::Nest))
6621 Flags.setNest();
6622 Flags.setOrigAlign(OriginalAlignment);
6623
Owen Anderson23b9b192009-08-12 00:36:31 +00006624 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6625 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006626 for (unsigned i = 0; i != NumRegs; ++i) {
6627 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6628 if (NumRegs > 1 && i == 0)
6629 MyFlags.Flags.setSplit();
6630 // if it isn't first piece, alignment must be 1
6631 else if (i > 0)
6632 MyFlags.Flags.setOrigAlign(1);
6633 Ins.push_back(MyFlags);
6634 }
6635 }
6636 }
6637
6638 // Call the target to set up the argument values.
6639 SmallVector<SDValue, 8> InVals;
6640 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6641 F.isVarArg(), Ins,
6642 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006643
6644 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006645 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006646 "LowerFormalArguments didn't return a valid chain!");
6647 assert(InVals.size() == Ins.size() &&
6648 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006649 DEBUG({
6650 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6651 assert(InVals[i].getNode() &&
6652 "LowerFormalArguments emitted a null value!");
6653 assert(Ins[i].VT == InVals[i].getValueType() &&
6654 "LowerFormalArguments emitted a value with the wrong type!");
6655 }
6656 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006657
Dan Gohman5e866062009-08-06 15:37:27 +00006658 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006659 DAG.setRoot(NewRoot);
6660
6661 // Set up the argument values.
6662 unsigned i = 0;
6663 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006664 if (!FLI.CanLowerReturn) {
6665 // Create a virtual register for the sret pointer, and put in a copy
6666 // from the sret argument into it.
6667 SmallVector<EVT, 1> ValueVTs;
6668 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6669 EVT VT = ValueVTs[0];
6670 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6671 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling3ea58b62009-12-22 21:35:02 +00006672 SDValue ArgValue = getCopyFromParts(DAG, dl, 0, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006673 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006674
Dan Gohman2048b852009-11-23 18:04:58 +00006675 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006676 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6677 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6678 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00006679 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006680 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006681
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006682 // i indexes lowered arguments. Bump it past the hidden sret argument.
6683 // Idx indexes LLVM arguments. Don't touch it.
6684 ++i;
6685 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006686
Dan Gohman98ca4f22009-08-05 01:29:28 +00006687 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6688 ++I, ++Idx) {
6689 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006690 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006691 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006692 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006693 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006694 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006695 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6696 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006697
6698 if (!I->use_empty()) {
6699 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6700 if (F.paramHasAttr(Idx, Attribute::SExt))
6701 AssertOp = ISD::AssertSext;
6702 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6703 AssertOp = ISD::AssertZext;
6704
Bill Wendling3ea58b62009-12-22 21:35:02 +00006705 ArgValues.push_back(getCopyFromParts(DAG, dl, 0, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006706 NumParts, PartVT, VT,
6707 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006708 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006709
Dan Gohman98ca4f22009-08-05 01:29:28 +00006710 i += NumParts;
6711 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006712
Dan Gohman98ca4f22009-08-05 01:29:28 +00006713 if (!I->use_empty()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +00006714 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6715 SDB->getCurDebugLoc());
6716 SDB->setValue(I, Res);
6717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006718 // If this argument is live outside of the entry block, insert a copy from
6719 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006720 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006721 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006722 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006723
Dan Gohman98ca4f22009-08-05 01:29:28 +00006724 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006725
6726 // Finally, if the target has anything special to do, allow it to do so.
6727 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006728 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006729}
6730
6731/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6732/// ensure constants are generated when needed. Remember the virtual registers
6733/// that need to be added to the Machine PHI nodes as input. We cannot just
6734/// directly add them, because expansion might result in multiple MBB's for one
6735/// BB. As such, the start of the BB might correspond to a different MBB than
6736/// the end.
6737///
6738void
6739SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6740 TerminatorInst *TI = LLVMBB->getTerminator();
6741
6742 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6743
6744 // Check successor nodes' PHI nodes that expect a constant to be available
6745 // from this block.
6746 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6747 BasicBlock *SuccBB = TI->getSuccessor(succ);
6748 if (!isa<PHINode>(SuccBB->begin())) continue;
6749 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006750
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006751 // If this terminator has multiple identical successors (common for
6752 // switches), only handle each succ once.
6753 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006754
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006755 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6756 PHINode *PN;
6757
6758 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6759 // nodes and Machine PHI nodes, but the incoming operands have not been
6760 // emitted yet.
6761 for (BasicBlock::iterator I = SuccBB->begin();
6762 (PN = dyn_cast<PHINode>(I)); ++I) {
6763 // Ignore dead phi's.
6764 if (PN->use_empty()) continue;
6765
6766 unsigned Reg;
6767 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6768
6769 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006770 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006771 if (RegOut == 0) {
6772 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006773 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006774 }
6775 Reg = RegOut;
6776 } else {
6777 Reg = FuncInfo->ValueMap[PHIOp];
6778 if (Reg == 0) {
6779 assert(isa<AllocaInst>(PHIOp) &&
6780 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6781 "Didn't codegen value into a register!??");
6782 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006783 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006784 }
6785 }
6786
6787 // Remember that this register needs to added to the machine PHI node as
6788 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006789 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006790 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6791 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006792 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006793 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006794 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006795 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006796 Reg += NumRegisters;
6797 }
6798 }
6799 }
Dan Gohman2048b852009-11-23 18:04:58 +00006800 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006801}
6802
Dan Gohman3df24e62008-09-03 23:12:08 +00006803/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6804/// supports legal types, and it emits MachineInstrs directly instead of
6805/// creating SelectionDAG nodes.
6806///
6807bool
6808SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6809 FastISel *F) {
6810 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006811
Dan Gohman3df24e62008-09-03 23:12:08 +00006812 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006813 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006814
6815 // Check successor nodes' PHI nodes that expect a constant to be available
6816 // from this block.
6817 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6818 BasicBlock *SuccBB = TI->getSuccessor(succ);
6819 if (!isa<PHINode>(SuccBB->begin())) continue;
6820 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006821
Dan Gohman3df24e62008-09-03 23:12:08 +00006822 // If this terminator has multiple identical successors (common for
6823 // switches), only handle each succ once.
6824 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006825
Dan Gohman3df24e62008-09-03 23:12:08 +00006826 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6827 PHINode *PN;
6828
6829 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6830 // nodes and Machine PHI nodes, but the incoming operands have not been
6831 // emitted yet.
6832 for (BasicBlock::iterator I = SuccBB->begin();
6833 (PN = dyn_cast<PHINode>(I)); ++I) {
6834 // Ignore dead phi's.
6835 if (PN->use_empty()) continue;
6836
6837 // Only handle legal types. Two interesting things to note here. First,
6838 // by bailing out early, we may leave behind some dead instructions,
6839 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6840 // own moves. Second, this check is necessary becuase FastISel doesn't
6841 // use CreateRegForValue to create registers, so it always creates
6842 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006843 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006844 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6845 // Promote MVT::i1.
6846 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006847 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006848 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006849 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006850 return false;
6851 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006852 }
6853
6854 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6855
6856 unsigned Reg = F->getRegForValue(PHIOp);
6857 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006858 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006859 return false;
6860 }
Dan Gohman2048b852009-11-23 18:04:58 +00006861 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006862 }
6863 }
6864
6865 return true;
6866}