blob: a3fb34576ac0730884e8fab0ab816f406d06e4de [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)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +000073 /// that a particular set of values is assigned, and the type information
74 /// about the value. The most common situation is to represent one value at a
75 /// time, but struct or array values are handled element-wise as multiple
76 /// values. The splitting of aggregates is performed recursively, so that we
77 /// never have aggregate-typed registers. The values at this point do not
78 /// necessarily have legal types, so each value may require one or more
79 /// registers of some 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 =
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000246 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
Owen Anderson23b9b192009-08-12 00:36:31 +0000247 NumIntermediates, RegisterVT);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000248 assert(NumRegs == NumParts
249 && "Part count doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000250 NumParts = NumRegs; // Silence a compiler warning.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000251 assert(RegisterVT == PartVT
252 && "Part type doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000253 assert(RegisterVT == Parts[0].getValueType() &&
254 "Part type doesn't match part!");
255
256 // Assemble the parts into intermediate operands.
257 SmallVector<SDValue, 8> Ops(NumIntermediates);
258 if (NumIntermediates == NumParts) {
259 // If the register was not expanded, truncate or copy the value,
260 // as appropriate.
261 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000262 Ops[i] = getCopyFromParts(DAG, dl, Order, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000263 PartVT, IntermediateVT);
264 } else if (NumParts > 0) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000265 // If the intermediate type was expanded, build the intermediate
266 // operands from the parts.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000267 assert(NumParts % NumIntermediates == 0 &&
268 "Must expand into a divisible number of parts!");
269 unsigned Factor = NumParts / NumIntermediates;
270 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000271 Ops[i] = getCopyFromParts(DAG, dl, Order, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000272 PartVT, IntermediateVT);
273 }
274
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000275 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
276 // intermediate operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000277 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000278 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000279 ValueVT, &Ops[0], NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000280 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000281 } else if (PartVT.isFloatingPoint()) {
282 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000284 "Unexpected split");
285 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
287 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000288 if (TLI.isBigEndian())
289 std::swap(Lo, Hi);
290 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000291
292 if (DisableScheduling) {
293 DAG.AssignOrdering(Hi.getNode(), Order);
294 DAG.AssignOrdering(Lo.getNode(), Order);
295 DAG.AssignOrdering(Val.getNode(), Order);
296 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000297 } else {
298 // FP split into integer parts (soft fp)
299 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
300 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000301 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling3ea3c242009-12-22 02:10:19 +0000302 Val = getCopyFromParts(DAG, dl, Order, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000303 }
304 }
305
306 // There is now one part, held in Val. Correct it to match ValueVT.
307 PartVT = Val.getValueType();
308
309 if (PartVT == ValueVT)
310 return Val;
311
312 if (PartVT.isVector()) {
313 assert(ValueVT.isVector() && "Unknown vector conversion!");
Bill Wendling3ea3c242009-12-22 02:10:19 +0000314 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
315 if (DisableScheduling)
316 DAG.AssignOrdering(Res.getNode(), Order);
317 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000318 }
319
320 if (ValueVT.isVector()) {
321 assert(ValueVT.getVectorElementType() == PartVT &&
322 ValueVT.getVectorNumElements() == 1 &&
323 "Only trivial scalar-to-vector conversions should get here!");
Bill Wendling3ea3c242009-12-22 02:10:19 +0000324 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
325 if (DisableScheduling)
326 DAG.AssignOrdering(Res.getNode(), Order);
327 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000328 }
329
330 if (PartVT.isInteger() &&
331 ValueVT.isInteger()) {
332 if (ValueVT.bitsLT(PartVT)) {
333 // For a truncate, see if we have any information to
334 // indicate whether the truncated bits will always be
335 // zero or sign-extension.
336 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000337 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000338 DAG.getValueType(ValueVT));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000339 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
340 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
341 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
342 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000343 } else {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000344 Val = DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
345 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
346 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000347 }
348 }
349
350 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000351 if (ValueVT.bitsLT(Val.getValueType())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000352 // FP_ROUND's are always exact here.
Bill Wendling3ea3c242009-12-22 02:10:19 +0000353 Val = DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
354 DAG.getIntPtrConstant(1));
355 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
356 return Val;
357 }
358
359 Val = DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
360 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
361 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000362 }
363
Bill Wendling3ea3c242009-12-22 02:10:19 +0000364 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
365 Val = DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
366 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
367 return Val;
368 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000369
Torok Edwinc23197a2009-07-14 16:55:14 +0000370 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000371 return SDValue();
372}
373
374/// getCopyToParts - Create a series of nodes that contain the specified value
375/// split into legal parts. If the parts contain more bits than Val, then, for
376/// integers, ExtendKind can be used to specify how to generate the extra bits.
Bill Wendling3ea3c242009-12-22 02:10:19 +0000377static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, unsigned Order,
378 SDValue Val, SDValue *Parts, unsigned NumParts,
379 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000380 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000381 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000382 EVT PtrVT = TLI.getPointerTy();
383 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000384 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000385 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000386 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
387
388 if (!NumParts)
389 return;
390
391 if (!ValueVT.isVector()) {
392 if (PartVT == ValueVT) {
393 assert(NumParts == 1 && "No-op copy with multiple parts!");
394 Parts[0] = Val;
395 return;
396 }
397
398 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
399 // If the parts cover more bits than the value has, promote the value.
400 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
401 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000402 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000403 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000404 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000405 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000406 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000407 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000408 }
409 } else if (PartBits == ValueVT.getSizeInBits()) {
410 // Different types of the same size.
411 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000412 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000413 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
414 // If the parts cover less bits than value has, truncate the value.
415 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000416 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000417 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000418 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000419 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000420 }
421 }
422
Bill Wendling3ea3c242009-12-22 02:10:19 +0000423 if (DisableScheduling) DAG.AssignOrdering(Val.getNode(), Order);
424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000425 // The value may have changed - recompute ValueVT.
426 ValueVT = Val.getValueType();
427 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
428 "Failed to tile the value with PartVT!");
429
430 if (NumParts == 1) {
431 assert(PartVT == ValueVT && "Type conversion failed!");
432 Parts[0] = Val;
433 return;
434 }
435
436 // Expand the value into multiple parts.
437 if (NumParts & (NumParts - 1)) {
438 // The number of parts is not a power of 2. Split off and copy the tail.
439 assert(PartVT.isInteger() && ValueVT.isInteger() &&
440 "Do not know what to expand to!");
441 unsigned RoundParts = 1 << Log2_32(NumParts);
442 unsigned RoundBits = RoundParts * PartBits;
443 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000444 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000445 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000446 TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000447 getCopyToParts(DAG, dl, Order, OddVal, Parts + RoundParts,
448 OddParts, PartVT);
449
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000450 if (TLI.isBigEndian())
451 // The odd parts were reversed by getCopyToParts - unreverse them.
452 std::reverse(Parts + RoundParts, Parts + NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000453
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000454 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000455 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000456 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000457
458 if (DisableScheduling) {
459 DAG.AssignOrdering(OddVal.getNode(), Order);
460 DAG.AssignOrdering(Val.getNode(), Order);
461 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000462 }
463
464 // The number of parts is a power of 2. Repeatedly bisect the value using
465 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000466 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Chris Lattnerf031e8a2010-01-01 03:32:16 +0000467 EVT::getIntegerVT(*DAG.getContext(),
468 ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000469 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000470
471 if (DisableScheduling)
472 DAG.AssignOrdering(Parts[0].getNode(), Order);
473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000474 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
475 for (unsigned i = 0; i < NumParts; i += StepSize) {
476 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000477 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000478 SDValue &Part0 = Parts[i];
479 SDValue &Part1 = Parts[i+StepSize/2];
480
Scott Michelfdc40a02009-02-17 22:15:04 +0000481 Part1 = 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(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000484 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000485 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000486 DAG.getConstant(0, PtrVT));
487
Bill Wendling3ea3c242009-12-22 02:10:19 +0000488 if (DisableScheduling) {
489 DAG.AssignOrdering(Part0.getNode(), Order);
490 DAG.AssignOrdering(Part1.getNode(), Order);
491 }
492
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000493 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000494 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000495 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000496 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000497 PartVT, Part1);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000498 if (DisableScheduling) {
499 DAG.AssignOrdering(Part0.getNode(), Order);
500 DAG.AssignOrdering(Part1.getNode(), Order);
501 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000502 }
503 }
504 }
505
506 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000507 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000508
509 return;
510 }
511
512 // Vector ValueVT.
513 if (NumParts == 1) {
514 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000515 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000516 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000517 } else {
518 assert(ValueVT.getVectorElementType() == PartVT &&
519 ValueVT.getVectorNumElements() == 1 &&
520 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000521 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000522 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000523 DAG.getConstant(0, PtrVT));
524 }
525 }
526
Bill Wendling3ea3c242009-12-22 02:10:19 +0000527 if (DisableScheduling)
528 DAG.AssignOrdering(Val.getNode(), Order);
529
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000530 Parts[0] = Val;
531 return;
532 }
533
534 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000535 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000536 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000537 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
538 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000539 unsigned NumElements = ValueVT.getVectorNumElements();
540
541 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
542 NumParts = NumRegs; // Silence a compiler warning.
543 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
544
545 // Split the vector into intermediate operands.
546 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000547 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000548 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000549 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000550 IntermediateVT, Val,
551 DAG.getConstant(i * (NumElements / NumIntermediates),
552 PtrVT));
553 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000554 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000555 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000556 DAG.getConstant(i, PtrVT));
557
Bill Wendling3ea3c242009-12-22 02:10:19 +0000558 if (DisableScheduling)
559 DAG.AssignOrdering(Ops[i].getNode(), Order);
560 }
561
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000562 // Split the intermediate operands into legal parts.
563 if (NumParts == NumIntermediates) {
564 // If the register was not expanded, promote or copy the value,
565 // as appropriate.
566 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000567 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000568 } else if (NumParts > 0) {
569 // If the intermediate type was expanded, split each the value into
570 // legal parts.
571 assert(NumParts % NumIntermediates == 0 &&
572 "Must expand into a divisible number of parts!");
573 unsigned Factor = NumParts / NumIntermediates;
574 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000575 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000576 }
577}
578
579
Dan Gohman2048b852009-11-23 18:04:58 +0000580void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000581 AA = &aa;
582 GFI = gfi;
583 TD = DAG.getTarget().getTargetData();
584}
585
586/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000587/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000588/// for a new block. This doesn't clear out information about
589/// additional blocks that are needed to complete switch lowering
590/// or PHI node updating; that information is cleared out as it is
591/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000592void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000593 NodeMap.clear();
594 PendingLoads.clear();
595 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000596 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000597 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000598 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000599 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000600}
601
602/// getRoot - Return the current virtual root of the Selection DAG,
603/// flushing any PendingLoad items. This must be done before emitting
604/// a store or any other node that may need to be ordered after any
605/// prior load instructions.
606///
Dan Gohman2048b852009-11-23 18:04:58 +0000607SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000608 if (PendingLoads.empty())
609 return DAG.getRoot();
610
611 if (PendingLoads.size() == 1) {
612 SDValue Root = PendingLoads[0];
613 DAG.setRoot(Root);
614 PendingLoads.clear();
615 return Root;
616 }
617
618 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000619 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000620 &PendingLoads[0], PendingLoads.size());
621 PendingLoads.clear();
622 DAG.setRoot(Root);
623 return Root;
624}
625
626/// getControlRoot - Similar to getRoot, but instead of flushing all the
627/// PendingLoad items, flush all the PendingExports items. It is necessary
628/// to do this before emitting a terminator instruction.
629///
Dan Gohman2048b852009-11-23 18:04:58 +0000630SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000631 SDValue Root = DAG.getRoot();
632
633 if (PendingExports.empty())
634 return Root;
635
636 // Turn all of the CopyToReg chains into one factored node.
637 if (Root.getOpcode() != ISD::EntryToken) {
638 unsigned i = 0, e = PendingExports.size();
639 for (; i != e; ++i) {
640 assert(PendingExports[i].getNode()->getNumOperands() > 1);
641 if (PendingExports[i].getNode()->getOperand(0) == Root)
642 break; // Don't add the root if we already indirectly depend on it.
643 }
644
645 if (i == e)
646 PendingExports.push_back(Root);
647 }
648
Owen Anderson825b72b2009-08-11 20:47:22 +0000649 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000650 &PendingExports[0],
651 PendingExports.size());
652 PendingExports.clear();
653 DAG.setRoot(Root);
654 return Root;
655}
656
Dan Gohman2048b852009-11-23 18:04:58 +0000657void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000658 visit(I.getOpcode(), I);
659}
660
Dan Gohman2048b852009-11-23 18:04:58 +0000661void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000662 // We're processing a new instruction.
663 ++SDNodeOrder;
664
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000665 // Note: this doesn't use InstVisitor, because it has to work with
666 // ConstantExpr's in addition to instructions.
667 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000668 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000669 // Build the switch statement using the Instruction.def file.
670#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000671 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000672#include "llvm/Instruction.def"
673 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000674}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000675
Dan Gohman2048b852009-11-23 18:04:58 +0000676SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000677 SDValue &N = NodeMap[V];
678 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000679
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000680 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000681 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000682
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000683 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000684 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000685
686 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
687 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000688
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000689 if (isa<ConstantPointerNull>(C))
690 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000691
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000692 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000693 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000694
Nate Begeman9008ca62009-04-27 18:41:29 +0000695 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000696 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000697
698 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
699 visit(CE->getOpcode(), *CE);
700 SDValue N1 = NodeMap[V];
701 assert(N1.getNode() && "visit didn't populate the ValueMap!");
702 return N1;
703 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000705 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
706 SmallVector<SDValue, 4> Constants;
707 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
708 OI != OE; ++OI) {
709 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000710 // If the operand is an empty aggregate, there are no values.
711 if (!Val) continue;
712 // Add each leaf value from the operand to the Constants list
713 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000714 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
715 Constants.push_back(SDValue(Val, i));
716 }
Bill Wendling87710f02009-12-21 23:47:40 +0000717
718 SDValue Res = DAG.getMergeValues(&Constants[0], Constants.size(),
719 getCurDebugLoc());
720 if (DisableScheduling)
721 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
722 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000723 }
724
725 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
726 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
727 "Unknown struct or array constant!");
728
Owen Andersone50ed302009-08-10 22:56:29 +0000729 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000730 ComputeValueVTs(TLI, C->getType(), ValueVTs);
731 unsigned NumElts = ValueVTs.size();
732 if (NumElts == 0)
733 return SDValue(); // empty struct
734 SmallVector<SDValue, 4> Constants(NumElts);
735 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000736 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000737 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000738 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000739 else if (EltVT.isFloatingPoint())
740 Constants[i] = DAG.getConstantFP(0, EltVT);
741 else
742 Constants[i] = DAG.getConstant(0, EltVT);
743 }
Bill Wendling87710f02009-12-21 23:47:40 +0000744
745 SDValue Res = DAG.getMergeValues(&Constants[0], NumElts,
746 getCurDebugLoc());
747 if (DisableScheduling)
748 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
749 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000750 }
751
Dan Gohman8c2b5252009-10-30 01:27:03 +0000752 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000753 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000754
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000755 const VectorType *VecTy = cast<VectorType>(V->getType());
756 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000757
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000758 // Now that we know the number and type of the elements, get that number of
759 // elements into the Ops array based on what kind of constant it is.
760 SmallVector<SDValue, 16> Ops;
761 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
762 for (unsigned i = 0; i != NumElements; ++i)
763 Ops.push_back(getValue(CP->getOperand(i)));
764 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000765 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000766 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000767
768 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000769 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000770 Op = DAG.getConstantFP(0, EltVT);
771 else
772 Op = DAG.getConstant(0, EltVT);
773 Ops.assign(NumElements, Op);
774 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000775
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000776 // Create a BUILD_VECTOR node.
Bill Wendling87710f02009-12-21 23:47:40 +0000777 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
778 VT, &Ops[0], Ops.size());
779 if (DisableScheduling)
780 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
781
782 return NodeMap[V] = Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000783 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000784
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000785 // If this is a static alloca, generate it as the frameindex instead of
786 // computation.
787 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
788 DenseMap<const AllocaInst*, int>::iterator SI =
789 FuncInfo.StaticAllocaMap.find(AI);
790 if (SI != FuncInfo.StaticAllocaMap.end())
791 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
792 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000793
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000794 unsigned InReg = FuncInfo.ValueMap[V];
795 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000796
Owen Anderson23b9b192009-08-12 00:36:31 +0000797 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000798 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +0000799 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(),
800 SDNodeOrder, Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000801}
802
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000803/// Get the EVTs and ArgFlags collections that represent the legalized return
804/// type of the given function. This does not require a DAG or a return value,
805/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000806static void getReturnInfo(const Type* ReturnType,
807 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000808 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000809 TargetLowering &TLI,
810 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000811 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000812 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000813 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000814 if (NumValues == 0) return;
815 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000816
817 for (unsigned j = 0, f = NumValues; j != f; ++j) {
818 EVT VT = ValueVTs[j];
819 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000820
821 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000822 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000823 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000824 ExtendKind = ISD::ZERO_EXTEND;
825
826 // FIXME: C calling convention requires the return type to be promoted to
827 // at least 32-bit. But this is not necessary for non-C calling
828 // conventions. The frontend should mark functions whose return values
829 // require promoting with signext or zeroext attributes.
830 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000831 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000832 if (VT.bitsLT(MinVT))
833 VT = MinVT;
834 }
835
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000836 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
837 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000838 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
839 PartVT.getTypeForEVT(ReturnType->getContext()));
840
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000841 // 'inreg' on function refers to return value
842 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000843 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000844 Flags.setInReg();
845
846 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000847 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000848 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000849 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000850 Flags.setZExt();
851
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000852 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000853 OutVTs.push_back(PartVT);
854 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000855 if (Offsets)
856 {
857 Offsets->push_back(Offset);
858 Offset += PartSize;
859 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000860 }
861 }
862}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000863
Dan Gohman2048b852009-11-23 18:04:58 +0000864void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000865 SDValue Chain = getControlRoot();
866 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000867 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000868
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000869 if (!FLI.CanLowerReturn) {
870 unsigned DemoteReg = FLI.DemoteRegister;
871 const Function *F = I.getParent()->getParent();
872
873 // Emit a store of the return value through the virtual register.
874 // Leave Outs empty so that LowerReturn won't try to load return
875 // registers the usual way.
876 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000877 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000878 PtrValueVTs);
879
880 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
881 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000882
Owen Andersone50ed302009-08-10 22:56:29 +0000883 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000884 SmallVector<uint64_t, 4> Offsets;
885 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000886 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000887
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000888 SmallVector<SDValue, 4> Chains(NumValues);
889 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000890 for (unsigned i = 0; i != NumValues; ++i) {
891 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
892 DAG.getConstant(Offsets[i], PtrVT));
893 Chains[i] =
894 DAG.getStore(Chain, getCurDebugLoc(),
895 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
896 Add, NULL, Offsets[i], false, 0);
897
898 if (DisableScheduling) {
899 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
900 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
901 }
902 }
903
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000904 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
905 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000906
907 if (DisableScheduling)
908 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
909 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000910 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
911 SmallVector<EVT, 4> ValueVTs;
912 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
913 unsigned NumValues = ValueVTs.size();
914 if (NumValues == 0) continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000915
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000916 SDValue RetOp = getValue(I.getOperand(i));
917 for (unsigned j = 0, f = NumValues; j != f; ++j) {
918 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000919
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000920 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000921
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000922 const Function *F = I.getParent()->getParent();
923 if (F->paramHasAttr(0, Attribute::SExt))
924 ExtendKind = ISD::SIGN_EXTEND;
925 else if (F->paramHasAttr(0, Attribute::ZExt))
926 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000927
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000928 // FIXME: C calling convention requires the return type to be promoted
929 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000930 // conventions. The frontend should mark functions whose return values
931 // require promoting with signext or zeroext attributes.
932 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
933 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
934 if (VT.bitsLT(MinVT))
935 VT = MinVT;
936 }
937
938 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
939 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
940 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000941 getCopyToParts(DAG, getCurDebugLoc(), SDNodeOrder,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000942 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
943 &Parts[0], NumParts, PartVT, ExtendKind);
944
945 // 'inreg' on function refers to return value
946 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
947 if (F->paramHasAttr(0, Attribute::InReg))
948 Flags.setInReg();
949
950 // Propagate extension type if any
951 if (F->paramHasAttr(0, Attribute::SExt))
952 Flags.setSExt();
953 else if (F->paramHasAttr(0, Attribute::ZExt))
954 Flags.setZExt();
955
956 for (unsigned i = 0; i < NumParts; ++i)
957 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000958 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000959 }
960 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000961
962 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000963 CallingConv::ID CallConv =
964 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000965 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
966 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000967
968 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000969 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000970 "LowerReturn didn't return a valid chain!");
971
972 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000973 DAG.setRoot(Chain);
Bill Wendling87710f02009-12-21 23:47:40 +0000974
975 if (DisableScheduling)
976 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000977}
978
Dan Gohmanad62f532009-04-23 23:13:24 +0000979/// CopyToExportRegsIfNeeded - If the given value has virtual registers
980/// created for it, emit nodes to copy the value into the virtual
981/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000982void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000983 if (!V->use_empty()) {
984 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
985 if (VMI != FuncInfo.ValueMap.end())
986 CopyValueToVirtualRegister(V, VMI->second);
987 }
988}
989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000990/// ExportFromCurrentBlock - If this condition isn't known to be exported from
991/// the current basic block, add it to ValueMap now so that we'll get a
992/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000993void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000994 // No need to export constants.
995 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000996
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000997 // Already exported?
998 if (FuncInfo.isExportedInst(V)) return;
999
1000 unsigned Reg = FuncInfo.InitializeRegForValue(V);
1001 CopyValueToVirtualRegister(V, Reg);
1002}
1003
Dan Gohman2048b852009-11-23 18:04:58 +00001004bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
1005 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001006 // The operands of the setcc have to be in this block. We don't know
1007 // how to export them from some other block.
1008 if (Instruction *VI = dyn_cast<Instruction>(V)) {
1009 // Can export from current BB.
1010 if (VI->getParent() == FromBB)
1011 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001012
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001013 // Is already exported, noop.
1014 return FuncInfo.isExportedInst(V);
1015 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001016
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001017 // If this is an argument, we can export it if the BB is the entry block or
1018 // if it is already exported.
1019 if (isa<Argument>(V)) {
1020 if (FromBB == &FromBB->getParent()->getEntryBlock())
1021 return true;
1022
1023 // Otherwise, can only export this if it is already exported.
1024 return FuncInfo.isExportedInst(V);
1025 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001026
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001027 // Otherwise, constants can always be exported.
1028 return true;
1029}
1030
1031static bool InBlock(const Value *V, const BasicBlock *BB) {
1032 if (const Instruction *I = dyn_cast<Instruction>(V))
1033 return I->getParent() == BB;
1034 return true;
1035}
1036
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001037/// getFCmpCondCode - Return the ISD condition code corresponding to
1038/// the given LLVM IR floating-point condition code. This includes
1039/// consideration of global floating-point math flags.
1040///
1041static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1042 ISD::CondCode FPC, FOC;
1043 switch (Pred) {
1044 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1045 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1046 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1047 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1048 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1049 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1050 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1051 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1052 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1053 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1054 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1055 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1056 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1057 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1058 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1059 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1060 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001061 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001062 FOC = FPC = ISD::SETFALSE;
1063 break;
1064 }
1065 if (FiniteOnlyFPMath())
1066 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001067 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001068 return FPC;
1069}
1070
1071/// getICmpCondCode - Return the ISD condition code corresponding to
1072/// the given LLVM IR integer condition code.
1073///
1074static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1075 switch (Pred) {
1076 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1077 case ICmpInst::ICMP_NE: return ISD::SETNE;
1078 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1079 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1080 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1081 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1082 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1083 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1084 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1085 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1086 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001087 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001088 return ISD::SETNE;
1089 }
1090}
1091
Dan Gohmanc2277342008-10-17 21:16:08 +00001092/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1093/// This function emits a branch and is used at the leaves of an OR or an
1094/// AND operator tree.
1095///
1096void
Dan Gohman2048b852009-11-23 18:04:58 +00001097SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1098 MachineBasicBlock *TBB,
1099 MachineBasicBlock *FBB,
1100 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001101 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001102
Dan Gohmanc2277342008-10-17 21:16:08 +00001103 // If the leaf of the tree is a comparison, merge the condition into
1104 // the caseblock.
1105 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1106 // The operands of the cmp have to be in this block. We don't know
1107 // how to export them from some other block. If this is the first block
1108 // of the sequence, no exporting is needed.
1109 if (CurBB == CurMBB ||
1110 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1111 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001112 ISD::CondCode Condition;
1113 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001114 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001115 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001116 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001117 } else {
1118 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001119 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001120 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001121
1122 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001123 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1124 SwitchCases.push_back(CB);
1125 return;
1126 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001127 }
1128
1129 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001130 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001131 NULL, TBB, FBB, CurBB);
1132 SwitchCases.push_back(CB);
1133}
1134
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001135/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001136void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1137 MachineBasicBlock *TBB,
1138 MachineBasicBlock *FBB,
1139 MachineBasicBlock *CurBB,
1140 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001141 // If this node is not part of the or/and tree, emit it as a branch.
1142 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001143 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001144 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1145 BOp->getParent() != CurBB->getBasicBlock() ||
1146 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1147 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1148 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001149 return;
1150 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001151
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001152 // Create TmpBB after CurBB.
1153 MachineFunction::iterator BBI = CurBB;
1154 MachineFunction &MF = DAG.getMachineFunction();
1155 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1156 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 if (Opc == Instruction::Or) {
1159 // Codegen X | Y as:
1160 // jmp_if_X TBB
1161 // jmp TmpBB
1162 // TmpBB:
1163 // jmp_if_Y TBB
1164 // jmp FBB
1165 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001167 // Emit the LHS condition.
1168 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001170 // Emit the RHS condition into TmpBB.
1171 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1172 } else {
1173 assert(Opc == Instruction::And && "Unknown merge op!");
1174 // Codegen X & Y as:
1175 // jmp_if_X TmpBB
1176 // jmp FBB
1177 // TmpBB:
1178 // jmp_if_Y TBB
1179 // jmp FBB
1180 //
1181 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001182
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001183 // Emit the LHS condition.
1184 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001186 // Emit the RHS condition into TmpBB.
1187 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1188 }
1189}
1190
1191/// If the set of cases should be emitted as a series of branches, return true.
1192/// If we should emit this as a bunch of and/or'd together conditions, return
1193/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001194bool
Dan Gohman2048b852009-11-23 18:04:58 +00001195SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001196 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001197
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001198 // If this is two comparisons of the same values or'd or and'd together, they
1199 // will get folded into a single comparison, so don't emit two blocks.
1200 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1201 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1202 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1203 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1204 return false;
1205 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001206
Chris Lattner133ce872010-01-02 00:00:03 +00001207 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1208 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1209 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1210 Cases[0].CC == Cases[1].CC &&
1211 isa<Constant>(Cases[0].CmpRHS) &&
1212 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1213 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1214 return false;
1215 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1216 return false;
1217 }
1218
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001219 return true;
1220}
1221
Dan Gohman2048b852009-11-23 18:04:58 +00001222void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001223 // Update machine-CFG edges.
1224 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1225
1226 // Figure out which block is immediately after the current one.
1227 MachineBasicBlock *NextBlock = 0;
1228 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001229 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001230 NextBlock = BBI;
1231
1232 if (I.isUnconditional()) {
1233 // Update machine-CFG edges.
1234 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001235
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001236 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001237 if (Succ0MBB != NextBlock) {
1238 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001239 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001240 DAG.getBasicBlock(Succ0MBB));
1241 DAG.setRoot(V);
1242
1243 if (DisableScheduling)
1244 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1245 }
1246
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001247 return;
1248 }
1249
1250 // If this condition is one of the special cases we handle, do special stuff
1251 // now.
1252 Value *CondVal = I.getCondition();
1253 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1254
1255 // If this is a series of conditions that are or'd or and'd together, emit
1256 // this as a sequence of branches instead of setcc's with and/or operations.
1257 // For example, instead of something like:
1258 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001259 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001260 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001261 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001262 // or C, F
1263 // jnz foo
1264 // Emit:
1265 // cmp A, B
1266 // je foo
1267 // cmp D, E
1268 // jle foo
1269 //
1270 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001271 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001272 (BOp->getOpcode() == Instruction::And ||
1273 BOp->getOpcode() == Instruction::Or)) {
1274 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1275 // If the compares in later blocks need to use values not currently
1276 // exported from this block, export them now. This block should always
1277 // be the first entry.
1278 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001279
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001280 // Allow some cases to be rejected.
1281 if (ShouldEmitAsBranches(SwitchCases)) {
1282 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1283 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1284 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1285 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001286
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001287 // Emit the branch for this block.
1288 visitSwitchCase(SwitchCases[0]);
1289 SwitchCases.erase(SwitchCases.begin());
1290 return;
1291 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001292
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001293 // Okay, we decided not to do this, remove any inserted MBB's and clear
1294 // SwitchCases.
1295 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001296 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001297
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001298 SwitchCases.clear();
1299 }
1300 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001301
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001302 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001303 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001304 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001305
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001306 // Use visitSwitchCase to actually insert the fast branch sequence for this
1307 // cond branch.
1308 visitSwitchCase(CB);
1309}
1310
1311/// visitSwitchCase - Emits the necessary code to represent a single node in
1312/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001313void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001314 SDValue Cond;
1315 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001316 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001317
1318 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001319 if (CB.CmpMHS == NULL) {
1320 // Fold "(X == true)" to X and "(X == false)" to !X to
1321 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001322 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001323 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001324 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001325 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001326 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001327 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001328 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001329 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001330 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331 } else {
1332 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1333
Anton Korobeynikov23218582008-12-23 22:25:27 +00001334 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1335 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001336
1337 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001338 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001339
1340 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001341 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001342 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001343 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001344 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001345 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001346 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347 DAG.getConstant(High-Low, VT), ISD::SETULE);
1348 }
1349 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001350
Bill Wendling87710f02009-12-21 23:47:40 +00001351 if (DisableScheduling)
1352 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1353
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001354 // Update successor info
1355 CurMBB->addSuccessor(CB.TrueBB);
1356 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001357
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001358 // Set NextBlock to be the MBB immediately after the current one, if any.
1359 // This is used to avoid emitting unnecessary branches to the next block.
1360 MachineBasicBlock *NextBlock = 0;
1361 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001362 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001363 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001364
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 // If the lhs block is the next block, invert the condition so that we can
1366 // fall through to the lhs instead of the rhs block.
1367 if (CB.TrueBB == NextBlock) {
1368 std::swap(CB.TrueBB, CB.FalseBB);
1369 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001370 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001371
1372 if (DisableScheduling)
1373 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001374 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001375
Dale Johannesenf5d97892009-02-04 01:48:28 +00001376 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001377 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001378 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001379
Bill Wendling87710f02009-12-21 23:47:40 +00001380 if (DisableScheduling)
1381 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1382
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001383 // If the branch was constant folded, fix up the CFG.
1384 if (BrCond.getOpcode() == ISD::BR) {
1385 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386 } else {
1387 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001388 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001389 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001390
Bill Wendling87710f02009-12-21 23:47:40 +00001391 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001392 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1393 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001394
1395 if (DisableScheduling)
1396 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1397 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001398 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001399
1400 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001401}
1402
1403/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001404void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001405 // Emit the code for the jump table
1406 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001407 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001408 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1409 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001410 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001411 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1412 MVT::Other, Index.getValue(1),
1413 Table, Index);
1414 DAG.setRoot(BrJumpTable);
1415
Bill Wendling87710f02009-12-21 23:47:40 +00001416 if (DisableScheduling) {
1417 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1418 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001419 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001420 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421}
1422
1423/// visitJumpTableHeader - This function emits necessary code to produce index
1424/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001425void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1426 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001427 // Subtract the lowest switch case value from the value being switched on and
1428 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001429 // difference between smallest and largest cases.
1430 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001431 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001432 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001433 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001434
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001435 // The SDNode we just created, which holds the value being switched on minus
1436 // the the smallest case value, needs to be copied to a virtual register so it
1437 // can be used as an index into the jump table in a subsequent basic block.
1438 // This value may be smaller or larger than the target's pointer type, and
1439 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001440 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001441
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001442 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001443 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1444 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445 JT.Reg = JumpTableReg;
1446
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001447 // Emit the range check for the jump table, and branch to the default block
1448 // for the switch statement if the value being switched on exceeds the largest
1449 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001450 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001451 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001452 DAG.getConstant(JTH.Last-JTH.First,VT),
1453 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454
Bill Wendling87710f02009-12-21 23:47:40 +00001455 if (DisableScheduling) {
1456 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1457 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1458 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1459 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1460 }
1461
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001462 // Set NextBlock to be the MBB immediately after the current one, if any.
1463 // This is used to avoid emitting unnecessary branches to the next block.
1464 MachineBasicBlock *NextBlock = 0;
1465 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001466
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001467 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001468 NextBlock = BBI;
1469
Dale Johannesen66978ee2009-01-31 02:22:37 +00001470 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001471 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001472 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001473
Bill Wendling87710f02009-12-21 23:47:40 +00001474 if (DisableScheduling)
1475 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1476
1477 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001478 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1479 DAG.getBasicBlock(JT.MBB));
1480
Bill Wendling87710f02009-12-21 23:47:40 +00001481 if (DisableScheduling)
1482 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1483 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001484
Bill Wendling87710f02009-12-21 23:47:40 +00001485 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001486}
1487
1488/// visitBitTestHeader - This function emits necessary code to produce value
1489/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001490void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001491 // Subtract the minimum value
1492 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001493 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001494 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001495 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001496
1497 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001498 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001499 TLI.getSetCCResultType(Sub.getValueType()),
1500 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001501 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001502
Bill Wendling87710f02009-12-21 23:47:40 +00001503 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1504 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001505
Duncan Sands92abc622009-01-31 15:50:11 +00001506 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001507 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1508 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001509
Bill Wendling87710f02009-12-21 23:47:40 +00001510 if (DisableScheduling) {
1511 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1512 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1513 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1514 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1515 }
1516
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001517 // Set NextBlock to be the MBB immediately after the current one, if any.
1518 // This is used to avoid emitting unnecessary branches to the next block.
1519 MachineBasicBlock *NextBlock = 0;
1520 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001521 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001522 NextBlock = BBI;
1523
1524 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1525
1526 CurMBB->addSuccessor(B.Default);
1527 CurMBB->addSuccessor(MBB);
1528
Dale Johannesen66978ee2009-01-31 02:22:37 +00001529 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001530 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001531 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001532
Bill Wendling87710f02009-12-21 23:47:40 +00001533 if (DisableScheduling)
1534 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1535
1536 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001537 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1538 DAG.getBasicBlock(MBB));
1539
Bill Wendling87710f02009-12-21 23:47:40 +00001540 if (DisableScheduling)
1541 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1542 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001543
Bill Wendling87710f02009-12-21 23:47:40 +00001544 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001545}
1546
1547/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001548void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1549 unsigned Reg,
1550 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001551 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001552 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001553 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001554 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001555 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001556 DAG.getConstant(1, TLI.getPointerTy()),
1557 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001558
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001559 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001560 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001561 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001562 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001563 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1564 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001565 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001566 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567
Bill Wendling87710f02009-12-21 23:47:40 +00001568 if (DisableScheduling) {
1569 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1570 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1571 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1572 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1573 }
1574
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001575 CurMBB->addSuccessor(B.TargetBB);
1576 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001577
Dale Johannesen66978ee2009-01-31 02:22:37 +00001578 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001579 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001580 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001581
Bill Wendling87710f02009-12-21 23:47:40 +00001582 if (DisableScheduling)
1583 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1584
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001585 // Set NextBlock to be the MBB immediately after the current one, if any.
1586 // This is used to avoid emitting unnecessary branches to the next block.
1587 MachineBasicBlock *NextBlock = 0;
1588 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001589 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001590 NextBlock = BBI;
1591
Bill Wendling87710f02009-12-21 23:47:40 +00001592 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001593 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1594 DAG.getBasicBlock(NextMBB));
1595
Bill Wendling87710f02009-12-21 23:47:40 +00001596 if (DisableScheduling)
1597 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1598 }
Bill Wendling0777e922009-12-21 21:59:52 +00001599
Bill Wendling87710f02009-12-21 23:47:40 +00001600 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001601}
1602
Dan Gohman2048b852009-11-23 18:04:58 +00001603void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001604 // Retrieve successors.
1605 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1606 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1607
Gabor Greifb67e6b32009-01-15 11:10:44 +00001608 const Value *Callee(I.getCalledValue());
1609 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001610 visitInlineAsm(&I);
1611 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001612 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001613
1614 // If the value of the invoke is used outside of its defining block, make it
1615 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001616 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001617
1618 // Update successor info
1619 CurMBB->addSuccessor(Return);
1620 CurMBB->addSuccessor(LandingPad);
1621
1622 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001623 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1624 MVT::Other, getControlRoot(),
1625 DAG.getBasicBlock(Return));
1626 DAG.setRoot(Branch);
1627
1628 if (DisableScheduling)
1629 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001630}
1631
Dan Gohman2048b852009-11-23 18:04:58 +00001632void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001633}
1634
1635/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1636/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001637bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1638 CaseRecVector& WorkList,
1639 Value* SV,
1640 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001641 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001642
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001643 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001644 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001645 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001646 return false;
1647
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001648 // Get the MachineFunction which holds the current MBB. This is used when
1649 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001650 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001651
1652 // Figure out which block is immediately after the current one.
1653 MachineBasicBlock *NextBlock = 0;
1654 MachineFunction::iterator BBI = CR.CaseBB;
1655
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001656 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001657 NextBlock = BBI;
1658
1659 // TODO: If any two of the cases has the same destination, and if one value
1660 // is the same as the other, but has one bit unset that the other has set,
1661 // use bit manipulation to do two compares at once. For example:
1662 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001664 // Rearrange the case blocks so that the last one falls through if possible.
1665 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1666 // The last case block won't fall through into 'NextBlock' if we emit the
1667 // branches in this order. See if rearranging a case value would help.
1668 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1669 if (I->BB == NextBlock) {
1670 std::swap(*I, BackCase);
1671 break;
1672 }
1673 }
1674 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001675
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001676 // Create a CaseBlock record representing a conditional branch to
1677 // the Case's target mbb if the value being switched on SV is equal
1678 // to C.
1679 MachineBasicBlock *CurBlock = CR.CaseBB;
1680 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1681 MachineBasicBlock *FallThrough;
1682 if (I != E-1) {
1683 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1684 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001685
1686 // Put SV in a virtual register to make it available from the new blocks.
1687 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001688 } else {
1689 // If the last case doesn't match, go to the default block.
1690 FallThrough = Default;
1691 }
1692
1693 Value *RHS, *LHS, *MHS;
1694 ISD::CondCode CC;
1695 if (I->High == I->Low) {
1696 // This is just small small case range :) containing exactly 1 case
1697 CC = ISD::SETEQ;
1698 LHS = SV; RHS = I->High; MHS = NULL;
1699 } else {
1700 CC = ISD::SETLE;
1701 LHS = I->Low; MHS = SV; RHS = I->High;
1702 }
1703 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001705 // If emitting the first comparison, just call visitSwitchCase to emit the
1706 // code into the current block. Otherwise, push the CaseBlock onto the
1707 // vector to be later processed by SDISel, and insert the node's MBB
1708 // before the next MBB.
1709 if (CurBlock == CurMBB)
1710 visitSwitchCase(CB);
1711 else
1712 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001713
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001714 CurBlock = FallThrough;
1715 }
1716
1717 return true;
1718}
1719
1720static inline bool areJTsAllowed(const TargetLowering &TLI) {
1721 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001722 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1723 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001724}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001725
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001726static APInt ComputeRange(const APInt &First, const APInt &Last) {
1727 APInt LastExt(Last), FirstExt(First);
1728 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1729 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1730 return (LastExt - FirstExt + 1ULL);
1731}
1732
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001733/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001734bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1735 CaseRecVector& WorkList,
1736 Value* SV,
1737 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001738 Case& FrontCase = *CR.Range.first;
1739 Case& BackCase = *(CR.Range.second-1);
1740
Chris Lattnere880efe2009-11-07 07:50:34 +00001741 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1742 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001743
Chris Lattnere880efe2009-11-07 07:50:34 +00001744 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001745 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1746 I!=E; ++I)
1747 TSize += I->size();
1748
Chris Lattnere880efe2009-11-07 07:50:34 +00001749 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001750 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001751
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001752 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001753 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001754 if (Density < 0.4)
1755 return false;
1756
David Greene4b69d992010-01-05 01:24:57 +00001757 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001758 << "First entry: " << First << ". Last entry: " << Last << '\n'
1759 << "Range: " << Range
1760 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001761
1762 // Get the MachineFunction which holds the current MBB. This is used when
1763 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001764 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001765
1766 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001767 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001768 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001769
1770 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1771
1772 // Create a new basic block to hold the code for loading the address
1773 // of the jump table, and jumping to it. Update successor information;
1774 // we will either branch to the default case for the switch, or the jump
1775 // table.
1776 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1777 CurMF->insert(BBI, JumpTableBB);
1778 CR.CaseBB->addSuccessor(Default);
1779 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001780
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001781 // Build a vector of destination BBs, corresponding to each target
1782 // of the jump table. If the value of the jump table slot corresponds to
1783 // a case statement, push the case's BB onto the vector, otherwise, push
1784 // the default BB.
1785 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001786 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001788 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1789 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1790
1791 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001792 DestBBs.push_back(I->BB);
1793 if (TEI==High)
1794 ++I;
1795 } else {
1796 DestBBs.push_back(Default);
1797 }
1798 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001799
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001800 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001801 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1802 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001803 E = DestBBs.end(); I != E; ++I) {
1804 if (!SuccsHandled[(*I)->getNumber()]) {
1805 SuccsHandled[(*I)->getNumber()] = true;
1806 JumpTableBB->addSuccessor(*I);
1807 }
1808 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001809
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001810 // Create a jump table index for this jump table, or return an existing
1811 // one.
1812 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001814 // Set the jump table information so that we can codegen it as a second
1815 // MachineBasicBlock
1816 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1817 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1818 if (CR.CaseBB == CurMBB)
1819 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001821 JTCases.push_back(JumpTableBlock(JTH, JT));
1822
1823 return true;
1824}
1825
1826/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1827/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001828bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1829 CaseRecVector& WorkList,
1830 Value* SV,
1831 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001832 // Get the MachineFunction which holds the current MBB. This is used when
1833 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001834 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001835
1836 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001837 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001838 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001839
1840 Case& FrontCase = *CR.Range.first;
1841 Case& BackCase = *(CR.Range.second-1);
1842 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1843
1844 // Size is the number of Cases represented by this range.
1845 unsigned Size = CR.Range.second - CR.Range.first;
1846
Chris Lattnere880efe2009-11-07 07:50:34 +00001847 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1848 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001849 double FMetric = 0;
1850 CaseItr Pivot = CR.Range.first + Size/2;
1851
1852 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1853 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001854 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1856 I!=E; ++I)
1857 TSize += I->size();
1858
Chris Lattnere880efe2009-11-07 07:50:34 +00001859 APInt LSize = FrontCase.size();
1860 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001861 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001862 << "First: " << First << ", Last: " << Last <<'\n'
1863 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001864 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1865 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001866 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1867 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001868 APInt Range = ComputeRange(LEnd, RBegin);
1869 assert((Range - 2ULL).isNonNegative() &&
1870 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001871 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001872 (LEnd - First + 1ULL).roundToDouble();
1873 double RDensity = (double)RSize.roundToDouble() /
1874 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001875 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001876 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001877 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001878 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1879 << "LDensity: " << LDensity
1880 << ", RDensity: " << RDensity << '\n'
1881 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 if (FMetric < Metric) {
1883 Pivot = J;
1884 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001885 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 }
1887
1888 LSize += J->size();
1889 RSize -= J->size();
1890 }
1891 if (areJTsAllowed(TLI)) {
1892 // If our case is dense we *really* should handle it earlier!
1893 assert((FMetric > 0) && "Should handle dense range earlier!");
1894 } else {
1895 Pivot = CR.Range.first + Size/2;
1896 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001897
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001898 CaseRange LHSR(CR.Range.first, Pivot);
1899 CaseRange RHSR(Pivot, CR.Range.second);
1900 Constant *C = Pivot->Low;
1901 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001902
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001904 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001905 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001906 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001907 // Pivot's Value, then we can branch directly to the LHS's Target,
1908 // rather than creating a leaf node for it.
1909 if ((LHSR.second - LHSR.first) == 1 &&
1910 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001911 cast<ConstantInt>(C)->getValue() ==
1912 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001913 TrueBB = LHSR.first->BB;
1914 } else {
1915 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1916 CurMF->insert(BBI, TrueBB);
1917 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001918
1919 // Put SV in a virtual register to make it available from the new blocks.
1920 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001922
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001923 // Similar to the optimization above, if the Value being switched on is
1924 // known to be less than the Constant CR.LT, and the current Case Value
1925 // is CR.LT - 1, then we can branch directly to the target block for
1926 // the current Case Value, rather than emitting a RHS leaf node for it.
1927 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001928 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1929 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001930 FalseBB = RHSR.first->BB;
1931 } else {
1932 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1933 CurMF->insert(BBI, FalseBB);
1934 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001935
1936 // Put SV in a virtual register to make it available from the new blocks.
1937 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001938 }
1939
1940 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001941 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001942 // Otherwise, branch to LHS.
1943 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1944
1945 if (CR.CaseBB == CurMBB)
1946 visitSwitchCase(CB);
1947 else
1948 SwitchCases.push_back(CB);
1949
1950 return true;
1951}
1952
1953/// handleBitTestsSwitchCase - if current case range has few destination and
1954/// range span less, than machine word bitwidth, encode case range into series
1955/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001956bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1957 CaseRecVector& WorkList,
1958 Value* SV,
1959 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001960 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001961 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001962
1963 Case& FrontCase = *CR.Range.first;
1964 Case& BackCase = *(CR.Range.second-1);
1965
1966 // Get the MachineFunction which holds the current MBB. This is used when
1967 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001968 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001969
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001970 // If target does not have legal shift left, do not emit bit tests at all.
1971 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1972 return false;
1973
Anton Korobeynikov23218582008-12-23 22:25:27 +00001974 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001975 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1976 I!=E; ++I) {
1977 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001978 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001979 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001980
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001981 // Count unique destinations
1982 SmallSet<MachineBasicBlock*, 4> Dests;
1983 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1984 Dests.insert(I->BB);
1985 if (Dests.size() > 3)
1986 // Don't bother the code below, if there are too much unique destinations
1987 return false;
1988 }
David Greene4b69d992010-01-05 01:24:57 +00001989 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001990 << Dests.size() << '\n'
1991 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001992
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001993 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001994 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1995 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001996 APInt cmpRange = maxValue - minValue;
1997
David Greene4b69d992010-01-05 01:24:57 +00001998 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001999 << "Low bound: " << minValue << '\n'
2000 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00002001
2002 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002003 (!(Dests.size() == 1 && numCmps >= 3) &&
2004 !(Dests.size() == 2 && numCmps >= 5) &&
2005 !(Dests.size() >= 3 && numCmps >= 6)))
2006 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002007
David Greene4b69d992010-01-05 01:24:57 +00002008 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002009 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2010
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002011 // Optimize the case where all the case values fit in a
2012 // word without having to subtract minValue. In this case,
2013 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002014 if (minValue.isNonNegative() &&
2015 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
2016 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002017 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002018 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002019 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 CaseBitsVector CasesBits;
2022 unsigned i, count = 0;
2023
2024 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2025 MachineBasicBlock* Dest = I->BB;
2026 for (i = 0; i < count; ++i)
2027 if (Dest == CasesBits[i].BB)
2028 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002030 if (i == count) {
2031 assert((count < 3) && "Too much destinations to test!");
2032 CasesBits.push_back(CaseBits(0, Dest, 0));
2033 count++;
2034 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002035
2036 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2037 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2038
2039 uint64_t lo = (lowValue - lowBound).getZExtValue();
2040 uint64_t hi = (highValue - lowBound).getZExtValue();
2041
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002042 for (uint64_t j = lo; j <= hi; j++) {
2043 CasesBits[i].Mask |= 1ULL << j;
2044 CasesBits[i].Bits++;
2045 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002046
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002047 }
2048 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002049
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002050 BitTestInfo BTC;
2051
2052 // Figure out which block is immediately after the current one.
2053 MachineFunction::iterator BBI = CR.CaseBB;
2054 ++BBI;
2055
2056 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2057
David Greene4b69d992010-01-05 01:24:57 +00002058 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002059 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002060 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002061 << ", Bits: " << CasesBits[i].Bits
2062 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002063
2064 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2065 CurMF->insert(BBI, CaseBB);
2066 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2067 CaseBB,
2068 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002069
2070 // Put SV in a virtual register to make it available from the new blocks.
2071 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002073
2074 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002075 -1U, (CR.CaseBB == CurMBB),
2076 CR.CaseBB, Default, BTC);
2077
2078 if (CR.CaseBB == CurMBB)
2079 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081 BitTestCases.push_back(BTB);
2082
2083 return true;
2084}
2085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002086/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002087size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2088 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002089 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002090
2091 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002092 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2094 Cases.push_back(Case(SI.getSuccessorValue(i),
2095 SI.getSuccessorValue(i),
2096 SMBB));
2097 }
2098 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2099
2100 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002101 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002102 // Must recompute end() each iteration because it may be
2103 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002104 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2105 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2106 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002107 MachineBasicBlock* nextBB = J->BB;
2108 MachineBasicBlock* currentBB = I->BB;
2109
2110 // If the two neighboring cases go to the same destination, merge them
2111 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002112 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002113 I->High = J->High;
2114 J = Cases.erase(J);
2115 } else {
2116 I = J++;
2117 }
2118 }
2119
2120 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2121 if (I->Low != I->High)
2122 // A range counts double, since it requires two compares.
2123 ++numCmps;
2124 }
2125
2126 return numCmps;
2127}
2128
Dan Gohman2048b852009-11-23 18:04:58 +00002129void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002130 // Figure out which block is immediately after the current one.
2131 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002132 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2133
2134 // If there is only the default destination, branch to it if it is not the
2135 // next basic block. Otherwise, just fall through.
2136 if (SI.getNumOperands() == 2) {
2137 // Update machine-CFG edges.
2138
2139 // If this is not a fall-through branch, emit the branch.
2140 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002141 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002142 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002143 MVT::Other, getControlRoot(),
2144 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002145 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002146
2147 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002148 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002149 }
2150
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002151 return;
2152 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002154 // If there are any non-default case statements, create a vector of Cases
2155 // representing each one, and sort the vector so that we can efficiently
2156 // create a binary search tree from them.
2157 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002158 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002159 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002160 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002161 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002162
2163 // Get the Value to be switched on and default basic blocks, which will be
2164 // inserted into CaseBlock records, representing basic blocks in the binary
2165 // search tree.
2166 Value *SV = SI.getOperand(0);
2167
2168 // Push the initial CaseRec onto the worklist
2169 CaseRecVector WorkList;
2170 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2171
2172 while (!WorkList.empty()) {
2173 // Grab a record representing a case range to process off the worklist
2174 CaseRec CR = WorkList.back();
2175 WorkList.pop_back();
2176
2177 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2178 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002180 // If the range has few cases (two or less) emit a series of specific
2181 // tests.
2182 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2183 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002184
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002185 // If the switch has more than 5 blocks, and at least 40% dense, and the
2186 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187 // lowering the switch to a binary tree of conditional branches.
2188 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2189 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002190
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002191 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2192 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2193 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2194 }
2195}
2196
Dan Gohman2048b852009-11-23 18:04:58 +00002197void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002198 // Update machine-CFG edges.
2199 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2200 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2201
Bill Wendling49fcff82009-12-21 22:30:11 +00002202 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2203 MVT::Other, getControlRoot(),
2204 getValue(I.getAddress()));
2205 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002206
Bill Wendling49fcff82009-12-21 22:30:11 +00002207 if (DisableScheduling)
2208 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2209}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002210
Dan Gohman2048b852009-11-23 18:04:58 +00002211void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002212 // -0.0 - X --> fneg
2213 const Type *Ty = I.getType();
2214 if (isa<VectorType>(Ty)) {
2215 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2216 const VectorType *DestTy = cast<VectorType>(I.getType());
2217 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002218 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002219 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002220 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002221 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002222 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002223 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002224 Op2.getValueType(), Op2);
Bill Wendling49fcff82009-12-21 22:30:11 +00002225 setValue(&I, Res);
2226
2227 if (DisableScheduling)
2228 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2229
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002230 return;
2231 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002232 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002233 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002234
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002235 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002236 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002237 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002238 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2239 Op2.getValueType(), Op2);
2240 setValue(&I, Res);
2241
2242 if (DisableScheduling)
2243 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2244
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002245 return;
2246 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002247
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002248 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002249}
2250
Dan Gohman2048b852009-11-23 18:04:58 +00002251void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252 SDValue Op1 = getValue(I.getOperand(0));
2253 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002254 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2255 Op1.getValueType(), Op1, Op2);
2256 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002257
Bill Wendling49fcff82009-12-21 22:30:11 +00002258 if (DisableScheduling)
2259 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002260}
2261
Dan Gohman2048b852009-11-23 18:04:58 +00002262void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002263 SDValue Op1 = getValue(I.getOperand(0));
2264 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002265 if (!isa<VectorType>(I.getType()) &&
2266 Op2.getValueType() != TLI.getShiftAmountTy()) {
2267 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002268 EVT PTy = TLI.getPointerTy();
2269 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002270 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002271 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2272 TLI.getShiftAmountTy(), Op2);
2273 // If the operand is larger than the shift count type but the shift
2274 // count type has enough bits to represent any shift value, truncate
2275 // it now. This is a common case and it exposes the truncate to
2276 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002277 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002278 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2279 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2280 TLI.getShiftAmountTy(), Op2);
2281 // Otherwise we'll need to temporarily settle for some other
2282 // convenient type; type legalization will make adjustments as
2283 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002284 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002285 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002286 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002287 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002288 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002289 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002290 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002291
Bill Wendling49fcff82009-12-21 22:30:11 +00002292 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2293 Op1.getValueType(), Op1, Op2);
2294 setValue(&I, Res);
2295
Bill Wendling87710f02009-12-21 23:47:40 +00002296 if (DisableScheduling) {
2297 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2298 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002299 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002300 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002301}
2302
Dan Gohman2048b852009-11-23 18:04:58 +00002303void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002304 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2305 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2306 predicate = IC->getPredicate();
2307 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2308 predicate = ICmpInst::Predicate(IC->getPredicate());
2309 SDValue Op1 = getValue(I.getOperand(0));
2310 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002311 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002312
Owen Andersone50ed302009-08-10 22:56:29 +00002313 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002314 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2315 setValue(&I, Res);
2316
2317 if (DisableScheduling)
2318 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002319}
2320
Dan Gohman2048b852009-11-23 18:04:58 +00002321void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002322 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2323 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2324 predicate = FC->getPredicate();
2325 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2326 predicate = FCmpInst::Predicate(FC->getPredicate());
2327 SDValue Op1 = getValue(I.getOperand(0));
2328 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002329 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002330 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002331 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2332 setValue(&I, Res);
2333
2334 if (DisableScheduling)
2335 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002336}
2337
Dan Gohman2048b852009-11-23 18:04:58 +00002338void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002339 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002340 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2341 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002342 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002343
Bill Wendling49fcff82009-12-21 22:30:11 +00002344 SmallVector<SDValue, 4> Values(NumValues);
2345 SDValue Cond = getValue(I.getOperand(0));
2346 SDValue TrueVal = getValue(I.getOperand(1));
2347 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002348
Bill Wendling49fcff82009-12-21 22:30:11 +00002349 for (unsigned i = 0; i != NumValues; ++i) {
2350 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2351 TrueVal.getNode()->getValueType(i), Cond,
2352 SDValue(TrueVal.getNode(),
2353 TrueVal.getResNo() + i),
2354 SDValue(FalseVal.getNode(),
2355 FalseVal.getResNo() + i));
2356
2357 if (DisableScheduling)
2358 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002359 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002360
Bill Wendling49fcff82009-12-21 22:30:11 +00002361 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2362 DAG.getVTList(&ValueVTs[0], NumValues),
2363 &Values[0], NumValues);
2364 setValue(&I, Res);
2365
2366 if (DisableScheduling)
2367 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2368}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002369
Dan Gohman2048b852009-11-23 18:04:58 +00002370void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002371 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2372 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002373 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002374 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2375 setValue(&I, Res);
2376
2377 if (DisableScheduling)
2378 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002379}
2380
Dan Gohman2048b852009-11-23 18:04:58 +00002381void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002382 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2383 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2384 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002385 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002386 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2387 setValue(&I, Res);
2388
2389 if (DisableScheduling)
2390 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002391}
2392
Dan Gohman2048b852009-11-23 18:04:58 +00002393void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002394 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2395 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2396 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002397 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002398 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2399 setValue(&I, Res);
2400
2401 if (DisableScheduling)
2402 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002403}
2404
Dan Gohman2048b852009-11-23 18:04:58 +00002405void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002406 // FPTrunc is never a no-op cast, no need to check
2407 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002408 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002409 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2410 DestVT, N, DAG.getIntPtrConstant(0));
2411 setValue(&I, Res);
2412
2413 if (DisableScheduling)
2414 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002415}
2416
Dan Gohman2048b852009-11-23 18:04:58 +00002417void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002418 // FPTrunc is never a no-op cast, no need to check
2419 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002420 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002421 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2422 setValue(&I, Res);
2423
2424 if (DisableScheduling)
2425 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002426}
2427
Dan Gohman2048b852009-11-23 18:04:58 +00002428void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002429 // FPToUI is never a no-op cast, no need to check
2430 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002431 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002432 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2433 setValue(&I, Res);
2434
2435 if (DisableScheduling)
2436 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002437}
2438
Dan Gohman2048b852009-11-23 18:04:58 +00002439void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002440 // FPToSI is never a no-op cast, no need to check
2441 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002442 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002443 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2444 setValue(&I, Res);
2445
2446 if (DisableScheduling)
2447 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002448}
2449
Dan Gohman2048b852009-11-23 18:04:58 +00002450void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002451 // UIToFP is never a no-op cast, no need to check
2452 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002453 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002454 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2455 setValue(&I, Res);
2456
2457 if (DisableScheduling)
2458 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002459}
2460
Dan Gohman2048b852009-11-23 18:04:58 +00002461void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002462 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002463 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002464 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002465 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2466 setValue(&I, Res);
2467
2468 if (DisableScheduling)
2469 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002470}
2471
Dan Gohman2048b852009-11-23 18:04:58 +00002472void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002473 // What to do depends on the size of the integer and the size of the pointer.
2474 // We can either truncate, zero extend, or no-op, accordingly.
2475 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002476 EVT SrcVT = N.getValueType();
2477 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002478 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2479 setValue(&I, Res);
2480
2481 if (DisableScheduling)
2482 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002483}
2484
Dan Gohman2048b852009-11-23 18:04:58 +00002485void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002486 // What to do depends on the size of the integer and the size of the pointer.
2487 // We can either truncate, zero extend, or no-op, accordingly.
2488 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002489 EVT SrcVT = N.getValueType();
2490 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002491 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2492 setValue(&I, Res);
2493
2494 if (DisableScheduling)
2495 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002496}
2497
Dan Gohman2048b852009-11-23 18:04:58 +00002498void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002499 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002500 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002501
Bill Wendling49fcff82009-12-21 22:30:11 +00002502 // BitCast assures us that source and destination are the same size so this is
2503 // either a BIT_CONVERT or a no-op.
2504 if (DestVT != N.getValueType()) {
2505 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2506 DestVT, N); // convert types.
2507 setValue(&I, Res);
2508
2509 if (DisableScheduling)
2510 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2511 } else {
2512 setValue(&I, N); // noop cast.
2513 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002514}
2515
Dan Gohman2048b852009-11-23 18:04:58 +00002516void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002517 SDValue InVec = getValue(I.getOperand(0));
2518 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002519 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002520 TLI.getPointerTy(),
2521 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002522 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2523 TLI.getValueType(I.getType()),
2524 InVec, InVal, InIdx);
2525 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002526
Bill Wendling87710f02009-12-21 23:47:40 +00002527 if (DisableScheduling) {
2528 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002529 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002530 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002531}
2532
Dan Gohman2048b852009-11-23 18:04:58 +00002533void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002534 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002535 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002536 TLI.getPointerTy(),
2537 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002538 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2539 TLI.getValueType(I.getType()), InVec, InIdx);
2540 setValue(&I, Res);
2541
Bill Wendling87710f02009-12-21 23:47:40 +00002542 if (DisableScheduling) {
2543 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002544 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002545 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002546}
2547
Mon P Wangaeb06d22008-11-10 04:46:22 +00002548
2549// Utility for visitShuffleVector - Returns true if the mask is mask starting
2550// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002551static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2552 unsigned MaskNumElts = Mask.size();
2553 for (unsigned i = 0; i != MaskNumElts; ++i)
2554 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002555 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002556 return true;
2557}
2558
Dan Gohman2048b852009-11-23 18:04:58 +00002559void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002560 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002561 SDValue Src1 = getValue(I.getOperand(0));
2562 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563
Nate Begeman9008ca62009-04-27 18:41:29 +00002564 // Convert the ConstantVector mask operand into an array of ints, with -1
2565 // representing undef values.
2566 SmallVector<Constant*, 8> MaskElts;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002567 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
Owen Anderson001dbfe2009-07-16 18:04:31 +00002568 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002569 unsigned MaskNumElts = MaskElts.size();
2570 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002571 if (isa<UndefValue>(MaskElts[i]))
2572 Mask.push_back(-1);
2573 else
2574 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2575 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002576
Owen Andersone50ed302009-08-10 22:56:29 +00002577 EVT VT = TLI.getValueType(I.getType());
2578 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002579 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002580
Mon P Wangc7849c22008-11-16 05:06:27 +00002581 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002582 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2583 &Mask[0]);
2584 setValue(&I, Res);
2585
2586 if (DisableScheduling)
2587 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2588
Mon P Wangaeb06d22008-11-10 04:46:22 +00002589 return;
2590 }
2591
2592 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002593 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2594 // Mask is longer than the source vectors and is a multiple of the source
2595 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002596 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002597 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2598 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002599 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2600 VT, Src1, Src2);
2601 setValue(&I, Res);
2602
2603 if (DisableScheduling)
2604 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2605
Mon P Wangaeb06d22008-11-10 04:46:22 +00002606 return;
2607 }
2608
Mon P Wangc7849c22008-11-16 05:06:27 +00002609 // Pad both vectors with undefs to make them the same length as the mask.
2610 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002611 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2612 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002613 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002614
Nate Begeman9008ca62009-04-27 18:41:29 +00002615 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2616 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002617 MOps1[0] = Src1;
2618 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002619
2620 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2621 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002622 &MOps1[0], NumConcat);
2623 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002624 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002625 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002626
Mon P Wangaeb06d22008-11-10 04:46:22 +00002627 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002628 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002629 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002630 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002631 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002632 MappedOps.push_back(Idx);
2633 else
2634 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002635 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002636
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002637 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002638 &MappedOps[0]);
2639 setValue(&I, Res);
2640
Bill Wendlinge1a90422009-12-21 23:10:19 +00002641 if (DisableScheduling) {
2642 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2643 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002644 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002645 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002646
Mon P Wangaeb06d22008-11-10 04:46:22 +00002647 return;
2648 }
2649
Mon P Wangc7849c22008-11-16 05:06:27 +00002650 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002651 // Analyze the access pattern of the vector to see if we can extract
2652 // two subvectors and do the shuffle. The analysis is done by calculating
2653 // the range of elements the mask access on both vectors.
2654 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2655 int MaxRange[2] = {-1, -1};
2656
Nate Begeman5a5ca152009-04-29 05:20:52 +00002657 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002658 int Idx = Mask[i];
2659 int Input = 0;
2660 if (Idx < 0)
2661 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002662
Nate Begeman5a5ca152009-04-29 05:20:52 +00002663 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002664 Input = 1;
2665 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002666 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002667 if (Idx > MaxRange[Input])
2668 MaxRange[Input] = Idx;
2669 if (Idx < MinRange[Input])
2670 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002671 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002672
Mon P Wangc7849c22008-11-16 05:06:27 +00002673 // Check if the access is smaller than the vector size and can we find
2674 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002675 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2676 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002677 int StartIdx[2]; // StartIdx to extract from
2678 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002679 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002680 RangeUse[Input] = 0; // Unused
2681 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002682 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002683 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002684 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002685 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002686 RangeUse[Input] = 1; // Extract from beginning of the vector
2687 StartIdx[Input] = 0;
2688 } else {
2689 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002690 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002691 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002692 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002693 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002694 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002695 }
2696
Bill Wendling636e2582009-08-21 18:16:06 +00002697 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002698 SDValue Res = DAG.getUNDEF(VT);
2699 setValue(&I, Res); // Vectors are not used.
2700
2701 if (DisableScheduling)
2702 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2703
Mon P Wangc7849c22008-11-16 05:06:27 +00002704 return;
2705 }
2706 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2707 // Extract appropriate subvector and generate a vector shuffle
2708 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002709 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002710 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002711 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002712 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002713 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002714 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002715
2716 if (DisableScheduling)
2717 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002718 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002719
Mon P Wangc7849c22008-11-16 05:06:27 +00002720 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002721 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002722 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002723 int Idx = Mask[i];
2724 if (Idx < 0)
2725 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002726 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002727 MappedOps.push_back(Idx - StartIdx[0]);
2728 else
2729 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002730 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002731
2732 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2733 &MappedOps[0]);
2734 setValue(&I, Res);
2735
2736 if (DisableScheduling)
2737 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2738
Mon P Wangc7849c22008-11-16 05:06:27 +00002739 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002740 }
2741 }
2742
Mon P Wangc7849c22008-11-16 05:06:27 +00002743 // We can't use either concat vectors or extract subvectors so fall back to
2744 // replacing the shuffle with extract and build vector.
2745 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002746 EVT EltVT = VT.getVectorElementType();
2747 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002748 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002749 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002750 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002751 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002752 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002753 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002754 SDValue Res;
2755
Nate Begeman5a5ca152009-04-29 05:20:52 +00002756 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002757 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2758 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002759 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002760 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2761 EltVT, Src2,
2762 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2763
2764 Ops.push_back(Res);
2765
2766 if (DisableScheduling)
2767 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002768 }
2769 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002770
2771 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2772 VT, &Ops[0], Ops.size());
2773 setValue(&I, Res);
2774
2775 if (DisableScheduling)
2776 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002777}
2778
Dan Gohman2048b852009-11-23 18:04:58 +00002779void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002780 const Value *Op0 = I.getOperand(0);
2781 const Value *Op1 = I.getOperand(1);
2782 const Type *AggTy = I.getType();
2783 const Type *ValTy = Op1->getType();
2784 bool IntoUndef = isa<UndefValue>(Op0);
2785 bool FromUndef = isa<UndefValue>(Op1);
2786
2787 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2788 I.idx_begin(), I.idx_end());
2789
Owen Andersone50ed302009-08-10 22:56:29 +00002790 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002791 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002792 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002793 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2794
2795 unsigned NumAggValues = AggValueVTs.size();
2796 unsigned NumValValues = ValValueVTs.size();
2797 SmallVector<SDValue, 4> Values(NumAggValues);
2798
2799 SDValue Agg = getValue(Op0);
2800 SDValue Val = getValue(Op1);
2801 unsigned i = 0;
2802 // Copy the beginning value(s) from the original aggregate.
2803 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002804 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002805 SDValue(Agg.getNode(), Agg.getResNo() + i);
2806 // Copy values from the inserted value(s).
2807 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002808 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002809 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2810 // Copy remaining value(s) from the original aggregate.
2811 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002812 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002813 SDValue(Agg.getNode(), Agg.getResNo() + i);
2814
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002815 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2816 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2817 &Values[0], NumAggValues);
2818 setValue(&I, Res);
2819
2820 if (DisableScheduling)
2821 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002822}
2823
Dan Gohman2048b852009-11-23 18:04:58 +00002824void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 const Value *Op0 = I.getOperand(0);
2826 const Type *AggTy = Op0->getType();
2827 const Type *ValTy = I.getType();
2828 bool OutOfUndef = isa<UndefValue>(Op0);
2829
2830 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2831 I.idx_begin(), I.idx_end());
2832
Owen Andersone50ed302009-08-10 22:56:29 +00002833 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002834 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2835
2836 unsigned NumValValues = ValValueVTs.size();
2837 SmallVector<SDValue, 4> Values(NumValValues);
2838
2839 SDValue Agg = getValue(Op0);
2840 // Copy out the selected value(s).
2841 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2842 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002843 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002844 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002845 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002846
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002847 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2848 DAG.getVTList(&ValValueVTs[0], NumValValues),
2849 &Values[0], NumValValues);
2850 setValue(&I, Res);
2851
2852 if (DisableScheduling)
2853 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854}
2855
Dan Gohman2048b852009-11-23 18:04:58 +00002856void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002857 SDValue N = getValue(I.getOperand(0));
2858 const Type *Ty = I.getOperand(0)->getType();
2859
2860 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2861 OI != E; ++OI) {
2862 Value *Idx = *OI;
2863 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2864 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2865 if (Field) {
2866 // N = N + Offset
2867 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002868 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002870
2871 if (DisableScheduling)
2872 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002873 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002874
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002875 Ty = StTy->getElementType(Field);
2876 } else {
2877 Ty = cast<SequentialType>(Ty)->getElementType();
2878
2879 // If this is a constant subscript, handle it quickly.
2880 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2881 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002882 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002883 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002884 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002885 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002886 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002887 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002888 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2889 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002890 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002891 else
Evan Chengb1032a82009-02-09 20:54:38 +00002892 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002893
Dale Johannesen66978ee2009-01-31 02:22:37 +00002894 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002895 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002896
2897 if (DisableScheduling) {
2898 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2899 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2900 }
2901
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002902 continue;
2903 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002904
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002905 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002906 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2907 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002908 SDValue IdxN = getValue(Idx);
2909
2910 // If the index is smaller or larger than intptr_t, truncate or extend
2911 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002912 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002913
2914 // If this is a multiply by a power of two, turn it into a shl
2915 // immediately. This is a very common case.
2916 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002917 if (ElementSize.isPowerOf2()) {
2918 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002919 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002920 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002921 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002922 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002923 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002924 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002925 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002926 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002927
2928 if (DisableScheduling)
2929 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002930 }
2931
Scott Michelfdc40a02009-02-17 22:15:04 +00002932 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002933 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002934
2935 if (DisableScheduling)
2936 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002937 }
2938 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002939
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002940 setValue(&I, N);
2941}
2942
Dan Gohman2048b852009-11-23 18:04:58 +00002943void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002944 // If this is a fixed sized alloca in the entry block of the function,
2945 // allocate it statically on the stack.
2946 if (FuncInfo.StaticAllocaMap.count(&I))
2947 return; // getValue will auto-populate this.
2948
2949 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002950 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002951 unsigned Align =
2952 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2953 I.getAlignment());
2954
2955 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002956
Chris Lattner0b18e592009-03-17 19:36:00 +00002957 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2958 AllocSize,
2959 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002960
Bill Wendling856ff412009-12-22 00:12:37 +00002961 if (DisableScheduling)
2962 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002963
Owen Andersone50ed302009-08-10 22:56:29 +00002964 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002965 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002966
Bill Wendling856ff412009-12-22 00:12:37 +00002967 if (DisableScheduling)
2968 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002969
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002970 // Handle alignment. If the requested alignment is less than or equal to
2971 // the stack alignment, ignore it. If the size is greater than or equal to
2972 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2973 unsigned StackAlign =
2974 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2975 if (Align <= StackAlign)
2976 Align = 0;
2977
2978 // Round the size of the allocation up to the stack alignment size
2979 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002980 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002981 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002982 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002983 if (DisableScheduling)
2984 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2985
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002986 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002987 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002988 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002989 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002990 if (DisableScheduling)
2991 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002992
2993 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002994 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002995 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002996 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002997 setValue(&I, DSA);
2998 DAG.setRoot(DSA.getValue(1));
2999
Bill Wendling856ff412009-12-22 00:12:37 +00003000 if (DisableScheduling)
3001 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
3002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003003 // Inform the Frame Information that we have just allocated a variable-sized
3004 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00003005 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003006}
3007
Dan Gohman2048b852009-11-23 18:04:58 +00003008void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003009 const Value *SV = I.getOperand(0);
3010 SDValue Ptr = getValue(SV);
3011
3012 const Type *Ty = I.getType();
3013 bool isVolatile = I.isVolatile();
3014 unsigned Alignment = I.getAlignment();
3015
Owen Andersone50ed302009-08-10 22:56:29 +00003016 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003017 SmallVector<uint64_t, 4> Offsets;
3018 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
3019 unsigned NumValues = ValueVTs.size();
3020 if (NumValues == 0)
3021 return;
3022
3023 SDValue Root;
3024 bool ConstantMemory = false;
3025 if (I.isVolatile())
3026 // Serialize volatile loads with other side effects.
3027 Root = getRoot();
3028 else if (AA->pointsToConstantMemory(SV)) {
3029 // Do not serialize (non-volatile) loads of constant memory with anything.
3030 Root = DAG.getEntryNode();
3031 ConstantMemory = true;
3032 } else {
3033 // Do not serialize non-volatile loads against each other.
3034 Root = DAG.getRoot();
3035 }
3036
3037 SmallVector<SDValue, 4> Values(NumValues);
3038 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003039 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003040 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00003041 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
3042 PtrVT, Ptr,
3043 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003044 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00003045 A, SV, Offsets[i], isVolatile, Alignment);
3046
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003047 Values[i] = L;
3048 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00003049
3050 if (DisableScheduling) {
3051 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
3052 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3053 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003054 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003056 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003057 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003058 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003059 if (isVolatile)
3060 DAG.setRoot(Chain);
3061 else
3062 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00003063
3064 if (DisableScheduling)
3065 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003066 }
3067
Bill Wendling856ff412009-12-22 00:12:37 +00003068 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
3069 DAG.getVTList(&ValueVTs[0], NumValues),
3070 &Values[0], NumValues);
3071 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003072
Bill Wendling856ff412009-12-22 00:12:37 +00003073 if (DisableScheduling)
3074 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
3075}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003076
Dan Gohman2048b852009-11-23 18:04:58 +00003077void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003078 Value *SrcV = I.getOperand(0);
3079 Value *PtrV = I.getOperand(1);
3080
Owen Andersone50ed302009-08-10 22:56:29 +00003081 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003082 SmallVector<uint64_t, 4> Offsets;
3083 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3084 unsigned NumValues = ValueVTs.size();
3085 if (NumValues == 0)
3086 return;
3087
3088 // Get the lowered operands. Note that we do this after
3089 // checking if NumResults is zero, because with zero results
3090 // the operands won't have values in the map.
3091 SDValue Src = getValue(SrcV);
3092 SDValue Ptr = getValue(PtrV);
3093
3094 SDValue Root = getRoot();
3095 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003096 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003097 bool isVolatile = I.isVolatile();
3098 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003099
3100 for (unsigned i = 0; i != NumValues; ++i) {
3101 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3102 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003103 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003104 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003105 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003106
Bill Wendling856ff412009-12-22 00:12:37 +00003107 if (DisableScheduling) {
3108 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3109 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3110 }
3111 }
3112
3113 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3114 MVT::Other, &Chains[0], NumValues);
3115 DAG.setRoot(Res);
3116
3117 if (DisableScheduling)
3118 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003119}
3120
3121/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3122/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003123void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3124 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003125 bool HasChain = !I.doesNotAccessMemory();
3126 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3127
3128 // Build the operand list.
3129 SmallVector<SDValue, 8> Ops;
3130 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3131 if (OnlyLoad) {
3132 // We don't need to serialize loads against other loads.
3133 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003134 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003135 Ops.push_back(getRoot());
3136 }
3137 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003138
3139 // Info is set by getTgtMemInstrinsic
3140 TargetLowering::IntrinsicInfo Info;
3141 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3142
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003143 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003144 if (!IsTgtIntrinsic)
3145 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003146
3147 // Add all operands of the call to the operand list.
3148 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3149 SDValue Op = getValue(I.getOperand(i));
3150 assert(TLI.isTypeLegal(Op.getValueType()) &&
3151 "Intrinsic uses a non-legal type?");
3152 Ops.push_back(Op);
3153 }
3154
Owen Andersone50ed302009-08-10 22:56:29 +00003155 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003156 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3157#ifndef NDEBUG
3158 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3159 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3160 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003161 }
Bob Wilson8d919552009-07-31 22:41:21 +00003162#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003163
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003164 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003165 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003166
Bob Wilson8d919552009-07-31 22:41:21 +00003167 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003168
3169 // Create the node.
3170 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003171 if (IsTgtIntrinsic) {
3172 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003173 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003174 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003175 Info.memVT, Info.ptrVal, Info.offset,
3176 Info.align, Info.vol,
3177 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003178 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003179 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003180 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003181 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003182 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003183 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003184 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003185 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003186 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003187 }
3188
3189 if (DisableScheduling)
3190 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003191
3192 if (HasChain) {
3193 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3194 if (OnlyLoad)
3195 PendingLoads.push_back(Chain);
3196 else
3197 DAG.setRoot(Chain);
3198 }
Bill Wendling856ff412009-12-22 00:12:37 +00003199
Benjamin Kramerf0127052010-01-05 13:12:22 +00003200 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003201 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003202 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003203 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003204
3205 if (DisableScheduling)
3206 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003207 }
Bill Wendling856ff412009-12-22 00:12:37 +00003208
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003209 setValue(&I, Result);
3210 }
3211}
3212
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003213/// GetSignificand - Get the significand and build it into a floating-point
3214/// number with exponent of 1:
3215///
3216/// Op = (Op & 0x007fffff) | 0x3f800000;
3217///
3218/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003219static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003220GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003221 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3222 DAG.getConstant(0x007fffff, MVT::i32));
3223 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3224 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003225 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3226
3227 if (DisableScheduling) {
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/// GetExponent - Get the exponent:
3237///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003238/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003239///
3240/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003241static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003242GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003243 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3245 DAG.getConstant(0x7f800000, MVT::i32));
3246 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003247 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003248 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3249 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003250 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3251
3252 if (DisableScheduling) {
3253 DAG.AssignOrdering(t0.getNode(), Order);
3254 DAG.AssignOrdering(t1.getNode(), Order);
3255 DAG.AssignOrdering(t2.getNode(), Order);
3256 DAG.AssignOrdering(Res.getNode(), Order);
3257 }
3258
3259 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003260}
3261
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003262/// getF32Constant - Get 32-bit floating point constant.
3263static SDValue
3264getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003265 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003266}
3267
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003268/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003269/// visitIntrinsicCall: I is a call instruction
3270/// Op is the associated NodeType for I
3271const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003272SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003273 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003274 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003275 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003276 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003277 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003278 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003279 getValue(I.getOperand(2)),
3280 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003281 setValue(&I, L);
3282 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003283
3284 if (DisableScheduling)
3285 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3286
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003287 return 0;
3288}
3289
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003290// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003291const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003292SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003293 SDValue Op1 = getValue(I.getOperand(1));
3294 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003295
Owen Anderson825b72b2009-08-11 20:47:22 +00003296 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003297 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003298
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003299 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003300
3301 if (DisableScheduling)
3302 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3303
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003304 return 0;
3305}
Bill Wendling74c37652008-12-09 22:08:41 +00003306
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003307/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3308/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003309void
Dan Gohman2048b852009-11-23 18:04:58 +00003310SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003311 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003312 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003313
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003315 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3316 SDValue Op = getValue(I.getOperand(1));
3317
3318 // Put the exponent in the right bit position for later addition to the
3319 // final result:
3320 //
3321 // #define LOG2OFe 1.4426950f
3322 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003323 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003324 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003325 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003326
3327 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003328 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3329 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003330
Bill Wendling856ff412009-12-22 00:12:37 +00003331 if (DisableScheduling) {
3332 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3333 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3334 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3335 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3336 }
3337
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003338 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003340 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003341
Bill Wendling856ff412009-12-22 00:12:37 +00003342 if (DisableScheduling)
3343 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3344
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003345 if (LimitFloatPrecision <= 6) {
3346 // For floating-point precision of 6:
3347 //
3348 // TwoToFractionalPartOfX =
3349 // 0.997535578f +
3350 // (0.735607626f + 0.252464424f * x) * x;
3351 //
3352 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003353 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003354 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003355 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003356 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003357 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3358 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003359 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003360 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003361
3362 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003363 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003364 TwoToFracPartOfX, IntegerPartOfX);
3365
Owen Anderson825b72b2009-08-11 20:47:22 +00003366 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003367
3368 if (DisableScheduling) {
3369 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3370 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3371 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3372 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3373 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3374 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3375 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3376 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003377 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3378 // For floating-point precision of 12:
3379 //
3380 // TwoToFractionalPartOfX =
3381 // 0.999892986f +
3382 // (0.696457318f +
3383 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3384 //
3385 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003386 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003387 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003388 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003389 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003390 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3391 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003392 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003393 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3394 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003395 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003396 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003397
3398 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003399 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003400 TwoToFracPartOfX, IntegerPartOfX);
3401
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003403
3404 if (DisableScheduling) {
3405 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3406 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3407 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3408 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3409 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3410 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3411 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3412 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3413 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3414 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003415 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3416 // For floating-point precision of 18:
3417 //
3418 // TwoToFractionalPartOfX =
3419 // 0.999999982f +
3420 // (0.693148872f +
3421 // (0.240227044f +
3422 // (0.554906021e-1f +
3423 // (0.961591928e-2f +
3424 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3425 //
3426 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003428 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003429 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003430 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003431 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3432 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003433 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3435 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003436 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003437 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3438 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003439 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003440 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3441 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003442 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003443 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3444 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003445 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003446 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003447 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003448
3449 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003450 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003451 TwoToFracPartOfX, IntegerPartOfX);
3452
Owen Anderson825b72b2009-08-11 20:47:22 +00003453 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003454
3455 if (DisableScheduling) {
3456 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3457 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3458 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3459 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3460 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3461 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3462 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3463 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3464 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3465 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3466 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3467 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3468 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3469 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3470 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3471 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003472 }
3473 } else {
3474 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003475 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003476 getValue(I.getOperand(1)).getValueType(),
3477 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003478 if (DisableScheduling)
3479 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003480 }
3481
Dale Johannesen59e577f2008-09-05 18:38:42 +00003482 setValue(&I, result);
3483}
3484
Bill Wendling39150252008-09-09 20:39:27 +00003485/// visitLog - Lower a log intrinsic. Handles the special sequences for
3486/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003487void
Dan Gohman2048b852009-11-23 18:04:58 +00003488SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003489 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003490 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003491
Owen Anderson825b72b2009-08-11 20:47:22 +00003492 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003493 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3494 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003496
Bill Wendling856ff412009-12-22 00:12:37 +00003497 if (DisableScheduling)
3498 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3499
Bill Wendling39150252008-09-09 20:39:27 +00003500 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003501 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003502 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003503 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003504
Bill Wendling856ff412009-12-22 00:12:37 +00003505 if (DisableScheduling)
3506 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3507
Bill Wendling39150252008-09-09 20:39:27 +00003508 // Get the significand and build it into a floating-point number with
3509 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003510 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003511
3512 if (LimitFloatPrecision <= 6) {
3513 // For floating-point precision of 6:
3514 //
3515 // LogofMantissa =
3516 // -1.1609546f +
3517 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003518 //
Bill Wendling39150252008-09-09 20:39:27 +00003519 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003521 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003523 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003524 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3525 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003526 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003527
Scott Michelfdc40a02009-02-17 22:15:04 +00003528 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003530
3531 if (DisableScheduling) {
3532 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3533 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3534 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3535 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3536 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3537 }
Bill Wendling39150252008-09-09 20:39:27 +00003538 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3539 // For floating-point precision of 12:
3540 //
3541 // LogOfMantissa =
3542 // -1.7417939f +
3543 // (2.8212026f +
3544 // (-1.4699568f +
3545 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3546 //
3547 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003548 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003549 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003550 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003551 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003552 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3553 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003554 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3556 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003557 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3559 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003560 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003561
Scott Michelfdc40a02009-02-17 22:15:04 +00003562 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003563 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003564
3565 if (DisableScheduling) {
3566 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3567 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3568 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3569 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3570 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3571 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3572 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3573 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3574 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3575 }
Bill Wendling39150252008-09-09 20:39:27 +00003576 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3577 // For floating-point precision of 18:
3578 //
3579 // LogOfMantissa =
3580 // -2.1072184f +
3581 // (4.2372794f +
3582 // (-3.7029485f +
3583 // (2.2781945f +
3584 // (-0.87823314f +
3585 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3586 //
3587 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003589 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003590 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003591 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3593 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003594 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3596 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003597 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003598 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3599 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003600 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003601 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3602 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003603 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003604 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3605 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003606 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003607
Scott Michelfdc40a02009-02-17 22:15:04 +00003608 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003610
3611 if (DisableScheduling) {
3612 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3613 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3614 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3615 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3616 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3617 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3618 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3619 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3620 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3621 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3622 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3623 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3624 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3625 }
Bill Wendling39150252008-09-09 20:39:27 +00003626 }
3627 } else {
3628 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003629 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003630 getValue(I.getOperand(1)).getValueType(),
3631 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003632
3633 if (DisableScheduling)
3634 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003635 }
3636
Dale Johannesen59e577f2008-09-05 18:38:42 +00003637 setValue(&I, result);
3638}
3639
Bill Wendling3eb59402008-09-09 00:28:24 +00003640/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3641/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003642void
Dan Gohman2048b852009-11-23 18:04:58 +00003643SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003644 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003645 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003646
Owen Anderson825b72b2009-08-11 20:47:22 +00003647 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003648 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3649 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003650 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003651
Bill Wendling856ff412009-12-22 00:12:37 +00003652 if (DisableScheduling)
3653 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3654
Bill Wendling39150252008-09-09 20:39:27 +00003655 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003656 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3657
3658 if (DisableScheduling)
3659 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003660
3661 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003662 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003663 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003664
Bill Wendling3eb59402008-09-09 00:28:24 +00003665 // Different possible minimax approximations of significand in
3666 // floating-point for various degrees of accuracy over [1,2].
3667 if (LimitFloatPrecision <= 6) {
3668 // For floating-point precision of 6:
3669 //
3670 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3671 //
3672 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003673 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003674 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003675 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003676 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003677 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3678 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003679 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003680
Scott Michelfdc40a02009-02-17 22:15:04 +00003681 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003682 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003683
3684 if (DisableScheduling) {
3685 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3686 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3687 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3688 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3689 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3690 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003691 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3692 // For floating-point precision of 12:
3693 //
3694 // Log2ofMantissa =
3695 // -2.51285454f +
3696 // (4.07009056f +
3697 // (-2.12067489f +
3698 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003699 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003700 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003701 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003702 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003703 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003704 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003705 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3706 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003707 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003708 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3709 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003710 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003711 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3712 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003713 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003714
Scott Michelfdc40a02009-02-17 22:15:04 +00003715 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003716 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003717
3718 if (DisableScheduling) {
3719 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3720 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3721 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3722 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3723 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3724 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3725 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3726 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3727 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3728 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003729 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3730 // For floating-point precision of 18:
3731 //
3732 // Log2ofMantissa =
3733 // -3.0400495f +
3734 // (6.1129976f +
3735 // (-5.3420409f +
3736 // (3.2865683f +
3737 // (-1.2669343f +
3738 // (0.27515199f -
3739 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3740 //
3741 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003742 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003743 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003744 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003745 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003746 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3747 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003748 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003749 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3750 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003751 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003752 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3753 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003754 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003755 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3756 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003757 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003758 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3759 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003760 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003761
Scott Michelfdc40a02009-02-17 22:15:04 +00003762 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003763 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003764
3765 if (DisableScheduling) {
3766 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3767 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3768 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3769 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3770 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3771 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3772 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3773 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3774 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3775 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3776 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3777 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3778 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3779 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003780 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003781 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003782 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003783 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003784 getValue(I.getOperand(1)).getValueType(),
3785 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003786
3787 if (DisableScheduling)
3788 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003789 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003790
Dale Johannesen59e577f2008-09-05 18:38:42 +00003791 setValue(&I, result);
3792}
3793
Bill Wendling3eb59402008-09-09 00:28:24 +00003794/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3795/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003796void
Dan Gohman2048b852009-11-23 18:04:58 +00003797SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003798 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003799 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003800
Owen Anderson825b72b2009-08-11 20:47:22 +00003801 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003802 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3803 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003804 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003805
Bill Wendling856ff412009-12-22 00:12:37 +00003806 if (DisableScheduling)
3807 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3808
Bill Wendling39150252008-09-09 20:39:27 +00003809 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003810 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003811 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003812 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003813
Bill Wendling856ff412009-12-22 00:12:37 +00003814 if (DisableScheduling)
3815 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3816
Bill Wendling3eb59402008-09-09 00:28:24 +00003817 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003818 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003819 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003820
3821 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003822 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003823 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003824 // Log10ofMantissa =
3825 // -0.50419619f +
3826 // (0.60948995f - 0.10380950f * x) * x;
3827 //
3828 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003829 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003830 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003831 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003832 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003833 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3834 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003835 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003836
Scott Michelfdc40a02009-02-17 22:15:04 +00003837 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003838 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003839
3840 if (DisableScheduling) {
3841 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3842 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3843 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3844 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3845 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3846 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003847 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3848 // For floating-point precision of 12:
3849 //
3850 // Log10ofMantissa =
3851 // -0.64831180f +
3852 // (0.91751397f +
3853 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3854 //
3855 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003856 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003857 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003858 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003859 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003860 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3861 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003862 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003863 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3864 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003865 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003866
Scott Michelfdc40a02009-02-17 22:15:04 +00003867 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003868 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003869
3870 if (DisableScheduling) {
3871 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3872 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3873 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3874 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3875 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3876 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3877 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3878 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003879 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003880 // For floating-point precision of 18:
3881 //
3882 // Log10ofMantissa =
3883 // -0.84299375f +
3884 // (1.5327582f +
3885 // (-1.0688956f +
3886 // (0.49102474f +
3887 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3888 //
3889 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003890 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003891 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003892 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003893 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003894 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3895 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003896 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003897 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3898 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003899 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003900 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3901 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003902 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003903 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3904 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003905 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003906
Scott Michelfdc40a02009-02-17 22:15:04 +00003907 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003908 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003909
3910 if (DisableScheduling) {
3911 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3912 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3913 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3914 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3915 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3916 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3917 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3918 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3919 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3920 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3921 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3922 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003923 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003924 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003925 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003926 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003927 getValue(I.getOperand(1)).getValueType(),
3928 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003929
3930 if (DisableScheduling)
3931 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003932 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003933
Dale Johannesen59e577f2008-09-05 18:38:42 +00003934 setValue(&I, result);
3935}
3936
Bill Wendlinge10c8142008-09-09 22:39:21 +00003937/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3938/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003939void
Dan Gohman2048b852009-11-23 18:04:58 +00003940SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003941 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003942 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003943
Owen Anderson825b72b2009-08-11 20:47:22 +00003944 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003945 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3946 SDValue Op = getValue(I.getOperand(1));
3947
Owen Anderson825b72b2009-08-11 20:47:22 +00003948 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003949
Bill Wendling856ff412009-12-22 00:12:37 +00003950 if (DisableScheduling)
3951 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3952
Bill Wendlinge10c8142008-09-09 22:39:21 +00003953 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3955 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003956
3957 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003959 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003960
Bill Wendling856ff412009-12-22 00:12:37 +00003961 if (DisableScheduling) {
3962 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3963 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3964 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3965 }
3966
Bill Wendlinge10c8142008-09-09 22:39:21 +00003967 if (LimitFloatPrecision <= 6) {
3968 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003969 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003970 // TwoToFractionalPartOfX =
3971 // 0.997535578f +
3972 // (0.735607626f + 0.252464424f * x) * x;
3973 //
3974 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003975 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003976 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003977 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003978 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003979 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3980 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003981 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003982 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003983 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003985
Scott Michelfdc40a02009-02-17 22:15:04 +00003986 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003987 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003988
3989 if (DisableScheduling) {
3990 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3991 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3992 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3993 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3994 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3995 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3996 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3997 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003998 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3999 // For floating-point precision of 12:
4000 //
4001 // TwoToFractionalPartOfX =
4002 // 0.999892986f +
4003 // (0.696457318f +
4004 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4005 //
4006 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004007 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004008 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004010 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004011 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4012 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004013 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004014 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4015 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004016 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004017 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004018 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004019 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004020
Scott Michelfdc40a02009-02-17 22:15:04 +00004021 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004023
4024 if (DisableScheduling) {
4025 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4026 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4027 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4028 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4029 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4030 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4031 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4032 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4033 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4034 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004035 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4036 // For floating-point precision of 18:
4037 //
4038 // TwoToFractionalPartOfX =
4039 // 0.999999982f +
4040 // (0.693148872f +
4041 // (0.240227044f +
4042 // (0.554906021e-1f +
4043 // (0.961591928e-2f +
4044 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4045 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004046 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004047 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004048 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004049 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4051 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004052 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004053 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4054 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004055 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004056 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4057 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004058 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004059 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4060 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004061 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004062 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4063 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004064 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004065 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004066 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004067 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004068
Scott Michelfdc40a02009-02-17 22:15:04 +00004069 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004070 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004071
4072 if (DisableScheduling) {
4073 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4074 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4075 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4076 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4077 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4078 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4079 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4080 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4081 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4082 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4083 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4084 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4085 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4086 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4087 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4088 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004089 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00004090 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00004091 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004092 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00004093 getValue(I.getOperand(1)).getValueType(),
4094 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00004095
4096 if (DisableScheduling)
4097 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004098 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004099
Dale Johannesen601d3c02008-09-05 01:48:15 +00004100 setValue(&I, result);
4101}
4102
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004103/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4104/// limited-precision mode with x == 10.0f.
4105void
Dan Gohman2048b852009-11-23 18:04:58 +00004106SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004107 SDValue result;
4108 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004109 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004110 bool IsExp10 = false;
4111
Owen Anderson825b72b2009-08-11 20:47:22 +00004112 if (getValue(Val).getValueType() == MVT::f32 &&
4113 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004114 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4115 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4116 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4117 APFloat Ten(10.0f);
4118 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4119 }
4120 }
4121 }
4122
4123 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4124 SDValue Op = getValue(I.getOperand(2));
4125
4126 // Put the exponent in the right bit position for later addition to the
4127 // final result:
4128 //
4129 // #define LOG2OF10 3.3219281f
4130 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004131 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004132 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004133 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004134
4135 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004136 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4137 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004138
Bill Wendling856ff412009-12-22 00:12:37 +00004139 if (DisableScheduling) {
4140 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4141 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4142 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4143 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4144 }
4145
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004146 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004148 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004149
Bill Wendling856ff412009-12-22 00:12:37 +00004150 if (DisableScheduling)
4151 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4152
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004153 if (LimitFloatPrecision <= 6) {
4154 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004155 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004156 // twoToFractionalPartOfX =
4157 // 0.997535578f +
4158 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004159 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004160 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004162 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004163 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004164 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004165 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4166 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004167 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004168 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004169 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004170 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004171
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004172 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004173 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004174
4175 if (DisableScheduling) {
4176 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4177 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4178 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4179 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4180 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4181 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4182 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4183 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004184 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4185 // For floating-point precision of 12:
4186 //
4187 // TwoToFractionalPartOfX =
4188 // 0.999892986f +
4189 // (0.696457318f +
4190 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4191 //
4192 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004193 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004194 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004195 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004196 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004197 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4198 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004199 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004200 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4201 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004202 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004203 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004204 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004205 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004206
Scott Michelfdc40a02009-02-17 22:15:04 +00004207 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004208 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004209
4210 if (DisableScheduling) {
4211 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4212 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4213 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4214 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4215 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4216 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4217 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4218 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4219 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4220 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004221 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4222 // For floating-point precision of 18:
4223 //
4224 // TwoToFractionalPartOfX =
4225 // 0.999999982f +
4226 // (0.693148872f +
4227 // (0.240227044f +
4228 // (0.554906021e-1f +
4229 // (0.961591928e-2f +
4230 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4231 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004233 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004234 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004235 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4237 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004238 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4240 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004241 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4243 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004244 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004245 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4246 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004247 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004248 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4249 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004250 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004251 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004252 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004253 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004254
Scott Michelfdc40a02009-02-17 22:15:04 +00004255 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004256 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004257
4258 if (DisableScheduling) {
4259 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4260 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4261 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4262 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4263 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4264 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4265 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4266 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4267 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4268 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4269 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4270 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4271 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4272 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4273 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4274 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004275 }
4276 } else {
4277 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004278 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004279 getValue(I.getOperand(1)).getValueType(),
4280 getValue(I.getOperand(1)),
4281 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004282
4283 if (DisableScheduling)
4284 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004285 }
4286
4287 setValue(&I, result);
4288}
4289
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004290
4291/// ExpandPowI - Expand a llvm.powi intrinsic.
4292static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
4293 SelectionDAG &DAG) {
4294 // If RHS is a constant, we can expand this out to a multiplication tree,
4295 // otherwise we end up lowering to a call to __powidf2 (for example). When
4296 // optimizing for size, we only want to do this if the expansion would produce
4297 // a small number of multiplies, otherwise we do the full expansion.
4298 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4299 // Get the exponent as a positive value.
4300 unsigned Val = RHSC->getSExtValue();
4301 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004302
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004303 // powi(x, 0) -> 1.0
4304 if (Val == 0)
4305 return DAG.getConstantFP(1.0, LHS.getValueType());
4306
4307 Function *F = DAG.getMachineFunction().getFunction();
4308 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
4309 // If optimizing for size, don't insert too many multiplies. This
4310 // inserts up to 5 multiplies.
4311 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4312 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004313 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004314 // powi(x,15) generates one more multiply than it should), but this has
4315 // the benefit of being both really simple and much better than a libcall.
4316 SDValue Res; // Logically starts equal to 1.0
4317 SDValue CurSquare = LHS;
4318 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004319 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004320 if (Res.getNode())
4321 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4322 else
4323 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004324 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004325
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004326 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4327 CurSquare, CurSquare);
4328 Val >>= 1;
4329 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004330
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004331 // If the original was negative, invert the result, producing 1/(x*x*x).
4332 if (RHSC->getSExtValue() < 0)
4333 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4334 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4335 return Res;
4336 }
4337 }
4338
4339 // Otherwise, expand to a libcall.
4340 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4341}
4342
4343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004344/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4345/// we want to emit this as a call to a named external function, return the name
4346/// otherwise lower it and return null.
4347const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004348SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004349 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004350 SDValue Res;
4351
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004352 switch (Intrinsic) {
4353 default:
4354 // By default, turn this into a target intrinsic node.
4355 visitTargetIntrinsic(I, Intrinsic);
4356 return 0;
4357 case Intrinsic::vastart: visitVAStart(I); return 0;
4358 case Intrinsic::vaend: visitVAEnd(I); return 0;
4359 case Intrinsic::vacopy: visitVACopy(I); return 0;
4360 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004361 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4362 getValue(I.getOperand(1)));
4363 setValue(&I, Res);
4364 if (DisableScheduling)
4365 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004366 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004367 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004368 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4369 getValue(I.getOperand(1)));
4370 setValue(&I, Res);
4371 if (DisableScheduling)
4372 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004373 return 0;
4374 case Intrinsic::setjmp:
4375 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004376 case Intrinsic::longjmp:
4377 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004378 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004379 SDValue Op1 = getValue(I.getOperand(1));
4380 SDValue Op2 = getValue(I.getOperand(2));
4381 SDValue Op3 = getValue(I.getOperand(3));
4382 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004383 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4384 I.getOperand(1), 0, I.getOperand(2), 0);
4385 DAG.setRoot(Res);
4386 if (DisableScheduling)
4387 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004388 return 0;
4389 }
Chris Lattner824b9582008-11-21 16:42:48 +00004390 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004391 SDValue Op1 = getValue(I.getOperand(1));
4392 SDValue Op2 = getValue(I.getOperand(2));
4393 SDValue Op3 = getValue(I.getOperand(3));
4394 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004395 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4396 I.getOperand(1), 0);
4397 DAG.setRoot(Res);
4398 if (DisableScheduling)
4399 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004400 return 0;
4401 }
Chris Lattner824b9582008-11-21 16:42:48 +00004402 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004403 SDValue Op1 = getValue(I.getOperand(1));
4404 SDValue Op2 = getValue(I.getOperand(2));
4405 SDValue Op3 = getValue(I.getOperand(3));
4406 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4407
4408 // If the source and destination are known to not be aliases, we can
4409 // lower memmove as memcpy.
4410 uint64_t Size = -1ULL;
4411 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004412 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004413 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4414 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004415 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4416 I.getOperand(1), 0, I.getOperand(2), 0);
4417 DAG.setRoot(Res);
4418 if (DisableScheduling)
4419 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004420 return 0;
4421 }
4422
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004423 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4424 I.getOperand(1), 0, I.getOperand(2), 0);
4425 DAG.setRoot(Res);
4426 if (DisableScheduling)
4427 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004428 return 0;
4429 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004430 case Intrinsic::dbg_declare: {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004431 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00004432 // FIXME: Variable debug info is not supported here.
4433 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004434 DwarfWriter *DW = DAG.getDwarfWriter();
4435 if (!DW)
4436 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004437 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00004438 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00004439 return 0;
4440
Devang Patelac1ceb32009-10-09 22:42:28 +00004441 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004442 Value *Address = DI.getAddress();
4443 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4444 Address = BCI->getOperand(0);
4445 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4446 // Don't handle byval struct arguments or VLAs, for example.
4447 if (!AI)
4448 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004449 DenseMap<const AllocaInst*, int>::iterator SI =
4450 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004451 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00004452 return 0; // VLAs.
4453 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004454
Chris Lattner3990b122009-12-28 23:41:32 +00004455 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo())
4456 if (MDNode *Dbg = DI.getMetadata("dbg"))
Chris Lattner0eb41982009-12-28 20:45:51 +00004457 MMI->setVariableDbgInfo(Variable, FI, Dbg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004458 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004459 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004460 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004461 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004462 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004463 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004464 SDValue Ops[1];
4465 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004466 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004467 setValue(&I, Op);
4468 DAG.setRoot(Op.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004469 if (DisableScheduling)
4470 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004471 return 0;
4472 }
4473
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004474 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004475 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004476
Chris Lattner3a5815f2009-09-17 23:54:54 +00004477 if (CurMBB->isLandingPad())
4478 AddCatchInfo(I, MMI, CurMBB);
4479 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004480#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004481 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004482#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004483 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4484 unsigned Reg = TLI.getExceptionSelectorRegister();
4485 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004486 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004487
Chris Lattner3a5815f2009-09-17 23:54:54 +00004488 // Insert the EHSELECTION instruction.
4489 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4490 SDValue Ops[2];
4491 Ops[0] = getValue(I.getOperand(1));
4492 Ops[1] = getRoot();
4493 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4494
4495 DAG.setRoot(Op.getValue(1));
4496
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004497 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4498 setValue(&I, Res);
4499 if (DisableScheduling) {
4500 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4501 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4502 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004503 return 0;
4504 }
4505
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004506 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004507 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004508
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004509 if (MMI) {
4510 // Find the type id for the given typeinfo.
4511 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004512 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004513 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004514 } else {
4515 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004516 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004517 }
4518
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004519 setValue(&I, Res);
4520 if (DisableScheduling)
4521 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004522 return 0;
4523 }
4524
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004525 case Intrinsic::eh_return_i32:
4526 case Intrinsic::eh_return_i64:
4527 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004528 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004529 Res = DAG.getNode(ISD::EH_RETURN, dl,
4530 MVT::Other,
4531 getControlRoot(),
4532 getValue(I.getOperand(1)),
4533 getValue(I.getOperand(2)));
4534 DAG.setRoot(Res);
4535 if (DisableScheduling)
4536 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004537 } else {
4538 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4539 }
4540
4541 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004542 case Intrinsic::eh_unwind_init:
4543 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4544 MMI->setCallsUnwindInit(true);
4545 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004546 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004547 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004548 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004549 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4550 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004551 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004552 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004553 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004554 TLI.getPointerTy()),
4555 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004556 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004557 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004558 DAG.getConstant(0, TLI.getPointerTy()));
4559 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4560 FA, Offset);
4561 setValue(&I, Res);
4562 if (DisableScheduling) {
4563 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4564 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4565 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4566 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4567 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004568 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004569 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004570 case Intrinsic::convertff:
4571 case Intrinsic::convertfsi:
4572 case Intrinsic::convertfui:
4573 case Intrinsic::convertsif:
4574 case Intrinsic::convertuif:
4575 case Intrinsic::convertss:
4576 case Intrinsic::convertsu:
4577 case Intrinsic::convertus:
4578 case Intrinsic::convertuu: {
4579 ISD::CvtCode Code = ISD::CVT_INVALID;
4580 switch (Intrinsic) {
4581 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4582 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4583 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4584 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4585 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4586 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4587 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4588 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4589 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4590 }
Owen Andersone50ed302009-08-10 22:56:29 +00004591 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004592 Value *Op1 = I.getOperand(1);
4593 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4594 DAG.getValueType(DestVT),
4595 DAG.getValueType(getValue(Op1).getValueType()),
4596 getValue(I.getOperand(2)),
4597 getValue(I.getOperand(3)),
4598 Code);
4599 setValue(&I, Res);
4600 if (DisableScheduling)
4601 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004602 return 0;
4603 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004604 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004605 Res = DAG.getNode(ISD::FSQRT, dl,
4606 getValue(I.getOperand(1)).getValueType(),
4607 getValue(I.getOperand(1)));
4608 setValue(&I, Res);
4609 if (DisableScheduling)
4610 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004611 return 0;
4612 case Intrinsic::powi:
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004613 Res = ExpandPowI(dl, getValue(I.getOperand(1)), getValue(I.getOperand(2)),
4614 DAG);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004615 setValue(&I, Res);
4616 if (DisableScheduling)
4617 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004618 return 0;
4619 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004620 Res = DAG.getNode(ISD::FSIN, dl,
4621 getValue(I.getOperand(1)).getValueType(),
4622 getValue(I.getOperand(1)));
4623 setValue(&I, Res);
4624 if (DisableScheduling)
4625 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004626 return 0;
4627 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004628 Res = DAG.getNode(ISD::FCOS, dl,
4629 getValue(I.getOperand(1)).getValueType(),
4630 getValue(I.getOperand(1)));
4631 setValue(&I, Res);
4632 if (DisableScheduling)
4633 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004634 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004635 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004636 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004637 return 0;
4638 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004639 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004640 return 0;
4641 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004642 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004643 return 0;
4644 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004645 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004646 return 0;
4647 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004648 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004649 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004651 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004652 return 0;
4653 case Intrinsic::pcmarker: {
4654 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004655 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4656 DAG.setRoot(Res);
4657 if (DisableScheduling)
4658 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004659 return 0;
4660 }
4661 case Intrinsic::readcyclecounter: {
4662 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004663 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4664 DAG.getVTList(MVT::i64, MVT::Other),
4665 &Op, 1);
4666 setValue(&I, Res);
4667 DAG.setRoot(Res.getValue(1));
4668 if (DisableScheduling)
4669 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004670 return 0;
4671 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004672 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004673 Res = DAG.getNode(ISD::BSWAP, dl,
4674 getValue(I.getOperand(1)).getValueType(),
4675 getValue(I.getOperand(1)));
4676 setValue(&I, Res);
4677 if (DisableScheduling)
4678 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004679 return 0;
4680 case Intrinsic::cttz: {
4681 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004682 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004683 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4684 setValue(&I, Res);
4685 if (DisableScheduling)
4686 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004687 return 0;
4688 }
4689 case Intrinsic::ctlz: {
4690 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004691 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004692 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4693 setValue(&I, Res);
4694 if (DisableScheduling)
4695 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004696 return 0;
4697 }
4698 case Intrinsic::ctpop: {
4699 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004700 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004701 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4702 setValue(&I, Res);
4703 if (DisableScheduling)
4704 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004705 return 0;
4706 }
4707 case Intrinsic::stacksave: {
4708 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004709 Res = DAG.getNode(ISD::STACKSAVE, dl,
4710 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4711 setValue(&I, Res);
4712 DAG.setRoot(Res.getValue(1));
4713 if (DisableScheduling)
4714 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004715 return 0;
4716 }
4717 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004718 Res = getValue(I.getOperand(1));
4719 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4720 DAG.setRoot(Res);
4721 if (DisableScheduling)
4722 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004723 return 0;
4724 }
Bill Wendling57344502008-11-18 11:01:33 +00004725 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004726 // Emit code into the DAG to store the stack guard onto the stack.
4727 MachineFunction &MF = DAG.getMachineFunction();
4728 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004729 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004730
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004731 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4732 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004733
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004734 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004735 MFI->setStackProtectorIndex(FI);
4736
4737 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4738
4739 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004740 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4741 PseudoSourceValue::getFixedStack(FI),
4742 0, true);
4743 setValue(&I, Res);
4744 DAG.setRoot(Res);
4745 if (DisableScheduling)
4746 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004747 return 0;
4748 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004749 case Intrinsic::objectsize: {
4750 // If we don't know by now, we're never going to know.
4751 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4752
4753 assert(CI && "Non-constant type in __builtin_object_size?");
4754
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004755 SDValue Arg = getValue(I.getOperand(0));
4756 EVT Ty = Arg.getValueType();
4757
Eric Christopherd060b252009-12-23 02:51:48 +00004758 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004759 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004760 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004761 Res = DAG.getConstant(0, Ty);
4762
4763 setValue(&I, Res);
4764 if (DisableScheduling)
4765 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004766 return 0;
4767 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 case Intrinsic::var_annotation:
4769 // Discard annotate attributes
4770 return 0;
4771
4772 case Intrinsic::init_trampoline: {
4773 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4774
4775 SDValue Ops[6];
4776 Ops[0] = getRoot();
4777 Ops[1] = getValue(I.getOperand(1));
4778 Ops[2] = getValue(I.getOperand(2));
4779 Ops[3] = getValue(I.getOperand(3));
4780 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4781 Ops[5] = DAG.getSrcValue(F);
4782
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004783 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4784 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4785 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004786
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004787 setValue(&I, Res);
4788 DAG.setRoot(Res.getValue(1));
4789 if (DisableScheduling)
4790 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004791 return 0;
4792 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004793 case Intrinsic::gcroot:
4794 if (GFI) {
4795 Value *Alloca = I.getOperand(1);
4796 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004797
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004798 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4799 GFI->addStackRoot(FI->getIndex(), TypeMap);
4800 }
4801 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802 case Intrinsic::gcread:
4803 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004804 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004805 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004806 case Intrinsic::flt_rounds:
4807 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4808 setValue(&I, Res);
4809 if (DisableScheduling)
4810 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004811 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004812 case Intrinsic::trap:
4813 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4814 DAG.setRoot(Res);
4815 if (DisableScheduling)
4816 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004817 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004818 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004819 return implVisitAluOverflow(I, ISD::UADDO);
4820 case Intrinsic::sadd_with_overflow:
4821 return implVisitAluOverflow(I, ISD::SADDO);
4822 case Intrinsic::usub_with_overflow:
4823 return implVisitAluOverflow(I, ISD::USUBO);
4824 case Intrinsic::ssub_with_overflow:
4825 return implVisitAluOverflow(I, ISD::SSUBO);
4826 case Intrinsic::umul_with_overflow:
4827 return implVisitAluOverflow(I, ISD::UMULO);
4828 case Intrinsic::smul_with_overflow:
4829 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004830
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004831 case Intrinsic::prefetch: {
4832 SDValue Ops[4];
4833 Ops[0] = getRoot();
4834 Ops[1] = getValue(I.getOperand(1));
4835 Ops[2] = getValue(I.getOperand(2));
4836 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004837 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4838 DAG.setRoot(Res);
4839 if (DisableScheduling)
4840 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004841 return 0;
4842 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004843
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004844 case Intrinsic::memory_barrier: {
4845 SDValue Ops[6];
4846 Ops[0] = getRoot();
4847 for (int x = 1; x < 6; ++x)
4848 Ops[x] = getValue(I.getOperand(x));
4849
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004850 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4851 DAG.setRoot(Res);
4852 if (DisableScheduling)
4853 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854 return 0;
4855 }
4856 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004857 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004858 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004859 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004860 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4861 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004862 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004863 getValue(I.getOperand(2)),
4864 getValue(I.getOperand(3)),
4865 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004866 setValue(&I, L);
4867 DAG.setRoot(L.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004868 if (DisableScheduling)
4869 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004870 return 0;
4871 }
4872 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004873 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004874 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004875 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004876 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004877 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004878 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004879 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004880 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004881 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004882 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004883 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004884 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004885 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004886 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004887 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004888 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004889 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004890 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004891 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004892 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004893 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004894
4895 case Intrinsic::invariant_start:
4896 case Intrinsic::lifetime_start:
4897 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004898 Res = DAG.getUNDEF(TLI.getPointerTy());
4899 setValue(&I, Res);
4900 if (DisableScheduling)
4901 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004902 return 0;
4903 case Intrinsic::invariant_end:
4904 case Intrinsic::lifetime_end:
4905 // Discard region information.
4906 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004907 }
4908}
4909
Dan Gohman98ca4f22009-08-05 01:29:28 +00004910/// Test if the given instruction is in a position to be optimized
4911/// with a tail-call. This roughly means that it's in a block with
4912/// a return and there's nothing that needs to be scheduled
4913/// between it and the return.
4914///
4915/// This function only tests target-independent requirements.
4916/// For target-dependent requirements, a target should override
4917/// TargetLowering::IsEligibleForTailCallOptimization.
4918///
4919static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004920isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004921 const TargetLowering &TLI) {
4922 const BasicBlock *ExitBB = I->getParent();
4923 const TerminatorInst *Term = ExitBB->getTerminator();
4924 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4925 const Function *F = ExitBB->getParent();
4926
4927 // The block must end in a return statement or an unreachable.
4928 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4929
4930 // If I will have a chain, make sure no other instruction that will have a
4931 // chain interposes between I and the return.
4932 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4933 !I->isSafeToSpeculativelyExecute())
4934 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4935 --BBI) {
4936 if (&*BBI == I)
4937 break;
4938 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4939 !BBI->isSafeToSpeculativelyExecute())
4940 return false;
4941 }
4942
4943 // If the block ends with a void return or unreachable, it doesn't matter
4944 // what the call's return type is.
4945 if (!Ret || Ret->getNumOperands() == 0) return true;
4946
Dan Gohmaned9bab32009-11-14 02:06:30 +00004947 // If the return value is undef, it doesn't matter what the call's
4948 // return type is.
4949 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4950
Dan Gohman98ca4f22009-08-05 01:29:28 +00004951 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004952 // the return. Ignore noalias because it doesn't affect the call sequence.
4953 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4954 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004955 return false;
4956
4957 // Otherwise, make sure the unmodified return value of I is the return value.
4958 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4959 U = dyn_cast<Instruction>(U->getOperand(0))) {
4960 if (!U)
4961 return false;
4962 if (!U->hasOneUse())
4963 return false;
4964 if (U == I)
4965 break;
4966 // Check for a truly no-op truncate.
4967 if (isa<TruncInst>(U) &&
4968 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4969 continue;
4970 // Check for a truly no-op bitcast.
4971 if (isa<BitCastInst>(U) &&
4972 (U->getOperand(0)->getType() == U->getType() ||
4973 (isa<PointerType>(U->getOperand(0)->getType()) &&
4974 isa<PointerType>(U->getType()))))
4975 continue;
4976 // Otherwise it's not a true no-op.
4977 return false;
4978 }
4979
4980 return true;
4981}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004982
Dan Gohman2048b852009-11-23 18:04:58 +00004983void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4984 bool isTailCall,
4985 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004986 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4987 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004988 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004989 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4990 unsigned BeginLabel = 0, EndLabel = 0;
4991
4992 TargetLowering::ArgListTy Args;
4993 TargetLowering::ArgListEntry Entry;
4994 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004995
4996 // Check whether the function can return without sret-demotion.
4997 SmallVector<EVT, 4> OutVTs;
4998 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4999 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005000 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00005001 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005002
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005003 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005004 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
5005
5006 SDValue DemoteStackSlot;
5007
5008 if (!CanLowerReturn) {
5009 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
5010 FTy->getReturnType());
5011 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
5012 FTy->getReturnType());
5013 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005014 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005015 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
5016
5017 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
5018 Entry.Node = DemoteStackSlot;
5019 Entry.Ty = StackSlotPtrType;
5020 Entry.isSExt = false;
5021 Entry.isZExt = false;
5022 Entry.isInReg = false;
5023 Entry.isSRet = true;
5024 Entry.isNest = false;
5025 Entry.isByVal = false;
5026 Entry.Alignment = Align;
5027 Args.push_back(Entry);
5028 RetTy = Type::getVoidTy(FTy->getContext());
5029 }
5030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005031 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005032 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005033 SDValue ArgNode = getValue(*i);
5034 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
5035
5036 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00005037 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5038 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5039 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5040 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5041 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5042 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005043 Entry.Alignment = CS.getParamAlignment(attrInd);
5044 Args.push_back(Entry);
5045 }
5046
5047 if (LandingPad && MMI) {
5048 // Insert a label before the invoke call to mark the try range. This can be
5049 // used to detect deletion of the invoke via the MachineModuleInfo.
5050 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005051
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005052 // Both PendingLoads and PendingExports must be flushed here;
5053 // this call might not return.
5054 (void)getRoot();
Bill Wendling0d580132009-12-23 01:28:19 +00005055 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5056 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005057 }
5058
Dan Gohman98ca4f22009-08-05 01:29:28 +00005059 // Check if target-independent constraints permit a tail call here.
5060 // Target-dependent constraints are checked within TLI.LowerCallTo.
5061 if (isTailCall &&
5062 !isInTailCallPosition(CS.getInstruction(),
5063 CS.getAttributes().getRetAttributes(),
5064 TLI))
5065 isTailCall = false;
5066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005067 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005068 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00005069 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005070 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005071 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005072 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00005073 isTailCall,
5074 !CS.getInstruction()->use_empty(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005075 Callee, Args, DAG, getCurDebugLoc(), SDNodeOrder);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005076 assert((isTailCall || Result.second.getNode()) &&
5077 "Non-null chain expected with non-tail call!");
5078 assert((Result.second.getNode() || !Result.first.getNode()) &&
5079 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005080 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005081 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005082 if (DisableScheduling)
5083 DAG.AssignOrdering(Result.first.getNode(), SDNodeOrder);
5084 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005085 // The instruction result is the result of loading from the
5086 // hidden sret parameter.
5087 SmallVector<EVT, 1> PVTs;
5088 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
5089
5090 ComputeValueVTs(TLI, PtrRetTy, PVTs);
5091 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5092 EVT PtrVT = PVTs[0];
5093 unsigned NumValues = OutVTs.size();
5094 SmallVector<SDValue, 4> Values(NumValues);
5095 SmallVector<SDValue, 4> Chains(NumValues);
5096
5097 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00005098 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
5099 DemoteStackSlot,
5100 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005101 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005102 Add, NULL, Offsets[i], false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005103 Values[i] = L;
5104 Chains[i] = L.getValue(1);
5105 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005106
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005107 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
5108 MVT::Other, &Chains[0], NumValues);
5109 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00005110
5111 // Collect the legal value parts into potentially illegal values
5112 // that correspond to the original function's return values.
5113 SmallVector<EVT, 4> RetTys;
5114 RetTy = FTy->getReturnType();
5115 ComputeValueVTs(TLI, RetTy, RetTys);
5116 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5117 SmallVector<SDValue, 4> ReturnValues;
5118 unsigned CurReg = 0;
5119 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
5120 EVT VT = RetTys[I];
5121 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
5122 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
5123
5124 SDValue ReturnValue =
5125 getCopyFromParts(DAG, getCurDebugLoc(), SDNodeOrder, &Values[CurReg], NumRegs,
5126 RegisterVT, VT, AssertOp);
5127 ReturnValues.push_back(ReturnValue);
5128 if (DisableScheduling)
5129 DAG.AssignOrdering(ReturnValue.getNode(), SDNodeOrder);
5130 CurReg += NumRegs;
5131 }
5132 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
5133 DAG.getVTList(&RetTys[0], RetTys.size()),
5134 &ReturnValues[0], ReturnValues.size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005135
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00005136 setValue(CS.getInstruction(), Res);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005137
5138 if (DisableScheduling) {
5139 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00005140 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005141 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005142 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005143
5144 // As a special case, a null chain means that a tail call has been emitted and
5145 // the DAG root is already updated.
5146 if (Result.second.getNode()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005147 DAG.setRoot(Result.second);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005148 if (DisableScheduling)
5149 DAG.AssignOrdering(Result.second.getNode(), SDNodeOrder);
5150 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005151 HasTailCall = true;
Bill Wendlinge80ae832009-12-22 00:50:32 +00005152 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005153
5154 if (LandingPad && MMI) {
5155 // Insert a label at the end of the invoke call to mark the try range. This
5156 // can be used to detect deletion of the invoke via the MachineModuleInfo.
5157 EndLabel = MMI->NextLabelID();
Bill Wendling0d580132009-12-23 01:28:19 +00005158 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5159 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005160
5161 // Inform MachineModuleInfo of range.
5162 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
5163 }
5164}
5165
Chris Lattner8047d9a2009-12-24 00:37:38 +00005166/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5167/// value is equal or not-equal to zero.
5168static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
5169 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
5170 UI != E; ++UI) {
5171 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
5172 if (IC->isEquality())
5173 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
5174 if (C->isNullValue())
5175 continue;
5176 // Unknown instruction.
5177 return false;
5178 }
5179 return true;
5180}
5181
Chris Lattner04b091a2009-12-24 01:07:17 +00005182static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005183 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005184
Chris Lattner8047d9a2009-12-24 00:37:38 +00005185 // Check to see if this load can be trivially constant folded, e.g. if the
5186 // input is from a string literal.
5187 if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
5188 // Cast pointer to the type we really want to load.
5189 LoadInput = ConstantExpr::getBitCast(LoadInput,
5190 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005191
Chris Lattner8047d9a2009-12-24 00:37:38 +00005192 if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD))
5193 return Builder.getValue(LoadCst);
5194 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005195
Chris Lattner8047d9a2009-12-24 00:37:38 +00005196 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5197 // still constant memory, the input chain can be the entry node.
5198 SDValue Root;
5199 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005200
Chris Lattner8047d9a2009-12-24 00:37:38 +00005201 // Do not serialize (non-volatile) loads of constant memory with anything.
5202 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5203 Root = Builder.DAG.getEntryNode();
5204 ConstantMemory = true;
5205 } else {
5206 // Do not serialize non-volatile loads against each other.
5207 Root = Builder.DAG.getRoot();
5208 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005209
Chris Lattner8047d9a2009-12-24 00:37:38 +00005210 SDValue Ptr = Builder.getValue(PtrVal);
5211 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
5212 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
5213 false /*volatile*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005214
Chris Lattner8047d9a2009-12-24 00:37:38 +00005215 if (!ConstantMemory)
5216 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5217 return LoadVal;
5218}
5219
5220
5221/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5222/// If so, return true and lower it, otherwise return false and it will be
5223/// lowered like a normal call.
5224bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) {
5225 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
5226 if (I.getNumOperands() != 4)
5227 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005228
Chris Lattner8047d9a2009-12-24 00:37:38 +00005229 Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
5230 if (!isa<PointerType>(LHS->getType()) || !isa<PointerType>(RHS->getType()) ||
5231 !isa<IntegerType>(I.getOperand(3)->getType()) ||
5232 !isa<IntegerType>(I.getType()))
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005233 return false;
5234
Chris Lattner8047d9a2009-12-24 00:37:38 +00005235 ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005236
Chris Lattner8047d9a2009-12-24 00:37:38 +00005237 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5238 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005239 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5240 bool ActuallyDoIt = true;
5241 MVT LoadVT;
5242 const Type *LoadTy;
5243 switch (Size->getZExtValue()) {
5244 default:
5245 LoadVT = MVT::Other;
5246 LoadTy = 0;
5247 ActuallyDoIt = false;
5248 break;
5249 case 2:
5250 LoadVT = MVT::i16;
5251 LoadTy = Type::getInt16Ty(Size->getContext());
5252 break;
5253 case 4:
5254 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005255 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005256 break;
5257 case 8:
5258 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005259 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005260 break;
5261 /*
5262 case 16:
5263 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005264 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005265 LoadTy = VectorType::get(LoadTy, 4);
5266 break;
5267 */
5268 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005269
Chris Lattner04b091a2009-12-24 01:07:17 +00005270 // This turns into unaligned loads. We only do this if the target natively
5271 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5272 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005273
Chris Lattner04b091a2009-12-24 01:07:17 +00005274 // Require that we can find a legal MVT, and only do this if the target
5275 // supports unaligned loads of that type. Expanding into byte loads would
5276 // bloat the code.
5277 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5278 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5279 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5280 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5281 ActuallyDoIt = false;
5282 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005283
Chris Lattner04b091a2009-12-24 01:07:17 +00005284 if (ActuallyDoIt) {
5285 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5286 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005287
Chris Lattner04b091a2009-12-24 01:07:17 +00005288 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
5289 ISD::SETNE);
5290 EVT CallVT = TLI.getValueType(I.getType(), true);
5291 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
5292 return true;
5293 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005294 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005295
5296
Chris Lattner8047d9a2009-12-24 00:37:38 +00005297 return false;
5298}
5299
5300
Dan Gohman2048b852009-11-23 18:04:58 +00005301void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005302 const char *RenameFn = 0;
5303 if (Function *F = I.getCalledFunction()) {
5304 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005305 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
5306 if (II) {
5307 if (unsigned IID = II->getIntrinsicID(F)) {
5308 RenameFn = visitIntrinsicCall(I, IID);
5309 if (!RenameFn)
5310 return;
5311 }
5312 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313 if (unsigned IID = F->getIntrinsicID()) {
5314 RenameFn = visitIntrinsicCall(I, IID);
5315 if (!RenameFn)
5316 return;
5317 }
5318 }
5319
5320 // Check for well-known libc/libm calls. If the function is internal, it
5321 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005322 if (!F->hasLocalLinkage() && F->hasName()) {
5323 StringRef Name = F->getName();
5324 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325 if (I.getNumOperands() == 3 && // Basic sanity checks.
5326 I.getOperand(1)->getType()->isFloatingPoint() &&
5327 I.getType() == I.getOperand(1)->getType() &&
5328 I.getType() == I.getOperand(2)->getType()) {
5329 SDValue LHS = getValue(I.getOperand(1));
5330 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00005331 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5332 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005333 return;
5334 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005335 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005336 if (I.getNumOperands() == 2 && // Basic sanity checks.
5337 I.getOperand(1)->getType()->isFloatingPoint() &&
5338 I.getType() == I.getOperand(1)->getType()) {
5339 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005340 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
5341 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005342 return;
5343 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005344 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005345 if (I.getNumOperands() == 2 && // Basic sanity checks.
5346 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005347 I.getType() == I.getOperand(1)->getType() &&
5348 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005349 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005350 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5351 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352 return;
5353 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005354 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005355 if (I.getNumOperands() == 2 && // Basic sanity checks.
5356 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005357 I.getType() == I.getOperand(1)->getType() &&
5358 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005359 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005360 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5361 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005362 return;
5363 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005364 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5365 if (I.getNumOperands() == 2 && // Basic sanity checks.
5366 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005367 I.getType() == I.getOperand(1)->getType() &&
5368 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005369 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005370 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5371 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005372 return;
5373 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005374 } else if (Name == "memcmp") {
5375 if (visitMemCmpCall(I))
5376 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005377 }
5378 }
5379 } else if (isa<InlineAsm>(I.getOperand(0))) {
5380 visitInlineAsm(&I);
5381 return;
5382 }
5383
5384 SDValue Callee;
5385 if (!RenameFn)
5386 Callee = getValue(I.getOperand(0));
5387 else
Bill Wendling056292f2008-09-16 21:48:12 +00005388 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005389
Bill Wendling0d580132009-12-23 01:28:19 +00005390 // Check if we can potentially perform a tail call. More detailed checking is
5391 // be done within LowerCallTo, after more information about the call is known.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005392 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5393
5394 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005395}
5396
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005397/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005398/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005399/// Chain/Flag as the input and updates them for the output Chain/Flag.
5400/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005401SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005402 unsigned Order, SDValue &Chain,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005403 SDValue *Flag) const {
5404 // Assemble the legal parts into the final values.
5405 SmallVector<SDValue, 4> Values(ValueVTs.size());
5406 SmallVector<SDValue, 8> Parts;
5407 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5408 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005409 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005410 unsigned NumRegs = 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
5413 Parts.resize(NumRegs);
5414 for (unsigned i = 0; i != NumRegs; ++i) {
5415 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00005416 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005417 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005418 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005419 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005420 *Flag = P.getValue(2);
5421 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005422
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005423 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005424
Bill Wendlingec72e322009-12-22 01:11:43 +00005425 if (DisableScheduling)
5426 DAG.AssignOrdering(P.getNode(), Order);
5427
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005428 // If the source register was virtual and if we know something about it,
5429 // add an assert node.
5430 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5431 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5432 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5433 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5434 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5435 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005436
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005437 unsigned RegSize = RegisterVT.getSizeInBits();
5438 unsigned NumSignBits = LOI.NumSignBits;
5439 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005441 // FIXME: We capture more information than the dag can represent. For
5442 // now, just use the tightest assertzext/assertsext possible.
5443 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005444 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005445 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005446 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005447 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005448 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005449 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005450 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005451 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005452 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005453 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005454 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005455 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005456 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005457 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005458 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005459 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005460 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005461
Owen Anderson825b72b2009-08-11 20:47:22 +00005462 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005463 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005464 RegisterVT, P, DAG.getValueType(FromVT));
5465
Bill Wendlingec72e322009-12-22 01:11:43 +00005466 if (DisableScheduling)
5467 DAG.AssignOrdering(P.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468 }
5469 }
5470 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005471
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005472 Parts[i] = P;
5473 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005474
Bill Wendling3ea3c242009-12-22 02:10:19 +00005475 Values[Value] = getCopyFromParts(DAG, dl, Order, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005476 NumRegs, RegisterVT, ValueVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005477 if (DisableScheduling)
5478 DAG.AssignOrdering(Values[Value].getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005479 Part += NumRegs;
5480 Parts.clear();
5481 }
5482
Bill Wendlingec72e322009-12-22 01:11:43 +00005483 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5484 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5485 &Values[0], ValueVTs.size());
5486 if (DisableScheduling)
5487 DAG.AssignOrdering(Res.getNode(), Order);
5488 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005489}
5490
5491/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005492/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005493/// Chain/Flag as the input and updates them for the output Chain/Flag.
5494/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005495void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005496 unsigned Order, SDValue &Chain,
5497 SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005498 // Get the list of the values's legal parts.
5499 unsigned NumRegs = Regs.size();
5500 SmallVector<SDValue, 8> Parts(NumRegs);
5501 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005502 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005503 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005504 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505
Bill Wendling3ea3c242009-12-22 02:10:19 +00005506 getCopyToParts(DAG, dl, Order,
5507 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005508 &Parts[Part], NumParts, RegisterVT);
5509 Part += NumParts;
5510 }
5511
5512 // Copy the parts into the registers.
5513 SmallVector<SDValue, 8> Chains(NumRegs);
5514 for (unsigned i = 0; i != NumRegs; ++i) {
5515 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00005516 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005517 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00005518 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005519 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005520 *Flag = Part.getValue(1);
5521 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005522
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005523 Chains[i] = Part.getValue(0);
Bill Wendlingec72e322009-12-22 01:11:43 +00005524
5525 if (DisableScheduling)
5526 DAG.AssignOrdering(Part.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005527 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005528
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005529 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005530 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005531 // flagged to it. That is the CopyToReg nodes and the user are considered
5532 // a single scheduling unit. If we create a TokenFactor and return it as
5533 // chain, then the TokenFactor is both a predecessor (operand) of the
5534 // user as well as a successor (the TF operands are flagged to the user).
5535 // c1, f1 = CopyToReg
5536 // c2, f2 = CopyToReg
5537 // c3 = TokenFactor c1, c2
5538 // ...
5539 // = op c3, ..., f2
5540 Chain = Chains[NumRegs-1];
5541 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005542 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Bill Wendlingec72e322009-12-22 01:11:43 +00005543
5544 if (DisableScheduling)
5545 DAG.AssignOrdering(Chain.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005546}
5547
5548/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005549/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005550/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005551void RegsForValue::AddInlineAsmOperands(unsigned Code,
5552 bool HasMatching,unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +00005553 SelectionDAG &DAG, unsigned Order,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005554 std::vector<SDValue> &Ops) const {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005555 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5556 unsigned Flag = Code | (Regs.size() << 3);
5557 if (HasMatching)
5558 Flag |= 0x80000000 | (MatchingIdx << 16);
Dale Johannesen99499332009-12-23 07:32:51 +00005559 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00005560 Ops.push_back(Res);
5561
5562 if (DisableScheduling)
5563 DAG.AssignOrdering(Res.getNode(), Order);
5564
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005565 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005566 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005567 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005568 for (unsigned i = 0; i != NumRegs; ++i) {
5569 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling651ad132009-12-22 01:25:10 +00005570 SDValue Res = DAG.getRegister(Regs[Reg++], RegisterVT);
5571 Ops.push_back(Res);
5572
5573 if (DisableScheduling)
5574 DAG.AssignOrdering(Res.getNode(), Order);
Chris Lattner58f15c42008-10-17 16:21:11 +00005575 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005576 }
5577}
5578
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005579/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005580/// i.e. it isn't a stack pointer or some other special register, return the
5581/// register class for the register. Otherwise, return null.
5582static const TargetRegisterClass *
5583isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5584 const TargetLowering &TLI,
5585 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005586 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005587 const TargetRegisterClass *FoundRC = 0;
5588 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5589 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005590 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005591
5592 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005593 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005594 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5595 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5596 I != E; ++I) {
5597 if (TLI.isTypeLegal(*I)) {
5598 // If we have already found this register in a different register class,
5599 // choose the one with the largest VT specified. For example, on
5600 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005601 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005602 ThisVT = *I;
5603 break;
5604 }
5605 }
5606 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005607
Owen Anderson825b72b2009-08-11 20:47:22 +00005608 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005609
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005610 // NOTE: This isn't ideal. In particular, this might allocate the
5611 // frame pointer in functions that need it (due to them not being taken
5612 // out of allocation, because a variable sized allocation hasn't been seen
5613 // yet). This is a slight code pessimization, but should still work.
5614 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5615 E = RC->allocation_order_end(MF); I != E; ++I)
5616 if (*I == Reg) {
5617 // We found a matching register class. Keep looking at others in case
5618 // we find one with larger registers that this physreg is also in.
5619 FoundRC = RC;
5620 FoundVT = ThisVT;
5621 break;
5622 }
5623 }
5624 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005625}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626
5627
5628namespace llvm {
5629/// AsmOperandInfo - This contains information for each constraint that we are
5630/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005631class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005632 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005633public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005634 /// CallOperand - If this is the result output operand or a clobber
5635 /// this is null, otherwise it is the incoming operand to the CallInst.
5636 /// This gets modified as the asm is processed.
5637 SDValue CallOperand;
5638
5639 /// AssignedRegs - If this is a register or register class operand, this
5640 /// contains the set of register corresponding to the operand.
5641 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005642
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005643 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5644 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5645 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005646
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005647 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5648 /// busy in OutputRegs/InputRegs.
5649 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005650 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005651 std::set<unsigned> &InputRegs,
5652 const TargetRegisterInfo &TRI) const {
5653 if (isOutReg) {
5654 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5655 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5656 }
5657 if (isInReg) {
5658 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5659 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5660 }
5661 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005662
Owen Andersone50ed302009-08-10 22:56:29 +00005663 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005664 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005665 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005666 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005667 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005668 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005669 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005670
Chris Lattner81249c92008-10-17 17:05:25 +00005671 if (isa<BasicBlock>(CallOperandVal))
5672 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005673
Chris Lattner81249c92008-10-17 17:05:25 +00005674 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005675
Chris Lattner81249c92008-10-17 17:05:25 +00005676 // If this is an indirect operand, the operand is a pointer to the
5677 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005678 if (isIndirect) {
5679 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5680 if (!PtrTy)
5681 llvm_report_error("Indirect operand for inline asm not a pointer!");
5682 OpTy = PtrTy->getElementType();
5683 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005684
Chris Lattner81249c92008-10-17 17:05:25 +00005685 // If OpTy is not a single value, it may be a struct/union that we
5686 // can tile with integers.
5687 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5688 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5689 switch (BitSize) {
5690 default: break;
5691 case 1:
5692 case 8:
5693 case 16:
5694 case 32:
5695 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005696 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005697 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005698 break;
5699 }
5700 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005701
Chris Lattner81249c92008-10-17 17:05:25 +00005702 return TLI.getValueType(OpTy, true);
5703 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005705private:
5706 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5707 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005708 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709 const TargetRegisterInfo &TRI) {
5710 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5711 Regs.insert(Reg);
5712 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5713 for (; *Aliases; ++Aliases)
5714 Regs.insert(*Aliases);
5715 }
5716};
5717} // end llvm namespace.
5718
5719
5720/// GetRegistersForValue - Assign registers (virtual or physical) for the
5721/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005722/// register allocator to handle the assignment process. However, if the asm
5723/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005724/// allocation. This produces generally horrible, but correct, code.
5725///
5726/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005727/// Input and OutputRegs are the set of already allocated physical registers.
5728///
Dan Gohman2048b852009-11-23 18:04:58 +00005729void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005730GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005731 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005732 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005733 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005734
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005735 // Compute whether this value requires an input register, an output register,
5736 // or both.
5737 bool isOutReg = false;
5738 bool isInReg = false;
5739 switch (OpInfo.Type) {
5740 case InlineAsm::isOutput:
5741 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005742
5743 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005744 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005745 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005746 break;
5747 case InlineAsm::isInput:
5748 isInReg = true;
5749 isOutReg = false;
5750 break;
5751 case InlineAsm::isClobber:
5752 isOutReg = true;
5753 isInReg = true;
5754 break;
5755 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005756
5757
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005758 MachineFunction &MF = DAG.getMachineFunction();
5759 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005760
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005761 // If this is a constraint for a single physreg, or a constraint for a
5762 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005763 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005764 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5765 OpInfo.ConstraintVT);
5766
5767 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005768 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005769 // If this is a FP input in an integer register (or visa versa) insert a bit
5770 // cast of the input value. More generally, handle any case where the input
5771 // value disagrees with the register class we plan to stick this in.
5772 if (OpInfo.Type == InlineAsm::isInput &&
5773 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005774 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005775 // types are identical size, use a bitcast to convert (e.g. two differing
5776 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005777 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005778 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005779 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005780 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005781 OpInfo.ConstraintVT = RegVT;
5782 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5783 // If the input is a FP value and we want it in FP registers, do a
5784 // bitcast to the corresponding integer type. This turns an f64 value
5785 // into i64, which can be passed with two i32 values on a 32-bit
5786 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005787 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005788 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005789 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005790 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005791 OpInfo.ConstraintVT = RegVT;
5792 }
Bill Wendling651ad132009-12-22 01:25:10 +00005793
5794 if (DisableScheduling)
5795 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Chris Lattner01426e12008-10-21 00:45:36 +00005796 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005797
Owen Anderson23b9b192009-08-12 00:36:31 +00005798 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005799 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005800
Owen Andersone50ed302009-08-10 22:56:29 +00005801 EVT RegVT;
5802 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005803
5804 // If this is a constraint for a specific physical register, like {r17},
5805 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005806 if (unsigned AssignedReg = PhysReg.first) {
5807 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005808 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005809 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005810
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005811 // Get the actual register value type. This is important, because the user
5812 // may have asked for (e.g.) the AX register in i32 type. We need to
5813 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005814 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005815
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005816 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005817 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005818
5819 // If this is an expanded reference, add the rest of the regs to Regs.
5820 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005821 TargetRegisterClass::iterator I = RC->begin();
5822 for (; *I != AssignedReg; ++I)
5823 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005824
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005825 // Already added the first reg.
5826 --NumRegs; ++I;
5827 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005828 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005829 Regs.push_back(*I);
5830 }
5831 }
Bill Wendling651ad132009-12-22 01:25:10 +00005832
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005833 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5834 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5835 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5836 return;
5837 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005838
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005839 // Otherwise, if this was a reference to an LLVM register class, create vregs
5840 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005841 if (const TargetRegisterClass *RC = PhysReg.second) {
5842 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005843 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005844 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005845
Evan Chengfb112882009-03-23 08:01:15 +00005846 // Create the appropriate number of virtual registers.
5847 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5848 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005849 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005850
Evan Chengfb112882009-03-23 08:01:15 +00005851 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5852 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005853 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005854
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005855 // This is a reference to a register class that doesn't directly correspond
5856 // to an LLVM register class. Allocate NumRegs consecutive, available,
5857 // registers from the class.
5858 std::vector<unsigned> RegClassRegs
5859 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5860 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005861
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005862 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5863 unsigned NumAllocated = 0;
5864 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5865 unsigned Reg = RegClassRegs[i];
5866 // See if this register is available.
5867 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5868 (isInReg && InputRegs.count(Reg))) { // Already used.
5869 // Make sure we find consecutive registers.
5870 NumAllocated = 0;
5871 continue;
5872 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005873
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005874 // Check to see if this register is allocatable (i.e. don't give out the
5875 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005876 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5877 if (!RC) { // Couldn't allocate this register.
5878 // Reset NumAllocated to make sure we return consecutive registers.
5879 NumAllocated = 0;
5880 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005881 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005882
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005883 // Okay, this register is good, we can use it.
5884 ++NumAllocated;
5885
5886 // If we allocated enough consecutive registers, succeed.
5887 if (NumAllocated == NumRegs) {
5888 unsigned RegStart = (i-NumAllocated)+1;
5889 unsigned RegEnd = i+1;
5890 // Mark all of the allocated registers used.
5891 for (unsigned i = RegStart; i != RegEnd; ++i)
5892 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005893
5894 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005895 OpInfo.ConstraintVT);
5896 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5897 return;
5898 }
5899 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005900
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005901 // Otherwise, we couldn't allocate enough registers for this.
5902}
5903
Evan Chengda43bcf2008-09-24 00:05:32 +00005904/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5905/// processed uses a memory 'm' constraint.
5906static bool
5907hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005908 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005909 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5910 InlineAsm::ConstraintInfo &CI = CInfos[i];
5911 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5912 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5913 if (CType == TargetLowering::C_Memory)
5914 return true;
5915 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005916
Chris Lattner6c147292009-04-30 00:48:50 +00005917 // Indirect operand accesses access memory.
5918 if (CI.isIndirect)
5919 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005920 }
5921
5922 return false;
5923}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005924
5925/// visitInlineAsm - Handle a call to an InlineAsm object.
5926///
Dan Gohman2048b852009-11-23 18:04:58 +00005927void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005928 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5929
5930 /// ConstraintOperands - Information about all of the constraints.
5931 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005932
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005933 std::set<unsigned> OutputRegs, InputRegs;
5934
5935 // Do a prepass over the constraints, canonicalizing them, and building up the
5936 // ConstraintOperands list.
5937 std::vector<InlineAsm::ConstraintInfo>
5938 ConstraintInfos = IA->ParseConstraints();
5939
Evan Chengda43bcf2008-09-24 00:05:32 +00005940 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005941
Chris Lattner6c147292009-04-30 00:48:50 +00005942 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005943
Chris Lattner6c147292009-04-30 00:48:50 +00005944 // We won't need to flush pending loads if this asm doesn't touch
5945 // memory and is nonvolatile.
5946 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005947 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005948 else
5949 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005950
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005951 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5952 unsigned ResNo = 0; // ResNo - The result number of the next output.
5953 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5954 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5955 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005956
Owen Anderson825b72b2009-08-11 20:47:22 +00005957 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005958
5959 // Compute the value type for each operand.
5960 switch (OpInfo.Type) {
5961 case InlineAsm::isOutput:
5962 // Indirect outputs just consume an argument.
5963 if (OpInfo.isIndirect) {
5964 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5965 break;
5966 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005967
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005968 // The return value of the call is this value. As such, there is no
5969 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005970 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005971 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005972 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5973 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5974 } else {
5975 assert(ResNo == 0 && "Asm only has one result!");
5976 OpVT = TLI.getValueType(CS.getType());
5977 }
5978 ++ResNo;
5979 break;
5980 case InlineAsm::isInput:
5981 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5982 break;
5983 case InlineAsm::isClobber:
5984 // Nothing to do.
5985 break;
5986 }
5987
5988 // If this is an input or an indirect output, process the call argument.
5989 // BasicBlocks are labels, currently appearing only in asm's.
5990 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005991 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005992 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5993
Chris Lattner81249c92008-10-17 17:05:25 +00005994 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005995 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005996 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005997 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005998 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005999
Owen Anderson1d0be152009-08-13 21:58:54 +00006000 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006001 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006004 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006005
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006006 // Second pass over the constraints: compute which constraint option to use
6007 // and assign registers to constraints that want a specific physreg.
6008 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
6009 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006010
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006011 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00006012 // matching input. If their types mismatch, e.g. one is an integer, the
6013 // other is floating point, or their sizes are different, flag it as an
6014 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006015 if (OpInfo.hasMatchingInput()) {
6016 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
6017 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00006018 if ((OpInfo.ConstraintVT.isInteger() !=
6019 Input.ConstraintVT.isInteger()) ||
6020 (OpInfo.ConstraintVT.getSizeInBits() !=
6021 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006022 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00006023 " with a matching output constraint of incompatible"
6024 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00006025 }
6026 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006027 }
6028 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006029
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006030 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00006031 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006032
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006033 // If this is a memory input, and if the operand is not indirect, do what we
6034 // need to to provide an address for the memory input.
6035 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6036 !OpInfo.isIndirect) {
6037 assert(OpInfo.Type == InlineAsm::isInput &&
6038 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006039
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006040 // Memory operands really want the address of the value. If we don't have
6041 // an indirect input, put it in the constpool if we can, otherwise spill
6042 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006043
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006044 // If the operand is a float, integer, or vector constant, spill to a
6045 // constant pool entry to get its address.
6046 Value *OpVal = OpInfo.CallOperandVal;
6047 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
6048 isa<ConstantVector>(OpVal)) {
6049 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
6050 TLI.getPointerTy());
6051 } else {
6052 // Otherwise, create a stack slot and emit a store to it before the
6053 // asm.
6054 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00006055 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006056 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
6057 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006058 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006059 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00006060 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006061 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006062 OpInfo.CallOperand = StackSlot;
6063 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006064
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006065 // There is no longer a Value* corresponding to this operand.
6066 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006068 // It is now an indirect operand.
6069 OpInfo.isIndirect = true;
6070 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006071
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006072 // If this constraint is for a specific register, allocate it before
6073 // anything else.
6074 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00006075 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006076 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006077
Bill Wendling651ad132009-12-22 01:25:10 +00006078 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006079
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006080 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006081 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006082 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6083 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006084
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006085 // C_Register operands have already been allocated, Other/Memory don't need
6086 // to be.
6087 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00006088 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006089 }
6090
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006091 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6092 std::vector<SDValue> AsmNodeOperands;
6093 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6094 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00006095 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
6096 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006097
6098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006099 // Loop over all of the inputs, copying the operand values into the
6100 // appropriate registers and processing the output regs.
6101 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006103 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6104 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006105
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006106 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6107 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6108
6109 switch (OpInfo.Type) {
6110 case InlineAsm::isOutput: {
6111 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6112 OpInfo.ConstraintType != TargetLowering::C_Register) {
6113 // Memory output, or 'other' output (e.g. 'X' constraint).
6114 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6115
6116 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006117 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6118 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006119 TLI.getPointerTy()));
6120 AsmNodeOperands.push_back(OpInfo.CallOperand);
6121 break;
6122 }
6123
6124 // Otherwise, this is a register or register class output.
6125
6126 // Copy the output from the appropriate register. Find a register that
6127 // we can use.
6128 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006129 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006130 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006131 }
6132
6133 // If this is an indirect operand, store through the pointer after the
6134 // asm.
6135 if (OpInfo.isIndirect) {
6136 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6137 OpInfo.CallOperandVal));
6138 } else {
6139 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00006140 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006141 // Concatenate this output onto the outputs list.
6142 RetValRegs.append(OpInfo.AssignedRegs);
6143 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145 // Add information to the INLINEASM node to know that this register is
6146 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00006147 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
6148 6 /* EARLYCLOBBER REGDEF */ :
6149 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00006150 false,
6151 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006152 DAG, SDNodeOrder,
6153 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006154 break;
6155 }
6156 case InlineAsm::isInput: {
6157 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006158
Chris Lattner6bdcda32008-10-17 16:47:46 +00006159 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006160 // If this is required to match an output register we have already set,
6161 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006162 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006163
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006164 // Scan until we find the definition we already emitted of this operand.
6165 // When we find it, create a RegsForValue operand.
6166 unsigned CurOp = 2; // The first operand.
6167 for (; OperandNo; --OperandNo) {
6168 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006169 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006170 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006171 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
6172 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
6173 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006174 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006175 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006176 }
6177
Evan Cheng697cbbf2009-03-20 18:03:34 +00006178 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006179 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006180 if ((OpFlag & 7) == 2 /*REGDEF*/
6181 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
6182 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00006183 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006184 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00006185 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00006186 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006187 RegsForValue MatchedRegs;
6188 MatchedRegs.TLI = &TLI;
6189 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00006190 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006191 MatchedRegs.RegVTs.push_back(RegVT);
6192 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006193 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00006194 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006195 MatchedRegs.Regs.push_back
6196 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006197
6198 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00006199 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006200 SDNodeOrder, Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00006201 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
6202 true, OpInfo.getMatchedOperand(),
Bill Wendling651ad132009-12-22 01:25:10 +00006203 DAG, SDNodeOrder, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006204 break;
6205 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006206 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
6207 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
6208 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006209 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00006210 // See InlineAsm.h isUseOperandTiedToDef.
6211 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00006212 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006213 TLI.getPointerTy()));
6214 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6215 break;
6216 }
6217 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006218
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006219 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006220 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006221 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006223 std::vector<SDValue> Ops;
6224 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00006225 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006226 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006227 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00006228 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006229 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006230
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006231 // Add information to the INLINEASM node to know about this input.
6232 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006233 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006234 TLI.getPointerTy()));
6235 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6236 break;
6237 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
6238 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
6239 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6240 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006241
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006242 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006243 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6244 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006245 TLI.getPointerTy()));
6246 AsmNodeOperands.push_back(InOperandVal);
6247 break;
6248 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006249
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006250 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6251 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6252 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006253 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006254 "Don't know how to handle indirect register inputs yet!");
6255
6256 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00006257 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006258 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006259 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00006260 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006261
Dale Johannesen66978ee2009-01-31 02:22:37 +00006262 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006263 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006264
Evan Cheng697cbbf2009-03-20 18:03:34 +00006265 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006266 DAG, SDNodeOrder,
6267 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006268 break;
6269 }
6270 case InlineAsm::isClobber: {
6271 // Add the clobbered value to the operand list, so that the register
6272 // allocator is aware that the physreg got clobbered.
6273 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00006274 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Bill Wendling651ad132009-12-22 01:25:10 +00006275 false, 0, DAG, SDNodeOrder,
6276 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006277 break;
6278 }
6279 }
6280 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006282 // Finish up input operands.
6283 AsmNodeOperands[0] = Chain;
6284 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006285
Dale Johannesen66978ee2009-01-31 02:22:37 +00006286 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006287 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006288 &AsmNodeOperands[0], AsmNodeOperands.size());
6289 Flag = Chain.getValue(1);
6290
6291 // If this asm returns a register value, copy the result from that register
6292 // and set it as the value of the call.
6293 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006294 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006295 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006296
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006297 // FIXME: Why don't we do this for inline asms with MRVs?
6298 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006299 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006300
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006301 // If any of the results of the inline asm is a vector, it may have the
6302 // wrong width/num elts. This can happen for register classes that can
6303 // contain multiple different value types. The preg or vreg allocated may
6304 // not have the same VT as was expected. Convert it to the right type
6305 // with bit_convert.
6306 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006307 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006308 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006309
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006310 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006311 ResultType.isInteger() && Val.getValueType().isInteger()) {
6312 // If a result value was tied to an input value, the computed result may
6313 // have a wider width than the expected result. Extract the relevant
6314 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006315 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006316 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006317
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006318 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006319 }
Dan Gohman95915732008-10-18 01:03:45 +00006320
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006321 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006322 // Don't need to use this as a chain in this case.
6323 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6324 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006325 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006326
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006327 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006328
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006329 // Process indirect outputs, first output all of the flagged copies out of
6330 // physregs.
6331 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6332 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
6333 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00006334 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006335 SDNodeOrder, Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006336 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00006337
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006338 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006339
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006340 // Emit the non-flagged stores from the physregs.
6341 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006342 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6343 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6344 StoresToEmit[i].first,
6345 getValue(StoresToEmit[i].second),
6346 StoresToEmit[i].second, 0);
6347 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006348 }
6349
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006350 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006351 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006352 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006353
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006354 DAG.setRoot(Chain);
6355}
6356
Dan Gohman2048b852009-11-23 18:04:58 +00006357void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006358 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6359 MVT::Other, getRoot(),
6360 getValue(I.getOperand(1)),
6361 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006362}
6363
Dan Gohman2048b852009-11-23 18:04:58 +00006364void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006365 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6366 getRoot(), getValue(I.getOperand(0)),
6367 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006368 setValue(&I, V);
6369 DAG.setRoot(V.getValue(1));
6370}
6371
Dan Gohman2048b852009-11-23 18:04:58 +00006372void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006373 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6374 MVT::Other, getRoot(),
6375 getValue(I.getOperand(1)),
6376 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006377}
6378
Dan Gohman2048b852009-11-23 18:04:58 +00006379void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006380 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6381 MVT::Other, getRoot(),
6382 getValue(I.getOperand(1)),
6383 getValue(I.getOperand(2)),
6384 DAG.getSrcValue(I.getOperand(1)),
6385 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006386}
6387
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006388/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006389/// implementation, which just calls LowerCall.
6390/// FIXME: When all targets are
6391/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006392std::pair<SDValue, SDValue>
6393TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6394 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006395 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006396 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006397 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006398 SDValue Callee,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006399 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl,
6400 unsigned Order) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00006401 assert((!isTailCall || PerformTailCallOpt) &&
6402 "isTailCall set when tail-call optimizations are disabled!");
6403
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006404 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006405 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006406 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006407 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006408 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6409 for (unsigned Value = 0, NumValues = ValueVTs.size();
6410 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006411 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006412 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006413 SDValue Op = SDValue(Args[i].Node.getNode(),
6414 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006415 ISD::ArgFlagsTy Flags;
6416 unsigned OriginalAlignment =
6417 getTargetData()->getABITypeAlignment(ArgTy);
6418
6419 if (Args[i].isZExt)
6420 Flags.setZExt();
6421 if (Args[i].isSExt)
6422 Flags.setSExt();
6423 if (Args[i].isInReg)
6424 Flags.setInReg();
6425 if (Args[i].isSRet)
6426 Flags.setSRet();
6427 if (Args[i].isByVal) {
6428 Flags.setByVal();
6429 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6430 const Type *ElementTy = Ty->getElementType();
6431 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006432 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006433 // For ByVal, alignment should come from FE. BE will guess if this
6434 // info is not there but there are cases it cannot get right.
6435 if (Args[i].Alignment)
6436 FrameAlign = Args[i].Alignment;
6437 Flags.setByValAlign(FrameAlign);
6438 Flags.setByValSize(FrameSize);
6439 }
6440 if (Args[i].isNest)
6441 Flags.setNest();
6442 Flags.setOrigAlign(OriginalAlignment);
6443
Owen Anderson23b9b192009-08-12 00:36:31 +00006444 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6445 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006446 SmallVector<SDValue, 4> Parts(NumParts);
6447 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6448
6449 if (Args[i].isSExt)
6450 ExtendKind = ISD::SIGN_EXTEND;
6451 else if (Args[i].isZExt)
6452 ExtendKind = ISD::ZERO_EXTEND;
6453
Bill Wendling3ea3c242009-12-22 02:10:19 +00006454 getCopyToParts(DAG, dl, Order, Op, &Parts[0], NumParts,
6455 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006456
Dan Gohman98ca4f22009-08-05 01:29:28 +00006457 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006458 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006459 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6460 if (NumParts > 1 && j == 0)
6461 MyFlags.Flags.setSplit();
6462 else if (j != 0)
6463 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006464
Dan Gohman98ca4f22009-08-05 01:29:28 +00006465 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006466 }
6467 }
6468 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006469
Dan Gohman98ca4f22009-08-05 01:29:28 +00006470 // Handle the incoming return values from the call.
6471 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006472 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006473 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006474 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006475 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006476 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6477 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006478 for (unsigned i = 0; i != NumRegs; ++i) {
6479 ISD::InputArg MyFlags;
6480 MyFlags.VT = RegisterVT;
6481 MyFlags.Used = isReturnValueUsed;
6482 if (RetSExt)
6483 MyFlags.Flags.setSExt();
6484 if (RetZExt)
6485 MyFlags.Flags.setZExt();
6486 if (isInreg)
6487 MyFlags.Flags.setInReg();
6488 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006489 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006490 }
6491
Dan Gohman98ca4f22009-08-05 01:29:28 +00006492 // Check if target-dependent constraints permit a tail call here.
6493 // Target-independent constraints should be checked by the caller.
6494 if (isTailCall &&
6495 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6496 isTailCall = false;
6497
6498 SmallVector<SDValue, 4> InVals;
6499 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6500 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006501
6502 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006503 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006504 "LowerCall didn't return a valid chain!");
6505 assert((!isTailCall || InVals.empty()) &&
6506 "LowerCall emitted a return value for a tail call!");
6507 assert((isTailCall || InVals.size() == Ins.size()) &&
6508 "LowerCall didn't emit the correct number of values!");
6509 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6510 assert(InVals[i].getNode() &&
6511 "LowerCall emitted a null value!");
6512 assert(Ins[i].VT == InVals[i].getValueType() &&
6513 "LowerCall emitted a value with the wrong type!");
6514 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006515
Bill Wendling3ea3c242009-12-22 02:10:19 +00006516 if (DisableScheduling)
6517 DAG.AssignOrdering(Chain.getNode(), Order);
6518
Dan Gohman98ca4f22009-08-05 01:29:28 +00006519 // For a tail call, the return value is merely live-out and there aren't
6520 // any nodes in the DAG representing it. Return a special value to
6521 // indicate that a tail call has been emitted and no more Instructions
6522 // should be processed in the current block.
6523 if (isTailCall) {
6524 DAG.setRoot(Chain);
6525 return std::make_pair(SDValue(), SDValue());
6526 }
6527
6528 // Collect the legal value parts into potentially illegal values
6529 // that correspond to the original function's return values.
6530 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6531 if (RetSExt)
6532 AssertOp = ISD::AssertSext;
6533 else if (RetZExt)
6534 AssertOp = ISD::AssertZext;
6535 SmallVector<SDValue, 4> ReturnValues;
6536 unsigned CurReg = 0;
6537 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006538 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006539 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6540 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006541
6542 SDValue ReturnValue =
Bill Wendling3ea3c242009-12-22 02:10:19 +00006543 getCopyFromParts(DAG, dl, Order, &InVals[CurReg], NumRegs,
6544 RegisterVT, VT, AssertOp);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006545 ReturnValues.push_back(ReturnValue);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006546 if (DisableScheduling)
6547 DAG.AssignOrdering(ReturnValue.getNode(), Order);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006548 CurReg += NumRegs;
6549 }
6550
6551 // For a function returning void, there is no return value. We can't create
6552 // such a node, so we just return a null return value in that case. In
6553 // that case, nothing will actualy look at the value.
6554 if (ReturnValues.empty())
6555 return std::make_pair(SDValue(), Chain);
6556
6557 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6558 DAG.getVTList(&RetTys[0], RetTys.size()),
6559 &ReturnValues[0], ReturnValues.size());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006560 if (DisableScheduling)
6561 DAG.AssignOrdering(Res.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006562 return std::make_pair(Res, Chain);
6563}
6564
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006565void TargetLowering::LowerOperationWrapper(SDNode *N,
6566 SmallVectorImpl<SDValue> &Results,
6567 SelectionDAG &DAG) {
6568 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006569 if (Res.getNode())
6570 Results.push_back(Res);
6571}
6572
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006573SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006574 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006575 return SDValue();
6576}
6577
Dan Gohman2048b852009-11-23 18:04:58 +00006578void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006579 SDValue Op = getValue(V);
6580 assert((Op.getOpcode() != ISD::CopyFromReg ||
6581 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6582 "Copy from a reg to the same reg!");
6583 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6584
Owen Anderson23b9b192009-08-12 00:36:31 +00006585 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006586 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +00006587 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006588 PendingExports.push_back(Chain);
6589}
6590
6591#include "llvm/CodeGen/SelectionDAGISel.h"
6592
Dan Gohman8c2b5252009-10-30 01:27:03 +00006593void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006594 // If this is the entry block, emit arguments.
6595 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006596 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006597 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006598 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006599 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006600 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006601
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006602 // Check whether the function can return without sret-demotion.
6603 SmallVector<EVT, 4> OutVTs;
6604 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006605 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006606 OutVTs, OutsFlags, TLI);
6607 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6608
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006609 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00006610 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006611 if (!FLI.CanLowerReturn) {
6612 // Put in an sret pointer parameter before all the other parameters.
6613 SmallVector<EVT, 1> ValueVTs;
6614 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6615
6616 // NOTE: Assuming that a pointer will never break down to more than one VT
6617 // or one register.
6618 ISD::ArgFlagsTy Flags;
6619 Flags.setSRet();
6620 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6621 ISD::InputArg RetArg(Flags, RegisterVT, true);
6622 Ins.push_back(RetArg);
6623 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006624
Dan Gohman98ca4f22009-08-05 01:29:28 +00006625 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006626 unsigned Idx = 1;
6627 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6628 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006629 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006630 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6631 bool isArgValueUsed = !I->use_empty();
6632 for (unsigned Value = 0, NumValues = ValueVTs.size();
6633 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006634 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006635 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006636 ISD::ArgFlagsTy Flags;
6637 unsigned OriginalAlignment =
6638 TD->getABITypeAlignment(ArgTy);
6639
6640 if (F.paramHasAttr(Idx, Attribute::ZExt))
6641 Flags.setZExt();
6642 if (F.paramHasAttr(Idx, Attribute::SExt))
6643 Flags.setSExt();
6644 if (F.paramHasAttr(Idx, Attribute::InReg))
6645 Flags.setInReg();
6646 if (F.paramHasAttr(Idx, Attribute::StructRet))
6647 Flags.setSRet();
6648 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6649 Flags.setByVal();
6650 const PointerType *Ty = cast<PointerType>(I->getType());
6651 const Type *ElementTy = Ty->getElementType();
6652 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6653 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6654 // For ByVal, alignment should be passed from FE. BE will guess if
6655 // this info is not there but there are cases it cannot get right.
6656 if (F.getParamAlignment(Idx))
6657 FrameAlign = F.getParamAlignment(Idx);
6658 Flags.setByValAlign(FrameAlign);
6659 Flags.setByValSize(FrameSize);
6660 }
6661 if (F.paramHasAttr(Idx, Attribute::Nest))
6662 Flags.setNest();
6663 Flags.setOrigAlign(OriginalAlignment);
6664
Owen Anderson23b9b192009-08-12 00:36:31 +00006665 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6666 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006667 for (unsigned i = 0; i != NumRegs; ++i) {
6668 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6669 if (NumRegs > 1 && i == 0)
6670 MyFlags.Flags.setSplit();
6671 // if it isn't first piece, alignment must be 1
6672 else if (i > 0)
6673 MyFlags.Flags.setOrigAlign(1);
6674 Ins.push_back(MyFlags);
6675 }
6676 }
6677 }
6678
6679 // Call the target to set up the argument values.
6680 SmallVector<SDValue, 8> InVals;
6681 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6682 F.isVarArg(), Ins,
6683 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006684
6685 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006686 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006687 "LowerFormalArguments didn't return a valid chain!");
6688 assert(InVals.size() == Ins.size() &&
6689 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006690 DEBUG({
6691 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6692 assert(InVals[i].getNode() &&
6693 "LowerFormalArguments emitted a null value!");
6694 assert(Ins[i].VT == InVals[i].getValueType() &&
6695 "LowerFormalArguments emitted a value with the wrong type!");
6696 }
6697 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006698
Dan Gohman5e866062009-08-06 15:37:27 +00006699 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006700 DAG.setRoot(NewRoot);
6701
6702 // Set up the argument values.
6703 unsigned i = 0;
6704 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006705 if (!FLI.CanLowerReturn) {
6706 // Create a virtual register for the sret pointer, and put in a copy
6707 // from the sret argument into it.
6708 SmallVector<EVT, 1> ValueVTs;
6709 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6710 EVT VT = ValueVTs[0];
6711 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6712 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling3ea58b62009-12-22 21:35:02 +00006713 SDValue ArgValue = getCopyFromParts(DAG, dl, 0, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006714 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006715
Dan Gohman2048b852009-11-23 18:04:58 +00006716 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006717 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6718 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6719 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006720 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6721 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006722 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006723
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006724 // i indexes lowered arguments. Bump it past the hidden sret argument.
6725 // Idx indexes LLVM arguments. Don't touch it.
6726 ++i;
6727 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006728
Dan Gohman98ca4f22009-08-05 01:29:28 +00006729 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6730 ++I, ++Idx) {
6731 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006732 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006733 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006734 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006735 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006736 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006737 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6738 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006739
6740 if (!I->use_empty()) {
6741 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6742 if (F.paramHasAttr(Idx, Attribute::SExt))
6743 AssertOp = ISD::AssertSext;
6744 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6745 AssertOp = ISD::AssertZext;
6746
Bill Wendling3ea58b62009-12-22 21:35:02 +00006747 ArgValues.push_back(getCopyFromParts(DAG, dl, 0, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006748 NumParts, PartVT, VT,
6749 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006750 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006751
Dan Gohman98ca4f22009-08-05 01:29:28 +00006752 i += NumParts;
6753 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006754
Dan Gohman98ca4f22009-08-05 01:29:28 +00006755 if (!I->use_empty()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +00006756 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6757 SDB->getCurDebugLoc());
6758 SDB->setValue(I, Res);
6759
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006760 // If this argument is live outside of the entry block, insert a copy from
6761 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006762 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006763 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006764 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006765
Dan Gohman98ca4f22009-08-05 01:29:28 +00006766 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006767
6768 // Finally, if the target has anything special to do, allow it to do so.
6769 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006770 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006771}
6772
6773/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6774/// ensure constants are generated when needed. Remember the virtual registers
6775/// that need to be added to the Machine PHI nodes as input. We cannot just
6776/// directly add them, because expansion might result in multiple MBB's for one
6777/// BB. As such, the start of the BB might correspond to a different MBB than
6778/// the end.
6779///
6780void
6781SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6782 TerminatorInst *TI = LLVMBB->getTerminator();
6783
6784 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6785
6786 // Check successor nodes' PHI nodes that expect a constant to be available
6787 // from this block.
6788 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6789 BasicBlock *SuccBB = TI->getSuccessor(succ);
6790 if (!isa<PHINode>(SuccBB->begin())) continue;
6791 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006792
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006793 // If this terminator has multiple identical successors (common for
6794 // switches), only handle each succ once.
6795 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006796
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006797 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6798 PHINode *PN;
6799
6800 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6801 // nodes and Machine PHI nodes, but the incoming operands have not been
6802 // emitted yet.
6803 for (BasicBlock::iterator I = SuccBB->begin();
6804 (PN = dyn_cast<PHINode>(I)); ++I) {
6805 // Ignore dead phi's.
6806 if (PN->use_empty()) continue;
6807
6808 unsigned Reg;
6809 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6810
6811 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006812 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006813 if (RegOut == 0) {
6814 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006815 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006816 }
6817 Reg = RegOut;
6818 } else {
6819 Reg = FuncInfo->ValueMap[PHIOp];
6820 if (Reg == 0) {
6821 assert(isa<AllocaInst>(PHIOp) &&
6822 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6823 "Didn't codegen value into a register!??");
6824 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006825 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006826 }
6827 }
6828
6829 // Remember that this register needs to added to the machine PHI node as
6830 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006831 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006832 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6833 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006834 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006835 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006836 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006837 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006838 Reg += NumRegisters;
6839 }
6840 }
6841 }
Dan Gohman2048b852009-11-23 18:04:58 +00006842 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006843}
6844
Dan Gohman3df24e62008-09-03 23:12:08 +00006845/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6846/// supports legal types, and it emits MachineInstrs directly instead of
6847/// creating SelectionDAG nodes.
6848///
6849bool
6850SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6851 FastISel *F) {
6852 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006853
Dan Gohman3df24e62008-09-03 23:12:08 +00006854 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006855 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006856
6857 // Check successor nodes' PHI nodes that expect a constant to be available
6858 // from this block.
6859 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6860 BasicBlock *SuccBB = TI->getSuccessor(succ);
6861 if (!isa<PHINode>(SuccBB->begin())) continue;
6862 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006863
Dan Gohman3df24e62008-09-03 23:12:08 +00006864 // If this terminator has multiple identical successors (common for
6865 // switches), only handle each succ once.
6866 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006867
Dan Gohman3df24e62008-09-03 23:12:08 +00006868 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6869 PHINode *PN;
6870
6871 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6872 // nodes and Machine PHI nodes, but the incoming operands have not been
6873 // emitted yet.
6874 for (BasicBlock::iterator I = SuccBB->begin();
6875 (PN = dyn_cast<PHINode>(I)); ++I) {
6876 // Ignore dead phi's.
6877 if (PN->use_empty()) continue;
6878
6879 // Only handle legal types. Two interesting things to note here. First,
6880 // by bailing out early, we may leave behind some dead instructions,
6881 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6882 // own moves. Second, this check is necessary becuase FastISel doesn't
6883 // use CreateRegForValue to create registers, so it always creates
6884 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006885 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006886 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6887 // Promote MVT::i1.
6888 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006889 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006890 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006891 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006892 return false;
6893 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006894 }
6895
6896 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6897
6898 unsigned Reg = F->getRegForValue(PHIOp);
6899 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006900 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006901 return false;
6902 }
Dan Gohman2048b852009-11-23 18:04:58 +00006903 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006904 }
6905 }
6906
6907 return true;
6908}