blob: 7747825f48c91f09438fd99c59c62206ae7ec3af [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,
Owen Anderson23b9b192009-08-12 00:36:31 +0000465 EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000466 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000467
468 if (DisableScheduling)
469 DAG.AssignOrdering(Parts[0].getNode(), Order);
470
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000471 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
472 for (unsigned i = 0; i < NumParts; i += StepSize) {
473 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000474 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000475 SDValue &Part0 = Parts[i];
476 SDValue &Part1 = Parts[i+StepSize/2];
477
Scott Michelfdc40a02009-02-17 22:15:04 +0000478 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000479 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000480 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000481 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000482 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000483 DAG.getConstant(0, PtrVT));
484
Bill Wendling3ea3c242009-12-22 02:10:19 +0000485 if (DisableScheduling) {
486 DAG.AssignOrdering(Part0.getNode(), Order);
487 DAG.AssignOrdering(Part1.getNode(), Order);
488 }
489
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000490 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000491 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000492 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000493 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000494 PartVT, Part1);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000495 if (DisableScheduling) {
496 DAG.AssignOrdering(Part0.getNode(), Order);
497 DAG.AssignOrdering(Part1.getNode(), Order);
498 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000499 }
500 }
501 }
502
503 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000504 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000505
506 return;
507 }
508
509 // Vector ValueVT.
510 if (NumParts == 1) {
511 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000512 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000513 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000514 } else {
515 assert(ValueVT.getVectorElementType() == PartVT &&
516 ValueVT.getVectorNumElements() == 1 &&
517 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000518 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000519 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000520 DAG.getConstant(0, PtrVT));
521 }
522 }
523
Bill Wendling3ea3c242009-12-22 02:10:19 +0000524 if (DisableScheduling)
525 DAG.AssignOrdering(Val.getNode(), Order);
526
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000527 Parts[0] = Val;
528 return;
529 }
530
531 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000532 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000533 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000534 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
535 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000536 unsigned NumElements = ValueVT.getVectorNumElements();
537
538 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
539 NumParts = NumRegs; // Silence a compiler warning.
540 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
541
542 // Split the vector into intermediate operands.
543 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000544 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000545 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000546 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000547 IntermediateVT, Val,
548 DAG.getConstant(i * (NumElements / NumIntermediates),
549 PtrVT));
550 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000551 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000552 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000553 DAG.getConstant(i, PtrVT));
554
Bill Wendling3ea3c242009-12-22 02:10:19 +0000555 if (DisableScheduling)
556 DAG.AssignOrdering(Ops[i].getNode(), Order);
557 }
558
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000559 // Split the intermediate operands into legal parts.
560 if (NumParts == NumIntermediates) {
561 // If the register was not expanded, promote or copy the value,
562 // as appropriate.
563 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000564 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000565 } else if (NumParts > 0) {
566 // If the intermediate type was expanded, split each the value into
567 // legal parts.
568 assert(NumParts % NumIntermediates == 0 &&
569 "Must expand into a divisible number of parts!");
570 unsigned Factor = NumParts / NumIntermediates;
571 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000572 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000573 }
574}
575
576
Dan Gohman2048b852009-11-23 18:04:58 +0000577void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000578 AA = &aa;
579 GFI = gfi;
580 TD = DAG.getTarget().getTargetData();
581}
582
583/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000584/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000585/// for a new block. This doesn't clear out information about
586/// additional blocks that are needed to complete switch lowering
587/// or PHI node updating; that information is cleared out as it is
588/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000589void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000590 NodeMap.clear();
591 PendingLoads.clear();
592 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000593 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000594 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000595 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000596 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000597}
598
599/// getRoot - Return the current virtual root of the Selection DAG,
600/// flushing any PendingLoad items. This must be done before emitting
601/// a store or any other node that may need to be ordered after any
602/// prior load instructions.
603///
Dan Gohman2048b852009-11-23 18:04:58 +0000604SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000605 if (PendingLoads.empty())
606 return DAG.getRoot();
607
608 if (PendingLoads.size() == 1) {
609 SDValue Root = PendingLoads[0];
610 DAG.setRoot(Root);
611 PendingLoads.clear();
612 return Root;
613 }
614
615 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000616 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000617 &PendingLoads[0], PendingLoads.size());
618 PendingLoads.clear();
619 DAG.setRoot(Root);
620 return Root;
621}
622
623/// getControlRoot - Similar to getRoot, but instead of flushing all the
624/// PendingLoad items, flush all the PendingExports items. It is necessary
625/// to do this before emitting a terminator instruction.
626///
Dan Gohman2048b852009-11-23 18:04:58 +0000627SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000628 SDValue Root = DAG.getRoot();
629
630 if (PendingExports.empty())
631 return Root;
632
633 // Turn all of the CopyToReg chains into one factored node.
634 if (Root.getOpcode() != ISD::EntryToken) {
635 unsigned i = 0, e = PendingExports.size();
636 for (; i != e; ++i) {
637 assert(PendingExports[i].getNode()->getNumOperands() > 1);
638 if (PendingExports[i].getNode()->getOperand(0) == Root)
639 break; // Don't add the root if we already indirectly depend on it.
640 }
641
642 if (i == e)
643 PendingExports.push_back(Root);
644 }
645
Owen Anderson825b72b2009-08-11 20:47:22 +0000646 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000647 &PendingExports[0],
648 PendingExports.size());
649 PendingExports.clear();
650 DAG.setRoot(Root);
651 return Root;
652}
653
Dan Gohman2048b852009-11-23 18:04:58 +0000654void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000655 visit(I.getOpcode(), I);
656}
657
Dan Gohman2048b852009-11-23 18:04:58 +0000658void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000659 // We're processing a new instruction.
660 ++SDNodeOrder;
661
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000662 // Note: this doesn't use InstVisitor, because it has to work with
663 // ConstantExpr's in addition to instructions.
664 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000665 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000666 // Build the switch statement using the Instruction.def file.
667#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000668 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000669#include "llvm/Instruction.def"
670 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000671}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000672
Dan Gohman2048b852009-11-23 18:04:58 +0000673SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000674 SDValue &N = NodeMap[V];
675 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000676
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000677 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000678 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000679
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000680 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000681 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682
683 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
684 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000685
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000686 if (isa<ConstantPointerNull>(C))
687 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000688
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000689 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000690 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000691
Nate Begeman9008ca62009-04-27 18:41:29 +0000692 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000693 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000694
695 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
696 visit(CE->getOpcode(), *CE);
697 SDValue N1 = NodeMap[V];
698 assert(N1.getNode() && "visit didn't populate the ValueMap!");
699 return N1;
700 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000701
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000702 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
703 SmallVector<SDValue, 4> Constants;
704 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
705 OI != OE; ++OI) {
706 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000707 // If the operand is an empty aggregate, there are no values.
708 if (!Val) continue;
709 // Add each leaf value from the operand to the Constants list
710 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000711 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
712 Constants.push_back(SDValue(Val, i));
713 }
Bill Wendling87710f02009-12-21 23:47:40 +0000714
715 SDValue Res = DAG.getMergeValues(&Constants[0], Constants.size(),
716 getCurDebugLoc());
717 if (DisableScheduling)
718 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
719 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000720 }
721
722 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
723 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
724 "Unknown struct or array constant!");
725
Owen Andersone50ed302009-08-10 22:56:29 +0000726 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000727 ComputeValueVTs(TLI, C->getType(), ValueVTs);
728 unsigned NumElts = ValueVTs.size();
729 if (NumElts == 0)
730 return SDValue(); // empty struct
731 SmallVector<SDValue, 4> Constants(NumElts);
732 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000733 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000734 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000735 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000736 else if (EltVT.isFloatingPoint())
737 Constants[i] = DAG.getConstantFP(0, EltVT);
738 else
739 Constants[i] = DAG.getConstant(0, EltVT);
740 }
Bill Wendling87710f02009-12-21 23:47:40 +0000741
742 SDValue Res = DAG.getMergeValues(&Constants[0], NumElts,
743 getCurDebugLoc());
744 if (DisableScheduling)
745 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
746 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000747 }
748
Dan Gohman8c2b5252009-10-30 01:27:03 +0000749 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000750 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000751
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000752 const VectorType *VecTy = cast<VectorType>(V->getType());
753 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000754
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000755 // Now that we know the number and type of the elements, get that number of
756 // elements into the Ops array based on what kind of constant it is.
757 SmallVector<SDValue, 16> Ops;
758 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
759 for (unsigned i = 0; i != NumElements; ++i)
760 Ops.push_back(getValue(CP->getOperand(i)));
761 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000762 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000763 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000764
765 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000766 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000767 Op = DAG.getConstantFP(0, EltVT);
768 else
769 Op = DAG.getConstant(0, EltVT);
770 Ops.assign(NumElements, Op);
771 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000772
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000773 // Create a BUILD_VECTOR node.
Bill Wendling87710f02009-12-21 23:47:40 +0000774 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
775 VT, &Ops[0], Ops.size());
776 if (DisableScheduling)
777 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
778
779 return NodeMap[V] = Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000780 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000781
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000782 // If this is a static alloca, generate it as the frameindex instead of
783 // computation.
784 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
785 DenseMap<const AllocaInst*, int>::iterator SI =
786 FuncInfo.StaticAllocaMap.find(AI);
787 if (SI != FuncInfo.StaticAllocaMap.end())
788 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
789 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000790
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000791 unsigned InReg = FuncInfo.ValueMap[V];
792 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000793
Owen Anderson23b9b192009-08-12 00:36:31 +0000794 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000795 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +0000796 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(),
797 SDNodeOrder, Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000798}
799
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000800/// Get the EVTs and ArgFlags collections that represent the return type
801/// of the given function. This does not require a DAG or a return value, and
802/// is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000803static void getReturnInfo(const Type* ReturnType,
804 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000805 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000806 TargetLowering &TLI,
807 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000808 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000809 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000810 unsigned NumValues = ValueVTs.size();
811 if ( NumValues == 0 ) return;
812
813 for (unsigned j = 0, f = NumValues; j != f; ++j) {
814 EVT VT = ValueVTs[j];
815 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000816
817 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000818 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000819 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000820 ExtendKind = ISD::ZERO_EXTEND;
821
822 // FIXME: C calling convention requires the return type to be promoted to
823 // at least 32-bit. But this is not necessary for non-C calling
824 // conventions. The frontend should mark functions whose return values
825 // require promoting with signext or zeroext attributes.
826 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000827 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000828 if (VT.bitsLT(MinVT))
829 VT = MinVT;
830 }
831
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000832 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
833 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000834 // 'inreg' on function refers to return value
835 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000836 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000837 Flags.setInReg();
838
839 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000840 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000841 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000842 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000843 Flags.setZExt();
844
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000845 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000846 OutVTs.push_back(PartVT);
847 OutFlags.push_back(Flags);
848 }
849 }
850}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000851
Dan Gohman2048b852009-11-23 18:04:58 +0000852void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000853 SDValue Chain = getControlRoot();
854 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000855 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
856
857 if (!FLI.CanLowerReturn) {
858 unsigned DemoteReg = FLI.DemoteRegister;
859 const Function *F = I.getParent()->getParent();
860
861 // Emit a store of the return value through the virtual register.
862 // Leave Outs empty so that LowerReturn won't try to load return
863 // registers the usual way.
864 SmallVector<EVT, 1> PtrValueVTs;
865 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
866 PtrValueVTs);
867
868 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
869 SDValue RetOp = getValue(I.getOperand(0));
870
Owen Andersone50ed302009-08-10 22:56:29 +0000871 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000872 SmallVector<uint64_t, 4> Offsets;
873 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000874 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000875
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000876 SmallVector<SDValue, 4> Chains(NumValues);
877 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000878 for (unsigned i = 0; i != NumValues; ++i) {
879 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
880 DAG.getConstant(Offsets[i], PtrVT));
881 Chains[i] =
882 DAG.getStore(Chain, getCurDebugLoc(),
883 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
884 Add, NULL, Offsets[i], false, 0);
885
886 if (DisableScheduling) {
887 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
888 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
889 }
890 }
891
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000892 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
893 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000894
895 if (DisableScheduling)
896 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
897 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000898 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
899 SmallVector<EVT, 4> ValueVTs;
900 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
901 unsigned NumValues = ValueVTs.size();
902 if (NumValues == 0) continue;
903
904 SDValue RetOp = getValue(I.getOperand(i));
905 for (unsigned j = 0, f = NumValues; j != f; ++j) {
906 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000907
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000908 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000909
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000910 const Function *F = I.getParent()->getParent();
911 if (F->paramHasAttr(0, Attribute::SExt))
912 ExtendKind = ISD::SIGN_EXTEND;
913 else if (F->paramHasAttr(0, Attribute::ZExt))
914 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000916 // FIXME: C calling convention requires the return type to be promoted to
917 // at least 32-bit. But this is not necessary for non-C calling
918 // conventions. The frontend should mark functions whose return values
919 // require promoting with signext or zeroext attributes.
920 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
921 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
922 if (VT.bitsLT(MinVT))
923 VT = MinVT;
924 }
925
926 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
927 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
928 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000929 getCopyToParts(DAG, getCurDebugLoc(), SDNodeOrder,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000930 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
931 &Parts[0], NumParts, PartVT, ExtendKind);
932
933 // 'inreg' on function refers to return value
934 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
935 if (F->paramHasAttr(0, Attribute::InReg))
936 Flags.setInReg();
937
938 // Propagate extension type if any
939 if (F->paramHasAttr(0, Attribute::SExt))
940 Flags.setSExt();
941 else if (F->paramHasAttr(0, Attribute::ZExt))
942 Flags.setZExt();
943
944 for (unsigned i = 0; i < NumParts; ++i)
945 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000946 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000947 }
948 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000949
950 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000951 CallingConv::ID CallConv =
952 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000953 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
954 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000955
956 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000957 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000958 "LowerReturn didn't return a valid chain!");
959
960 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000961 DAG.setRoot(Chain);
Bill Wendling87710f02009-12-21 23:47:40 +0000962
963 if (DisableScheduling)
964 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000965}
966
Dan Gohmanad62f532009-04-23 23:13:24 +0000967/// CopyToExportRegsIfNeeded - If the given value has virtual registers
968/// created for it, emit nodes to copy the value into the virtual
969/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000970void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000971 if (!V->use_empty()) {
972 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
973 if (VMI != FuncInfo.ValueMap.end())
974 CopyValueToVirtualRegister(V, VMI->second);
975 }
976}
977
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000978/// ExportFromCurrentBlock - If this condition isn't known to be exported from
979/// the current basic block, add it to ValueMap now so that we'll get a
980/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000981void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000982 // No need to export constants.
983 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000985 // Already exported?
986 if (FuncInfo.isExportedInst(V)) return;
987
988 unsigned Reg = FuncInfo.InitializeRegForValue(V);
989 CopyValueToVirtualRegister(V, Reg);
990}
991
Dan Gohman2048b852009-11-23 18:04:58 +0000992bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
993 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000994 // The operands of the setcc have to be in this block. We don't know
995 // how to export them from some other block.
996 if (Instruction *VI = dyn_cast<Instruction>(V)) {
997 // Can export from current BB.
998 if (VI->getParent() == FromBB)
999 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001000
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001001 // Is already exported, noop.
1002 return FuncInfo.isExportedInst(V);
1003 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001005 // If this is an argument, we can export it if the BB is the entry block or
1006 // if it is already exported.
1007 if (isa<Argument>(V)) {
1008 if (FromBB == &FromBB->getParent()->getEntryBlock())
1009 return true;
1010
1011 // Otherwise, can only export this if it is already exported.
1012 return FuncInfo.isExportedInst(V);
1013 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001015 // Otherwise, constants can always be exported.
1016 return true;
1017}
1018
1019static bool InBlock(const Value *V, const BasicBlock *BB) {
1020 if (const Instruction *I = dyn_cast<Instruction>(V))
1021 return I->getParent() == BB;
1022 return true;
1023}
1024
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001025/// getFCmpCondCode - Return the ISD condition code corresponding to
1026/// the given LLVM IR floating-point condition code. This includes
1027/// consideration of global floating-point math flags.
1028///
1029static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1030 ISD::CondCode FPC, FOC;
1031 switch (Pred) {
1032 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1033 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1034 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1035 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1036 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1037 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1038 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1039 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1040 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1041 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1042 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1043 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1044 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1045 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1046 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1047 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1048 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001049 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001050 FOC = FPC = ISD::SETFALSE;
1051 break;
1052 }
1053 if (FiniteOnlyFPMath())
1054 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001055 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001056 return FPC;
1057}
1058
1059/// getICmpCondCode - Return the ISD condition code corresponding to
1060/// the given LLVM IR integer condition code.
1061///
1062static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1063 switch (Pred) {
1064 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1065 case ICmpInst::ICMP_NE: return ISD::SETNE;
1066 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1067 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1068 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1069 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1070 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1071 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1072 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1073 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1074 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001075 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001076 return ISD::SETNE;
1077 }
1078}
1079
Dan Gohmanc2277342008-10-17 21:16:08 +00001080/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1081/// This function emits a branch and is used at the leaves of an OR or an
1082/// AND operator tree.
1083///
1084void
Dan Gohman2048b852009-11-23 18:04:58 +00001085SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1086 MachineBasicBlock *TBB,
1087 MachineBasicBlock *FBB,
1088 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001089 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001090
Dan Gohmanc2277342008-10-17 21:16:08 +00001091 // If the leaf of the tree is a comparison, merge the condition into
1092 // the caseblock.
1093 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1094 // The operands of the cmp have to be in this block. We don't know
1095 // how to export them from some other block. If this is the first block
1096 // of the sequence, no exporting is needed.
1097 if (CurBB == CurMBB ||
1098 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1099 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001100 ISD::CondCode Condition;
1101 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001102 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001103 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001104 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001105 } else {
1106 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001107 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001108 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001109
1110 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001111 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1112 SwitchCases.push_back(CB);
1113 return;
1114 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001115 }
1116
1117 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001118 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001119 NULL, TBB, FBB, CurBB);
1120 SwitchCases.push_back(CB);
1121}
1122
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001123/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001124void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1125 MachineBasicBlock *TBB,
1126 MachineBasicBlock *FBB,
1127 MachineBasicBlock *CurBB,
1128 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001129 // If this node is not part of the or/and tree, emit it as a branch.
1130 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001131 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001132 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1133 BOp->getParent() != CurBB->getBasicBlock() ||
1134 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1135 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1136 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001137 return;
1138 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001139
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001140 // Create TmpBB after CurBB.
1141 MachineFunction::iterator BBI = CurBB;
1142 MachineFunction &MF = DAG.getMachineFunction();
1143 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1144 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001146 if (Opc == Instruction::Or) {
1147 // Codegen X | Y as:
1148 // jmp_if_X TBB
1149 // jmp TmpBB
1150 // TmpBB:
1151 // jmp_if_Y TBB
1152 // jmp FBB
1153 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001155 // Emit the LHS condition.
1156 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 // Emit the RHS condition into TmpBB.
1159 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1160 } else {
1161 assert(Opc == Instruction::And && "Unknown merge op!");
1162 // Codegen X & Y as:
1163 // jmp_if_X TmpBB
1164 // jmp FBB
1165 // TmpBB:
1166 // jmp_if_Y TBB
1167 // jmp FBB
1168 //
1169 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001170
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001171 // Emit the LHS condition.
1172 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001174 // Emit the RHS condition into TmpBB.
1175 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1176 }
1177}
1178
1179/// If the set of cases should be emitted as a series of branches, return true.
1180/// If we should emit this as a bunch of and/or'd together conditions, return
1181/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001182bool
Dan Gohman2048b852009-11-23 18:04:58 +00001183SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001184 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001186 // If this is two comparisons of the same values or'd or and'd together, they
1187 // will get folded into a single comparison, so don't emit two blocks.
1188 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1189 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1190 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1191 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1192 return false;
1193 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001195 return true;
1196}
1197
Dan Gohman2048b852009-11-23 18:04:58 +00001198void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001199 // Update machine-CFG edges.
1200 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1201
1202 // Figure out which block is immediately after the current one.
1203 MachineBasicBlock *NextBlock = 0;
1204 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001205 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001206 NextBlock = BBI;
1207
1208 if (I.isUnconditional()) {
1209 // Update machine-CFG edges.
1210 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001211
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001212 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001213 if (Succ0MBB != NextBlock) {
1214 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001215 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001216 DAG.getBasicBlock(Succ0MBB));
1217 DAG.setRoot(V);
1218
1219 if (DisableScheduling)
1220 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1221 }
1222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 return;
1224 }
1225
1226 // If this condition is one of the special cases we handle, do special stuff
1227 // now.
1228 Value *CondVal = I.getCondition();
1229 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1230
1231 // If this is a series of conditions that are or'd or and'd together, emit
1232 // this as a sequence of branches instead of setcc's with and/or operations.
1233 // For example, instead of something like:
1234 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001235 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001237 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001238 // or C, F
1239 // jnz foo
1240 // Emit:
1241 // cmp A, B
1242 // je foo
1243 // cmp D, E
1244 // jle foo
1245 //
1246 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001247 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001248 (BOp->getOpcode() == Instruction::And ||
1249 BOp->getOpcode() == Instruction::Or)) {
1250 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1251 // If the compares in later blocks need to use values not currently
1252 // exported from this block, export them now. This block should always
1253 // be the first entry.
1254 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001255
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001256 // Allow some cases to be rejected.
1257 if (ShouldEmitAsBranches(SwitchCases)) {
1258 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1259 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1260 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1261 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 // Emit the branch for this block.
1264 visitSwitchCase(SwitchCases[0]);
1265 SwitchCases.erase(SwitchCases.begin());
1266 return;
1267 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001268
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001269 // Okay, we decided not to do this, remove any inserted MBB's and clear
1270 // SwitchCases.
1271 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001272 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001273
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001274 SwitchCases.clear();
1275 }
1276 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001279 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001280 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001282 // Use visitSwitchCase to actually insert the fast branch sequence for this
1283 // cond branch.
1284 visitSwitchCase(CB);
1285}
1286
1287/// visitSwitchCase - Emits the necessary code to represent a single node in
1288/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001289void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001290 SDValue Cond;
1291 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001292 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001293
1294 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001295 if (CB.CmpMHS == NULL) {
1296 // Fold "(X == true)" to X and "(X == false)" to !X to
1297 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001298 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001299 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001300 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001301 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001302 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001304 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001305 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001306 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001307 } else {
1308 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1309
Anton Korobeynikov23218582008-12-23 22:25:27 +00001310 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1311 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001312
1313 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001314 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315
1316 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001317 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001318 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001319 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001320 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001321 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001322 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001323 DAG.getConstant(High-Low, VT), ISD::SETULE);
1324 }
1325 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001326
Bill Wendling87710f02009-12-21 23:47:40 +00001327 if (DisableScheduling)
1328 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330 // Update successor info
1331 CurMBB->addSuccessor(CB.TrueBB);
1332 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001333
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 // Set NextBlock to be the MBB immediately after the current one, if any.
1335 // This is used to avoid emitting unnecessary branches to the next block.
1336 MachineBasicBlock *NextBlock = 0;
1337 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001338 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001339 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001340
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001341 // If the lhs block is the next block, invert the condition so that we can
1342 // fall through to the lhs instead of the rhs block.
1343 if (CB.TrueBB == NextBlock) {
1344 std::swap(CB.TrueBB, CB.FalseBB);
1345 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001346 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001347
1348 if (DisableScheduling)
1349 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001350 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001351
Dale Johannesenf5d97892009-02-04 01:48:28 +00001352 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001353 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001354 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001355
Bill Wendling87710f02009-12-21 23:47:40 +00001356 if (DisableScheduling)
1357 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1358
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001359 // If the branch was constant folded, fix up the CFG.
1360 if (BrCond.getOpcode() == ISD::BR) {
1361 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001362 } else {
1363 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001364 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001366
Bill Wendling87710f02009-12-21 23:47:40 +00001367 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001368 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1369 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001370
1371 if (DisableScheduling)
1372 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1373 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001374 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001375
1376 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377}
1378
1379/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001380void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001381 // Emit the code for the jump table
1382 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001383 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001384 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1385 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001387 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1388 MVT::Other, Index.getValue(1),
1389 Table, Index);
1390 DAG.setRoot(BrJumpTable);
1391
Bill Wendling87710f02009-12-21 23:47:40 +00001392 if (DisableScheduling) {
1393 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1394 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001395 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001396 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001397}
1398
1399/// visitJumpTableHeader - This function emits necessary code to produce index
1400/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001401void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1402 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001403 // Subtract the lowest switch case value from the value being switched on and
1404 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001405 // difference between smallest and largest cases.
1406 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001407 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001408 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001409 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001410
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001411 // The SDNode we just created, which holds the value being switched on minus
1412 // the the smallest case value, needs to be copied to a virtual register so it
1413 // can be used as an index into the jump table in a subsequent basic block.
1414 // This value may be smaller or larger than the target's pointer type, and
1415 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001416 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001417
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001418 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001419 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1420 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421 JT.Reg = JumpTableReg;
1422
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001423 // Emit the range check for the jump table, and branch to the default block
1424 // for the switch statement if the value being switched on exceeds the largest
1425 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001426 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001427 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001428 DAG.getConstant(JTH.Last-JTH.First,VT),
1429 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430
Bill Wendling87710f02009-12-21 23:47:40 +00001431 if (DisableScheduling) {
1432 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1433 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1434 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1435 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1436 }
1437
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001438 // Set NextBlock to be the MBB immediately after the current one, if any.
1439 // This is used to avoid emitting unnecessary branches to the next block.
1440 MachineBasicBlock *NextBlock = 0;
1441 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001442
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001443 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001444 NextBlock = BBI;
1445
Dale Johannesen66978ee2009-01-31 02:22:37 +00001446 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001447 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001448 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001449
Bill Wendling87710f02009-12-21 23:47:40 +00001450 if (DisableScheduling)
1451 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1452
1453 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001454 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1455 DAG.getBasicBlock(JT.MBB));
1456
Bill Wendling87710f02009-12-21 23:47:40 +00001457 if (DisableScheduling)
1458 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1459 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001460
Bill Wendling87710f02009-12-21 23:47:40 +00001461 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001462}
1463
1464/// visitBitTestHeader - This function emits necessary code to produce value
1465/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001466void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001467 // Subtract the minimum value
1468 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001469 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001470 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001471 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001472
1473 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001474 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001475 TLI.getSetCCResultType(Sub.getValueType()),
1476 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001477 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001478
Bill Wendling87710f02009-12-21 23:47:40 +00001479 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1480 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001481
Duncan Sands92abc622009-01-31 15:50:11 +00001482 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001483 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1484 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001485
Bill Wendling87710f02009-12-21 23:47:40 +00001486 if (DisableScheduling) {
1487 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1488 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1489 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1490 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1491 }
1492
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001493 // Set NextBlock to be the MBB immediately after the current one, if any.
1494 // This is used to avoid emitting unnecessary branches to the next block.
1495 MachineBasicBlock *NextBlock = 0;
1496 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001497 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001498 NextBlock = BBI;
1499
1500 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1501
1502 CurMBB->addSuccessor(B.Default);
1503 CurMBB->addSuccessor(MBB);
1504
Dale Johannesen66978ee2009-01-31 02:22:37 +00001505 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001506 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001507 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001508
Bill Wendling87710f02009-12-21 23:47:40 +00001509 if (DisableScheduling)
1510 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1511
1512 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001513 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1514 DAG.getBasicBlock(MBB));
1515
Bill Wendling87710f02009-12-21 23:47:40 +00001516 if (DisableScheduling)
1517 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1518 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001519
Bill Wendling87710f02009-12-21 23:47:40 +00001520 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001521}
1522
1523/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001524void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1525 unsigned Reg,
1526 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001527 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001528 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001529 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001530 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001531 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001532 DAG.getConstant(1, TLI.getPointerTy()),
1533 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001534
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001535 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001536 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001537 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001538 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001539 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1540 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001541 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001542 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001543
Bill Wendling87710f02009-12-21 23:47:40 +00001544 if (DisableScheduling) {
1545 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1546 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1547 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1548 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1549 }
1550
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 CurMBB->addSuccessor(B.TargetBB);
1552 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001553
Dale Johannesen66978ee2009-01-31 02:22:37 +00001554 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001555 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001556 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001557
Bill Wendling87710f02009-12-21 23:47:40 +00001558 if (DisableScheduling)
1559 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1560
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001561 // Set NextBlock to be the MBB immediately after the current one, if any.
1562 // This is used to avoid emitting unnecessary branches to the next block.
1563 MachineBasicBlock *NextBlock = 0;
1564 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001565 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001566 NextBlock = BBI;
1567
Bill Wendling87710f02009-12-21 23:47:40 +00001568 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001569 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1570 DAG.getBasicBlock(NextMBB));
1571
Bill Wendling87710f02009-12-21 23:47:40 +00001572 if (DisableScheduling)
1573 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1574 }
Bill Wendling0777e922009-12-21 21:59:52 +00001575
Bill Wendling87710f02009-12-21 23:47:40 +00001576 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001577}
1578
Dan Gohman2048b852009-11-23 18:04:58 +00001579void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 // Retrieve successors.
1581 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1582 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1583
Gabor Greifb67e6b32009-01-15 11:10:44 +00001584 const Value *Callee(I.getCalledValue());
1585 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001586 visitInlineAsm(&I);
1587 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001588 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001589
1590 // If the value of the invoke is used outside of its defining block, make it
1591 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001592 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593
1594 // Update successor info
1595 CurMBB->addSuccessor(Return);
1596 CurMBB->addSuccessor(LandingPad);
1597
1598 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001599 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1600 MVT::Other, getControlRoot(),
1601 DAG.getBasicBlock(Return));
1602 DAG.setRoot(Branch);
1603
1604 if (DisableScheduling)
1605 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001606}
1607
Dan Gohman2048b852009-11-23 18:04:58 +00001608void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609}
1610
1611/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1612/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001613bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1614 CaseRecVector& WorkList,
1615 Value* SV,
1616 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001617 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001619 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001620 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001621 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001622 return false;
1623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624 // Get the MachineFunction which holds the current MBB. This is used when
1625 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001626 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001627
1628 // Figure out which block is immediately after the current one.
1629 MachineBasicBlock *NextBlock = 0;
1630 MachineFunction::iterator BBI = CR.CaseBB;
1631
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001632 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001633 NextBlock = BBI;
1634
1635 // TODO: If any two of the cases has the same destination, and if one value
1636 // is the same as the other, but has one bit unset that the other has set,
1637 // use bit manipulation to do two compares at once. For example:
1638 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001639
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001640 // Rearrange the case blocks so that the last one falls through if possible.
1641 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1642 // The last case block won't fall through into 'NextBlock' if we emit the
1643 // branches in this order. See if rearranging a case value would help.
1644 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1645 if (I->BB == NextBlock) {
1646 std::swap(*I, BackCase);
1647 break;
1648 }
1649 }
1650 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001651
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001652 // Create a CaseBlock record representing a conditional branch to
1653 // the Case's target mbb if the value being switched on SV is equal
1654 // to C.
1655 MachineBasicBlock *CurBlock = CR.CaseBB;
1656 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1657 MachineBasicBlock *FallThrough;
1658 if (I != E-1) {
1659 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1660 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001661
1662 // Put SV in a virtual register to make it available from the new blocks.
1663 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001664 } else {
1665 // If the last case doesn't match, go to the default block.
1666 FallThrough = Default;
1667 }
1668
1669 Value *RHS, *LHS, *MHS;
1670 ISD::CondCode CC;
1671 if (I->High == I->Low) {
1672 // This is just small small case range :) containing exactly 1 case
1673 CC = ISD::SETEQ;
1674 LHS = SV; RHS = I->High; MHS = NULL;
1675 } else {
1676 CC = ISD::SETLE;
1677 LHS = I->Low; MHS = SV; RHS = I->High;
1678 }
1679 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681 // If emitting the first comparison, just call visitSwitchCase to emit the
1682 // code into the current block. Otherwise, push the CaseBlock onto the
1683 // vector to be later processed by SDISel, and insert the node's MBB
1684 // before the next MBB.
1685 if (CurBlock == CurMBB)
1686 visitSwitchCase(CB);
1687 else
1688 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001689
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001690 CurBlock = FallThrough;
1691 }
1692
1693 return true;
1694}
1695
1696static inline bool areJTsAllowed(const TargetLowering &TLI) {
1697 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001698 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1699 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001700}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001701
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001702static APInt ComputeRange(const APInt &First, const APInt &Last) {
1703 APInt LastExt(Last), FirstExt(First);
1704 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1705 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1706 return (LastExt - FirstExt + 1ULL);
1707}
1708
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001709/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001710bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1711 CaseRecVector& WorkList,
1712 Value* SV,
1713 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 Case& FrontCase = *CR.Range.first;
1715 Case& BackCase = *(CR.Range.second-1);
1716
Chris Lattnere880efe2009-11-07 07:50:34 +00001717 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1718 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719
Chris Lattnere880efe2009-11-07 07:50:34 +00001720 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001721 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1722 I!=E; ++I)
1723 TSize += I->size();
1724
Chris Lattnere880efe2009-11-07 07:50:34 +00001725 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001726 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001727
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001728 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001729 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001730 if (Density < 0.4)
1731 return false;
1732
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001733 DEBUG(errs() << "Lowering jump table\n"
1734 << "First entry: " << First << ". Last entry: " << Last << '\n'
1735 << "Range: " << Range
1736 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737
1738 // Get the MachineFunction which holds the current MBB. This is used when
1739 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001740 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741
1742 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001743 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001744 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001745
1746 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1747
1748 // Create a new basic block to hold the code for loading the address
1749 // of the jump table, and jumping to it. Update successor information;
1750 // we will either branch to the default case for the switch, or the jump
1751 // table.
1752 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1753 CurMF->insert(BBI, JumpTableBB);
1754 CR.CaseBB->addSuccessor(Default);
1755 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001756
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001757 // Build a vector of destination BBs, corresponding to each target
1758 // of the jump table. If the value of the jump table slot corresponds to
1759 // a case statement, push the case's BB onto the vector, otherwise, push
1760 // the default BB.
1761 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001762 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001763 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001764 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1765 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1766
1767 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001768 DestBBs.push_back(I->BB);
1769 if (TEI==High)
1770 ++I;
1771 } else {
1772 DestBBs.push_back(Default);
1773 }
1774 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001775
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001776 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001777 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1778 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001779 E = DestBBs.end(); I != E; ++I) {
1780 if (!SuccsHandled[(*I)->getNumber()]) {
1781 SuccsHandled[(*I)->getNumber()] = true;
1782 JumpTableBB->addSuccessor(*I);
1783 }
1784 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001785
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001786 // Create a jump table index for this jump table, or return an existing
1787 // one.
1788 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001789
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001790 // Set the jump table information so that we can codegen it as a second
1791 // MachineBasicBlock
1792 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1793 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1794 if (CR.CaseBB == CurMBB)
1795 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001796
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001797 JTCases.push_back(JumpTableBlock(JTH, JT));
1798
1799 return true;
1800}
1801
1802/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1803/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001804bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1805 CaseRecVector& WorkList,
1806 Value* SV,
1807 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001808 // Get the MachineFunction which holds the current MBB. This is used when
1809 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001810 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001811
1812 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001813 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001814 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001815
1816 Case& FrontCase = *CR.Range.first;
1817 Case& BackCase = *(CR.Range.second-1);
1818 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1819
1820 // Size is the number of Cases represented by this range.
1821 unsigned Size = CR.Range.second - CR.Range.first;
1822
Chris Lattnere880efe2009-11-07 07:50:34 +00001823 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1824 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001825 double FMetric = 0;
1826 CaseItr Pivot = CR.Range.first + Size/2;
1827
1828 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1829 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001830 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001831 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1832 I!=E; ++I)
1833 TSize += I->size();
1834
Chris Lattnere880efe2009-11-07 07:50:34 +00001835 APInt LSize = FrontCase.size();
1836 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001837 DEBUG(errs() << "Selecting best pivot: \n"
1838 << "First: " << First << ", Last: " << Last <<'\n'
1839 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001840 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1841 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001842 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1843 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001844 APInt Range = ComputeRange(LEnd, RBegin);
1845 assert((Range - 2ULL).isNonNegative() &&
1846 "Invalid case distance");
Chris Lattnere880efe2009-11-07 07:50:34 +00001847 double LDensity = (double)LSize.roundToDouble() /
1848 (LEnd - First + 1ULL).roundToDouble();
1849 double RDensity = (double)RSize.roundToDouble() /
1850 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001851 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001852 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001853 DEBUG(errs() <<"=>Step\n"
1854 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1855 << "LDensity: " << LDensity
1856 << ", RDensity: " << RDensity << '\n'
1857 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001858 if (FMetric < Metric) {
1859 Pivot = J;
1860 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001861 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001862 }
1863
1864 LSize += J->size();
1865 RSize -= J->size();
1866 }
1867 if (areJTsAllowed(TLI)) {
1868 // If our case is dense we *really* should handle it earlier!
1869 assert((FMetric > 0) && "Should handle dense range earlier!");
1870 } else {
1871 Pivot = CR.Range.first + Size/2;
1872 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001873
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001874 CaseRange LHSR(CR.Range.first, Pivot);
1875 CaseRange RHSR(Pivot, CR.Range.second);
1876 Constant *C = Pivot->Low;
1877 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001878
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001879 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001880 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001881 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001882 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001883 // Pivot's Value, then we can branch directly to the LHS's Target,
1884 // rather than creating a leaf node for it.
1885 if ((LHSR.second - LHSR.first) == 1 &&
1886 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001887 cast<ConstantInt>(C)->getValue() ==
1888 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001889 TrueBB = LHSR.first->BB;
1890 } else {
1891 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1892 CurMF->insert(BBI, TrueBB);
1893 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001894
1895 // Put SV in a virtual register to make it available from the new blocks.
1896 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001897 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001898
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001899 // Similar to the optimization above, if the Value being switched on is
1900 // known to be less than the Constant CR.LT, and the current Case Value
1901 // is CR.LT - 1, then we can branch directly to the target block for
1902 // the current Case Value, rather than emitting a RHS leaf node for it.
1903 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001904 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1905 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001906 FalseBB = RHSR.first->BB;
1907 } else {
1908 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1909 CurMF->insert(BBI, FalseBB);
1910 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001911
1912 // Put SV in a virtual register to make it available from the new blocks.
1913 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001914 }
1915
1916 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001917 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001918 // Otherwise, branch to LHS.
1919 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1920
1921 if (CR.CaseBB == CurMBB)
1922 visitSwitchCase(CB);
1923 else
1924 SwitchCases.push_back(CB);
1925
1926 return true;
1927}
1928
1929/// handleBitTestsSwitchCase - if current case range has few destination and
1930/// range span less, than machine word bitwidth, encode case range into series
1931/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001932bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1933 CaseRecVector& WorkList,
1934 Value* SV,
1935 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001936 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001937 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001938
1939 Case& FrontCase = *CR.Range.first;
1940 Case& BackCase = *(CR.Range.second-1);
1941
1942 // Get the MachineFunction which holds the current MBB. This is used when
1943 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001944 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001946 // If target does not have legal shift left, do not emit bit tests at all.
1947 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1948 return false;
1949
Anton Korobeynikov23218582008-12-23 22:25:27 +00001950 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001951 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1952 I!=E; ++I) {
1953 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001954 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001955 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001956
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001957 // Count unique destinations
1958 SmallSet<MachineBasicBlock*, 4> Dests;
1959 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1960 Dests.insert(I->BB);
1961 if (Dests.size() > 3)
1962 // Don't bother the code below, if there are too much unique destinations
1963 return false;
1964 }
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001965 DEBUG(errs() << "Total number of unique destinations: " << Dests.size() << '\n'
1966 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001967
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001968 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001969 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1970 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001971 APInt cmpRange = maxValue - minValue;
1972
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001973 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1974 << "Low bound: " << minValue << '\n'
1975 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001976
1977 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001978 (!(Dests.size() == 1 && numCmps >= 3) &&
1979 !(Dests.size() == 2 && numCmps >= 5) &&
1980 !(Dests.size() >= 3 && numCmps >= 6)))
1981 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001982
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001983 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001984 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1985
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001986 // Optimize the case where all the case values fit in a
1987 // word without having to subtract minValue. In this case,
1988 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001989 if (minValue.isNonNegative() &&
1990 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1991 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001993 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001994 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001995
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001996 CaseBitsVector CasesBits;
1997 unsigned i, count = 0;
1998
1999 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2000 MachineBasicBlock* Dest = I->BB;
2001 for (i = 0; i < count; ++i)
2002 if (Dest == CasesBits[i].BB)
2003 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002005 if (i == count) {
2006 assert((count < 3) && "Too much destinations to test!");
2007 CasesBits.push_back(CaseBits(0, Dest, 0));
2008 count++;
2009 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002010
2011 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2012 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2013
2014 uint64_t lo = (lowValue - lowBound).getZExtValue();
2015 uint64_t hi = (highValue - lowBound).getZExtValue();
2016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 for (uint64_t j = lo; j <= hi; j++) {
2018 CasesBits[i].Mask |= 1ULL << j;
2019 CasesBits[i].Bits++;
2020 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002021
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002022 }
2023 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002024
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 BitTestInfo BTC;
2026
2027 // Figure out which block is immediately after the current one.
2028 MachineFunction::iterator BBI = CR.CaseBB;
2029 ++BBI;
2030
2031 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2032
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002033 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002034 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002035 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
2036 << ", Bits: " << CasesBits[i].Bits
2037 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002038
2039 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2040 CurMF->insert(BBI, CaseBB);
2041 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2042 CaseBB,
2043 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002044
2045 // Put SV in a virtual register to make it available from the new blocks.
2046 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002047 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002048
2049 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002050 -1U, (CR.CaseBB == CurMBB),
2051 CR.CaseBB, Default, BTC);
2052
2053 if (CR.CaseBB == CurMBB)
2054 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002056 BitTestCases.push_back(BTB);
2057
2058 return true;
2059}
2060
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002061/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002062size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2063 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002064 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002065
2066 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002067 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002068 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2069 Cases.push_back(Case(SI.getSuccessorValue(i),
2070 SI.getSuccessorValue(i),
2071 SMBB));
2072 }
2073 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2074
2075 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002076 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002077 // Must recompute end() each iteration because it may be
2078 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002079 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2080 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2081 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002082 MachineBasicBlock* nextBB = J->BB;
2083 MachineBasicBlock* currentBB = I->BB;
2084
2085 // If the two neighboring cases go to the same destination, merge them
2086 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002087 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002088 I->High = J->High;
2089 J = Cases.erase(J);
2090 } else {
2091 I = J++;
2092 }
2093 }
2094
2095 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2096 if (I->Low != I->High)
2097 // A range counts double, since it requires two compares.
2098 ++numCmps;
2099 }
2100
2101 return numCmps;
2102}
2103
Dan Gohman2048b852009-11-23 18:04:58 +00002104void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002105 // Figure out which block is immediately after the current one.
2106 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002107 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2108
2109 // If there is only the default destination, branch to it if it is not the
2110 // next basic block. Otherwise, just fall through.
2111 if (SI.getNumOperands() == 2) {
2112 // Update machine-CFG edges.
2113
2114 // If this is not a fall-through branch, emit the branch.
2115 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002116 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002117 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002118 MVT::Other, getControlRoot(),
2119 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002120 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002121
2122 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002123 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002124 }
2125
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002126 return;
2127 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002128
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002129 // If there are any non-default case statements, create a vector of Cases
2130 // representing each one, and sort the vector so that we can efficiently
2131 // create a binary search tree from them.
2132 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002133 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002134 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2135 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002136 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002137
2138 // Get the Value to be switched on and default basic blocks, which will be
2139 // inserted into CaseBlock records, representing basic blocks in the binary
2140 // search tree.
2141 Value *SV = SI.getOperand(0);
2142
2143 // Push the initial CaseRec onto the worklist
2144 CaseRecVector WorkList;
2145 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2146
2147 while (!WorkList.empty()) {
2148 // Grab a record representing a case range to process off the worklist
2149 CaseRec CR = WorkList.back();
2150 WorkList.pop_back();
2151
2152 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2153 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002155 // If the range has few cases (two or less) emit a series of specific
2156 // tests.
2157 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2158 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002159
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002160 // If the switch has more than 5 blocks, and at least 40% dense, and the
2161 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002162 // lowering the switch to a binary tree of conditional branches.
2163 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2164 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002165
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2167 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2168 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2169 }
2170}
2171
Dan Gohman2048b852009-11-23 18:04:58 +00002172void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002173 // Update machine-CFG edges.
2174 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2175 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2176
Bill Wendling49fcff82009-12-21 22:30:11 +00002177 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2178 MVT::Other, getControlRoot(),
2179 getValue(I.getAddress()));
2180 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002181
Bill Wendling49fcff82009-12-21 22:30:11 +00002182 if (DisableScheduling)
2183 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2184}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002185
Dan Gohman2048b852009-11-23 18:04:58 +00002186void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187 // -0.0 - X --> fneg
2188 const Type *Ty = I.getType();
2189 if (isa<VectorType>(Ty)) {
2190 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2191 const VectorType *DestTy = cast<VectorType>(I.getType());
2192 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002193 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002194 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002195 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002196 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002197 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002198 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2199 Op2.getValueType(), Op2);
2200 setValue(&I, Res);
2201
2202 if (DisableScheduling)
2203 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205 return;
2206 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002207 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002209
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002210 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002211 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002212 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002213 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2214 Op2.getValueType(), Op2);
2215 setValue(&I, Res);
2216
2217 if (DisableScheduling)
2218 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2219
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002220 return;
2221 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002223 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224}
2225
Dan Gohman2048b852009-11-23 18:04:58 +00002226void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002227 SDValue Op1 = getValue(I.getOperand(0));
2228 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002229 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2230 Op1.getValueType(), Op1, Op2);
2231 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002232
Bill Wendling49fcff82009-12-21 22:30:11 +00002233 if (DisableScheduling)
2234 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002235}
2236
Dan Gohman2048b852009-11-23 18:04:58 +00002237void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002238 SDValue Op1 = getValue(I.getOperand(0));
2239 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002240 if (!isa<VectorType>(I.getType()) &&
2241 Op2.getValueType() != TLI.getShiftAmountTy()) {
2242 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002243 EVT PTy = TLI.getPointerTy();
2244 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002245 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002246 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2247 TLI.getShiftAmountTy(), Op2);
2248 // If the operand is larger than the shift count type but the shift
2249 // count type has enough bits to represent any shift value, truncate
2250 // it now. This is a common case and it exposes the truncate to
2251 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002252 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002253 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2254 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2255 TLI.getShiftAmountTy(), Op2);
2256 // Otherwise we'll need to temporarily settle for some other
2257 // convenient type; type legalization will make adjustments as
2258 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002259 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002260 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002261 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002262 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002263 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002264 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002265 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002266
Bill Wendling49fcff82009-12-21 22:30:11 +00002267 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2268 Op1.getValueType(), Op1, Op2);
2269 setValue(&I, Res);
2270
Bill Wendling87710f02009-12-21 23:47:40 +00002271 if (DisableScheduling) {
2272 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2273 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002274 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002275 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002276}
2277
Dan Gohman2048b852009-11-23 18:04:58 +00002278void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002279 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2280 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2281 predicate = IC->getPredicate();
2282 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2283 predicate = ICmpInst::Predicate(IC->getPredicate());
2284 SDValue Op1 = getValue(I.getOperand(0));
2285 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002286 ISD::CondCode Opcode = getICmpCondCode(predicate);
Chris Lattner9800e842009-07-07 22:41:32 +00002287
Owen Andersone50ed302009-08-10 22:56:29 +00002288 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002289 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2290 setValue(&I, Res);
2291
2292 if (DisableScheduling)
2293 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002294}
2295
Dan Gohman2048b852009-11-23 18:04:58 +00002296void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002297 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2298 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2299 predicate = FC->getPredicate();
2300 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2301 predicate = FCmpInst::Predicate(FC->getPredicate());
2302 SDValue Op1 = getValue(I.getOperand(0));
2303 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002304 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002305 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002306 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2307 setValue(&I, Res);
2308
2309 if (DisableScheduling)
2310 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002311}
2312
Dan Gohman2048b852009-11-23 18:04:58 +00002313void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002314 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002315 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2316 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002317 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002318
Bill Wendling49fcff82009-12-21 22:30:11 +00002319 SmallVector<SDValue, 4> Values(NumValues);
2320 SDValue Cond = getValue(I.getOperand(0));
2321 SDValue TrueVal = getValue(I.getOperand(1));
2322 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002323
Bill Wendling49fcff82009-12-21 22:30:11 +00002324 for (unsigned i = 0; i != NumValues; ++i) {
2325 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2326 TrueVal.getNode()->getValueType(i), Cond,
2327 SDValue(TrueVal.getNode(),
2328 TrueVal.getResNo() + i),
2329 SDValue(FalseVal.getNode(),
2330 FalseVal.getResNo() + i));
2331
2332 if (DisableScheduling)
2333 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002334 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002335
Bill Wendling49fcff82009-12-21 22:30:11 +00002336 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2337 DAG.getVTList(&ValueVTs[0], NumValues),
2338 &Values[0], NumValues);
2339 setValue(&I, Res);
2340
2341 if (DisableScheduling)
2342 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2343}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002344
Dan Gohman2048b852009-11-23 18:04:58 +00002345void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002346 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2347 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002348 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002349 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2350 setValue(&I, Res);
2351
2352 if (DisableScheduling)
2353 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002354}
2355
Dan Gohman2048b852009-11-23 18:04:58 +00002356void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002357 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2358 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2359 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002360 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002361 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2362 setValue(&I, Res);
2363
2364 if (DisableScheduling)
2365 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002366}
2367
Dan Gohman2048b852009-11-23 18:04:58 +00002368void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002369 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2370 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2371 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002372 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002373 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2374 setValue(&I, Res);
2375
2376 if (DisableScheduling)
2377 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002378}
2379
Dan Gohman2048b852009-11-23 18:04:58 +00002380void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002381 // FPTrunc is never a no-op cast, no need to check
2382 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002383 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002384 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2385 DestVT, N, DAG.getIntPtrConstant(0));
2386 setValue(&I, Res);
2387
2388 if (DisableScheduling)
2389 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002390}
2391
Dan Gohman2048b852009-11-23 18:04:58 +00002392void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002393 // FPTrunc is never a no-op cast, no need to check
2394 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002395 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002396 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2397 setValue(&I, Res);
2398
2399 if (DisableScheduling)
2400 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002401}
2402
Dan Gohman2048b852009-11-23 18:04:58 +00002403void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002404 // FPToUI is never a no-op cast, no need to check
2405 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002406 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002407 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2408 setValue(&I, Res);
2409
2410 if (DisableScheduling)
2411 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002412}
2413
Dan Gohman2048b852009-11-23 18:04:58 +00002414void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002415 // FPToSI is never a no-op cast, no need to check
2416 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002417 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002418 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2419 setValue(&I, Res);
2420
2421 if (DisableScheduling)
2422 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002423}
2424
Dan Gohman2048b852009-11-23 18:04:58 +00002425void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002426 // UIToFP is never a no-op cast, no need to check
2427 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002428 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002429 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2430 setValue(&I, Res);
2431
2432 if (DisableScheduling)
2433 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002434}
2435
Dan Gohman2048b852009-11-23 18:04:58 +00002436void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002437 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002438 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002439 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002440 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2441 setValue(&I, Res);
2442
2443 if (DisableScheduling)
2444 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002445}
2446
Dan Gohman2048b852009-11-23 18:04:58 +00002447void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002448 // What to do depends on the size of the integer and the size of the pointer.
2449 // We can either truncate, zero extend, or no-op, accordingly.
2450 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002451 EVT SrcVT = N.getValueType();
2452 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002453 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2454 setValue(&I, Res);
2455
2456 if (DisableScheduling)
2457 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002458}
2459
Dan Gohman2048b852009-11-23 18:04:58 +00002460void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002461 // What to do depends on the size of the integer and the size of the pointer.
2462 // We can either truncate, zero extend, or no-op, accordingly.
2463 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002464 EVT SrcVT = N.getValueType();
2465 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002466 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2467 setValue(&I, Res);
2468
2469 if (DisableScheduling)
2470 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002471}
2472
Dan Gohman2048b852009-11-23 18:04:58 +00002473void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002474 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002475 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002476
Bill Wendling49fcff82009-12-21 22:30:11 +00002477 // BitCast assures us that source and destination are the same size so this is
2478 // either a BIT_CONVERT or a no-op.
2479 if (DestVT != N.getValueType()) {
2480 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2481 DestVT, N); // convert types.
2482 setValue(&I, Res);
2483
2484 if (DisableScheduling)
2485 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2486 } else {
2487 setValue(&I, N); // noop cast.
2488 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002489}
2490
Dan Gohman2048b852009-11-23 18:04:58 +00002491void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492 SDValue InVec = getValue(I.getOperand(0));
2493 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002494 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002495 TLI.getPointerTy(),
2496 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002497 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2498 TLI.getValueType(I.getType()),
2499 InVec, InVal, InIdx);
2500 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002501
Bill Wendling87710f02009-12-21 23:47:40 +00002502 if (DisableScheduling) {
2503 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002504 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002505 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002506}
2507
Dan Gohman2048b852009-11-23 18:04:58 +00002508void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002509 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002510 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002511 TLI.getPointerTy(),
2512 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002513 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2514 TLI.getValueType(I.getType()), InVec, InIdx);
2515 setValue(&I, Res);
2516
Bill Wendling87710f02009-12-21 23:47:40 +00002517 if (DisableScheduling) {
2518 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002519 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002520 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002521}
2522
Mon P Wangaeb06d22008-11-10 04:46:22 +00002523
2524// Utility for visitShuffleVector - Returns true if the mask is mask starting
2525// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002526static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2527 unsigned MaskNumElts = Mask.size();
2528 for (unsigned i = 0; i != MaskNumElts; ++i)
2529 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002530 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002531 return true;
2532}
2533
Dan Gohman2048b852009-11-23 18:04:58 +00002534void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002535 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002536 SDValue Src1 = getValue(I.getOperand(0));
2537 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002538
Nate Begeman9008ca62009-04-27 18:41:29 +00002539 // Convert the ConstantVector mask operand into an array of ints, with -1
2540 // representing undef values.
2541 SmallVector<Constant*, 8> MaskElts;
Owen Anderson001dbfe2009-07-16 18:04:31 +00002542 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
2543 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002544 unsigned MaskNumElts = MaskElts.size();
2545 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002546 if (isa<UndefValue>(MaskElts[i]))
2547 Mask.push_back(-1);
2548 else
2549 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2550 }
2551
Owen Andersone50ed302009-08-10 22:56:29 +00002552 EVT VT = TLI.getValueType(I.getType());
2553 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002554 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002555
Mon P Wangc7849c22008-11-16 05:06:27 +00002556 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002557 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2558 &Mask[0]);
2559 setValue(&I, Res);
2560
2561 if (DisableScheduling)
2562 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2563
Mon P Wangaeb06d22008-11-10 04:46:22 +00002564 return;
2565 }
2566
2567 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002568 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2569 // Mask is longer than the source vectors and is a multiple of the source
2570 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002571 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002572 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2573 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002574 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2575 VT, Src1, Src2);
2576 setValue(&I, Res);
2577
2578 if (DisableScheduling)
2579 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2580
Mon P Wangaeb06d22008-11-10 04:46:22 +00002581 return;
2582 }
2583
Mon P Wangc7849c22008-11-16 05:06:27 +00002584 // Pad both vectors with undefs to make them the same length as the mask.
2585 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002586 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2587 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002588 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002589
Nate Begeman9008ca62009-04-27 18:41:29 +00002590 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2591 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002592 MOps1[0] = Src1;
2593 MOps2[0] = Src2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002594
2595 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2596 getCurDebugLoc(), VT,
2597 &MOps1[0], NumConcat);
2598 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2599 getCurDebugLoc(), VT,
2600 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002601
Mon P Wangaeb06d22008-11-10 04:46:22 +00002602 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002603 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002604 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002605 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002606 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002607 MappedOps.push_back(Idx);
2608 else
2609 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002610 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002611
2612 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2613 &MappedOps[0]);
2614 setValue(&I, Res);
2615
Bill Wendlinge1a90422009-12-21 23:10:19 +00002616 if (DisableScheduling) {
2617 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2618 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002619 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002620 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002621
Mon P Wangaeb06d22008-11-10 04:46:22 +00002622 return;
2623 }
2624
Mon P Wangc7849c22008-11-16 05:06:27 +00002625 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002626 // Analyze the access pattern of the vector to see if we can extract
2627 // two subvectors and do the shuffle. The analysis is done by calculating
2628 // the range of elements the mask access on both vectors.
2629 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2630 int MaxRange[2] = {-1, -1};
2631
Nate Begeman5a5ca152009-04-29 05:20:52 +00002632 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002633 int Idx = Mask[i];
2634 int Input = 0;
2635 if (Idx < 0)
2636 continue;
2637
Nate Begeman5a5ca152009-04-29 05:20:52 +00002638 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002639 Input = 1;
2640 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002641 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002642 if (Idx > MaxRange[Input])
2643 MaxRange[Input] = Idx;
2644 if (Idx < MinRange[Input])
2645 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002646 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002647
Mon P Wangc7849c22008-11-16 05:06:27 +00002648 // Check if the access is smaller than the vector size and can we find
2649 // a reasonable extract index.
Mon P Wang230e4fa2008-11-21 04:25:21 +00002650 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002651 int StartIdx[2]; // StartIdx to extract from
2652 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002653 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002654 RangeUse[Input] = 0; // Unused
2655 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002656 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002657 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002658 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002659 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002660 RangeUse[Input] = 1; // Extract from beginning of the vector
2661 StartIdx[Input] = 0;
2662 } else {
2663 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002664 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002665 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002666 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002667 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002668 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002669 }
2670
Bill Wendling636e2582009-08-21 18:16:06 +00002671 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002672 SDValue Res = DAG.getUNDEF(VT);
2673 setValue(&I, Res); // Vectors are not used.
2674
2675 if (DisableScheduling)
2676 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2677
Mon P Wangc7849c22008-11-16 05:06:27 +00002678 return;
2679 }
2680 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2681 // Extract appropriate subvector and generate a vector shuffle
2682 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002683 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002684 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002685 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002686 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002687 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002688 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002689
2690 if (DisableScheduling)
2691 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002692 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002693
Mon P Wangc7849c22008-11-16 05:06:27 +00002694 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002695 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002696 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002697 int Idx = Mask[i];
2698 if (Idx < 0)
2699 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002700 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002701 MappedOps.push_back(Idx - StartIdx[0]);
2702 else
2703 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002704 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002705
2706 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2707 &MappedOps[0]);
2708 setValue(&I, Res);
2709
2710 if (DisableScheduling)
2711 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2712
Mon P Wangc7849c22008-11-16 05:06:27 +00002713 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002714 }
2715 }
2716
Mon P Wangc7849c22008-11-16 05:06:27 +00002717 // We can't use either concat vectors or extract subvectors so fall back to
2718 // replacing the shuffle with extract and build vector.
2719 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002720 EVT EltVT = VT.getVectorElementType();
2721 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002722 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002723 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002724 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002725 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002726 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002727 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002728 SDValue Res;
2729
Nate Begeman5a5ca152009-04-29 05:20:52 +00002730 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002731 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2732 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002733 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002734 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2735 EltVT, Src2,
2736 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2737
2738 Ops.push_back(Res);
2739
2740 if (DisableScheduling)
2741 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002742 }
2743 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002744
2745 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2746 VT, &Ops[0], Ops.size());
2747 setValue(&I, Res);
2748
2749 if (DisableScheduling)
2750 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002751}
2752
Dan Gohman2048b852009-11-23 18:04:58 +00002753void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002754 const Value *Op0 = I.getOperand(0);
2755 const Value *Op1 = I.getOperand(1);
2756 const Type *AggTy = I.getType();
2757 const Type *ValTy = Op1->getType();
2758 bool IntoUndef = isa<UndefValue>(Op0);
2759 bool FromUndef = isa<UndefValue>(Op1);
2760
2761 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2762 I.idx_begin(), I.idx_end());
2763
Owen Andersone50ed302009-08-10 22:56:29 +00002764 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002765 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002766 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002767 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2768
2769 unsigned NumAggValues = AggValueVTs.size();
2770 unsigned NumValValues = ValValueVTs.size();
2771 SmallVector<SDValue, 4> Values(NumAggValues);
2772
2773 SDValue Agg = getValue(Op0);
2774 SDValue Val = getValue(Op1);
2775 unsigned i = 0;
2776 // Copy the beginning value(s) from the original aggregate.
2777 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002778 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002779 SDValue(Agg.getNode(), Agg.getResNo() + i);
2780 // Copy values from the inserted value(s).
2781 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002782 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002783 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2784 // Copy remaining value(s) from the original aggregate.
2785 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002786 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002787 SDValue(Agg.getNode(), Agg.getResNo() + i);
2788
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002789 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2790 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2791 &Values[0], NumAggValues);
2792 setValue(&I, Res);
2793
2794 if (DisableScheduling)
2795 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796}
2797
Dan Gohman2048b852009-11-23 18:04:58 +00002798void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002799 const Value *Op0 = I.getOperand(0);
2800 const Type *AggTy = Op0->getType();
2801 const Type *ValTy = I.getType();
2802 bool OutOfUndef = isa<UndefValue>(Op0);
2803
2804 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2805 I.idx_begin(), I.idx_end());
2806
Owen Andersone50ed302009-08-10 22:56:29 +00002807 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002808 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2809
2810 unsigned NumValValues = ValValueVTs.size();
2811 SmallVector<SDValue, 4> Values(NumValValues);
2812
2813 SDValue Agg = getValue(Op0);
2814 // Copy out the selected value(s).
2815 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2816 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002817 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002818 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002819 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002820
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002821 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2822 DAG.getVTList(&ValValueVTs[0], NumValValues),
2823 &Values[0], NumValValues);
2824 setValue(&I, Res);
2825
2826 if (DisableScheduling)
2827 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002828}
2829
Dan Gohman2048b852009-11-23 18:04:58 +00002830void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002831 SDValue N = getValue(I.getOperand(0));
2832 const Type *Ty = I.getOperand(0)->getType();
2833
2834 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2835 OI != E; ++OI) {
2836 Value *Idx = *OI;
2837 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2838 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2839 if (Field) {
2840 // N = N + Offset
2841 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002842 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002843 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002844
2845 if (DisableScheduling)
2846 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002847 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002848
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002849 Ty = StTy->getElementType(Field);
2850 } else {
2851 Ty = cast<SequentialType>(Ty)->getElementType();
2852
2853 // If this is a constant subscript, handle it quickly.
2854 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2855 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002856 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002857 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002858 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002859 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002860 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002861 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002862 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2863 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002864 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002865 else
Evan Chengb1032a82009-02-09 20:54:38 +00002866 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002867
Dale Johannesen66978ee2009-01-31 02:22:37 +00002868 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002869 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002870
2871 if (DisableScheduling) {
2872 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2873 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2874 }
2875
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002876 continue;
2877 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002878
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002879 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002880 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2881 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002882 SDValue IdxN = getValue(Idx);
2883
2884 // If the index is smaller or larger than intptr_t, truncate or extend
2885 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002886 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002887
2888 // If this is a multiply by a power of two, turn it into a shl
2889 // immediately. This is a very common case.
2890 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002891 if (ElementSize.isPowerOf2()) {
2892 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002893 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002894 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002895 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002896 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002897 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002898 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002899 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002901
2902 if (DisableScheduling)
2903 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002904 }
2905
Scott Michelfdc40a02009-02-17 22:15:04 +00002906 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002907 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002908
2909 if (DisableScheduling)
2910 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002911 }
2912 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002914 setValue(&I, N);
2915}
2916
Dan Gohman2048b852009-11-23 18:04:58 +00002917void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002918 // If this is a fixed sized alloca in the entry block of the function,
2919 // allocate it statically on the stack.
2920 if (FuncInfo.StaticAllocaMap.count(&I))
2921 return; // getValue will auto-populate this.
2922
2923 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002924 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002925 unsigned Align =
2926 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2927 I.getAlignment());
2928
2929 SDValue AllocSize = getValue(I.getArraySize());
Chris Lattner0b18e592009-03-17 19:36:00 +00002930
2931 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2932 AllocSize,
2933 DAG.getConstant(TySize, AllocSize.getValueType()));
2934
Bill Wendling856ff412009-12-22 00:12:37 +00002935 if (DisableScheduling)
2936 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Chris Lattner0b18e592009-03-17 19:36:00 +00002937
Owen Andersone50ed302009-08-10 22:56:29 +00002938 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002939 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002940
Bill Wendling856ff412009-12-22 00:12:37 +00002941 if (DisableScheduling)
2942 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2943
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002944 // Handle alignment. If the requested alignment is less than or equal to
2945 // the stack alignment, ignore it. If the size is greater than or equal to
2946 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2947 unsigned StackAlign =
2948 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2949 if (Align <= StackAlign)
2950 Align = 0;
2951
2952 // Round the size of the allocation up to the stack alignment size
2953 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002954 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002955 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002956 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002957 if (DisableScheduling)
2958 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2959
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002960 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002961 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002962 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002963 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002964 if (DisableScheduling)
2965 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002966
2967 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002968 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002969 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002970 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002971 setValue(&I, DSA);
2972 DAG.setRoot(DSA.getValue(1));
2973
Bill Wendling856ff412009-12-22 00:12:37 +00002974 if (DisableScheduling)
2975 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
2976
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002977 // Inform the Frame Information that we have just allocated a variable-sized
2978 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002979 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002980}
2981
Dan Gohman2048b852009-11-23 18:04:58 +00002982void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002983 const Value *SV = I.getOperand(0);
2984 SDValue Ptr = getValue(SV);
2985
2986 const Type *Ty = I.getType();
2987 bool isVolatile = I.isVolatile();
2988 unsigned Alignment = I.getAlignment();
2989
Owen Andersone50ed302009-08-10 22:56:29 +00002990 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002991 SmallVector<uint64_t, 4> Offsets;
2992 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2993 unsigned NumValues = ValueVTs.size();
2994 if (NumValues == 0)
2995 return;
2996
2997 SDValue Root;
2998 bool ConstantMemory = false;
2999 if (I.isVolatile())
3000 // Serialize volatile loads with other side effects.
3001 Root = getRoot();
3002 else if (AA->pointsToConstantMemory(SV)) {
3003 // Do not serialize (non-volatile) loads of constant memory with anything.
3004 Root = DAG.getEntryNode();
3005 ConstantMemory = true;
3006 } else {
3007 // Do not serialize non-volatile loads against each other.
3008 Root = DAG.getRoot();
3009 }
3010
3011 SmallVector<SDValue, 4> Values(NumValues);
3012 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003013 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003014 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00003015 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
3016 PtrVT, Ptr,
3017 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003018 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00003019 A, SV, Offsets[i], isVolatile, Alignment);
3020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003021 Values[i] = L;
3022 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00003023
3024 if (DisableScheduling) {
3025 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
3026 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3027 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003028 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003030 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003031 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003032 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003033 if (isVolatile)
3034 DAG.setRoot(Chain);
3035 else
3036 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00003037
3038 if (DisableScheduling)
3039 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003040 }
3041
Bill Wendling856ff412009-12-22 00:12:37 +00003042 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
3043 DAG.getVTList(&ValueVTs[0], NumValues),
3044 &Values[0], NumValues);
3045 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003046
Bill Wendling856ff412009-12-22 00:12:37 +00003047 if (DisableScheduling)
3048 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
3049}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003050
Dan Gohman2048b852009-11-23 18:04:58 +00003051void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003052 Value *SrcV = I.getOperand(0);
3053 Value *PtrV = I.getOperand(1);
3054
Owen Andersone50ed302009-08-10 22:56:29 +00003055 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003056 SmallVector<uint64_t, 4> Offsets;
3057 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3058 unsigned NumValues = ValueVTs.size();
3059 if (NumValues == 0)
3060 return;
3061
3062 // Get the lowered operands. Note that we do this after
3063 // checking if NumResults is zero, because with zero results
3064 // the operands won't have values in the map.
3065 SDValue Src = getValue(SrcV);
3066 SDValue Ptr = getValue(PtrV);
3067
3068 SDValue Root = getRoot();
3069 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003070 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003071 bool isVolatile = I.isVolatile();
3072 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003073
3074 for (unsigned i = 0; i != NumValues; ++i) {
3075 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3076 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003077 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003078 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003079 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003080
Bill Wendling856ff412009-12-22 00:12:37 +00003081 if (DisableScheduling) {
3082 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3083 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3084 }
3085 }
3086
3087 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3088 MVT::Other, &Chains[0], NumValues);
3089 DAG.setRoot(Res);
3090
3091 if (DisableScheduling)
3092 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003093}
3094
3095/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3096/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003097void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3098 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003099 bool HasChain = !I.doesNotAccessMemory();
3100 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3101
3102 // Build the operand list.
3103 SmallVector<SDValue, 8> Ops;
3104 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3105 if (OnlyLoad) {
3106 // We don't need to serialize loads against other loads.
3107 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003108 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003109 Ops.push_back(getRoot());
3110 }
3111 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003112
3113 // Info is set by getTgtMemInstrinsic
3114 TargetLowering::IntrinsicInfo Info;
3115 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3116
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003117 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003118 if (!IsTgtIntrinsic)
3119 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003120
3121 // Add all operands of the call to the operand list.
3122 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3123 SDValue Op = getValue(I.getOperand(i));
3124 assert(TLI.isTypeLegal(Op.getValueType()) &&
3125 "Intrinsic uses a non-legal type?");
3126 Ops.push_back(Op);
3127 }
3128
Owen Andersone50ed302009-08-10 22:56:29 +00003129 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003130 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3131#ifndef NDEBUG
3132 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3133 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3134 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003135 }
Bob Wilson8d919552009-07-31 22:41:21 +00003136#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003138 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003139 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003140
Bob Wilson8d919552009-07-31 22:41:21 +00003141 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003142
3143 // Create the node.
3144 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003145 if (IsTgtIntrinsic) {
3146 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003147 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003148 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003149 Info.memVT, Info.ptrVal, Info.offset,
3150 Info.align, Info.vol,
3151 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003152 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003153 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003154 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003155 } else if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003156 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003157 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003158 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003159 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003160 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003161 }
3162
3163 if (DisableScheduling)
3164 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003165
3166 if (HasChain) {
3167 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3168 if (OnlyLoad)
3169 PendingLoads.push_back(Chain);
3170 else
3171 DAG.setRoot(Chain);
3172 }
Bill Wendling856ff412009-12-22 00:12:37 +00003173
Owen Anderson1d0be152009-08-13 21:58:54 +00003174 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003175 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003176 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003177 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003178
3179 if (DisableScheduling)
3180 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003181 }
Bill Wendling856ff412009-12-22 00:12:37 +00003182
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003183 setValue(&I, Result);
3184 }
3185}
3186
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003187/// GetSignificand - Get the significand and build it into a floating-point
3188/// number with exponent of 1:
3189///
3190/// Op = (Op & 0x007fffff) | 0x3f800000;
3191///
3192/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003193static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003194GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3196 DAG.getConstant(0x007fffff, MVT::i32));
3197 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3198 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003199 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3200
3201 if (DisableScheduling) {
3202 DAG.AssignOrdering(t1.getNode(), Order);
3203 DAG.AssignOrdering(t2.getNode(), Order);
3204 DAG.AssignOrdering(Res.getNode(), Order);
3205 }
3206
3207 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003208}
3209
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003210/// GetExponent - Get the exponent:
3211///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003212/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003213///
3214/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003215static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003216GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003217 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003218 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3219 DAG.getConstant(0x7f800000, MVT::i32));
3220 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003221 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3223 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003224 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3225
3226 if (DisableScheduling) {
3227 DAG.AssignOrdering(t0.getNode(), Order);
3228 DAG.AssignOrdering(t1.getNode(), Order);
3229 DAG.AssignOrdering(t2.getNode(), Order);
3230 DAG.AssignOrdering(Res.getNode(), Order);
3231 }
3232
3233 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003234}
3235
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003236/// getF32Constant - Get 32-bit floating point constant.
3237static SDValue
3238getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003239 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003240}
3241
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003242/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003243/// visitIntrinsicCall: I is a call instruction
3244/// Op is the associated NodeType for I
3245const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003246SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003247 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003248 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003249 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003250 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003251 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003252 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003253 getValue(I.getOperand(2)),
3254 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003255 setValue(&I, L);
3256 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003257
3258 if (DisableScheduling)
3259 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003261 return 0;
3262}
3263
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003264// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003265const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003266SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003267 SDValue Op1 = getValue(I.getOperand(1));
3268 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003269
Owen Anderson825b72b2009-08-11 20:47:22 +00003270 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003271 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003272
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003273 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003274
3275 if (DisableScheduling)
3276 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3277
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003278 return 0;
3279}
Bill Wendling74c37652008-12-09 22:08:41 +00003280
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003281/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3282/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003283void
Dan Gohman2048b852009-11-23 18:04:58 +00003284SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003285 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003286 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003287
Owen Anderson825b72b2009-08-11 20:47:22 +00003288 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003289 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3290 SDValue Op = getValue(I.getOperand(1));
3291
3292 // Put the exponent in the right bit position for later addition to the
3293 // final result:
3294 //
3295 // #define LOG2OFe 1.4426950f
3296 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003298 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003299 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003300
3301 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3303 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003304
Bill Wendling856ff412009-12-22 00:12:37 +00003305 if (DisableScheduling) {
3306 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3307 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3308 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3309 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3310 }
3311
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003312 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003313 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003314 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003315
Bill Wendling856ff412009-12-22 00:12:37 +00003316 if (DisableScheduling)
3317 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3318
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003319 if (LimitFloatPrecision <= 6) {
3320 // For floating-point precision of 6:
3321 //
3322 // TwoToFractionalPartOfX =
3323 // 0.997535578f +
3324 // (0.735607626f + 0.252464424f * x) * x;
3325 //
3326 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003328 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003329 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003330 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003331 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3332 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003333 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003335
3336 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003337 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003338 TwoToFracPartOfX, IntegerPartOfX);
3339
Owen Anderson825b72b2009-08-11 20:47:22 +00003340 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003341
3342 if (DisableScheduling) {
3343 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3344 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3345 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3346 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3347 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3348 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3349 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3350 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003351 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3352 // For floating-point precision of 12:
3353 //
3354 // TwoToFractionalPartOfX =
3355 // 0.999892986f +
3356 // (0.696457318f +
3357 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3358 //
3359 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003361 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003362 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003363 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003364 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3365 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003366 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3368 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003369 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003370 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003371
3372 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003374 TwoToFracPartOfX, IntegerPartOfX);
3375
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003377
3378 if (DisableScheduling) {
3379 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3380 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3381 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3382 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3383 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3384 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3385 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3386 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3387 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3388 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003389 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3390 // For floating-point precision of 18:
3391 //
3392 // TwoToFractionalPartOfX =
3393 // 0.999999982f +
3394 // (0.693148872f +
3395 // (0.240227044f +
3396 // (0.554906021e-1f +
3397 // (0.961591928e-2f +
3398 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3399 //
3400 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003401 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003402 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003403 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003404 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3406 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003407 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3409 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003410 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3412 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003413 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3415 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003416 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003417 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3418 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003419 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003420 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003421 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003422
3423 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003424 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003425 TwoToFracPartOfX, IntegerPartOfX);
3426
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003428
3429 if (DisableScheduling) {
3430 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3431 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3432 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3433 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3434 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3435 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3436 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3437 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3438 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3439 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3440 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3441 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3442 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3443 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3444 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3445 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003446 }
3447 } else {
3448 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003449 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003450 getValue(I.getOperand(1)).getValueType(),
3451 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003452 if (DisableScheduling)
3453 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003454 }
3455
Dale Johannesen59e577f2008-09-05 18:38:42 +00003456 setValue(&I, result);
3457}
3458
Bill Wendling39150252008-09-09 20:39:27 +00003459/// visitLog - Lower a log intrinsic. Handles the special sequences for
3460/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003461void
Dan Gohman2048b852009-11-23 18:04:58 +00003462SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003463 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003464 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003465
Owen Anderson825b72b2009-08-11 20:47:22 +00003466 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003467 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3468 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003469 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003470
Bill Wendling856ff412009-12-22 00:12:37 +00003471 if (DisableScheduling)
3472 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3473
Bill Wendling39150252008-09-09 20:39:27 +00003474 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003475 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003476 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003477 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003478
Bill Wendling856ff412009-12-22 00:12:37 +00003479 if (DisableScheduling)
3480 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3481
Bill Wendling39150252008-09-09 20:39:27 +00003482 // Get the significand and build it into a floating-point number with
3483 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003484 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003485
3486 if (LimitFloatPrecision <= 6) {
3487 // For floating-point precision of 6:
3488 //
3489 // LogofMantissa =
3490 // -1.1609546f +
3491 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003492 //
Bill Wendling39150252008-09-09 20:39:27 +00003493 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003494 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003495 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003496 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003497 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3499 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003500 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003501
Scott Michelfdc40a02009-02-17 22:15:04 +00003502 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003503 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003504
3505 if (DisableScheduling) {
3506 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3507 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3508 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3509 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3510 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3511 }
Bill Wendling39150252008-09-09 20:39:27 +00003512 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3513 // For floating-point precision of 12:
3514 //
3515 // LogOfMantissa =
3516 // -1.7417939f +
3517 // (2.8212026f +
3518 // (-1.4699568f +
3519 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3520 //
3521 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003523 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003525 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003526 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3527 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003528 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3530 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003531 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003532 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3533 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003534 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003535
Scott Michelfdc40a02009-02-17 22:15:04 +00003536 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003538
3539 if (DisableScheduling) {
3540 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3541 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3542 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3543 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3544 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3545 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3546 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3547 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3548 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3549 }
Bill Wendling39150252008-09-09 20:39:27 +00003550 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3551 // For floating-point precision of 18:
3552 //
3553 // LogOfMantissa =
3554 // -2.1072184f +
3555 // (4.2372794f +
3556 // (-3.7029485f +
3557 // (2.2781945f +
3558 // (-0.87823314f +
3559 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3560 //
3561 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003565 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003566 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3567 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003568 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3570 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003571 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3573 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003574 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003575 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3576 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003577 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003578 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3579 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003580 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003581
Scott Michelfdc40a02009-02-17 22:15:04 +00003582 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003584
3585 if (DisableScheduling) {
3586 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3587 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3588 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3589 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3590 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3591 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3592 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3593 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3594 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3595 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3596 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3597 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3598 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3599 }
Bill Wendling39150252008-09-09 20:39:27 +00003600 }
3601 } else {
3602 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003603 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003604 getValue(I.getOperand(1)).getValueType(),
3605 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003606
3607 if (DisableScheduling)
3608 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003609 }
3610
Dale Johannesen59e577f2008-09-05 18:38:42 +00003611 setValue(&I, result);
3612}
3613
Bill Wendling3eb59402008-09-09 00:28:24 +00003614/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3615/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003616void
Dan Gohman2048b852009-11-23 18:04:58 +00003617SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003618 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003619 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003620
Owen Anderson825b72b2009-08-11 20:47:22 +00003621 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003622 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3623 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003624 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003625
Bill Wendling856ff412009-12-22 00:12:37 +00003626 if (DisableScheduling)
3627 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3628
Bill Wendling39150252008-09-09 20:39:27 +00003629 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003630 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3631
3632 if (DisableScheduling)
3633 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003634
3635 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003636 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003637 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003638
Bill Wendling3eb59402008-09-09 00:28:24 +00003639 // Different possible minimax approximations of significand in
3640 // floating-point for various degrees of accuracy over [1,2].
3641 if (LimitFloatPrecision <= 6) {
3642 // For floating-point precision of 6:
3643 //
3644 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3645 //
3646 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003648 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003649 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003650 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003651 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3652 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003653 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003654
Scott Michelfdc40a02009-02-17 22:15:04 +00003655 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003656 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003657
3658 if (DisableScheduling) {
3659 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3660 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3661 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3662 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3663 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3664 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003665 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3666 // For floating-point precision of 12:
3667 //
3668 // Log2ofMantissa =
3669 // -2.51285454f +
3670 // (4.07009056f +
3671 // (-2.12067489f +
3672 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003673 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003674 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003675 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003676 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003677 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003678 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003679 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3680 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003681 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003682 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3683 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003684 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003685 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3686 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003687 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003688
Scott Michelfdc40a02009-02-17 22:15:04 +00003689 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003690 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003691
3692 if (DisableScheduling) {
3693 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3694 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3695 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3696 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3697 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3698 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3699 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3700 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3701 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3702 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003703 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3704 // For floating-point precision of 18:
3705 //
3706 // Log2ofMantissa =
3707 // -3.0400495f +
3708 // (6.1129976f +
3709 // (-5.3420409f +
3710 // (3.2865683f +
3711 // (-1.2669343f +
3712 // (0.27515199f -
3713 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3714 //
3715 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003717 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003718 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003719 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003720 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3721 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003722 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003723 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3724 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003725 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003726 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3727 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003728 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003729 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3730 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003731 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003732 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3733 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003734 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003735
Scott Michelfdc40a02009-02-17 22:15:04 +00003736 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003738
3739 if (DisableScheduling) {
3740 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3741 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3742 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3743 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3744 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3745 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3746 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3747 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3748 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3749 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3750 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3751 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3752 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3753 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003754 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003755 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003756 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003757 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003758 getValue(I.getOperand(1)).getValueType(),
3759 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003760
3761 if (DisableScheduling)
3762 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003763 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003764
Dale Johannesen59e577f2008-09-05 18:38:42 +00003765 setValue(&I, result);
3766}
3767
Bill Wendling3eb59402008-09-09 00:28:24 +00003768/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3769/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003770void
Dan Gohman2048b852009-11-23 18:04:58 +00003771SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003772 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003773 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003774
Owen Anderson825b72b2009-08-11 20:47:22 +00003775 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003776 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3777 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003778 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003779
Bill Wendling856ff412009-12-22 00:12:37 +00003780 if (DisableScheduling)
3781 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3782
Bill Wendling39150252008-09-09 20:39:27 +00003783 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003784 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003785 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003786 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003787
Bill Wendling856ff412009-12-22 00:12:37 +00003788 if (DisableScheduling)
3789 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3790
Bill Wendling3eb59402008-09-09 00:28:24 +00003791 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003792 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003793 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003794
3795 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003796 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003797 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003798 // Log10ofMantissa =
3799 // -0.50419619f +
3800 // (0.60948995f - 0.10380950f * x) * x;
3801 //
3802 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003803 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003804 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003805 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003806 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003807 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3808 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003809 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003810
Scott Michelfdc40a02009-02-17 22:15:04 +00003811 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003812 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003813
3814 if (DisableScheduling) {
3815 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3816 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3817 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3818 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3819 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3820 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003821 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3822 // For floating-point precision of 12:
3823 //
3824 // Log10ofMantissa =
3825 // -0.64831180f +
3826 // (0.91751397f +
3827 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3828 //
3829 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003830 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003831 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003832 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003833 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003834 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3835 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003836 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003837 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3838 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003839 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003840
Scott Michelfdc40a02009-02-17 22:15:04 +00003841 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003842 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003843
3844 if (DisableScheduling) {
3845 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3846 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3847 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3848 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3849 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3850 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3851 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3852 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003853 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003854 // For floating-point precision of 18:
3855 //
3856 // Log10ofMantissa =
3857 // -0.84299375f +
3858 // (1.5327582f +
3859 // (-1.0688956f +
3860 // (0.49102474f +
3861 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3862 //
3863 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003864 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003865 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003866 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003867 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3869 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003870 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3872 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003873 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003874 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3875 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003876 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003877 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3878 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003879 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003880
Scott Michelfdc40a02009-02-17 22:15:04 +00003881 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003883
3884 if (DisableScheduling) {
3885 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3886 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3887 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3888 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3889 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3890 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3891 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3892 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3893 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3894 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3895 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3896 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003897 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003898 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003899 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003900 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003901 getValue(I.getOperand(1)).getValueType(),
3902 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003903
3904 if (DisableScheduling)
3905 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003906 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003907
Dale Johannesen59e577f2008-09-05 18:38:42 +00003908 setValue(&I, result);
3909}
3910
Bill Wendlinge10c8142008-09-09 22:39:21 +00003911/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3912/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003913void
Dan Gohman2048b852009-11-23 18:04:58 +00003914SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003915 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003916 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003917
Owen Anderson825b72b2009-08-11 20:47:22 +00003918 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003919 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3920 SDValue Op = getValue(I.getOperand(1));
3921
Owen Anderson825b72b2009-08-11 20:47:22 +00003922 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003923
Bill Wendling856ff412009-12-22 00:12:37 +00003924 if (DisableScheduling)
3925 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3926
Bill Wendlinge10c8142008-09-09 22:39:21 +00003927 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003928 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3929 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003930
3931 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003932 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003933 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003934
Bill Wendling856ff412009-12-22 00:12:37 +00003935 if (DisableScheduling) {
3936 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3937 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3938 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3939 }
3940
Bill Wendlinge10c8142008-09-09 22:39:21 +00003941 if (LimitFloatPrecision <= 6) {
3942 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003943 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003944 // TwoToFractionalPartOfX =
3945 // 0.997535578f +
3946 // (0.735607626f + 0.252464424f * x) * x;
3947 //
3948 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003950 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003952 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003953 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3954 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003955 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003957 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003959
Scott Michelfdc40a02009-02-17 22:15:04 +00003960 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003962
3963 if (DisableScheduling) {
3964 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3965 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3966 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3967 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3968 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3969 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3970 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3971 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003972 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3973 // For floating-point precision of 12:
3974 //
3975 // TwoToFractionalPartOfX =
3976 // 0.999892986f +
3977 // (0.696457318f +
3978 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3979 //
3980 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003981 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003982 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003983 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003984 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003985 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3986 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003987 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003988 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3989 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003990 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003991 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003992 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003993 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003994
Scott Michelfdc40a02009-02-17 22:15:04 +00003995 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003996 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003997
3998 if (DisableScheduling) {
3999 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4000 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4001 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4002 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4003 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4004 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4005 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4006 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4007 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4008 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004009 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4010 // For floating-point precision of 18:
4011 //
4012 // TwoToFractionalPartOfX =
4013 // 0.999999982f +
4014 // (0.693148872f +
4015 // (0.240227044f +
4016 // (0.554906021e-1f +
4017 // (0.961591928e-2f +
4018 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4019 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004020 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004021 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004023 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004024 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4025 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004026 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004027 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4028 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004029 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004030 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4031 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004032 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4034 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004035 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004036 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4037 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004038 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004040 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004041 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004042
Scott Michelfdc40a02009-02-17 22:15:04 +00004043 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004044 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004045
4046 if (DisableScheduling) {
4047 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4048 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4049 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4050 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4051 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4052 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4053 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4054 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4055 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4056 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4057 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4058 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4059 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4060 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4061 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4062 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004063 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00004064 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00004065 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004066 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00004067 getValue(I.getOperand(1)).getValueType(),
4068 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00004069
4070 if (DisableScheduling)
4071 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004072 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004073
Dale Johannesen601d3c02008-09-05 01:48:15 +00004074 setValue(&I, result);
4075}
4076
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004077/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4078/// limited-precision mode with x == 10.0f.
4079void
Dan Gohman2048b852009-11-23 18:04:58 +00004080SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004081 SDValue result;
4082 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004083 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004084 bool IsExp10 = false;
4085
Owen Anderson825b72b2009-08-11 20:47:22 +00004086 if (getValue(Val).getValueType() == MVT::f32 &&
4087 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004088 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4089 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4090 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4091 APFloat Ten(10.0f);
4092 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4093 }
4094 }
4095 }
4096
4097 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4098 SDValue Op = getValue(I.getOperand(2));
4099
4100 // Put the exponent in the right bit position for later addition to the
4101 // final result:
4102 //
4103 // #define LOG2OF10 3.3219281f
4104 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004105 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004106 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004107 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004108
4109 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4111 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004112
Bill Wendling856ff412009-12-22 00:12:37 +00004113 if (DisableScheduling) {
4114 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4115 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4116 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4117 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4118 }
4119
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004120 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004121 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004122 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004123
Bill Wendling856ff412009-12-22 00:12:37 +00004124 if (DisableScheduling)
4125 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4126
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004127 if (LimitFloatPrecision <= 6) {
4128 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004129 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004130 // twoToFractionalPartOfX =
4131 // 0.997535578f +
4132 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004133 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004134 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004135 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004136 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004137 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004138 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004139 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4140 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004141 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004143 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004145
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004146 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004148
4149 if (DisableScheduling) {
4150 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4151 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4152 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4153 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4154 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4155 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4156 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4157 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004158 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4159 // For floating-point precision of 12:
4160 //
4161 // TwoToFractionalPartOfX =
4162 // 0.999892986f +
4163 // (0.696457318f +
4164 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4165 //
4166 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004167 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004168 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004169 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004170 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004171 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4172 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004173 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4175 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004176 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004177 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004178 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004180
Scott Michelfdc40a02009-02-17 22:15:04 +00004181 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004183
4184 if (DisableScheduling) {
4185 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4186 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4187 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4188 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4189 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4190 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4191 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4192 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4193 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4194 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004195 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4196 // For floating-point precision of 18:
4197 //
4198 // TwoToFractionalPartOfX =
4199 // 0.999999982f +
4200 // (0.693148872f +
4201 // (0.240227044f +
4202 // (0.554906021e-1f +
4203 // (0.961591928e-2f +
4204 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4205 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004206 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004207 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004208 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004209 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004210 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4211 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004212 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4214 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004215 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004216 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4217 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004218 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004219 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4220 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004221 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004222 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4223 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004224 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004226 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004228
Scott Michelfdc40a02009-02-17 22:15:04 +00004229 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004231
4232 if (DisableScheduling) {
4233 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4234 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4235 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4236 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4237 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4238 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4239 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4240 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4241 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4242 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4243 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4244 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4245 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4246 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4247 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4248 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004249 }
4250 } else {
4251 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004252 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004253 getValue(I.getOperand(1)).getValueType(),
4254 getValue(I.getOperand(1)),
4255 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004256
4257 if (DisableScheduling)
4258 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004259 }
4260
4261 setValue(&I, result);
4262}
4263
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004264/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4265/// we want to emit this as a call to a named external function, return the name
4266/// otherwise lower it and return null.
4267const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004268SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004269 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004270 SDValue Res;
4271
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004272 switch (Intrinsic) {
4273 default:
4274 // By default, turn this into a target intrinsic node.
4275 visitTargetIntrinsic(I, Intrinsic);
4276 return 0;
4277 case Intrinsic::vastart: visitVAStart(I); return 0;
4278 case Intrinsic::vaend: visitVAEnd(I); return 0;
4279 case Intrinsic::vacopy: visitVACopy(I); return 0;
4280 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004281 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4282 getValue(I.getOperand(1)));
4283 setValue(&I, Res);
4284 if (DisableScheduling)
4285 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004286 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004287 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004288 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4289 getValue(I.getOperand(1)));
4290 setValue(&I, Res);
4291 if (DisableScheduling)
4292 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004293 return 0;
4294 case Intrinsic::setjmp:
4295 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004296 case Intrinsic::longjmp:
4297 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004298 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004299 SDValue Op1 = getValue(I.getOperand(1));
4300 SDValue Op2 = getValue(I.getOperand(2));
4301 SDValue Op3 = getValue(I.getOperand(3));
4302 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004303 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4304 I.getOperand(1), 0, I.getOperand(2), 0);
4305 DAG.setRoot(Res);
4306 if (DisableScheduling)
4307 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004308 return 0;
4309 }
Chris Lattner824b9582008-11-21 16:42:48 +00004310 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004311 SDValue Op1 = getValue(I.getOperand(1));
4312 SDValue Op2 = getValue(I.getOperand(2));
4313 SDValue Op3 = getValue(I.getOperand(3));
4314 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004315 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4316 I.getOperand(1), 0);
4317 DAG.setRoot(Res);
4318 if (DisableScheduling)
4319 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004320 return 0;
4321 }
Chris Lattner824b9582008-11-21 16:42:48 +00004322 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004323 SDValue Op1 = getValue(I.getOperand(1));
4324 SDValue Op2 = getValue(I.getOperand(2));
4325 SDValue Op3 = getValue(I.getOperand(3));
4326 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4327
4328 // If the source and destination are known to not be aliases, we can
4329 // lower memmove as memcpy.
4330 uint64_t Size = -1ULL;
4331 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004332 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004333 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4334 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004335 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4336 I.getOperand(1), 0, I.getOperand(2), 0);
4337 DAG.setRoot(Res);
4338 if (DisableScheduling)
4339 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004340 return 0;
4341 }
4342
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004343 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4344 I.getOperand(1), 0, I.getOperand(2), 0);
4345 DAG.setRoot(Res);
4346 if (DisableScheduling)
4347 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004348 return 0;
4349 }
Devang Patel70d75ca2009-11-12 19:02:56 +00004350 case Intrinsic::dbg_stoppoint:
4351 case Intrinsic::dbg_region_start:
4352 case Intrinsic::dbg_region_end:
4353 case Intrinsic::dbg_func_start:
4354 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004355 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004356 case Intrinsic::dbg_declare: {
Devang Patel7e1e31f2009-07-02 22:43:26 +00004357 if (OptLevel != CodeGenOpt::None)
4358 // FIXME: Variable debug info is not supported here.
4359 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004360 DwarfWriter *DW = DAG.getDwarfWriter();
4361 if (!DW)
4362 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004363 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00004364 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00004365 return 0;
4366
Devang Patelac1ceb32009-10-09 22:42:28 +00004367 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004368 Value *Address = DI.getAddress();
4369 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4370 Address = BCI->getOperand(0);
4371 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4372 // Don't handle byval struct arguments or VLAs, for example.
4373 if (!AI)
4374 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004375 DenseMap<const AllocaInst*, int>::iterator SI =
4376 FuncInfo.StaticAllocaMap.find(AI);
4377 if (SI == FuncInfo.StaticAllocaMap.end())
4378 return 0; // VLAs.
4379 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004380
Chris Lattner3990b122009-12-28 23:41:32 +00004381 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo())
4382 if (MDNode *Dbg = DI.getMetadata("dbg"))
Chris Lattner0eb41982009-12-28 20:45:51 +00004383 MMI->setVariableDbgInfo(Variable, FI, Dbg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004384 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004385 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004386 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004387 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004388 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004389 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004390 SDValue Ops[1];
4391 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004392 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004393 setValue(&I, Op);
4394 DAG.setRoot(Op.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004395 if (DisableScheduling)
4396 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004397 return 0;
4398 }
4399
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004400 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004401 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004402
Chris Lattner3a5815f2009-09-17 23:54:54 +00004403 if (CurMBB->isLandingPad())
4404 AddCatchInfo(I, MMI, CurMBB);
4405 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004407 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004408#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004409 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4410 unsigned Reg = TLI.getExceptionSelectorRegister();
4411 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004412 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004413
Chris Lattner3a5815f2009-09-17 23:54:54 +00004414 // Insert the EHSELECTION instruction.
4415 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4416 SDValue Ops[2];
4417 Ops[0] = getValue(I.getOperand(1));
4418 Ops[1] = getRoot();
4419 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4420
4421 DAG.setRoot(Op.getValue(1));
4422
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004423 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4424 setValue(&I, Res);
4425 if (DisableScheduling) {
4426 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4427 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4428 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004429 return 0;
4430 }
4431
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004432 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004433 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004434
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004435 if (MMI) {
4436 // Find the type id for the given typeinfo.
4437 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004438 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004439 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004440 } else {
4441 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004442 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004443 }
4444
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004445 setValue(&I, Res);
4446 if (DisableScheduling)
4447 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004448 return 0;
4449 }
4450
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004451 case Intrinsic::eh_return_i32:
4452 case Intrinsic::eh_return_i64:
4453 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004454 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004455 Res = DAG.getNode(ISD::EH_RETURN, dl,
4456 MVT::Other,
4457 getControlRoot(),
4458 getValue(I.getOperand(1)),
4459 getValue(I.getOperand(2)));
4460 DAG.setRoot(Res);
4461 if (DisableScheduling)
4462 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004463 } else {
4464 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4465 }
4466
4467 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004468 case Intrinsic::eh_unwind_init:
4469 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4470 MMI->setCallsUnwindInit(true);
4471 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004472 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004473 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004474 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004475 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4476 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004477 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004478 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004479 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004480 TLI.getPointerTy()),
4481 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004482 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004483 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004484 DAG.getConstant(0, TLI.getPointerTy()));
4485 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4486 FA, Offset);
4487 setValue(&I, Res);
4488 if (DisableScheduling) {
4489 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4490 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4491 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4492 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4493 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004494 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004495 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004496 case Intrinsic::convertff:
4497 case Intrinsic::convertfsi:
4498 case Intrinsic::convertfui:
4499 case Intrinsic::convertsif:
4500 case Intrinsic::convertuif:
4501 case Intrinsic::convertss:
4502 case Intrinsic::convertsu:
4503 case Intrinsic::convertus:
4504 case Intrinsic::convertuu: {
4505 ISD::CvtCode Code = ISD::CVT_INVALID;
4506 switch (Intrinsic) {
4507 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4508 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4509 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4510 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4511 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4512 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4513 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4514 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4515 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4516 }
Owen Andersone50ed302009-08-10 22:56:29 +00004517 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004518 Value *Op1 = I.getOperand(1);
4519 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4520 DAG.getValueType(DestVT),
4521 DAG.getValueType(getValue(Op1).getValueType()),
4522 getValue(I.getOperand(2)),
4523 getValue(I.getOperand(3)),
4524 Code);
4525 setValue(&I, Res);
4526 if (DisableScheduling)
4527 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004528 return 0;
4529 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004530 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004531 Res = DAG.getNode(ISD::FSQRT, dl,
4532 getValue(I.getOperand(1)).getValueType(),
4533 getValue(I.getOperand(1)));
4534 setValue(&I, Res);
4535 if (DisableScheduling)
4536 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004537 return 0;
4538 case Intrinsic::powi:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004539 Res = DAG.getNode(ISD::FPOWI, dl,
4540 getValue(I.getOperand(1)).getValueType(),
4541 getValue(I.getOperand(1)),
4542 getValue(I.getOperand(2)));
4543 setValue(&I, Res);
4544 if (DisableScheduling)
4545 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004546 return 0;
4547 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004548 Res = DAG.getNode(ISD::FSIN, dl,
4549 getValue(I.getOperand(1)).getValueType(),
4550 getValue(I.getOperand(1)));
4551 setValue(&I, Res);
4552 if (DisableScheduling)
4553 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004554 return 0;
4555 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004556 Res = DAG.getNode(ISD::FCOS, dl,
4557 getValue(I.getOperand(1)).getValueType(),
4558 getValue(I.getOperand(1)));
4559 setValue(&I, Res);
4560 if (DisableScheduling)
4561 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004562 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004563 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004564 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004565 return 0;
4566 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004567 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004568 return 0;
4569 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004570 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004571 return 0;
4572 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004573 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004574 return 0;
4575 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004576 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004577 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004578 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004579 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004580 return 0;
4581 case Intrinsic::pcmarker: {
4582 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004583 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4584 DAG.setRoot(Res);
4585 if (DisableScheduling)
4586 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004587 return 0;
4588 }
4589 case Intrinsic::readcyclecounter: {
4590 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004591 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4592 DAG.getVTList(MVT::i64, MVT::Other),
4593 &Op, 1);
4594 setValue(&I, Res);
4595 DAG.setRoot(Res.getValue(1));
4596 if (DisableScheduling)
4597 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004598 return 0;
4599 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004600 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004601 Res = DAG.getNode(ISD::BSWAP, dl,
4602 getValue(I.getOperand(1)).getValueType(),
4603 getValue(I.getOperand(1)));
4604 setValue(&I, Res);
4605 if (DisableScheduling)
4606 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004607 return 0;
4608 case Intrinsic::cttz: {
4609 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004610 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004611 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4612 setValue(&I, Res);
4613 if (DisableScheduling)
4614 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004615 return 0;
4616 }
4617 case Intrinsic::ctlz: {
4618 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004619 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004620 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4621 setValue(&I, Res);
4622 if (DisableScheduling)
4623 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624 return 0;
4625 }
4626 case Intrinsic::ctpop: {
4627 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004628 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004629 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4630 setValue(&I, Res);
4631 if (DisableScheduling)
4632 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004633 return 0;
4634 }
4635 case Intrinsic::stacksave: {
4636 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004637 Res = DAG.getNode(ISD::STACKSAVE, dl,
4638 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4639 setValue(&I, Res);
4640 DAG.setRoot(Res.getValue(1));
4641 if (DisableScheduling)
4642 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004643 return 0;
4644 }
4645 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004646 Res = getValue(I.getOperand(1));
4647 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4648 DAG.setRoot(Res);
4649 if (DisableScheduling)
4650 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004651 return 0;
4652 }
Bill Wendling57344502008-11-18 11:01:33 +00004653 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004654 // Emit code into the DAG to store the stack guard onto the stack.
4655 MachineFunction &MF = DAG.getMachineFunction();
4656 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004657 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004658
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004659 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4660 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004661
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004662 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004663 MFI->setStackProtectorIndex(FI);
4664
4665 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4666
4667 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004668 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4669 PseudoSourceValue::getFixedStack(FI),
4670 0, true);
4671 setValue(&I, Res);
4672 DAG.setRoot(Res);
4673 if (DisableScheduling)
4674 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004675 return 0;
4676 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004677 case Intrinsic::objectsize: {
4678 // If we don't know by now, we're never going to know.
4679 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4680
4681 assert(CI && "Non-constant type in __builtin_object_size?");
4682
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004683 SDValue Arg = getValue(I.getOperand(0));
4684 EVT Ty = Arg.getValueType();
4685
Eric Christopherd060b252009-12-23 02:51:48 +00004686 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004687 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004688 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004689 Res = DAG.getConstant(0, Ty);
4690
4691 setValue(&I, Res);
4692 if (DisableScheduling)
4693 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004694 return 0;
4695 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004696 case Intrinsic::var_annotation:
4697 // Discard annotate attributes
4698 return 0;
4699
4700 case Intrinsic::init_trampoline: {
4701 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4702
4703 SDValue Ops[6];
4704 Ops[0] = getRoot();
4705 Ops[1] = getValue(I.getOperand(1));
4706 Ops[2] = getValue(I.getOperand(2));
4707 Ops[3] = getValue(I.getOperand(3));
4708 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4709 Ops[5] = DAG.getSrcValue(F);
4710
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004711 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4712 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4713 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004714
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004715 setValue(&I, Res);
4716 DAG.setRoot(Res.getValue(1));
4717 if (DisableScheduling)
4718 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004719 return 0;
4720 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004721 case Intrinsic::gcroot:
4722 if (GFI) {
4723 Value *Alloca = I.getOperand(1);
4724 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004725
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004726 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4727 GFI->addStackRoot(FI->getIndex(), TypeMap);
4728 }
4729 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004730 case Intrinsic::gcread:
4731 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004732 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004733 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004734 case Intrinsic::flt_rounds:
4735 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4736 setValue(&I, Res);
4737 if (DisableScheduling)
4738 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004739 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004740 case Intrinsic::trap:
4741 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4742 DAG.setRoot(Res);
4743 if (DisableScheduling)
4744 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004745 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004746 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004747 return implVisitAluOverflow(I, ISD::UADDO);
4748 case Intrinsic::sadd_with_overflow:
4749 return implVisitAluOverflow(I, ISD::SADDO);
4750 case Intrinsic::usub_with_overflow:
4751 return implVisitAluOverflow(I, ISD::USUBO);
4752 case Intrinsic::ssub_with_overflow:
4753 return implVisitAluOverflow(I, ISD::SSUBO);
4754 case Intrinsic::umul_with_overflow:
4755 return implVisitAluOverflow(I, ISD::UMULO);
4756 case Intrinsic::smul_with_overflow:
4757 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004758
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004759 case Intrinsic::prefetch: {
4760 SDValue Ops[4];
4761 Ops[0] = getRoot();
4762 Ops[1] = getValue(I.getOperand(1));
4763 Ops[2] = getValue(I.getOperand(2));
4764 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004765 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4766 DAG.setRoot(Res);
4767 if (DisableScheduling)
4768 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004769 return 0;
4770 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004771
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004772 case Intrinsic::memory_barrier: {
4773 SDValue Ops[6];
4774 Ops[0] = getRoot();
4775 for (int x = 1; x < 6; ++x)
4776 Ops[x] = getValue(I.getOperand(x));
4777
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004778 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4779 DAG.setRoot(Res);
4780 if (DisableScheduling)
4781 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004782 return 0;
4783 }
4784 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004785 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004786 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004787 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004788 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4789 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004790 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004791 getValue(I.getOperand(2)),
4792 getValue(I.getOperand(3)),
4793 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004794 setValue(&I, L);
4795 DAG.setRoot(L.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004796 if (DisableScheduling)
4797 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004798 return 0;
4799 }
4800 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004801 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004803 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004804 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004805 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004806 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004807 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004808 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004809 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004810 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004811 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004812 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004813 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004814 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004815 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004816 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004817 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004818 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004819 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004820 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004821 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004822
4823 case Intrinsic::invariant_start:
4824 case Intrinsic::lifetime_start:
4825 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004826 Res = DAG.getUNDEF(TLI.getPointerTy());
4827 setValue(&I, Res);
4828 if (DisableScheduling)
4829 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004830 return 0;
4831 case Intrinsic::invariant_end:
4832 case Intrinsic::lifetime_end:
4833 // Discard region information.
4834 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004835 }
4836}
4837
Dan Gohman98ca4f22009-08-05 01:29:28 +00004838/// Test if the given instruction is in a position to be optimized
4839/// with a tail-call. This roughly means that it's in a block with
4840/// a return and there's nothing that needs to be scheduled
4841/// between it and the return.
4842///
4843/// This function only tests target-independent requirements.
4844/// For target-dependent requirements, a target should override
4845/// TargetLowering::IsEligibleForTailCallOptimization.
4846///
4847static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004848isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004849 const TargetLowering &TLI) {
4850 const BasicBlock *ExitBB = I->getParent();
4851 const TerminatorInst *Term = ExitBB->getTerminator();
4852 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4853 const Function *F = ExitBB->getParent();
4854
4855 // The block must end in a return statement or an unreachable.
4856 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4857
4858 // If I will have a chain, make sure no other instruction that will have a
4859 // chain interposes between I and the return.
4860 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4861 !I->isSafeToSpeculativelyExecute())
4862 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4863 --BBI) {
4864 if (&*BBI == I)
4865 break;
4866 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4867 !BBI->isSafeToSpeculativelyExecute())
4868 return false;
4869 }
4870
4871 // If the block ends with a void return or unreachable, it doesn't matter
4872 // what the call's return type is.
4873 if (!Ret || Ret->getNumOperands() == 0) return true;
4874
Dan Gohmaned9bab32009-11-14 02:06:30 +00004875 // If the return value is undef, it doesn't matter what the call's
4876 // return type is.
4877 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4878
Dan Gohman98ca4f22009-08-05 01:29:28 +00004879 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004880 // the return. Ignore noalias because it doesn't affect the call sequence.
4881 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4882 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004883 return false;
4884
4885 // Otherwise, make sure the unmodified return value of I is the return value.
4886 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4887 U = dyn_cast<Instruction>(U->getOperand(0))) {
4888 if (!U)
4889 return false;
4890 if (!U->hasOneUse())
4891 return false;
4892 if (U == I)
4893 break;
4894 // Check for a truly no-op truncate.
4895 if (isa<TruncInst>(U) &&
4896 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4897 continue;
4898 // Check for a truly no-op bitcast.
4899 if (isa<BitCastInst>(U) &&
4900 (U->getOperand(0)->getType() == U->getType() ||
4901 (isa<PointerType>(U->getOperand(0)->getType()) &&
4902 isa<PointerType>(U->getType()))))
4903 continue;
4904 // Otherwise it's not a true no-op.
4905 return false;
4906 }
4907
4908 return true;
4909}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004910
Dan Gohman2048b852009-11-23 18:04:58 +00004911void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4912 bool isTailCall,
4913 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004914 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4915 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004916 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004917 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4918 unsigned BeginLabel = 0, EndLabel = 0;
4919
4920 TargetLowering::ArgListTy Args;
4921 TargetLowering::ArgListEntry Entry;
4922 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004923
4924 // Check whether the function can return without sret-demotion.
4925 SmallVector<EVT, 4> OutVTs;
4926 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4927 SmallVector<uint64_t, 4> Offsets;
4928 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004929 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004930
4931 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
4932 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4933
4934 SDValue DemoteStackSlot;
4935
4936 if (!CanLowerReturn) {
4937 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4938 FTy->getReturnType());
4939 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4940 FTy->getReturnType());
4941 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004942 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004943 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4944
4945 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4946 Entry.Node = DemoteStackSlot;
4947 Entry.Ty = StackSlotPtrType;
4948 Entry.isSExt = false;
4949 Entry.isZExt = false;
4950 Entry.isInReg = false;
4951 Entry.isSRet = true;
4952 Entry.isNest = false;
4953 Entry.isByVal = false;
4954 Entry.Alignment = Align;
4955 Args.push_back(Entry);
4956 RetTy = Type::getVoidTy(FTy->getContext());
4957 }
4958
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004959 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004960 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004961 SDValue ArgNode = getValue(*i);
4962 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4963
4964 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004965 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4966 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4967 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4968 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4969 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4970 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004971 Entry.Alignment = CS.getParamAlignment(attrInd);
4972 Args.push_back(Entry);
4973 }
4974
4975 if (LandingPad && MMI) {
4976 // Insert a label before the invoke call to mark the try range. This can be
4977 // used to detect deletion of the invoke via the MachineModuleInfo.
4978 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004980 // Both PendingLoads and PendingExports must be flushed here;
4981 // this call might not return.
4982 (void)getRoot();
Bill Wendling0d580132009-12-23 01:28:19 +00004983 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4984 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004985 }
4986
Dan Gohman98ca4f22009-08-05 01:29:28 +00004987 // Check if target-independent constraints permit a tail call here.
4988 // Target-dependent constraints are checked within TLI.LowerCallTo.
4989 if (isTailCall &&
4990 !isInTailCallPosition(CS.getInstruction(),
4991 CS.getAttributes().getRetAttributes(),
4992 TLI))
4993 isTailCall = false;
4994
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004995 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004996 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004997 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004998 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004999 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005000 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00005001 isTailCall,
5002 !CS.getInstruction()->use_empty(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005003 Callee, Args, DAG, getCurDebugLoc(), SDNodeOrder);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005004 assert((isTailCall || Result.second.getNode()) &&
5005 "Non-null chain expected with non-tail call!");
5006 assert((Result.second.getNode() || !Result.first.getNode()) &&
5007 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005008 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005009 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005010 if (DisableScheduling)
5011 DAG.AssignOrdering(Result.first.getNode(), SDNodeOrder);
5012 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005013 // The instruction result is the result of loading from the
5014 // hidden sret parameter.
5015 SmallVector<EVT, 1> PVTs;
5016 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
5017
5018 ComputeValueVTs(TLI, PtrRetTy, PVTs);
5019 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5020 EVT PtrVT = PVTs[0];
5021 unsigned NumValues = OutVTs.size();
5022 SmallVector<SDValue, 4> Values(NumValues);
5023 SmallVector<SDValue, 4> Chains(NumValues);
5024
5025 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00005026 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
5027 DemoteStackSlot,
5028 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005029 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005030 Add, NULL, Offsets[i], false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005031 Values[i] = L;
5032 Chains[i] = L.getValue(1);
5033 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005034
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005035 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
5036 MVT::Other, &Chains[0], NumValues);
5037 PendingLoads.push_back(Chain);
5038
Bill Wendlinge80ae832009-12-22 00:50:32 +00005039 SDValue MV = DAG.getNode(ISD::MERGE_VALUES,
5040 getCurDebugLoc(),
5041 DAG.getVTList(&OutVTs[0], NumValues),
5042 &Values[0], NumValues);
5043 setValue(CS.getInstruction(), MV);
5044
5045 if (DisableScheduling) {
5046 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
5047 DAG.AssignOrdering(MV.getNode(), SDNodeOrder);
5048 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005049 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005050
5051 // As a special case, a null chain means that a tail call has been emitted and
5052 // the DAG root is already updated.
5053 if (Result.second.getNode()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005054 DAG.setRoot(Result.second);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005055 if (DisableScheduling)
5056 DAG.AssignOrdering(Result.second.getNode(), SDNodeOrder);
5057 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005058 HasTailCall = true;
Bill Wendlinge80ae832009-12-22 00:50:32 +00005059 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005060
5061 if (LandingPad && MMI) {
5062 // Insert a label at the end of the invoke call to mark the try range. This
5063 // can be used to detect deletion of the invoke via the MachineModuleInfo.
5064 EndLabel = MMI->NextLabelID();
Bill Wendling0d580132009-12-23 01:28:19 +00005065 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5066 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067
5068 // Inform MachineModuleInfo of range.
5069 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
5070 }
5071}
5072
Chris Lattner8047d9a2009-12-24 00:37:38 +00005073/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5074/// value is equal or not-equal to zero.
5075static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
5076 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
5077 UI != E; ++UI) {
5078 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
5079 if (IC->isEquality())
5080 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
5081 if (C->isNullValue())
5082 continue;
5083 // Unknown instruction.
5084 return false;
5085 }
5086 return true;
5087}
5088
Chris Lattner04b091a2009-12-24 01:07:17 +00005089static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005090 SelectionDAGBuilder &Builder) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00005091
5092 // Check to see if this load can be trivially constant folded, e.g. if the
5093 // input is from a string literal.
5094 if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
5095 // Cast pointer to the type we really want to load.
5096 LoadInput = ConstantExpr::getBitCast(LoadInput,
5097 PointerType::getUnqual(LoadTy));
5098
5099 if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD))
5100 return Builder.getValue(LoadCst);
5101 }
5102
5103 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5104 // still constant memory, the input chain can be the entry node.
5105 SDValue Root;
5106 bool ConstantMemory = false;
5107
5108 // Do not serialize (non-volatile) loads of constant memory with anything.
5109 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5110 Root = Builder.DAG.getEntryNode();
5111 ConstantMemory = true;
5112 } else {
5113 // Do not serialize non-volatile loads against each other.
5114 Root = Builder.DAG.getRoot();
5115 }
5116
5117 SDValue Ptr = Builder.getValue(PtrVal);
5118 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
5119 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
5120 false /*volatile*/, 1 /* align=1 */);
5121
5122 if (!ConstantMemory)
5123 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5124 return LoadVal;
5125}
5126
5127
5128/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5129/// If so, return true and lower it, otherwise return false and it will be
5130/// lowered like a normal call.
5131bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) {
5132 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
5133 if (I.getNumOperands() != 4)
5134 return false;
5135
5136 Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
5137 if (!isa<PointerType>(LHS->getType()) || !isa<PointerType>(RHS->getType()) ||
5138 !isa<IntegerType>(I.getOperand(3)->getType()) ||
5139 !isa<IntegerType>(I.getType()))
5140 return false;
5141
5142 ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
5143
5144 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5145 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005146 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5147 bool ActuallyDoIt = true;
5148 MVT LoadVT;
5149 const Type *LoadTy;
5150 switch (Size->getZExtValue()) {
5151 default:
5152 LoadVT = MVT::Other;
5153 LoadTy = 0;
5154 ActuallyDoIt = false;
5155 break;
5156 case 2:
5157 LoadVT = MVT::i16;
5158 LoadTy = Type::getInt16Ty(Size->getContext());
5159 break;
5160 case 4:
5161 LoadVT = MVT::i32;
5162 LoadTy = Type::getInt32Ty(Size->getContext());
5163 break;
5164 case 8:
5165 LoadVT = MVT::i64;
5166 LoadTy = Type::getInt64Ty(Size->getContext());
5167 break;
5168 /*
5169 case 16:
5170 LoadVT = MVT::v4i32;
5171 LoadTy = Type::getInt32Ty(Size->getContext());
5172 LoadTy = VectorType::get(LoadTy, 4);
5173 break;
5174 */
5175 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005176
Chris Lattner04b091a2009-12-24 01:07:17 +00005177 // This turns into unaligned loads. We only do this if the target natively
5178 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5179 // we'll only produce a small number of byte loads.
5180
5181 // Require that we can find a legal MVT, and only do this if the target
5182 // supports unaligned loads of that type. Expanding into byte loads would
5183 // bloat the code.
5184 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5185 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5186 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5187 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5188 ActuallyDoIt = false;
5189 }
5190
5191 if (ActuallyDoIt) {
5192 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5193 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
5194
5195 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
5196 ISD::SETNE);
5197 EVT CallVT = TLI.getValueType(I.getType(), true);
5198 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
5199 return true;
5200 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005201 }
5202
5203
5204 return false;
5205}
5206
5207
Dan Gohman2048b852009-11-23 18:04:58 +00005208void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005209 const char *RenameFn = 0;
5210 if (Function *F = I.getCalledFunction()) {
5211 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005212 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
5213 if (II) {
5214 if (unsigned IID = II->getIntrinsicID(F)) {
5215 RenameFn = visitIntrinsicCall(I, IID);
5216 if (!RenameFn)
5217 return;
5218 }
5219 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 if (unsigned IID = F->getIntrinsicID()) {
5221 RenameFn = visitIntrinsicCall(I, IID);
5222 if (!RenameFn)
5223 return;
5224 }
5225 }
5226
5227 // Check for well-known libc/libm calls. If the function is internal, it
5228 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005229 if (!F->hasLocalLinkage() && F->hasName()) {
5230 StringRef Name = F->getName();
5231 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005232 if (I.getNumOperands() == 3 && // Basic sanity checks.
5233 I.getOperand(1)->getType()->isFloatingPoint() &&
5234 I.getType() == I.getOperand(1)->getType() &&
5235 I.getType() == I.getOperand(2)->getType()) {
5236 SDValue LHS = getValue(I.getOperand(1));
5237 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00005238 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5239 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 return;
5241 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005242 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005243 if (I.getNumOperands() == 2 && // Basic sanity checks.
5244 I.getOperand(1)->getType()->isFloatingPoint() &&
5245 I.getType() == I.getOperand(1)->getType()) {
5246 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005247 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
5248 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005249 return;
5250 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005251 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005252 if (I.getNumOperands() == 2 && // Basic sanity checks.
5253 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005254 I.getType() == I.getOperand(1)->getType() &&
5255 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005256 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005257 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5258 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005259 return;
5260 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005261 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005262 if (I.getNumOperands() == 2 && // Basic sanity checks.
5263 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005264 I.getType() == I.getOperand(1)->getType() &&
5265 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005266 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005267 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5268 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005269 return;
5270 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005271 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5272 if (I.getNumOperands() == 2 && // Basic sanity checks.
5273 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005274 I.getType() == I.getOperand(1)->getType() &&
5275 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005276 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005277 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5278 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005279 return;
5280 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005281 } else if (Name == "memcmp") {
5282 if (visitMemCmpCall(I))
5283 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005284 }
5285 }
5286 } else if (isa<InlineAsm>(I.getOperand(0))) {
5287 visitInlineAsm(&I);
5288 return;
5289 }
5290
5291 SDValue Callee;
5292 if (!RenameFn)
5293 Callee = getValue(I.getOperand(0));
5294 else
Bill Wendling056292f2008-09-16 21:48:12 +00005295 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005296
Bill Wendling0d580132009-12-23 01:28:19 +00005297 // Check if we can potentially perform a tail call. More detailed checking is
5298 // be done within LowerCallTo, after more information about the call is known.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005299 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5300
5301 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005302}
5303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005304/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005305/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005306/// Chain/Flag as the input and updates them for the output Chain/Flag.
5307/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005308SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005309 unsigned Order, SDValue &Chain,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005310 SDValue *Flag) const {
5311 // Assemble the legal parts into the final values.
5312 SmallVector<SDValue, 4> Values(ValueVTs.size());
5313 SmallVector<SDValue, 8> Parts;
5314 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5315 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005316 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005317 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005318 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005319
5320 Parts.resize(NumRegs);
5321 for (unsigned i = 0; i != NumRegs; ++i) {
5322 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00005323 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005324 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005325 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005326 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005327 *Flag = P.getValue(2);
5328 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005330 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005331
Bill Wendlingec72e322009-12-22 01:11:43 +00005332 if (DisableScheduling)
5333 DAG.AssignOrdering(P.getNode(), Order);
5334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 // If the source register was virtual and if we know something about it,
5336 // add an assert node.
5337 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5338 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5339 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5340 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5341 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5342 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005344 unsigned RegSize = RegisterVT.getSizeInBits();
5345 unsigned NumSignBits = LOI.NumSignBits;
5346 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005347
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005348 // FIXME: We capture more information than the dag can represent. For
5349 // now, just use the tightest assertzext/assertsext possible.
5350 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005351 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005353 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005354 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005355 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005356 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005357 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005358 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005359 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005360 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005361 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005362 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005363 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005364 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005365 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005366 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005367 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005368
Owen Anderson825b72b2009-08-11 20:47:22 +00005369 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005370 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005371 RegisterVT, P, DAG.getValueType(FromVT));
5372
Bill Wendlingec72e322009-12-22 01:11:43 +00005373 if (DisableScheduling)
5374 DAG.AssignOrdering(P.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005375 }
5376 }
5377 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005378
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005379 Parts[i] = P;
5380 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005381
Bill Wendling3ea3c242009-12-22 02:10:19 +00005382 Values[Value] = getCopyFromParts(DAG, dl, Order, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005383 NumRegs, RegisterVT, ValueVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005384 if (DisableScheduling)
5385 DAG.AssignOrdering(Values[Value].getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005386 Part += NumRegs;
5387 Parts.clear();
5388 }
5389
Bill Wendlingec72e322009-12-22 01:11:43 +00005390 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5391 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5392 &Values[0], ValueVTs.size());
5393 if (DisableScheduling)
5394 DAG.AssignOrdering(Res.getNode(), Order);
5395 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005396}
5397
5398/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005399/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400/// Chain/Flag as the input and updates them for the output Chain/Flag.
5401/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005402void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005403 unsigned Order, SDValue &Chain,
5404 SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005405 // Get the list of the values's legal parts.
5406 unsigned NumRegs = Regs.size();
5407 SmallVector<SDValue, 8> Parts(NumRegs);
5408 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005409 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005410 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005411 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005412
Bill Wendling3ea3c242009-12-22 02:10:19 +00005413 getCopyToParts(DAG, dl, Order,
5414 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005415 &Parts[Part], NumParts, RegisterVT);
5416 Part += NumParts;
5417 }
5418
5419 // Copy the parts into the registers.
5420 SmallVector<SDValue, 8> Chains(NumRegs);
5421 for (unsigned i = 0; i != NumRegs; ++i) {
5422 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00005423 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005424 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00005425 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005426 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005427 *Flag = Part.getValue(1);
5428 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005429
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005430 Chains[i] = Part.getValue(0);
Bill Wendlingec72e322009-12-22 01:11:43 +00005431
5432 if (DisableScheduling)
5433 DAG.AssignOrdering(Part.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005434 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005435
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005436 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005437 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005438 // flagged to it. That is the CopyToReg nodes and the user are considered
5439 // a single scheduling unit. If we create a TokenFactor and return it as
5440 // chain, then the TokenFactor is both a predecessor (operand) of the
5441 // user as well as a successor (the TF operands are flagged to the user).
5442 // c1, f1 = CopyToReg
5443 // c2, f2 = CopyToReg
5444 // c3 = TokenFactor c1, c2
5445 // ...
5446 // = op c3, ..., f2
5447 Chain = Chains[NumRegs-1];
5448 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005449 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Bill Wendlingec72e322009-12-22 01:11:43 +00005450
5451 if (DisableScheduling)
5452 DAG.AssignOrdering(Chain.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005453}
5454
5455/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005456/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005457/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005458void RegsForValue::AddInlineAsmOperands(unsigned Code,
5459 bool HasMatching,unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +00005460 SelectionDAG &DAG, unsigned Order,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005461 std::vector<SDValue> &Ops) const {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005462 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5463 unsigned Flag = Code | (Regs.size() << 3);
5464 if (HasMatching)
5465 Flag |= 0x80000000 | (MatchingIdx << 16);
Dale Johannesen99499332009-12-23 07:32:51 +00005466 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00005467 Ops.push_back(Res);
5468
5469 if (DisableScheduling)
5470 DAG.AssignOrdering(Res.getNode(), Order);
5471
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005472 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005473 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005474 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005475 for (unsigned i = 0; i != NumRegs; ++i) {
5476 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling651ad132009-12-22 01:25:10 +00005477 SDValue Res = DAG.getRegister(Regs[Reg++], RegisterVT);
5478 Ops.push_back(Res);
5479
5480 if (DisableScheduling)
5481 DAG.AssignOrdering(Res.getNode(), Order);
Chris Lattner58f15c42008-10-17 16:21:11 +00005482 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005483 }
5484}
5485
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005486/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005487/// i.e. it isn't a stack pointer or some other special register, return the
5488/// register class for the register. Otherwise, return null.
5489static const TargetRegisterClass *
5490isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5491 const TargetLowering &TLI,
5492 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005493 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005494 const TargetRegisterClass *FoundRC = 0;
5495 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5496 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005497 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005498
5499 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005500 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005501 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5502 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5503 I != E; ++I) {
5504 if (TLI.isTypeLegal(*I)) {
5505 // If we have already found this register in a different register class,
5506 // choose the one with the largest VT specified. For example, on
5507 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005508 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005509 ThisVT = *I;
5510 break;
5511 }
5512 }
5513 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005514
Owen Anderson825b72b2009-08-11 20:47:22 +00005515 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005516
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005517 // NOTE: This isn't ideal. In particular, this might allocate the
5518 // frame pointer in functions that need it (due to them not being taken
5519 // out of allocation, because a variable sized allocation hasn't been seen
5520 // yet). This is a slight code pessimization, but should still work.
5521 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5522 E = RC->allocation_order_end(MF); I != E; ++I)
5523 if (*I == Reg) {
5524 // We found a matching register class. Keep looking at others in case
5525 // we find one with larger registers that this physreg is also in.
5526 FoundRC = RC;
5527 FoundVT = ThisVT;
5528 break;
5529 }
5530 }
5531 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005532}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005533
5534
5535namespace llvm {
5536/// AsmOperandInfo - This contains information for each constraint that we are
5537/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005538class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005539 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005540public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 /// CallOperand - If this is the result output operand or a clobber
5542 /// this is null, otherwise it is the incoming operand to the CallInst.
5543 /// This gets modified as the asm is processed.
5544 SDValue CallOperand;
5545
5546 /// AssignedRegs - If this is a register or register class operand, this
5547 /// contains the set of register corresponding to the operand.
5548 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005549
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005550 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5551 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5552 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005553
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005554 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5555 /// busy in OutputRegs/InputRegs.
5556 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005557 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005558 std::set<unsigned> &InputRegs,
5559 const TargetRegisterInfo &TRI) const {
5560 if (isOutReg) {
5561 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5562 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5563 }
5564 if (isInReg) {
5565 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5566 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5567 }
5568 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005569
Owen Andersone50ed302009-08-10 22:56:29 +00005570 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005571 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005572 /// MVT::Other.
Owen Anderson1d0be152009-08-13 21:58:54 +00005573 EVT getCallOperandValEVT(LLVMContext &Context,
5574 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005575 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005576 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005577
Chris Lattner81249c92008-10-17 17:05:25 +00005578 if (isa<BasicBlock>(CallOperandVal))
5579 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005580
Chris Lattner81249c92008-10-17 17:05:25 +00005581 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005582
Chris Lattner81249c92008-10-17 17:05:25 +00005583 // If this is an indirect operand, the operand is a pointer to the
5584 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005585 if (isIndirect) {
5586 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5587 if (!PtrTy)
5588 llvm_report_error("Indirect operand for inline asm not a pointer!");
5589 OpTy = PtrTy->getElementType();
5590 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005591
Chris Lattner81249c92008-10-17 17:05:25 +00005592 // If OpTy is not a single value, it may be a struct/union that we
5593 // can tile with integers.
5594 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5595 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5596 switch (BitSize) {
5597 default: break;
5598 case 1:
5599 case 8:
5600 case 16:
5601 case 32:
5602 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005603 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005604 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005605 break;
5606 }
5607 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005608
Chris Lattner81249c92008-10-17 17:05:25 +00005609 return TLI.getValueType(OpTy, true);
5610 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005611
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005612private:
5613 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5614 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005615 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005616 const TargetRegisterInfo &TRI) {
5617 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5618 Regs.insert(Reg);
5619 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5620 for (; *Aliases; ++Aliases)
5621 Regs.insert(*Aliases);
5622 }
5623};
5624} // end llvm namespace.
5625
5626
5627/// GetRegistersForValue - Assign registers (virtual or physical) for the
5628/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005629/// register allocator to handle the assignment process. However, if the asm
5630/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005631/// allocation. This produces generally horrible, but correct, code.
5632///
5633/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005634/// Input and OutputRegs are the set of already allocated physical registers.
5635///
Dan Gohman2048b852009-11-23 18:04:58 +00005636void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005637GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005638 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005639 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005640 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005641
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005642 // Compute whether this value requires an input register, an output register,
5643 // or both.
5644 bool isOutReg = false;
5645 bool isInReg = false;
5646 switch (OpInfo.Type) {
5647 case InlineAsm::isOutput:
5648 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005649
5650 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005651 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005652 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005653 break;
5654 case InlineAsm::isInput:
5655 isInReg = true;
5656 isOutReg = false;
5657 break;
5658 case InlineAsm::isClobber:
5659 isOutReg = true;
5660 isInReg = true;
5661 break;
5662 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005663
5664
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005665 MachineFunction &MF = DAG.getMachineFunction();
5666 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005667
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005668 // If this is a constraint for a single physreg, or a constraint for a
5669 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005670 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005671 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5672 OpInfo.ConstraintVT);
5673
5674 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005675 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005676 // If this is a FP input in an integer register (or visa versa) insert a bit
5677 // cast of the input value. More generally, handle any case where the input
5678 // value disagrees with the register class we plan to stick this in.
5679 if (OpInfo.Type == InlineAsm::isInput &&
5680 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005681 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005682 // types are identical size, use a bitcast to convert (e.g. two differing
5683 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005684 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005685 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005686 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005687 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005688 OpInfo.ConstraintVT = RegVT;
5689 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5690 // If the input is a FP value and we want it in FP registers, do a
5691 // bitcast to the corresponding integer type. This turns an f64 value
5692 // into i64, which can be passed with two i32 values on a 32-bit
5693 // machine.
Owen Anderson23b9b192009-08-12 00:36:31 +00005694 RegVT = EVT::getIntegerVT(Context,
5695 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005696 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005697 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005698 OpInfo.ConstraintVT = RegVT;
5699 }
Bill Wendling651ad132009-12-22 01:25:10 +00005700
5701 if (DisableScheduling)
5702 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Chris Lattner01426e12008-10-21 00:45:36 +00005703 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005704
Owen Anderson23b9b192009-08-12 00:36:31 +00005705 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005706 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005707
Owen Andersone50ed302009-08-10 22:56:29 +00005708 EVT RegVT;
5709 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005710
5711 // If this is a constraint for a specific physical register, like {r17},
5712 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005713 if (unsigned AssignedReg = PhysReg.first) {
5714 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005715 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005716 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005718 // Get the actual register value type. This is important, because the user
5719 // may have asked for (e.g.) the AX register in i32 type. We need to
5720 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005721 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005722
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005723 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005724 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005725
5726 // If this is an expanded reference, add the rest of the regs to Regs.
5727 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005728 TargetRegisterClass::iterator I = RC->begin();
5729 for (; *I != AssignedReg; ++I)
5730 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005731
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005732 // Already added the first reg.
5733 --NumRegs; ++I;
5734 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005735 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005736 Regs.push_back(*I);
5737 }
5738 }
Bill Wendling651ad132009-12-22 01:25:10 +00005739
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005740 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5741 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5742 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5743 return;
5744 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005745
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005746 // Otherwise, if this was a reference to an LLVM register class, create vregs
5747 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005748 if (const TargetRegisterClass *RC = PhysReg.second) {
5749 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005750 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005751 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005752
Evan Chengfb112882009-03-23 08:01:15 +00005753 // Create the appropriate number of virtual registers.
5754 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5755 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005756 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005757
Evan Chengfb112882009-03-23 08:01:15 +00005758 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5759 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005760 }
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005761
5762 // This is a reference to a register class that doesn't directly correspond
5763 // to an LLVM register class. Allocate NumRegs consecutive, available,
5764 // registers from the class.
5765 std::vector<unsigned> RegClassRegs
5766 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5767 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005768
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005769 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5770 unsigned NumAllocated = 0;
5771 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5772 unsigned Reg = RegClassRegs[i];
5773 // See if this register is available.
5774 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5775 (isInReg && InputRegs.count(Reg))) { // Already used.
5776 // Make sure we find consecutive registers.
5777 NumAllocated = 0;
5778 continue;
5779 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005780
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005781 // Check to see if this register is allocatable (i.e. don't give out the
5782 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005783 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5784 if (!RC) { // Couldn't allocate this register.
5785 // Reset NumAllocated to make sure we return consecutive registers.
5786 NumAllocated = 0;
5787 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005788 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005789
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005790 // Okay, this register is good, we can use it.
5791 ++NumAllocated;
5792
5793 // If we allocated enough consecutive registers, succeed.
5794 if (NumAllocated == NumRegs) {
5795 unsigned RegStart = (i-NumAllocated)+1;
5796 unsigned RegEnd = i+1;
5797 // Mark all of the allocated registers used.
5798 for (unsigned i = RegStart; i != RegEnd; ++i)
5799 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005800
5801 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 OpInfo.ConstraintVT);
5803 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5804 return;
5805 }
5806 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005807
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808 // Otherwise, we couldn't allocate enough registers for this.
5809}
5810
Evan Chengda43bcf2008-09-24 00:05:32 +00005811/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5812/// processed uses a memory 'm' constraint.
5813static bool
5814hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005815 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005816 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5817 InlineAsm::ConstraintInfo &CI = CInfos[i];
5818 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5819 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5820 if (CType == TargetLowering::C_Memory)
5821 return true;
5822 }
Chris Lattner6c147292009-04-30 00:48:50 +00005823
5824 // Indirect operand accesses access memory.
5825 if (CI.isIndirect)
5826 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005827 }
5828
5829 return false;
5830}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005831
5832/// visitInlineAsm - Handle a call to an InlineAsm object.
5833///
Dan Gohman2048b852009-11-23 18:04:58 +00005834void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005835 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5836
5837 /// ConstraintOperands - Information about all of the constraints.
5838 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005839
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005840 std::set<unsigned> OutputRegs, InputRegs;
5841
5842 // Do a prepass over the constraints, canonicalizing them, and building up the
5843 // ConstraintOperands list.
5844 std::vector<InlineAsm::ConstraintInfo>
5845 ConstraintInfos = IA->ParseConstraints();
5846
Evan Chengda43bcf2008-09-24 00:05:32 +00005847 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Chris Lattner6c147292009-04-30 00:48:50 +00005848
5849 SDValue Chain, Flag;
5850
5851 // We won't need to flush pending loads if this asm doesn't touch
5852 // memory and is nonvolatile.
5853 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005854 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005855 else
5856 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005857
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005858 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5859 unsigned ResNo = 0; // ResNo - The result number of the next output.
5860 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5861 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5862 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005863
Owen Anderson825b72b2009-08-11 20:47:22 +00005864 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005865
5866 // Compute the value type for each operand.
5867 switch (OpInfo.Type) {
5868 case InlineAsm::isOutput:
5869 // Indirect outputs just consume an argument.
5870 if (OpInfo.isIndirect) {
5871 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5872 break;
5873 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005874
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005875 // The return value of the call is this value. As such, there is no
5876 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005877 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5878 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005879 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5880 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5881 } else {
5882 assert(ResNo == 0 && "Asm only has one result!");
5883 OpVT = TLI.getValueType(CS.getType());
5884 }
5885 ++ResNo;
5886 break;
5887 case InlineAsm::isInput:
5888 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5889 break;
5890 case InlineAsm::isClobber:
5891 // Nothing to do.
5892 break;
5893 }
5894
5895 // If this is an input or an indirect output, process the call argument.
5896 // BasicBlocks are labels, currently appearing only in asm's.
5897 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005898 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005899 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5900
Chris Lattner81249c92008-10-17 17:05:25 +00005901 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005902 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005903 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005904 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005905 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005906
Owen Anderson1d0be152009-08-13 21:58:54 +00005907 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005908 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005909
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005910 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005911 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005912
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005913 // Second pass over the constraints: compute which constraint option to use
5914 // and assign registers to constraints that want a specific physreg.
5915 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5916 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005917
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005918 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005919 // matching input. If their types mismatch, e.g. one is an integer, the
5920 // other is floating point, or their sizes are different, flag it as an
5921 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005922 if (OpInfo.hasMatchingInput()) {
5923 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5924 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005925 if ((OpInfo.ConstraintVT.isInteger() !=
5926 Input.ConstraintVT.isInteger()) ||
5927 (OpInfo.ConstraintVT.getSizeInBits() !=
5928 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005929 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005930 " with a matching output constraint of incompatible"
5931 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005932 }
5933 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005934 }
5935 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005936
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005937 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005938 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005939
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005940 // If this is a memory input, and if the operand is not indirect, do what we
5941 // need to to provide an address for the memory input.
5942 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5943 !OpInfo.isIndirect) {
5944 assert(OpInfo.Type == InlineAsm::isInput &&
5945 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005946
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005947 // Memory operands really want the address of the value. If we don't have
5948 // an indirect input, put it in the constpool if we can, otherwise spill
5949 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005950
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005951 // If the operand is a float, integer, or vector constant, spill to a
5952 // constant pool entry to get its address.
5953 Value *OpVal = OpInfo.CallOperandVal;
5954 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5955 isa<ConstantVector>(OpVal)) {
5956 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5957 TLI.getPointerTy());
5958 } else {
5959 // Otherwise, create a stack slot and emit a store to it before the
5960 // asm.
5961 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005962 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005963 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5964 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005965 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005966 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005967 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005968 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969 OpInfo.CallOperand = StackSlot;
5970 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005971
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005972 // There is no longer a Value* corresponding to this operand.
5973 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005974
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005975 // It is now an indirect operand.
5976 OpInfo.isIndirect = true;
5977 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005978
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005979 // If this constraint is for a specific register, allocate it before
5980 // anything else.
5981 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005982 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005984
Bill Wendling651ad132009-12-22 01:25:10 +00005985 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005986
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005987 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005988 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005989 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5990 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005992 // C_Register operands have already been allocated, Other/Memory don't need
5993 // to be.
5994 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005995 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005996 }
5997
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005998 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5999 std::vector<SDValue> AsmNodeOperands;
6000 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6001 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00006002 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006003
6004
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006005 // Loop over all of the inputs, copying the operand values into the
6006 // appropriate registers and processing the output regs.
6007 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006008
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006009 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6010 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006011
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006012 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6013 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6014
6015 switch (OpInfo.Type) {
6016 case InlineAsm::isOutput: {
6017 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6018 OpInfo.ConstraintType != TargetLowering::C_Register) {
6019 // Memory output, or 'other' output (e.g. 'X' constraint).
6020 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6021
6022 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006023 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6024 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006025 TLI.getPointerTy()));
6026 AsmNodeOperands.push_back(OpInfo.CallOperand);
6027 break;
6028 }
6029
6030 // Otherwise, this is a register or register class output.
6031
6032 // Copy the output from the appropriate register. Find a register that
6033 // we can use.
6034 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006035 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006036 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006037 }
6038
6039 // If this is an indirect operand, store through the pointer after the
6040 // asm.
6041 if (OpInfo.isIndirect) {
6042 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6043 OpInfo.CallOperandVal));
6044 } else {
6045 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00006046 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
6047 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006048 // Concatenate this output onto the outputs list.
6049 RetValRegs.append(OpInfo.AssignedRegs);
6050 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006052 // Add information to the INLINEASM node to know that this register is
6053 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00006054 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
6055 6 /* EARLYCLOBBER REGDEF */ :
6056 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00006057 false,
6058 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006059 DAG, SDNodeOrder,
6060 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006061 break;
6062 }
6063 case InlineAsm::isInput: {
6064 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006065
Chris Lattner6bdcda32008-10-17 16:47:46 +00006066 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006067 // If this is required to match an output register we have already set,
6068 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006069 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006070
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006071 // Scan until we find the definition we already emitted of this operand.
6072 // When we find it, create a RegsForValue operand.
6073 unsigned CurOp = 2; // The first operand.
6074 for (; OperandNo; --OperandNo) {
6075 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006076 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006077 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006078 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
6079 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
6080 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006082 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006083 }
6084
Evan Cheng697cbbf2009-03-20 18:03:34 +00006085 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006086 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006087 if ((OpFlag & 7) == 2 /*REGDEF*/
6088 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
6089 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00006090 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006091 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00006092 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00006093 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006094 RegsForValue MatchedRegs;
6095 MatchedRegs.TLI = &TLI;
6096 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00006097 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006098 MatchedRegs.RegVTs.push_back(RegVT);
6099 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006100 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00006101 i != e; ++i)
6102 MatchedRegs.Regs.
6103 push_back(RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006104
6105 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00006106 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006107 SDNodeOrder, Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00006108 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
6109 true, OpInfo.getMatchedOperand(),
Bill Wendling651ad132009-12-22 01:25:10 +00006110 DAG, SDNodeOrder, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006111 break;
6112 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006113 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
6114 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
6115 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006116 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00006117 // See InlineAsm.h isUseOperandTiedToDef.
6118 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00006119 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006120 TLI.getPointerTy()));
6121 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6122 break;
6123 }
6124 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006125
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006126 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006127 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006128 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006130 std::vector<SDValue> Ops;
6131 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00006132 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006133 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006134 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00006135 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006136 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006137
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006138 // Add information to the INLINEASM node to know about this input.
6139 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006140 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006141 TLI.getPointerTy()));
6142 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6143 break;
6144 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
6145 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
6146 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6147 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006148
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006149 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006150 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6151 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006152 TLI.getPointerTy()));
6153 AsmNodeOperands.push_back(InOperandVal);
6154 break;
6155 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006156
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006157 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6158 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6159 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006160 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006161 "Don't know how to handle indirect register inputs yet!");
6162
6163 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00006164 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006165 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006166 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00006167 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006168
Dale Johannesen66978ee2009-01-31 02:22:37 +00006169 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006170 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006171
Evan Cheng697cbbf2009-03-20 18:03:34 +00006172 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006173 DAG, SDNodeOrder,
6174 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006175 break;
6176 }
6177 case InlineAsm::isClobber: {
6178 // Add the clobbered value to the operand list, so that the register
6179 // allocator is aware that the physreg got clobbered.
6180 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00006181 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Bill Wendling651ad132009-12-22 01:25:10 +00006182 false, 0, DAG, SDNodeOrder,
6183 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006184 break;
6185 }
6186 }
6187 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006189 // Finish up input operands.
6190 AsmNodeOperands[0] = Chain;
6191 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006192
Dale Johannesen66978ee2009-01-31 02:22:37 +00006193 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006194 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006195 &AsmNodeOperands[0], AsmNodeOperands.size());
6196 Flag = Chain.getValue(1);
6197
6198 // If this asm returns a register value, copy the result from that register
6199 // and set it as the value of the call.
6200 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006201 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006202 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006203
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006204 // FIXME: Why don't we do this for inline asms with MRVs?
6205 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006206 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006207
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006208 // If any of the results of the inline asm is a vector, it may have the
6209 // wrong width/num elts. This can happen for register classes that can
6210 // contain multiple different value types. The preg or vreg allocated may
6211 // not have the same VT as was expected. Convert it to the right type
6212 // with bit_convert.
6213 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006214 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006215 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006216
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006217 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006218 ResultType.isInteger() && Val.getValueType().isInteger()) {
6219 // If a result value was tied to an input value, the computed result may
6220 // have a wider width than the expected result. Extract the relevant
6221 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006222 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006223 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006224
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006225 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006226 }
Dan Gohman95915732008-10-18 01:03:45 +00006227
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006228 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006229 // Don't need to use this as a chain in this case.
6230 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6231 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006232 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006233
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006234 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006235
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006236 // Process indirect outputs, first output all of the flagged copies out of
6237 // physregs.
6238 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6239 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
6240 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00006241 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006242 SDNodeOrder, Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006243 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00006244
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006245 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006246
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006247 // Emit the non-flagged stores from the physregs.
6248 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006249 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6250 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6251 StoresToEmit[i].first,
6252 getValue(StoresToEmit[i].second),
6253 StoresToEmit[i].second, 0);
6254 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006255 }
6256
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006257 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006258 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006259 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006260
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006261 DAG.setRoot(Chain);
6262}
6263
Dan Gohman2048b852009-11-23 18:04:58 +00006264void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006265 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6266 MVT::Other, getRoot(),
6267 getValue(I.getOperand(1)),
6268 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006269}
6270
Dan Gohman2048b852009-11-23 18:04:58 +00006271void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006272 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6273 getRoot(), getValue(I.getOperand(0)),
6274 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006275 setValue(&I, V);
6276 DAG.setRoot(V.getValue(1));
6277}
6278
Dan Gohman2048b852009-11-23 18:04:58 +00006279void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006280 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6281 MVT::Other, getRoot(),
6282 getValue(I.getOperand(1)),
6283 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006284}
6285
Dan Gohman2048b852009-11-23 18:04:58 +00006286void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006287 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6288 MVT::Other, getRoot(),
6289 getValue(I.getOperand(1)),
6290 getValue(I.getOperand(2)),
6291 DAG.getSrcValue(I.getOperand(1)),
6292 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006293}
6294
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006295/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006296/// implementation, which just calls LowerCall.
6297/// FIXME: When all targets are
6298/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006299std::pair<SDValue, SDValue>
6300TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6301 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006302 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006303 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006304 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006305 SDValue Callee,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006306 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl,
6307 unsigned Order) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00006308 assert((!isTailCall || PerformTailCallOpt) &&
6309 "isTailCall set when tail-call optimizations are disabled!");
6310
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006311 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006312 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006313 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006314 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006315 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6316 for (unsigned Value = 0, NumValues = ValueVTs.size();
6317 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006318 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006319 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006320 SDValue Op = SDValue(Args[i].Node.getNode(),
6321 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006322 ISD::ArgFlagsTy Flags;
6323 unsigned OriginalAlignment =
6324 getTargetData()->getABITypeAlignment(ArgTy);
6325
6326 if (Args[i].isZExt)
6327 Flags.setZExt();
6328 if (Args[i].isSExt)
6329 Flags.setSExt();
6330 if (Args[i].isInReg)
6331 Flags.setInReg();
6332 if (Args[i].isSRet)
6333 Flags.setSRet();
6334 if (Args[i].isByVal) {
6335 Flags.setByVal();
6336 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6337 const Type *ElementTy = Ty->getElementType();
6338 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006339 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006340 // For ByVal, alignment should come from FE. BE will guess if this
6341 // info is not there but there are cases it cannot get right.
6342 if (Args[i].Alignment)
6343 FrameAlign = Args[i].Alignment;
6344 Flags.setByValAlign(FrameAlign);
6345 Flags.setByValSize(FrameSize);
6346 }
6347 if (Args[i].isNest)
6348 Flags.setNest();
6349 Flags.setOrigAlign(OriginalAlignment);
6350
Owen Anderson23b9b192009-08-12 00:36:31 +00006351 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6352 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006353 SmallVector<SDValue, 4> Parts(NumParts);
6354 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6355
6356 if (Args[i].isSExt)
6357 ExtendKind = ISD::SIGN_EXTEND;
6358 else if (Args[i].isZExt)
6359 ExtendKind = ISD::ZERO_EXTEND;
6360
Bill Wendling3ea3c242009-12-22 02:10:19 +00006361 getCopyToParts(DAG, dl, Order, Op, &Parts[0], NumParts,
6362 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006363
Dan Gohman98ca4f22009-08-05 01:29:28 +00006364 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006365 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006366 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6367 if (NumParts > 1 && j == 0)
6368 MyFlags.Flags.setSplit();
6369 else if (j != 0)
6370 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006371
Dan Gohman98ca4f22009-08-05 01:29:28 +00006372 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006373 }
6374 }
6375 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006376
Dan Gohman98ca4f22009-08-05 01:29:28 +00006377 // Handle the incoming return values from the call.
6378 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006379 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006380 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006381 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006382 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006383 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6384 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006385 for (unsigned i = 0; i != NumRegs; ++i) {
6386 ISD::InputArg MyFlags;
6387 MyFlags.VT = RegisterVT;
6388 MyFlags.Used = isReturnValueUsed;
6389 if (RetSExt)
6390 MyFlags.Flags.setSExt();
6391 if (RetZExt)
6392 MyFlags.Flags.setZExt();
6393 if (isInreg)
6394 MyFlags.Flags.setInReg();
6395 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006396 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006397 }
6398
Dan Gohman98ca4f22009-08-05 01:29:28 +00006399 // Check if target-dependent constraints permit a tail call here.
6400 // Target-independent constraints should be checked by the caller.
6401 if (isTailCall &&
6402 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6403 isTailCall = false;
6404
6405 SmallVector<SDValue, 4> InVals;
6406 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6407 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006408
6409 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006410 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006411 "LowerCall didn't return a valid chain!");
6412 assert((!isTailCall || InVals.empty()) &&
6413 "LowerCall emitted a return value for a tail call!");
6414 assert((isTailCall || InVals.size() == Ins.size()) &&
6415 "LowerCall didn't emit the correct number of values!");
6416 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6417 assert(InVals[i].getNode() &&
6418 "LowerCall emitted a null value!");
6419 assert(Ins[i].VT == InVals[i].getValueType() &&
6420 "LowerCall emitted a value with the wrong type!");
6421 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006422
Bill Wendling3ea3c242009-12-22 02:10:19 +00006423 if (DisableScheduling)
6424 DAG.AssignOrdering(Chain.getNode(), Order);
6425
Dan Gohman98ca4f22009-08-05 01:29:28 +00006426 // For a tail call, the return value is merely live-out and there aren't
6427 // any nodes in the DAG representing it. Return a special value to
6428 // indicate that a tail call has been emitted and no more Instructions
6429 // should be processed in the current block.
6430 if (isTailCall) {
6431 DAG.setRoot(Chain);
6432 return std::make_pair(SDValue(), SDValue());
6433 }
6434
6435 // Collect the legal value parts into potentially illegal values
6436 // that correspond to the original function's return values.
6437 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6438 if (RetSExt)
6439 AssertOp = ISD::AssertSext;
6440 else if (RetZExt)
6441 AssertOp = ISD::AssertZext;
6442 SmallVector<SDValue, 4> ReturnValues;
6443 unsigned CurReg = 0;
6444 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006445 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006446 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6447 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006448
6449 SDValue ReturnValue =
Bill Wendling3ea3c242009-12-22 02:10:19 +00006450 getCopyFromParts(DAG, dl, Order, &InVals[CurReg], NumRegs,
6451 RegisterVT, VT, AssertOp);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006452 ReturnValues.push_back(ReturnValue);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006453 if (DisableScheduling)
6454 DAG.AssignOrdering(ReturnValue.getNode(), Order);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006455 CurReg += NumRegs;
6456 }
6457
6458 // For a function returning void, there is no return value. We can't create
6459 // such a node, so we just return a null return value in that case. In
6460 // that case, nothing will actualy look at the value.
6461 if (ReturnValues.empty())
6462 return std::make_pair(SDValue(), Chain);
6463
6464 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6465 DAG.getVTList(&RetTys[0], RetTys.size()),
6466 &ReturnValues[0], ReturnValues.size());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006467 if (DisableScheduling)
6468 DAG.AssignOrdering(Res.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006469 return std::make_pair(Res, Chain);
6470}
6471
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006472void TargetLowering::LowerOperationWrapper(SDNode *N,
6473 SmallVectorImpl<SDValue> &Results,
6474 SelectionDAG &DAG) {
6475 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006476 if (Res.getNode())
6477 Results.push_back(Res);
6478}
6479
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006480SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006481 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006482 return SDValue();
6483}
6484
Dan Gohman2048b852009-11-23 18:04:58 +00006485void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006486 SDValue Op = getValue(V);
6487 assert((Op.getOpcode() != ISD::CopyFromReg ||
6488 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6489 "Copy from a reg to the same reg!");
6490 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6491
Owen Anderson23b9b192009-08-12 00:36:31 +00006492 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006493 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +00006494 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006495 PendingExports.push_back(Chain);
6496}
6497
6498#include "llvm/CodeGen/SelectionDAGISel.h"
6499
Dan Gohman8c2b5252009-10-30 01:27:03 +00006500void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006501 // If this is the entry block, emit arguments.
6502 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006503 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006504 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006505 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006506 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006507 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006508
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006509 // Check whether the function can return without sret-demotion.
6510 SmallVector<EVT, 4> OutVTs;
6511 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006512 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
6513 OutVTs, OutsFlags, TLI);
6514 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6515
6516 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00006517 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006518 if (!FLI.CanLowerReturn) {
6519 // Put in an sret pointer parameter before all the other parameters.
6520 SmallVector<EVT, 1> ValueVTs;
6521 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6522
6523 // NOTE: Assuming that a pointer will never break down to more than one VT
6524 // or one register.
6525 ISD::ArgFlagsTy Flags;
6526 Flags.setSRet();
6527 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6528 ISD::InputArg RetArg(Flags, RegisterVT, true);
6529 Ins.push_back(RetArg);
6530 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006531
Dan Gohman98ca4f22009-08-05 01:29:28 +00006532 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006533 unsigned Idx = 1;
6534 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6535 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006536 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006537 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6538 bool isArgValueUsed = !I->use_empty();
6539 for (unsigned Value = 0, NumValues = ValueVTs.size();
6540 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006541 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006542 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006543 ISD::ArgFlagsTy Flags;
6544 unsigned OriginalAlignment =
6545 TD->getABITypeAlignment(ArgTy);
6546
6547 if (F.paramHasAttr(Idx, Attribute::ZExt))
6548 Flags.setZExt();
6549 if (F.paramHasAttr(Idx, Attribute::SExt))
6550 Flags.setSExt();
6551 if (F.paramHasAttr(Idx, Attribute::InReg))
6552 Flags.setInReg();
6553 if (F.paramHasAttr(Idx, Attribute::StructRet))
6554 Flags.setSRet();
6555 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6556 Flags.setByVal();
6557 const PointerType *Ty = cast<PointerType>(I->getType());
6558 const Type *ElementTy = Ty->getElementType();
6559 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6560 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6561 // For ByVal, alignment should be passed from FE. BE will guess if
6562 // this info is not there but there are cases it cannot get right.
6563 if (F.getParamAlignment(Idx))
6564 FrameAlign = F.getParamAlignment(Idx);
6565 Flags.setByValAlign(FrameAlign);
6566 Flags.setByValSize(FrameSize);
6567 }
6568 if (F.paramHasAttr(Idx, Attribute::Nest))
6569 Flags.setNest();
6570 Flags.setOrigAlign(OriginalAlignment);
6571
Owen Anderson23b9b192009-08-12 00:36:31 +00006572 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6573 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006574 for (unsigned i = 0; i != NumRegs; ++i) {
6575 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6576 if (NumRegs > 1 && i == 0)
6577 MyFlags.Flags.setSplit();
6578 // if it isn't first piece, alignment must be 1
6579 else if (i > 0)
6580 MyFlags.Flags.setOrigAlign(1);
6581 Ins.push_back(MyFlags);
6582 }
6583 }
6584 }
6585
6586 // Call the target to set up the argument values.
6587 SmallVector<SDValue, 8> InVals;
6588 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6589 F.isVarArg(), Ins,
6590 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006591
6592 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006593 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006594 "LowerFormalArguments didn't return a valid chain!");
6595 assert(InVals.size() == Ins.size() &&
6596 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006597 DEBUG({
6598 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6599 assert(InVals[i].getNode() &&
6600 "LowerFormalArguments emitted a null value!");
6601 assert(Ins[i].VT == InVals[i].getValueType() &&
6602 "LowerFormalArguments emitted a value with the wrong type!");
6603 }
6604 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006605
Dan Gohman5e866062009-08-06 15:37:27 +00006606 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006607 DAG.setRoot(NewRoot);
6608
6609 // Set up the argument values.
6610 unsigned i = 0;
6611 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006612 if (!FLI.CanLowerReturn) {
6613 // Create a virtual register for the sret pointer, and put in a copy
6614 // from the sret argument into it.
6615 SmallVector<EVT, 1> ValueVTs;
6616 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6617 EVT VT = ValueVTs[0];
6618 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6619 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling3ea58b62009-12-22 21:35:02 +00006620 SDValue ArgValue = getCopyFromParts(DAG, dl, 0, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006621 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006622
Dan Gohman2048b852009-11-23 18:04:58 +00006623 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006624 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6625 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6626 FLI.DemoteRegister = SRetReg;
Dan Gohman2048b852009-11-23 18:04:58 +00006627 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(), SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006628 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006629
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006630 // i indexes lowered arguments. Bump it past the hidden sret argument.
6631 // Idx indexes LLVM arguments. Don't touch it.
6632 ++i;
6633 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006634
Dan Gohman98ca4f22009-08-05 01:29:28 +00006635 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6636 ++I, ++Idx) {
6637 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006638 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006639 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006640 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006641 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006642 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006643 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6644 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006645
6646 if (!I->use_empty()) {
6647 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6648 if (F.paramHasAttr(Idx, Attribute::SExt))
6649 AssertOp = ISD::AssertSext;
6650 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6651 AssertOp = ISD::AssertZext;
6652
Bill Wendling3ea58b62009-12-22 21:35:02 +00006653 ArgValues.push_back(getCopyFromParts(DAG, dl, 0, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006654 NumParts, PartVT, VT,
6655 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006656 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006657
Dan Gohman98ca4f22009-08-05 01:29:28 +00006658 i += NumParts;
6659 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006660
Dan Gohman98ca4f22009-08-05 01:29:28 +00006661 if (!I->use_empty()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +00006662 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6663 SDB->getCurDebugLoc());
6664 SDB->setValue(I, Res);
6665
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006666 // If this argument is live outside of the entry block, insert a copy from
6667 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006668 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006669 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006670 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006671
Dan Gohman98ca4f22009-08-05 01:29:28 +00006672 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006673
6674 // Finally, if the target has anything special to do, allow it to do so.
6675 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006676 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006677}
6678
6679/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6680/// ensure constants are generated when needed. Remember the virtual registers
6681/// that need to be added to the Machine PHI nodes as input. We cannot just
6682/// directly add them, because expansion might result in multiple MBB's for one
6683/// BB. As such, the start of the BB might correspond to a different MBB than
6684/// the end.
6685///
6686void
6687SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6688 TerminatorInst *TI = LLVMBB->getTerminator();
6689
6690 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6691
6692 // Check successor nodes' PHI nodes that expect a constant to be available
6693 // from this block.
6694 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6695 BasicBlock *SuccBB = TI->getSuccessor(succ);
6696 if (!isa<PHINode>(SuccBB->begin())) continue;
6697 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006698
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006699 // If this terminator has multiple identical successors (common for
6700 // switches), only handle each succ once.
6701 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006702
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006703 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6704 PHINode *PN;
6705
6706 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6707 // nodes and Machine PHI nodes, but the incoming operands have not been
6708 // emitted yet.
6709 for (BasicBlock::iterator I = SuccBB->begin();
6710 (PN = dyn_cast<PHINode>(I)); ++I) {
6711 // Ignore dead phi's.
6712 if (PN->use_empty()) continue;
6713
6714 unsigned Reg;
6715 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6716
6717 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006718 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006719 if (RegOut == 0) {
6720 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006721 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006722 }
6723 Reg = RegOut;
6724 } else {
6725 Reg = FuncInfo->ValueMap[PHIOp];
6726 if (Reg == 0) {
6727 assert(isa<AllocaInst>(PHIOp) &&
6728 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6729 "Didn't codegen value into a register!??");
6730 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006731 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006732 }
6733 }
6734
6735 // Remember that this register needs to added to the machine PHI node as
6736 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006737 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006738 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6739 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006740 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006741 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006742 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006743 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006744 Reg += NumRegisters;
6745 }
6746 }
6747 }
Dan Gohman2048b852009-11-23 18:04:58 +00006748 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006749}
6750
Dan Gohman3df24e62008-09-03 23:12:08 +00006751/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6752/// supports legal types, and it emits MachineInstrs directly instead of
6753/// creating SelectionDAG nodes.
6754///
6755bool
6756SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6757 FastISel *F) {
6758 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006759
Dan Gohman3df24e62008-09-03 23:12:08 +00006760 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006761 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006762
6763 // Check successor nodes' PHI nodes that expect a constant to be available
6764 // from this block.
6765 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6766 BasicBlock *SuccBB = TI->getSuccessor(succ);
6767 if (!isa<PHINode>(SuccBB->begin())) continue;
6768 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006769
Dan Gohman3df24e62008-09-03 23:12:08 +00006770 // If this terminator has multiple identical successors (common for
6771 // switches), only handle each succ once.
6772 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006773
Dan Gohman3df24e62008-09-03 23:12:08 +00006774 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6775 PHINode *PN;
6776
6777 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6778 // nodes and Machine PHI nodes, but the incoming operands have not been
6779 // emitted yet.
6780 for (BasicBlock::iterator I = SuccBB->begin();
6781 (PN = dyn_cast<PHINode>(I)); ++I) {
6782 // Ignore dead phi's.
6783 if (PN->use_empty()) continue;
6784
6785 // Only handle legal types. Two interesting things to note here. First,
6786 // by bailing out early, we may leave behind some dead instructions,
6787 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6788 // own moves. Second, this check is necessary becuase FastISel doesn't
6789 // use CreateRegForValue to create registers, so it always creates
6790 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006791 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006792 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6793 // Promote MVT::i1.
6794 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006795 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006796 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006797 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006798 return false;
6799 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006800 }
6801
6802 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6803
6804 unsigned Reg = F->getRegForValue(PHIOp);
6805 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006806 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006807 return false;
6808 }
Dan Gohman2048b852009-11-23 18:04:58 +00006809 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006810 }
6811 }
6812
6813 return true;
6814}