blob: ee2012ef57c5f298601819144fc2067147fcbaa7 [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 Uildriksb4997ae2009-11-07 02:11:54 +0000803/// Get the EVTs and ArgFlags collections that represent the return type
804/// of the given function. This does not require a DAG or a return value, and
805/// 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 Uildriksc158dde2009-11-11 19:59:24 +0000812 ComputeValueVTs(TLI, ReturnType, ValueVTs, Offsets);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000813 unsigned NumValues = ValueVTs.size();
814 if ( NumValues == 0 ) return;
815
816 for (unsigned j = 0, f = NumValues; j != f; ++j) {
817 EVT VT = ValueVTs[j];
818 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000819
820 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000821 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000822 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000823 ExtendKind = ISD::ZERO_EXTEND;
824
825 // FIXME: C calling convention requires the return type to be promoted to
826 // at least 32-bit. But this is not necessary for non-C calling
827 // conventions. The frontend should mark functions whose return values
828 // require promoting with signext or zeroext attributes.
829 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000830 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000831 if (VT.bitsLT(MinVT))
832 VT = MinVT;
833 }
834
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000835 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
836 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000837 // 'inreg' on function refers to return value
838 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000839 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000840 Flags.setInReg();
841
842 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000843 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000844 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000845 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000846 Flags.setZExt();
847
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000848 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000849 OutVTs.push_back(PartVT);
850 OutFlags.push_back(Flags);
851 }
852 }
853}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000854
Dan Gohman2048b852009-11-23 18:04:58 +0000855void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000856 SDValue Chain = getControlRoot();
857 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000858 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000859
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000860 if (!FLI.CanLowerReturn) {
861 unsigned DemoteReg = FLI.DemoteRegister;
862 const Function *F = I.getParent()->getParent();
863
864 // Emit a store of the return value through the virtual register.
865 // Leave Outs empty so that LowerReturn won't try to load return
866 // registers the usual way.
867 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000868 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000869 PtrValueVTs);
870
871 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
872 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000873
Owen Andersone50ed302009-08-10 22:56:29 +0000874 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000875 SmallVector<uint64_t, 4> Offsets;
876 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000877 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000878
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000879 SmallVector<SDValue, 4> Chains(NumValues);
880 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000881 for (unsigned i = 0; i != NumValues; ++i) {
882 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
883 DAG.getConstant(Offsets[i], PtrVT));
884 Chains[i] =
885 DAG.getStore(Chain, getCurDebugLoc(),
886 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
887 Add, NULL, Offsets[i], false, 0);
888
889 if (DisableScheduling) {
890 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
891 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
892 }
893 }
894
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000895 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
896 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000897
898 if (DisableScheduling)
899 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
900 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000901 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
902 SmallVector<EVT, 4> ValueVTs;
903 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
904 unsigned NumValues = ValueVTs.size();
905 if (NumValues == 0) continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000906
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000907 SDValue RetOp = getValue(I.getOperand(i));
908 for (unsigned j = 0, f = NumValues; j != f; ++j) {
909 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000910
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000911 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000912
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000913 const Function *F = I.getParent()->getParent();
914 if (F->paramHasAttr(0, Attribute::SExt))
915 ExtendKind = ISD::SIGN_EXTEND;
916 else if (F->paramHasAttr(0, Attribute::ZExt))
917 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000918
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000919 // FIXME: C calling convention requires the return type to be promoted
920 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000921 // conventions. The frontend should mark functions whose return values
922 // require promoting with signext or zeroext attributes.
923 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
924 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
925 if (VT.bitsLT(MinVT))
926 VT = MinVT;
927 }
928
929 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
930 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
931 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000932 getCopyToParts(DAG, getCurDebugLoc(), SDNodeOrder,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000933 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
934 &Parts[0], NumParts, PartVT, ExtendKind);
935
936 // 'inreg' on function refers to return value
937 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
938 if (F->paramHasAttr(0, Attribute::InReg))
939 Flags.setInReg();
940
941 // Propagate extension type if any
942 if (F->paramHasAttr(0, Attribute::SExt))
943 Flags.setSExt();
944 else if (F->paramHasAttr(0, Attribute::ZExt))
945 Flags.setZExt();
946
947 for (unsigned i = 0; i < NumParts; ++i)
948 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000949 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000950 }
951 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000952
953 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000954 CallingConv::ID CallConv =
955 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000956 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
957 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000958
959 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000960 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000961 "LowerReturn didn't return a valid chain!");
962
963 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000964 DAG.setRoot(Chain);
Bill Wendling87710f02009-12-21 23:47:40 +0000965
966 if (DisableScheduling)
967 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000968}
969
Dan Gohmanad62f532009-04-23 23:13:24 +0000970/// CopyToExportRegsIfNeeded - If the given value has virtual registers
971/// created for it, emit nodes to copy the value into the virtual
972/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000973void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000974 if (!V->use_empty()) {
975 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
976 if (VMI != FuncInfo.ValueMap.end())
977 CopyValueToVirtualRegister(V, VMI->second);
978 }
979}
980
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000981/// ExportFromCurrentBlock - If this condition isn't known to be exported from
982/// the current basic block, add it to ValueMap now so that we'll get a
983/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000984void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000985 // No need to export constants.
986 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000987
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000988 // Already exported?
989 if (FuncInfo.isExportedInst(V)) return;
990
991 unsigned Reg = FuncInfo.InitializeRegForValue(V);
992 CopyValueToVirtualRegister(V, Reg);
993}
994
Dan Gohman2048b852009-11-23 18:04:58 +0000995bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
996 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000997 // The operands of the setcc have to be in this block. We don't know
998 // how to export them from some other block.
999 if (Instruction *VI = dyn_cast<Instruction>(V)) {
1000 // Can export from current BB.
1001 if (VI->getParent() == FromBB)
1002 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001003
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001004 // Is already exported, noop.
1005 return FuncInfo.isExportedInst(V);
1006 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001007
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001008 // If this is an argument, we can export it if the BB is the entry block or
1009 // if it is already exported.
1010 if (isa<Argument>(V)) {
1011 if (FromBB == &FromBB->getParent()->getEntryBlock())
1012 return true;
1013
1014 // Otherwise, can only export this if it is already exported.
1015 return FuncInfo.isExportedInst(V);
1016 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001017
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001018 // Otherwise, constants can always be exported.
1019 return true;
1020}
1021
1022static bool InBlock(const Value *V, const BasicBlock *BB) {
1023 if (const Instruction *I = dyn_cast<Instruction>(V))
1024 return I->getParent() == BB;
1025 return true;
1026}
1027
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001028/// getFCmpCondCode - Return the ISD condition code corresponding to
1029/// the given LLVM IR floating-point condition code. This includes
1030/// consideration of global floating-point math flags.
1031///
1032static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1033 ISD::CondCode FPC, FOC;
1034 switch (Pred) {
1035 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1036 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1037 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1038 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1039 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1040 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1041 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1042 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1043 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1044 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1045 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1046 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1047 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1048 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1049 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1050 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1051 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001052 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001053 FOC = FPC = ISD::SETFALSE;
1054 break;
1055 }
1056 if (FiniteOnlyFPMath())
1057 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001058 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001059 return FPC;
1060}
1061
1062/// getICmpCondCode - Return the ISD condition code corresponding to
1063/// the given LLVM IR integer condition code.
1064///
1065static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1066 switch (Pred) {
1067 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1068 case ICmpInst::ICMP_NE: return ISD::SETNE;
1069 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1070 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1071 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1072 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1073 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1074 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1075 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1076 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1077 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001078 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001079 return ISD::SETNE;
1080 }
1081}
1082
Dan Gohmanc2277342008-10-17 21:16:08 +00001083/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1084/// This function emits a branch and is used at the leaves of an OR or an
1085/// AND operator tree.
1086///
1087void
Dan Gohman2048b852009-11-23 18:04:58 +00001088SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1089 MachineBasicBlock *TBB,
1090 MachineBasicBlock *FBB,
1091 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001092 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001093
Dan Gohmanc2277342008-10-17 21:16:08 +00001094 // If the leaf of the tree is a comparison, merge the condition into
1095 // the caseblock.
1096 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1097 // The operands of the cmp have to be in this block. We don't know
1098 // how to export them from some other block. If this is the first block
1099 // of the sequence, no exporting is needed.
1100 if (CurBB == CurMBB ||
1101 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1102 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001103 ISD::CondCode Condition;
1104 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001105 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001106 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001107 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001108 } else {
1109 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001110 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001111 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001112
1113 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001114 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1115 SwitchCases.push_back(CB);
1116 return;
1117 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001118 }
1119
1120 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001121 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001122 NULL, TBB, FBB, CurBB);
1123 SwitchCases.push_back(CB);
1124}
1125
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001126/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001127void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1128 MachineBasicBlock *TBB,
1129 MachineBasicBlock *FBB,
1130 MachineBasicBlock *CurBB,
1131 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001132 // If this node is not part of the or/and tree, emit it as a branch.
1133 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001134 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001135 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1136 BOp->getParent() != CurBB->getBasicBlock() ||
1137 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1138 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1139 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001140 return;
1141 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001143 // Create TmpBB after CurBB.
1144 MachineFunction::iterator BBI = CurBB;
1145 MachineFunction &MF = DAG.getMachineFunction();
1146 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1147 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001148
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001149 if (Opc == Instruction::Or) {
1150 // Codegen X | Y as:
1151 // jmp_if_X TBB
1152 // jmp TmpBB
1153 // TmpBB:
1154 // jmp_if_Y TBB
1155 // jmp FBB
1156 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 // Emit the LHS condition.
1159 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001160
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001161 // Emit the RHS condition into TmpBB.
1162 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1163 } else {
1164 assert(Opc == Instruction::And && "Unknown merge op!");
1165 // Codegen X & Y as:
1166 // jmp_if_X TmpBB
1167 // jmp FBB
1168 // TmpBB:
1169 // jmp_if_Y TBB
1170 // jmp FBB
1171 //
1172 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001173
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001174 // Emit the LHS condition.
1175 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001176
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001177 // Emit the RHS condition into TmpBB.
1178 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1179 }
1180}
1181
1182/// If the set of cases should be emitted as a series of branches, return true.
1183/// If we should emit this as a bunch of and/or'd together conditions, return
1184/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001185bool
Dan Gohman2048b852009-11-23 18:04:58 +00001186SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001187 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001188
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001189 // If this is two comparisons of the same values or'd or and'd together, they
1190 // will get folded into a single comparison, so don't emit two blocks.
1191 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1192 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1193 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1194 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1195 return false;
1196 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001197
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001198 return true;
1199}
1200
Dan Gohman2048b852009-11-23 18:04:58 +00001201void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001202 // Update machine-CFG edges.
1203 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1204
1205 // Figure out which block is immediately after the current one.
1206 MachineBasicBlock *NextBlock = 0;
1207 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001208 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001209 NextBlock = BBI;
1210
1211 if (I.isUnconditional()) {
1212 // Update machine-CFG edges.
1213 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001214
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001215 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001216 if (Succ0MBB != NextBlock) {
1217 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001218 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001219 DAG.getBasicBlock(Succ0MBB));
1220 DAG.setRoot(V);
1221
1222 if (DisableScheduling)
1223 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1224 }
1225
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001226 return;
1227 }
1228
1229 // If this condition is one of the special cases we handle, do special stuff
1230 // now.
1231 Value *CondVal = I.getCondition();
1232 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1233
1234 // If this is a series of conditions that are or'd or and'd together, emit
1235 // this as a sequence of branches instead of setcc's with and/or operations.
1236 // For example, instead of something like:
1237 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001238 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001239 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001240 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001241 // or C, F
1242 // jnz foo
1243 // Emit:
1244 // cmp A, B
1245 // je foo
1246 // cmp D, E
1247 // jle foo
1248 //
1249 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001250 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001251 (BOp->getOpcode() == Instruction::And ||
1252 BOp->getOpcode() == Instruction::Or)) {
1253 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1254 // If the compares in later blocks need to use values not currently
1255 // exported from this block, export them now. This block should always
1256 // be the first entry.
1257 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001259 // Allow some cases to be rejected.
1260 if (ShouldEmitAsBranches(SwitchCases)) {
1261 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1262 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1263 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1264 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001266 // Emit the branch for this block.
1267 visitSwitchCase(SwitchCases[0]);
1268 SwitchCases.erase(SwitchCases.begin());
1269 return;
1270 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001271
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001272 // Okay, we decided not to do this, remove any inserted MBB's and clear
1273 // SwitchCases.
1274 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001275 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001276
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001277 SwitchCases.clear();
1278 }
1279 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001280
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001281 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001282 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001283 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001284
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001285 // Use visitSwitchCase to actually insert the fast branch sequence for this
1286 // cond branch.
1287 visitSwitchCase(CB);
1288}
1289
1290/// visitSwitchCase - Emits the necessary code to represent a single node in
1291/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001292void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001293 SDValue Cond;
1294 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001295 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001296
1297 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001298 if (CB.CmpMHS == NULL) {
1299 // Fold "(X == true)" to X and "(X == false)" to !X to
1300 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001301 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001302 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001303 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001304 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001305 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001306 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001307 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001308 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001309 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001310 } else {
1311 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1312
Anton Korobeynikov23218582008-12-23 22:25:27 +00001313 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1314 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315
1316 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001317 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001318
1319 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001320 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001321 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001322 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001323 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001324 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001325 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001326 DAG.getConstant(High-Low, VT), ISD::SETULE);
1327 }
1328 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001329
Bill Wendling87710f02009-12-21 23:47:40 +00001330 if (DisableScheduling)
1331 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1332
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001333 // Update successor info
1334 CurMBB->addSuccessor(CB.TrueBB);
1335 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001336
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001337 // Set NextBlock to be the MBB immediately after the current one, if any.
1338 // This is used to avoid emitting unnecessary branches to the next block.
1339 MachineBasicBlock *NextBlock = 0;
1340 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001341 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001342 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001343
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001344 // If the lhs block is the next block, invert the condition so that we can
1345 // fall through to the lhs instead of the rhs block.
1346 if (CB.TrueBB == NextBlock) {
1347 std::swap(CB.TrueBB, CB.FalseBB);
1348 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001349 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001350
1351 if (DisableScheduling)
1352 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001353 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001354
Dale Johannesenf5d97892009-02-04 01:48:28 +00001355 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001356 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001357 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001358
Bill Wendling87710f02009-12-21 23:47:40 +00001359 if (DisableScheduling)
1360 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1361
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001362 // If the branch was constant folded, fix up the CFG.
1363 if (BrCond.getOpcode() == ISD::BR) {
1364 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 } else {
1366 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001367 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001368 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001369
Bill Wendling87710f02009-12-21 23:47:40 +00001370 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001371 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1372 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001373
1374 if (DisableScheduling)
1375 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1376 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001378
1379 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001380}
1381
1382/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001383void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001384 // Emit the code for the jump table
1385 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001386 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001387 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1388 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001389 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001390 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1391 MVT::Other, Index.getValue(1),
1392 Table, Index);
1393 DAG.setRoot(BrJumpTable);
1394
Bill Wendling87710f02009-12-21 23:47:40 +00001395 if (DisableScheduling) {
1396 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1397 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001398 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001399 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001400}
1401
1402/// visitJumpTableHeader - This function emits necessary code to produce index
1403/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001404void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1405 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001406 // Subtract the lowest switch case value from the value being switched on and
1407 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001408 // difference between smallest and largest cases.
1409 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001410 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001411 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001412 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001413
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001414 // The SDNode we just created, which holds the value being switched on minus
1415 // the the smallest case value, needs to be copied to a virtual register so it
1416 // can be used as an index into the jump table in a subsequent basic block.
1417 // This value may be smaller or larger than the target's pointer type, and
1418 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001419 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001422 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1423 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001424 JT.Reg = JumpTableReg;
1425
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001426 // Emit the range check for the jump table, and branch to the default block
1427 // for the switch statement if the value being switched on exceeds the largest
1428 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001429 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001430 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001431 DAG.getConstant(JTH.Last-JTH.First,VT),
1432 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001433
Bill Wendling87710f02009-12-21 23:47:40 +00001434 if (DisableScheduling) {
1435 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1436 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1437 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1438 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1439 }
1440
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001441 // Set NextBlock to be the MBB immediately after the current one, if any.
1442 // This is used to avoid emitting unnecessary branches to the next block.
1443 MachineBasicBlock *NextBlock = 0;
1444 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001445
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001446 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001447 NextBlock = BBI;
1448
Dale Johannesen66978ee2009-01-31 02:22:37 +00001449 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001450 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001451 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001452
Bill Wendling87710f02009-12-21 23:47:40 +00001453 if (DisableScheduling)
1454 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1455
1456 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001457 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1458 DAG.getBasicBlock(JT.MBB));
1459
Bill Wendling87710f02009-12-21 23:47:40 +00001460 if (DisableScheduling)
1461 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1462 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001463
Bill Wendling87710f02009-12-21 23:47:40 +00001464 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001465}
1466
1467/// visitBitTestHeader - This function emits necessary code to produce value
1468/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001469void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470 // Subtract the minimum value
1471 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001472 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001473 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001474 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001475
1476 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001477 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001478 TLI.getSetCCResultType(Sub.getValueType()),
1479 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001480 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001481
Bill Wendling87710f02009-12-21 23:47:40 +00001482 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1483 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001484
Duncan Sands92abc622009-01-31 15:50:11 +00001485 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001486 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1487 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001488
Bill Wendling87710f02009-12-21 23:47:40 +00001489 if (DisableScheduling) {
1490 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1491 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1492 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1493 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1494 }
1495
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001496 // Set NextBlock to be the MBB immediately after the current one, if any.
1497 // This is used to avoid emitting unnecessary branches to the next block.
1498 MachineBasicBlock *NextBlock = 0;
1499 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001500 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001501 NextBlock = BBI;
1502
1503 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1504
1505 CurMBB->addSuccessor(B.Default);
1506 CurMBB->addSuccessor(MBB);
1507
Dale Johannesen66978ee2009-01-31 02:22:37 +00001508 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001509 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001510 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001511
Bill Wendling87710f02009-12-21 23:47:40 +00001512 if (DisableScheduling)
1513 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1514
1515 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001516 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1517 DAG.getBasicBlock(MBB));
1518
Bill Wendling87710f02009-12-21 23:47:40 +00001519 if (DisableScheduling)
1520 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1521 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001522
Bill Wendling87710f02009-12-21 23:47:40 +00001523 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001524}
1525
1526/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001527void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1528 unsigned Reg,
1529 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001530 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001531 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001532 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001533 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001534 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001535 DAG.getConstant(1, TLI.getPointerTy()),
1536 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001537
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001538 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001539 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001540 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001541 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001542 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1543 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001544 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001545 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546
Bill Wendling87710f02009-12-21 23:47:40 +00001547 if (DisableScheduling) {
1548 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1549 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1550 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1551 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1552 }
1553
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001554 CurMBB->addSuccessor(B.TargetBB);
1555 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001556
Dale Johannesen66978ee2009-01-31 02:22:37 +00001557 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001558 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001559 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001560
Bill Wendling87710f02009-12-21 23:47:40 +00001561 if (DisableScheduling)
1562 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1563
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001564 // Set NextBlock to be the MBB immediately after the current one, if any.
1565 // This is used to avoid emitting unnecessary branches to the next block.
1566 MachineBasicBlock *NextBlock = 0;
1567 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001568 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001569 NextBlock = BBI;
1570
Bill Wendling87710f02009-12-21 23:47:40 +00001571 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001572 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1573 DAG.getBasicBlock(NextMBB));
1574
Bill Wendling87710f02009-12-21 23:47:40 +00001575 if (DisableScheduling)
1576 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1577 }
Bill Wendling0777e922009-12-21 21:59:52 +00001578
Bill Wendling87710f02009-12-21 23:47:40 +00001579 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580}
1581
Dan Gohman2048b852009-11-23 18:04:58 +00001582void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001583 // Retrieve successors.
1584 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1585 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1586
Gabor Greifb67e6b32009-01-15 11:10:44 +00001587 const Value *Callee(I.getCalledValue());
1588 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001589 visitInlineAsm(&I);
1590 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001591 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001592
1593 // If the value of the invoke is used outside of its defining block, make it
1594 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001595 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001596
1597 // Update successor info
1598 CurMBB->addSuccessor(Return);
1599 CurMBB->addSuccessor(LandingPad);
1600
1601 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001602 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1603 MVT::Other, getControlRoot(),
1604 DAG.getBasicBlock(Return));
1605 DAG.setRoot(Branch);
1606
1607 if (DisableScheduling)
1608 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001609}
1610
Dan Gohman2048b852009-11-23 18:04:58 +00001611void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001612}
1613
1614/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1615/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001616bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1617 CaseRecVector& WorkList,
1618 Value* SV,
1619 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001620 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001621
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001622 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001623 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001625 return false;
1626
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001627 // Get the MachineFunction which holds the current MBB. This is used when
1628 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001629 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001630
1631 // Figure out which block is immediately after the current one.
1632 MachineBasicBlock *NextBlock = 0;
1633 MachineFunction::iterator BBI = CR.CaseBB;
1634
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001635 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001636 NextBlock = BBI;
1637
1638 // TODO: If any two of the cases has the same destination, and if one value
1639 // is the same as the other, but has one bit unset that the other has set,
1640 // use bit manipulation to do two compares at once. For example:
1641 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001642
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001643 // Rearrange the case blocks so that the last one falls through if possible.
1644 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1645 // The last case block won't fall through into 'NextBlock' if we emit the
1646 // branches in this order. See if rearranging a case value would help.
1647 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1648 if (I->BB == NextBlock) {
1649 std::swap(*I, BackCase);
1650 break;
1651 }
1652 }
1653 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001655 // Create a CaseBlock record representing a conditional branch to
1656 // the Case's target mbb if the value being switched on SV is equal
1657 // to C.
1658 MachineBasicBlock *CurBlock = CR.CaseBB;
1659 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1660 MachineBasicBlock *FallThrough;
1661 if (I != E-1) {
1662 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1663 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001664
1665 // Put SV in a virtual register to make it available from the new blocks.
1666 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001667 } else {
1668 // If the last case doesn't match, go to the default block.
1669 FallThrough = Default;
1670 }
1671
1672 Value *RHS, *LHS, *MHS;
1673 ISD::CondCode CC;
1674 if (I->High == I->Low) {
1675 // This is just small small case range :) containing exactly 1 case
1676 CC = ISD::SETEQ;
1677 LHS = SV; RHS = I->High; MHS = NULL;
1678 } else {
1679 CC = ISD::SETLE;
1680 LHS = I->Low; MHS = SV; RHS = I->High;
1681 }
1682 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001683
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001684 // If emitting the first comparison, just call visitSwitchCase to emit the
1685 // code into the current block. Otherwise, push the CaseBlock onto the
1686 // vector to be later processed by SDISel, and insert the node's MBB
1687 // before the next MBB.
1688 if (CurBlock == CurMBB)
1689 visitSwitchCase(CB);
1690 else
1691 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001692
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001693 CurBlock = FallThrough;
1694 }
1695
1696 return true;
1697}
1698
1699static inline bool areJTsAllowed(const TargetLowering &TLI) {
1700 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001701 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1702 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001703}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001704
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001705static APInt ComputeRange(const APInt &First, const APInt &Last) {
1706 APInt LastExt(Last), FirstExt(First);
1707 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1708 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1709 return (LastExt - FirstExt + 1ULL);
1710}
1711
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001712/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001713bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1714 CaseRecVector& WorkList,
1715 Value* SV,
1716 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001717 Case& FrontCase = *CR.Range.first;
1718 Case& BackCase = *(CR.Range.second-1);
1719
Chris Lattnere880efe2009-11-07 07:50:34 +00001720 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1721 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001722
Chris Lattnere880efe2009-11-07 07:50:34 +00001723 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001724 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1725 I!=E; ++I)
1726 TSize += I->size();
1727
Chris Lattnere880efe2009-11-07 07:50:34 +00001728 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001729 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001730
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001731 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001732 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001733 if (Density < 0.4)
1734 return false;
1735
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001736 DEBUG(errs() << "Lowering jump table\n"
1737 << "First entry: " << First << ". Last entry: " << Last << '\n'
1738 << "Range: " << Range
1739 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001740
1741 // Get the MachineFunction which holds the current MBB. This is used when
1742 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001743 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744
1745 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001746 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001747 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748
1749 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1750
1751 // Create a new basic block to hold the code for loading the address
1752 // of the jump table, and jumping to it. Update successor information;
1753 // we will either branch to the default case for the switch, or the jump
1754 // table.
1755 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1756 CurMF->insert(BBI, JumpTableBB);
1757 CR.CaseBB->addSuccessor(Default);
1758 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001759
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001760 // Build a vector of destination BBs, corresponding to each target
1761 // of the jump table. If the value of the jump table slot corresponds to
1762 // a case statement, push the case's BB onto the vector, otherwise, push
1763 // the default BB.
1764 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001765 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001766 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001767 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1768 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1769
1770 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001771 DestBBs.push_back(I->BB);
1772 if (TEI==High)
1773 ++I;
1774 } else {
1775 DestBBs.push_back(Default);
1776 }
1777 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001778
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001779 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001780 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1781 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001782 E = DestBBs.end(); I != E; ++I) {
1783 if (!SuccsHandled[(*I)->getNumber()]) {
1784 SuccsHandled[(*I)->getNumber()] = true;
1785 JumpTableBB->addSuccessor(*I);
1786 }
1787 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001788
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001789 // Create a jump table index for this jump table, or return an existing
1790 // one.
1791 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001792
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001793 // Set the jump table information so that we can codegen it as a second
1794 // MachineBasicBlock
1795 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1796 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1797 if (CR.CaseBB == CurMBB)
1798 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001799
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001800 JTCases.push_back(JumpTableBlock(JTH, JT));
1801
1802 return true;
1803}
1804
1805/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1806/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001807bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1808 CaseRecVector& WorkList,
1809 Value* SV,
1810 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001811 // Get the MachineFunction which holds the current MBB. This is used when
1812 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001813 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001814
1815 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001816 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001817 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001818
1819 Case& FrontCase = *CR.Range.first;
1820 Case& BackCase = *(CR.Range.second-1);
1821 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1822
1823 // Size is the number of Cases represented by this range.
1824 unsigned Size = CR.Range.second - CR.Range.first;
1825
Chris Lattnere880efe2009-11-07 07:50:34 +00001826 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1827 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001828 double FMetric = 0;
1829 CaseItr Pivot = CR.Range.first + Size/2;
1830
1831 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1832 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001833 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001834 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1835 I!=E; ++I)
1836 TSize += I->size();
1837
Chris Lattnere880efe2009-11-07 07:50:34 +00001838 APInt LSize = FrontCase.size();
1839 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001840 DEBUG(errs() << "Selecting best pivot: \n"
1841 << "First: " << First << ", Last: " << Last <<'\n'
1842 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001843 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1844 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001845 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1846 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001847 APInt Range = ComputeRange(LEnd, RBegin);
1848 assert((Range - 2ULL).isNonNegative() &&
1849 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001850 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001851 (LEnd - First + 1ULL).roundToDouble();
1852 double RDensity = (double)RSize.roundToDouble() /
1853 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001854 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001856 DEBUG(errs() <<"=>Step\n"
1857 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1858 << "LDensity: " << LDensity
1859 << ", RDensity: " << RDensity << '\n'
1860 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001861 if (FMetric < Metric) {
1862 Pivot = J;
1863 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001864 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001865 }
1866
1867 LSize += J->size();
1868 RSize -= J->size();
1869 }
1870 if (areJTsAllowed(TLI)) {
1871 // If our case is dense we *really* should handle it earlier!
1872 assert((FMetric > 0) && "Should handle dense range earlier!");
1873 } else {
1874 Pivot = CR.Range.first + Size/2;
1875 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001877 CaseRange LHSR(CR.Range.first, Pivot);
1878 CaseRange RHSR(Pivot, CR.Range.second);
1879 Constant *C = Pivot->Low;
1880 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001881
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001882 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001883 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001884 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001885 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001886 // Pivot's Value, then we can branch directly to the LHS's Target,
1887 // rather than creating a leaf node for it.
1888 if ((LHSR.second - LHSR.first) == 1 &&
1889 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001890 cast<ConstantInt>(C)->getValue() ==
1891 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001892 TrueBB = LHSR.first->BB;
1893 } else {
1894 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1895 CurMF->insert(BBI, TrueBB);
1896 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001897
1898 // Put SV in a virtual register to make it available from the new blocks.
1899 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001900 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001901
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001902 // Similar to the optimization above, if the Value being switched on is
1903 // known to be less than the Constant CR.LT, and the current Case Value
1904 // is CR.LT - 1, then we can branch directly to the target block for
1905 // the current Case Value, rather than emitting a RHS leaf node for it.
1906 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001907 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1908 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001909 FalseBB = RHSR.first->BB;
1910 } else {
1911 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1912 CurMF->insert(BBI, FalseBB);
1913 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001914
1915 // Put SV in a virtual register to make it available from the new blocks.
1916 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001917 }
1918
1919 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001920 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 // Otherwise, branch to LHS.
1922 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1923
1924 if (CR.CaseBB == CurMBB)
1925 visitSwitchCase(CB);
1926 else
1927 SwitchCases.push_back(CB);
1928
1929 return true;
1930}
1931
1932/// handleBitTestsSwitchCase - if current case range has few destination and
1933/// range span less, than machine word bitwidth, encode case range into series
1934/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001935bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1936 CaseRecVector& WorkList,
1937 Value* SV,
1938 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001939 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001940 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001941
1942 Case& FrontCase = *CR.Range.first;
1943 Case& BackCase = *(CR.Range.second-1);
1944
1945 // Get the MachineFunction which holds the current MBB. This is used when
1946 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001947 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001948
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001949 // If target does not have legal shift left, do not emit bit tests at all.
1950 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1951 return false;
1952
Anton Korobeynikov23218582008-12-23 22:25:27 +00001953 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001954 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1955 I!=E; ++I) {
1956 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001957 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001958 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001959
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001960 // Count unique destinations
1961 SmallSet<MachineBasicBlock*, 4> Dests;
1962 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1963 Dests.insert(I->BB);
1964 if (Dests.size() > 3)
1965 // Don't bother the code below, if there are too much unique destinations
1966 return false;
1967 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001968 DEBUG(errs() << "Total number of unique destinations: "
1969 << Dests.size() << '\n'
1970 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001971
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001972 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001973 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1974 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001975 APInt cmpRange = maxValue - minValue;
1976
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001977 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1978 << "Low bound: " << minValue << '\n'
1979 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001980
1981 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001982 (!(Dests.size() == 1 && numCmps >= 3) &&
1983 !(Dests.size() == 2 && numCmps >= 5) &&
1984 !(Dests.size() >= 3 && numCmps >= 6)))
1985 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001986
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001987 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001988 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001990 // Optimize the case where all the case values fit in a
1991 // word without having to subtract minValue. In this case,
1992 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001993 if (minValue.isNonNegative() &&
1994 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1995 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001996 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001997 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001998 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001999
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002000 CaseBitsVector CasesBits;
2001 unsigned i, count = 0;
2002
2003 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2004 MachineBasicBlock* Dest = I->BB;
2005 for (i = 0; i < count; ++i)
2006 if (Dest == CasesBits[i].BB)
2007 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002008
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002009 if (i == count) {
2010 assert((count < 3) && "Too much destinations to test!");
2011 CasesBits.push_back(CaseBits(0, Dest, 0));
2012 count++;
2013 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002014
2015 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2016 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2017
2018 uint64_t lo = (lowValue - lowBound).getZExtValue();
2019 uint64_t hi = (highValue - lowBound).getZExtValue();
2020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 for (uint64_t j = lo; j <= hi; j++) {
2022 CasesBits[i].Mask |= 1ULL << j;
2023 CasesBits[i].Bits++;
2024 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002025
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002026 }
2027 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002028
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002029 BitTestInfo BTC;
2030
2031 // Figure out which block is immediately after the current one.
2032 MachineFunction::iterator BBI = CR.CaseBB;
2033 ++BBI;
2034
2035 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2036
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002037 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002038 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002039 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
2040 << ", Bits: " << CasesBits[i].Bits
2041 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002042
2043 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2044 CurMF->insert(BBI, CaseBB);
2045 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2046 CaseBB,
2047 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002048
2049 // Put SV in a virtual register to make it available from the new blocks.
2050 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002051 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002052
2053 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002054 -1U, (CR.CaseBB == CurMBB),
2055 CR.CaseBB, Default, BTC);
2056
2057 if (CR.CaseBB == CurMBB)
2058 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002059
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002060 BitTestCases.push_back(BTB);
2061
2062 return true;
2063}
2064
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002065/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002066size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2067 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002068 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002069
2070 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002071 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2073 Cases.push_back(Case(SI.getSuccessorValue(i),
2074 SI.getSuccessorValue(i),
2075 SMBB));
2076 }
2077 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2078
2079 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002080 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081 // Must recompute end() each iteration because it may be
2082 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002083 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2084 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2085 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002086 MachineBasicBlock* nextBB = J->BB;
2087 MachineBasicBlock* currentBB = I->BB;
2088
2089 // If the two neighboring cases go to the same destination, merge them
2090 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002091 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002092 I->High = J->High;
2093 J = Cases.erase(J);
2094 } else {
2095 I = J++;
2096 }
2097 }
2098
2099 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2100 if (I->Low != I->High)
2101 // A range counts double, since it requires two compares.
2102 ++numCmps;
2103 }
2104
2105 return numCmps;
2106}
2107
Dan Gohman2048b852009-11-23 18:04:58 +00002108void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002109 // Figure out which block is immediately after the current one.
2110 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002111 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2112
2113 // If there is only the default destination, branch to it if it is not the
2114 // next basic block. Otherwise, just fall through.
2115 if (SI.getNumOperands() == 2) {
2116 // Update machine-CFG edges.
2117
2118 // If this is not a fall-through branch, emit the branch.
2119 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002120 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002121 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002122 MVT::Other, getControlRoot(),
2123 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002124 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002125
2126 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002127 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002128 }
2129
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002130 return;
2131 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002132
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002133 // If there are any non-default case statements, create a vector of Cases
2134 // representing each one, and sort the vector so that we can efficiently
2135 // create a binary search tree from them.
2136 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002137 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002138 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2139 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002140 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002141
2142 // Get the Value to be switched on and default basic blocks, which will be
2143 // inserted into CaseBlock records, representing basic blocks in the binary
2144 // search tree.
2145 Value *SV = SI.getOperand(0);
2146
2147 // Push the initial CaseRec onto the worklist
2148 CaseRecVector WorkList;
2149 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2150
2151 while (!WorkList.empty()) {
2152 // Grab a record representing a case range to process off the worklist
2153 CaseRec CR = WorkList.back();
2154 WorkList.pop_back();
2155
2156 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2157 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002158
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002159 // If the range has few cases (two or less) emit a series of specific
2160 // tests.
2161 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2162 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002163
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002164 // If the switch has more than 5 blocks, and at least 40% dense, and the
2165 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002166 // lowering the switch to a binary tree of conditional branches.
2167 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2168 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002169
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002170 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2171 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2172 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2173 }
2174}
2175
Dan Gohman2048b852009-11-23 18:04:58 +00002176void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002177 // Update machine-CFG edges.
2178 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2179 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2180
Bill Wendling49fcff82009-12-21 22:30:11 +00002181 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2182 MVT::Other, getControlRoot(),
2183 getValue(I.getAddress()));
2184 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002185
Bill Wendling49fcff82009-12-21 22:30:11 +00002186 if (DisableScheduling)
2187 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2188}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002189
Dan Gohman2048b852009-11-23 18:04:58 +00002190void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002191 // -0.0 - X --> fneg
2192 const Type *Ty = I.getType();
2193 if (isa<VectorType>(Ty)) {
2194 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2195 const VectorType *DestTy = cast<VectorType>(I.getType());
2196 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002197 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002198 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002199 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002200 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002201 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002202 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002203 Op2.getValueType(), Op2);
Bill Wendling49fcff82009-12-21 22:30:11 +00002204 setValue(&I, Res);
2205
2206 if (DisableScheduling)
2207 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2208
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002209 return;
2210 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002211 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002212 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002213
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002214 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002215 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002216 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002217 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2218 Op2.getValueType(), Op2);
2219 setValue(&I, Res);
2220
2221 if (DisableScheduling)
2222 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2223
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002224 return;
2225 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002227 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228}
2229
Dan Gohman2048b852009-11-23 18:04:58 +00002230void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002231 SDValue Op1 = getValue(I.getOperand(0));
2232 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002233 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2234 Op1.getValueType(), Op1, Op2);
2235 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002236
Bill Wendling49fcff82009-12-21 22:30:11 +00002237 if (DisableScheduling)
2238 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239}
2240
Dan Gohman2048b852009-11-23 18:04:58 +00002241void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002242 SDValue Op1 = getValue(I.getOperand(0));
2243 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002244 if (!isa<VectorType>(I.getType()) &&
2245 Op2.getValueType() != TLI.getShiftAmountTy()) {
2246 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002247 EVT PTy = TLI.getPointerTy();
2248 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002249 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002250 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2251 TLI.getShiftAmountTy(), Op2);
2252 // If the operand is larger than the shift count type but the shift
2253 // count type has enough bits to represent any shift value, truncate
2254 // it now. This is a common case and it exposes the truncate to
2255 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002256 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002257 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2258 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2259 TLI.getShiftAmountTy(), Op2);
2260 // Otherwise we'll need to temporarily settle for some other
2261 // convenient type; type legalization will make adjustments as
2262 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002263 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002264 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002265 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002266 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002267 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002268 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002269 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002270
Bill Wendling49fcff82009-12-21 22:30:11 +00002271 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2272 Op1.getValueType(), Op1, Op2);
2273 setValue(&I, Res);
2274
Bill Wendling87710f02009-12-21 23:47:40 +00002275 if (DisableScheduling) {
2276 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2277 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002278 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002279 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002280}
2281
Dan Gohman2048b852009-11-23 18:04:58 +00002282void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002283 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2284 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2285 predicate = IC->getPredicate();
2286 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2287 predicate = ICmpInst::Predicate(IC->getPredicate());
2288 SDValue Op1 = getValue(I.getOperand(0));
2289 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002290 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002291
Owen Andersone50ed302009-08-10 22:56:29 +00002292 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002293 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2294 setValue(&I, Res);
2295
2296 if (DisableScheduling)
2297 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002298}
2299
Dan Gohman2048b852009-11-23 18:04:58 +00002300void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002301 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2302 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2303 predicate = FC->getPredicate();
2304 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2305 predicate = FCmpInst::Predicate(FC->getPredicate());
2306 SDValue Op1 = getValue(I.getOperand(0));
2307 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002308 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002309 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002310 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2311 setValue(&I, Res);
2312
2313 if (DisableScheduling)
2314 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002315}
2316
Dan Gohman2048b852009-11-23 18:04:58 +00002317void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002318 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002319 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2320 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002321 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002322
Bill Wendling49fcff82009-12-21 22:30:11 +00002323 SmallVector<SDValue, 4> Values(NumValues);
2324 SDValue Cond = getValue(I.getOperand(0));
2325 SDValue TrueVal = getValue(I.getOperand(1));
2326 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002327
Bill Wendling49fcff82009-12-21 22:30:11 +00002328 for (unsigned i = 0; i != NumValues; ++i) {
2329 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2330 TrueVal.getNode()->getValueType(i), Cond,
2331 SDValue(TrueVal.getNode(),
2332 TrueVal.getResNo() + i),
2333 SDValue(FalseVal.getNode(),
2334 FalseVal.getResNo() + i));
2335
2336 if (DisableScheduling)
2337 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002338 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002339
Bill Wendling49fcff82009-12-21 22:30:11 +00002340 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2341 DAG.getVTList(&ValueVTs[0], NumValues),
2342 &Values[0], NumValues);
2343 setValue(&I, Res);
2344
2345 if (DisableScheduling)
2346 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2347}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002348
Dan Gohman2048b852009-11-23 18:04:58 +00002349void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002350 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2351 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002352 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002353 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2354 setValue(&I, Res);
2355
2356 if (DisableScheduling)
2357 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002358}
2359
Dan Gohman2048b852009-11-23 18:04:58 +00002360void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002361 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2362 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2363 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002364 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002365 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2366 setValue(&I, Res);
2367
2368 if (DisableScheduling)
2369 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002370}
2371
Dan Gohman2048b852009-11-23 18:04:58 +00002372void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002373 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2374 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2375 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002376 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002377 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2378 setValue(&I, Res);
2379
2380 if (DisableScheduling)
2381 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002382}
2383
Dan Gohman2048b852009-11-23 18:04:58 +00002384void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002385 // FPTrunc is never a no-op cast, no need to check
2386 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002387 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002388 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2389 DestVT, N, DAG.getIntPtrConstant(0));
2390 setValue(&I, Res);
2391
2392 if (DisableScheduling)
2393 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002394}
2395
Dan Gohman2048b852009-11-23 18:04:58 +00002396void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002397 // FPTrunc is never a no-op cast, no need to check
2398 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002399 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002400 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2401 setValue(&I, Res);
2402
2403 if (DisableScheduling)
2404 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002405}
2406
Dan Gohman2048b852009-11-23 18:04:58 +00002407void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002408 // FPToUI is never a no-op cast, no need to check
2409 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002410 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002411 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2412 setValue(&I, Res);
2413
2414 if (DisableScheduling)
2415 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002416}
2417
Dan Gohman2048b852009-11-23 18:04:58 +00002418void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002419 // FPToSI is never a no-op cast, no need to check
2420 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002421 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002422 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2423 setValue(&I, Res);
2424
2425 if (DisableScheduling)
2426 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002427}
2428
Dan Gohman2048b852009-11-23 18:04:58 +00002429void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002430 // UIToFP is never a no-op cast, no need to check
2431 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002432 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002433 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2434 setValue(&I, Res);
2435
2436 if (DisableScheduling)
2437 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002438}
2439
Dan Gohman2048b852009-11-23 18:04:58 +00002440void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002441 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002442 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002443 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002444 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2445 setValue(&I, Res);
2446
2447 if (DisableScheduling)
2448 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002449}
2450
Dan Gohman2048b852009-11-23 18:04:58 +00002451void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002452 // What to do depends on the size of the integer and the size of the pointer.
2453 // We can either truncate, zero extend, or no-op, accordingly.
2454 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002455 EVT SrcVT = N.getValueType();
2456 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002457 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2458 setValue(&I, Res);
2459
2460 if (DisableScheduling)
2461 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002462}
2463
Dan Gohman2048b852009-11-23 18:04:58 +00002464void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002465 // What to do depends on the size of the integer and the size of the pointer.
2466 // We can either truncate, zero extend, or no-op, accordingly.
2467 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002468 EVT SrcVT = N.getValueType();
2469 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002470 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2471 setValue(&I, Res);
2472
2473 if (DisableScheduling)
2474 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002475}
2476
Dan Gohman2048b852009-11-23 18:04:58 +00002477void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002478 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002479 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002480
Bill Wendling49fcff82009-12-21 22:30:11 +00002481 // BitCast assures us that source and destination are the same size so this is
2482 // either a BIT_CONVERT or a no-op.
2483 if (DestVT != N.getValueType()) {
2484 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2485 DestVT, N); // convert types.
2486 setValue(&I, Res);
2487
2488 if (DisableScheduling)
2489 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2490 } else {
2491 setValue(&I, N); // noop cast.
2492 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002493}
2494
Dan Gohman2048b852009-11-23 18:04:58 +00002495void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002496 SDValue InVec = getValue(I.getOperand(0));
2497 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002498 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002499 TLI.getPointerTy(),
2500 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002501 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2502 TLI.getValueType(I.getType()),
2503 InVec, InVal, InIdx);
2504 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002505
Bill Wendling87710f02009-12-21 23:47:40 +00002506 if (DisableScheduling) {
2507 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002508 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002509 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002510}
2511
Dan Gohman2048b852009-11-23 18:04:58 +00002512void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002513 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002514 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002515 TLI.getPointerTy(),
2516 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002517 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2518 TLI.getValueType(I.getType()), InVec, InIdx);
2519 setValue(&I, Res);
2520
Bill Wendling87710f02009-12-21 23:47:40 +00002521 if (DisableScheduling) {
2522 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002523 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002524 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002525}
2526
Mon P Wangaeb06d22008-11-10 04:46:22 +00002527
2528// Utility for visitShuffleVector - Returns true if the mask is mask starting
2529// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002530static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2531 unsigned MaskNumElts = Mask.size();
2532 for (unsigned i = 0; i != MaskNumElts; ++i)
2533 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002534 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002535 return true;
2536}
2537
Dan Gohman2048b852009-11-23 18:04:58 +00002538void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002539 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002540 SDValue Src1 = getValue(I.getOperand(0));
2541 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002542
Nate Begeman9008ca62009-04-27 18:41:29 +00002543 // Convert the ConstantVector mask operand into an array of ints, with -1
2544 // representing undef values.
2545 SmallVector<Constant*, 8> MaskElts;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002546 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
Owen Anderson001dbfe2009-07-16 18:04:31 +00002547 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002548 unsigned MaskNumElts = MaskElts.size();
2549 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002550 if (isa<UndefValue>(MaskElts[i]))
2551 Mask.push_back(-1);
2552 else
2553 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2554 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002555
Owen Andersone50ed302009-08-10 22:56:29 +00002556 EVT VT = TLI.getValueType(I.getType());
2557 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002558 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002559
Mon P Wangc7849c22008-11-16 05:06:27 +00002560 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002561 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2562 &Mask[0]);
2563 setValue(&I, Res);
2564
2565 if (DisableScheduling)
2566 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2567
Mon P Wangaeb06d22008-11-10 04:46:22 +00002568 return;
2569 }
2570
2571 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002572 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2573 // Mask is longer than the source vectors and is a multiple of the source
2574 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002575 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002576 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2577 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002578 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2579 VT, Src1, Src2);
2580 setValue(&I, Res);
2581
2582 if (DisableScheduling)
2583 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2584
Mon P Wangaeb06d22008-11-10 04:46:22 +00002585 return;
2586 }
2587
Mon P Wangc7849c22008-11-16 05:06:27 +00002588 // Pad both vectors with undefs to make them the same length as the mask.
2589 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002590 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2591 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002592 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002593
Nate Begeman9008ca62009-04-27 18:41:29 +00002594 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2595 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002596 MOps1[0] = Src1;
2597 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002598
2599 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2600 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002601 &MOps1[0], NumConcat);
2602 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002603 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002604 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002605
Mon P Wangaeb06d22008-11-10 04:46:22 +00002606 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002607 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002608 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002609 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002610 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002611 MappedOps.push_back(Idx);
2612 else
2613 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002614 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002615
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002616 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002617 &MappedOps[0]);
2618 setValue(&I, Res);
2619
Bill Wendlinge1a90422009-12-21 23:10:19 +00002620 if (DisableScheduling) {
2621 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2622 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002623 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002624 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002625
Mon P Wangaeb06d22008-11-10 04:46:22 +00002626 return;
2627 }
2628
Mon P Wangc7849c22008-11-16 05:06:27 +00002629 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002630 // Analyze the access pattern of the vector to see if we can extract
2631 // two subvectors and do the shuffle. The analysis is done by calculating
2632 // the range of elements the mask access on both vectors.
2633 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2634 int MaxRange[2] = {-1, -1};
2635
Nate Begeman5a5ca152009-04-29 05:20:52 +00002636 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002637 int Idx = Mask[i];
2638 int Input = 0;
2639 if (Idx < 0)
2640 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002641
Nate Begeman5a5ca152009-04-29 05:20:52 +00002642 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002643 Input = 1;
2644 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002645 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002646 if (Idx > MaxRange[Input])
2647 MaxRange[Input] = Idx;
2648 if (Idx < MinRange[Input])
2649 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002650 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002651
Mon P Wangc7849c22008-11-16 05:06:27 +00002652 // Check if the access is smaller than the vector size and can we find
2653 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002654 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2655 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002656 int StartIdx[2]; // StartIdx to extract from
2657 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002658 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002659 RangeUse[Input] = 0; // Unused
2660 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002661 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002662 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002663 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002664 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002665 RangeUse[Input] = 1; // Extract from beginning of the vector
2666 StartIdx[Input] = 0;
2667 } else {
2668 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002669 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002670 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002671 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002672 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002673 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002674 }
2675
Bill Wendling636e2582009-08-21 18:16:06 +00002676 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002677 SDValue Res = DAG.getUNDEF(VT);
2678 setValue(&I, Res); // Vectors are not used.
2679
2680 if (DisableScheduling)
2681 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2682
Mon P Wangc7849c22008-11-16 05:06:27 +00002683 return;
2684 }
2685 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2686 // Extract appropriate subvector and generate a vector shuffle
2687 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002688 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002689 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002690 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002691 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002692 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002693 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002694
2695 if (DisableScheduling)
2696 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002697 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002698
Mon P Wangc7849c22008-11-16 05:06:27 +00002699 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002700 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002701 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002702 int Idx = Mask[i];
2703 if (Idx < 0)
2704 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002705 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002706 MappedOps.push_back(Idx - StartIdx[0]);
2707 else
2708 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002709 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002710
2711 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2712 &MappedOps[0]);
2713 setValue(&I, Res);
2714
2715 if (DisableScheduling)
2716 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2717
Mon P Wangc7849c22008-11-16 05:06:27 +00002718 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002719 }
2720 }
2721
Mon P Wangc7849c22008-11-16 05:06:27 +00002722 // We can't use either concat vectors or extract subvectors so fall back to
2723 // replacing the shuffle with extract and build vector.
2724 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002725 EVT EltVT = VT.getVectorElementType();
2726 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002727 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002728 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002729 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002730 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002731 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002732 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002733 SDValue Res;
2734
Nate Begeman5a5ca152009-04-29 05:20:52 +00002735 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002736 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2737 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002738 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002739 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2740 EltVT, Src2,
2741 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2742
2743 Ops.push_back(Res);
2744
2745 if (DisableScheduling)
2746 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002747 }
2748 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002749
2750 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2751 VT, &Ops[0], Ops.size());
2752 setValue(&I, Res);
2753
2754 if (DisableScheduling)
2755 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002756}
2757
Dan Gohman2048b852009-11-23 18:04:58 +00002758void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002759 const Value *Op0 = I.getOperand(0);
2760 const Value *Op1 = I.getOperand(1);
2761 const Type *AggTy = I.getType();
2762 const Type *ValTy = Op1->getType();
2763 bool IntoUndef = isa<UndefValue>(Op0);
2764 bool FromUndef = isa<UndefValue>(Op1);
2765
2766 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2767 I.idx_begin(), I.idx_end());
2768
Owen Andersone50ed302009-08-10 22:56:29 +00002769 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002770 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002771 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002772 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2773
2774 unsigned NumAggValues = AggValueVTs.size();
2775 unsigned NumValValues = ValValueVTs.size();
2776 SmallVector<SDValue, 4> Values(NumAggValues);
2777
2778 SDValue Agg = getValue(Op0);
2779 SDValue Val = getValue(Op1);
2780 unsigned i = 0;
2781 // Copy the beginning value(s) from the original aggregate.
2782 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002783 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002784 SDValue(Agg.getNode(), Agg.getResNo() + i);
2785 // Copy values from the inserted value(s).
2786 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002787 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002788 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2789 // Copy remaining value(s) from the original aggregate.
2790 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002791 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792 SDValue(Agg.getNode(), Agg.getResNo() + i);
2793
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002794 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2795 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2796 &Values[0], NumAggValues);
2797 setValue(&I, Res);
2798
2799 if (DisableScheduling)
2800 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002801}
2802
Dan Gohman2048b852009-11-23 18:04:58 +00002803void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002804 const Value *Op0 = I.getOperand(0);
2805 const Type *AggTy = Op0->getType();
2806 const Type *ValTy = I.getType();
2807 bool OutOfUndef = isa<UndefValue>(Op0);
2808
2809 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2810 I.idx_begin(), I.idx_end());
2811
Owen Andersone50ed302009-08-10 22:56:29 +00002812 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002813 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2814
2815 unsigned NumValValues = ValValueVTs.size();
2816 SmallVector<SDValue, 4> Values(NumValValues);
2817
2818 SDValue Agg = getValue(Op0);
2819 // Copy out the selected value(s).
2820 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2821 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002822 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002823 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002824 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002826 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2827 DAG.getVTList(&ValValueVTs[0], NumValValues),
2828 &Values[0], NumValValues);
2829 setValue(&I, Res);
2830
2831 if (DisableScheduling)
2832 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002833}
2834
Dan Gohman2048b852009-11-23 18:04:58 +00002835void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002836 SDValue N = getValue(I.getOperand(0));
2837 const Type *Ty = I.getOperand(0)->getType();
2838
2839 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2840 OI != E; ++OI) {
2841 Value *Idx = *OI;
2842 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2843 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2844 if (Field) {
2845 // N = N + Offset
2846 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002847 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002849
2850 if (DisableScheduling)
2851 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002852 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002853
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854 Ty = StTy->getElementType(Field);
2855 } else {
2856 Ty = cast<SequentialType>(Ty)->getElementType();
2857
2858 // If this is a constant subscript, handle it quickly.
2859 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2860 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002861 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002862 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002863 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002864 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002865 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002866 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002867 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2868 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002869 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002870 else
Evan Chengb1032a82009-02-09 20:54:38 +00002871 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002872
Dale Johannesen66978ee2009-01-31 02:22:37 +00002873 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002874 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002875
2876 if (DisableScheduling) {
2877 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2878 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2879 }
2880
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002881 continue;
2882 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002883
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002884 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002885 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2886 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002887 SDValue IdxN = getValue(Idx);
2888
2889 // If the index is smaller or larger than intptr_t, truncate or extend
2890 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002891 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002892
2893 // If this is a multiply by a power of two, turn it into a shl
2894 // immediately. This is a very common case.
2895 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002896 if (ElementSize.isPowerOf2()) {
2897 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002898 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002899 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002900 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002901 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002902 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002903 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002904 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002905 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002906
2907 if (DisableScheduling)
2908 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002909 }
2910
Scott Michelfdc40a02009-02-17 22:15:04 +00002911 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002912 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002913
2914 if (DisableScheduling)
2915 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002916 }
2917 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002918
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002919 setValue(&I, N);
2920}
2921
Dan Gohman2048b852009-11-23 18:04:58 +00002922void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002923 // If this is a fixed sized alloca in the entry block of the function,
2924 // allocate it statically on the stack.
2925 if (FuncInfo.StaticAllocaMap.count(&I))
2926 return; // getValue will auto-populate this.
2927
2928 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002929 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002930 unsigned Align =
2931 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2932 I.getAlignment());
2933
2934 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002935
Chris Lattner0b18e592009-03-17 19:36:00 +00002936 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2937 AllocSize,
2938 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002939
Bill Wendling856ff412009-12-22 00:12:37 +00002940 if (DisableScheduling)
2941 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002942
Owen Andersone50ed302009-08-10 22:56:29 +00002943 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002944 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002945
Bill Wendling856ff412009-12-22 00:12:37 +00002946 if (DisableScheduling)
2947 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002948
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002949 // Handle alignment. If the requested alignment is less than or equal to
2950 // the stack alignment, ignore it. If the size is greater than or equal to
2951 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2952 unsigned StackAlign =
2953 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2954 if (Align <= StackAlign)
2955 Align = 0;
2956
2957 // Round the size of the allocation up to the stack alignment size
2958 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002959 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002960 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002961 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002962 if (DisableScheduling)
2963 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002965 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002966 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002967 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002968 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002969 if (DisableScheduling)
2970 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002971
2972 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002973 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002974 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002975 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002976 setValue(&I, DSA);
2977 DAG.setRoot(DSA.getValue(1));
2978
Bill Wendling856ff412009-12-22 00:12:37 +00002979 if (DisableScheduling)
2980 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
2981
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002982 // Inform the Frame Information that we have just allocated a variable-sized
2983 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002984 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002985}
2986
Dan Gohman2048b852009-11-23 18:04:58 +00002987void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002988 const Value *SV = I.getOperand(0);
2989 SDValue Ptr = getValue(SV);
2990
2991 const Type *Ty = I.getType();
2992 bool isVolatile = I.isVolatile();
2993 unsigned Alignment = I.getAlignment();
2994
Owen Andersone50ed302009-08-10 22:56:29 +00002995 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002996 SmallVector<uint64_t, 4> Offsets;
2997 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2998 unsigned NumValues = ValueVTs.size();
2999 if (NumValues == 0)
3000 return;
3001
3002 SDValue Root;
3003 bool ConstantMemory = false;
3004 if (I.isVolatile())
3005 // Serialize volatile loads with other side effects.
3006 Root = getRoot();
3007 else if (AA->pointsToConstantMemory(SV)) {
3008 // Do not serialize (non-volatile) loads of constant memory with anything.
3009 Root = DAG.getEntryNode();
3010 ConstantMemory = true;
3011 } else {
3012 // Do not serialize non-volatile loads against each other.
3013 Root = DAG.getRoot();
3014 }
3015
3016 SmallVector<SDValue, 4> Values(NumValues);
3017 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003018 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003019 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00003020 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
3021 PtrVT, Ptr,
3022 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003023 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00003024 A, SV, Offsets[i], isVolatile, Alignment);
3025
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003026 Values[i] = L;
3027 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00003028
3029 if (DisableScheduling) {
3030 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
3031 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3032 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003033 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003035 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003036 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003037 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003038 if (isVolatile)
3039 DAG.setRoot(Chain);
3040 else
3041 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00003042
3043 if (DisableScheduling)
3044 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003045 }
3046
Bill Wendling856ff412009-12-22 00:12:37 +00003047 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
3048 DAG.getVTList(&ValueVTs[0], NumValues),
3049 &Values[0], NumValues);
3050 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003051
Bill Wendling856ff412009-12-22 00:12:37 +00003052 if (DisableScheduling)
3053 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
3054}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003055
Dan Gohman2048b852009-11-23 18:04:58 +00003056void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003057 Value *SrcV = I.getOperand(0);
3058 Value *PtrV = I.getOperand(1);
3059
Owen Andersone50ed302009-08-10 22:56:29 +00003060 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003061 SmallVector<uint64_t, 4> Offsets;
3062 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3063 unsigned NumValues = ValueVTs.size();
3064 if (NumValues == 0)
3065 return;
3066
3067 // Get the lowered operands. Note that we do this after
3068 // checking if NumResults is zero, because with zero results
3069 // the operands won't have values in the map.
3070 SDValue Src = getValue(SrcV);
3071 SDValue Ptr = getValue(PtrV);
3072
3073 SDValue Root = getRoot();
3074 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003075 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003076 bool isVolatile = I.isVolatile();
3077 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003078
3079 for (unsigned i = 0; i != NumValues; ++i) {
3080 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3081 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003082 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003083 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003084 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003085
Bill Wendling856ff412009-12-22 00:12:37 +00003086 if (DisableScheduling) {
3087 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3088 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3089 }
3090 }
3091
3092 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3093 MVT::Other, &Chains[0], NumValues);
3094 DAG.setRoot(Res);
3095
3096 if (DisableScheduling)
3097 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003098}
3099
3100/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3101/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003102void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3103 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003104 bool HasChain = !I.doesNotAccessMemory();
3105 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3106
3107 // Build the operand list.
3108 SmallVector<SDValue, 8> Ops;
3109 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3110 if (OnlyLoad) {
3111 // We don't need to serialize loads against other loads.
3112 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003113 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003114 Ops.push_back(getRoot());
3115 }
3116 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003117
3118 // Info is set by getTgtMemInstrinsic
3119 TargetLowering::IntrinsicInfo Info;
3120 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3121
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003122 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003123 if (!IsTgtIntrinsic)
3124 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003125
3126 // Add all operands of the call to the operand list.
3127 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3128 SDValue Op = getValue(I.getOperand(i));
3129 assert(TLI.isTypeLegal(Op.getValueType()) &&
3130 "Intrinsic uses a non-legal type?");
3131 Ops.push_back(Op);
3132 }
3133
Owen Andersone50ed302009-08-10 22:56:29 +00003134 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003135 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3136#ifndef NDEBUG
3137 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3138 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3139 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003140 }
Bob Wilson8d919552009-07-31 22:41:21 +00003141#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003142
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003143 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003144 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003145
Bob Wilson8d919552009-07-31 22:41:21 +00003146 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003147
3148 // Create the node.
3149 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003150 if (IsTgtIntrinsic) {
3151 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003152 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003153 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003154 Info.memVT, Info.ptrVal, Info.offset,
3155 Info.align, Info.vol,
3156 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003157 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003158 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003159 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003160 } else if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003161 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003162 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003163 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003164 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003165 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003166 }
3167
3168 if (DisableScheduling)
3169 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003170
3171 if (HasChain) {
3172 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3173 if (OnlyLoad)
3174 PendingLoads.push_back(Chain);
3175 else
3176 DAG.setRoot(Chain);
3177 }
Bill Wendling856ff412009-12-22 00:12:37 +00003178
Owen Anderson1d0be152009-08-13 21:58:54 +00003179 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003180 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003181 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003182 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003183
3184 if (DisableScheduling)
3185 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003186 }
Bill Wendling856ff412009-12-22 00:12:37 +00003187
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003188 setValue(&I, Result);
3189 }
3190}
3191
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003192/// GetSignificand - Get the significand and build it into a floating-point
3193/// number with exponent of 1:
3194///
3195/// Op = (Op & 0x007fffff) | 0x3f800000;
3196///
3197/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003198static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003199GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003200 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3201 DAG.getConstant(0x007fffff, MVT::i32));
3202 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3203 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003204 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3205
3206 if (DisableScheduling) {
3207 DAG.AssignOrdering(t1.getNode(), Order);
3208 DAG.AssignOrdering(t2.getNode(), Order);
3209 DAG.AssignOrdering(Res.getNode(), Order);
3210 }
3211
3212 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003213}
3214
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003215/// GetExponent - Get the exponent:
3216///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003217/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003218///
3219/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003220static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003221GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003222 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003223 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3224 DAG.getConstant(0x7f800000, MVT::i32));
3225 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003226 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003227 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3228 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003229 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3230
3231 if (DisableScheduling) {
3232 DAG.AssignOrdering(t0.getNode(), Order);
3233 DAG.AssignOrdering(t1.getNode(), Order);
3234 DAG.AssignOrdering(t2.getNode(), Order);
3235 DAG.AssignOrdering(Res.getNode(), Order);
3236 }
3237
3238 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003239}
3240
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003241/// getF32Constant - Get 32-bit floating point constant.
3242static SDValue
3243getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003244 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003245}
3246
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003247/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003248/// visitIntrinsicCall: I is a call instruction
3249/// Op is the associated NodeType for I
3250const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003251SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003252 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003253 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003254 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003255 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003256 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003257 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003258 getValue(I.getOperand(2)),
3259 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003260 setValue(&I, L);
3261 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003262
3263 if (DisableScheduling)
3264 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003266 return 0;
3267}
3268
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003269// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003270const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003271SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003272 SDValue Op1 = getValue(I.getOperand(1));
3273 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003274
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003276 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003277
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003278 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003279
3280 if (DisableScheduling)
3281 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3282
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003283 return 0;
3284}
Bill Wendling74c37652008-12-09 22:08:41 +00003285
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003286/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3287/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003288void
Dan Gohman2048b852009-11-23 18:04:58 +00003289SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003290 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003291 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003292
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003294 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3295 SDValue Op = getValue(I.getOperand(1));
3296
3297 // Put the exponent in the right bit position for later addition to the
3298 // final result:
3299 //
3300 // #define LOG2OFe 1.4426950f
3301 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003302 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003303 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003304 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003305
3306 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003307 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3308 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003309
Bill Wendling856ff412009-12-22 00:12:37 +00003310 if (DisableScheduling) {
3311 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3312 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3313 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3314 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3315 }
3316
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003317 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003318 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003319 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003320
Bill Wendling856ff412009-12-22 00:12:37 +00003321 if (DisableScheduling)
3322 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3323
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003324 if (LimitFloatPrecision <= 6) {
3325 // For floating-point precision of 6:
3326 //
3327 // TwoToFractionalPartOfX =
3328 // 0.997535578f +
3329 // (0.735607626f + 0.252464424f * x) * x;
3330 //
3331 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003332 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003333 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003334 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003335 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003336 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3337 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003338 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003339 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003340
3341 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003342 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003343 TwoToFracPartOfX, IntegerPartOfX);
3344
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003346
3347 if (DisableScheduling) {
3348 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3349 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3350 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3351 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3352 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3353 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3354 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3355 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003356 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3357 // For floating-point precision of 12:
3358 //
3359 // TwoToFractionalPartOfX =
3360 // 0.999892986f +
3361 // (0.696457318f +
3362 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3363 //
3364 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003366 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003367 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003368 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3370 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003371 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003372 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3373 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003374 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003375 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003376
3377 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003378 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003379 TwoToFracPartOfX, IntegerPartOfX);
3380
Owen Anderson825b72b2009-08-11 20:47:22 +00003381 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003382
3383 if (DisableScheduling) {
3384 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3385 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3386 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3387 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3388 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3389 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3390 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3391 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3392 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3393 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003394 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3395 // For floating-point precision of 18:
3396 //
3397 // TwoToFractionalPartOfX =
3398 // 0.999999982f +
3399 // (0.693148872f +
3400 // (0.240227044f +
3401 // (0.554906021e-1f +
3402 // (0.961591928e-2f +
3403 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3404 //
3405 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003406 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003407 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003409 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003410 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3411 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003412 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003413 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3414 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003415 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003416 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3417 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003418 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003419 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3420 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003421 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3423 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003424 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003425 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003426 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003427
3428 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003429 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003430 TwoToFracPartOfX, IntegerPartOfX);
3431
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003433
3434 if (DisableScheduling) {
3435 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3436 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3437 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3438 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3439 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3440 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3441 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3442 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3443 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3444 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3445 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3446 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3447 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3448 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3449 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3450 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003451 }
3452 } else {
3453 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003454 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003455 getValue(I.getOperand(1)).getValueType(),
3456 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003457 if (DisableScheduling)
3458 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003459 }
3460
Dale Johannesen59e577f2008-09-05 18:38:42 +00003461 setValue(&I, result);
3462}
3463
Bill Wendling39150252008-09-09 20:39:27 +00003464/// visitLog - Lower a log intrinsic. Handles the special sequences for
3465/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003466void
Dan Gohman2048b852009-11-23 18:04:58 +00003467SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003468 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003469 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003470
Owen Anderson825b72b2009-08-11 20:47:22 +00003471 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003472 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3473 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003474 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003475
Bill Wendling856ff412009-12-22 00:12:37 +00003476 if (DisableScheduling)
3477 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3478
Bill Wendling39150252008-09-09 20:39:27 +00003479 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003480 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003481 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003482 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003483
Bill Wendling856ff412009-12-22 00:12:37 +00003484 if (DisableScheduling)
3485 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3486
Bill Wendling39150252008-09-09 20:39:27 +00003487 // Get the significand and build it into a floating-point number with
3488 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003489 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003490
3491 if (LimitFloatPrecision <= 6) {
3492 // For floating-point precision of 6:
3493 //
3494 // LogofMantissa =
3495 // -1.1609546f +
3496 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003497 //
Bill Wendling39150252008-09-09 20:39:27 +00003498 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003499 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003500 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003501 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003502 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003503 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3504 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003505 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003506
Scott Michelfdc40a02009-02-17 22:15:04 +00003507 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003508 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003509
3510 if (DisableScheduling) {
3511 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3512 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3513 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3514 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3515 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3516 }
Bill Wendling39150252008-09-09 20:39:27 +00003517 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3518 // For floating-point precision of 12:
3519 //
3520 // LogOfMantissa =
3521 // -1.7417939f +
3522 // (2.8212026f +
3523 // (-1.4699568f +
3524 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3525 //
3526 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003528 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003529 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003530 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3532 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003533 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003534 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3535 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003536 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003537 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3538 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003539 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003540
Scott Michelfdc40a02009-02-17 22:15:04 +00003541 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003542 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003543
3544 if (DisableScheduling) {
3545 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3546 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3547 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3548 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3549 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3550 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3551 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3552 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3553 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3554 }
Bill Wendling39150252008-09-09 20:39:27 +00003555 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3556 // For floating-point precision of 18:
3557 //
3558 // LogOfMantissa =
3559 // -2.1072184f +
3560 // (4.2372794f +
3561 // (-3.7029485f +
3562 // (2.2781945f +
3563 // (-0.87823314f +
3564 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3565 //
3566 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003568 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003569 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003570 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3572 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003573 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3575 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003576 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3578 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003579 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003580 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3581 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003582 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3584 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003585 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003586
Scott Michelfdc40a02009-02-17 22:15:04 +00003587 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003588 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003589
3590 if (DisableScheduling) {
3591 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3592 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3593 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3594 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3595 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3596 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3597 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3598 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3599 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3600 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3601 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3602 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3603 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3604 }
Bill Wendling39150252008-09-09 20:39:27 +00003605 }
3606 } else {
3607 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003608 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003609 getValue(I.getOperand(1)).getValueType(),
3610 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003611
3612 if (DisableScheduling)
3613 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003614 }
3615
Dale Johannesen59e577f2008-09-05 18:38:42 +00003616 setValue(&I, result);
3617}
3618
Bill Wendling3eb59402008-09-09 00:28:24 +00003619/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3620/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003621void
Dan Gohman2048b852009-11-23 18:04:58 +00003622SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003623 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003624 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003625
Owen Anderson825b72b2009-08-11 20:47:22 +00003626 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003627 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3628 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003629 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003630
Bill Wendling856ff412009-12-22 00:12:37 +00003631 if (DisableScheduling)
3632 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3633
Bill Wendling39150252008-09-09 20:39:27 +00003634 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003635 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3636
3637 if (DisableScheduling)
3638 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003639
3640 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003641 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003642 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003643
Bill Wendling3eb59402008-09-09 00:28:24 +00003644 // Different possible minimax approximations of significand in
3645 // floating-point for various degrees of accuracy over [1,2].
3646 if (LimitFloatPrecision <= 6) {
3647 // For floating-point precision of 6:
3648 //
3649 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3650 //
3651 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003652 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003653 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003654 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003655 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003656 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3657 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003658 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003659
Scott Michelfdc40a02009-02-17 22:15:04 +00003660 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003662
3663 if (DisableScheduling) {
3664 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3665 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3666 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3667 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3668 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3669 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003670 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3671 // For floating-point precision of 12:
3672 //
3673 // Log2ofMantissa =
3674 // -2.51285454f +
3675 // (4.07009056f +
3676 // (-2.12067489f +
3677 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003678 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003679 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003680 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003681 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003682 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003683 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003684 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3685 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003686 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003687 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3688 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003689 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003690 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3691 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003692 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003693
Scott Michelfdc40a02009-02-17 22:15:04 +00003694 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003695 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003696
3697 if (DisableScheduling) {
3698 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3699 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3700 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3701 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3702 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3703 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3704 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3705 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3706 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3707 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003708 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3709 // For floating-point precision of 18:
3710 //
3711 // Log2ofMantissa =
3712 // -3.0400495f +
3713 // (6.1129976f +
3714 // (-5.3420409f +
3715 // (3.2865683f +
3716 // (-1.2669343f +
3717 // (0.27515199f -
3718 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3719 //
3720 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003721 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003722 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003723 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003724 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003725 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3726 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003727 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003728 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3729 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003730 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003731 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3732 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003733 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003734 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3735 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003736 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3738 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003739 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003740
Scott Michelfdc40a02009-02-17 22:15:04 +00003741 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003742 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003743
3744 if (DisableScheduling) {
3745 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3746 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3747 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3748 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3749 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3750 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3751 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3752 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3753 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3754 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3755 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3756 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3757 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3758 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003759 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003760 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003761 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003762 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003763 getValue(I.getOperand(1)).getValueType(),
3764 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003765
3766 if (DisableScheduling)
3767 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003768 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003769
Dale Johannesen59e577f2008-09-05 18:38:42 +00003770 setValue(&I, result);
3771}
3772
Bill Wendling3eb59402008-09-09 00:28:24 +00003773/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3774/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003775void
Dan Gohman2048b852009-11-23 18:04:58 +00003776SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003777 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003778 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003779
Owen Anderson825b72b2009-08-11 20:47:22 +00003780 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003781 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3782 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003783 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003784
Bill Wendling856ff412009-12-22 00:12:37 +00003785 if (DisableScheduling)
3786 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3787
Bill Wendling39150252008-09-09 20:39:27 +00003788 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003789 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003790 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003791 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003792
Bill Wendling856ff412009-12-22 00:12:37 +00003793 if (DisableScheduling)
3794 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3795
Bill Wendling3eb59402008-09-09 00:28:24 +00003796 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003797 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003798 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003799
3800 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003801 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003802 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003803 // Log10ofMantissa =
3804 // -0.50419619f +
3805 // (0.60948995f - 0.10380950f * x) * x;
3806 //
3807 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003808 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003809 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003810 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003811 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003812 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3813 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003814 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003815
Scott Michelfdc40a02009-02-17 22:15:04 +00003816 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003817 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003818
3819 if (DisableScheduling) {
3820 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3821 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3822 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3823 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3824 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3825 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003826 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3827 // For floating-point precision of 12:
3828 //
3829 // Log10ofMantissa =
3830 // -0.64831180f +
3831 // (0.91751397f +
3832 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3833 //
3834 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003835 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003836 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003837 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003838 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003839 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3840 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003841 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003842 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3843 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003844 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003845
Scott Michelfdc40a02009-02-17 22:15:04 +00003846 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003847 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003848
3849 if (DisableScheduling) {
3850 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3851 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3852 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3853 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3854 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3855 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3856 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3857 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003858 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003859 // For floating-point precision of 18:
3860 //
3861 // Log10ofMantissa =
3862 // -0.84299375f +
3863 // (1.5327582f +
3864 // (-1.0688956f +
3865 // (0.49102474f +
3866 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3867 //
3868 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003869 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003870 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003871 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003872 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003873 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3874 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003875 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003876 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3877 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003878 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003879 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3880 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003881 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003882 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3883 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003884 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003885
Scott Michelfdc40a02009-02-17 22:15:04 +00003886 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003887 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003888
3889 if (DisableScheduling) {
3890 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3891 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3892 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3893 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3894 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3895 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3896 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3897 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3898 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3899 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3900 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3901 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003902 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003903 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003904 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003905 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003906 getValue(I.getOperand(1)).getValueType(),
3907 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003908
3909 if (DisableScheduling)
3910 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003911 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003912
Dale Johannesen59e577f2008-09-05 18:38:42 +00003913 setValue(&I, result);
3914}
3915
Bill Wendlinge10c8142008-09-09 22:39:21 +00003916/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3917/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003918void
Dan Gohman2048b852009-11-23 18:04:58 +00003919SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003920 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003921 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003922
Owen Anderson825b72b2009-08-11 20:47:22 +00003923 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003924 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3925 SDValue Op = getValue(I.getOperand(1));
3926
Owen Anderson825b72b2009-08-11 20:47:22 +00003927 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003928
Bill Wendling856ff412009-12-22 00:12:37 +00003929 if (DisableScheduling)
3930 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3931
Bill Wendlinge10c8142008-09-09 22:39:21 +00003932 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003933 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3934 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003935
3936 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003937 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003938 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003939
Bill Wendling856ff412009-12-22 00:12:37 +00003940 if (DisableScheduling) {
3941 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3942 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3943 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3944 }
3945
Bill Wendlinge10c8142008-09-09 22:39:21 +00003946 if (LimitFloatPrecision <= 6) {
3947 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003948 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003949 // TwoToFractionalPartOfX =
3950 // 0.997535578f +
3951 // (0.735607626f + 0.252464424f * x) * x;
3952 //
3953 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003955 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003956 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003957 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003958 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3959 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003960 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003961 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003962 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003963 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003964
Scott Michelfdc40a02009-02-17 22:15:04 +00003965 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003966 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003967
3968 if (DisableScheduling) {
3969 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3970 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3971 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3972 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3973 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3974 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3975 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3976 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003977 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3978 // For floating-point precision of 12:
3979 //
3980 // TwoToFractionalPartOfX =
3981 // 0.999892986f +
3982 // (0.696457318f +
3983 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3984 //
3985 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003986 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003987 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003988 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003989 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003990 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3991 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003992 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003993 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3994 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003995 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003996 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003997 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003998 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003999
Scott Michelfdc40a02009-02-17 22:15:04 +00004000 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004001 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004002
4003 if (DisableScheduling) {
4004 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4005 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4006 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4007 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4008 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4009 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4010 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4011 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4012 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4013 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004014 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4015 // For floating-point precision of 18:
4016 //
4017 // TwoToFractionalPartOfX =
4018 // 0.999999982f +
4019 // (0.693148872f +
4020 // (0.240227044f +
4021 // (0.554906021e-1f +
4022 // (0.961591928e-2f +
4023 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4024 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004025 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004026 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004027 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004028 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004029 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4030 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004031 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004032 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4033 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004034 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004035 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4036 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004037 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004038 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4039 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004040 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004041 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4042 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004043 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004044 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004045 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004046 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004047
Scott Michelfdc40a02009-02-17 22:15:04 +00004048 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004049 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004050
4051 if (DisableScheduling) {
4052 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4053 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4054 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4055 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4056 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4057 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4058 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4059 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4060 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4061 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4062 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4063 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4064 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4065 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4066 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4067 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004068 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00004069 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00004070 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004071 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00004072 getValue(I.getOperand(1)).getValueType(),
4073 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00004074
4075 if (DisableScheduling)
4076 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004077 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004078
Dale Johannesen601d3c02008-09-05 01:48:15 +00004079 setValue(&I, result);
4080}
4081
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004082/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4083/// limited-precision mode with x == 10.0f.
4084void
Dan Gohman2048b852009-11-23 18:04:58 +00004085SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004086 SDValue result;
4087 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004088 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004089 bool IsExp10 = false;
4090
Owen Anderson825b72b2009-08-11 20:47:22 +00004091 if (getValue(Val).getValueType() == MVT::f32 &&
4092 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004093 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4094 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4095 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4096 APFloat Ten(10.0f);
4097 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4098 }
4099 }
4100 }
4101
4102 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4103 SDValue Op = getValue(I.getOperand(2));
4104
4105 // Put the exponent in the right bit position for later addition to the
4106 // final result:
4107 //
4108 // #define LOG2OF10 3.3219281f
4109 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004110 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004111 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004112 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004113
4114 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004115 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4116 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004117
Bill Wendling856ff412009-12-22 00:12:37 +00004118 if (DisableScheduling) {
4119 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4120 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4121 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4122 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4123 }
4124
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004125 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004126 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004127 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004128
Bill Wendling856ff412009-12-22 00:12:37 +00004129 if (DisableScheduling)
4130 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4131
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004132 if (LimitFloatPrecision <= 6) {
4133 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004134 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004135 // twoToFractionalPartOfX =
4136 // 0.997535578f +
4137 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004138 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004139 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004140 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004141 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004142 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004143 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004144 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4145 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004146 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004147 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004148 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004149 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004150
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004151 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004152 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004153
4154 if (DisableScheduling) {
4155 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4156 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4157 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4158 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4159 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4160 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4161 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4162 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004163 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4164 // For floating-point precision of 12:
4165 //
4166 // TwoToFractionalPartOfX =
4167 // 0.999892986f +
4168 // (0.696457318f +
4169 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4170 //
4171 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004172 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004173 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004174 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004175 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004176 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4177 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004178 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004179 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4180 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004181 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004182 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004183 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004184 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004185
Scott Michelfdc40a02009-02-17 22:15:04 +00004186 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004187 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004188
4189 if (DisableScheduling) {
4190 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4191 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4192 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4193 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4194 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4195 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4196 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4197 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4198 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4199 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004200 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4201 // For floating-point precision of 18:
4202 //
4203 // TwoToFractionalPartOfX =
4204 // 0.999999982f +
4205 // (0.693148872f +
4206 // (0.240227044f +
4207 // (0.554906021e-1f +
4208 // (0.961591928e-2f +
4209 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4210 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004211 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004212 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004213 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004214 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004215 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4216 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004217 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004218 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4219 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004220 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004221 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4222 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004223 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004224 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4225 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004226 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4228 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004229 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004231 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004232 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004233
Scott Michelfdc40a02009-02-17 22:15:04 +00004234 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004235 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004236
4237 if (DisableScheduling) {
4238 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4239 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4240 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4241 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4242 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4243 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4244 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4245 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4246 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4247 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4248 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4249 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4250 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4251 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4252 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4253 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004254 }
4255 } else {
4256 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004257 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004258 getValue(I.getOperand(1)).getValueType(),
4259 getValue(I.getOperand(1)),
4260 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004261
4262 if (DisableScheduling)
4263 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004264 }
4265
4266 setValue(&I, result);
4267}
4268
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004269
4270/// ExpandPowI - Expand a llvm.powi intrinsic.
4271static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
4272 SelectionDAG &DAG) {
4273 // If RHS is a constant, we can expand this out to a multiplication tree,
4274 // otherwise we end up lowering to a call to __powidf2 (for example). When
4275 // optimizing for size, we only want to do this if the expansion would produce
4276 // a small number of multiplies, otherwise we do the full expansion.
4277 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4278 // Get the exponent as a positive value.
4279 unsigned Val = RHSC->getSExtValue();
4280 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004281
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004282 // powi(x, 0) -> 1.0
4283 if (Val == 0)
4284 return DAG.getConstantFP(1.0, LHS.getValueType());
4285
4286 Function *F = DAG.getMachineFunction().getFunction();
4287 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
4288 // If optimizing for size, don't insert too many multiplies. This
4289 // inserts up to 5 multiplies.
4290 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4291 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004292 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004293 // powi(x,15) generates one more multiply than it should), but this has
4294 // the benefit of being both really simple and much better than a libcall.
4295 SDValue Res; // Logically starts equal to 1.0
4296 SDValue CurSquare = LHS;
4297 while (Val) {
4298 if (Val & 1)
4299 if (Res.getNode())
4300 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4301 else
4302 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004303
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004304 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4305 CurSquare, CurSquare);
4306 Val >>= 1;
4307 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004308
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004309 // If the original was negative, invert the result, producing 1/(x*x*x).
4310 if (RHSC->getSExtValue() < 0)
4311 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4312 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4313 return Res;
4314 }
4315 }
4316
4317 // Otherwise, expand to a libcall.
4318 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4319}
4320
4321
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004322/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4323/// we want to emit this as a call to a named external function, return the name
4324/// otherwise lower it and return null.
4325const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004326SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004327 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004328 SDValue Res;
4329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004330 switch (Intrinsic) {
4331 default:
4332 // By default, turn this into a target intrinsic node.
4333 visitTargetIntrinsic(I, Intrinsic);
4334 return 0;
4335 case Intrinsic::vastart: visitVAStart(I); return 0;
4336 case Intrinsic::vaend: visitVAEnd(I); return 0;
4337 case Intrinsic::vacopy: visitVACopy(I); return 0;
4338 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004339 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4340 getValue(I.getOperand(1)));
4341 setValue(&I, Res);
4342 if (DisableScheduling)
4343 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004344 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004345 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004346 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4347 getValue(I.getOperand(1)));
4348 setValue(&I, Res);
4349 if (DisableScheduling)
4350 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004351 return 0;
4352 case Intrinsic::setjmp:
4353 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004354 case Intrinsic::longjmp:
4355 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004356 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004357 SDValue Op1 = getValue(I.getOperand(1));
4358 SDValue Op2 = getValue(I.getOperand(2));
4359 SDValue Op3 = getValue(I.getOperand(3));
4360 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004361 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4362 I.getOperand(1), 0, I.getOperand(2), 0);
4363 DAG.setRoot(Res);
4364 if (DisableScheduling)
4365 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004366 return 0;
4367 }
Chris Lattner824b9582008-11-21 16:42:48 +00004368 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004369 SDValue Op1 = getValue(I.getOperand(1));
4370 SDValue Op2 = getValue(I.getOperand(2));
4371 SDValue Op3 = getValue(I.getOperand(3));
4372 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004373 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4374 I.getOperand(1), 0);
4375 DAG.setRoot(Res);
4376 if (DisableScheduling)
4377 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004378 return 0;
4379 }
Chris Lattner824b9582008-11-21 16:42:48 +00004380 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004381 SDValue Op1 = getValue(I.getOperand(1));
4382 SDValue Op2 = getValue(I.getOperand(2));
4383 SDValue Op3 = getValue(I.getOperand(3));
4384 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4385
4386 // If the source and destination are known to not be aliases, we can
4387 // lower memmove as memcpy.
4388 uint64_t Size = -1ULL;
4389 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004390 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004391 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4392 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004393 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4394 I.getOperand(1), 0, I.getOperand(2), 0);
4395 DAG.setRoot(Res);
4396 if (DisableScheduling)
4397 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004398 return 0;
4399 }
4400
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004401 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4402 I.getOperand(1), 0, I.getOperand(2), 0);
4403 DAG.setRoot(Res);
4404 if (DisableScheduling)
4405 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004406 return 0;
4407 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004408 case Intrinsic::dbg_stoppoint:
Devang Patel70d75ca2009-11-12 19:02:56 +00004409 case Intrinsic::dbg_region_start:
4410 case Intrinsic::dbg_region_end:
4411 case Intrinsic::dbg_func_start:
4412 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004413 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004414 case Intrinsic::dbg_declare: {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004415 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00004416 // FIXME: Variable debug info is not supported here.
4417 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004418 DwarfWriter *DW = DAG.getDwarfWriter();
4419 if (!DW)
4420 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004421 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00004422 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00004423 return 0;
4424
Devang Patelac1ceb32009-10-09 22:42:28 +00004425 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004426 Value *Address = DI.getAddress();
4427 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4428 Address = BCI->getOperand(0);
4429 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4430 // Don't handle byval struct arguments or VLAs, for example.
4431 if (!AI)
4432 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004433 DenseMap<const AllocaInst*, int>::iterator SI =
4434 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004435 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00004436 return 0; // VLAs.
4437 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004438
Chris Lattner3990b122009-12-28 23:41:32 +00004439 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo())
4440 if (MDNode *Dbg = DI.getMetadata("dbg"))
Chris Lattner0eb41982009-12-28 20:45:51 +00004441 MMI->setVariableDbgInfo(Variable, FI, Dbg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004442 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004443 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004444 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004445 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004446 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004447 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004448 SDValue Ops[1];
4449 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004450 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004451 setValue(&I, Op);
4452 DAG.setRoot(Op.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004453 if (DisableScheduling)
4454 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004455 return 0;
4456 }
4457
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004458 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004459 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004460
Chris Lattner3a5815f2009-09-17 23:54:54 +00004461 if (CurMBB->isLandingPad())
4462 AddCatchInfo(I, MMI, CurMBB);
4463 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004464#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004465 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004466#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004467 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4468 unsigned Reg = TLI.getExceptionSelectorRegister();
4469 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004470 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004471
Chris Lattner3a5815f2009-09-17 23:54:54 +00004472 // Insert the EHSELECTION instruction.
4473 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4474 SDValue Ops[2];
4475 Ops[0] = getValue(I.getOperand(1));
4476 Ops[1] = getRoot();
4477 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4478
4479 DAG.setRoot(Op.getValue(1));
4480
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004481 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4482 setValue(&I, Res);
4483 if (DisableScheduling) {
4484 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4485 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4486 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004487 return 0;
4488 }
4489
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004490 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004491 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004492
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004493 if (MMI) {
4494 // Find the type id for the given typeinfo.
4495 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004496 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004497 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004498 } else {
4499 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004500 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004501 }
4502
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004503 setValue(&I, Res);
4504 if (DisableScheduling)
4505 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004506 return 0;
4507 }
4508
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004509 case Intrinsic::eh_return_i32:
4510 case Intrinsic::eh_return_i64:
4511 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004512 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004513 Res = DAG.getNode(ISD::EH_RETURN, dl,
4514 MVT::Other,
4515 getControlRoot(),
4516 getValue(I.getOperand(1)),
4517 getValue(I.getOperand(2)));
4518 DAG.setRoot(Res);
4519 if (DisableScheduling)
4520 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004521 } else {
4522 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4523 }
4524
4525 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004526 case Intrinsic::eh_unwind_init:
4527 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4528 MMI->setCallsUnwindInit(true);
4529 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004530 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004531 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004532 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004533 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4534 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004535 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004536 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004537 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004538 TLI.getPointerTy()),
4539 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004540 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004541 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004542 DAG.getConstant(0, TLI.getPointerTy()));
4543 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4544 FA, Offset);
4545 setValue(&I, Res);
4546 if (DisableScheduling) {
4547 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4548 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4549 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4550 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4551 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004552 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004553 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004554 case Intrinsic::convertff:
4555 case Intrinsic::convertfsi:
4556 case Intrinsic::convertfui:
4557 case Intrinsic::convertsif:
4558 case Intrinsic::convertuif:
4559 case Intrinsic::convertss:
4560 case Intrinsic::convertsu:
4561 case Intrinsic::convertus:
4562 case Intrinsic::convertuu: {
4563 ISD::CvtCode Code = ISD::CVT_INVALID;
4564 switch (Intrinsic) {
4565 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4566 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4567 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4568 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4569 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4570 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4571 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4572 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4573 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4574 }
Owen Andersone50ed302009-08-10 22:56:29 +00004575 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004576 Value *Op1 = I.getOperand(1);
4577 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4578 DAG.getValueType(DestVT),
4579 DAG.getValueType(getValue(Op1).getValueType()),
4580 getValue(I.getOperand(2)),
4581 getValue(I.getOperand(3)),
4582 Code);
4583 setValue(&I, Res);
4584 if (DisableScheduling)
4585 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004586 return 0;
4587 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004588 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004589 Res = DAG.getNode(ISD::FSQRT, dl,
4590 getValue(I.getOperand(1)).getValueType(),
4591 getValue(I.getOperand(1)));
4592 setValue(&I, Res);
4593 if (DisableScheduling)
4594 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004595 return 0;
4596 case Intrinsic::powi:
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004597 Res = ExpandPowI(dl, getValue(I.getOperand(1)), getValue(I.getOperand(2)),
4598 DAG);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004599 setValue(&I, Res);
4600 if (DisableScheduling)
4601 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004602 return 0;
4603 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004604 Res = DAG.getNode(ISD::FSIN, dl,
4605 getValue(I.getOperand(1)).getValueType(),
4606 getValue(I.getOperand(1)));
4607 setValue(&I, Res);
4608 if (DisableScheduling)
4609 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004610 return 0;
4611 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004612 Res = DAG.getNode(ISD::FCOS, dl,
4613 getValue(I.getOperand(1)).getValueType(),
4614 getValue(I.getOperand(1)));
4615 setValue(&I, Res);
4616 if (DisableScheduling)
4617 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004618 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004619 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004620 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004621 return 0;
4622 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004623 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004624 return 0;
4625 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004626 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004627 return 0;
4628 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004629 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004630 return 0;
4631 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004632 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004633 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004634 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004635 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004636 return 0;
4637 case Intrinsic::pcmarker: {
4638 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004639 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4640 DAG.setRoot(Res);
4641 if (DisableScheduling)
4642 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004643 return 0;
4644 }
4645 case Intrinsic::readcyclecounter: {
4646 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004647 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4648 DAG.getVTList(MVT::i64, MVT::Other),
4649 &Op, 1);
4650 setValue(&I, Res);
4651 DAG.setRoot(Res.getValue(1));
4652 if (DisableScheduling)
4653 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004654 return 0;
4655 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004657 Res = DAG.getNode(ISD::BSWAP, dl,
4658 getValue(I.getOperand(1)).getValueType(),
4659 getValue(I.getOperand(1)));
4660 setValue(&I, Res);
4661 if (DisableScheduling)
4662 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004663 return 0;
4664 case Intrinsic::cttz: {
4665 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004666 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004667 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4668 setValue(&I, Res);
4669 if (DisableScheduling)
4670 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004671 return 0;
4672 }
4673 case Intrinsic::ctlz: {
4674 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004675 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004676 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4677 setValue(&I, Res);
4678 if (DisableScheduling)
4679 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004680 return 0;
4681 }
4682 case Intrinsic::ctpop: {
4683 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004684 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004685 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4686 setValue(&I, Res);
4687 if (DisableScheduling)
4688 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004689 return 0;
4690 }
4691 case Intrinsic::stacksave: {
4692 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004693 Res = DAG.getNode(ISD::STACKSAVE, dl,
4694 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4695 setValue(&I, Res);
4696 DAG.setRoot(Res.getValue(1));
4697 if (DisableScheduling)
4698 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004699 return 0;
4700 }
4701 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004702 Res = getValue(I.getOperand(1));
4703 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4704 DAG.setRoot(Res);
4705 if (DisableScheduling)
4706 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004707 return 0;
4708 }
Bill Wendling57344502008-11-18 11:01:33 +00004709 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004710 // Emit code into the DAG to store the stack guard onto the stack.
4711 MachineFunction &MF = DAG.getMachineFunction();
4712 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004713 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004714
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004715 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4716 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004717
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004718 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004719 MFI->setStackProtectorIndex(FI);
4720
4721 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4722
4723 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004724 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4725 PseudoSourceValue::getFixedStack(FI),
4726 0, true);
4727 setValue(&I, Res);
4728 DAG.setRoot(Res);
4729 if (DisableScheduling)
4730 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004731 return 0;
4732 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004733 case Intrinsic::objectsize: {
4734 // If we don't know by now, we're never going to know.
4735 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4736
4737 assert(CI && "Non-constant type in __builtin_object_size?");
4738
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004739 SDValue Arg = getValue(I.getOperand(0));
4740 EVT Ty = Arg.getValueType();
4741
Eric Christopherd060b252009-12-23 02:51:48 +00004742 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004743 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004744 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004745 Res = DAG.getConstant(0, Ty);
4746
4747 setValue(&I, Res);
4748 if (DisableScheduling)
4749 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004750 return 0;
4751 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004752 case Intrinsic::var_annotation:
4753 // Discard annotate attributes
4754 return 0;
4755
4756 case Intrinsic::init_trampoline: {
4757 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4758
4759 SDValue Ops[6];
4760 Ops[0] = getRoot();
4761 Ops[1] = getValue(I.getOperand(1));
4762 Ops[2] = getValue(I.getOperand(2));
4763 Ops[3] = getValue(I.getOperand(3));
4764 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4765 Ops[5] = DAG.getSrcValue(F);
4766
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004767 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4768 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4769 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004770
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004771 setValue(&I, Res);
4772 DAG.setRoot(Res.getValue(1));
4773 if (DisableScheduling)
4774 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004775 return 0;
4776 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004777 case Intrinsic::gcroot:
4778 if (GFI) {
4779 Value *Alloca = I.getOperand(1);
4780 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004781
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004782 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4783 GFI->addStackRoot(FI->getIndex(), TypeMap);
4784 }
4785 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004786 case Intrinsic::gcread:
4787 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004788 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004789 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004790 case Intrinsic::flt_rounds:
4791 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4792 setValue(&I, Res);
4793 if (DisableScheduling)
4794 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004795 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004796 case Intrinsic::trap:
4797 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4798 DAG.setRoot(Res);
4799 if (DisableScheduling)
4800 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004801 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004802 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004803 return implVisitAluOverflow(I, ISD::UADDO);
4804 case Intrinsic::sadd_with_overflow:
4805 return implVisitAluOverflow(I, ISD::SADDO);
4806 case Intrinsic::usub_with_overflow:
4807 return implVisitAluOverflow(I, ISD::USUBO);
4808 case Intrinsic::ssub_with_overflow:
4809 return implVisitAluOverflow(I, ISD::SSUBO);
4810 case Intrinsic::umul_with_overflow:
4811 return implVisitAluOverflow(I, ISD::UMULO);
4812 case Intrinsic::smul_with_overflow:
4813 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004814
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004815 case Intrinsic::prefetch: {
4816 SDValue Ops[4];
4817 Ops[0] = getRoot();
4818 Ops[1] = getValue(I.getOperand(1));
4819 Ops[2] = getValue(I.getOperand(2));
4820 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004821 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4822 DAG.setRoot(Res);
4823 if (DisableScheduling)
4824 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004825 return 0;
4826 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 case Intrinsic::memory_barrier: {
4829 SDValue Ops[6];
4830 Ops[0] = getRoot();
4831 for (int x = 1; x < 6; ++x)
4832 Ops[x] = getValue(I.getOperand(x));
4833
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004834 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4835 DAG.setRoot(Res);
4836 if (DisableScheduling)
4837 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004838 return 0;
4839 }
4840 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004841 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004842 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004843 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004844 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4845 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004846 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004847 getValue(I.getOperand(2)),
4848 getValue(I.getOperand(3)),
4849 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004850 setValue(&I, L);
4851 DAG.setRoot(L.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004852 if (DisableScheduling)
4853 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004854 return 0;
4855 }
4856 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004857 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004858 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004859 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004860 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004861 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004862 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004863 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004864 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004865 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004866 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004867 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004868 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004869 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004870 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004871 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004872 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004873 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004874 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004875 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004876 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004877 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004878
4879 case Intrinsic::invariant_start:
4880 case Intrinsic::lifetime_start:
4881 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004882 Res = DAG.getUNDEF(TLI.getPointerTy());
4883 setValue(&I, Res);
4884 if (DisableScheduling)
4885 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004886 return 0;
4887 case Intrinsic::invariant_end:
4888 case Intrinsic::lifetime_end:
4889 // Discard region information.
4890 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004891 }
4892}
4893
Dan Gohman98ca4f22009-08-05 01:29:28 +00004894/// Test if the given instruction is in a position to be optimized
4895/// with a tail-call. This roughly means that it's in a block with
4896/// a return and there's nothing that needs to be scheduled
4897/// between it and the return.
4898///
4899/// This function only tests target-independent requirements.
4900/// For target-dependent requirements, a target should override
4901/// TargetLowering::IsEligibleForTailCallOptimization.
4902///
4903static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004904isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004905 const TargetLowering &TLI) {
4906 const BasicBlock *ExitBB = I->getParent();
4907 const TerminatorInst *Term = ExitBB->getTerminator();
4908 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4909 const Function *F = ExitBB->getParent();
4910
4911 // The block must end in a return statement or an unreachable.
4912 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4913
4914 // If I will have a chain, make sure no other instruction that will have a
4915 // chain interposes between I and the return.
4916 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4917 !I->isSafeToSpeculativelyExecute())
4918 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4919 --BBI) {
4920 if (&*BBI == I)
4921 break;
4922 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4923 !BBI->isSafeToSpeculativelyExecute())
4924 return false;
4925 }
4926
4927 // If the block ends with a void return or unreachable, it doesn't matter
4928 // what the call's return type is.
4929 if (!Ret || Ret->getNumOperands() == 0) return true;
4930
Dan Gohmaned9bab32009-11-14 02:06:30 +00004931 // If the return value is undef, it doesn't matter what the call's
4932 // return type is.
4933 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4934
Dan Gohman98ca4f22009-08-05 01:29:28 +00004935 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004936 // the return. Ignore noalias because it doesn't affect the call sequence.
4937 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4938 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004939 return false;
4940
4941 // Otherwise, make sure the unmodified return value of I is the return value.
4942 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4943 U = dyn_cast<Instruction>(U->getOperand(0))) {
4944 if (!U)
4945 return false;
4946 if (!U->hasOneUse())
4947 return false;
4948 if (U == I)
4949 break;
4950 // Check for a truly no-op truncate.
4951 if (isa<TruncInst>(U) &&
4952 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4953 continue;
4954 // Check for a truly no-op bitcast.
4955 if (isa<BitCastInst>(U) &&
4956 (U->getOperand(0)->getType() == U->getType() ||
4957 (isa<PointerType>(U->getOperand(0)->getType()) &&
4958 isa<PointerType>(U->getType()))))
4959 continue;
4960 // Otherwise it's not a true no-op.
4961 return false;
4962 }
4963
4964 return true;
4965}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004966
Dan Gohman2048b852009-11-23 18:04:58 +00004967void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4968 bool isTailCall,
4969 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004970 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4971 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004972 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004973 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4974 unsigned BeginLabel = 0, EndLabel = 0;
4975
4976 TargetLowering::ArgListTy Args;
4977 TargetLowering::ArgListEntry Entry;
4978 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004979
4980 // Check whether the function can return without sret-demotion.
4981 SmallVector<EVT, 4> OutVTs;
4982 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4983 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004984 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004985 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004986
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004987 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004988 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4989
4990 SDValue DemoteStackSlot;
4991
4992 if (!CanLowerReturn) {
4993 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4994 FTy->getReturnType());
4995 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4996 FTy->getReturnType());
4997 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004998 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004999 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
5000
5001 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
5002 Entry.Node = DemoteStackSlot;
5003 Entry.Ty = StackSlotPtrType;
5004 Entry.isSExt = false;
5005 Entry.isZExt = false;
5006 Entry.isInReg = false;
5007 Entry.isSRet = true;
5008 Entry.isNest = false;
5009 Entry.isByVal = false;
5010 Entry.Alignment = Align;
5011 Args.push_back(Entry);
5012 RetTy = Type::getVoidTy(FTy->getContext());
5013 }
5014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005015 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005016 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005017 SDValue ArgNode = getValue(*i);
5018 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
5019
5020 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00005021 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5022 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5023 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5024 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5025 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5026 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005027 Entry.Alignment = CS.getParamAlignment(attrInd);
5028 Args.push_back(Entry);
5029 }
5030
5031 if (LandingPad && MMI) {
5032 // Insert a label before the invoke call to mark the try range. This can be
5033 // used to detect deletion of the invoke via the MachineModuleInfo.
5034 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005035
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005036 // Both PendingLoads and PendingExports must be flushed here;
5037 // this call might not return.
5038 (void)getRoot();
Bill Wendling0d580132009-12-23 01:28:19 +00005039 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5040 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005041 }
5042
Dan Gohman98ca4f22009-08-05 01:29:28 +00005043 // Check if target-independent constraints permit a tail call here.
5044 // Target-dependent constraints are checked within TLI.LowerCallTo.
5045 if (isTailCall &&
5046 !isInTailCallPosition(CS.getInstruction(),
5047 CS.getAttributes().getRetAttributes(),
5048 TLI))
5049 isTailCall = false;
5050
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005051 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005052 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00005053 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005054 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005055 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005056 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00005057 isTailCall,
5058 !CS.getInstruction()->use_empty(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005059 Callee, Args, DAG, getCurDebugLoc(), SDNodeOrder);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005060 assert((isTailCall || Result.second.getNode()) &&
5061 "Non-null chain expected with non-tail call!");
5062 assert((Result.second.getNode() || !Result.first.getNode()) &&
5063 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005064 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005065 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005066 if (DisableScheduling)
5067 DAG.AssignOrdering(Result.first.getNode(), SDNodeOrder);
5068 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005069 // The instruction result is the result of loading from the
5070 // hidden sret parameter.
5071 SmallVector<EVT, 1> PVTs;
5072 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
5073
5074 ComputeValueVTs(TLI, PtrRetTy, PVTs);
5075 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5076 EVT PtrVT = PVTs[0];
5077 unsigned NumValues = OutVTs.size();
5078 SmallVector<SDValue, 4> Values(NumValues);
5079 SmallVector<SDValue, 4> Chains(NumValues);
5080
5081 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00005082 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
5083 DemoteStackSlot,
5084 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005085 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005086 Add, NULL, Offsets[i], false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005087 Values[i] = L;
5088 Chains[i] = L.getValue(1);
5089 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005090
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005091 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
5092 MVT::Other, &Chains[0], NumValues);
5093 PendingLoads.push_back(Chain);
5094
Bill Wendlinge80ae832009-12-22 00:50:32 +00005095 SDValue MV = DAG.getNode(ISD::MERGE_VALUES,
5096 getCurDebugLoc(),
5097 DAG.getVTList(&OutVTs[0], NumValues),
5098 &Values[0], NumValues);
5099 setValue(CS.getInstruction(), MV);
5100
5101 if (DisableScheduling) {
5102 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
5103 DAG.AssignOrdering(MV.getNode(), SDNodeOrder);
5104 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005105 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005106
5107 // As a special case, a null chain means that a tail call has been emitted and
5108 // the DAG root is already updated.
5109 if (Result.second.getNode()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005110 DAG.setRoot(Result.second);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005111 if (DisableScheduling)
5112 DAG.AssignOrdering(Result.second.getNode(), SDNodeOrder);
5113 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005114 HasTailCall = true;
Bill Wendlinge80ae832009-12-22 00:50:32 +00005115 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005116
5117 if (LandingPad && MMI) {
5118 // Insert a label at the end of the invoke call to mark the try range. This
5119 // can be used to detect deletion of the invoke via the MachineModuleInfo.
5120 EndLabel = MMI->NextLabelID();
Bill Wendling0d580132009-12-23 01:28:19 +00005121 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5122 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005123
5124 // Inform MachineModuleInfo of range.
5125 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
5126 }
5127}
5128
Chris Lattner8047d9a2009-12-24 00:37:38 +00005129/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5130/// value is equal or not-equal to zero.
5131static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
5132 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
5133 UI != E; ++UI) {
5134 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
5135 if (IC->isEquality())
5136 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
5137 if (C->isNullValue())
5138 continue;
5139 // Unknown instruction.
5140 return false;
5141 }
5142 return true;
5143}
5144
Chris Lattner04b091a2009-12-24 01:07:17 +00005145static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005146 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005147
Chris Lattner8047d9a2009-12-24 00:37:38 +00005148 // Check to see if this load can be trivially constant folded, e.g. if the
5149 // input is from a string literal.
5150 if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
5151 // Cast pointer to the type we really want to load.
5152 LoadInput = ConstantExpr::getBitCast(LoadInput,
5153 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005154
Chris Lattner8047d9a2009-12-24 00:37:38 +00005155 if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD))
5156 return Builder.getValue(LoadCst);
5157 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005158
Chris Lattner8047d9a2009-12-24 00:37:38 +00005159 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5160 // still constant memory, the input chain can be the entry node.
5161 SDValue Root;
5162 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005163
Chris Lattner8047d9a2009-12-24 00:37:38 +00005164 // Do not serialize (non-volatile) loads of constant memory with anything.
5165 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5166 Root = Builder.DAG.getEntryNode();
5167 ConstantMemory = true;
5168 } else {
5169 // Do not serialize non-volatile loads against each other.
5170 Root = Builder.DAG.getRoot();
5171 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005172
Chris Lattner8047d9a2009-12-24 00:37:38 +00005173 SDValue Ptr = Builder.getValue(PtrVal);
5174 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
5175 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
5176 false /*volatile*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005177
Chris Lattner8047d9a2009-12-24 00:37:38 +00005178 if (!ConstantMemory)
5179 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5180 return LoadVal;
5181}
5182
5183
5184/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5185/// If so, return true and lower it, otherwise return false and it will be
5186/// lowered like a normal call.
5187bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) {
5188 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
5189 if (I.getNumOperands() != 4)
5190 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005191
Chris Lattner8047d9a2009-12-24 00:37:38 +00005192 Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
5193 if (!isa<PointerType>(LHS->getType()) || !isa<PointerType>(RHS->getType()) ||
5194 !isa<IntegerType>(I.getOperand(3)->getType()) ||
5195 !isa<IntegerType>(I.getType()))
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005196 return false;
5197
Chris Lattner8047d9a2009-12-24 00:37:38 +00005198 ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005199
Chris Lattner8047d9a2009-12-24 00:37:38 +00005200 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5201 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005202 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5203 bool ActuallyDoIt = true;
5204 MVT LoadVT;
5205 const Type *LoadTy;
5206 switch (Size->getZExtValue()) {
5207 default:
5208 LoadVT = MVT::Other;
5209 LoadTy = 0;
5210 ActuallyDoIt = false;
5211 break;
5212 case 2:
5213 LoadVT = MVT::i16;
5214 LoadTy = Type::getInt16Ty(Size->getContext());
5215 break;
5216 case 4:
5217 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005218 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005219 break;
5220 case 8:
5221 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005222 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005223 break;
5224 /*
5225 case 16:
5226 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005227 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005228 LoadTy = VectorType::get(LoadTy, 4);
5229 break;
5230 */
5231 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005232
Chris Lattner04b091a2009-12-24 01:07:17 +00005233 // This turns into unaligned loads. We only do this if the target natively
5234 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5235 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005236
Chris Lattner04b091a2009-12-24 01:07:17 +00005237 // Require that we can find a legal MVT, and only do this if the target
5238 // supports unaligned loads of that type. Expanding into byte loads would
5239 // bloat the code.
5240 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5241 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5242 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5243 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5244 ActuallyDoIt = false;
5245 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005246
Chris Lattner04b091a2009-12-24 01:07:17 +00005247 if (ActuallyDoIt) {
5248 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5249 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005250
Chris Lattner04b091a2009-12-24 01:07:17 +00005251 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
5252 ISD::SETNE);
5253 EVT CallVT = TLI.getValueType(I.getType(), true);
5254 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
5255 return true;
5256 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005257 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005258
5259
Chris Lattner8047d9a2009-12-24 00:37:38 +00005260 return false;
5261}
5262
5263
Dan Gohman2048b852009-11-23 18:04:58 +00005264void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005265 const char *RenameFn = 0;
5266 if (Function *F = I.getCalledFunction()) {
5267 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005268 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
5269 if (II) {
5270 if (unsigned IID = II->getIntrinsicID(F)) {
5271 RenameFn = visitIntrinsicCall(I, IID);
5272 if (!RenameFn)
5273 return;
5274 }
5275 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005276 if (unsigned IID = F->getIntrinsicID()) {
5277 RenameFn = visitIntrinsicCall(I, IID);
5278 if (!RenameFn)
5279 return;
5280 }
5281 }
5282
5283 // Check for well-known libc/libm calls. If the function is internal, it
5284 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005285 if (!F->hasLocalLinkage() && F->hasName()) {
5286 StringRef Name = F->getName();
5287 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005288 if (I.getNumOperands() == 3 && // Basic sanity checks.
5289 I.getOperand(1)->getType()->isFloatingPoint() &&
5290 I.getType() == I.getOperand(1)->getType() &&
5291 I.getType() == I.getOperand(2)->getType()) {
5292 SDValue LHS = getValue(I.getOperand(1));
5293 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00005294 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5295 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005296 return;
5297 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005298 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005299 if (I.getNumOperands() == 2 && // Basic sanity checks.
5300 I.getOperand(1)->getType()->isFloatingPoint() &&
5301 I.getType() == I.getOperand(1)->getType()) {
5302 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005303 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
5304 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005305 return;
5306 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005307 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005308 if (I.getNumOperands() == 2 && // Basic sanity checks.
5309 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005310 I.getType() == I.getOperand(1)->getType() &&
5311 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005313 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5314 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005315 return;
5316 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005317 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005318 if (I.getNumOperands() == 2 && // Basic sanity checks.
5319 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005320 I.getType() == I.getOperand(1)->getType() &&
5321 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005322 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005323 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5324 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325 return;
5326 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005327 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5328 if (I.getNumOperands() == 2 && // Basic sanity checks.
5329 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005330 I.getType() == I.getOperand(1)->getType() &&
5331 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005332 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005333 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5334 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005335 return;
5336 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005337 } else if (Name == "memcmp") {
5338 if (visitMemCmpCall(I))
5339 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005340 }
5341 }
5342 } else if (isa<InlineAsm>(I.getOperand(0))) {
5343 visitInlineAsm(&I);
5344 return;
5345 }
5346
5347 SDValue Callee;
5348 if (!RenameFn)
5349 Callee = getValue(I.getOperand(0));
5350 else
Bill Wendling056292f2008-09-16 21:48:12 +00005351 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005352
Bill Wendling0d580132009-12-23 01:28:19 +00005353 // Check if we can potentially perform a tail call. More detailed checking is
5354 // be done within LowerCallTo, after more information about the call is known.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005355 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5356
5357 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005358}
5359
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005360/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005361/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005362/// Chain/Flag as the input and updates them for the output Chain/Flag.
5363/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005364SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005365 unsigned Order, SDValue &Chain,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005366 SDValue *Flag) const {
5367 // Assemble the legal parts into the final values.
5368 SmallVector<SDValue, 4> Values(ValueVTs.size());
5369 SmallVector<SDValue, 8> Parts;
5370 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5371 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005372 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005373 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005374 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005375
5376 Parts.resize(NumRegs);
5377 for (unsigned i = 0; i != NumRegs; ++i) {
5378 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00005379 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005380 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005381 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005382 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005383 *Flag = P.getValue(2);
5384 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005385
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005386 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005387
Bill Wendlingec72e322009-12-22 01:11:43 +00005388 if (DisableScheduling)
5389 DAG.AssignOrdering(P.getNode(), Order);
5390
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005391 // If the source register was virtual and if we know something about it,
5392 // add an assert node.
5393 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5394 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5395 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5396 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5397 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5398 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005399
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005400 unsigned RegSize = RegisterVT.getSizeInBits();
5401 unsigned NumSignBits = LOI.NumSignBits;
5402 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005403
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005404 // FIXME: We capture more information than the dag can represent. For
5405 // now, just use the tightest assertzext/assertsext possible.
5406 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005407 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005408 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005409 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005410 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005411 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005412 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005413 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005414 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005415 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005416 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005417 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005418 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005419 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005420 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005421 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005422 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005423 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005424
Owen Anderson825b72b2009-08-11 20:47:22 +00005425 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005426 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005427 RegisterVT, P, DAG.getValueType(FromVT));
5428
Bill Wendlingec72e322009-12-22 01:11:43 +00005429 if (DisableScheduling)
5430 DAG.AssignOrdering(P.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005431 }
5432 }
5433 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005434
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005435 Parts[i] = P;
5436 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005437
Bill Wendling3ea3c242009-12-22 02:10:19 +00005438 Values[Value] = getCopyFromParts(DAG, dl, Order, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005439 NumRegs, RegisterVT, ValueVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005440 if (DisableScheduling)
5441 DAG.AssignOrdering(Values[Value].getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005442 Part += NumRegs;
5443 Parts.clear();
5444 }
5445
Bill Wendlingec72e322009-12-22 01:11:43 +00005446 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5447 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5448 &Values[0], ValueVTs.size());
5449 if (DisableScheduling)
5450 DAG.AssignOrdering(Res.getNode(), Order);
5451 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005452}
5453
5454/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005455/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005456/// Chain/Flag as the input and updates them for the output Chain/Flag.
5457/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005458void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005459 unsigned Order, SDValue &Chain,
5460 SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005461 // Get the list of the values's legal parts.
5462 unsigned NumRegs = Regs.size();
5463 SmallVector<SDValue, 8> Parts(NumRegs);
5464 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005465 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005466 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005467 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005468
Bill Wendling3ea3c242009-12-22 02:10:19 +00005469 getCopyToParts(DAG, dl, Order,
5470 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005471 &Parts[Part], NumParts, RegisterVT);
5472 Part += NumParts;
5473 }
5474
5475 // Copy the parts into the registers.
5476 SmallVector<SDValue, 8> Chains(NumRegs);
5477 for (unsigned i = 0; i != NumRegs; ++i) {
5478 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00005479 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005480 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00005481 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005482 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005483 *Flag = Part.getValue(1);
5484 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005485
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005486 Chains[i] = Part.getValue(0);
Bill Wendlingec72e322009-12-22 01:11:43 +00005487
5488 if (DisableScheduling)
5489 DAG.AssignOrdering(Part.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005490 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005491
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005492 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005493 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005494 // flagged to it. That is the CopyToReg nodes and the user are considered
5495 // a single scheduling unit. If we create a TokenFactor and return it as
5496 // chain, then the TokenFactor is both a predecessor (operand) of the
5497 // user as well as a successor (the TF operands are flagged to the user).
5498 // c1, f1 = CopyToReg
5499 // c2, f2 = CopyToReg
5500 // c3 = TokenFactor c1, c2
5501 // ...
5502 // = op c3, ..., f2
5503 Chain = Chains[NumRegs-1];
5504 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005505 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Bill Wendlingec72e322009-12-22 01:11:43 +00005506
5507 if (DisableScheduling)
5508 DAG.AssignOrdering(Chain.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005509}
5510
5511/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005512/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005513/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005514void RegsForValue::AddInlineAsmOperands(unsigned Code,
5515 bool HasMatching,unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +00005516 SelectionDAG &DAG, unsigned Order,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005517 std::vector<SDValue> &Ops) const {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005518 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5519 unsigned Flag = Code | (Regs.size() << 3);
5520 if (HasMatching)
5521 Flag |= 0x80000000 | (MatchingIdx << 16);
Dale Johannesen99499332009-12-23 07:32:51 +00005522 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00005523 Ops.push_back(Res);
5524
5525 if (DisableScheduling)
5526 DAG.AssignOrdering(Res.getNode(), Order);
5527
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005528 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005529 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005530 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005531 for (unsigned i = 0; i != NumRegs; ++i) {
5532 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling651ad132009-12-22 01:25:10 +00005533 SDValue Res = DAG.getRegister(Regs[Reg++], RegisterVT);
5534 Ops.push_back(Res);
5535
5536 if (DisableScheduling)
5537 DAG.AssignOrdering(Res.getNode(), Order);
Chris Lattner58f15c42008-10-17 16:21:11 +00005538 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005539 }
5540}
5541
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005542/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005543/// i.e. it isn't a stack pointer or some other special register, return the
5544/// register class for the register. Otherwise, return null.
5545static const TargetRegisterClass *
5546isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5547 const TargetLowering &TLI,
5548 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005549 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005550 const TargetRegisterClass *FoundRC = 0;
5551 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5552 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005553 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005554
5555 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005556 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005557 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5558 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5559 I != E; ++I) {
5560 if (TLI.isTypeLegal(*I)) {
5561 // If we have already found this register in a different register class,
5562 // choose the one with the largest VT specified. For example, on
5563 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005564 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005565 ThisVT = *I;
5566 break;
5567 }
5568 }
5569 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005570
Owen Anderson825b72b2009-08-11 20:47:22 +00005571 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005572
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005573 // NOTE: This isn't ideal. In particular, this might allocate the
5574 // frame pointer in functions that need it (due to them not being taken
5575 // out of allocation, because a variable sized allocation hasn't been seen
5576 // yet). This is a slight code pessimization, but should still work.
5577 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5578 E = RC->allocation_order_end(MF); I != E; ++I)
5579 if (*I == Reg) {
5580 // We found a matching register class. Keep looking at others in case
5581 // we find one with larger registers that this physreg is also in.
5582 FoundRC = RC;
5583 FoundVT = ThisVT;
5584 break;
5585 }
5586 }
5587 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005588}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005589
5590
5591namespace llvm {
5592/// AsmOperandInfo - This contains information for each constraint that we are
5593/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005594class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005595 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005596public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005597 /// CallOperand - If this is the result output operand or a clobber
5598 /// this is null, otherwise it is the incoming operand to the CallInst.
5599 /// This gets modified as the asm is processed.
5600 SDValue CallOperand;
5601
5602 /// AssignedRegs - If this is a register or register class operand, this
5603 /// contains the set of register corresponding to the operand.
5604 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005605
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005606 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5607 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5608 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005609
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005610 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5611 /// busy in OutputRegs/InputRegs.
5612 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005613 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005614 std::set<unsigned> &InputRegs,
5615 const TargetRegisterInfo &TRI) const {
5616 if (isOutReg) {
5617 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5618 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5619 }
5620 if (isInReg) {
5621 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5622 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5623 }
5624 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005625
Owen Andersone50ed302009-08-10 22:56:29 +00005626 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005627 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005628 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005629 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005630 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005631 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005632 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005633
Chris Lattner81249c92008-10-17 17:05:25 +00005634 if (isa<BasicBlock>(CallOperandVal))
5635 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005636
Chris Lattner81249c92008-10-17 17:05:25 +00005637 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005638
Chris Lattner81249c92008-10-17 17:05:25 +00005639 // If this is an indirect operand, the operand is a pointer to the
5640 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005641 if (isIndirect) {
5642 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5643 if (!PtrTy)
5644 llvm_report_error("Indirect operand for inline asm not a pointer!");
5645 OpTy = PtrTy->getElementType();
5646 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005647
Chris Lattner81249c92008-10-17 17:05:25 +00005648 // If OpTy is not a single value, it may be a struct/union that we
5649 // can tile with integers.
5650 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5651 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5652 switch (BitSize) {
5653 default: break;
5654 case 1:
5655 case 8:
5656 case 16:
5657 case 32:
5658 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005659 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005660 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005661 break;
5662 }
5663 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005664
Chris Lattner81249c92008-10-17 17:05:25 +00005665 return TLI.getValueType(OpTy, true);
5666 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005667
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005668private:
5669 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5670 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005671 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005672 const TargetRegisterInfo &TRI) {
5673 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5674 Regs.insert(Reg);
5675 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5676 for (; *Aliases; ++Aliases)
5677 Regs.insert(*Aliases);
5678 }
5679};
5680} // end llvm namespace.
5681
5682
5683/// GetRegistersForValue - Assign registers (virtual or physical) for the
5684/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005685/// register allocator to handle the assignment process. However, if the asm
5686/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005687/// allocation. This produces generally horrible, but correct, code.
5688///
5689/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005690/// Input and OutputRegs are the set of already allocated physical registers.
5691///
Dan Gohman2048b852009-11-23 18:04:58 +00005692void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005693GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005694 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005695 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005696 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005697
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005698 // Compute whether this value requires an input register, an output register,
5699 // or both.
5700 bool isOutReg = false;
5701 bool isInReg = false;
5702 switch (OpInfo.Type) {
5703 case InlineAsm::isOutput:
5704 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005705
5706 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005707 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005708 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005709 break;
5710 case InlineAsm::isInput:
5711 isInReg = true;
5712 isOutReg = false;
5713 break;
5714 case InlineAsm::isClobber:
5715 isOutReg = true;
5716 isInReg = true;
5717 break;
5718 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005719
5720
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005721 MachineFunction &MF = DAG.getMachineFunction();
5722 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005723
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005724 // If this is a constraint for a single physreg, or a constraint for a
5725 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005726 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005727 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5728 OpInfo.ConstraintVT);
5729
5730 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005731 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005732 // If this is a FP input in an integer register (or visa versa) insert a bit
5733 // cast of the input value. More generally, handle any case where the input
5734 // value disagrees with the register class we plan to stick this in.
5735 if (OpInfo.Type == InlineAsm::isInput &&
5736 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005737 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005738 // types are identical size, use a bitcast to convert (e.g. two differing
5739 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005740 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005741 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005742 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005743 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005744 OpInfo.ConstraintVT = RegVT;
5745 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5746 // If the input is a FP value and we want it in FP registers, do a
5747 // bitcast to the corresponding integer type. This turns an f64 value
5748 // into i64, which can be passed with two i32 values on a 32-bit
5749 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005750 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005751 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005752 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005753 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005754 OpInfo.ConstraintVT = RegVT;
5755 }
Bill Wendling651ad132009-12-22 01:25:10 +00005756
5757 if (DisableScheduling)
5758 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Chris Lattner01426e12008-10-21 00:45:36 +00005759 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005760
Owen Anderson23b9b192009-08-12 00:36:31 +00005761 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005762 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005763
Owen Andersone50ed302009-08-10 22:56:29 +00005764 EVT RegVT;
5765 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005766
5767 // If this is a constraint for a specific physical register, like {r17},
5768 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005769 if (unsigned AssignedReg = PhysReg.first) {
5770 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005771 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005772 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005773
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005774 // Get the actual register value type. This is important, because the user
5775 // may have asked for (e.g.) the AX register in i32 type. We need to
5776 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005777 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005778
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005779 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005780 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005781
5782 // If this is an expanded reference, add the rest of the regs to Regs.
5783 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005784 TargetRegisterClass::iterator I = RC->begin();
5785 for (; *I != AssignedReg; ++I)
5786 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005787
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005788 // Already added the first reg.
5789 --NumRegs; ++I;
5790 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005791 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005792 Regs.push_back(*I);
5793 }
5794 }
Bill Wendling651ad132009-12-22 01:25:10 +00005795
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005796 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5797 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5798 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5799 return;
5800 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005802 // Otherwise, if this was a reference to an LLVM register class, create vregs
5803 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005804 if (const TargetRegisterClass *RC = PhysReg.second) {
5805 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005806 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005807 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005808
Evan Chengfb112882009-03-23 08:01:15 +00005809 // Create the appropriate number of virtual registers.
5810 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5811 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005812 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005813
Evan Chengfb112882009-03-23 08:01:15 +00005814 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5815 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005816 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005817
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005818 // This is a reference to a register class that doesn't directly correspond
5819 // to an LLVM register class. Allocate NumRegs consecutive, available,
5820 // registers from the class.
5821 std::vector<unsigned> RegClassRegs
5822 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5823 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005824
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005825 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5826 unsigned NumAllocated = 0;
5827 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5828 unsigned Reg = RegClassRegs[i];
5829 // See if this register is available.
5830 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5831 (isInReg && InputRegs.count(Reg))) { // Already used.
5832 // Make sure we find consecutive registers.
5833 NumAllocated = 0;
5834 continue;
5835 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005836
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005837 // Check to see if this register is allocatable (i.e. don't give out the
5838 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005839 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5840 if (!RC) { // Couldn't allocate this register.
5841 // Reset NumAllocated to make sure we return consecutive registers.
5842 NumAllocated = 0;
5843 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005844 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005845
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005846 // Okay, this register is good, we can use it.
5847 ++NumAllocated;
5848
5849 // If we allocated enough consecutive registers, succeed.
5850 if (NumAllocated == NumRegs) {
5851 unsigned RegStart = (i-NumAllocated)+1;
5852 unsigned RegEnd = i+1;
5853 // Mark all of the allocated registers used.
5854 for (unsigned i = RegStart; i != RegEnd; ++i)
5855 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005856
5857 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005858 OpInfo.ConstraintVT);
5859 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5860 return;
5861 }
5862 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005863
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005864 // Otherwise, we couldn't allocate enough registers for this.
5865}
5866
Evan Chengda43bcf2008-09-24 00:05:32 +00005867/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5868/// processed uses a memory 'm' constraint.
5869static bool
5870hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005871 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005872 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5873 InlineAsm::ConstraintInfo &CI = CInfos[i];
5874 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5875 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5876 if (CType == TargetLowering::C_Memory)
5877 return true;
5878 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005879
Chris Lattner6c147292009-04-30 00:48:50 +00005880 // Indirect operand accesses access memory.
5881 if (CI.isIndirect)
5882 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005883 }
5884
5885 return false;
5886}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005887
5888/// visitInlineAsm - Handle a call to an InlineAsm object.
5889///
Dan Gohman2048b852009-11-23 18:04:58 +00005890void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005891 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5892
5893 /// ConstraintOperands - Information about all of the constraints.
5894 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005895
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005896 std::set<unsigned> OutputRegs, InputRegs;
5897
5898 // Do a prepass over the constraints, canonicalizing them, and building up the
5899 // ConstraintOperands list.
5900 std::vector<InlineAsm::ConstraintInfo>
5901 ConstraintInfos = IA->ParseConstraints();
5902
Evan Chengda43bcf2008-09-24 00:05:32 +00005903 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005904
Chris Lattner6c147292009-04-30 00:48:50 +00005905 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005906
Chris Lattner6c147292009-04-30 00:48:50 +00005907 // We won't need to flush pending loads if this asm doesn't touch
5908 // memory and is nonvolatile.
5909 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005910 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005911 else
5912 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005914 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5915 unsigned ResNo = 0; // ResNo - The result number of the next output.
5916 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5917 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5918 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005919
Owen Anderson825b72b2009-08-11 20:47:22 +00005920 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005921
5922 // Compute the value type for each operand.
5923 switch (OpInfo.Type) {
5924 case InlineAsm::isOutput:
5925 // Indirect outputs just consume an argument.
5926 if (OpInfo.isIndirect) {
5927 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5928 break;
5929 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005930
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005931 // The return value of the call is this value. As such, there is no
5932 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005933 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5934 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005935 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5936 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5937 } else {
5938 assert(ResNo == 0 && "Asm only has one result!");
5939 OpVT = TLI.getValueType(CS.getType());
5940 }
5941 ++ResNo;
5942 break;
5943 case InlineAsm::isInput:
5944 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5945 break;
5946 case InlineAsm::isClobber:
5947 // Nothing to do.
5948 break;
5949 }
5950
5951 // If this is an input or an indirect output, process the call argument.
5952 // BasicBlocks are labels, currently appearing only in asm's.
5953 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005954 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005955 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5956
Chris Lattner81249c92008-10-17 17:05:25 +00005957 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005958 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005959 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005960 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005961 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005962
Owen Anderson1d0be152009-08-13 21:58:54 +00005963 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005964 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005965
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005966 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005967 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005968
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005969 // Second pass over the constraints: compute which constraint option to use
5970 // and assign registers to constraints that want a specific physreg.
5971 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5972 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005973
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005974 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005975 // matching input. If their types mismatch, e.g. one is an integer, the
5976 // other is floating point, or their sizes are different, flag it as an
5977 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005978 if (OpInfo.hasMatchingInput()) {
5979 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5980 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005981 if ((OpInfo.ConstraintVT.isInteger() !=
5982 Input.ConstraintVT.isInteger()) ||
5983 (OpInfo.ConstraintVT.getSizeInBits() !=
5984 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005985 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005986 " with a matching output constraint of incompatible"
5987 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005988 }
5989 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005990 }
5991 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005992
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005993 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005994 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005995
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005996 // If this is a memory input, and if the operand is not indirect, do what we
5997 // need to to provide an address for the memory input.
5998 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5999 !OpInfo.isIndirect) {
6000 assert(OpInfo.Type == InlineAsm::isInput &&
6001 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006002
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003 // Memory operands really want the address of the value. If we don't have
6004 // an indirect input, put it in the constpool if we can, otherwise spill
6005 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006006
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006007 // If the operand is a float, integer, or vector constant, spill to a
6008 // constant pool entry to get its address.
6009 Value *OpVal = OpInfo.CallOperandVal;
6010 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
6011 isa<ConstantVector>(OpVal)) {
6012 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
6013 TLI.getPointerTy());
6014 } else {
6015 // Otherwise, create a stack slot and emit a store to it before the
6016 // asm.
6017 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00006018 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006019 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
6020 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006021 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006022 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00006023 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006024 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006025 OpInfo.CallOperand = StackSlot;
6026 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006027
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006028 // There is no longer a Value* corresponding to this operand.
6029 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006031 // It is now an indirect operand.
6032 OpInfo.isIndirect = true;
6033 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006035 // If this constraint is for a specific register, allocate it before
6036 // anything else.
6037 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00006038 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006039 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006040
Bill Wendling651ad132009-12-22 01:25:10 +00006041 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006042
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006043 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006044 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006045 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6046 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006047
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006048 // C_Register operands have already been allocated, Other/Memory don't need
6049 // to be.
6050 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00006051 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006052 }
6053
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006054 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6055 std::vector<SDValue> AsmNodeOperands;
6056 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6057 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00006058 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006059
6060
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006061 // Loop over all of the inputs, copying the operand values into the
6062 // appropriate registers and processing the output regs.
6063 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006064
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006065 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6066 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006067
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006068 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6069 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6070
6071 switch (OpInfo.Type) {
6072 case InlineAsm::isOutput: {
6073 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6074 OpInfo.ConstraintType != TargetLowering::C_Register) {
6075 // Memory output, or 'other' output (e.g. 'X' constraint).
6076 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6077
6078 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006079 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6080 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081 TLI.getPointerTy()));
6082 AsmNodeOperands.push_back(OpInfo.CallOperand);
6083 break;
6084 }
6085
6086 // Otherwise, this is a register or register class output.
6087
6088 // Copy the output from the appropriate register. Find a register that
6089 // we can use.
6090 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006091 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006092 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006093 }
6094
6095 // If this is an indirect operand, store through the pointer after the
6096 // asm.
6097 if (OpInfo.isIndirect) {
6098 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6099 OpInfo.CallOperandVal));
6100 } else {
6101 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00006102 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
6103 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006104 // Concatenate this output onto the outputs list.
6105 RetValRegs.append(OpInfo.AssignedRegs);
6106 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006107
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006108 // Add information to the INLINEASM node to know that this register is
6109 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00006110 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
6111 6 /* EARLYCLOBBER REGDEF */ :
6112 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00006113 false,
6114 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006115 DAG, SDNodeOrder,
6116 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006117 break;
6118 }
6119 case InlineAsm::isInput: {
6120 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006121
Chris Lattner6bdcda32008-10-17 16:47:46 +00006122 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006123 // If this is required to match an output register we have already set,
6124 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006125 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006126
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006127 // Scan until we find the definition we already emitted of this operand.
6128 // When we find it, create a RegsForValue operand.
6129 unsigned CurOp = 2; // The first operand.
6130 for (; OperandNo; --OperandNo) {
6131 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006132 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006133 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006134 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
6135 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
6136 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006137 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006138 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006139 }
6140
Evan Cheng697cbbf2009-03-20 18:03:34 +00006141 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006142 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006143 if ((OpFlag & 7) == 2 /*REGDEF*/
6144 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
6145 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00006146 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006147 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00006148 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00006149 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006150 RegsForValue MatchedRegs;
6151 MatchedRegs.TLI = &TLI;
6152 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00006153 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006154 MatchedRegs.RegVTs.push_back(RegVT);
6155 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006156 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00006157 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006158 MatchedRegs.Regs.push_back
6159 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006160
6161 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00006162 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006163 SDNodeOrder, Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00006164 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
6165 true, OpInfo.getMatchedOperand(),
Bill Wendling651ad132009-12-22 01:25:10 +00006166 DAG, SDNodeOrder, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006167 break;
6168 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006169 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
6170 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
6171 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006172 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00006173 // See InlineAsm.h isUseOperandTiedToDef.
6174 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00006175 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006176 TLI.getPointerTy()));
6177 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6178 break;
6179 }
6180 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006181
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006182 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006183 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006184 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006185
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006186 std::vector<SDValue> Ops;
6187 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00006188 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006189 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006190 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00006191 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006192 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006193
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006194 // Add information to the INLINEASM node to know about this input.
6195 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006196 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006197 TLI.getPointerTy()));
6198 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6199 break;
6200 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
6201 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
6202 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6203 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006204
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006205 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006206 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6207 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006208 TLI.getPointerTy()));
6209 AsmNodeOperands.push_back(InOperandVal);
6210 break;
6211 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006212
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006213 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6214 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6215 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006216 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006217 "Don't know how to handle indirect register inputs yet!");
6218
6219 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00006220 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006221 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006222 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00006223 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006224
Dale Johannesen66978ee2009-01-31 02:22:37 +00006225 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006226 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006227
Evan Cheng697cbbf2009-03-20 18:03:34 +00006228 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006229 DAG, SDNodeOrder,
6230 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006231 break;
6232 }
6233 case InlineAsm::isClobber: {
6234 // Add the clobbered value to the operand list, so that the register
6235 // allocator is aware that the physreg got clobbered.
6236 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00006237 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Bill Wendling651ad132009-12-22 01:25:10 +00006238 false, 0, DAG, SDNodeOrder,
6239 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006240 break;
6241 }
6242 }
6243 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006244
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006245 // Finish up input operands.
6246 AsmNodeOperands[0] = Chain;
6247 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006248
Dale Johannesen66978ee2009-01-31 02:22:37 +00006249 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006250 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006251 &AsmNodeOperands[0], AsmNodeOperands.size());
6252 Flag = Chain.getValue(1);
6253
6254 // If this asm returns a register value, copy the result from that register
6255 // and set it as the value of the call.
6256 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006257 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006258 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006259
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006260 // FIXME: Why don't we do this for inline asms with MRVs?
6261 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006262 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006263
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006264 // If any of the results of the inline asm is a vector, it may have the
6265 // wrong width/num elts. This can happen for register classes that can
6266 // contain multiple different value types. The preg or vreg allocated may
6267 // not have the same VT as was expected. Convert it to the right type
6268 // with bit_convert.
6269 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006270 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006271 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006272
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006273 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006274 ResultType.isInteger() && Val.getValueType().isInteger()) {
6275 // If a result value was tied to an input value, the computed result may
6276 // have a wider width than the expected result. Extract the relevant
6277 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006278 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006279 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006280
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006281 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006282 }
Dan Gohman95915732008-10-18 01:03:45 +00006283
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006284 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006285 // Don't need to use this as a chain in this case.
6286 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6287 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006288 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006289
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006290 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006291
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006292 // Process indirect outputs, first output all of the flagged copies out of
6293 // physregs.
6294 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6295 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
6296 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00006297 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006298 SDNodeOrder, Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006299 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00006300
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006301 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006302
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006303 // Emit the non-flagged stores from the physregs.
6304 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006305 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6306 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6307 StoresToEmit[i].first,
6308 getValue(StoresToEmit[i].second),
6309 StoresToEmit[i].second, 0);
6310 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006311 }
6312
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006313 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006314 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006315 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006316
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006317 DAG.setRoot(Chain);
6318}
6319
Dan Gohman2048b852009-11-23 18:04:58 +00006320void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006321 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6322 MVT::Other, getRoot(),
6323 getValue(I.getOperand(1)),
6324 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006325}
6326
Dan Gohman2048b852009-11-23 18:04:58 +00006327void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006328 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6329 getRoot(), getValue(I.getOperand(0)),
6330 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006331 setValue(&I, V);
6332 DAG.setRoot(V.getValue(1));
6333}
6334
Dan Gohman2048b852009-11-23 18:04:58 +00006335void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006336 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6337 MVT::Other, getRoot(),
6338 getValue(I.getOperand(1)),
6339 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006340}
6341
Dan Gohman2048b852009-11-23 18:04:58 +00006342void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006343 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6344 MVT::Other, getRoot(),
6345 getValue(I.getOperand(1)),
6346 getValue(I.getOperand(2)),
6347 DAG.getSrcValue(I.getOperand(1)),
6348 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006349}
6350
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006351/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006352/// implementation, which just calls LowerCall.
6353/// FIXME: When all targets are
6354/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006355std::pair<SDValue, SDValue>
6356TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6357 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006358 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006359 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006360 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006361 SDValue Callee,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006362 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl,
6363 unsigned Order) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00006364 assert((!isTailCall || PerformTailCallOpt) &&
6365 "isTailCall set when tail-call optimizations are disabled!");
6366
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006367 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006368 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006369 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006370 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006371 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6372 for (unsigned Value = 0, NumValues = ValueVTs.size();
6373 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006374 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006375 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006376 SDValue Op = SDValue(Args[i].Node.getNode(),
6377 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006378 ISD::ArgFlagsTy Flags;
6379 unsigned OriginalAlignment =
6380 getTargetData()->getABITypeAlignment(ArgTy);
6381
6382 if (Args[i].isZExt)
6383 Flags.setZExt();
6384 if (Args[i].isSExt)
6385 Flags.setSExt();
6386 if (Args[i].isInReg)
6387 Flags.setInReg();
6388 if (Args[i].isSRet)
6389 Flags.setSRet();
6390 if (Args[i].isByVal) {
6391 Flags.setByVal();
6392 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6393 const Type *ElementTy = Ty->getElementType();
6394 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006395 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006396 // For ByVal, alignment should come from FE. BE will guess if this
6397 // info is not there but there are cases it cannot get right.
6398 if (Args[i].Alignment)
6399 FrameAlign = Args[i].Alignment;
6400 Flags.setByValAlign(FrameAlign);
6401 Flags.setByValSize(FrameSize);
6402 }
6403 if (Args[i].isNest)
6404 Flags.setNest();
6405 Flags.setOrigAlign(OriginalAlignment);
6406
Owen Anderson23b9b192009-08-12 00:36:31 +00006407 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6408 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006409 SmallVector<SDValue, 4> Parts(NumParts);
6410 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6411
6412 if (Args[i].isSExt)
6413 ExtendKind = ISD::SIGN_EXTEND;
6414 else if (Args[i].isZExt)
6415 ExtendKind = ISD::ZERO_EXTEND;
6416
Bill Wendling3ea3c242009-12-22 02:10:19 +00006417 getCopyToParts(DAG, dl, Order, Op, &Parts[0], NumParts,
6418 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006419
Dan Gohman98ca4f22009-08-05 01:29:28 +00006420 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006421 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006422 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6423 if (NumParts > 1 && j == 0)
6424 MyFlags.Flags.setSplit();
6425 else if (j != 0)
6426 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006427
Dan Gohman98ca4f22009-08-05 01:29:28 +00006428 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006429 }
6430 }
6431 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006432
Dan Gohman98ca4f22009-08-05 01:29:28 +00006433 // Handle the incoming return values from the call.
6434 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006435 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006436 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006437 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006438 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006439 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6440 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006441 for (unsigned i = 0; i != NumRegs; ++i) {
6442 ISD::InputArg MyFlags;
6443 MyFlags.VT = RegisterVT;
6444 MyFlags.Used = isReturnValueUsed;
6445 if (RetSExt)
6446 MyFlags.Flags.setSExt();
6447 if (RetZExt)
6448 MyFlags.Flags.setZExt();
6449 if (isInreg)
6450 MyFlags.Flags.setInReg();
6451 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006452 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006453 }
6454
Dan Gohman98ca4f22009-08-05 01:29:28 +00006455 // Check if target-dependent constraints permit a tail call here.
6456 // Target-independent constraints should be checked by the caller.
6457 if (isTailCall &&
6458 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6459 isTailCall = false;
6460
6461 SmallVector<SDValue, 4> InVals;
6462 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6463 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006464
6465 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006466 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006467 "LowerCall didn't return a valid chain!");
6468 assert((!isTailCall || InVals.empty()) &&
6469 "LowerCall emitted a return value for a tail call!");
6470 assert((isTailCall || InVals.size() == Ins.size()) &&
6471 "LowerCall didn't emit the correct number of values!");
6472 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6473 assert(InVals[i].getNode() &&
6474 "LowerCall emitted a null value!");
6475 assert(Ins[i].VT == InVals[i].getValueType() &&
6476 "LowerCall emitted a value with the wrong type!");
6477 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006478
Bill Wendling3ea3c242009-12-22 02:10:19 +00006479 if (DisableScheduling)
6480 DAG.AssignOrdering(Chain.getNode(), Order);
6481
Dan Gohman98ca4f22009-08-05 01:29:28 +00006482 // For a tail call, the return value is merely live-out and there aren't
6483 // any nodes in the DAG representing it. Return a special value to
6484 // indicate that a tail call has been emitted and no more Instructions
6485 // should be processed in the current block.
6486 if (isTailCall) {
6487 DAG.setRoot(Chain);
6488 return std::make_pair(SDValue(), SDValue());
6489 }
6490
6491 // Collect the legal value parts into potentially illegal values
6492 // that correspond to the original function's return values.
6493 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6494 if (RetSExt)
6495 AssertOp = ISD::AssertSext;
6496 else if (RetZExt)
6497 AssertOp = ISD::AssertZext;
6498 SmallVector<SDValue, 4> ReturnValues;
6499 unsigned CurReg = 0;
6500 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006501 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006502 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6503 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006504
6505 SDValue ReturnValue =
Bill Wendling3ea3c242009-12-22 02:10:19 +00006506 getCopyFromParts(DAG, dl, Order, &InVals[CurReg], NumRegs,
6507 RegisterVT, VT, AssertOp);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006508 ReturnValues.push_back(ReturnValue);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006509 if (DisableScheduling)
6510 DAG.AssignOrdering(ReturnValue.getNode(), Order);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006511 CurReg += NumRegs;
6512 }
6513
6514 // For a function returning void, there is no return value. We can't create
6515 // such a node, so we just return a null return value in that case. In
6516 // that case, nothing will actualy look at the value.
6517 if (ReturnValues.empty())
6518 return std::make_pair(SDValue(), Chain);
6519
6520 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6521 DAG.getVTList(&RetTys[0], RetTys.size()),
6522 &ReturnValues[0], ReturnValues.size());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006523 if (DisableScheduling)
6524 DAG.AssignOrdering(Res.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006525 return std::make_pair(Res, Chain);
6526}
6527
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006528void TargetLowering::LowerOperationWrapper(SDNode *N,
6529 SmallVectorImpl<SDValue> &Results,
6530 SelectionDAG &DAG) {
6531 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006532 if (Res.getNode())
6533 Results.push_back(Res);
6534}
6535
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006536SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006537 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006538 return SDValue();
6539}
6540
Dan Gohman2048b852009-11-23 18:04:58 +00006541void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006542 SDValue Op = getValue(V);
6543 assert((Op.getOpcode() != ISD::CopyFromReg ||
6544 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6545 "Copy from a reg to the same reg!");
6546 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6547
Owen Anderson23b9b192009-08-12 00:36:31 +00006548 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006549 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +00006550 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006551 PendingExports.push_back(Chain);
6552}
6553
6554#include "llvm/CodeGen/SelectionDAGISel.h"
6555
Dan Gohman8c2b5252009-10-30 01:27:03 +00006556void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006557 // If this is the entry block, emit arguments.
6558 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006559 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006560 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006561 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006562 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006563 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006564
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006565 // Check whether the function can return without sret-demotion.
6566 SmallVector<EVT, 4> OutVTs;
6567 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006568 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006569 OutVTs, OutsFlags, TLI);
6570 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6571
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006572 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00006573 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006574 if (!FLI.CanLowerReturn) {
6575 // Put in an sret pointer parameter before all the other parameters.
6576 SmallVector<EVT, 1> ValueVTs;
6577 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6578
6579 // NOTE: Assuming that a pointer will never break down to more than one VT
6580 // or one register.
6581 ISD::ArgFlagsTy Flags;
6582 Flags.setSRet();
6583 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6584 ISD::InputArg RetArg(Flags, RegisterVT, true);
6585 Ins.push_back(RetArg);
6586 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006587
Dan Gohman98ca4f22009-08-05 01:29:28 +00006588 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006589 unsigned Idx = 1;
6590 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6591 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006592 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006593 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6594 bool isArgValueUsed = !I->use_empty();
6595 for (unsigned Value = 0, NumValues = ValueVTs.size();
6596 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006597 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006598 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006599 ISD::ArgFlagsTy Flags;
6600 unsigned OriginalAlignment =
6601 TD->getABITypeAlignment(ArgTy);
6602
6603 if (F.paramHasAttr(Idx, Attribute::ZExt))
6604 Flags.setZExt();
6605 if (F.paramHasAttr(Idx, Attribute::SExt))
6606 Flags.setSExt();
6607 if (F.paramHasAttr(Idx, Attribute::InReg))
6608 Flags.setInReg();
6609 if (F.paramHasAttr(Idx, Attribute::StructRet))
6610 Flags.setSRet();
6611 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6612 Flags.setByVal();
6613 const PointerType *Ty = cast<PointerType>(I->getType());
6614 const Type *ElementTy = Ty->getElementType();
6615 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6616 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6617 // For ByVal, alignment should be passed from FE. BE will guess if
6618 // this info is not there but there are cases it cannot get right.
6619 if (F.getParamAlignment(Idx))
6620 FrameAlign = F.getParamAlignment(Idx);
6621 Flags.setByValAlign(FrameAlign);
6622 Flags.setByValSize(FrameSize);
6623 }
6624 if (F.paramHasAttr(Idx, Attribute::Nest))
6625 Flags.setNest();
6626 Flags.setOrigAlign(OriginalAlignment);
6627
Owen Anderson23b9b192009-08-12 00:36:31 +00006628 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6629 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006630 for (unsigned i = 0; i != NumRegs; ++i) {
6631 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6632 if (NumRegs > 1 && i == 0)
6633 MyFlags.Flags.setSplit();
6634 // if it isn't first piece, alignment must be 1
6635 else if (i > 0)
6636 MyFlags.Flags.setOrigAlign(1);
6637 Ins.push_back(MyFlags);
6638 }
6639 }
6640 }
6641
6642 // Call the target to set up the argument values.
6643 SmallVector<SDValue, 8> InVals;
6644 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6645 F.isVarArg(), Ins,
6646 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006647
6648 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006649 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006650 "LowerFormalArguments didn't return a valid chain!");
6651 assert(InVals.size() == Ins.size() &&
6652 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006653 DEBUG({
6654 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6655 assert(InVals[i].getNode() &&
6656 "LowerFormalArguments emitted a null value!");
6657 assert(Ins[i].VT == InVals[i].getValueType() &&
6658 "LowerFormalArguments emitted a value with the wrong type!");
6659 }
6660 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006661
Dan Gohman5e866062009-08-06 15:37:27 +00006662 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006663 DAG.setRoot(NewRoot);
6664
6665 // Set up the argument values.
6666 unsigned i = 0;
6667 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006668 if (!FLI.CanLowerReturn) {
6669 // Create a virtual register for the sret pointer, and put in a copy
6670 // from the sret argument into it.
6671 SmallVector<EVT, 1> ValueVTs;
6672 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6673 EVT VT = ValueVTs[0];
6674 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6675 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling3ea58b62009-12-22 21:35:02 +00006676 SDValue ArgValue = getCopyFromParts(DAG, dl, 0, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006677 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006678
Dan Gohman2048b852009-11-23 18:04:58 +00006679 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006680 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6681 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6682 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006683 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6684 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006685 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006686
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006687 // i indexes lowered arguments. Bump it past the hidden sret argument.
6688 // Idx indexes LLVM arguments. Don't touch it.
6689 ++i;
6690 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006691
Dan Gohman98ca4f22009-08-05 01:29:28 +00006692 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6693 ++I, ++Idx) {
6694 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006695 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006696 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006697 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006698 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006699 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006700 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6701 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006702
6703 if (!I->use_empty()) {
6704 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6705 if (F.paramHasAttr(Idx, Attribute::SExt))
6706 AssertOp = ISD::AssertSext;
6707 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6708 AssertOp = ISD::AssertZext;
6709
Bill Wendling3ea58b62009-12-22 21:35:02 +00006710 ArgValues.push_back(getCopyFromParts(DAG, dl, 0, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006711 NumParts, PartVT, VT,
6712 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006713 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006714
Dan Gohman98ca4f22009-08-05 01:29:28 +00006715 i += NumParts;
6716 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006717
Dan Gohman98ca4f22009-08-05 01:29:28 +00006718 if (!I->use_empty()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +00006719 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6720 SDB->getCurDebugLoc());
6721 SDB->setValue(I, Res);
6722
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006723 // If this argument is live outside of the entry block, insert a copy from
6724 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006725 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006726 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006727 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006728
Dan Gohman98ca4f22009-08-05 01:29:28 +00006729 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006730
6731 // Finally, if the target has anything special to do, allow it to do so.
6732 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006733 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006734}
6735
6736/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6737/// ensure constants are generated when needed. Remember the virtual registers
6738/// that need to be added to the Machine PHI nodes as input. We cannot just
6739/// directly add them, because expansion might result in multiple MBB's for one
6740/// BB. As such, the start of the BB might correspond to a different MBB than
6741/// the end.
6742///
6743void
6744SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6745 TerminatorInst *TI = LLVMBB->getTerminator();
6746
6747 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6748
6749 // Check successor nodes' PHI nodes that expect a constant to be available
6750 // from this block.
6751 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6752 BasicBlock *SuccBB = TI->getSuccessor(succ);
6753 if (!isa<PHINode>(SuccBB->begin())) continue;
6754 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006755
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006756 // If this terminator has multiple identical successors (common for
6757 // switches), only handle each succ once.
6758 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006759
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006760 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6761 PHINode *PN;
6762
6763 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6764 // nodes and Machine PHI nodes, but the incoming operands have not been
6765 // emitted yet.
6766 for (BasicBlock::iterator I = SuccBB->begin();
6767 (PN = dyn_cast<PHINode>(I)); ++I) {
6768 // Ignore dead phi's.
6769 if (PN->use_empty()) continue;
6770
6771 unsigned Reg;
6772 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6773
6774 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006775 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006776 if (RegOut == 0) {
6777 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006778 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006779 }
6780 Reg = RegOut;
6781 } else {
6782 Reg = FuncInfo->ValueMap[PHIOp];
6783 if (Reg == 0) {
6784 assert(isa<AllocaInst>(PHIOp) &&
6785 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6786 "Didn't codegen value into a register!??");
6787 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006788 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006789 }
6790 }
6791
6792 // Remember that this register needs to added to the machine PHI node as
6793 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006794 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006795 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6796 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006797 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006798 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006799 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006800 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006801 Reg += NumRegisters;
6802 }
6803 }
6804 }
Dan Gohman2048b852009-11-23 18:04:58 +00006805 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006806}
6807
Dan Gohman3df24e62008-09-03 23:12:08 +00006808/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6809/// supports legal types, and it emits MachineInstrs directly instead of
6810/// creating SelectionDAG nodes.
6811///
6812bool
6813SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6814 FastISel *F) {
6815 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006816
Dan Gohman3df24e62008-09-03 23:12:08 +00006817 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006818 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006819
6820 // Check successor nodes' PHI nodes that expect a constant to be available
6821 // from this block.
6822 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6823 BasicBlock *SuccBB = TI->getSuccessor(succ);
6824 if (!isa<PHINode>(SuccBB->begin())) continue;
6825 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006826
Dan Gohman3df24e62008-09-03 23:12:08 +00006827 // If this terminator has multiple identical successors (common for
6828 // switches), only handle each succ once.
6829 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006830
Dan Gohman3df24e62008-09-03 23:12:08 +00006831 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6832 PHINode *PN;
6833
6834 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6835 // nodes and Machine PHI nodes, but the incoming operands have not been
6836 // emitted yet.
6837 for (BasicBlock::iterator I = SuccBB->begin();
6838 (PN = dyn_cast<PHINode>(I)); ++I) {
6839 // Ignore dead phi's.
6840 if (PN->use_empty()) continue;
6841
6842 // Only handle legal types. Two interesting things to note here. First,
6843 // by bailing out early, we may leave behind some dead instructions,
6844 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6845 // own moves. Second, this check is necessary becuase FastISel doesn't
6846 // use CreateRegForValue to create registers, so it always creates
6847 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006848 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006849 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6850 // Promote MVT::i1.
6851 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006852 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006853 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006854 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006855 return false;
6856 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006857 }
6858
6859 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6860
6861 unsigned Reg = F->getRegForValue(PHIOp);
6862 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006863 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006864 return false;
6865 }
Dan Gohman2048b852009-11-23 18:04:58 +00006866 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006867 }
6868 }
6869
6870 return true;
6871}