blob: 40eb9eeb82a97f607f6c256d308899673d5b79e5 [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
Chris Lattner133ce872010-01-02 00:00:03 +00001198 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1199 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1200 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1201 Cases[0].CC == Cases[1].CC &&
1202 isa<Constant>(Cases[0].CmpRHS) &&
1203 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1204 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1205 return false;
1206 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1207 return false;
1208 }
1209
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001210 return true;
1211}
1212
Dan Gohman2048b852009-11-23 18:04:58 +00001213void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001214 // Update machine-CFG edges.
1215 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1216
1217 // Figure out which block is immediately after the current one.
1218 MachineBasicBlock *NextBlock = 0;
1219 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001220 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001221 NextBlock = BBI;
1222
1223 if (I.isUnconditional()) {
1224 // Update machine-CFG edges.
1225 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001226
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001227 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001228 if (Succ0MBB != NextBlock) {
1229 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001230 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001231 DAG.getBasicBlock(Succ0MBB));
1232 DAG.setRoot(V);
1233
1234 if (DisableScheduling)
1235 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
1236 }
1237
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001238 return;
1239 }
1240
1241 // If this condition is one of the special cases we handle, do special stuff
1242 // now.
1243 Value *CondVal = I.getCondition();
1244 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1245
1246 // If this is a series of conditions that are or'd or and'd together, emit
1247 // this as a sequence of branches instead of setcc's with and/or operations.
1248 // For example, instead of something like:
1249 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001250 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001251 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001252 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001253 // or C, F
1254 // jnz foo
1255 // Emit:
1256 // cmp A, B
1257 // je foo
1258 // cmp D, E
1259 // jle foo
1260 //
1261 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001262 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 (BOp->getOpcode() == Instruction::And ||
1264 BOp->getOpcode() == Instruction::Or)) {
1265 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1266 // If the compares in later blocks need to use values not currently
1267 // exported from this block, export them now. This block should always
1268 // be the first entry.
1269 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001270
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001271 // Allow some cases to be rejected.
1272 if (ShouldEmitAsBranches(SwitchCases)) {
1273 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1274 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1275 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1276 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001278 // Emit the branch for this block.
1279 visitSwitchCase(SwitchCases[0]);
1280 SwitchCases.erase(SwitchCases.begin());
1281 return;
1282 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001283
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001284 // Okay, we decided not to do this, remove any inserted MBB's and clear
1285 // SwitchCases.
1286 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001287 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001288
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001289 SwitchCases.clear();
1290 }
1291 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001292
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001293 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001294 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001295 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001297 // Use visitSwitchCase to actually insert the fast branch sequence for this
1298 // cond branch.
1299 visitSwitchCase(CB);
1300}
1301
1302/// visitSwitchCase - Emits the necessary code to represent a single node in
1303/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001304void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001305 SDValue Cond;
1306 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001307 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001308
1309 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001310 if (CB.CmpMHS == NULL) {
1311 // Fold "(X == true)" to X and "(X == false)" to !X to
1312 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001313 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001314 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001315 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001316 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001317 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001318 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001319 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001321 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001322 } else {
1323 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1324
Anton Korobeynikov23218582008-12-23 22:25:27 +00001325 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1326 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001327
1328 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001329 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330
1331 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001332 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001333 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001334 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001335 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001336 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001337 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001338 DAG.getConstant(High-Low, VT), ISD::SETULE);
1339 }
1340 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001341
Bill Wendling87710f02009-12-21 23:47:40 +00001342 if (DisableScheduling)
1343 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
1344
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001345 // Update successor info
1346 CurMBB->addSuccessor(CB.TrueBB);
1347 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001348
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001349 // Set NextBlock to be the MBB immediately after the current one, if any.
1350 // This is used to avoid emitting unnecessary branches to the next block.
1351 MachineBasicBlock *NextBlock = 0;
1352 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001353 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001354 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001355
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001356 // If the lhs block is the next block, invert the condition so that we can
1357 // fall through to the lhs instead of the rhs block.
1358 if (CB.TrueBB == NextBlock) {
1359 std::swap(CB.TrueBB, CB.FalseBB);
1360 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001361 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling87710f02009-12-21 23:47:40 +00001362
1363 if (DisableScheduling)
1364 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001366
Dale Johannesenf5d97892009-02-04 01:48:28 +00001367 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001368 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001369 DAG.getBasicBlock(CB.TrueBB));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001370
Bill Wendling87710f02009-12-21 23:47:40 +00001371 if (DisableScheduling)
1372 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1373
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001374 // If the branch was constant folded, fix up the CFG.
1375 if (BrCond.getOpcode() == ISD::BR) {
1376 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377 } else {
1378 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001379 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001380 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001381
Bill Wendling87710f02009-12-21 23:47:40 +00001382 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001383 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1384 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001385
1386 if (DisableScheduling)
1387 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1388 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001389 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001390
1391 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001392}
1393
1394/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001395void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001396 // Emit the code for the jump table
1397 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001398 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001399 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1400 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001401 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001402 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1403 MVT::Other, Index.getValue(1),
1404 Table, Index);
1405 DAG.setRoot(BrJumpTable);
1406
Bill Wendling87710f02009-12-21 23:47:40 +00001407 if (DisableScheduling) {
1408 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1409 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001410 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001411 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001412}
1413
1414/// visitJumpTableHeader - This function emits necessary code to produce index
1415/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001416void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1417 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001418 // Subtract the lowest switch case value from the value being switched on and
1419 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001420 // difference between smallest and largest cases.
1421 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001422 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001423 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001424 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001425
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001426 // The SDNode we just created, which holds the value being switched on minus
1427 // the the smallest case value, needs to be copied to a virtual register so it
1428 // can be used as an index into the jump table in a subsequent basic block.
1429 // This value may be smaller or larger than the target's pointer type, and
1430 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001431 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001432
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001433 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001434 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1435 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436 JT.Reg = JumpTableReg;
1437
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001438 // Emit the range check for the jump table, and branch to the default block
1439 // for the switch statement if the value being switched on exceeds the largest
1440 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001441 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001442 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001443 DAG.getConstant(JTH.Last-JTH.First,VT),
1444 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001445
Bill Wendling87710f02009-12-21 23:47:40 +00001446 if (DisableScheduling) {
1447 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1448 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1449 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1450 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
1451 }
1452
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001453 // Set NextBlock to be the MBB immediately after the current one, if any.
1454 // This is used to avoid emitting unnecessary branches to the next block.
1455 MachineBasicBlock *NextBlock = 0;
1456 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001457
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001458 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001459 NextBlock = BBI;
1460
Dale Johannesen66978ee2009-01-31 02:22:37 +00001461 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001462 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001463 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001464
Bill Wendling87710f02009-12-21 23:47:40 +00001465 if (DisableScheduling)
1466 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1467
1468 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001469 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1470 DAG.getBasicBlock(JT.MBB));
1471
Bill Wendling87710f02009-12-21 23:47:40 +00001472 if (DisableScheduling)
1473 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
1474 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001475
Bill Wendling87710f02009-12-21 23:47:40 +00001476 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001477}
1478
1479/// visitBitTestHeader - This function emits necessary code to produce value
1480/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001481void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001482 // Subtract the minimum value
1483 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001484 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001485 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001486 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001487
1488 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001489 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001490 TLI.getSetCCResultType(Sub.getValueType()),
1491 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001492 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001493
Bill Wendling87710f02009-12-21 23:47:40 +00001494 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1495 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001496
Duncan Sands92abc622009-01-31 15:50:11 +00001497 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001498 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1499 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001500
Bill Wendling87710f02009-12-21 23:47:40 +00001501 if (DisableScheduling) {
1502 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1503 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1504 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1505 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1506 }
1507
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001508 // Set NextBlock to be the MBB immediately after the current one, if any.
1509 // This is used to avoid emitting unnecessary branches to the next block.
1510 MachineBasicBlock *NextBlock = 0;
1511 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001512 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001513 NextBlock = BBI;
1514
1515 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1516
1517 CurMBB->addSuccessor(B.Default);
1518 CurMBB->addSuccessor(MBB);
1519
Dale Johannesen66978ee2009-01-31 02:22:37 +00001520 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001521 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001522 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001523
Bill Wendling87710f02009-12-21 23:47:40 +00001524 if (DisableScheduling)
1525 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1526
1527 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001528 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1529 DAG.getBasicBlock(MBB));
1530
Bill Wendling87710f02009-12-21 23:47:40 +00001531 if (DisableScheduling)
1532 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
1533 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001534
Bill Wendling87710f02009-12-21 23:47:40 +00001535 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001536}
1537
1538/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001539void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1540 unsigned Reg,
1541 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001542 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001543 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001544 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001545 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001546 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001547 DAG.getConstant(1, TLI.getPointerTy()),
1548 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001549
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001550 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001551 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001552 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001553 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001554 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1555 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001556 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001557 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001558
Bill Wendling87710f02009-12-21 23:47:40 +00001559 if (DisableScheduling) {
1560 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1561 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1562 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1563 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
1564 }
1565
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001566 CurMBB->addSuccessor(B.TargetBB);
1567 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001568
Dale Johannesen66978ee2009-01-31 02:22:37 +00001569 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001570 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001571 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001572
Bill Wendling87710f02009-12-21 23:47:40 +00001573 if (DisableScheduling)
1574 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1575
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001576 // Set NextBlock to be the MBB immediately after the current one, if any.
1577 // This is used to avoid emitting unnecessary branches to the next block.
1578 MachineBasicBlock *NextBlock = 0;
1579 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001580 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001581 NextBlock = BBI;
1582
Bill Wendling87710f02009-12-21 23:47:40 +00001583 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001584 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1585 DAG.getBasicBlock(NextMBB));
1586
Bill Wendling87710f02009-12-21 23:47:40 +00001587 if (DisableScheduling)
1588 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
1589 }
Bill Wendling0777e922009-12-21 21:59:52 +00001590
Bill Wendling87710f02009-12-21 23:47:40 +00001591 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001592}
1593
Dan Gohman2048b852009-11-23 18:04:58 +00001594void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001595 // Retrieve successors.
1596 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1597 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1598
Gabor Greifb67e6b32009-01-15 11:10:44 +00001599 const Value *Callee(I.getCalledValue());
1600 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001601 visitInlineAsm(&I);
1602 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001603 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001604
1605 // If the value of the invoke is used outside of its defining block, make it
1606 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001607 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001608
1609 // Update successor info
1610 CurMBB->addSuccessor(Return);
1611 CurMBB->addSuccessor(LandingPad);
1612
1613 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001614 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1615 MVT::Other, getControlRoot(),
1616 DAG.getBasicBlock(Return));
1617 DAG.setRoot(Branch);
1618
1619 if (DisableScheduling)
1620 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001621}
1622
Dan Gohman2048b852009-11-23 18:04:58 +00001623void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624}
1625
1626/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1627/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001628bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1629 CaseRecVector& WorkList,
1630 Value* SV,
1631 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001632 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001633
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001634 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001635 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001636 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001637 return false;
1638
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001639 // Get the MachineFunction which holds the current MBB. This is used when
1640 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001641 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001642
1643 // Figure out which block is immediately after the current one.
1644 MachineBasicBlock *NextBlock = 0;
1645 MachineFunction::iterator BBI = CR.CaseBB;
1646
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001647 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001648 NextBlock = BBI;
1649
1650 // TODO: If any two of the cases has the same destination, and if one value
1651 // is the same as the other, but has one bit unset that the other has set,
1652 // use bit manipulation to do two compares at once. For example:
1653 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001655 // Rearrange the case blocks so that the last one falls through if possible.
1656 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1657 // The last case block won't fall through into 'NextBlock' if we emit the
1658 // branches in this order. See if rearranging a case value would help.
1659 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1660 if (I->BB == NextBlock) {
1661 std::swap(*I, BackCase);
1662 break;
1663 }
1664 }
1665 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001666
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001667 // Create a CaseBlock record representing a conditional branch to
1668 // the Case's target mbb if the value being switched on SV is equal
1669 // to C.
1670 MachineBasicBlock *CurBlock = CR.CaseBB;
1671 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1672 MachineBasicBlock *FallThrough;
1673 if (I != E-1) {
1674 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1675 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001676
1677 // Put SV in a virtual register to make it available from the new blocks.
1678 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001679 } else {
1680 // If the last case doesn't match, go to the default block.
1681 FallThrough = Default;
1682 }
1683
1684 Value *RHS, *LHS, *MHS;
1685 ISD::CondCode CC;
1686 if (I->High == I->Low) {
1687 // This is just small small case range :) containing exactly 1 case
1688 CC = ISD::SETEQ;
1689 LHS = SV; RHS = I->High; MHS = NULL;
1690 } else {
1691 CC = ISD::SETLE;
1692 LHS = I->Low; MHS = SV; RHS = I->High;
1693 }
1694 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001695
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696 // If emitting the first comparison, just call visitSwitchCase to emit the
1697 // code into the current block. Otherwise, push the CaseBlock onto the
1698 // vector to be later processed by SDISel, and insert the node's MBB
1699 // before the next MBB.
1700 if (CurBlock == CurMBB)
1701 visitSwitchCase(CB);
1702 else
1703 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001704
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001705 CurBlock = FallThrough;
1706 }
1707
1708 return true;
1709}
1710
1711static inline bool areJTsAllowed(const TargetLowering &TLI) {
1712 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001713 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1714 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001715}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001716
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001717static APInt ComputeRange(const APInt &First, const APInt &Last) {
1718 APInt LastExt(Last), FirstExt(First);
1719 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1720 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1721 return (LastExt - FirstExt + 1ULL);
1722}
1723
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001724/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001725bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1726 CaseRecVector& WorkList,
1727 Value* SV,
1728 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001729 Case& FrontCase = *CR.Range.first;
1730 Case& BackCase = *(CR.Range.second-1);
1731
Chris Lattnere880efe2009-11-07 07:50:34 +00001732 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1733 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001734
Chris Lattnere880efe2009-11-07 07:50:34 +00001735 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001736 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1737 I!=E; ++I)
1738 TSize += I->size();
1739
Chris Lattnere880efe2009-11-07 07:50:34 +00001740 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001741 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001742
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001743 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001744 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001745 if (Density < 0.4)
1746 return false;
1747
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001748 DEBUG(errs() << "Lowering jump table\n"
1749 << "First entry: " << First << ". Last entry: " << Last << '\n'
1750 << "Range: " << Range
1751 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001752
1753 // Get the MachineFunction which holds the current MBB. This is used when
1754 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001755 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001756
1757 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001758 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001759 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001760
1761 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1762
1763 // Create a new basic block to hold the code for loading the address
1764 // of the jump table, and jumping to it. Update successor information;
1765 // we will either branch to the default case for the switch, or the jump
1766 // table.
1767 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1768 CurMF->insert(BBI, JumpTableBB);
1769 CR.CaseBB->addSuccessor(Default);
1770 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001771
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001772 // Build a vector of destination BBs, corresponding to each target
1773 // of the jump table. If the value of the jump table slot corresponds to
1774 // a case statement, push the case's BB onto the vector, otherwise, push
1775 // the default BB.
1776 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001777 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001778 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001779 const APInt& Low = cast<ConstantInt>(I->Low)->getValue();
1780 const APInt& High = cast<ConstantInt>(I->High)->getValue();
1781
1782 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001783 DestBBs.push_back(I->BB);
1784 if (TEI==High)
1785 ++I;
1786 } else {
1787 DestBBs.push_back(Default);
1788 }
1789 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001790
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001791 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001792 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1793 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001794 E = DestBBs.end(); I != E; ++I) {
1795 if (!SuccsHandled[(*I)->getNumber()]) {
1796 SuccsHandled[(*I)->getNumber()] = true;
1797 JumpTableBB->addSuccessor(*I);
1798 }
1799 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001800
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001801 // Create a jump table index for this jump table, or return an existing
1802 // one.
1803 unsigned JTI = CurMF->getJumpTableInfo()->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001804
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001805 // Set the jump table information so that we can codegen it as a second
1806 // MachineBasicBlock
1807 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1808 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1809 if (CR.CaseBB == CurMBB)
1810 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001811
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001812 JTCases.push_back(JumpTableBlock(JTH, JT));
1813
1814 return true;
1815}
1816
1817/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1818/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001819bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1820 CaseRecVector& WorkList,
1821 Value* SV,
1822 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001823 // Get the MachineFunction which holds the current MBB. This is used when
1824 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001825 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826
1827 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001828 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001829 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001830
1831 Case& FrontCase = *CR.Range.first;
1832 Case& BackCase = *(CR.Range.second-1);
1833 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1834
1835 // Size is the number of Cases represented by this range.
1836 unsigned Size = CR.Range.second - CR.Range.first;
1837
Chris Lattnere880efe2009-11-07 07:50:34 +00001838 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1839 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001840 double FMetric = 0;
1841 CaseItr Pivot = CR.Range.first + Size/2;
1842
1843 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1844 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001845 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001846 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1847 I!=E; ++I)
1848 TSize += I->size();
1849
Chris Lattnere880efe2009-11-07 07:50:34 +00001850 APInt LSize = FrontCase.size();
1851 APInt RSize = TSize-LSize;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001852 DEBUG(errs() << "Selecting best pivot: \n"
1853 << "First: " << First << ", Last: " << Last <<'\n'
1854 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1856 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001857 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1858 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001859 APInt Range = ComputeRange(LEnd, RBegin);
1860 assert((Range - 2ULL).isNonNegative() &&
1861 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001862 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001863 (LEnd - First + 1ULL).roundToDouble();
1864 double RDensity = (double)RSize.roundToDouble() /
1865 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001866 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001867 // Should always split in some non-trivial place
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001868 DEBUG(errs() <<"=>Step\n"
1869 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1870 << "LDensity: " << LDensity
1871 << ", RDensity: " << RDensity << '\n'
1872 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001873 if (FMetric < Metric) {
1874 Pivot = J;
1875 FMetric = Metric;
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001876 DEBUG(errs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001877 }
1878
1879 LSize += J->size();
1880 RSize -= J->size();
1881 }
1882 if (areJTsAllowed(TLI)) {
1883 // If our case is dense we *really* should handle it earlier!
1884 assert((FMetric > 0) && "Should handle dense range earlier!");
1885 } else {
1886 Pivot = CR.Range.first + Size/2;
1887 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001888
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001889 CaseRange LHSR(CR.Range.first, Pivot);
1890 CaseRange RHSR(Pivot, CR.Range.second);
1891 Constant *C = Pivot->Low;
1892 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001893
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001894 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001895 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001896 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001897 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001898 // Pivot's Value, then we can branch directly to the LHS's Target,
1899 // rather than creating a leaf node for it.
1900 if ((LHSR.second - LHSR.first) == 1 &&
1901 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001902 cast<ConstantInt>(C)->getValue() ==
1903 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001904 TrueBB = LHSR.first->BB;
1905 } else {
1906 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1907 CurMF->insert(BBI, TrueBB);
1908 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001909
1910 // Put SV in a virtual register to make it available from the new blocks.
1911 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001912 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001914 // Similar to the optimization above, if the Value being switched on is
1915 // known to be less than the Constant CR.LT, and the current Case Value
1916 // is CR.LT - 1, then we can branch directly to the target block for
1917 // the current Case Value, rather than emitting a RHS leaf node for it.
1918 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001919 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1920 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001921 FalseBB = RHSR.first->BB;
1922 } else {
1923 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1924 CurMF->insert(BBI, FalseBB);
1925 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001926
1927 // Put SV in a virtual register to make it available from the new blocks.
1928 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929 }
1930
1931 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001932 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001933 // Otherwise, branch to LHS.
1934 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1935
1936 if (CR.CaseBB == CurMBB)
1937 visitSwitchCase(CB);
1938 else
1939 SwitchCases.push_back(CB);
1940
1941 return true;
1942}
1943
1944/// handleBitTestsSwitchCase - if current case range has few destination and
1945/// range span less, than machine word bitwidth, encode case range into series
1946/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001947bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1948 CaseRecVector& WorkList,
1949 Value* SV,
1950 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001951 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001952 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001953
1954 Case& FrontCase = *CR.Range.first;
1955 Case& BackCase = *(CR.Range.second-1);
1956
1957 // Get the MachineFunction which holds the current MBB. This is used when
1958 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001959 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001960
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001961 // If target does not have legal shift left, do not emit bit tests at all.
1962 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1963 return false;
1964
Anton Korobeynikov23218582008-12-23 22:25:27 +00001965 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001966 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1967 I!=E; ++I) {
1968 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001969 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001970 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001971
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001972 // Count unique destinations
1973 SmallSet<MachineBasicBlock*, 4> Dests;
1974 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1975 Dests.insert(I->BB);
1976 if (Dests.size() > 3)
1977 // Don't bother the code below, if there are too much unique destinations
1978 return false;
1979 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001980 DEBUG(errs() << "Total number of unique destinations: "
1981 << Dests.size() << '\n'
1982 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001983
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001984 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001985 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1986 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001987 APInt cmpRange = maxValue - minValue;
1988
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001989 DEBUG(errs() << "Compare range: " << cmpRange << '\n'
1990 << "Low bound: " << minValue << '\n'
1991 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001992
1993 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001994 (!(Dests.size() == 1 && numCmps >= 3) &&
1995 !(Dests.size() == 2 && numCmps >= 5) &&
1996 !(Dests.size() >= 3 && numCmps >= 6)))
1997 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001998
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001999 DEBUG(errs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00002000 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
2001
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002002 // Optimize the case where all the case values fit in a
2003 // word without having to subtract minValue. In this case,
2004 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002005 if (minValue.isNonNegative() &&
2006 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
2007 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002008 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002009 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002010 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002011
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002012 CaseBitsVector CasesBits;
2013 unsigned i, count = 0;
2014
2015 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
2016 MachineBasicBlock* Dest = I->BB;
2017 for (i = 0; i < count; ++i)
2018 if (Dest == CasesBits[i].BB)
2019 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002020
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002021 if (i == count) {
2022 assert((count < 3) && "Too much destinations to test!");
2023 CasesBits.push_back(CaseBits(0, Dest, 0));
2024 count++;
2025 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002026
2027 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
2028 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
2029
2030 uint64_t lo = (lowValue - lowBound).getZExtValue();
2031 uint64_t hi = (highValue - lowBound).getZExtValue();
2032
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002033 for (uint64_t j = lo; j <= hi; j++) {
2034 CasesBits[i].Mask |= 1ULL << j;
2035 CasesBits[i].Bits++;
2036 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002037
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002038 }
2039 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00002040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002041 BitTestInfo BTC;
2042
2043 // Figure out which block is immediately after the current one.
2044 MachineFunction::iterator BBI = CR.CaseBB;
2045 ++BBI;
2046
2047 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2048
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002049 DEBUG(errs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002050 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002051 DEBUG(errs() << "Mask: " << CasesBits[i].Mask
2052 << ", Bits: " << CasesBits[i].Bits
2053 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002054
2055 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2056 CurMF->insert(BBI, CaseBB);
2057 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2058 CaseBB,
2059 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002060
2061 // Put SV in a virtual register to make it available from the new blocks.
2062 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002063 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002064
2065 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002066 -1U, (CR.CaseBB == CurMBB),
2067 CR.CaseBB, Default, BTC);
2068
2069 if (CR.CaseBB == CurMBB)
2070 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002071
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002072 BitTestCases.push_back(BTB);
2073
2074 return true;
2075}
2076
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002077/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002078size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2079 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002080 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002081
2082 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002083 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002084 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2085 Cases.push_back(Case(SI.getSuccessorValue(i),
2086 SI.getSuccessorValue(i),
2087 SMBB));
2088 }
2089 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2090
2091 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002092 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093 // Must recompute end() each iteration because it may be
2094 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002095 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2096 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2097 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002098 MachineBasicBlock* nextBB = J->BB;
2099 MachineBasicBlock* currentBB = I->BB;
2100
2101 // If the two neighboring cases go to the same destination, merge them
2102 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002103 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002104 I->High = J->High;
2105 J = Cases.erase(J);
2106 } else {
2107 I = J++;
2108 }
2109 }
2110
2111 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2112 if (I->Low != I->High)
2113 // A range counts double, since it requires two compares.
2114 ++numCmps;
2115 }
2116
2117 return numCmps;
2118}
2119
Dan Gohman2048b852009-11-23 18:04:58 +00002120void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002121 // Figure out which block is immediately after the current one.
2122 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002123 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2124
2125 // If there is only the default destination, branch to it if it is not the
2126 // next basic block. Otherwise, just fall through.
2127 if (SI.getNumOperands() == 2) {
2128 // Update machine-CFG edges.
2129
2130 // If this is not a fall-through branch, emit the branch.
2131 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002132 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002133 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002134 MVT::Other, getControlRoot(),
2135 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002136 DAG.setRoot(Res);
Bill Wendling49fcff82009-12-21 22:30:11 +00002137
2138 if (DisableScheduling)
Bill Wendling87710f02009-12-21 23:47:40 +00002139 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002140 }
2141
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002142 return;
2143 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002145 // If there are any non-default case statements, create a vector of Cases
2146 // representing each one, and sort the vector so that we can efficiently
2147 // create a binary search tree from them.
2148 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002149 size_t numCmps = Clusterify(Cases, SI);
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002150 DEBUG(errs() << "Clusterify finished. Total clusters: " << Cases.size()
2151 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002152 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002153
2154 // Get the Value to be switched on and default basic blocks, which will be
2155 // inserted into CaseBlock records, representing basic blocks in the binary
2156 // search tree.
2157 Value *SV = SI.getOperand(0);
2158
2159 // Push the initial CaseRec onto the worklist
2160 CaseRecVector WorkList;
2161 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2162
2163 while (!WorkList.empty()) {
2164 // Grab a record representing a case range to process off the worklist
2165 CaseRec CR = WorkList.back();
2166 WorkList.pop_back();
2167
2168 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2169 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002170
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002171 // If the range has few cases (two or less) emit a series of specific
2172 // tests.
2173 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2174 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002175
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002176 // If the switch has more than 5 blocks, and at least 40% dense, and the
2177 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002178 // lowering the switch to a binary tree of conditional branches.
2179 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2180 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002181
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002182 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2183 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2184 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2185 }
2186}
2187
Dan Gohman2048b852009-11-23 18:04:58 +00002188void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002189 // Update machine-CFG edges.
2190 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2191 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2192
Bill Wendling49fcff82009-12-21 22:30:11 +00002193 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2194 MVT::Other, getControlRoot(),
2195 getValue(I.getAddress()));
2196 DAG.setRoot(Res);
Chris Lattnerf9be95f2009-10-27 19:13:16 +00002197
Bill Wendling49fcff82009-12-21 22:30:11 +00002198 if (DisableScheduling)
2199 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2200}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002201
Dan Gohman2048b852009-11-23 18:04:58 +00002202void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002203 // -0.0 - X --> fneg
2204 const Type *Ty = I.getType();
2205 if (isa<VectorType>(Ty)) {
2206 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2207 const VectorType *DestTy = cast<VectorType>(I.getType());
2208 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002209 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002210 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002211 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002212 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002213 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002214 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002215 Op2.getValueType(), Op2);
Bill Wendling49fcff82009-12-21 22:30:11 +00002216 setValue(&I, Res);
2217
2218 if (DisableScheduling)
2219 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2220
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002221 return;
2222 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002223 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002224 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002225
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002226 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002227 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002228 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002229 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2230 Op2.getValueType(), Op2);
2231 setValue(&I, Res);
2232
2233 if (DisableScheduling)
2234 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2235
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002236 return;
2237 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002238
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002239 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002240}
2241
Dan Gohman2048b852009-11-23 18:04:58 +00002242void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002243 SDValue Op1 = getValue(I.getOperand(0));
2244 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002245 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2246 Op1.getValueType(), Op1, Op2);
2247 setValue(&I, Res);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002248
Bill Wendling49fcff82009-12-21 22:30:11 +00002249 if (DisableScheduling)
2250 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002251}
2252
Dan Gohman2048b852009-11-23 18:04:58 +00002253void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002254 SDValue Op1 = getValue(I.getOperand(0));
2255 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002256 if (!isa<VectorType>(I.getType()) &&
2257 Op2.getValueType() != TLI.getShiftAmountTy()) {
2258 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002259 EVT PTy = TLI.getPointerTy();
2260 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002261 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002262 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2263 TLI.getShiftAmountTy(), Op2);
2264 // If the operand is larger than the shift count type but the shift
2265 // count type has enough bits to represent any shift value, truncate
2266 // it now. This is a common case and it exposes the truncate to
2267 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002268 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002269 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2270 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2271 TLI.getShiftAmountTy(), Op2);
2272 // Otherwise we'll need to temporarily settle for some other
2273 // convenient type; type legalization will make adjustments as
2274 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002275 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002276 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002277 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002278 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002279 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002280 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002281 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002282
Bill Wendling49fcff82009-12-21 22:30:11 +00002283 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2284 Op1.getValueType(), Op1, Op2);
2285 setValue(&I, Res);
2286
Bill Wendling87710f02009-12-21 23:47:40 +00002287 if (DisableScheduling) {
2288 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2289 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002290 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002291 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002292}
2293
Dan Gohman2048b852009-11-23 18:04:58 +00002294void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002295 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2296 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2297 predicate = IC->getPredicate();
2298 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2299 predicate = ICmpInst::Predicate(IC->getPredicate());
2300 SDValue Op1 = getValue(I.getOperand(0));
2301 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002302 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002303
Owen Andersone50ed302009-08-10 22:56:29 +00002304 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002305 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2306 setValue(&I, Res);
2307
2308 if (DisableScheduling)
2309 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002310}
2311
Dan Gohman2048b852009-11-23 18:04:58 +00002312void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002313 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2314 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2315 predicate = FC->getPredicate();
2316 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2317 predicate = FCmpInst::Predicate(FC->getPredicate());
2318 SDValue Op1 = getValue(I.getOperand(0));
2319 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002320 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002321 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002322 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2323 setValue(&I, Res);
2324
2325 if (DisableScheduling)
2326 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002327}
2328
Dan Gohman2048b852009-11-23 18:04:58 +00002329void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002330 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002331 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2332 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002333 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002334
Bill Wendling49fcff82009-12-21 22:30:11 +00002335 SmallVector<SDValue, 4> Values(NumValues);
2336 SDValue Cond = getValue(I.getOperand(0));
2337 SDValue TrueVal = getValue(I.getOperand(1));
2338 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002339
Bill Wendling49fcff82009-12-21 22:30:11 +00002340 for (unsigned i = 0; i != NumValues; ++i) {
2341 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2342 TrueVal.getNode()->getValueType(i), Cond,
2343 SDValue(TrueVal.getNode(),
2344 TrueVal.getResNo() + i),
2345 SDValue(FalseVal.getNode(),
2346 FalseVal.getResNo() + i));
2347
2348 if (DisableScheduling)
2349 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002350 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002351
Bill Wendling49fcff82009-12-21 22:30:11 +00002352 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2353 DAG.getVTList(&ValueVTs[0], NumValues),
2354 &Values[0], NumValues);
2355 setValue(&I, Res);
2356
2357 if (DisableScheduling)
2358 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2359}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002360
Dan Gohman2048b852009-11-23 18:04:58 +00002361void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002362 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
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::TRUNCATE, 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::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002373 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2374 // ZExt 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::ZERO_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::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002385 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2386 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2387 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002388 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002389 SDValue Res = DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
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::visitFPTrunc(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_ROUND, getCurDebugLoc(),
2401 DestVT, N, DAG.getIntPtrConstant(0));
2402 setValue(&I, Res);
2403
2404 if (DisableScheduling)
2405 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002406}
2407
Dan Gohman2048b852009-11-23 18:04:58 +00002408void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002409 // FPTrunc is never a no-op cast, no need to check
2410 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002411 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002412 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2413 setValue(&I, Res);
2414
2415 if (DisableScheduling)
2416 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002417}
2418
Dan Gohman2048b852009-11-23 18:04:58 +00002419void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002420 // FPToUI is never a no-op cast, no need to check
2421 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002422 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002423 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2424 setValue(&I, Res);
2425
2426 if (DisableScheduling)
2427 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002428}
2429
Dan Gohman2048b852009-11-23 18:04:58 +00002430void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002431 // FPToSI is never a no-op cast, no need to check
2432 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002433 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002434 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2435 setValue(&I, Res);
2436
2437 if (DisableScheduling)
2438 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002439}
2440
Dan Gohman2048b852009-11-23 18:04:58 +00002441void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002442 // UIToFP is never a no-op cast, no need to check
2443 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002444 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002445 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2446 setValue(&I, Res);
2447
2448 if (DisableScheduling)
2449 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002450}
2451
Dan Gohman2048b852009-11-23 18:04:58 +00002452void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002453 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002454 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002455 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002456 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2457 setValue(&I, Res);
2458
2459 if (DisableScheduling)
2460 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002461}
2462
Dan Gohman2048b852009-11-23 18:04:58 +00002463void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002464 // What to do depends on the size of the integer and the size of the pointer.
2465 // We can either truncate, zero extend, or no-op, accordingly.
2466 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002467 EVT SrcVT = N.getValueType();
2468 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002469 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2470 setValue(&I, Res);
2471
2472 if (DisableScheduling)
2473 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002474}
2475
Dan Gohman2048b852009-11-23 18:04:58 +00002476void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002477 // What to do depends on the size of the integer and the size of the pointer.
2478 // We can either truncate, zero extend, or no-op, accordingly.
2479 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002480 EVT SrcVT = N.getValueType();
2481 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002482 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2483 setValue(&I, Res);
2484
2485 if (DisableScheduling)
2486 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002487}
2488
Dan Gohman2048b852009-11-23 18:04:58 +00002489void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002490 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002491 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002492
Bill Wendling49fcff82009-12-21 22:30:11 +00002493 // BitCast assures us that source and destination are the same size so this is
2494 // either a BIT_CONVERT or a no-op.
2495 if (DestVT != N.getValueType()) {
2496 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2497 DestVT, N); // convert types.
2498 setValue(&I, Res);
2499
2500 if (DisableScheduling)
2501 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2502 } else {
2503 setValue(&I, N); // noop cast.
2504 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002505}
2506
Dan Gohman2048b852009-11-23 18:04:58 +00002507void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002508 SDValue InVec = getValue(I.getOperand(0));
2509 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002510 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002511 TLI.getPointerTy(),
2512 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002513 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2514 TLI.getValueType(I.getType()),
2515 InVec, InVal, InIdx);
2516 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002517
Bill Wendling87710f02009-12-21 23:47:40 +00002518 if (DisableScheduling) {
2519 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002520 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002521 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002522}
2523
Dan Gohman2048b852009-11-23 18:04:58 +00002524void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002525 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002526 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002527 TLI.getPointerTy(),
2528 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002529 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2530 TLI.getValueType(I.getType()), InVec, InIdx);
2531 setValue(&I, Res);
2532
Bill Wendling87710f02009-12-21 23:47:40 +00002533 if (DisableScheduling) {
2534 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002535 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00002536 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002537}
2538
Mon P Wangaeb06d22008-11-10 04:46:22 +00002539
2540// Utility for visitShuffleVector - Returns true if the mask is mask starting
2541// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002542static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2543 unsigned MaskNumElts = Mask.size();
2544 for (unsigned i = 0; i != MaskNumElts; ++i)
2545 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002546 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002547 return true;
2548}
2549
Dan Gohman2048b852009-11-23 18:04:58 +00002550void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002551 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002552 SDValue Src1 = getValue(I.getOperand(0));
2553 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002554
Nate Begeman9008ca62009-04-27 18:41:29 +00002555 // Convert the ConstantVector mask operand into an array of ints, with -1
2556 // representing undef values.
2557 SmallVector<Constant*, 8> MaskElts;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002558 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
Owen Anderson001dbfe2009-07-16 18:04:31 +00002559 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002560 unsigned MaskNumElts = MaskElts.size();
2561 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002562 if (isa<UndefValue>(MaskElts[i]))
2563 Mask.push_back(-1);
2564 else
2565 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2566 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002567
Owen Andersone50ed302009-08-10 22:56:29 +00002568 EVT VT = TLI.getValueType(I.getType());
2569 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002570 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002571
Mon P Wangc7849c22008-11-16 05:06:27 +00002572 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002573 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2574 &Mask[0]);
2575 setValue(&I, Res);
2576
2577 if (DisableScheduling)
2578 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2579
Mon P Wangaeb06d22008-11-10 04:46:22 +00002580 return;
2581 }
2582
2583 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002584 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2585 // Mask is longer than the source vectors and is a multiple of the source
2586 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002587 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002588 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2589 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002590 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2591 VT, Src1, Src2);
2592 setValue(&I, Res);
2593
2594 if (DisableScheduling)
2595 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2596
Mon P Wangaeb06d22008-11-10 04:46:22 +00002597 return;
2598 }
2599
Mon P Wangc7849c22008-11-16 05:06:27 +00002600 // Pad both vectors with undefs to make them the same length as the mask.
2601 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002602 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2603 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002604 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002605
Nate Begeman9008ca62009-04-27 18:41:29 +00002606 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2607 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002608 MOps1[0] = Src1;
2609 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002610
2611 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2612 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002613 &MOps1[0], NumConcat);
2614 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002615 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002616 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002617
Mon P Wangaeb06d22008-11-10 04:46:22 +00002618 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002619 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002620 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002621 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002622 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002623 MappedOps.push_back(Idx);
2624 else
2625 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002626 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002627
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002628 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002629 &MappedOps[0]);
2630 setValue(&I, Res);
2631
Bill Wendlinge1a90422009-12-21 23:10:19 +00002632 if (DisableScheduling) {
2633 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2634 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002635 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002636 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002637
Mon P Wangaeb06d22008-11-10 04:46:22 +00002638 return;
2639 }
2640
Mon P Wangc7849c22008-11-16 05:06:27 +00002641 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002642 // Analyze the access pattern of the vector to see if we can extract
2643 // two subvectors and do the shuffle. The analysis is done by calculating
2644 // the range of elements the mask access on both vectors.
2645 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2646 int MaxRange[2] = {-1, -1};
2647
Nate Begeman5a5ca152009-04-29 05:20:52 +00002648 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002649 int Idx = Mask[i];
2650 int Input = 0;
2651 if (Idx < 0)
2652 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002653
Nate Begeman5a5ca152009-04-29 05:20:52 +00002654 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002655 Input = 1;
2656 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002657 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002658 if (Idx > MaxRange[Input])
2659 MaxRange[Input] = Idx;
2660 if (Idx < MinRange[Input])
2661 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002662 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002663
Mon P Wangc7849c22008-11-16 05:06:27 +00002664 // Check if the access is smaller than the vector size and can we find
2665 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002666 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2667 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002668 int StartIdx[2]; // StartIdx to extract from
2669 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002670 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002671 RangeUse[Input] = 0; // Unused
2672 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002673 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002674 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002675 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002676 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002677 RangeUse[Input] = 1; // Extract from beginning of the vector
2678 StartIdx[Input] = 0;
2679 } else {
2680 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002681 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002682 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002683 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002684 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002685 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002686 }
2687
Bill Wendling636e2582009-08-21 18:16:06 +00002688 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002689 SDValue Res = DAG.getUNDEF(VT);
2690 setValue(&I, Res); // Vectors are not used.
2691
2692 if (DisableScheduling)
2693 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2694
Mon P Wangc7849c22008-11-16 05:06:27 +00002695 return;
2696 }
2697 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2698 // Extract appropriate subvector and generate a vector shuffle
2699 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002700 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002701 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002702 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002703 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002704 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002705 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002706
2707 if (DisableScheduling)
2708 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002709 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002710
Mon P Wangc7849c22008-11-16 05:06:27 +00002711 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002712 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002713 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002714 int Idx = Mask[i];
2715 if (Idx < 0)
2716 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002717 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002718 MappedOps.push_back(Idx - StartIdx[0]);
2719 else
2720 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002721 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002722
2723 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2724 &MappedOps[0]);
2725 setValue(&I, Res);
2726
2727 if (DisableScheduling)
2728 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
2729
Mon P Wangc7849c22008-11-16 05:06:27 +00002730 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002731 }
2732 }
2733
Mon P Wangc7849c22008-11-16 05:06:27 +00002734 // We can't use either concat vectors or extract subvectors so fall back to
2735 // replacing the shuffle with extract and build vector.
2736 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002737 EVT EltVT = VT.getVectorElementType();
2738 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002739 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002740 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002741 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002742 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002743 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002744 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002745 SDValue Res;
2746
Nate Begeman5a5ca152009-04-29 05:20:52 +00002747 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002748 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2749 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002750 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002751 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2752 EltVT, Src2,
2753 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2754
2755 Ops.push_back(Res);
2756
2757 if (DisableScheduling)
2758 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002759 }
2760 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002761
2762 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2763 VT, &Ops[0], Ops.size());
2764 setValue(&I, Res);
2765
2766 if (DisableScheduling)
2767 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002768}
2769
Dan Gohman2048b852009-11-23 18:04:58 +00002770void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002771 const Value *Op0 = I.getOperand(0);
2772 const Value *Op1 = I.getOperand(1);
2773 const Type *AggTy = I.getType();
2774 const Type *ValTy = Op1->getType();
2775 bool IntoUndef = isa<UndefValue>(Op0);
2776 bool FromUndef = isa<UndefValue>(Op1);
2777
2778 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2779 I.idx_begin(), I.idx_end());
2780
Owen Andersone50ed302009-08-10 22:56:29 +00002781 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002782 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002783 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002784 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2785
2786 unsigned NumAggValues = AggValueVTs.size();
2787 unsigned NumValValues = ValValueVTs.size();
2788 SmallVector<SDValue, 4> Values(NumAggValues);
2789
2790 SDValue Agg = getValue(Op0);
2791 SDValue Val = getValue(Op1);
2792 unsigned i = 0;
2793 // Copy the beginning value(s) from the original aggregate.
2794 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002795 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796 SDValue(Agg.getNode(), Agg.getResNo() + i);
2797 // Copy values from the inserted value(s).
2798 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002799 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002800 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2801 // Copy remaining value(s) from the original aggregate.
2802 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002803 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002804 SDValue(Agg.getNode(), Agg.getResNo() + i);
2805
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002806 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2807 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2808 &Values[0], NumAggValues);
2809 setValue(&I, Res);
2810
2811 if (DisableScheduling)
2812 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002813}
2814
Dan Gohman2048b852009-11-23 18:04:58 +00002815void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002816 const Value *Op0 = I.getOperand(0);
2817 const Type *AggTy = Op0->getType();
2818 const Type *ValTy = I.getType();
2819 bool OutOfUndef = isa<UndefValue>(Op0);
2820
2821 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2822 I.idx_begin(), I.idx_end());
2823
Owen Andersone50ed302009-08-10 22:56:29 +00002824 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002825 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2826
2827 unsigned NumValValues = ValValueVTs.size();
2828 SmallVector<SDValue, 4> Values(NumValValues);
2829
2830 SDValue Agg = getValue(Op0);
2831 // Copy out the selected value(s).
2832 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2833 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002834 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002835 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002836 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002837
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002838 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2839 DAG.getVTList(&ValValueVTs[0], NumValValues),
2840 &Values[0], NumValValues);
2841 setValue(&I, Res);
2842
2843 if (DisableScheduling)
2844 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002845}
2846
Dan Gohman2048b852009-11-23 18:04:58 +00002847void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002848 SDValue N = getValue(I.getOperand(0));
2849 const Type *Ty = I.getOperand(0)->getType();
2850
2851 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2852 OI != E; ++OI) {
2853 Value *Idx = *OI;
2854 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2855 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2856 if (Field) {
2857 // N = N + Offset
2858 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002859 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002860 DAG.getIntPtrConstant(Offset));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002861
2862 if (DisableScheduling)
2863 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002864 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002865
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002866 Ty = StTy->getElementType(Field);
2867 } else {
2868 Ty = cast<SequentialType>(Ty)->getElementType();
2869
2870 // If this is a constant subscript, handle it quickly.
2871 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2872 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002873 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002874 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002875 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002876 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002877 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002878 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002879 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2880 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002881 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002882 else
Evan Chengb1032a82009-02-09 20:54:38 +00002883 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002884
Dale Johannesen66978ee2009-01-31 02:22:37 +00002885 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002886 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002887
2888 if (DisableScheduling) {
2889 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2890 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
2891 }
2892
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002893 continue;
2894 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002895
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002896 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002897 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2898 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002899 SDValue IdxN = getValue(Idx);
2900
2901 // If the index is smaller or larger than intptr_t, truncate or extend
2902 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002903 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002904
2905 // If this is a multiply by a power of two, turn it into a shl
2906 // immediately. This is a very common case.
2907 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002908 if (ElementSize.isPowerOf2()) {
2909 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002910 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002911 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002912 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002913 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002914 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002915 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002916 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002917 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002918
2919 if (DisableScheduling)
2920 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002921 }
2922
Scott Michelfdc40a02009-02-17 22:15:04 +00002923 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002924 N.getValueType(), N, IdxN);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002925
2926 if (DisableScheduling)
2927 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002928 }
2929 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002930
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002931 setValue(&I, N);
2932}
2933
Dan Gohman2048b852009-11-23 18:04:58 +00002934void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002935 // If this is a fixed sized alloca in the entry block of the function,
2936 // allocate it statically on the stack.
2937 if (FuncInfo.StaticAllocaMap.count(&I))
2938 return; // getValue will auto-populate this.
2939
2940 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002941 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002942 unsigned Align =
2943 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2944 I.getAlignment());
2945
2946 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002947
Chris Lattner0b18e592009-03-17 19:36:00 +00002948 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2949 AllocSize,
2950 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002951
Bill Wendling856ff412009-12-22 00:12:37 +00002952 if (DisableScheduling)
2953 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002954
Owen Andersone50ed302009-08-10 22:56:29 +00002955 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002956 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002957
Bill Wendling856ff412009-12-22 00:12:37 +00002958 if (DisableScheduling)
2959 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002960
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002961 // Handle alignment. If the requested alignment is less than or equal to
2962 // the stack alignment, ignore it. If the size is greater than or equal to
2963 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2964 unsigned StackAlign =
2965 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2966 if (Align <= StackAlign)
2967 Align = 0;
2968
2969 // Round the size of the allocation up to the stack alignment size
2970 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002971 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002972 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002973 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002974 if (DisableScheduling)
2975 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
2976
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002977 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002978 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002979 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002980 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling856ff412009-12-22 00:12:37 +00002981 if (DisableScheduling)
2982 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002983
2984 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002985 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002986 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002987 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002988 setValue(&I, DSA);
2989 DAG.setRoot(DSA.getValue(1));
2990
Bill Wendling856ff412009-12-22 00:12:37 +00002991 if (DisableScheduling)
2992 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
2993
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002994 // Inform the Frame Information that we have just allocated a variable-sized
2995 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002996 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002997}
2998
Dan Gohman2048b852009-11-23 18:04:58 +00002999void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003000 const Value *SV = I.getOperand(0);
3001 SDValue Ptr = getValue(SV);
3002
3003 const Type *Ty = I.getType();
3004 bool isVolatile = I.isVolatile();
3005 unsigned Alignment = I.getAlignment();
3006
Owen Andersone50ed302009-08-10 22:56:29 +00003007 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003008 SmallVector<uint64_t, 4> Offsets;
3009 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
3010 unsigned NumValues = ValueVTs.size();
3011 if (NumValues == 0)
3012 return;
3013
3014 SDValue Root;
3015 bool ConstantMemory = false;
3016 if (I.isVolatile())
3017 // Serialize volatile loads with other side effects.
3018 Root = getRoot();
3019 else if (AA->pointsToConstantMemory(SV)) {
3020 // Do not serialize (non-volatile) loads of constant memory with anything.
3021 Root = DAG.getEntryNode();
3022 ConstantMemory = true;
3023 } else {
3024 // Do not serialize non-volatile loads against each other.
3025 Root = DAG.getRoot();
3026 }
3027
3028 SmallVector<SDValue, 4> Values(NumValues);
3029 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003030 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003031 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00003032 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
3033 PtrVT, Ptr,
3034 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003035 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00003036 A, SV, Offsets[i], isVolatile, Alignment);
3037
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003038 Values[i] = L;
3039 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00003040
3041 if (DisableScheduling) {
3042 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
3043 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3044 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003045 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003046
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003047 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003048 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00003049 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003050 if (isVolatile)
3051 DAG.setRoot(Chain);
3052 else
3053 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00003054
3055 if (DisableScheduling)
3056 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003057 }
3058
Bill Wendling856ff412009-12-22 00:12:37 +00003059 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
3060 DAG.getVTList(&ValueVTs[0], NumValues),
3061 &Values[0], NumValues);
3062 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003063
Bill Wendling856ff412009-12-22 00:12:37 +00003064 if (DisableScheduling)
3065 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
3066}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003067
Dan Gohman2048b852009-11-23 18:04:58 +00003068void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003069 Value *SrcV = I.getOperand(0);
3070 Value *PtrV = I.getOperand(1);
3071
Owen Andersone50ed302009-08-10 22:56:29 +00003072 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003073 SmallVector<uint64_t, 4> Offsets;
3074 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
3075 unsigned NumValues = ValueVTs.size();
3076 if (NumValues == 0)
3077 return;
3078
3079 // Get the lowered operands. Note that we do this after
3080 // checking if NumResults is zero, because with zero results
3081 // the operands won't have values in the map.
3082 SDValue Src = getValue(SrcV);
3083 SDValue Ptr = getValue(PtrV);
3084
3085 SDValue Root = getRoot();
3086 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00003087 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003088 bool isVolatile = I.isVolatile();
3089 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00003090
3091 for (unsigned i = 0; i != NumValues; ++i) {
3092 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
3093 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00003094 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003095 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00003096 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003097
Bill Wendling856ff412009-12-22 00:12:37 +00003098 if (DisableScheduling) {
3099 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
3100 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
3101 }
3102 }
3103
3104 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
3105 MVT::Other, &Chains[0], NumValues);
3106 DAG.setRoot(Res);
3107
3108 if (DisableScheduling)
3109 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003110}
3111
3112/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
3113/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00003114void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
3115 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003116 bool HasChain = !I.doesNotAccessMemory();
3117 bool OnlyLoad = HasChain && I.onlyReadsMemory();
3118
3119 // Build the operand list.
3120 SmallVector<SDValue, 8> Ops;
3121 if (HasChain) { // If this intrinsic has side-effects, chainify it.
3122 if (OnlyLoad) {
3123 // We don't need to serialize loads against other loads.
3124 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003125 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003126 Ops.push_back(getRoot());
3127 }
3128 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003129
3130 // Info is set by getTgtMemInstrinsic
3131 TargetLowering::IntrinsicInfo Info;
3132 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
3133
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003134 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003135 if (!IsTgtIntrinsic)
3136 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003137
3138 // Add all operands of the call to the operand list.
3139 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3140 SDValue Op = getValue(I.getOperand(i));
3141 assert(TLI.isTypeLegal(Op.getValueType()) &&
3142 "Intrinsic uses a non-legal type?");
3143 Ops.push_back(Op);
3144 }
3145
Owen Andersone50ed302009-08-10 22:56:29 +00003146 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003147 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3148#ifndef NDEBUG
3149 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3150 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3151 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003152 }
Bob Wilson8d919552009-07-31 22:41:21 +00003153#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003155 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003156 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003157
Bob Wilson8d919552009-07-31 22:41:21 +00003158 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003159
3160 // Create the node.
3161 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003162 if (IsTgtIntrinsic) {
3163 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003164 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003165 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003166 Info.memVT, Info.ptrVal, Info.offset,
3167 Info.align, Info.vol,
3168 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003169 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003170 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003171 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003172 } else if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003173 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003174 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003175 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003176 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003177 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003178 }
3179
3180 if (DisableScheduling)
3181 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003182
3183 if (HasChain) {
3184 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3185 if (OnlyLoad)
3186 PendingLoads.push_back(Chain);
3187 else
3188 DAG.setRoot(Chain);
3189 }
Bill Wendling856ff412009-12-22 00:12:37 +00003190
Owen Anderson1d0be152009-08-13 21:58:54 +00003191 if (I.getType() != Type::getVoidTy(*DAG.getContext())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003192 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003193 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003194 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003195
3196 if (DisableScheduling)
3197 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003198 }
Bill Wendling856ff412009-12-22 00:12:37 +00003199
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003200 setValue(&I, Result);
3201 }
3202}
3203
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003204/// GetSignificand - Get the significand and build it into a floating-point
3205/// number with exponent of 1:
3206///
3207/// Op = (Op & 0x007fffff) | 0x3f800000;
3208///
3209/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003210static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003211GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003212 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3213 DAG.getConstant(0x007fffff, MVT::i32));
3214 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3215 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003216 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3217
3218 if (DisableScheduling) {
3219 DAG.AssignOrdering(t1.getNode(), Order);
3220 DAG.AssignOrdering(t2.getNode(), Order);
3221 DAG.AssignOrdering(Res.getNode(), Order);
3222 }
3223
3224 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003225}
3226
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003227/// GetExponent - Get the exponent:
3228///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003229/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003230///
3231/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003232static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003233GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003234 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003235 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3236 DAG.getConstant(0x7f800000, MVT::i32));
3237 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003238 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003239 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3240 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003241 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3242
3243 if (DisableScheduling) {
3244 DAG.AssignOrdering(t0.getNode(), Order);
3245 DAG.AssignOrdering(t1.getNode(), Order);
3246 DAG.AssignOrdering(t2.getNode(), Order);
3247 DAG.AssignOrdering(Res.getNode(), Order);
3248 }
3249
3250 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003251}
3252
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003253/// getF32Constant - Get 32-bit floating point constant.
3254static SDValue
3255getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003256 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003257}
3258
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003259/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003260/// visitIntrinsicCall: I is a call instruction
3261/// Op is the associated NodeType for I
3262const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003263SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003264 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003265 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003266 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003267 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003268 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003269 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003270 getValue(I.getOperand(2)),
3271 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003272 setValue(&I, L);
3273 DAG.setRoot(L.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00003274
3275 if (DisableScheduling)
3276 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
3277
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003278 return 0;
3279}
3280
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003281// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003282const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003283SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003284 SDValue Op1 = getValue(I.getOperand(1));
3285 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003286
Owen Anderson825b72b2009-08-11 20:47:22 +00003287 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003288 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003289
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003290 setValue(&I, Result);
Bill Wendling856ff412009-12-22 00:12:37 +00003291
3292 if (DisableScheduling)
3293 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
3294
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003295 return 0;
3296}
Bill Wendling74c37652008-12-09 22:08:41 +00003297
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003298/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3299/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003300void
Dan Gohman2048b852009-11-23 18:04:58 +00003301SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003302 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003303 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003304
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003306 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3307 SDValue Op = getValue(I.getOperand(1));
3308
3309 // Put the exponent in the right bit position for later addition to the
3310 // final result:
3311 //
3312 // #define LOG2OFe 1.4426950f
3313 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003314 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003315 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003316 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003317
3318 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3320 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003321
Bill Wendling856ff412009-12-22 00:12:37 +00003322 if (DisableScheduling) {
3323 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3324 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3325 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3326 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3327 }
3328
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003329 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003331 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003332
Bill Wendling856ff412009-12-22 00:12:37 +00003333 if (DisableScheduling)
3334 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3335
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003336 if (LimitFloatPrecision <= 6) {
3337 // For floating-point precision of 6:
3338 //
3339 // TwoToFractionalPartOfX =
3340 // 0.997535578f +
3341 // (0.735607626f + 0.252464424f * x) * x;
3342 //
3343 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003344 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003345 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003346 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003347 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003348 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3349 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003350 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003351 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003352
3353 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003354 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003355 TwoToFracPartOfX, IntegerPartOfX);
3356
Owen Anderson825b72b2009-08-11 20:47:22 +00003357 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003358
3359 if (DisableScheduling) {
3360 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3361 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3362 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3363 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3364 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3365 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3366 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3367 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003368 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3369 // For floating-point precision of 12:
3370 //
3371 // TwoToFractionalPartOfX =
3372 // 0.999892986f +
3373 // (0.696457318f +
3374 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3375 //
3376 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003377 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003378 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003380 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003381 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3382 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003383 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003384 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3385 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003386 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003387 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003388
3389 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003390 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003391 TwoToFracPartOfX, IntegerPartOfX);
3392
Owen Anderson825b72b2009-08-11 20:47:22 +00003393 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003394
3395 if (DisableScheduling) {
3396 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3397 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3398 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3399 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3400 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3401 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3402 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3403 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3404 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3405 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003406 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3407 // For floating-point precision of 18:
3408 //
3409 // TwoToFractionalPartOfX =
3410 // 0.999999982f +
3411 // (0.693148872f +
3412 // (0.240227044f +
3413 // (0.554906021e-1f +
3414 // (0.961591928e-2f +
3415 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3416 //
3417 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003418 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003419 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003420 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003421 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003422 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3423 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003424 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3426 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003427 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003428 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3429 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003430 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003431 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3432 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003433 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003434 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3435 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003436 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003437 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003438 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003439
3440 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003442 TwoToFracPartOfX, IntegerPartOfX);
3443
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003445
3446 if (DisableScheduling) {
3447 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3448 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3449 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3450 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3451 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3452 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3453 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3454 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3455 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3456 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3457 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3458 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3459 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3460 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3461 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3462 }
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003463 }
3464 } else {
3465 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003466 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003467 getValue(I.getOperand(1)).getValueType(),
3468 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003469 if (DisableScheduling)
3470 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003471 }
3472
Dale Johannesen59e577f2008-09-05 18:38:42 +00003473 setValue(&I, result);
3474}
3475
Bill Wendling39150252008-09-09 20:39:27 +00003476/// visitLog - Lower a log intrinsic. Handles the special sequences for
3477/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003478void
Dan Gohman2048b852009-11-23 18:04:58 +00003479SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003480 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003481 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003482
Owen Anderson825b72b2009-08-11 20:47:22 +00003483 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003484 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3485 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003487
Bill Wendling856ff412009-12-22 00:12:37 +00003488 if (DisableScheduling)
3489 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3490
Bill Wendling39150252008-09-09 20:39:27 +00003491 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003492 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003493 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003494 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003495
Bill Wendling856ff412009-12-22 00:12:37 +00003496 if (DisableScheduling)
3497 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3498
Bill Wendling39150252008-09-09 20:39:27 +00003499 // Get the significand and build it into a floating-point number with
3500 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003501 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003502
3503 if (LimitFloatPrecision <= 6) {
3504 // For floating-point precision of 6:
3505 //
3506 // LogofMantissa =
3507 // -1.1609546f +
3508 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003509 //
Bill Wendling39150252008-09-09 20:39:27 +00003510 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003512 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003513 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003514 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003515 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3516 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003517 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003518
Scott Michelfdc40a02009-02-17 22:15:04 +00003519 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003520 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003521
3522 if (DisableScheduling) {
3523 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3524 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3525 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3526 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3527 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3528 }
Bill Wendling39150252008-09-09 20:39:27 +00003529 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3530 // For floating-point precision of 12:
3531 //
3532 // LogOfMantissa =
3533 // -1.7417939f +
3534 // (2.8212026f +
3535 // (-1.4699568f +
3536 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3537 //
3538 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003539 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003540 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003542 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3544 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003545 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3547 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003548 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003549 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3550 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003551 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003552
Scott Michelfdc40a02009-02-17 22:15:04 +00003553 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003554 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003555
3556 if (DisableScheduling) {
3557 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3558 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3559 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3560 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3561 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3562 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3563 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3564 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3565 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3566 }
Bill Wendling39150252008-09-09 20:39:27 +00003567 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3568 // For floating-point precision of 18:
3569 //
3570 // LogOfMantissa =
3571 // -2.1072184f +
3572 // (4.2372794f +
3573 // (-3.7029485f +
3574 // (2.2781945f +
3575 // (-0.87823314f +
3576 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3577 //
3578 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003580 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003581 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003582 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003583 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3584 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003585 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003586 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3587 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003588 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003589 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3590 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003591 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003592 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3593 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003594 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003595 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3596 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003597 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003598
Scott Michelfdc40a02009-02-17 22:15:04 +00003599 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003600 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003601
3602 if (DisableScheduling) {
3603 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3604 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3605 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3606 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3607 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3608 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3609 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3610 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3611 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3612 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3613 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3614 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3615 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3616 }
Bill Wendling39150252008-09-09 20:39:27 +00003617 }
3618 } else {
3619 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003620 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003621 getValue(I.getOperand(1)).getValueType(),
3622 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003623
3624 if (DisableScheduling)
3625 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003626 }
3627
Dale Johannesen59e577f2008-09-05 18:38:42 +00003628 setValue(&I, result);
3629}
3630
Bill Wendling3eb59402008-09-09 00:28:24 +00003631/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3632/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003633void
Dan Gohman2048b852009-11-23 18:04:58 +00003634SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003635 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003636 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003637
Owen Anderson825b72b2009-08-11 20:47:22 +00003638 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003639 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3640 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003641 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003642
Bill Wendling856ff412009-12-22 00:12:37 +00003643 if (DisableScheduling)
3644 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3645
Bill Wendling39150252008-09-09 20:39:27 +00003646 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003647 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3648
3649 if (DisableScheduling)
3650 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003651
3652 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003653 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003654 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003655
Bill Wendling3eb59402008-09-09 00:28:24 +00003656 // Different possible minimax approximations of significand in
3657 // floating-point for various degrees of accuracy over [1,2].
3658 if (LimitFloatPrecision <= 6) {
3659 // For floating-point precision of 6:
3660 //
3661 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3662 //
3663 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003664 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003665 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003666 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003667 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003668 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3669 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003670 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003671
Scott Michelfdc40a02009-02-17 22:15:04 +00003672 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003673 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003674
3675 if (DisableScheduling) {
3676 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3677 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3678 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3679 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3680 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3681 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003682 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3683 // For floating-point precision of 12:
3684 //
3685 // Log2ofMantissa =
3686 // -2.51285454f +
3687 // (4.07009056f +
3688 // (-2.12067489f +
3689 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003690 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003691 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003692 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003693 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003694 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003695 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003696 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3697 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003698 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003699 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3700 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003701 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003702 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3703 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003704 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003705
Scott Michelfdc40a02009-02-17 22:15:04 +00003706 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003707 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003708
3709 if (DisableScheduling) {
3710 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3711 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3712 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3713 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3714 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3715 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3716 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3717 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3718 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3719 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003720 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3721 // For floating-point precision of 18:
3722 //
3723 // Log2ofMantissa =
3724 // -3.0400495f +
3725 // (6.1129976f +
3726 // (-5.3420409f +
3727 // (3.2865683f +
3728 // (-1.2669343f +
3729 // (0.27515199f -
3730 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3731 //
3732 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003733 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003734 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003735 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003736 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003737 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3738 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003739 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003740 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3741 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003742 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003743 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3744 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003745 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003746 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3747 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003748 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003749 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3750 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003751 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003752
Scott Michelfdc40a02009-02-17 22:15:04 +00003753 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003754 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003755
3756 if (DisableScheduling) {
3757 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3758 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3759 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3760 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3761 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3762 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3763 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3764 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3765 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3766 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3767 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3768 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3769 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3770 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003771 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003772 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003773 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003774 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003775 getValue(I.getOperand(1)).getValueType(),
3776 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003777
3778 if (DisableScheduling)
3779 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003780 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003781
Dale Johannesen59e577f2008-09-05 18:38:42 +00003782 setValue(&I, result);
3783}
3784
Bill Wendling3eb59402008-09-09 00:28:24 +00003785/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3786/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003787void
Dan Gohman2048b852009-11-23 18:04:58 +00003788SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003789 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003790 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003791
Owen Anderson825b72b2009-08-11 20:47:22 +00003792 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003793 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3794 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003795 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003796
Bill Wendling856ff412009-12-22 00:12:37 +00003797 if (DisableScheduling)
3798 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
3799
Bill Wendling39150252008-09-09 20:39:27 +00003800 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003801 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003802 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003803 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003804
Bill Wendling856ff412009-12-22 00:12:37 +00003805 if (DisableScheduling)
3806 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
3807
Bill Wendling3eb59402008-09-09 00:28:24 +00003808 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003809 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003810 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003811
3812 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003813 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003814 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003815 // Log10ofMantissa =
3816 // -0.50419619f +
3817 // (0.60948995f - 0.10380950f * x) * x;
3818 //
3819 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003820 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003821 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003822 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003823 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003824 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3825 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003826 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003827
Scott Michelfdc40a02009-02-17 22:15:04 +00003828 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003829 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003830
3831 if (DisableScheduling) {
3832 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3833 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3834 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3835 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3836 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3837 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003838 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3839 // For floating-point precision of 12:
3840 //
3841 // Log10ofMantissa =
3842 // -0.64831180f +
3843 // (0.91751397f +
3844 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3845 //
3846 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003847 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003848 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003849 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003850 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003851 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3852 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003853 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003854 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3855 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003856 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003857
Scott Michelfdc40a02009-02-17 22:15:04 +00003858 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003859 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003860
3861 if (DisableScheduling) {
3862 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3863 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3864 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3865 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3866 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3867 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3868 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3869 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003870 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003871 // For floating-point precision of 18:
3872 //
3873 // Log10ofMantissa =
3874 // -0.84299375f +
3875 // (1.5327582f +
3876 // (-1.0688956f +
3877 // (0.49102474f +
3878 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3879 //
3880 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003881 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003882 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003883 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003884 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003885 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3886 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003887 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003888 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3889 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003890 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003891 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3892 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003893 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003894 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3895 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003896 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003897
Scott Michelfdc40a02009-02-17 22:15:04 +00003898 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003899 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003900
3901 if (DisableScheduling) {
3902 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3903 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3904 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3905 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3906 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3907 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3908 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3909 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3910 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3911 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3912 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3913 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003914 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003915 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003916 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003917 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003918 getValue(I.getOperand(1)).getValueType(),
3919 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00003920
3921 if (DisableScheduling)
3922 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003923 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003924
Dale Johannesen59e577f2008-09-05 18:38:42 +00003925 setValue(&I, result);
3926}
3927
Bill Wendlinge10c8142008-09-09 22:39:21 +00003928/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3929/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003930void
Dan Gohman2048b852009-11-23 18:04:58 +00003931SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003932 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003933 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003934
Owen Anderson825b72b2009-08-11 20:47:22 +00003935 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003936 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3937 SDValue Op = getValue(I.getOperand(1));
3938
Owen Anderson825b72b2009-08-11 20:47:22 +00003939 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003940
Bill Wendling856ff412009-12-22 00:12:37 +00003941 if (DisableScheduling)
3942 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3943
Bill Wendlinge10c8142008-09-09 22:39:21 +00003944 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003945 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3946 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003947
3948 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003950 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003951
Bill Wendling856ff412009-12-22 00:12:37 +00003952 if (DisableScheduling) {
3953 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3954 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3955 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3956 }
3957
Bill Wendlinge10c8142008-09-09 22:39:21 +00003958 if (LimitFloatPrecision <= 6) {
3959 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003960 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003961 // TwoToFractionalPartOfX =
3962 // 0.997535578f +
3963 // (0.735607626f + 0.252464424f * x) * x;
3964 //
3965 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003966 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003967 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003968 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003969 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003970 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3971 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003972 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003973 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003974 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003975 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003976
Scott Michelfdc40a02009-02-17 22:15:04 +00003977 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003978 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003979
3980 if (DisableScheduling) {
3981 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3982 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3983 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3984 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3985 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3986 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3987 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
3988 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003989 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3990 // For floating-point precision of 12:
3991 //
3992 // TwoToFractionalPartOfX =
3993 // 0.999892986f +
3994 // (0.696457318f +
3995 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3996 //
3997 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003998 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003999 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004000 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004001 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004002 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4003 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004004 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004005 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4006 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004007 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004008 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004009 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004010 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004011
Scott Michelfdc40a02009-02-17 22:15:04 +00004012 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004013 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004014
4015 if (DisableScheduling) {
4016 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4017 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4018 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4019 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4020 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4021 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4022 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4023 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4024 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4025 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004026 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4027 // For floating-point precision of 18:
4028 //
4029 // TwoToFractionalPartOfX =
4030 // 0.999999982f +
4031 // (0.693148872f +
4032 // (0.240227044f +
4033 // (0.554906021e-1f +
4034 // (0.961591928e-2f +
4035 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4036 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004037 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004038 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004039 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004040 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004041 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4042 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004043 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004044 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4045 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004046 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004047 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4048 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004049 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004050 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4051 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004052 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004053 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4054 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004055 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004056 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004057 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004058 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00004059
Scott Michelfdc40a02009-02-17 22:15:04 +00004060 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004061 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004062
4063 if (DisableScheduling) {
4064 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4065 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4066 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4067 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4068 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4069 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4070 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4071 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4072 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4073 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4074 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4075 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4076 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4077 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4078 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4079 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004080 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00004081 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00004082 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004083 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00004084 getValue(I.getOperand(1)).getValueType(),
4085 getValue(I.getOperand(1)));
Bill Wendling856ff412009-12-22 00:12:37 +00004086
4087 if (DisableScheduling)
4088 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00004089 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00004090
Dale Johannesen601d3c02008-09-05 01:48:15 +00004091 setValue(&I, result);
4092}
4093
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004094/// visitPow - Lower a pow intrinsic. Handles the special sequences for
4095/// limited-precision mode with x == 10.0f.
4096void
Dan Gohman2048b852009-11-23 18:04:58 +00004097SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004098 SDValue result;
4099 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00004100 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004101 bool IsExp10 = false;
4102
Owen Anderson825b72b2009-08-11 20:47:22 +00004103 if (getValue(Val).getValueType() == MVT::f32 &&
4104 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004105 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4106 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
4107 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
4108 APFloat Ten(10.0f);
4109 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
4110 }
4111 }
4112 }
4113
4114 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
4115 SDValue Op = getValue(I.getOperand(2));
4116
4117 // Put the exponent in the right bit position for later addition to the
4118 // final result:
4119 //
4120 // #define LOG2OF10 3.3219281f
4121 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00004122 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004123 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00004124 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004125
4126 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00004127 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
4128 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004129
Bill Wendling856ff412009-12-22 00:12:37 +00004130 if (DisableScheduling) {
4131 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
4132 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
4133 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4134 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
4135 }
4136
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004137 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00004138 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00004139 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004140
Bill Wendling856ff412009-12-22 00:12:37 +00004141 if (DisableScheduling)
4142 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
4143
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004144 if (LimitFloatPrecision <= 6) {
4145 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004146 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004147 // twoToFractionalPartOfX =
4148 // 0.997535578f +
4149 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004150 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004151 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004152 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004153 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00004154 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004155 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00004156 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4157 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004158 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004159 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004160 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004161 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004162
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004163 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004164 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004165
4166 if (DisableScheduling) {
4167 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4168 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4169 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4170 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4171 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4172 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4173 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4174 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004175 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
4176 // For floating-point precision of 12:
4177 //
4178 // TwoToFractionalPartOfX =
4179 // 0.999892986f +
4180 // (0.696457318f +
4181 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
4182 //
4183 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004184 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004185 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004186 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004187 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00004188 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4189 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004190 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00004191 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4192 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004193 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00004194 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004195 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004196 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004197
Scott Michelfdc40a02009-02-17 22:15:04 +00004198 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004199 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004200
4201 if (DisableScheduling) {
4202 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4203 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4204 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4205 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4206 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4207 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4208 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4209 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4210 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4211 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004212 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
4213 // For floating-point precision of 18:
4214 //
4215 // TwoToFractionalPartOfX =
4216 // 0.999999982f +
4217 // (0.693148872f +
4218 // (0.240227044f +
4219 // (0.554906021e-1f +
4220 // (0.961591928e-2f +
4221 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4222 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004223 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004224 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004225 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004226 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004227 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4228 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004229 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4231 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004232 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004233 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4234 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004235 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004236 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4237 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004238 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004239 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4240 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004241 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004242 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004243 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004244 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004245
Scott Michelfdc40a02009-02-17 22:15:04 +00004246 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004247 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004248
4249 if (DisableScheduling) {
4250 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4251 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4252 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4253 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4254 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4255 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4256 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4257 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4258 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4259 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4260 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4261 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4262 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4263 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4264 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
4265 }
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004266 }
4267 } else {
4268 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004269 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004270 getValue(I.getOperand(1)).getValueType(),
4271 getValue(I.getOperand(1)),
4272 getValue(I.getOperand(2)));
Bill Wendling856ff412009-12-22 00:12:37 +00004273
4274 if (DisableScheduling)
4275 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004276 }
4277
4278 setValue(&I, result);
4279}
4280
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004281
4282/// ExpandPowI - Expand a llvm.powi intrinsic.
4283static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
4284 SelectionDAG &DAG) {
4285 // If RHS is a constant, we can expand this out to a multiplication tree,
4286 // otherwise we end up lowering to a call to __powidf2 (for example). When
4287 // optimizing for size, we only want to do this if the expansion would produce
4288 // a small number of multiplies, otherwise we do the full expansion.
4289 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4290 // Get the exponent as a positive value.
4291 unsigned Val = RHSC->getSExtValue();
4292 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004293
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004294 // powi(x, 0) -> 1.0
4295 if (Val == 0)
4296 return DAG.getConstantFP(1.0, LHS.getValueType());
4297
4298 Function *F = DAG.getMachineFunction().getFunction();
4299 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
4300 // If optimizing for size, don't insert too many multiplies. This
4301 // inserts up to 5 multiplies.
4302 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4303 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004304 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004305 // powi(x,15) generates one more multiply than it should), but this has
4306 // the benefit of being both really simple and much better than a libcall.
4307 SDValue Res; // Logically starts equal to 1.0
4308 SDValue CurSquare = LHS;
4309 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004310 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004311 if (Res.getNode())
4312 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4313 else
4314 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004315 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004316
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004317 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4318 CurSquare, CurSquare);
4319 Val >>= 1;
4320 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004321
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004322 // If the original was negative, invert the result, producing 1/(x*x*x).
4323 if (RHSC->getSExtValue() < 0)
4324 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4325 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4326 return Res;
4327 }
4328 }
4329
4330 // Otherwise, expand to a libcall.
4331 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4332}
4333
4334
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004335/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4336/// we want to emit this as a call to a named external function, return the name
4337/// otherwise lower it and return null.
4338const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004339SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004340 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004341 SDValue Res;
4342
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004343 switch (Intrinsic) {
4344 default:
4345 // By default, turn this into a target intrinsic node.
4346 visitTargetIntrinsic(I, Intrinsic);
4347 return 0;
4348 case Intrinsic::vastart: visitVAStart(I); return 0;
4349 case Intrinsic::vaend: visitVAEnd(I); return 0;
4350 case Intrinsic::vacopy: visitVACopy(I); return 0;
4351 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004352 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4353 getValue(I.getOperand(1)));
4354 setValue(&I, Res);
4355 if (DisableScheduling)
4356 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004357 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004358 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004359 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4360 getValue(I.getOperand(1)));
4361 setValue(&I, Res);
4362 if (DisableScheduling)
4363 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004364 return 0;
4365 case Intrinsic::setjmp:
4366 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004367 case Intrinsic::longjmp:
4368 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004369 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004370 SDValue Op1 = getValue(I.getOperand(1));
4371 SDValue Op2 = getValue(I.getOperand(2));
4372 SDValue Op3 = getValue(I.getOperand(3));
4373 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004374 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4375 I.getOperand(1), 0, I.getOperand(2), 0);
4376 DAG.setRoot(Res);
4377 if (DisableScheduling)
4378 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004379 return 0;
4380 }
Chris Lattner824b9582008-11-21 16:42:48 +00004381 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004382 SDValue Op1 = getValue(I.getOperand(1));
4383 SDValue Op2 = getValue(I.getOperand(2));
4384 SDValue Op3 = getValue(I.getOperand(3));
4385 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004386 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4387 I.getOperand(1), 0);
4388 DAG.setRoot(Res);
4389 if (DisableScheduling)
4390 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004391 return 0;
4392 }
Chris Lattner824b9582008-11-21 16:42:48 +00004393 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004394 SDValue Op1 = getValue(I.getOperand(1));
4395 SDValue Op2 = getValue(I.getOperand(2));
4396 SDValue Op3 = getValue(I.getOperand(3));
4397 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4398
4399 // If the source and destination are known to not be aliases, we can
4400 // lower memmove as memcpy.
4401 uint64_t Size = -1ULL;
4402 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004403 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004404 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4405 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004406 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4407 I.getOperand(1), 0, I.getOperand(2), 0);
4408 DAG.setRoot(Res);
4409 if (DisableScheduling)
4410 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004411 return 0;
4412 }
4413
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004414 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4415 I.getOperand(1), 0, I.getOperand(2), 0);
4416 DAG.setRoot(Res);
4417 if (DisableScheduling)
4418 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004419 return 0;
4420 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004421 case Intrinsic::dbg_stoppoint:
Devang Patel70d75ca2009-11-12 19:02:56 +00004422 case Intrinsic::dbg_region_start:
4423 case Intrinsic::dbg_region_end:
4424 case Intrinsic::dbg_func_start:
4425 // FIXME - Remove this instructions once the dust settles.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004426 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004427 case Intrinsic::dbg_declare: {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004428 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00004429 // FIXME: Variable debug info is not supported here.
4430 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004431 DwarfWriter *DW = DAG.getDwarfWriter();
4432 if (!DW)
4433 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004434 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00004435 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00004436 return 0;
4437
Devang Patelac1ceb32009-10-09 22:42:28 +00004438 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004439 Value *Address = DI.getAddress();
4440 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4441 Address = BCI->getOperand(0);
4442 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4443 // Don't handle byval struct arguments or VLAs, for example.
4444 if (!AI)
4445 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004446 DenseMap<const AllocaInst*, int>::iterator SI =
4447 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004448 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00004449 return 0; // VLAs.
4450 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004451
Chris Lattner3990b122009-12-28 23:41:32 +00004452 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo())
4453 if (MDNode *Dbg = DI.getMetadata("dbg"))
Chris Lattner0eb41982009-12-28 20:45:51 +00004454 MMI->setVariableDbgInfo(Variable, FI, Dbg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004455 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004456 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004457 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004458 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004459 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004460 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004461 SDValue Ops[1];
4462 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004463 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004464 setValue(&I, Op);
4465 DAG.setRoot(Op.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004466 if (DisableScheduling)
4467 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004468 return 0;
4469 }
4470
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004471 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004472 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004473
Chris Lattner3a5815f2009-09-17 23:54:54 +00004474 if (CurMBB->isLandingPad())
4475 AddCatchInfo(I, MMI, CurMBB);
4476 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004477#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004478 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004479#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004480 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4481 unsigned Reg = TLI.getExceptionSelectorRegister();
4482 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004483 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004484
Chris Lattner3a5815f2009-09-17 23:54:54 +00004485 // Insert the EHSELECTION instruction.
4486 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4487 SDValue Ops[2];
4488 Ops[0] = getValue(I.getOperand(1));
4489 Ops[1] = getRoot();
4490 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4491
4492 DAG.setRoot(Op.getValue(1));
4493
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004494 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4495 setValue(&I, Res);
4496 if (DisableScheduling) {
4497 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4498 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4499 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004500 return 0;
4501 }
4502
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004503 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004504 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004505
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004506 if (MMI) {
4507 // Find the type id for the given typeinfo.
4508 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004509 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004510 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004511 } else {
4512 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004513 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004514 }
4515
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004516 setValue(&I, Res);
4517 if (DisableScheduling)
4518 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004519 return 0;
4520 }
4521
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004522 case Intrinsic::eh_return_i32:
4523 case Intrinsic::eh_return_i64:
4524 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004525 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004526 Res = DAG.getNode(ISD::EH_RETURN, dl,
4527 MVT::Other,
4528 getControlRoot(),
4529 getValue(I.getOperand(1)),
4530 getValue(I.getOperand(2)));
4531 DAG.setRoot(Res);
4532 if (DisableScheduling)
4533 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004534 } else {
4535 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4536 }
4537
4538 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004539 case Intrinsic::eh_unwind_init:
4540 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4541 MMI->setCallsUnwindInit(true);
4542 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004543 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004544 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004545 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004546 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4547 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004548 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004549 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004550 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004551 TLI.getPointerTy()),
4552 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004553 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004554 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004555 DAG.getConstant(0, TLI.getPointerTy()));
4556 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4557 FA, Offset);
4558 setValue(&I, Res);
4559 if (DisableScheduling) {
4560 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4561 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4562 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4563 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
4564 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004565 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004566 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004567 case Intrinsic::convertff:
4568 case Intrinsic::convertfsi:
4569 case Intrinsic::convertfui:
4570 case Intrinsic::convertsif:
4571 case Intrinsic::convertuif:
4572 case Intrinsic::convertss:
4573 case Intrinsic::convertsu:
4574 case Intrinsic::convertus:
4575 case Intrinsic::convertuu: {
4576 ISD::CvtCode Code = ISD::CVT_INVALID;
4577 switch (Intrinsic) {
4578 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4579 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4580 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4581 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4582 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4583 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4584 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4585 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4586 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4587 }
Owen Andersone50ed302009-08-10 22:56:29 +00004588 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004589 Value *Op1 = I.getOperand(1);
4590 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4591 DAG.getValueType(DestVT),
4592 DAG.getValueType(getValue(Op1).getValueType()),
4593 getValue(I.getOperand(2)),
4594 getValue(I.getOperand(3)),
4595 Code);
4596 setValue(&I, Res);
4597 if (DisableScheduling)
4598 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004599 return 0;
4600 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004601 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004602 Res = DAG.getNode(ISD::FSQRT, dl,
4603 getValue(I.getOperand(1)).getValueType(),
4604 getValue(I.getOperand(1)));
4605 setValue(&I, Res);
4606 if (DisableScheduling)
4607 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004608 return 0;
4609 case Intrinsic::powi:
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004610 Res = ExpandPowI(dl, getValue(I.getOperand(1)), getValue(I.getOperand(2)),
4611 DAG);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004612 setValue(&I, Res);
4613 if (DisableScheduling)
4614 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004615 return 0;
4616 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004617 Res = DAG.getNode(ISD::FSIN, dl,
4618 getValue(I.getOperand(1)).getValueType(),
4619 getValue(I.getOperand(1)));
4620 setValue(&I, Res);
4621 if (DisableScheduling)
4622 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004623 return 0;
4624 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004625 Res = DAG.getNode(ISD::FCOS, dl,
4626 getValue(I.getOperand(1)).getValueType(),
4627 getValue(I.getOperand(1)));
4628 setValue(&I, Res);
4629 if (DisableScheduling)
4630 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004631 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004632 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004633 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004634 return 0;
4635 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004636 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004637 return 0;
4638 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004639 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004640 return 0;
4641 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004642 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004643 return 0;
4644 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004645 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004646 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004647 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004648 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004649 return 0;
4650 case Intrinsic::pcmarker: {
4651 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004652 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4653 DAG.setRoot(Res);
4654 if (DisableScheduling)
4655 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004656 return 0;
4657 }
4658 case Intrinsic::readcyclecounter: {
4659 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004660 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4661 DAG.getVTList(MVT::i64, MVT::Other),
4662 &Op, 1);
4663 setValue(&I, Res);
4664 DAG.setRoot(Res.getValue(1));
4665 if (DisableScheduling)
4666 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004667 return 0;
4668 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004669 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004670 Res = DAG.getNode(ISD::BSWAP, dl,
4671 getValue(I.getOperand(1)).getValueType(),
4672 getValue(I.getOperand(1)));
4673 setValue(&I, Res);
4674 if (DisableScheduling)
4675 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004676 return 0;
4677 case Intrinsic::cttz: {
4678 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004679 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004680 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4681 setValue(&I, Res);
4682 if (DisableScheduling)
4683 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004684 return 0;
4685 }
4686 case Intrinsic::ctlz: {
4687 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004688 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004689 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4690 setValue(&I, Res);
4691 if (DisableScheduling)
4692 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004693 return 0;
4694 }
4695 case Intrinsic::ctpop: {
4696 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004697 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004698 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4699 setValue(&I, Res);
4700 if (DisableScheduling)
4701 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004702 return 0;
4703 }
4704 case Intrinsic::stacksave: {
4705 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004706 Res = DAG.getNode(ISD::STACKSAVE, dl,
4707 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4708 setValue(&I, Res);
4709 DAG.setRoot(Res.getValue(1));
4710 if (DisableScheduling)
4711 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004712 return 0;
4713 }
4714 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004715 Res = getValue(I.getOperand(1));
4716 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4717 DAG.setRoot(Res);
4718 if (DisableScheduling)
4719 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004720 return 0;
4721 }
Bill Wendling57344502008-11-18 11:01:33 +00004722 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004723 // Emit code into the DAG to store the stack guard onto the stack.
4724 MachineFunction &MF = DAG.getMachineFunction();
4725 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004726 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004727
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004728 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4729 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004730
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004731 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004732 MFI->setStackProtectorIndex(FI);
4733
4734 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4735
4736 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004737 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4738 PseudoSourceValue::getFixedStack(FI),
4739 0, true);
4740 setValue(&I, Res);
4741 DAG.setRoot(Res);
4742 if (DisableScheduling)
4743 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004744 return 0;
4745 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004746 case Intrinsic::objectsize: {
4747 // If we don't know by now, we're never going to know.
4748 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4749
4750 assert(CI && "Non-constant type in __builtin_object_size?");
4751
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004752 SDValue Arg = getValue(I.getOperand(0));
4753 EVT Ty = Arg.getValueType();
4754
Eric Christopherd060b252009-12-23 02:51:48 +00004755 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004756 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004757 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004758 Res = DAG.getConstant(0, Ty);
4759
4760 setValue(&I, Res);
4761 if (DisableScheduling)
4762 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004763 return 0;
4764 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004765 case Intrinsic::var_annotation:
4766 // Discard annotate attributes
4767 return 0;
4768
4769 case Intrinsic::init_trampoline: {
4770 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4771
4772 SDValue Ops[6];
4773 Ops[0] = getRoot();
4774 Ops[1] = getValue(I.getOperand(1));
4775 Ops[2] = getValue(I.getOperand(2));
4776 Ops[3] = getValue(I.getOperand(3));
4777 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4778 Ops[5] = DAG.getSrcValue(F);
4779
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004780 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4781 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4782 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004783
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004784 setValue(&I, Res);
4785 DAG.setRoot(Res.getValue(1));
4786 if (DisableScheduling)
4787 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004788 return 0;
4789 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004790 case Intrinsic::gcroot:
4791 if (GFI) {
4792 Value *Alloca = I.getOperand(1);
4793 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004794
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004795 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4796 GFI->addStackRoot(FI->getIndex(), TypeMap);
4797 }
4798 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004799 case Intrinsic::gcread:
4800 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004801 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004803 case Intrinsic::flt_rounds:
4804 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4805 setValue(&I, Res);
4806 if (DisableScheduling)
4807 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004808 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004809 case Intrinsic::trap:
4810 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4811 DAG.setRoot(Res);
4812 if (DisableScheduling)
4813 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004814 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004815 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004816 return implVisitAluOverflow(I, ISD::UADDO);
4817 case Intrinsic::sadd_with_overflow:
4818 return implVisitAluOverflow(I, ISD::SADDO);
4819 case Intrinsic::usub_with_overflow:
4820 return implVisitAluOverflow(I, ISD::USUBO);
4821 case Intrinsic::ssub_with_overflow:
4822 return implVisitAluOverflow(I, ISD::SSUBO);
4823 case Intrinsic::umul_with_overflow:
4824 return implVisitAluOverflow(I, ISD::UMULO);
4825 case Intrinsic::smul_with_overflow:
4826 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 case Intrinsic::prefetch: {
4829 SDValue Ops[4];
4830 Ops[0] = getRoot();
4831 Ops[1] = getValue(I.getOperand(1));
4832 Ops[2] = getValue(I.getOperand(2));
4833 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004834 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4835 DAG.setRoot(Res);
4836 if (DisableScheduling)
4837 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004838 return 0;
4839 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004840
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004841 case Intrinsic::memory_barrier: {
4842 SDValue Ops[6];
4843 Ops[0] = getRoot();
4844 for (int x = 1; x < 6; ++x)
4845 Ops[x] = getValue(I.getOperand(x));
4846
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004847 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4848 DAG.setRoot(Res);
4849 if (DisableScheduling)
4850 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004851 return 0;
4852 }
4853 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004854 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004855 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004856 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004857 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4858 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004859 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004860 getValue(I.getOperand(2)),
4861 getValue(I.getOperand(3)),
4862 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004863 setValue(&I, L);
4864 DAG.setRoot(L.getValue(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004865 if (DisableScheduling)
4866 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004867 return 0;
4868 }
4869 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004870 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004871 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004872 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004873 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004874 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004875 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004876 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004877 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004878 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004879 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004880 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004881 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004882 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004883 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004884 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004885 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004886 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004887 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004888 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004889 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004890 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004891
4892 case Intrinsic::invariant_start:
4893 case Intrinsic::lifetime_start:
4894 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004895 Res = DAG.getUNDEF(TLI.getPointerTy());
4896 setValue(&I, Res);
4897 if (DisableScheduling)
4898 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004899 return 0;
4900 case Intrinsic::invariant_end:
4901 case Intrinsic::lifetime_end:
4902 // Discard region information.
4903 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004904 }
4905}
4906
Dan Gohman98ca4f22009-08-05 01:29:28 +00004907/// Test if the given instruction is in a position to be optimized
4908/// with a tail-call. This roughly means that it's in a block with
4909/// a return and there's nothing that needs to be scheduled
4910/// between it and the return.
4911///
4912/// This function only tests target-independent requirements.
4913/// For target-dependent requirements, a target should override
4914/// TargetLowering::IsEligibleForTailCallOptimization.
4915///
4916static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004917isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004918 const TargetLowering &TLI) {
4919 const BasicBlock *ExitBB = I->getParent();
4920 const TerminatorInst *Term = ExitBB->getTerminator();
4921 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4922 const Function *F = ExitBB->getParent();
4923
4924 // The block must end in a return statement or an unreachable.
4925 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4926
4927 // If I will have a chain, make sure no other instruction that will have a
4928 // chain interposes between I and the return.
4929 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4930 !I->isSafeToSpeculativelyExecute())
4931 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4932 --BBI) {
4933 if (&*BBI == I)
4934 break;
4935 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4936 !BBI->isSafeToSpeculativelyExecute())
4937 return false;
4938 }
4939
4940 // If the block ends with a void return or unreachable, it doesn't matter
4941 // what the call's return type is.
4942 if (!Ret || Ret->getNumOperands() == 0) return true;
4943
Dan Gohmaned9bab32009-11-14 02:06:30 +00004944 // If the return value is undef, it doesn't matter what the call's
4945 // return type is.
4946 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4947
Dan Gohman98ca4f22009-08-05 01:29:28 +00004948 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004949 // the return. Ignore noalias because it doesn't affect the call sequence.
4950 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4951 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004952 return false;
4953
4954 // Otherwise, make sure the unmodified return value of I is the return value.
4955 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4956 U = dyn_cast<Instruction>(U->getOperand(0))) {
4957 if (!U)
4958 return false;
4959 if (!U->hasOneUse())
4960 return false;
4961 if (U == I)
4962 break;
4963 // Check for a truly no-op truncate.
4964 if (isa<TruncInst>(U) &&
4965 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4966 continue;
4967 // Check for a truly no-op bitcast.
4968 if (isa<BitCastInst>(U) &&
4969 (U->getOperand(0)->getType() == U->getType() ||
4970 (isa<PointerType>(U->getOperand(0)->getType()) &&
4971 isa<PointerType>(U->getType()))))
4972 continue;
4973 // Otherwise it's not a true no-op.
4974 return false;
4975 }
4976
4977 return true;
4978}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004979
Dan Gohman2048b852009-11-23 18:04:58 +00004980void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4981 bool isTailCall,
4982 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004983 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4984 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004985 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004986 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4987 unsigned BeginLabel = 0, EndLabel = 0;
4988
4989 TargetLowering::ArgListTy Args;
4990 TargetLowering::ArgListEntry Entry;
4991 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004992
4993 // Check whether the function can return without sret-demotion.
4994 SmallVector<EVT, 4> OutVTs;
4995 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4996 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004997 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004998 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004999
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005000 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005001 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
5002
5003 SDValue DemoteStackSlot;
5004
5005 if (!CanLowerReturn) {
5006 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
5007 FTy->getReturnType());
5008 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
5009 FTy->getReturnType());
5010 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005011 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005012 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
5013
5014 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
5015 Entry.Node = DemoteStackSlot;
5016 Entry.Ty = StackSlotPtrType;
5017 Entry.isSExt = false;
5018 Entry.isZExt = false;
5019 Entry.isInReg = false;
5020 Entry.isSRet = true;
5021 Entry.isNest = false;
5022 Entry.isByVal = false;
5023 Entry.Alignment = Align;
5024 Args.push_back(Entry);
5025 RetTy = Type::getVoidTy(FTy->getContext());
5026 }
5027
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005028 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005029 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005030 SDValue ArgNode = getValue(*i);
5031 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
5032
5033 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00005034 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
5035 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
5036 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
5037 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
5038 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
5039 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005040 Entry.Alignment = CS.getParamAlignment(attrInd);
5041 Args.push_back(Entry);
5042 }
5043
5044 if (LandingPad && MMI) {
5045 // Insert a label before the invoke call to mark the try range. This can be
5046 // used to detect deletion of the invoke via the MachineModuleInfo.
5047 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00005048
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005049 // Both PendingLoads and PendingExports must be flushed here;
5050 // this call might not return.
5051 (void)getRoot();
Bill Wendling0d580132009-12-23 01:28:19 +00005052 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5053 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005054 }
5055
Dan Gohman98ca4f22009-08-05 01:29:28 +00005056 // Check if target-independent constraints permit a tail call here.
5057 // Target-dependent constraints are checked within TLI.LowerCallTo.
5058 if (isTailCall &&
5059 !isInTailCallPosition(CS.getInstruction(),
5060 CS.getAttributes().getRetAttributes(),
5061 TLI))
5062 isTailCall = false;
5063
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005064 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005065 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00005066 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005067 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005068 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00005069 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00005070 isTailCall,
5071 !CS.getInstruction()->use_empty(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005072 Callee, Args, DAG, getCurDebugLoc(), SDNodeOrder);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005073 assert((isTailCall || Result.second.getNode()) &&
5074 "Non-null chain expected with non-tail call!");
5075 assert((Result.second.getNode() || !Result.first.getNode()) &&
5076 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00005077 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005078 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005079 if (DisableScheduling)
5080 DAG.AssignOrdering(Result.first.getNode(), SDNodeOrder);
5081 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005082 // The instruction result is the result of loading from the
5083 // hidden sret parameter.
5084 SmallVector<EVT, 1> PVTs;
5085 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
5086
5087 ComputeValueVTs(TLI, PtrRetTy, PVTs);
5088 assert(PVTs.size() == 1 && "Pointers should fit in one register");
5089 EVT PtrVT = PVTs[0];
5090 unsigned NumValues = OutVTs.size();
5091 SmallVector<SDValue, 4> Values(NumValues);
5092 SmallVector<SDValue, 4> Chains(NumValues);
5093
5094 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00005095 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
5096 DemoteStackSlot,
5097 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005098 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
Bill Wendlinge80ae832009-12-22 00:50:32 +00005099 Add, NULL, Offsets[i], false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005100 Values[i] = L;
5101 Chains[i] = L.getValue(1);
5102 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005103
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005104 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
5105 MVT::Other, &Chains[0], NumValues);
5106 PendingLoads.push_back(Chain);
5107
Bill Wendlinge80ae832009-12-22 00:50:32 +00005108 SDValue MV = DAG.getNode(ISD::MERGE_VALUES,
5109 getCurDebugLoc(),
5110 DAG.getVTList(&OutVTs[0], NumValues),
5111 &Values[0], NumValues);
5112 setValue(CS.getInstruction(), MV);
5113
5114 if (DisableScheduling) {
5115 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
5116 DAG.AssignOrdering(MV.getNode(), SDNodeOrder);
5117 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005118 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00005119
5120 // As a special case, a null chain means that a tail call has been emitted and
5121 // the DAG root is already updated.
5122 if (Result.second.getNode()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005123 DAG.setRoot(Result.second);
Bill Wendlinge80ae832009-12-22 00:50:32 +00005124 if (DisableScheduling)
5125 DAG.AssignOrdering(Result.second.getNode(), SDNodeOrder);
5126 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00005127 HasTailCall = true;
Bill Wendlinge80ae832009-12-22 00:50:32 +00005128 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005129
5130 if (LandingPad && MMI) {
5131 // Insert a label at the end of the invoke call to mark the try range. This
5132 // can be used to detect deletion of the invoke via the MachineModuleInfo.
5133 EndLabel = MMI->NextLabelID();
Bill Wendling0d580132009-12-23 01:28:19 +00005134 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
5135 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005136
5137 // Inform MachineModuleInfo of range.
5138 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
5139 }
5140}
5141
Chris Lattner8047d9a2009-12-24 00:37:38 +00005142/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
5143/// value is equal or not-equal to zero.
5144static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
5145 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
5146 UI != E; ++UI) {
5147 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
5148 if (IC->isEquality())
5149 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
5150 if (C->isNullValue())
5151 continue;
5152 // Unknown instruction.
5153 return false;
5154 }
5155 return true;
5156}
5157
Chris Lattner04b091a2009-12-24 01:07:17 +00005158static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00005159 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005160
Chris Lattner8047d9a2009-12-24 00:37:38 +00005161 // Check to see if this load can be trivially constant folded, e.g. if the
5162 // input is from a string literal.
5163 if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
5164 // Cast pointer to the type we really want to load.
5165 LoadInput = ConstantExpr::getBitCast(LoadInput,
5166 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005167
Chris Lattner8047d9a2009-12-24 00:37:38 +00005168 if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD))
5169 return Builder.getValue(LoadCst);
5170 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005171
Chris Lattner8047d9a2009-12-24 00:37:38 +00005172 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
5173 // still constant memory, the input chain can be the entry node.
5174 SDValue Root;
5175 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005176
Chris Lattner8047d9a2009-12-24 00:37:38 +00005177 // Do not serialize (non-volatile) loads of constant memory with anything.
5178 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
5179 Root = Builder.DAG.getEntryNode();
5180 ConstantMemory = true;
5181 } else {
5182 // Do not serialize non-volatile loads against each other.
5183 Root = Builder.DAG.getRoot();
5184 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005185
Chris Lattner8047d9a2009-12-24 00:37:38 +00005186 SDValue Ptr = Builder.getValue(PtrVal);
5187 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
5188 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
5189 false /*volatile*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005190
Chris Lattner8047d9a2009-12-24 00:37:38 +00005191 if (!ConstantMemory)
5192 Builder.PendingLoads.push_back(LoadVal.getValue(1));
5193 return LoadVal;
5194}
5195
5196
5197/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
5198/// If so, return true and lower it, otherwise return false and it will be
5199/// lowered like a normal call.
5200bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) {
5201 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
5202 if (I.getNumOperands() != 4)
5203 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005204
Chris Lattner8047d9a2009-12-24 00:37:38 +00005205 Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
5206 if (!isa<PointerType>(LHS->getType()) || !isa<PointerType>(RHS->getType()) ||
5207 !isa<IntegerType>(I.getOperand(3)->getType()) ||
5208 !isa<IntegerType>(I.getType()))
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005209 return false;
5210
Chris Lattner8047d9a2009-12-24 00:37:38 +00005211 ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005212
Chris Lattner8047d9a2009-12-24 00:37:38 +00005213 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
5214 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00005215 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
5216 bool ActuallyDoIt = true;
5217 MVT LoadVT;
5218 const Type *LoadTy;
5219 switch (Size->getZExtValue()) {
5220 default:
5221 LoadVT = MVT::Other;
5222 LoadTy = 0;
5223 ActuallyDoIt = false;
5224 break;
5225 case 2:
5226 LoadVT = MVT::i16;
5227 LoadTy = Type::getInt16Ty(Size->getContext());
5228 break;
5229 case 4:
5230 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005231 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005232 break;
5233 case 8:
5234 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005235 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005236 break;
5237 /*
5238 case 16:
5239 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005240 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00005241 LoadTy = VectorType::get(LoadTy, 4);
5242 break;
5243 */
5244 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005245
Chris Lattner04b091a2009-12-24 01:07:17 +00005246 // This turns into unaligned loads. We only do this if the target natively
5247 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5248 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005249
Chris Lattner04b091a2009-12-24 01:07:17 +00005250 // Require that we can find a legal MVT, and only do this if the target
5251 // supports unaligned loads of that type. Expanding into byte loads would
5252 // bloat the code.
5253 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5254 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5255 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5256 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5257 ActuallyDoIt = false;
5258 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005259
Chris Lattner04b091a2009-12-24 01:07:17 +00005260 if (ActuallyDoIt) {
5261 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5262 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005263
Chris Lattner04b091a2009-12-24 01:07:17 +00005264 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
5265 ISD::SETNE);
5266 EVT CallVT = TLI.getValueType(I.getType(), true);
5267 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
5268 return true;
5269 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005270 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005271
5272
Chris Lattner8047d9a2009-12-24 00:37:38 +00005273 return false;
5274}
5275
5276
Dan Gohman2048b852009-11-23 18:04:58 +00005277void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005278 const char *RenameFn = 0;
5279 if (Function *F = I.getCalledFunction()) {
5280 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005281 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
5282 if (II) {
5283 if (unsigned IID = II->getIntrinsicID(F)) {
5284 RenameFn = visitIntrinsicCall(I, IID);
5285 if (!RenameFn)
5286 return;
5287 }
5288 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005289 if (unsigned IID = F->getIntrinsicID()) {
5290 RenameFn = visitIntrinsicCall(I, IID);
5291 if (!RenameFn)
5292 return;
5293 }
5294 }
5295
5296 // Check for well-known libc/libm calls. If the function is internal, it
5297 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005298 if (!F->hasLocalLinkage() && F->hasName()) {
5299 StringRef Name = F->getName();
5300 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005301 if (I.getNumOperands() == 3 && // Basic sanity checks.
5302 I.getOperand(1)->getType()->isFloatingPoint() &&
5303 I.getType() == I.getOperand(1)->getType() &&
5304 I.getType() == I.getOperand(2)->getType()) {
5305 SDValue LHS = getValue(I.getOperand(1));
5306 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00005307 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5308 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005309 return;
5310 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005311 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005312 if (I.getNumOperands() == 2 && // Basic sanity checks.
5313 I.getOperand(1)->getType()->isFloatingPoint() &&
5314 I.getType() == I.getOperand(1)->getType()) {
5315 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005316 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
5317 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005318 return;
5319 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005320 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005321 if (I.getNumOperands() == 2 && // Basic sanity checks.
5322 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005323 I.getType() == I.getOperand(1)->getType() &&
5324 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005325 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005326 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5327 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005328 return;
5329 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005330 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005331 if (I.getNumOperands() == 2 && // Basic sanity checks.
5332 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005333 I.getType() == I.getOperand(1)->getType() &&
5334 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005335 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005336 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5337 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005338 return;
5339 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005340 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5341 if (I.getNumOperands() == 2 && // Basic sanity checks.
5342 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005343 I.getType() == I.getOperand(1)->getType() &&
5344 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005345 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005346 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5347 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005348 return;
5349 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005350 } else if (Name == "memcmp") {
5351 if (visitMemCmpCall(I))
5352 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005353 }
5354 }
5355 } else if (isa<InlineAsm>(I.getOperand(0))) {
5356 visitInlineAsm(&I);
5357 return;
5358 }
5359
5360 SDValue Callee;
5361 if (!RenameFn)
5362 Callee = getValue(I.getOperand(0));
5363 else
Bill Wendling056292f2008-09-16 21:48:12 +00005364 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005365
Bill Wendling0d580132009-12-23 01:28:19 +00005366 // Check if we can potentially perform a tail call. More detailed checking is
5367 // be done within LowerCallTo, after more information about the call is known.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005368 bool isTailCall = PerformTailCallOpt && I.isTailCall();
5369
5370 LowerCallTo(&I, Callee, isTailCall);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005371}
5372
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005373/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005374/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005375/// Chain/Flag as the input and updates them for the output Chain/Flag.
5376/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005377SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005378 unsigned Order, SDValue &Chain,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005379 SDValue *Flag) const {
5380 // Assemble the legal parts into the final values.
5381 SmallVector<SDValue, 4> Values(ValueVTs.size());
5382 SmallVector<SDValue, 8> Parts;
5383 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5384 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005385 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005386 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005387 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005388
5389 Parts.resize(NumRegs);
5390 for (unsigned i = 0; i != NumRegs; ++i) {
5391 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00005392 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005393 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005394 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005395 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005396 *Flag = P.getValue(2);
5397 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005398
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005399 Chain = P.getValue(1);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005400
Bill Wendlingec72e322009-12-22 01:11:43 +00005401 if (DisableScheduling)
5402 DAG.AssignOrdering(P.getNode(), Order);
5403
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005404 // If the source register was virtual and if we know something about it,
5405 // add an assert node.
5406 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5407 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5408 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5409 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5410 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5411 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005412
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005413 unsigned RegSize = RegisterVT.getSizeInBits();
5414 unsigned NumSignBits = LOI.NumSignBits;
5415 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005416
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005417 // FIXME: We capture more information than the dag can represent. For
5418 // now, just use the tightest assertzext/assertsext possible.
5419 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005420 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005421 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005422 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005423 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005424 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005425 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005426 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005427 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005428 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005429 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005430 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005431 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005432 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005433 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005434 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005435 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005436 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005437
Owen Anderson825b72b2009-08-11 20:47:22 +00005438 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005439 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005440 RegisterVT, P, DAG.getValueType(FromVT));
5441
Bill Wendlingec72e322009-12-22 01:11:43 +00005442 if (DisableScheduling)
5443 DAG.AssignOrdering(P.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005444 }
5445 }
5446 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005447
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005448 Parts[i] = P;
5449 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005450
Bill Wendling3ea3c242009-12-22 02:10:19 +00005451 Values[Value] = getCopyFromParts(DAG, dl, Order, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005452 NumRegs, RegisterVT, ValueVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005453 if (DisableScheduling)
5454 DAG.AssignOrdering(Values[Value].getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005455 Part += NumRegs;
5456 Parts.clear();
5457 }
5458
Bill Wendlingec72e322009-12-22 01:11:43 +00005459 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5460 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5461 &Values[0], ValueVTs.size());
5462 if (DisableScheduling)
5463 DAG.AssignOrdering(Res.getNode(), Order);
5464 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005465}
5466
5467/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005468/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005469/// Chain/Flag as the input and updates them for the output Chain/Flag.
5470/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005471void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005472 unsigned Order, SDValue &Chain,
5473 SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 // Get the list of the values's legal parts.
5475 unsigned NumRegs = Regs.size();
5476 SmallVector<SDValue, 8> Parts(NumRegs);
5477 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005478 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005479 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005480 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005481
Bill Wendling3ea3c242009-12-22 02:10:19 +00005482 getCopyToParts(DAG, dl, Order,
5483 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005484 &Parts[Part], NumParts, RegisterVT);
5485 Part += NumParts;
5486 }
5487
5488 // Copy the parts into the registers.
5489 SmallVector<SDValue, 8> Chains(NumRegs);
5490 for (unsigned i = 0; i != NumRegs; ++i) {
5491 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00005492 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005493 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00005494 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005495 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005496 *Flag = Part.getValue(1);
5497 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005498
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005499 Chains[i] = Part.getValue(0);
Bill Wendlingec72e322009-12-22 01:11:43 +00005500
5501 if (DisableScheduling)
5502 DAG.AssignOrdering(Part.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005503 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005504
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005505 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005506 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005507 // flagged to it. That is the CopyToReg nodes and the user are considered
5508 // a single scheduling unit. If we create a TokenFactor and return it as
5509 // chain, then the TokenFactor is both a predecessor (operand) of the
5510 // user as well as a successor (the TF operands are flagged to the user).
5511 // c1, f1 = CopyToReg
5512 // c2, f2 = CopyToReg
5513 // c3 = TokenFactor c1, c2
5514 // ...
5515 // = op c3, ..., f2
5516 Chain = Chains[NumRegs-1];
5517 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005518 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Bill Wendlingec72e322009-12-22 01:11:43 +00005519
5520 if (DisableScheduling)
5521 DAG.AssignOrdering(Chain.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005522}
5523
5524/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005525/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005526/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005527void RegsForValue::AddInlineAsmOperands(unsigned Code,
5528 bool HasMatching,unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +00005529 SelectionDAG &DAG, unsigned Order,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005530 std::vector<SDValue> &Ops) const {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005531 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5532 unsigned Flag = Code | (Regs.size() << 3);
5533 if (HasMatching)
5534 Flag |= 0x80000000 | (MatchingIdx << 16);
Dale Johannesen99499332009-12-23 07:32:51 +00005535 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00005536 Ops.push_back(Res);
5537
5538 if (DisableScheduling)
5539 DAG.AssignOrdering(Res.getNode(), Order);
5540
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005541 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005542 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005543 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005544 for (unsigned i = 0; i != NumRegs; ++i) {
5545 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling651ad132009-12-22 01:25:10 +00005546 SDValue Res = DAG.getRegister(Regs[Reg++], RegisterVT);
5547 Ops.push_back(Res);
5548
5549 if (DisableScheduling)
5550 DAG.AssignOrdering(Res.getNode(), Order);
Chris Lattner58f15c42008-10-17 16:21:11 +00005551 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005552 }
5553}
5554
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005555/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005556/// i.e. it isn't a stack pointer or some other special register, return the
5557/// register class for the register. Otherwise, return null.
5558static const TargetRegisterClass *
5559isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5560 const TargetLowering &TLI,
5561 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005562 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005563 const TargetRegisterClass *FoundRC = 0;
5564 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5565 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005566 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005567
5568 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005569 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005570 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5571 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5572 I != E; ++I) {
5573 if (TLI.isTypeLegal(*I)) {
5574 // If we have already found this register in a different register class,
5575 // choose the one with the largest VT specified. For example, on
5576 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005577 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005578 ThisVT = *I;
5579 break;
5580 }
5581 }
5582 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005583
Owen Anderson825b72b2009-08-11 20:47:22 +00005584 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005585
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005586 // NOTE: This isn't ideal. In particular, this might allocate the
5587 // frame pointer in functions that need it (due to them not being taken
5588 // out of allocation, because a variable sized allocation hasn't been seen
5589 // yet). This is a slight code pessimization, but should still work.
5590 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5591 E = RC->allocation_order_end(MF); I != E; ++I)
5592 if (*I == Reg) {
5593 // We found a matching register class. Keep looking at others in case
5594 // we find one with larger registers that this physreg is also in.
5595 FoundRC = RC;
5596 FoundVT = ThisVT;
5597 break;
5598 }
5599 }
5600 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005601}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005602
5603
5604namespace llvm {
5605/// AsmOperandInfo - This contains information for each constraint that we are
5606/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005607class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005608 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005609public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005610 /// CallOperand - If this is the result output operand or a clobber
5611 /// this is null, otherwise it is the incoming operand to the CallInst.
5612 /// This gets modified as the asm is processed.
5613 SDValue CallOperand;
5614
5615 /// AssignedRegs - If this is a register or register class operand, this
5616 /// contains the set of register corresponding to the operand.
5617 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005619 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5620 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5621 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005622
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005623 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5624 /// busy in OutputRegs/InputRegs.
5625 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005626 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005627 std::set<unsigned> &InputRegs,
5628 const TargetRegisterInfo &TRI) const {
5629 if (isOutReg) {
5630 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5631 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5632 }
5633 if (isInReg) {
5634 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5635 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5636 }
5637 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005638
Owen Andersone50ed302009-08-10 22:56:29 +00005639 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005640 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005641 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005642 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005643 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005644 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005645 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005646
Chris Lattner81249c92008-10-17 17:05:25 +00005647 if (isa<BasicBlock>(CallOperandVal))
5648 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005649
Chris Lattner81249c92008-10-17 17:05:25 +00005650 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005651
Chris Lattner81249c92008-10-17 17:05:25 +00005652 // If this is an indirect operand, the operand is a pointer to the
5653 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005654 if (isIndirect) {
5655 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5656 if (!PtrTy)
5657 llvm_report_error("Indirect operand for inline asm not a pointer!");
5658 OpTy = PtrTy->getElementType();
5659 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005660
Chris Lattner81249c92008-10-17 17:05:25 +00005661 // If OpTy is not a single value, it may be a struct/union that we
5662 // can tile with integers.
5663 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5664 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5665 switch (BitSize) {
5666 default: break;
5667 case 1:
5668 case 8:
5669 case 16:
5670 case 32:
5671 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005672 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005673 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005674 break;
5675 }
5676 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005677
Chris Lattner81249c92008-10-17 17:05:25 +00005678 return TLI.getValueType(OpTy, true);
5679 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005681private:
5682 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5683 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005684 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005685 const TargetRegisterInfo &TRI) {
5686 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5687 Regs.insert(Reg);
5688 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5689 for (; *Aliases; ++Aliases)
5690 Regs.insert(*Aliases);
5691 }
5692};
5693} // end llvm namespace.
5694
5695
5696/// GetRegistersForValue - Assign registers (virtual or physical) for the
5697/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005698/// register allocator to handle the assignment process. However, if the asm
5699/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005700/// allocation. This produces generally horrible, but correct, code.
5701///
5702/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005703/// Input and OutputRegs are the set of already allocated physical registers.
5704///
Dan Gohman2048b852009-11-23 18:04:58 +00005705void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005706GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005707 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005708 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005709 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005710
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005711 // Compute whether this value requires an input register, an output register,
5712 // or both.
5713 bool isOutReg = false;
5714 bool isInReg = false;
5715 switch (OpInfo.Type) {
5716 case InlineAsm::isOutput:
5717 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005718
5719 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005720 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005721 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005722 break;
5723 case InlineAsm::isInput:
5724 isInReg = true;
5725 isOutReg = false;
5726 break;
5727 case InlineAsm::isClobber:
5728 isOutReg = true;
5729 isInReg = true;
5730 break;
5731 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005732
5733
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005734 MachineFunction &MF = DAG.getMachineFunction();
5735 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005736
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005737 // If this is a constraint for a single physreg, or a constraint for a
5738 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005739 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005740 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5741 OpInfo.ConstraintVT);
5742
5743 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005744 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005745 // If this is a FP input in an integer register (or visa versa) insert a bit
5746 // cast of the input value. More generally, handle any case where the input
5747 // value disagrees with the register class we plan to stick this in.
5748 if (OpInfo.Type == InlineAsm::isInput &&
5749 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005750 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005751 // types are identical size, use a bitcast to convert (e.g. two differing
5752 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005753 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005754 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005755 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005756 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005757 OpInfo.ConstraintVT = RegVT;
5758 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5759 // If the input is a FP value and we want it in FP registers, do a
5760 // bitcast to the corresponding integer type. This turns an f64 value
5761 // into i64, which can be passed with two i32 values on a 32-bit
5762 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005763 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005764 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005765 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005766 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005767 OpInfo.ConstraintVT = RegVT;
5768 }
Bill Wendling651ad132009-12-22 01:25:10 +00005769
5770 if (DisableScheduling)
5771 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Chris Lattner01426e12008-10-21 00:45:36 +00005772 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005773
Owen Anderson23b9b192009-08-12 00:36:31 +00005774 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005775 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005776
Owen Andersone50ed302009-08-10 22:56:29 +00005777 EVT RegVT;
5778 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005779
5780 // If this is a constraint for a specific physical register, like {r17},
5781 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005782 if (unsigned AssignedReg = PhysReg.first) {
5783 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005784 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005785 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005786
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005787 // Get the actual register value type. This is important, because the user
5788 // may have asked for (e.g.) the AX register in i32 type. We need to
5789 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005790 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005791
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005792 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005793 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005794
5795 // If this is an expanded reference, add the rest of the regs to Regs.
5796 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005797 TargetRegisterClass::iterator I = RC->begin();
5798 for (; *I != AssignedReg; ++I)
5799 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005800
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005801 // Already added the first reg.
5802 --NumRegs; ++I;
5803 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005804 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005805 Regs.push_back(*I);
5806 }
5807 }
Bill Wendling651ad132009-12-22 01:25:10 +00005808
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005809 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5810 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5811 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5812 return;
5813 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005814
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005815 // Otherwise, if this was a reference to an LLVM register class, create vregs
5816 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005817 if (const TargetRegisterClass *RC = PhysReg.second) {
5818 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005819 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005820 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005821
Evan Chengfb112882009-03-23 08:01:15 +00005822 // Create the appropriate number of virtual registers.
5823 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5824 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005825 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005826
Evan Chengfb112882009-03-23 08:01:15 +00005827 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5828 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005829 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005830
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005831 // This is a reference to a register class that doesn't directly correspond
5832 // to an LLVM register class. Allocate NumRegs consecutive, available,
5833 // registers from the class.
5834 std::vector<unsigned> RegClassRegs
5835 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5836 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005837
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005838 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5839 unsigned NumAllocated = 0;
5840 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5841 unsigned Reg = RegClassRegs[i];
5842 // See if this register is available.
5843 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5844 (isInReg && InputRegs.count(Reg))) { // Already used.
5845 // Make sure we find consecutive registers.
5846 NumAllocated = 0;
5847 continue;
5848 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005849
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005850 // Check to see if this register is allocatable (i.e. don't give out the
5851 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005852 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5853 if (!RC) { // Couldn't allocate this register.
5854 // Reset NumAllocated to make sure we return consecutive registers.
5855 NumAllocated = 0;
5856 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005857 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005858
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005859 // Okay, this register is good, we can use it.
5860 ++NumAllocated;
5861
5862 // If we allocated enough consecutive registers, succeed.
5863 if (NumAllocated == NumRegs) {
5864 unsigned RegStart = (i-NumAllocated)+1;
5865 unsigned RegEnd = i+1;
5866 // Mark all of the allocated registers used.
5867 for (unsigned i = RegStart; i != RegEnd; ++i)
5868 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005869
5870 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005871 OpInfo.ConstraintVT);
5872 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5873 return;
5874 }
5875 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005876
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005877 // Otherwise, we couldn't allocate enough registers for this.
5878}
5879
Evan Chengda43bcf2008-09-24 00:05:32 +00005880/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5881/// processed uses a memory 'm' constraint.
5882static bool
5883hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005884 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005885 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5886 InlineAsm::ConstraintInfo &CI = CInfos[i];
5887 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5888 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5889 if (CType == TargetLowering::C_Memory)
5890 return true;
5891 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005892
Chris Lattner6c147292009-04-30 00:48:50 +00005893 // Indirect operand accesses access memory.
5894 if (CI.isIndirect)
5895 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005896 }
5897
5898 return false;
5899}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005900
5901/// visitInlineAsm - Handle a call to an InlineAsm object.
5902///
Dan Gohman2048b852009-11-23 18:04:58 +00005903void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005904 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5905
5906 /// ConstraintOperands - Information about all of the constraints.
5907 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005908
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005909 std::set<unsigned> OutputRegs, InputRegs;
5910
5911 // Do a prepass over the constraints, canonicalizing them, and building up the
5912 // ConstraintOperands list.
5913 std::vector<InlineAsm::ConstraintInfo>
5914 ConstraintInfos = IA->ParseConstraints();
5915
Evan Chengda43bcf2008-09-24 00:05:32 +00005916 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005917
Chris Lattner6c147292009-04-30 00:48:50 +00005918 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005919
Chris Lattner6c147292009-04-30 00:48:50 +00005920 // We won't need to flush pending loads if this asm doesn't touch
5921 // memory and is nonvolatile.
5922 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005923 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005924 else
5925 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005926
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005927 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5928 unsigned ResNo = 0; // ResNo - The result number of the next output.
5929 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5930 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5931 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005932
Owen Anderson825b72b2009-08-11 20:47:22 +00005933 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934
5935 // Compute the value type for each operand.
5936 switch (OpInfo.Type) {
5937 case InlineAsm::isOutput:
5938 // Indirect outputs just consume an argument.
5939 if (OpInfo.isIndirect) {
5940 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5941 break;
5942 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005943
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005944 // The return value of the call is this value. As such, there is no
5945 // corresponding argument.
Owen Anderson1d0be152009-08-13 21:58:54 +00005946 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
5947 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005948 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5949 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5950 } else {
5951 assert(ResNo == 0 && "Asm only has one result!");
5952 OpVT = TLI.getValueType(CS.getType());
5953 }
5954 ++ResNo;
5955 break;
5956 case InlineAsm::isInput:
5957 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5958 break;
5959 case InlineAsm::isClobber:
5960 // Nothing to do.
5961 break;
5962 }
5963
5964 // If this is an input or an indirect output, process the call argument.
5965 // BasicBlocks are labels, currently appearing only in asm's.
5966 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005967 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005968 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5969
Chris Lattner81249c92008-10-17 17:05:25 +00005970 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005971 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005972 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005973 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005974 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005975
Owen Anderson1d0be152009-08-13 21:58:54 +00005976 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005977 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005978
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005979 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005980 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005981
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005982 // Second pass over the constraints: compute which constraint option to use
5983 // and assign registers to constraints that want a specific physreg.
5984 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5985 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005986
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005987 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005988 // matching input. If their types mismatch, e.g. one is an integer, the
5989 // other is floating point, or their sizes are different, flag it as an
5990 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005991 if (OpInfo.hasMatchingInput()) {
5992 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5993 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005994 if ((OpInfo.ConstraintVT.isInteger() !=
5995 Input.ConstraintVT.isInteger()) ||
5996 (OpInfo.ConstraintVT.getSizeInBits() !=
5997 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005998 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005999 " with a matching output constraint of incompatible"
6000 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00006001 }
6002 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006003 }
6004 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006005
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006006 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00006007 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006008
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006009 // If this is a memory input, and if the operand is not indirect, do what we
6010 // need to to provide an address for the memory input.
6011 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
6012 !OpInfo.isIndirect) {
6013 assert(OpInfo.Type == InlineAsm::isInput &&
6014 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006015
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006016 // Memory operands really want the address of the value. If we don't have
6017 // an indirect input, put it in the constpool if we can, otherwise spill
6018 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006019
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006020 // If the operand is a float, integer, or vector constant, spill to a
6021 // constant pool entry to get its address.
6022 Value *OpVal = OpInfo.CallOperandVal;
6023 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
6024 isa<ConstantVector>(OpVal)) {
6025 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
6026 TLI.getPointerTy());
6027 } else {
6028 // Otherwise, create a stack slot and emit a store to it before the
6029 // asm.
6030 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00006031 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006032 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
6033 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00006034 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006035 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00006036 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006037 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006038 OpInfo.CallOperand = StackSlot;
6039 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006041 // There is no longer a Value* corresponding to this operand.
6042 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00006043
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006044 // It is now an indirect operand.
6045 OpInfo.isIndirect = true;
6046 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006047
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006048 // If this constraint is for a specific register, allocate it before
6049 // anything else.
6050 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00006051 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006052 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006053
Bill Wendling651ad132009-12-22 01:25:10 +00006054 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006055
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006056 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00006057 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006058 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6059 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006060
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006061 // C_Register operands have already been allocated, Other/Memory don't need
6062 // to be.
6063 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00006064 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006065 }
6066
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006067 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
6068 std::vector<SDValue> AsmNodeOperands;
6069 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
6070 AsmNodeOperands.push_back(
Owen Anderson825b72b2009-08-11 20:47:22 +00006071 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(), MVT::Other));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006072
6073
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006074 // Loop over all of the inputs, copying the operand values into the
6075 // appropriate registers and processing the output regs.
6076 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006077
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006078 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
6079 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006081 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
6082 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
6083
6084 switch (OpInfo.Type) {
6085 case InlineAsm::isOutput: {
6086 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
6087 OpInfo.ConstraintType != TargetLowering::C_Register) {
6088 // Memory output, or 'other' output (e.g. 'X' constraint).
6089 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
6090
6091 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006092 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6093 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006094 TLI.getPointerTy()));
6095 AsmNodeOperands.push_back(OpInfo.CallOperand);
6096 break;
6097 }
6098
6099 // Otherwise, this is a register or register class output.
6100
6101 // Copy the output from the appropriate register. Find a register that
6102 // we can use.
6103 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006104 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006105 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006106 }
6107
6108 // If this is an indirect operand, store through the pointer after the
6109 // asm.
6110 if (OpInfo.isIndirect) {
6111 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
6112 OpInfo.CallOperandVal));
6113 } else {
6114 // This is the result value of the call.
Owen Anderson1d0be152009-08-13 21:58:54 +00006115 assert(CS.getType() != Type::getVoidTy(*DAG.getContext()) &&
6116 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006117 // Concatenate this output onto the outputs list.
6118 RetValRegs.append(OpInfo.AssignedRegs);
6119 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006120
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006121 // Add information to the INLINEASM node to know that this register is
6122 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00006123 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
6124 6 /* EARLYCLOBBER REGDEF */ :
6125 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00006126 false,
6127 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006128 DAG, SDNodeOrder,
6129 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006130 break;
6131 }
6132 case InlineAsm::isInput: {
6133 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006134
Chris Lattner6bdcda32008-10-17 16:47:46 +00006135 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006136 // If this is required to match an output register we have already set,
6137 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00006138 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006139
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006140 // Scan until we find the definition we already emitted of this operand.
6141 // When we find it, create a RegsForValue operand.
6142 unsigned CurOp = 2; // The first operand.
6143 for (; OperandNo; --OperandNo) {
6144 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00006145 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006146 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006147 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
6148 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
6149 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006150 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00006151 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006152 }
6153
Evan Cheng697cbbf2009-03-20 18:03:34 +00006154 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006155 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006156 if ((OpFlag & 7) == 2 /*REGDEF*/
6157 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
6158 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00006159 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006160 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00006161 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00006162 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006163 RegsForValue MatchedRegs;
6164 MatchedRegs.TLI = &TLI;
6165 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00006166 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00006167 MatchedRegs.RegVTs.push_back(RegVT);
6168 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00006169 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00006170 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006171 MatchedRegs.Regs.push_back
6172 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006173
6174 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00006175 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006176 SDNodeOrder, Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00006177 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
6178 true, OpInfo.getMatchedOperand(),
Bill Wendling651ad132009-12-22 01:25:10 +00006179 DAG, SDNodeOrder, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006180 break;
6181 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00006182 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
6183 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
6184 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006185 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00006186 // See InlineAsm.h isUseOperandTiedToDef.
6187 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00006188 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006189 TLI.getPointerTy()));
6190 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
6191 break;
6192 }
6193 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006194
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006195 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006196 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006197 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006198
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006199 std::vector<SDValue> Ops;
6200 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00006201 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006202 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006203 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00006204 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006205 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006206
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006207 // Add information to the INLINEASM node to know about this input.
6208 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006209 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006210 TLI.getPointerTy()));
6211 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
6212 break;
6213 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
6214 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
6215 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
6216 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006217
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006218 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00006219 unsigned ResOpType = 4/*MEM*/ | (1<<3);
6220 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006221 TLI.getPointerTy()));
6222 AsmNodeOperands.push_back(InOperandVal);
6223 break;
6224 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006225
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006226 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
6227 OpInfo.ConstraintType == TargetLowering::C_Register) &&
6228 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006229 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006230 "Don't know how to handle indirect register inputs yet!");
6231
6232 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00006233 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00006234 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00006235 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00006236 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006237
Dale Johannesen66978ee2009-01-31 02:22:37 +00006238 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006239 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006240
Evan Cheng697cbbf2009-03-20 18:03:34 +00006241 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Bill Wendling651ad132009-12-22 01:25:10 +00006242 DAG, SDNodeOrder,
6243 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006244 break;
6245 }
6246 case InlineAsm::isClobber: {
6247 // Add the clobbered value to the operand list, so that the register
6248 // allocator is aware that the physreg got clobbered.
6249 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00006250 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Bill Wendling651ad132009-12-22 01:25:10 +00006251 false, 0, DAG, SDNodeOrder,
6252 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006253 break;
6254 }
6255 }
6256 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006257
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006258 // Finish up input operands.
6259 AsmNodeOperands[0] = Chain;
6260 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006261
Dale Johannesen66978ee2009-01-31 02:22:37 +00006262 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006263 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006264 &AsmNodeOperands[0], AsmNodeOperands.size());
6265 Flag = Chain.getValue(1);
6266
6267 // If this asm returns a register value, copy the result from that register
6268 // and set it as the value of the call.
6269 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006270 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006271 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006272
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006273 // FIXME: Why don't we do this for inline asms with MRVs?
6274 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006275 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006276
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006277 // If any of the results of the inline asm is a vector, it may have the
6278 // wrong width/num elts. This can happen for register classes that can
6279 // contain multiple different value types. The preg or vreg allocated may
6280 // not have the same VT as was expected. Convert it to the right type
6281 // with bit_convert.
6282 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006283 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006284 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006285
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006286 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006287 ResultType.isInteger() && Val.getValueType().isInteger()) {
6288 // If a result value was tied to an input value, the computed result may
6289 // have a wider width than the expected result. Extract the relevant
6290 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006291 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006292 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006293
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006294 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006295 }
Dan Gohman95915732008-10-18 01:03:45 +00006296
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006297 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006298 // Don't need to use this as a chain in this case.
6299 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6300 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006301 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006302
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006303 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006304
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006305 // Process indirect outputs, first output all of the flagged copies out of
6306 // physregs.
6307 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6308 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
6309 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00006310 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006311 SDNodeOrder, Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006312 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00006313
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006314 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006315
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006316 // Emit the non-flagged stores from the physregs.
6317 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006318 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6319 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6320 StoresToEmit[i].first,
6321 getValue(StoresToEmit[i].second),
6322 StoresToEmit[i].second, 0);
6323 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006324 }
6325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006326 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006327 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006328 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006330 DAG.setRoot(Chain);
6331}
6332
Dan Gohman2048b852009-11-23 18:04:58 +00006333void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006334 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6335 MVT::Other, getRoot(),
6336 getValue(I.getOperand(1)),
6337 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006338}
6339
Dan Gohman2048b852009-11-23 18:04:58 +00006340void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006341 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6342 getRoot(), getValue(I.getOperand(0)),
6343 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006344 setValue(&I, V);
6345 DAG.setRoot(V.getValue(1));
6346}
6347
Dan Gohman2048b852009-11-23 18:04:58 +00006348void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006349 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6350 MVT::Other, getRoot(),
6351 getValue(I.getOperand(1)),
6352 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006353}
6354
Dan Gohman2048b852009-11-23 18:04:58 +00006355void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006356 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6357 MVT::Other, getRoot(),
6358 getValue(I.getOperand(1)),
6359 getValue(I.getOperand(2)),
6360 DAG.getSrcValue(I.getOperand(1)),
6361 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006362}
6363
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006364/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006365/// implementation, which just calls LowerCall.
6366/// FIXME: When all targets are
6367/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006368std::pair<SDValue, SDValue>
6369TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6370 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006371 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006372 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006373 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006374 SDValue Callee,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006375 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl,
6376 unsigned Order) {
Dan Gohman1937e2f2008-09-16 01:42:28 +00006377 assert((!isTailCall || PerformTailCallOpt) &&
6378 "isTailCall set when tail-call optimizations are disabled!");
6379
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006380 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006381 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006382 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006383 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006384 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6385 for (unsigned Value = 0, NumValues = ValueVTs.size();
6386 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006387 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006388 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006389 SDValue Op = SDValue(Args[i].Node.getNode(),
6390 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006391 ISD::ArgFlagsTy Flags;
6392 unsigned OriginalAlignment =
6393 getTargetData()->getABITypeAlignment(ArgTy);
6394
6395 if (Args[i].isZExt)
6396 Flags.setZExt();
6397 if (Args[i].isSExt)
6398 Flags.setSExt();
6399 if (Args[i].isInReg)
6400 Flags.setInReg();
6401 if (Args[i].isSRet)
6402 Flags.setSRet();
6403 if (Args[i].isByVal) {
6404 Flags.setByVal();
6405 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6406 const Type *ElementTy = Ty->getElementType();
6407 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006408 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006409 // For ByVal, alignment should come from FE. BE will guess if this
6410 // info is not there but there are cases it cannot get right.
6411 if (Args[i].Alignment)
6412 FrameAlign = Args[i].Alignment;
6413 Flags.setByValAlign(FrameAlign);
6414 Flags.setByValSize(FrameSize);
6415 }
6416 if (Args[i].isNest)
6417 Flags.setNest();
6418 Flags.setOrigAlign(OriginalAlignment);
6419
Owen Anderson23b9b192009-08-12 00:36:31 +00006420 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6421 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006422 SmallVector<SDValue, 4> Parts(NumParts);
6423 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6424
6425 if (Args[i].isSExt)
6426 ExtendKind = ISD::SIGN_EXTEND;
6427 else if (Args[i].isZExt)
6428 ExtendKind = ISD::ZERO_EXTEND;
6429
Bill Wendling3ea3c242009-12-22 02:10:19 +00006430 getCopyToParts(DAG, dl, Order, Op, &Parts[0], NumParts,
6431 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006432
Dan Gohman98ca4f22009-08-05 01:29:28 +00006433 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006434 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006435 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6436 if (NumParts > 1 && j == 0)
6437 MyFlags.Flags.setSplit();
6438 else if (j != 0)
6439 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006440
Dan Gohman98ca4f22009-08-05 01:29:28 +00006441 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006442 }
6443 }
6444 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006445
Dan Gohman98ca4f22009-08-05 01:29:28 +00006446 // Handle the incoming return values from the call.
6447 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006448 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006449 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006450 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006451 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006452 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6453 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006454 for (unsigned i = 0; i != NumRegs; ++i) {
6455 ISD::InputArg MyFlags;
6456 MyFlags.VT = RegisterVT;
6457 MyFlags.Used = isReturnValueUsed;
6458 if (RetSExt)
6459 MyFlags.Flags.setSExt();
6460 if (RetZExt)
6461 MyFlags.Flags.setZExt();
6462 if (isInreg)
6463 MyFlags.Flags.setInReg();
6464 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006465 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006466 }
6467
Dan Gohman98ca4f22009-08-05 01:29:28 +00006468 // Check if target-dependent constraints permit a tail call here.
6469 // Target-independent constraints should be checked by the caller.
6470 if (isTailCall &&
6471 !IsEligibleForTailCallOptimization(Callee, CallConv, isVarArg, Ins, DAG))
6472 isTailCall = false;
6473
6474 SmallVector<SDValue, 4> InVals;
6475 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6476 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006477
6478 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006479 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006480 "LowerCall didn't return a valid chain!");
6481 assert((!isTailCall || InVals.empty()) &&
6482 "LowerCall emitted a return value for a tail call!");
6483 assert((isTailCall || InVals.size() == Ins.size()) &&
6484 "LowerCall didn't emit the correct number of values!");
6485 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6486 assert(InVals[i].getNode() &&
6487 "LowerCall emitted a null value!");
6488 assert(Ins[i].VT == InVals[i].getValueType() &&
6489 "LowerCall emitted a value with the wrong type!");
6490 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006491
Bill Wendling3ea3c242009-12-22 02:10:19 +00006492 if (DisableScheduling)
6493 DAG.AssignOrdering(Chain.getNode(), Order);
6494
Dan Gohman98ca4f22009-08-05 01:29:28 +00006495 // For a tail call, the return value is merely live-out and there aren't
6496 // any nodes in the DAG representing it. Return a special value to
6497 // indicate that a tail call has been emitted and no more Instructions
6498 // should be processed in the current block.
6499 if (isTailCall) {
6500 DAG.setRoot(Chain);
6501 return std::make_pair(SDValue(), SDValue());
6502 }
6503
6504 // Collect the legal value parts into potentially illegal values
6505 // that correspond to the original function's return values.
6506 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6507 if (RetSExt)
6508 AssertOp = ISD::AssertSext;
6509 else if (RetZExt)
6510 AssertOp = ISD::AssertZext;
6511 SmallVector<SDValue, 4> ReturnValues;
6512 unsigned CurReg = 0;
6513 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006514 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006515 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6516 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006517
6518 SDValue ReturnValue =
Bill Wendling3ea3c242009-12-22 02:10:19 +00006519 getCopyFromParts(DAG, dl, Order, &InVals[CurReg], NumRegs,
6520 RegisterVT, VT, AssertOp);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006521 ReturnValues.push_back(ReturnValue);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006522 if (DisableScheduling)
6523 DAG.AssignOrdering(ReturnValue.getNode(), Order);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006524 CurReg += NumRegs;
6525 }
6526
6527 // For a function returning void, there is no return value. We can't create
6528 // such a node, so we just return a null return value in that case. In
6529 // that case, nothing will actualy look at the value.
6530 if (ReturnValues.empty())
6531 return std::make_pair(SDValue(), Chain);
6532
6533 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6534 DAG.getVTList(&RetTys[0], RetTys.size()),
6535 &ReturnValues[0], ReturnValues.size());
Bill Wendling3ea3c242009-12-22 02:10:19 +00006536 if (DisableScheduling)
6537 DAG.AssignOrdering(Res.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006538 return std::make_pair(Res, Chain);
6539}
6540
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006541void TargetLowering::LowerOperationWrapper(SDNode *N,
6542 SmallVectorImpl<SDValue> &Results,
6543 SelectionDAG &DAG) {
6544 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006545 if (Res.getNode())
6546 Results.push_back(Res);
6547}
6548
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006549SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006550 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006551 return SDValue();
6552}
6553
Dan Gohman2048b852009-11-23 18:04:58 +00006554void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006555 SDValue Op = getValue(V);
6556 assert((Op.getOpcode() != ISD::CopyFromReg ||
6557 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6558 "Copy from a reg to the same reg!");
6559 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6560
Owen Anderson23b9b192009-08-12 00:36:31 +00006561 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006562 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +00006563 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006564 PendingExports.push_back(Chain);
6565}
6566
6567#include "llvm/CodeGen/SelectionDAGISel.h"
6568
Dan Gohman8c2b5252009-10-30 01:27:03 +00006569void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006570 // If this is the entry block, emit arguments.
6571 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006572 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006573 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006574 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006575 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006576 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006577
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006578 // Check whether the function can return without sret-demotion.
6579 SmallVector<EVT, 4> OutVTs;
6580 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006581 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006582 OutVTs, OutsFlags, TLI);
6583 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6584
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006585 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00006586 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006587 if (!FLI.CanLowerReturn) {
6588 // Put in an sret pointer parameter before all the other parameters.
6589 SmallVector<EVT, 1> ValueVTs;
6590 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6591
6592 // NOTE: Assuming that a pointer will never break down to more than one VT
6593 // or one register.
6594 ISD::ArgFlagsTy Flags;
6595 Flags.setSRet();
6596 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6597 ISD::InputArg RetArg(Flags, RegisterVT, true);
6598 Ins.push_back(RetArg);
6599 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006600
Dan Gohman98ca4f22009-08-05 01:29:28 +00006601 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006602 unsigned Idx = 1;
6603 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6604 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006605 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006606 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6607 bool isArgValueUsed = !I->use_empty();
6608 for (unsigned Value = 0, NumValues = ValueVTs.size();
6609 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006610 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006611 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006612 ISD::ArgFlagsTy Flags;
6613 unsigned OriginalAlignment =
6614 TD->getABITypeAlignment(ArgTy);
6615
6616 if (F.paramHasAttr(Idx, Attribute::ZExt))
6617 Flags.setZExt();
6618 if (F.paramHasAttr(Idx, Attribute::SExt))
6619 Flags.setSExt();
6620 if (F.paramHasAttr(Idx, Attribute::InReg))
6621 Flags.setInReg();
6622 if (F.paramHasAttr(Idx, Attribute::StructRet))
6623 Flags.setSRet();
6624 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6625 Flags.setByVal();
6626 const PointerType *Ty = cast<PointerType>(I->getType());
6627 const Type *ElementTy = Ty->getElementType();
6628 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6629 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6630 // For ByVal, alignment should be passed from FE. BE will guess if
6631 // this info is not there but there are cases it cannot get right.
6632 if (F.getParamAlignment(Idx))
6633 FrameAlign = F.getParamAlignment(Idx);
6634 Flags.setByValAlign(FrameAlign);
6635 Flags.setByValSize(FrameSize);
6636 }
6637 if (F.paramHasAttr(Idx, Attribute::Nest))
6638 Flags.setNest();
6639 Flags.setOrigAlign(OriginalAlignment);
6640
Owen Anderson23b9b192009-08-12 00:36:31 +00006641 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6642 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006643 for (unsigned i = 0; i != NumRegs; ++i) {
6644 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6645 if (NumRegs > 1 && i == 0)
6646 MyFlags.Flags.setSplit();
6647 // if it isn't first piece, alignment must be 1
6648 else if (i > 0)
6649 MyFlags.Flags.setOrigAlign(1);
6650 Ins.push_back(MyFlags);
6651 }
6652 }
6653 }
6654
6655 // Call the target to set up the argument values.
6656 SmallVector<SDValue, 8> InVals;
6657 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6658 F.isVarArg(), Ins,
6659 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006660
6661 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006662 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006663 "LowerFormalArguments didn't return a valid chain!");
6664 assert(InVals.size() == Ins.size() &&
6665 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006666 DEBUG({
6667 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6668 assert(InVals[i].getNode() &&
6669 "LowerFormalArguments emitted a null value!");
6670 assert(Ins[i].VT == InVals[i].getValueType() &&
6671 "LowerFormalArguments emitted a value with the wrong type!");
6672 }
6673 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006674
Dan Gohman5e866062009-08-06 15:37:27 +00006675 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006676 DAG.setRoot(NewRoot);
6677
6678 // Set up the argument values.
6679 unsigned i = 0;
6680 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006681 if (!FLI.CanLowerReturn) {
6682 // Create a virtual register for the sret pointer, and put in a copy
6683 // from the sret argument into it.
6684 SmallVector<EVT, 1> ValueVTs;
6685 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6686 EVT VT = ValueVTs[0];
6687 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6688 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling3ea58b62009-12-22 21:35:02 +00006689 SDValue ArgValue = getCopyFromParts(DAG, dl, 0, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006690 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006691
Dan Gohman2048b852009-11-23 18:04:58 +00006692 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006693 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6694 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6695 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006696 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6697 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006698 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006699
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006700 // i indexes lowered arguments. Bump it past the hidden sret argument.
6701 // Idx indexes LLVM arguments. Don't touch it.
6702 ++i;
6703 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006704
Dan Gohman98ca4f22009-08-05 01:29:28 +00006705 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6706 ++I, ++Idx) {
6707 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006708 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006709 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006710 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006711 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006712 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006713 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6714 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006715
6716 if (!I->use_empty()) {
6717 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6718 if (F.paramHasAttr(Idx, Attribute::SExt))
6719 AssertOp = ISD::AssertSext;
6720 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6721 AssertOp = ISD::AssertZext;
6722
Bill Wendling3ea58b62009-12-22 21:35:02 +00006723 ArgValues.push_back(getCopyFromParts(DAG, dl, 0, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006724 NumParts, PartVT, VT,
6725 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006726 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006727
Dan Gohman98ca4f22009-08-05 01:29:28 +00006728 i += NumParts;
6729 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006730
Dan Gohman98ca4f22009-08-05 01:29:28 +00006731 if (!I->use_empty()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +00006732 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6733 SDB->getCurDebugLoc());
6734 SDB->setValue(I, Res);
6735
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006736 // If this argument is live outside of the entry block, insert a copy from
6737 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006738 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006739 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006740 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006741
Dan Gohman98ca4f22009-08-05 01:29:28 +00006742 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006743
6744 // Finally, if the target has anything special to do, allow it to do so.
6745 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006746 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006747}
6748
6749/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6750/// ensure constants are generated when needed. Remember the virtual registers
6751/// that need to be added to the Machine PHI nodes as input. We cannot just
6752/// directly add them, because expansion might result in multiple MBB's for one
6753/// BB. As such, the start of the BB might correspond to a different MBB than
6754/// the end.
6755///
6756void
6757SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6758 TerminatorInst *TI = LLVMBB->getTerminator();
6759
6760 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6761
6762 // Check successor nodes' PHI nodes that expect a constant to be available
6763 // from this block.
6764 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6765 BasicBlock *SuccBB = TI->getSuccessor(succ);
6766 if (!isa<PHINode>(SuccBB->begin())) continue;
6767 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006768
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006769 // If this terminator has multiple identical successors (common for
6770 // switches), only handle each succ once.
6771 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006772
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006773 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6774 PHINode *PN;
6775
6776 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6777 // nodes and Machine PHI nodes, but the incoming operands have not been
6778 // emitted yet.
6779 for (BasicBlock::iterator I = SuccBB->begin();
6780 (PN = dyn_cast<PHINode>(I)); ++I) {
6781 // Ignore dead phi's.
6782 if (PN->use_empty()) continue;
6783
6784 unsigned Reg;
6785 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6786
6787 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006788 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006789 if (RegOut == 0) {
6790 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006791 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006792 }
6793 Reg = RegOut;
6794 } else {
6795 Reg = FuncInfo->ValueMap[PHIOp];
6796 if (Reg == 0) {
6797 assert(isa<AllocaInst>(PHIOp) &&
6798 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6799 "Didn't codegen value into a register!??");
6800 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006801 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006802 }
6803 }
6804
6805 // Remember that this register needs to added to the machine PHI node as
6806 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006807 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006808 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6809 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006810 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006811 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006812 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006813 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006814 Reg += NumRegisters;
6815 }
6816 }
6817 }
Dan Gohman2048b852009-11-23 18:04:58 +00006818 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006819}
6820
Dan Gohman3df24e62008-09-03 23:12:08 +00006821/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6822/// supports legal types, and it emits MachineInstrs directly instead of
6823/// creating SelectionDAG nodes.
6824///
6825bool
6826SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6827 FastISel *F) {
6828 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006829
Dan Gohman3df24e62008-09-03 23:12:08 +00006830 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006831 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006832
6833 // Check successor nodes' PHI nodes that expect a constant to be available
6834 // from this block.
6835 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6836 BasicBlock *SuccBB = TI->getSuccessor(succ);
6837 if (!isa<PHINode>(SuccBB->begin())) continue;
6838 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006839
Dan Gohman3df24e62008-09-03 23:12:08 +00006840 // If this terminator has multiple identical successors (common for
6841 // switches), only handle each succ once.
6842 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006843
Dan Gohman3df24e62008-09-03 23:12:08 +00006844 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6845 PHINode *PN;
6846
6847 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6848 // nodes and Machine PHI nodes, but the incoming operands have not been
6849 // emitted yet.
6850 for (BasicBlock::iterator I = SuccBB->begin();
6851 (PN = dyn_cast<PHINode>(I)); ++I) {
6852 // Ignore dead phi's.
6853 if (PN->use_empty()) continue;
6854
6855 // Only handle legal types. Two interesting things to note here. First,
6856 // by bailing out early, we may leave behind some dead instructions,
6857 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6858 // own moves. Second, this check is necessary becuase FastISel doesn't
6859 // use CreateRegForValue to create registers, so it always creates
6860 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006861 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006862 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6863 // Promote MVT::i1.
6864 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006865 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006866 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006867 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006868 return false;
6869 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006870 }
6871
6872 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6873
6874 unsigned Reg = F->getRegForValue(PHIOp);
6875 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006876 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006877 return false;
6878 }
Dan Gohman2048b852009-11-23 18:04:58 +00006879 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006880 }
6881 }
6882
6883 return true;
6884}