blob: 36059a5e85ba91913f124e2e66cd6b7f6b7e2108 [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 Wendling187361b2010-01-23 10:26:57 +0000179 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 Wendling187361b2010-01-23 10:26:57 +0000212 DAG.AssignOrdering(Lo.getNode(), Order);
213 DAG.AssignOrdering(Hi.getNode(), Order);
214 DAG.AssignOrdering(Val.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000215
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000216 if (RoundParts < NumParts) {
217 // Assemble the trailing non-power-of-2 part.
218 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000219 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000220 Hi = getCopyFromParts(DAG, dl, Order,
221 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000222
223 // Combine the round and odd parts.
224 Lo = Val;
225 if (TLI.isBigEndian())
226 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000227 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000228 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
Bill Wendling187361b2010-01-23 10:26:57 +0000229 DAG.AssignOrdering(Hi.getNode(), Order);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000230 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000231 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000232 TLI.getPointerTy()));
Bill Wendling187361b2010-01-23 10:26:57 +0000233 DAG.AssignOrdering(Hi.getNode(), Order);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000234 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
Bill Wendling187361b2010-01-23 10:26:57 +0000235 DAG.AssignOrdering(Lo.getNode(), Order);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000236 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Bill Wendling187361b2010-01-23 10:26:57 +0000237 DAG.AssignOrdering(Val.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000238 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000239 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000240 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000241 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000242 unsigned NumIntermediates;
243 unsigned NumRegs =
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000244 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
Owen Anderson23b9b192009-08-12 00:36:31 +0000245 NumIntermediates, RegisterVT);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000246 assert(NumRegs == NumParts
247 && "Part count doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000248 NumParts = NumRegs; // Silence a compiler warning.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000249 assert(RegisterVT == PartVT
250 && "Part type doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000251 assert(RegisterVT == Parts[0].getValueType() &&
252 "Part type doesn't match part!");
253
254 // Assemble the parts into intermediate operands.
255 SmallVector<SDValue, 8> Ops(NumIntermediates);
256 if (NumIntermediates == NumParts) {
257 // If the register was not expanded, truncate or copy the value,
258 // as appropriate.
259 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000260 Ops[i] = getCopyFromParts(DAG, dl, Order, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000261 PartVT, IntermediateVT);
262 } else if (NumParts > 0) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000263 // If the intermediate type was expanded, build the intermediate
264 // operands from the parts.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000265 assert(NumParts % NumIntermediates == 0 &&
266 "Must expand into a divisible number of parts!");
267 unsigned Factor = NumParts / NumIntermediates;
268 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000269 Ops[i] = getCopyFromParts(DAG, dl, Order, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000270 PartVT, IntermediateVT);
271 }
272
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000273 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
274 // intermediate operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000275 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000276 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000277 ValueVT, &Ops[0], NumIntermediates);
Bill Wendling187361b2010-01-23 10:26:57 +0000278 DAG.AssignOrdering(Val.getNode(), Order);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000279 } else if (PartVT.isFloatingPoint()) {
280 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000282 "Unexpected split");
283 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000284 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
285 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000286 if (TLI.isBigEndian())
287 std::swap(Lo, Hi);
288 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000289
Bill Wendling187361b2010-01-23 10:26:57 +0000290 DAG.AssignOrdering(Hi.getNode(), Order);
291 DAG.AssignOrdering(Lo.getNode(), Order);
292 DAG.AssignOrdering(Val.getNode(), Order);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000293 } else {
294 // FP split into integer parts (soft fp)
295 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
296 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000297 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling3ea3c242009-12-22 02:10:19 +0000298 Val = getCopyFromParts(DAG, dl, Order, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000299 }
300 }
301
302 // There is now one part, held in Val. Correct it to match ValueVT.
303 PartVT = Val.getValueType();
304
305 if (PartVT == ValueVT)
306 return Val;
307
308 if (PartVT.isVector()) {
309 assert(ValueVT.isVector() && "Unknown vector conversion!");
Bill Wendling3ea3c242009-12-22 02:10:19 +0000310 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Bill Wendling187361b2010-01-23 10:26:57 +0000311 DAG.AssignOrdering(Res.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000312 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000313 }
314
315 if (ValueVT.isVector()) {
316 assert(ValueVT.getVectorElementType() == PartVT &&
317 ValueVT.getVectorNumElements() == 1 &&
318 "Only trivial scalar-to-vector conversions should get here!");
Bill Wendling3ea3c242009-12-22 02:10:19 +0000319 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Bill Wendling187361b2010-01-23 10:26:57 +0000320 DAG.AssignOrdering(Res.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000321 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000322 }
323
324 if (PartVT.isInteger() &&
325 ValueVT.isInteger()) {
326 if (ValueVT.bitsLT(PartVT)) {
327 // For a truncate, see if we have any information to
328 // indicate whether the truncated bits will always be
329 // zero or sign-extension.
330 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000331 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000332 DAG.getValueType(ValueVT));
Bill Wendling187361b2010-01-23 10:26:57 +0000333 DAG.AssignOrdering(Val.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000334 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Bill Wendling187361b2010-01-23 10:26:57 +0000335 DAG.AssignOrdering(Val.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000336 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000337 } else {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000338 Val = DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Bill Wendling187361b2010-01-23 10:26:57 +0000339 DAG.AssignOrdering(Val.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000340 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 }
342 }
343
344 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000345 if (ValueVT.bitsLT(Val.getValueType())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000346 // FP_ROUND's are always exact here.
Bill Wendling3ea3c242009-12-22 02:10:19 +0000347 Val = DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
348 DAG.getIntPtrConstant(1));
Bill Wendling187361b2010-01-23 10:26:57 +0000349 DAG.AssignOrdering(Val.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000350 return Val;
351 }
352
353 Val = DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Bill Wendling187361b2010-01-23 10:26:57 +0000354 DAG.AssignOrdering(Val.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000355 return Val;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000356 }
357
Bill Wendling3ea3c242009-12-22 02:10:19 +0000358 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
359 Val = DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Bill Wendling187361b2010-01-23 10:26:57 +0000360 DAG.AssignOrdering(Val.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000361 return Val;
362 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000363
Torok Edwinc23197a2009-07-14 16:55:14 +0000364 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000365 return SDValue();
366}
367
368/// getCopyToParts - Create a series of nodes that contain the specified value
369/// split into legal parts. If the parts contain more bits than Val, then, for
370/// integers, ExtendKind can be used to specify how to generate the extra bits.
Bill Wendling3ea3c242009-12-22 02:10:19 +0000371static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl, unsigned Order,
372 SDValue Val, SDValue *Parts, unsigned NumParts,
373 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000374 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000375 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000376 EVT PtrVT = TLI.getPointerTy();
377 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000378 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000379 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000380 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
381
382 if (!NumParts)
383 return;
384
385 if (!ValueVT.isVector()) {
386 if (PartVT == ValueVT) {
387 assert(NumParts == 1 && "No-op copy with multiple parts!");
388 Parts[0] = Val;
389 return;
390 }
391
392 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
393 // If the parts cover more bits than the value has, promote the value.
394 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
395 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000396 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000397 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000398 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000399 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000400 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000401 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000402 }
403 } else if (PartBits == ValueVT.getSizeInBits()) {
404 // Different types of the same size.
405 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000406 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000407 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
408 // If the parts cover less bits than value has, truncate the value.
409 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000410 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000411 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000412 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000413 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000414 }
415 }
416
Bill Wendling187361b2010-01-23 10:26:57 +0000417 DAG.AssignOrdering(Val.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000418
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000419 // The value may have changed - recompute ValueVT.
420 ValueVT = Val.getValueType();
421 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
422 "Failed to tile the value with PartVT!");
423
424 if (NumParts == 1) {
425 assert(PartVT == ValueVT && "Type conversion failed!");
426 Parts[0] = Val;
427 return;
428 }
429
430 // Expand the value into multiple parts.
431 if (NumParts & (NumParts - 1)) {
432 // The number of parts is not a power of 2. Split off and copy the tail.
433 assert(PartVT.isInteger() && ValueVT.isInteger() &&
434 "Do not know what to expand to!");
435 unsigned RoundParts = 1 << Log2_32(NumParts);
436 unsigned RoundBits = RoundParts * PartBits;
437 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000438 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000439 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000440 TLI.getPointerTy()));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000441 getCopyToParts(DAG, dl, Order, OddVal, Parts + RoundParts,
442 OddParts, PartVT);
443
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000444 if (TLI.isBigEndian())
445 // The odd parts were reversed by getCopyToParts - unreverse them.
446 std::reverse(Parts + RoundParts, Parts + NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000447
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000448 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000449 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000450 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000451
Bill Wendling187361b2010-01-23 10:26:57 +0000452 DAG.AssignOrdering(OddVal.getNode(), Order);
453 DAG.AssignOrdering(Val.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000454 }
455
456 // The number of parts is a power of 2. Repeatedly bisect the value using
457 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000458 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Chris Lattnerf031e8a2010-01-01 03:32:16 +0000459 EVT::getIntegerVT(*DAG.getContext(),
460 ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000461 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000462
Bill Wendling187361b2010-01-23 10:26:57 +0000463 DAG.AssignOrdering(Parts[0].getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000464
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000465 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
466 for (unsigned i = 0; i < NumParts; i += StepSize) {
467 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000468 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000469 SDValue &Part0 = Parts[i];
470 SDValue &Part1 = Parts[i+StepSize/2];
471
Scott Michelfdc40a02009-02-17 22:15:04 +0000472 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000473 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000474 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000475 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000476 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000477 DAG.getConstant(0, PtrVT));
478
Bill Wendling187361b2010-01-23 10:26:57 +0000479 DAG.AssignOrdering(Part0.getNode(), Order);
480 DAG.AssignOrdering(Part1.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000481
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000482 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000483 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000484 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000485 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000486 PartVT, Part1);
Bill Wendling187361b2010-01-23 10:26:57 +0000487 DAG.AssignOrdering(Part0.getNode(), Order);
488 DAG.AssignOrdering(Part1.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000489 }
490 }
491 }
492
493 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000494 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000495
496 return;
497 }
498
499 // Vector ValueVT.
500 if (NumParts == 1) {
501 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000502 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000503 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000504 } else {
505 assert(ValueVT.getVectorElementType() == PartVT &&
506 ValueVT.getVectorNumElements() == 1 &&
507 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000508 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000509 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000510 DAG.getConstant(0, PtrVT));
511 }
512 }
513
Bill Wendling187361b2010-01-23 10:26:57 +0000514 DAG.AssignOrdering(Val.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000515 Parts[0] = Val;
516 return;
517 }
518
519 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000520 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000521 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000522 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
523 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000524 unsigned NumElements = ValueVT.getVectorNumElements();
525
526 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
527 NumParts = NumRegs; // Silence a compiler warning.
528 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
529
530 // Split the vector into intermediate operands.
531 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000532 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000533 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000534 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000535 IntermediateVT, Val,
536 DAG.getConstant(i * (NumElements / NumIntermediates),
537 PtrVT));
538 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000539 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000540 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000541 DAG.getConstant(i, PtrVT));
542
Bill Wendling187361b2010-01-23 10:26:57 +0000543 DAG.AssignOrdering(Ops[i].getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000544 }
545
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000546 // Split the intermediate operands into legal parts.
547 if (NumParts == NumIntermediates) {
548 // If the register was not expanded, promote or copy the value,
549 // as appropriate.
550 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000551 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000552 } else if (NumParts > 0) {
553 // If the intermediate type was expanded, split each the value into
554 // legal parts.
555 assert(NumParts % NumIntermediates == 0 &&
556 "Must expand into a divisible number of parts!");
557 unsigned Factor = NumParts / NumIntermediates;
558 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling3ea3c242009-12-22 02:10:19 +0000559 getCopyToParts(DAG, dl, Order, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000560 }
561}
562
563
Dan Gohman2048b852009-11-23 18:04:58 +0000564void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000565 AA = &aa;
566 GFI = gfi;
567 TD = DAG.getTarget().getTargetData();
568}
569
570/// clear - Clear out the curret SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000571/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000572/// for a new block. This doesn't clear out information about
573/// additional blocks that are needed to complete switch lowering
574/// or PHI node updating; that information is cleared out as it is
575/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000576void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000577 NodeMap.clear();
578 PendingLoads.clear();
579 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000580 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000581 DAG.clear();
Bill Wendling8fcf1702009-02-06 21:36:23 +0000582 CurDebugLoc = DebugLoc::getUnknownLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000583 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000584}
585
586/// getRoot - Return the current virtual root of the Selection DAG,
587/// flushing any PendingLoad items. This must be done before emitting
588/// a store or any other node that may need to be ordered after any
589/// prior load instructions.
590///
Dan Gohman2048b852009-11-23 18:04:58 +0000591SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000592 if (PendingLoads.empty())
593 return DAG.getRoot();
594
595 if (PendingLoads.size() == 1) {
596 SDValue Root = PendingLoads[0];
597 DAG.setRoot(Root);
598 PendingLoads.clear();
599 return Root;
600 }
601
602 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000603 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000604 &PendingLoads[0], PendingLoads.size());
605 PendingLoads.clear();
606 DAG.setRoot(Root);
607 return Root;
608}
609
610/// getControlRoot - Similar to getRoot, but instead of flushing all the
611/// PendingLoad items, flush all the PendingExports items. It is necessary
612/// to do this before emitting a terminator instruction.
613///
Dan Gohman2048b852009-11-23 18:04:58 +0000614SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000615 SDValue Root = DAG.getRoot();
616
617 if (PendingExports.empty())
618 return Root;
619
620 // Turn all of the CopyToReg chains into one factored node.
621 if (Root.getOpcode() != ISD::EntryToken) {
622 unsigned i = 0, e = PendingExports.size();
623 for (; i != e; ++i) {
624 assert(PendingExports[i].getNode()->getNumOperands() > 1);
625 if (PendingExports[i].getNode()->getOperand(0) == Root)
626 break; // Don't add the root if we already indirectly depend on it.
627 }
628
629 if (i == e)
630 PendingExports.push_back(Root);
631 }
632
Owen Anderson825b72b2009-08-11 20:47:22 +0000633 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000634 &PendingExports[0],
635 PendingExports.size());
636 PendingExports.clear();
637 DAG.setRoot(Root);
638 return Root;
639}
640
Dan Gohman2048b852009-11-23 18:04:58 +0000641void SelectionDAGBuilder::visit(Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000642 visit(I.getOpcode(), I);
643}
644
Dan Gohman2048b852009-11-23 18:04:58 +0000645void SelectionDAGBuilder::visit(unsigned Opcode, User &I) {
Bill Wendlingb4e6a5d2009-12-18 23:32:53 +0000646 // We're processing a new instruction.
647 ++SDNodeOrder;
648
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000649 // Note: this doesn't use InstVisitor, because it has to work with
650 // ConstantExpr's in addition to instructions.
651 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000652 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000653 // Build the switch statement using the Instruction.def file.
654#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling3b7a41c2009-12-21 19:59:38 +0000655 case Instruction::OPCODE: return visit##OPCODE((CLASS&)I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000656#include "llvm/Instruction.def"
657 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000658}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000659
Dan Gohman2048b852009-11-23 18:04:58 +0000660SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000661 SDValue &N = NodeMap[V];
662 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000663
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000664 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(V))) {
Owen Andersone50ed302009-08-10 22:56:29 +0000665 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000666
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000667 if (ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000668 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000669
670 if (GlobalValue *GV = dyn_cast<GlobalValue>(C))
671 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000672
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000673 if (isa<ConstantPointerNull>(C))
674 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000675
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000676 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000677 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000678
Nate Begeman9008ca62009-04-27 18:41:29 +0000679 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000680 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000681
682 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
683 visit(CE->getOpcode(), *CE);
684 SDValue N1 = NodeMap[V];
685 assert(N1.getNode() && "visit didn't populate the ValueMap!");
686 return N1;
687 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000688
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000689 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
690 SmallVector<SDValue, 4> Constants;
691 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
692 OI != OE; ++OI) {
693 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000694 // If the operand is an empty aggregate, there are no values.
695 if (!Val) continue;
696 // Add each leaf value from the operand to the Constants list
697 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000698 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
699 Constants.push_back(SDValue(Val, i));
700 }
Bill Wendling87710f02009-12-21 23:47:40 +0000701
702 SDValue Res = DAG.getMergeValues(&Constants[0], Constants.size(),
703 getCurDebugLoc());
Bill Wendling187361b2010-01-23 10:26:57 +0000704 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +0000705 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000706 }
707
708 if (isa<StructType>(C->getType()) || isa<ArrayType>(C->getType())) {
709 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
710 "Unknown struct or array constant!");
711
Owen Andersone50ed302009-08-10 22:56:29 +0000712 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000713 ComputeValueVTs(TLI, C->getType(), ValueVTs);
714 unsigned NumElts = ValueVTs.size();
715 if (NumElts == 0)
716 return SDValue(); // empty struct
717 SmallVector<SDValue, 4> Constants(NumElts);
718 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000719 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000720 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000721 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000722 else if (EltVT.isFloatingPoint())
723 Constants[i] = DAG.getConstantFP(0, EltVT);
724 else
725 Constants[i] = DAG.getConstant(0, EltVT);
726 }
Bill Wendling87710f02009-12-21 23:47:40 +0000727
728 SDValue Res = DAG.getMergeValues(&Constants[0], NumElts,
729 getCurDebugLoc());
Bill Wendling187361b2010-01-23 10:26:57 +0000730 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +0000731 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000732 }
733
Dan Gohman8c2b5252009-10-30 01:27:03 +0000734 if (BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000735 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000736
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000737 const VectorType *VecTy = cast<VectorType>(V->getType());
738 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000739
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000740 // Now that we know the number and type of the elements, get that number of
741 // elements into the Ops array based on what kind of constant it is.
742 SmallVector<SDValue, 16> Ops;
743 if (ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
744 for (unsigned i = 0; i != NumElements; ++i)
745 Ops.push_back(getValue(CP->getOperand(i)));
746 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000747 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000748 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000749
750 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000751 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000752 Op = DAG.getConstantFP(0, EltVT);
753 else
754 Op = DAG.getConstant(0, EltVT);
755 Ops.assign(NumElements, Op);
756 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000757
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000758 // Create a BUILD_VECTOR node.
Bill Wendling87710f02009-12-21 23:47:40 +0000759 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
760 VT, &Ops[0], Ops.size());
Bill Wendling187361b2010-01-23 10:26:57 +0000761 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +0000762 return NodeMap[V] = Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000763 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000764
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000765 // If this is a static alloca, generate it as the frameindex instead of
766 // computation.
767 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
768 DenseMap<const AllocaInst*, int>::iterator SI =
769 FuncInfo.StaticAllocaMap.find(AI);
770 if (SI != FuncInfo.StaticAllocaMap.end())
771 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
772 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000773
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000774 unsigned InReg = FuncInfo.ValueMap[V];
775 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000776
Owen Anderson23b9b192009-08-12 00:36:31 +0000777 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000778 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +0000779 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(),
780 SDNodeOrder, Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000781}
782
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000783/// Get the EVTs and ArgFlags collections that represent the legalized return
784/// type of the given function. This does not require a DAG or a return value,
785/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000786static void getReturnInfo(const Type* ReturnType,
787 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000788 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000789 TargetLowering &TLI,
790 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000791 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000792 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000793 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000794 if (NumValues == 0) return;
795 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000796
797 for (unsigned j = 0, f = NumValues; j != f; ++j) {
798 EVT VT = ValueVTs[j];
799 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000800
801 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000802 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000803 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000804 ExtendKind = ISD::ZERO_EXTEND;
805
806 // FIXME: C calling convention requires the return type to be promoted to
807 // at least 32-bit. But this is not necessary for non-C calling
808 // conventions. The frontend should mark functions whose return values
809 // require promoting with signext or zeroext attributes.
810 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000811 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000812 if (VT.bitsLT(MinVT))
813 VT = MinVT;
814 }
815
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000816 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
817 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000818 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
819 PartVT.getTypeForEVT(ReturnType->getContext()));
820
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000821 // 'inreg' on function refers to return value
822 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000823 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000824 Flags.setInReg();
825
826 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000827 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000828 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000829 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000830 Flags.setZExt();
831
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000832 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000833 OutVTs.push_back(PartVT);
834 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000835 if (Offsets)
836 {
837 Offsets->push_back(Offset);
838 Offset += PartSize;
839 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000840 }
841 }
842}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000843
Dan Gohman2048b852009-11-23 18:04:58 +0000844void SelectionDAGBuilder::visitRet(ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000845 SDValue Chain = getControlRoot();
846 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000847 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000848
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000849 if (!FLI.CanLowerReturn) {
850 unsigned DemoteReg = FLI.DemoteRegister;
851 const Function *F = I.getParent()->getParent();
852
853 // Emit a store of the return value through the virtual register.
854 // Leave Outs empty so that LowerReturn won't try to load return
855 // registers the usual way.
856 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000857 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000858 PtrValueVTs);
859
860 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
861 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000862
Owen Andersone50ed302009-08-10 22:56:29 +0000863 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000864 SmallVector<uint64_t, 4> Offsets;
865 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000866 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000867
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000868 SmallVector<SDValue, 4> Chains(NumValues);
869 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000870 for (unsigned i = 0; i != NumValues; ++i) {
871 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
872 DAG.getConstant(Offsets[i], PtrVT));
873 Chains[i] =
874 DAG.getStore(Chain, getCurDebugLoc(),
875 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
876 Add, NULL, Offsets[i], false, 0);
877
Bill Wendling187361b2010-01-23 10:26:57 +0000878 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
879 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +0000880 }
881
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000882 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
883 MVT::Other, &Chains[0], NumValues);
Bill Wendling87710f02009-12-21 23:47:40 +0000884
Bill Wendling187361b2010-01-23 10:26:57 +0000885 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +0000886 } else {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000887 for (unsigned i = 0, e = I.getNumOperands(); i != e; ++i) {
888 SmallVector<EVT, 4> ValueVTs;
889 ComputeValueVTs(TLI, I.getOperand(i)->getType(), ValueVTs);
890 unsigned NumValues = ValueVTs.size();
891 if (NumValues == 0) continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000892
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000893 SDValue RetOp = getValue(I.getOperand(i));
894 for (unsigned j = 0, f = NumValues; j != f; ++j) {
895 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000896
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000897 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000898
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000899 const Function *F = I.getParent()->getParent();
900 if (F->paramHasAttr(0, Attribute::SExt))
901 ExtendKind = ISD::SIGN_EXTEND;
902 else if (F->paramHasAttr(0, Attribute::ZExt))
903 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000904
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000905 // FIXME: C calling convention requires the return type to be promoted
906 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000907 // conventions. The frontend should mark functions whose return values
908 // require promoting with signext or zeroext attributes.
909 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
910 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
911 if (VT.bitsLT(MinVT))
912 VT = MinVT;
913 }
914
915 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
916 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
917 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000918 getCopyToParts(DAG, getCurDebugLoc(), SDNodeOrder,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000919 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
920 &Parts[0], NumParts, PartVT, ExtendKind);
921
922 // 'inreg' on function refers to return value
923 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
924 if (F->paramHasAttr(0, Attribute::InReg))
925 Flags.setInReg();
926
927 // Propagate extension type if any
928 if (F->paramHasAttr(0, Attribute::SExt))
929 Flags.setSExt();
930 else if (F->paramHasAttr(0, Attribute::ZExt))
931 Flags.setZExt();
932
933 for (unsigned i = 0; i < NumParts; ++i)
934 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000935 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000936 }
937 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000938
939 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000940 CallingConv::ID CallConv =
941 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000942 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
943 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000944
945 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000946 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000947 "LowerReturn didn't return a valid chain!");
948
949 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000950 DAG.setRoot(Chain);
Bill Wendling187361b2010-01-23 10:26:57 +0000951 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000952}
953
Dan Gohmanad62f532009-04-23 23:13:24 +0000954/// CopyToExportRegsIfNeeded - If the given value has virtual registers
955/// created for it, emit nodes to copy the value into the virtual
956/// registers.
Dan Gohman2048b852009-11-23 18:04:58 +0000957void SelectionDAGBuilder::CopyToExportRegsIfNeeded(Value *V) {
Dan Gohmanad62f532009-04-23 23:13:24 +0000958 if (!V->use_empty()) {
959 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
960 if (VMI != FuncInfo.ValueMap.end())
961 CopyValueToVirtualRegister(V, VMI->second);
962 }
963}
964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000965/// ExportFromCurrentBlock - If this condition isn't known to be exported from
966/// the current basic block, add it to ValueMap now so that we'll get a
967/// CopyTo/FromReg.
Dan Gohman2048b852009-11-23 18:04:58 +0000968void SelectionDAGBuilder::ExportFromCurrentBlock(Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000969 // No need to export constants.
970 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000971
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000972 // Already exported?
973 if (FuncInfo.isExportedInst(V)) return;
974
975 unsigned Reg = FuncInfo.InitializeRegForValue(V);
976 CopyValueToVirtualRegister(V, Reg);
977}
978
Dan Gohman2048b852009-11-23 18:04:58 +0000979bool SelectionDAGBuilder::isExportableFromCurrentBlock(Value *V,
980 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000981 // The operands of the setcc have to be in this block. We don't know
982 // how to export them from some other block.
983 if (Instruction *VI = dyn_cast<Instruction>(V)) {
984 // Can export from current BB.
985 if (VI->getParent() == FromBB)
986 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000987
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000988 // Is already exported, noop.
989 return FuncInfo.isExportedInst(V);
990 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000992 // If this is an argument, we can export it if the BB is the entry block or
993 // if it is already exported.
994 if (isa<Argument>(V)) {
995 if (FromBB == &FromBB->getParent()->getEntryBlock())
996 return true;
997
998 // Otherwise, can only export this if it is already exported.
999 return FuncInfo.isExportedInst(V);
1000 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001001
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001002 // Otherwise, constants can always be exported.
1003 return true;
1004}
1005
1006static bool InBlock(const Value *V, const BasicBlock *BB) {
1007 if (const Instruction *I = dyn_cast<Instruction>(V))
1008 return I->getParent() == BB;
1009 return true;
1010}
1011
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001012/// getFCmpCondCode - Return the ISD condition code corresponding to
1013/// the given LLVM IR floating-point condition code. This includes
1014/// consideration of global floating-point math flags.
1015///
1016static ISD::CondCode getFCmpCondCode(FCmpInst::Predicate Pred) {
1017 ISD::CondCode FPC, FOC;
1018 switch (Pred) {
1019 case FCmpInst::FCMP_FALSE: FOC = FPC = ISD::SETFALSE; break;
1020 case FCmpInst::FCMP_OEQ: FOC = ISD::SETEQ; FPC = ISD::SETOEQ; break;
1021 case FCmpInst::FCMP_OGT: FOC = ISD::SETGT; FPC = ISD::SETOGT; break;
1022 case FCmpInst::FCMP_OGE: FOC = ISD::SETGE; FPC = ISD::SETOGE; break;
1023 case FCmpInst::FCMP_OLT: FOC = ISD::SETLT; FPC = ISD::SETOLT; break;
1024 case FCmpInst::FCMP_OLE: FOC = ISD::SETLE; FPC = ISD::SETOLE; break;
1025 case FCmpInst::FCMP_ONE: FOC = ISD::SETNE; FPC = ISD::SETONE; break;
1026 case FCmpInst::FCMP_ORD: FOC = FPC = ISD::SETO; break;
1027 case FCmpInst::FCMP_UNO: FOC = FPC = ISD::SETUO; break;
1028 case FCmpInst::FCMP_UEQ: FOC = ISD::SETEQ; FPC = ISD::SETUEQ; break;
1029 case FCmpInst::FCMP_UGT: FOC = ISD::SETGT; FPC = ISD::SETUGT; break;
1030 case FCmpInst::FCMP_UGE: FOC = ISD::SETGE; FPC = ISD::SETUGE; break;
1031 case FCmpInst::FCMP_ULT: FOC = ISD::SETLT; FPC = ISD::SETULT; break;
1032 case FCmpInst::FCMP_ULE: FOC = ISD::SETLE; FPC = ISD::SETULE; break;
1033 case FCmpInst::FCMP_UNE: FOC = ISD::SETNE; FPC = ISD::SETUNE; break;
1034 case FCmpInst::FCMP_TRUE: FOC = FPC = ISD::SETTRUE; break;
1035 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001036 llvm_unreachable("Invalid FCmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001037 FOC = FPC = ISD::SETFALSE;
1038 break;
1039 }
1040 if (FiniteOnlyFPMath())
1041 return FOC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001042 else
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001043 return FPC;
1044}
1045
1046/// getICmpCondCode - Return the ISD condition code corresponding to
1047/// the given LLVM IR integer condition code.
1048///
1049static ISD::CondCode getICmpCondCode(ICmpInst::Predicate Pred) {
1050 switch (Pred) {
1051 case ICmpInst::ICMP_EQ: return ISD::SETEQ;
1052 case ICmpInst::ICMP_NE: return ISD::SETNE;
1053 case ICmpInst::ICMP_SLE: return ISD::SETLE;
1054 case ICmpInst::ICMP_ULE: return ISD::SETULE;
1055 case ICmpInst::ICMP_SGE: return ISD::SETGE;
1056 case ICmpInst::ICMP_UGE: return ISD::SETUGE;
1057 case ICmpInst::ICMP_SLT: return ISD::SETLT;
1058 case ICmpInst::ICMP_ULT: return ISD::SETULT;
1059 case ICmpInst::ICMP_SGT: return ISD::SETGT;
1060 case ICmpInst::ICMP_UGT: return ISD::SETUGT;
1061 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00001062 llvm_unreachable("Invalid ICmp predicate opcode!");
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001063 return ISD::SETNE;
1064 }
1065}
1066
Dan Gohmanc2277342008-10-17 21:16:08 +00001067/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
1068/// This function emits a branch and is used at the leaves of an OR or an
1069/// AND operator tree.
1070///
1071void
Dan Gohman2048b852009-11-23 18:04:58 +00001072SelectionDAGBuilder::EmitBranchForMergedCondition(Value *Cond,
1073 MachineBasicBlock *TBB,
1074 MachineBasicBlock *FBB,
1075 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001076 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001077
Dan Gohmanc2277342008-10-17 21:16:08 +00001078 // If the leaf of the tree is a comparison, merge the condition into
1079 // the caseblock.
1080 if (CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
1081 // The operands of the cmp have to be in this block. We don't know
1082 // how to export them from some other block. If this is the first block
1083 // of the sequence, no exporting is needed.
1084 if (CurBB == CurMBB ||
1085 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
1086 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001087 ISD::CondCode Condition;
1088 if (ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001089 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001090 } else if (FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00001091 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001092 } else {
1093 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001094 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001095 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001096
1097 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001098 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1099 SwitchCases.push_back(CB);
1100 return;
1101 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001102 }
1103
1104 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001105 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001106 NULL, TBB, FBB, CurBB);
1107 SwitchCases.push_back(CB);
1108}
1109
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001110/// FindMergedConditions - If Cond is an expression like
Dan Gohman2048b852009-11-23 18:04:58 +00001111void SelectionDAGBuilder::FindMergedConditions(Value *Cond,
1112 MachineBasicBlock *TBB,
1113 MachineBasicBlock *FBB,
1114 MachineBasicBlock *CurBB,
1115 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001116 // If this node is not part of the or/and tree, emit it as a branch.
1117 Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001118 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001119 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1120 BOp->getParent() != CurBB->getBasicBlock() ||
1121 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1122 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1123 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001124 return;
1125 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001126
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001127 // Create TmpBB after CurBB.
1128 MachineFunction::iterator BBI = CurBB;
1129 MachineFunction &MF = DAG.getMachineFunction();
1130 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1131 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001132
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001133 if (Opc == Instruction::Or) {
1134 // Codegen X | Y as:
1135 // jmp_if_X TBB
1136 // jmp TmpBB
1137 // TmpBB:
1138 // jmp_if_Y TBB
1139 // jmp FBB
1140 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001141
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001142 // Emit the LHS condition.
1143 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001144
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001145 // Emit the RHS condition into TmpBB.
1146 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1147 } else {
1148 assert(Opc == Instruction::And && "Unknown merge op!");
1149 // Codegen X & Y as:
1150 // jmp_if_X TmpBB
1151 // jmp FBB
1152 // TmpBB:
1153 // jmp_if_Y TBB
1154 // jmp FBB
1155 //
1156 // This requires creation of TmpBB after CurBB.
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), TmpBB, FBB, 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 }
1164}
1165
1166/// If the set of cases should be emitted as a series of branches, return true.
1167/// If we should emit this as a bunch of and/or'd together conditions, return
1168/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001169bool
Dan Gohman2048b852009-11-23 18:04:58 +00001170SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001171 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001172
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001173 // If this is two comparisons of the same values or'd or and'd together, they
1174 // will get folded into a single comparison, so don't emit two blocks.
1175 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1176 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1177 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1178 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1179 return false;
1180 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001181
Chris Lattner133ce872010-01-02 00:00:03 +00001182 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1183 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1184 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1185 Cases[0].CC == Cases[1].CC &&
1186 isa<Constant>(Cases[0].CmpRHS) &&
1187 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1188 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1189 return false;
1190 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1191 return false;
1192 }
1193
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001194 return true;
1195}
1196
Dan Gohman2048b852009-11-23 18:04:58 +00001197void SelectionDAGBuilder::visitBr(BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001198 // Update machine-CFG edges.
1199 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1200
1201 // Figure out which block is immediately after the current one.
1202 MachineBasicBlock *NextBlock = 0;
1203 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001204 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205 NextBlock = BBI;
1206
1207 if (I.isUnconditional()) {
1208 // Update machine-CFG edges.
1209 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001210
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001211 // If this is not a fall-through branch, emit the branch.
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001212 if (Succ0MBB != NextBlock) {
1213 SDValue V = DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001214 MVT::Other, getControlRoot(),
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001215 DAG.getBasicBlock(Succ0MBB));
1216 DAG.setRoot(V);
Bill Wendling187361b2010-01-23 10:26:57 +00001217 DAG.AssignOrdering(V.getNode(), SDNodeOrder);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001218 }
1219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001220 return;
1221 }
1222
1223 // If this condition is one of the special cases we handle, do special stuff
1224 // now.
1225 Value *CondVal = I.getCondition();
1226 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1227
1228 // If this is a series of conditions that are or'd or and'd together, emit
1229 // this as a sequence of branches instead of setcc's with and/or operations.
1230 // For example, instead of something like:
1231 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001232 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001233 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001234 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001235 // or C, F
1236 // jnz foo
1237 // Emit:
1238 // cmp A, B
1239 // je foo
1240 // cmp D, E
1241 // jle foo
1242 //
1243 if (BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001244 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001245 (BOp->getOpcode() == Instruction::And ||
1246 BOp->getOpcode() == Instruction::Or)) {
1247 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1248 // If the compares in later blocks need to use values not currently
1249 // exported from this block, export them now. This block should always
1250 // be the first entry.
1251 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001252
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001253 // Allow some cases to be rejected.
1254 if (ShouldEmitAsBranches(SwitchCases)) {
1255 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1256 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1257 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1258 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001259
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001260 // Emit the branch for this block.
1261 visitSwitchCase(SwitchCases[0]);
1262 SwitchCases.erase(SwitchCases.begin());
1263 return;
1264 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001266 // Okay, we decided not to do this, remove any inserted MBB's and clear
1267 // SwitchCases.
1268 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001269 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001270
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001271 SwitchCases.clear();
1272 }
1273 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001274
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001276 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001277 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001278
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001279 // Use visitSwitchCase to actually insert the fast branch sequence for this
1280 // cond branch.
1281 visitSwitchCase(CB);
1282}
1283
1284/// visitSwitchCase - Emits the necessary code to represent a single node in
1285/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001286void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001287 SDValue Cond;
1288 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001289 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001290
1291 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001292 if (CB.CmpMHS == NULL) {
1293 // Fold "(X == true)" to X and "(X == false)" to !X to
1294 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001295 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001296 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001297 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001298 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001299 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001300 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001301 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001302 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001303 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001304 } else {
1305 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1306
Anton Korobeynikov23218582008-12-23 22:25:27 +00001307 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1308 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001309
1310 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001311 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001312
1313 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001314 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001315 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001316 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001317 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001318 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001319 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320 DAG.getConstant(High-Low, VT), ISD::SETULE);
1321 }
1322 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001323
Bill Wendling187361b2010-01-23 10:26:57 +00001324 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001326 // Update successor info
1327 CurMBB->addSuccessor(CB.TrueBB);
1328 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001329
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001330 // Set NextBlock to be the MBB immediately after the current one, if any.
1331 // This is used to avoid emitting unnecessary branches to the next block.
1332 MachineBasicBlock *NextBlock = 0;
1333 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001334 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001335 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001336
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001337 // If the lhs block is the next block, invert the condition so that we can
1338 // fall through to the lhs instead of the rhs block.
1339 if (CB.TrueBB == NextBlock) {
1340 std::swap(CB.TrueBB, CB.FalseBB);
1341 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001342 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Bill Wendling187361b2010-01-23 10:26:57 +00001343 DAG.AssignOrdering(Cond.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001344 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001345
Dale Johannesenf5d97892009-02-04 01:48:28 +00001346 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001347 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001348 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling187361b2010-01-23 10:26:57 +00001349 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001350
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001351 // If the branch was constant folded, fix up the CFG.
1352 if (BrCond.getOpcode() == ISD::BR) {
1353 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001354 } else {
1355 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001356 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001357 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001358
Bill Wendling87710f02009-12-21 23:47:40 +00001359 if (CB.FalseBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001360 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1361 DAG.getBasicBlock(CB.FalseBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001362
Bill Wendling187361b2010-01-23 10:26:57 +00001363 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001364 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001365 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001366
1367 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001368}
1369
1370/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001371void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001372 // Emit the code for the jump table
1373 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001374 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001375 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1376 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001377 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001378 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1379 MVT::Other, Index.getValue(1),
1380 Table, Index);
1381 DAG.setRoot(BrJumpTable);
1382
Bill Wendling187361b2010-01-23 10:26:57 +00001383 DAG.AssignOrdering(Index.getNode(), SDNodeOrder);
1384 DAG.AssignOrdering(Table.getNode(), SDNodeOrder);
1385 DAG.AssignOrdering(BrJumpTable.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001386}
1387
1388/// visitJumpTableHeader - This function emits necessary code to produce index
1389/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001390void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1391 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001392 // Subtract the lowest switch case value from the value being switched on and
1393 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001394 // difference between smallest and largest cases.
1395 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001396 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001397 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001398 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001399
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001400 // The SDNode we just created, which holds the value being switched on minus
1401 // the the smallest case value, needs to be copied to a virtual register so it
1402 // can be used as an index into the jump table in a subsequent basic block.
1403 // This value may be smaller or larger than the target's pointer type, and
1404 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001405 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001406
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001407 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001408 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1409 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001410 JT.Reg = JumpTableReg;
1411
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001412 // Emit the range check for the jump table, and branch to the default block
1413 // for the switch statement if the value being switched on exceeds the largest
1414 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001415 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001416 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001417 DAG.getConstant(JTH.Last-JTH.First,VT),
1418 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001419
Bill Wendling187361b2010-01-23 10:26:57 +00001420 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1421 DAG.AssignOrdering(SwitchOp.getNode(), SDNodeOrder);
1422 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
1423 DAG.AssignOrdering(CMP.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001424
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001425 // Set NextBlock to be the MBB immediately after the current one, if any.
1426 // This is used to avoid emitting unnecessary branches to the next block.
1427 MachineBasicBlock *NextBlock = 0;
1428 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001429
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001430 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001431 NextBlock = BBI;
1432
Dale Johannesen66978ee2009-01-31 02:22:37 +00001433 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001434 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001435 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001436
Bill Wendling187361b2010-01-23 10:26:57 +00001437 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001438
1439 if (JT.MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001440 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1441 DAG.getBasicBlock(JT.MBB));
Bill Wendling187361b2010-01-23 10:26:57 +00001442 DAG.AssignOrdering(BrCond.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001443 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001444
Bill Wendling87710f02009-12-21 23:47:40 +00001445 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001446}
1447
1448/// visitBitTestHeader - This function emits necessary code to produce value
1449/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001450void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001451 // Subtract the minimum value
1452 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001453 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001454 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001455 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001456
1457 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001458 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001459 TLI.getSetCCResultType(Sub.getValueType()),
1460 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001461 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001462
Bill Wendling87710f02009-12-21 23:47:40 +00001463 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1464 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001465
Duncan Sands92abc622009-01-31 15:50:11 +00001466 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001467 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1468 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001469
Bill Wendling187361b2010-01-23 10:26:57 +00001470 DAG.AssignOrdering(Sub.getNode(), SDNodeOrder);
1471 DAG.AssignOrdering(RangeCmp.getNode(), SDNodeOrder);
1472 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1473 DAG.AssignOrdering(CopyTo.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001474
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001475 // Set NextBlock to be the MBB immediately after the current one, if any.
1476 // This is used to avoid emitting unnecessary branches to the next block.
1477 MachineBasicBlock *NextBlock = 0;
1478 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001479 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001480 NextBlock = BBI;
1481
1482 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1483
1484 CurMBB->addSuccessor(B.Default);
1485 CurMBB->addSuccessor(MBB);
1486
Dale Johannesen66978ee2009-01-31 02:22:37 +00001487 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001488 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001489 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001490
Bill Wendling187361b2010-01-23 10:26:57 +00001491 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001492
1493 if (MBB != NextBlock) {
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001494 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1495 DAG.getBasicBlock(MBB));
Bill Wendling187361b2010-01-23 10:26:57 +00001496 DAG.AssignOrdering(BrRange.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001497 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001498
Bill Wendling87710f02009-12-21 23:47:40 +00001499 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001500}
1501
1502/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001503void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1504 unsigned Reg,
1505 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001506 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001507 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001508 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001509 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001510 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001511 DAG.getConstant(1, TLI.getPointerTy()),
1512 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001513
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001514 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001515 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001516 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001517 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001518 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1519 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001520 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001521 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001522
Bill Wendling187361b2010-01-23 10:26:57 +00001523 DAG.AssignOrdering(ShiftOp.getNode(), SDNodeOrder);
1524 DAG.AssignOrdering(SwitchVal.getNode(), SDNodeOrder);
1525 DAG.AssignOrdering(AndOp.getNode(), SDNodeOrder);
1526 DAG.AssignOrdering(AndCmp.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001527
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001528 CurMBB->addSuccessor(B.TargetBB);
1529 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001530
Dale Johannesen66978ee2009-01-31 02:22:37 +00001531 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001532 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001533 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001534
Bill Wendling187361b2010-01-23 10:26:57 +00001535 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001536
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001537 // Set NextBlock to be the MBB immediately after the current one, if any.
1538 // This is used to avoid emitting unnecessary branches to the next block.
1539 MachineBasicBlock *NextBlock = 0;
1540 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001541 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001542 NextBlock = BBI;
1543
Bill Wendling87710f02009-12-21 23:47:40 +00001544 if (NextMBB != NextBlock) {
Bill Wendling0777e922009-12-21 21:59:52 +00001545 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1546 DAG.getBasicBlock(NextMBB));
Bill Wendling187361b2010-01-23 10:26:57 +00001547 DAG.AssignOrdering(BrAnd.getNode(), SDNodeOrder);
Bill Wendling87710f02009-12-21 23:47:40 +00001548 }
Bill Wendling0777e922009-12-21 21:59:52 +00001549
Bill Wendling87710f02009-12-21 23:47:40 +00001550 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551}
1552
Dan Gohman2048b852009-11-23 18:04:58 +00001553void SelectionDAGBuilder::visitInvoke(InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001554 // Retrieve successors.
1555 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1556 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1557
Gabor Greifb67e6b32009-01-15 11:10:44 +00001558 const Value *Callee(I.getCalledValue());
1559 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001560 visitInlineAsm(&I);
1561 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001562 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001563
1564 // If the value of the invoke is used outside of its defining block, make it
1565 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001566 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567
1568 // Update successor info
1569 CurMBB->addSuccessor(Return);
1570 CurMBB->addSuccessor(LandingPad);
1571
1572 // Drop into normal successor.
Bill Wendling0777e922009-12-21 21:59:52 +00001573 SDValue Branch = DAG.getNode(ISD::BR, getCurDebugLoc(),
1574 MVT::Other, getControlRoot(),
1575 DAG.getBasicBlock(Return));
1576 DAG.setRoot(Branch);
Bill Wendling187361b2010-01-23 10:26:57 +00001577 DAG.AssignOrdering(Branch.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001578}
1579
Dan Gohman2048b852009-11-23 18:04:58 +00001580void SelectionDAGBuilder::visitUnwind(UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001581}
1582
1583/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1584/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001585bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1586 CaseRecVector& WorkList,
1587 Value* SV,
1588 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001589 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001590
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001591 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001592 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001593 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001594 return false;
1595
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001596 // Get the MachineFunction which holds the current MBB. This is used when
1597 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001598 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001599
1600 // Figure out which block is immediately after the current one.
1601 MachineBasicBlock *NextBlock = 0;
1602 MachineFunction::iterator BBI = CR.CaseBB;
1603
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001604 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001605 NextBlock = BBI;
1606
1607 // TODO: If any two of the cases has the same destination, and if one value
1608 // is the same as the other, but has one bit unset that the other has set,
1609 // use bit manipulation to do two compares at once. For example:
1610 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001611
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001612 // Rearrange the case blocks so that the last one falls through if possible.
1613 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1614 // The last case block won't fall through into 'NextBlock' if we emit the
1615 // branches in this order. See if rearranging a case value would help.
1616 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1617 if (I->BB == NextBlock) {
1618 std::swap(*I, BackCase);
1619 break;
1620 }
1621 }
1622 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001623
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001624 // Create a CaseBlock record representing a conditional branch to
1625 // the Case's target mbb if the value being switched on SV is equal
1626 // to C.
1627 MachineBasicBlock *CurBlock = CR.CaseBB;
1628 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1629 MachineBasicBlock *FallThrough;
1630 if (I != E-1) {
1631 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1632 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001633
1634 // Put SV in a virtual register to make it available from the new blocks.
1635 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001636 } else {
1637 // If the last case doesn't match, go to the default block.
1638 FallThrough = Default;
1639 }
1640
1641 Value *RHS, *LHS, *MHS;
1642 ISD::CondCode CC;
1643 if (I->High == I->Low) {
1644 // This is just small small case range :) containing exactly 1 case
1645 CC = ISD::SETEQ;
1646 LHS = SV; RHS = I->High; MHS = NULL;
1647 } else {
1648 CC = ISD::SETLE;
1649 LHS = I->Low; MHS = SV; RHS = I->High;
1650 }
1651 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001652
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653 // If emitting the first comparison, just call visitSwitchCase to emit the
1654 // code into the current block. Otherwise, push the CaseBlock onto the
1655 // vector to be later processed by SDISel, and insert the node's MBB
1656 // before the next MBB.
1657 if (CurBlock == CurMBB)
1658 visitSwitchCase(CB);
1659 else
1660 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001661
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001662 CurBlock = FallThrough;
1663 }
1664
1665 return true;
1666}
1667
1668static inline bool areJTsAllowed(const TargetLowering &TLI) {
1669 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001670 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1671 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001672}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001673
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001674static APInt ComputeRange(const APInt &First, const APInt &Last) {
1675 APInt LastExt(Last), FirstExt(First);
1676 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1677 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1678 return (LastExt - FirstExt + 1ULL);
1679}
1680
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001681/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001682bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1683 CaseRecVector& WorkList,
1684 Value* SV,
1685 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001686 Case& FrontCase = *CR.Range.first;
1687 Case& BackCase = *(CR.Range.second-1);
1688
Chris Lattnere880efe2009-11-07 07:50:34 +00001689 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1690 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001691
Chris Lattnere880efe2009-11-07 07:50:34 +00001692 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001693 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1694 I!=E; ++I)
1695 TSize += I->size();
1696
Chris Lattnere880efe2009-11-07 07:50:34 +00001697 if (!areJTsAllowed(TLI) || TSize.ult(APInt(First.getBitWidth(), 4)))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001698 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001699
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001700 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001701 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001702 if (Density < 0.4)
1703 return false;
1704
David Greene4b69d992010-01-05 01:24:57 +00001705 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001706 << "First entry: " << First << ". Last entry: " << Last << '\n'
1707 << "Range: " << Range
1708 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001709
1710 // Get the MachineFunction which holds the current MBB. This is used when
1711 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001712 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001713
1714 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001715 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001716 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001717
1718 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1719
1720 // Create a new basic block to hold the code for loading the address
1721 // of the jump table, and jumping to it. Update successor information;
1722 // we will either branch to the default case for the switch, or the jump
1723 // table.
1724 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1725 CurMF->insert(BBI, JumpTableBB);
1726 CR.CaseBB->addSuccessor(Default);
1727 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001728
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001729 // Build a vector of destination BBs, corresponding to each target
1730 // of the jump table. If the value of the jump table slot corresponds to
1731 // a case statement, push the case's BB onto the vector, otherwise, push
1732 // the default BB.
1733 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001734 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001735 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001736 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1737 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001738
1739 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001740 DestBBs.push_back(I->BB);
1741 if (TEI==High)
1742 ++I;
1743 } else {
1744 DestBBs.push_back(Default);
1745 }
1746 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001748 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001749 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1750 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001751 E = DestBBs.end(); I != E; ++I) {
1752 if (!SuccsHandled[(*I)->getNumber()]) {
1753 SuccsHandled[(*I)->getNumber()] = true;
1754 JumpTableBB->addSuccessor(*I);
1755 }
1756 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001757
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001758 // Create a jump table index for this jump table, or return an existing
1759 // one.
Chris Lattner071c62f2010-01-25 23:26:13 +00001760 unsigned JTEncoding = TLI.getJumpTableEncoding();
1761 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
1762 ->getJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001763
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001764 // Set the jump table information so that we can codegen it as a second
1765 // MachineBasicBlock
1766 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1767 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1768 if (CR.CaseBB == CurMBB)
1769 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001770
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001771 JTCases.push_back(JumpTableBlock(JTH, JT));
1772
1773 return true;
1774}
1775
1776/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1777/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001778bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1779 CaseRecVector& WorkList,
1780 Value* SV,
1781 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001782 // Get the MachineFunction which holds the current MBB. This is used when
1783 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001784 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001785
1786 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001787 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001788 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001789
1790 Case& FrontCase = *CR.Range.first;
1791 Case& BackCase = *(CR.Range.second-1);
1792 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1793
1794 // Size is the number of Cases represented by this range.
1795 unsigned Size = CR.Range.second - CR.Range.first;
1796
Chris Lattnere880efe2009-11-07 07:50:34 +00001797 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1798 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001799 double FMetric = 0;
1800 CaseItr Pivot = CR.Range.first + Size/2;
1801
1802 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1803 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001804 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001805 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1806 I!=E; ++I)
1807 TSize += I->size();
1808
Chris Lattnere880efe2009-11-07 07:50:34 +00001809 APInt LSize = FrontCase.size();
1810 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001811 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001812 << "First: " << First << ", Last: " << Last <<'\n'
1813 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001814 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1815 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001816 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1817 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001818 APInt Range = ComputeRange(LEnd, RBegin);
1819 assert((Range - 2ULL).isNonNegative() &&
1820 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001821 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001822 (LEnd - First + 1ULL).roundToDouble();
1823 double RDensity = (double)RSize.roundToDouble() /
1824 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001825 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001826 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001827 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001828 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1829 << "LDensity: " << LDensity
1830 << ", RDensity: " << RDensity << '\n'
1831 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001832 if (FMetric < Metric) {
1833 Pivot = J;
1834 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001835 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001836 }
1837
1838 LSize += J->size();
1839 RSize -= J->size();
1840 }
1841 if (areJTsAllowed(TLI)) {
1842 // If our case is dense we *really* should handle it earlier!
1843 assert((FMetric > 0) && "Should handle dense range earlier!");
1844 } else {
1845 Pivot = CR.Range.first + Size/2;
1846 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001847
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001848 CaseRange LHSR(CR.Range.first, Pivot);
1849 CaseRange RHSR(Pivot, CR.Range.second);
1850 Constant *C = Pivot->Low;
1851 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001852
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001853 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001854 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001856 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001857 // Pivot's Value, then we can branch directly to the LHS's Target,
1858 // rather than creating a leaf node for it.
1859 if ((LHSR.second - LHSR.first) == 1 &&
1860 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001861 cast<ConstantInt>(C)->getValue() ==
1862 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 TrueBB = LHSR.first->BB;
1864 } else {
1865 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1866 CurMF->insert(BBI, TrueBB);
1867 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001868
1869 // Put SV in a virtual register to make it available from the new blocks.
1870 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001871 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001872
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001873 // Similar to the optimization above, if the Value being switched on is
1874 // known to be less than the Constant CR.LT, and the current Case Value
1875 // is CR.LT - 1, then we can branch directly to the target block for
1876 // the current Case Value, rather than emitting a RHS leaf node for it.
1877 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001878 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1879 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001880 FalseBB = RHSR.first->BB;
1881 } else {
1882 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1883 CurMF->insert(BBI, FalseBB);
1884 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001885
1886 // Put SV in a virtual register to make it available from the new blocks.
1887 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001888 }
1889
1890 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001891 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001892 // Otherwise, branch to LHS.
1893 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1894
1895 if (CR.CaseBB == CurMBB)
1896 visitSwitchCase(CB);
1897 else
1898 SwitchCases.push_back(CB);
1899
1900 return true;
1901}
1902
1903/// handleBitTestsSwitchCase - if current case range has few destination and
1904/// range span less, than machine word bitwidth, encode case range into series
1905/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001906bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1907 CaseRecVector& WorkList,
1908 Value* SV,
1909 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001910 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001911 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001912
1913 Case& FrontCase = *CR.Range.first;
1914 Case& BackCase = *(CR.Range.second-1);
1915
1916 // Get the MachineFunction which holds the current MBB. This is used when
1917 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001918 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001919
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001920 // If target does not have legal shift left, do not emit bit tests at all.
1921 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1922 return false;
1923
Anton Korobeynikov23218582008-12-23 22:25:27 +00001924 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001925 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1926 I!=E; ++I) {
1927 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001928 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001929 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001930
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001931 // Count unique destinations
1932 SmallSet<MachineBasicBlock*, 4> Dests;
1933 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1934 Dests.insert(I->BB);
1935 if (Dests.size() > 3)
1936 // Don't bother the code below, if there are too much unique destinations
1937 return false;
1938 }
David Greene4b69d992010-01-05 01:24:57 +00001939 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001940 << Dests.size() << '\n'
1941 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001942
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001943 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001944 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1945 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001946 APInt cmpRange = maxValue - minValue;
1947
David Greene4b69d992010-01-05 01:24:57 +00001948 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001949 << "Low bound: " << minValue << '\n'
1950 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001951
1952 if (cmpRange.uge(APInt(cmpRange.getBitWidth(), IntPtrBits)) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001953 (!(Dests.size() == 1 && numCmps >= 3) &&
1954 !(Dests.size() == 2 && numCmps >= 5) &&
1955 !(Dests.size() >= 3 && numCmps >= 6)))
1956 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001957
David Greene4b69d992010-01-05 01:24:57 +00001958 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001959 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1960
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001961 // Optimize the case where all the case values fit in a
1962 // word without having to subtract minValue. In this case,
1963 // we can optimize away the subtraction.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001964 if (minValue.isNonNegative() &&
1965 maxValue.slt(APInt(maxValue.getBitWidth(), IntPtrBits))) {
1966 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001967 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001968 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001969 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001970
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001971 CaseBitsVector CasesBits;
1972 unsigned i, count = 0;
1973
1974 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1975 MachineBasicBlock* Dest = I->BB;
1976 for (i = 0; i < count; ++i)
1977 if (Dest == CasesBits[i].BB)
1978 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001979
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001980 if (i == count) {
1981 assert((count < 3) && "Too much destinations to test!");
1982 CasesBits.push_back(CaseBits(0, Dest, 0));
1983 count++;
1984 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001985
1986 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1987 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1988
1989 uint64_t lo = (lowValue - lowBound).getZExtValue();
1990 uint64_t hi = (highValue - lowBound).getZExtValue();
1991
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001992 for (uint64_t j = lo; j <= hi; j++) {
1993 CasesBits[i].Mask |= 1ULL << j;
1994 CasesBits[i].Bits++;
1995 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001996
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001997 }
1998 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001999
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002000 BitTestInfo BTC;
2001
2002 // Figure out which block is immediately after the current one.
2003 MachineFunction::iterator BBI = CR.CaseBB;
2004 ++BBI;
2005
2006 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
2007
David Greene4b69d992010-01-05 01:24:57 +00002008 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002009 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00002010 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002011 << ", Bits: " << CasesBits[i].Bits
2012 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002013
2014 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
2015 CurMF->insert(BBI, CaseBB);
2016 BTC.push_back(BitTestCase(CasesBits[i].Mask,
2017 CaseBB,
2018 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00002019
2020 // Put SV in a virtual register to make it available from the new blocks.
2021 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002022 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002023
2024 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 -1U, (CR.CaseBB == CurMBB),
2026 CR.CaseBB, Default, BTC);
2027
2028 if (CR.CaseBB == CurMBB)
2029 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00002030
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002031 BitTestCases.push_back(BTB);
2032
2033 return true;
2034}
2035
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002036/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00002037size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
2038 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00002039 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002040
2041 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00002042 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002043 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
2044 Cases.push_back(Case(SI.getSuccessorValue(i),
2045 SI.getSuccessorValue(i),
2046 SMBB));
2047 }
2048 std::sort(Cases.begin(), Cases.end(), CaseCmp());
2049
2050 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00002051 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002052 // Must recompute end() each iteration because it may be
2053 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00002054 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
2055 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
2056 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002057 MachineBasicBlock* nextBB = J->BB;
2058 MachineBasicBlock* currentBB = I->BB;
2059
2060 // If the two neighboring cases go to the same destination, merge them
2061 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00002062 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002063 I->High = J->High;
2064 J = Cases.erase(J);
2065 } else {
2066 I = J++;
2067 }
2068 }
2069
2070 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
2071 if (I->Low != I->High)
2072 // A range counts double, since it requires two compares.
2073 ++numCmps;
2074 }
2075
2076 return numCmps;
2077}
2078
Dan Gohman2048b852009-11-23 18:04:58 +00002079void SelectionDAGBuilder::visitSwitch(SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002080 // Figure out which block is immediately after the current one.
2081 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002082 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
2083
2084 // If there is only the default destination, branch to it if it is not the
2085 // next basic block. Otherwise, just fall through.
2086 if (SI.getNumOperands() == 2) {
2087 // Update machine-CFG edges.
2088
2089 // If this is not a fall-through branch, emit the branch.
2090 CurMBB->addSuccessor(Default);
Bill Wendling49fcff82009-12-21 22:30:11 +00002091 if (Default != NextBlock) {
Bill Wendling87710f02009-12-21 23:47:40 +00002092 SDValue Res = DAG.getNode(ISD::BR, getCurDebugLoc(),
Bill Wendling49fcff82009-12-21 22:30:11 +00002093 MVT::Other, getControlRoot(),
2094 DAG.getBasicBlock(Default));
Bill Wendling87710f02009-12-21 23:47:40 +00002095 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002096 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002097 }
2098
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002099 return;
2100 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00002101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002102 // If there are any non-default case statements, create a vector of Cases
2103 // representing each one, and sort the vector so that we can efficiently
2104 // create a binary search tree from them.
2105 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002106 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00002107 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00002108 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00002109 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002110
2111 // Get the Value to be switched on and default basic blocks, which will be
2112 // inserted into CaseBlock records, representing basic blocks in the binary
2113 // search tree.
2114 Value *SV = SI.getOperand(0);
2115
2116 // Push the initial CaseRec onto the worklist
2117 CaseRecVector WorkList;
2118 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
2119
2120 while (!WorkList.empty()) {
2121 // Grab a record representing a case range to process off the worklist
2122 CaseRec CR = WorkList.back();
2123 WorkList.pop_back();
2124
2125 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
2126 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002127
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002128 // If the range has few cases (two or less) emit a series of specific
2129 // tests.
2130 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
2131 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002132
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00002133 // If the switch has more than 5 blocks, and at least 40% dense, and the
2134 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002135 // lowering the switch to a binary tree of conditional branches.
2136 if (handleJTSwitchCase(CR, WorkList, SV, Default))
2137 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00002138
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002139 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2140 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2141 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2142 }
2143}
2144
Dan Gohman2048b852009-11-23 18:04:58 +00002145void SelectionDAGBuilder::visitIndirectBr(IndirectBrInst &I) {
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002146 // Update machine-CFG edges.
2147 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
2148 CurMBB->addSuccessor(FuncInfo.MBBMap[I.getSuccessor(i)]);
2149
Bill Wendling49fcff82009-12-21 22:30:11 +00002150 SDValue Res = DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2151 MVT::Other, getControlRoot(),
2152 getValue(I.getAddress()));
2153 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002154 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002155}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002156
Dan Gohman2048b852009-11-23 18:04:58 +00002157void SelectionDAGBuilder::visitFSub(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002158 // -0.0 - X --> fneg
2159 const Type *Ty = I.getType();
2160 if (isa<VectorType>(Ty)) {
2161 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2162 const VectorType *DestTy = cast<VectorType>(I.getType());
2163 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002164 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002165 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002166 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002167 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002168 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002169 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002170 Op2.getValueType(), Op2);
Bill Wendling49fcff82009-12-21 22:30:11 +00002171 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002172 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002173 return;
2174 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002175 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002176 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002177
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002178 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002179 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002180 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002181 SDValue Res = DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2182 Op2.getValueType(), Op2);
2183 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002184 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002185 return;
2186 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002188 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002189}
2190
Dan Gohman2048b852009-11-23 18:04:58 +00002191void SelectionDAGBuilder::visitBinary(User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002192 SDValue Op1 = getValue(I.getOperand(0));
2193 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling49fcff82009-12-21 22:30:11 +00002194 SDValue Res = DAG.getNode(OpCode, getCurDebugLoc(),
2195 Op1.getValueType(), Op1, Op2);
2196 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002197 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002198}
2199
Dan Gohman2048b852009-11-23 18:04:58 +00002200void SelectionDAGBuilder::visitShift(User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002201 SDValue Op1 = getValue(I.getOperand(0));
2202 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman57fc82d2009-04-09 03:51:29 +00002203 if (!isa<VectorType>(I.getType()) &&
2204 Op2.getValueType() != TLI.getShiftAmountTy()) {
2205 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002206 EVT PTy = TLI.getPointerTy();
2207 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002208 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002209 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2210 TLI.getShiftAmountTy(), Op2);
2211 // If the operand is larger than the shift count type but the shift
2212 // count type has enough bits to represent any shift value, truncate
2213 // it now. This is a common case and it exposes the truncate to
2214 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002215 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002216 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2217 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2218 TLI.getShiftAmountTy(), Op2);
2219 // Otherwise we'll need to temporarily settle for some other
2220 // convenient type; type legalization will make adjustments as
2221 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002222 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002223 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002224 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002225 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002226 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002227 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002228 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002229
Bill Wendling49fcff82009-12-21 22:30:11 +00002230 SDValue Res = DAG.getNode(Opcode, getCurDebugLoc(),
2231 Op1.getValueType(), Op1, Op2);
2232 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002233 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
2234 DAG.AssignOrdering(Op2.getNode(), SDNodeOrder);
2235 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002236}
2237
Dan Gohman2048b852009-11-23 18:04:58 +00002238void SelectionDAGBuilder::visitICmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
2240 if (ICmpInst *IC = dyn_cast<ICmpInst>(&I))
2241 predicate = IC->getPredicate();
2242 else if (ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
2243 predicate = ICmpInst::Predicate(IC->getPredicate());
2244 SDValue Op1 = getValue(I.getOperand(0));
2245 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002246 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002247
Owen Andersone50ed302009-08-10 22:56:29 +00002248 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002249 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode);
2250 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002251 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002252}
2253
Dan Gohman2048b852009-11-23 18:04:58 +00002254void SelectionDAGBuilder::visitFCmp(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002255 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
2256 if (FCmpInst *FC = dyn_cast<FCmpInst>(&I))
2257 predicate = FC->getPredicate();
2258 else if (ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
2259 predicate = FCmpInst::Predicate(FC->getPredicate());
2260 SDValue Op1 = getValue(I.getOperand(0));
2261 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002262 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002263 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002264 SDValue Res = DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition);
2265 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002266 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002267}
2268
Dan Gohman2048b852009-11-23 18:04:58 +00002269void SelectionDAGBuilder::visitSelect(User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002270 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002271 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2272 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002273 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002274
Bill Wendling49fcff82009-12-21 22:30:11 +00002275 SmallVector<SDValue, 4> Values(NumValues);
2276 SDValue Cond = getValue(I.getOperand(0));
2277 SDValue TrueVal = getValue(I.getOperand(1));
2278 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002279
Bill Wendling49fcff82009-12-21 22:30:11 +00002280 for (unsigned i = 0; i != NumValues; ++i) {
2281 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
2282 TrueVal.getNode()->getValueType(i), Cond,
2283 SDValue(TrueVal.getNode(),
2284 TrueVal.getResNo() + i),
2285 SDValue(FalseVal.getNode(),
2286 FalseVal.getResNo() + i));
2287
Bill Wendling187361b2010-01-23 10:26:57 +00002288 DAG.AssignOrdering(Values[i].getNode(), SDNodeOrder);
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002289 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002290
Bill Wendling49fcff82009-12-21 22:30:11 +00002291 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2292 DAG.getVTList(&ValueVTs[0], NumValues),
2293 &Values[0], NumValues);
2294 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002295 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002296}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002297
Dan Gohman2048b852009-11-23 18:04:58 +00002298void SelectionDAGBuilder::visitTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002299 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2300 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002301 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002302 SDValue Res = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N);
2303 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002304 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002305}
2306
Dan Gohman2048b852009-11-23 18:04:58 +00002307void SelectionDAGBuilder::visitZExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002308 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2309 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2310 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002311 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002312 SDValue Res = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N);
2313 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002314 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002315}
2316
Dan Gohman2048b852009-11-23 18:04:58 +00002317void SelectionDAGBuilder::visitSExt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002318 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2319 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2320 SDValue N = getValue(I.getOperand(0));
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.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N);
2323 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002324 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002325}
2326
Dan Gohman2048b852009-11-23 18:04:58 +00002327void SelectionDAGBuilder::visitFPTrunc(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002328 // FPTrunc is never a no-op cast, no need to check
2329 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002330 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002331 SDValue Res = DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2332 DestVT, N, DAG.getIntPtrConstant(0));
2333 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002334 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002335}
2336
Dan Gohman2048b852009-11-23 18:04:58 +00002337void SelectionDAGBuilder::visitFPExt(User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002338 // FPTrunc is never a no-op cast, no need to check
2339 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002340 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002341 SDValue Res = DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N);
2342 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002343 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002344}
2345
Dan Gohman2048b852009-11-23 18:04:58 +00002346void SelectionDAGBuilder::visitFPToUI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002347 // FPToUI is never a no-op cast, no need to check
2348 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002349 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002350 SDValue Res = DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N);
2351 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002352 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002353}
2354
Dan Gohman2048b852009-11-23 18:04:58 +00002355void SelectionDAGBuilder::visitFPToSI(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002356 // FPToSI is never a no-op cast, no need to check
2357 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002358 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002359 SDValue Res = DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N);
2360 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002361 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002362}
2363
Dan Gohman2048b852009-11-23 18:04:58 +00002364void SelectionDAGBuilder::visitUIToFP(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002365 // UIToFP is never a no-op cast, no need to check
2366 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002367 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002368 SDValue Res = DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N);
2369 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002370 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002371}
2372
Dan Gohman2048b852009-11-23 18:04:58 +00002373void SelectionDAGBuilder::visitSIToFP(User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002374 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002375 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::SINT_TO_FP, getCurDebugLoc(), DestVT, N);
2378 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002379 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002380}
2381
Dan Gohman2048b852009-11-23 18:04:58 +00002382void SelectionDAGBuilder::visitPtrToInt(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002383 // What to do depends on the size of the integer and the size of the pointer.
2384 // We can either truncate, zero extend, or no-op, accordingly.
2385 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002386 EVT SrcVT = N.getValueType();
2387 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002388 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2389 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002390 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002391}
2392
Dan Gohman2048b852009-11-23 18:04:58 +00002393void SelectionDAGBuilder::visitIntToPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002394 // What to do depends on the size of the integer and the size of the pointer.
2395 // We can either truncate, zero extend, or no-op, accordingly.
2396 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002397 EVT SrcVT = N.getValueType();
2398 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling49fcff82009-12-21 22:30:11 +00002399 SDValue Res = DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT);
2400 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002401 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002402}
2403
Dan Gohman2048b852009-11-23 18:04:58 +00002404void SelectionDAGBuilder::visitBitCast(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002405 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002406 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002407
Bill Wendling49fcff82009-12-21 22:30:11 +00002408 // BitCast assures us that source and destination are the same size so this is
2409 // either a BIT_CONVERT or a no-op.
2410 if (DestVT != N.getValueType()) {
2411 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2412 DestVT, N); // convert types.
2413 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002414 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling49fcff82009-12-21 22:30:11 +00002415 } else {
2416 setValue(&I, N); // noop cast.
2417 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002418}
2419
Dan Gohman2048b852009-11-23 18:04:58 +00002420void SelectionDAGBuilder::visitInsertElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002421 SDValue InVec = getValue(I.getOperand(0));
2422 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002423 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002424 TLI.getPointerTy(),
2425 getValue(I.getOperand(2)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002426 SDValue Res = DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2427 TLI.getValueType(I.getType()),
2428 InVec, InVal, InIdx);
2429 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002430
Bill Wendling187361b2010-01-23 10:26:57 +00002431 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
2432 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002433}
2434
Dan Gohman2048b852009-11-23 18:04:58 +00002435void SelectionDAGBuilder::visitExtractElement(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002436 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002437 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002438 TLI.getPointerTy(),
2439 getValue(I.getOperand(1)));
Bill Wendling49fcff82009-12-21 22:30:11 +00002440 SDValue Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2441 TLI.getValueType(I.getType()), InVec, InIdx);
2442 setValue(&I, Res);
2443
Bill Wendling187361b2010-01-23 10:26:57 +00002444 DAG.AssignOrdering(InIdx.getNode(), SDNodeOrder);
2445 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002446}
2447
Mon P Wangaeb06d22008-11-10 04:46:22 +00002448
2449// Utility for visitShuffleVector - Returns true if the mask is mask starting
2450// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002451static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2452 unsigned MaskNumElts = Mask.size();
2453 for (unsigned i = 0; i != MaskNumElts; ++i)
2454 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002455 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002456 return true;
2457}
2458
Dan Gohman2048b852009-11-23 18:04:58 +00002459void SelectionDAGBuilder::visitShuffleVector(User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002460 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002461 SDValue Src1 = getValue(I.getOperand(0));
2462 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002463
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 // Convert the ConstantVector mask operand into an array of ints, with -1
2465 // representing undef values.
2466 SmallVector<Constant*, 8> MaskElts;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002467 cast<Constant>(I.getOperand(2))->getVectorElements(*DAG.getContext(),
Owen Anderson001dbfe2009-07-16 18:04:31 +00002468 MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002469 unsigned MaskNumElts = MaskElts.size();
2470 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002471 if (isa<UndefValue>(MaskElts[i]))
2472 Mask.push_back(-1);
2473 else
2474 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2475 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002476
Owen Andersone50ed302009-08-10 22:56:29 +00002477 EVT VT = TLI.getValueType(I.getType());
2478 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002479 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002480
Mon P Wangc7849c22008-11-16 05:06:27 +00002481 if (SrcNumElts == MaskNumElts) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002482 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2483 &Mask[0]);
2484 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002485 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002486 return;
2487 }
2488
2489 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002490 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2491 // Mask is longer than the source vectors and is a multiple of the source
2492 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002493 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002494 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2495 // The shuffle is concatenating two vectors together.
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002496 SDValue Res = DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2497 VT, Src1, Src2);
2498 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002499 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002500 return;
2501 }
2502
Mon P Wangc7849c22008-11-16 05:06:27 +00002503 // Pad both vectors with undefs to make them the same length as the mask.
2504 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002505 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2506 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002507 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002508
Nate Begeman9008ca62009-04-27 18:41:29 +00002509 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2510 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002511 MOps1[0] = Src1;
2512 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002513
2514 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2515 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002516 &MOps1[0], NumConcat);
2517 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002518 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002519 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002520
Mon P Wangaeb06d22008-11-10 04:46:22 +00002521 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002522 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002523 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002524 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002525 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002526 MappedOps.push_back(Idx);
2527 else
2528 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002529 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002530
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002531 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002532 &MappedOps[0]);
2533 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002534 DAG.AssignOrdering(Src1.getNode(), SDNodeOrder);
2535 DAG.AssignOrdering(Src2.getNode(), SDNodeOrder);
2536 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002537 return;
2538 }
2539
Mon P Wangc7849c22008-11-16 05:06:27 +00002540 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002541 // Analyze the access pattern of the vector to see if we can extract
2542 // two subvectors and do the shuffle. The analysis is done by calculating
2543 // the range of elements the mask access on both vectors.
2544 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2545 int MaxRange[2] = {-1, -1};
2546
Nate Begeman5a5ca152009-04-29 05:20:52 +00002547 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002548 int Idx = Mask[i];
2549 int Input = 0;
2550 if (Idx < 0)
2551 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002552
Nate Begeman5a5ca152009-04-29 05:20:52 +00002553 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002554 Input = 1;
2555 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002556 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002557 if (Idx > MaxRange[Input])
2558 MaxRange[Input] = Idx;
2559 if (Idx < MinRange[Input])
2560 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002561 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002562
Mon P Wangc7849c22008-11-16 05:06:27 +00002563 // Check if the access is smaller than the vector size and can we find
2564 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002565 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2566 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002567 int StartIdx[2]; // StartIdx to extract from
2568 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002569 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002570 RangeUse[Input] = 0; // Unused
2571 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002572 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002573 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002574 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002575 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002576 RangeUse[Input] = 1; // Extract from beginning of the vector
2577 StartIdx[Input] = 0;
2578 } else {
2579 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002580 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002581 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002582 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002583 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002584 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002585 }
2586
Bill Wendling636e2582009-08-21 18:16:06 +00002587 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002588 SDValue Res = DAG.getUNDEF(VT);
2589 setValue(&I, Res); // Vectors are not used.
Bill Wendling187361b2010-01-23 10:26:57 +00002590 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangc7849c22008-11-16 05:06:27 +00002591 return;
2592 }
2593 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2594 // Extract appropriate subvector and generate a vector shuffle
2595 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002596 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002597 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002598 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002599 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002600 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002601 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002602
Bill Wendling187361b2010-01-23 10:26:57 +00002603 DAG.AssignOrdering(Src.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002604 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002605
Mon P Wangc7849c22008-11-16 05:06:27 +00002606 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002607 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002608 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002609 int Idx = Mask[i];
2610 if (Idx < 0)
2611 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002612 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002613 MappedOps.push_back(Idx - StartIdx[0]);
2614 else
2615 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002616 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002617
2618 SDValue Res = DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2619 &MappedOps[0]);
2620 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002621 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangc7849c22008-11-16 05:06:27 +00002622 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002623 }
2624 }
2625
Mon P Wangc7849c22008-11-16 05:06:27 +00002626 // We can't use either concat vectors or extract subvectors so fall back to
2627 // replacing the shuffle with extract and build vector.
2628 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002629 EVT EltVT = VT.getVectorElementType();
2630 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002631 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002632 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002633 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002634 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002635 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002636 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002637 SDValue Res;
2638
Nate Begeman5a5ca152009-04-29 05:20:52 +00002639 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002640 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2641 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002642 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002643 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2644 EltVT, Src2,
2645 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2646
2647 Ops.push_back(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002648 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002649 }
2650 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002651
2652 SDValue Res = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2653 VT, &Ops[0], Ops.size());
2654 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002655 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002656}
2657
Dan Gohman2048b852009-11-23 18:04:58 +00002658void SelectionDAGBuilder::visitInsertValue(InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002659 const Value *Op0 = I.getOperand(0);
2660 const Value *Op1 = I.getOperand(1);
2661 const Type *AggTy = I.getType();
2662 const Type *ValTy = Op1->getType();
2663 bool IntoUndef = isa<UndefValue>(Op0);
2664 bool FromUndef = isa<UndefValue>(Op1);
2665
2666 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2667 I.idx_begin(), I.idx_end());
2668
Owen Andersone50ed302009-08-10 22:56:29 +00002669 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002670 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002671 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002672 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2673
2674 unsigned NumAggValues = AggValueVTs.size();
2675 unsigned NumValValues = ValValueVTs.size();
2676 SmallVector<SDValue, 4> Values(NumAggValues);
2677
2678 SDValue Agg = getValue(Op0);
2679 SDValue Val = getValue(Op1);
2680 unsigned i = 0;
2681 // Copy the beginning value(s) from the original aggregate.
2682 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002683 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002684 SDValue(Agg.getNode(), Agg.getResNo() + i);
2685 // Copy values from the inserted value(s).
2686 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002687 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002688 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2689 // Copy remaining value(s) from the original aggregate.
2690 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002691 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002692 SDValue(Agg.getNode(), Agg.getResNo() + i);
2693
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002694 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2695 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2696 &Values[0], NumAggValues);
2697 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002698 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002699}
2700
Dan Gohman2048b852009-11-23 18:04:58 +00002701void SelectionDAGBuilder::visitExtractValue(ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002702 const Value *Op0 = I.getOperand(0);
2703 const Type *AggTy = Op0->getType();
2704 const Type *ValTy = I.getType();
2705 bool OutOfUndef = isa<UndefValue>(Op0);
2706
2707 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2708 I.idx_begin(), I.idx_end());
2709
Owen Andersone50ed302009-08-10 22:56:29 +00002710 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002711 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2712
2713 unsigned NumValValues = ValValueVTs.size();
2714 SmallVector<SDValue, 4> Values(NumValValues);
2715
2716 SDValue Agg = getValue(Op0);
2717 // Copy out the selected value(s).
2718 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2719 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002720 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002721 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002722 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002723
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002724 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2725 DAG.getVTList(&ValValueVTs[0], NumValValues),
2726 &Values[0], NumValValues);
2727 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002728 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002729}
2730
Dan Gohman2048b852009-11-23 18:04:58 +00002731void SelectionDAGBuilder::visitGetElementPtr(User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002732 SDValue N = getValue(I.getOperand(0));
2733 const Type *Ty = I.getOperand(0)->getType();
2734
2735 for (GetElementPtrInst::op_iterator OI = I.op_begin()+1, E = I.op_end();
2736 OI != E; ++OI) {
2737 Value *Idx = *OI;
2738 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2739 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2740 if (Field) {
2741 // N = N + Offset
2742 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002743 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002744 DAG.getIntPtrConstant(Offset));
Bill Wendling187361b2010-01-23 10:26:57 +00002745 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002746 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002748 Ty = StTy->getElementType(Field);
2749 } else {
2750 Ty = cast<SequentialType>(Ty)->getElementType();
2751
2752 // If this is a constant subscript, handle it quickly.
2753 if (ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
2754 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002755 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002756 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002757 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002758 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002759 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002760 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002761 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2762 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002763 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002764 else
Evan Chengb1032a82009-02-09 20:54:38 +00002765 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002766
Dale Johannesen66978ee2009-01-31 02:22:37 +00002767 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002768 OffsVal);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002769
Bill Wendling187361b2010-01-23 10:26:57 +00002770 DAG.AssignOrdering(OffsVal.getNode(), SDNodeOrder);
2771 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002772 continue;
2773 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002774
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002775 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002776 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2777 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002778 SDValue IdxN = getValue(Idx);
2779
2780 // If the index is smaller or larger than intptr_t, truncate or extend
2781 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002782 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002783
2784 // If this is a multiply by a power of two, turn it into a shl
2785 // immediately. This is a very common case.
2786 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002787 if (ElementSize.isPowerOf2()) {
2788 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002789 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002790 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002791 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002792 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002793 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002794 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002795 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002796 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002797
Bill Wendling187361b2010-01-23 10:26:57 +00002798 DAG.AssignOrdering(IdxN.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002799 }
2800
Scott Michelfdc40a02009-02-17 22:15:04 +00002801 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002802 N.getValueType(), N, IdxN);
Bill Wendling187361b2010-01-23 10:26:57 +00002803 DAG.AssignOrdering(N.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002804 }
2805 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002806
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002807 setValue(&I, N);
2808}
2809
Dan Gohman2048b852009-11-23 18:04:58 +00002810void SelectionDAGBuilder::visitAlloca(AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002811 // If this is a fixed sized alloca in the entry block of the function,
2812 // allocate it statically on the stack.
2813 if (FuncInfo.StaticAllocaMap.count(&I))
2814 return; // getValue will auto-populate this.
2815
2816 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002817 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002818 unsigned Align =
2819 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2820 I.getAlignment());
2821
2822 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002823
Chris Lattner0b18e592009-03-17 19:36:00 +00002824 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2825 AllocSize,
2826 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002827
Bill Wendling187361b2010-01-23 10:26:57 +00002828 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002829
Owen Andersone50ed302009-08-10 22:56:29 +00002830 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002831 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Bill Wendling187361b2010-01-23 10:26:57 +00002832 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002833
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002834 // Handle alignment. If the requested alignment is less than or equal to
2835 // the stack alignment, ignore it. If the size is greater than or equal to
2836 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
2837 unsigned StackAlign =
2838 TLI.getTargetMachine().getFrameInfo()->getStackAlignment();
2839 if (Align <= StackAlign)
2840 Align = 0;
2841
2842 // Round the size of the allocation up to the stack alignment size
2843 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002844 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002845 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002846 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling187361b2010-01-23 10:26:57 +00002847 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00002848
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002849 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002850 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002851 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002852 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
Bill Wendling187361b2010-01-23 10:26:57 +00002853 DAG.AssignOrdering(AllocSize.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002854
2855 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002856 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002857 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002858 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002859 setValue(&I, DSA);
2860 DAG.setRoot(DSA.getValue(1));
Bill Wendling187361b2010-01-23 10:26:57 +00002861 DAG.AssignOrdering(DSA.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00002862
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002863 // Inform the Frame Information that we have just allocated a variable-sized
2864 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002865 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002866}
2867
Dan Gohman2048b852009-11-23 18:04:58 +00002868void SelectionDAGBuilder::visitLoad(LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002869 const Value *SV = I.getOperand(0);
2870 SDValue Ptr = getValue(SV);
2871
2872 const Type *Ty = I.getType();
2873 bool isVolatile = I.isVolatile();
2874 unsigned Alignment = I.getAlignment();
2875
Owen Andersone50ed302009-08-10 22:56:29 +00002876 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002877 SmallVector<uint64_t, 4> Offsets;
2878 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2879 unsigned NumValues = ValueVTs.size();
2880 if (NumValues == 0)
2881 return;
2882
2883 SDValue Root;
2884 bool ConstantMemory = false;
2885 if (I.isVolatile())
2886 // Serialize volatile loads with other side effects.
2887 Root = getRoot();
2888 else if (AA->pointsToConstantMemory(SV)) {
2889 // Do not serialize (non-volatile) loads of constant memory with anything.
2890 Root = DAG.getEntryNode();
2891 ConstantMemory = true;
2892 } else {
2893 // Do not serialize non-volatile loads against each other.
2894 Root = DAG.getRoot();
2895 }
2896
2897 SmallVector<SDValue, 4> Values(NumValues);
2898 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002899 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002900 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002901 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2902 PtrVT, Ptr,
2903 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002904 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
Bill Wendling856ff412009-12-22 00:12:37 +00002905 A, SV, Offsets[i], isVolatile, Alignment);
2906
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002907 Values[i] = L;
2908 Chains[i] = L.getValue(1);
Bill Wendling856ff412009-12-22 00:12:37 +00002909
Bill Wendling187361b2010-01-23 10:26:57 +00002910 DAG.AssignOrdering(A.getNode(), SDNodeOrder);
2911 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002912 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002913
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002914 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002915 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002916 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002917 if (isVolatile)
2918 DAG.setRoot(Chain);
2919 else
2920 PendingLoads.push_back(Chain);
Bill Wendling856ff412009-12-22 00:12:37 +00002921
Bill Wendling187361b2010-01-23 10:26:57 +00002922 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002923 }
2924
Bill Wendling856ff412009-12-22 00:12:37 +00002925 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2926 DAG.getVTList(&ValueVTs[0], NumValues),
2927 &Values[0], NumValues);
2928 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002929 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00002930}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002931
Dan Gohman2048b852009-11-23 18:04:58 +00002932void SelectionDAGBuilder::visitStore(StoreInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002933 Value *SrcV = I.getOperand(0);
2934 Value *PtrV = I.getOperand(1);
2935
Owen Andersone50ed302009-08-10 22:56:29 +00002936 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002937 SmallVector<uint64_t, 4> Offsets;
2938 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2939 unsigned NumValues = ValueVTs.size();
2940 if (NumValues == 0)
2941 return;
2942
2943 // Get the lowered operands. Note that we do this after
2944 // checking if NumResults is zero, because with zero results
2945 // the operands won't have values in the map.
2946 SDValue Src = getValue(SrcV);
2947 SDValue Ptr = getValue(PtrV);
2948
2949 SDValue Root = getRoot();
2950 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002951 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002952 bool isVolatile = I.isVolatile();
2953 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002954
2955 for (unsigned i = 0; i != NumValues; ++i) {
2956 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2957 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002958 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002959 SDValue(Src.getNode(), Src.getResNo() + i),
Bill Wendling856ff412009-12-22 00:12:37 +00002960 Add, PtrV, Offsets[i], isVolatile, Alignment);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002961
Bill Wendling187361b2010-01-23 10:26:57 +00002962 DAG.AssignOrdering(Add.getNode(), SDNodeOrder);
2963 DAG.AssignOrdering(Chains[i].getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00002964 }
2965
2966 SDValue Res = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2967 MVT::Other, &Chains[0], NumValues);
2968 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00002969 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002970}
2971
2972/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2973/// node.
Dan Gohman2048b852009-11-23 18:04:58 +00002974void SelectionDAGBuilder::visitTargetIntrinsic(CallInst &I,
2975 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002976 bool HasChain = !I.doesNotAccessMemory();
2977 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2978
2979 // Build the operand list.
2980 SmallVector<SDValue, 8> Ops;
2981 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2982 if (OnlyLoad) {
2983 // We don't need to serialize loads against other loads.
2984 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002985 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002986 Ops.push_back(getRoot());
2987 }
2988 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002989
2990 // Info is set by getTgtMemInstrinsic
2991 TargetLowering::IntrinsicInfo Info;
2992 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2993
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002994 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002995 if (!IsTgtIntrinsic)
2996 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002997
2998 // Add all operands of the call to the operand list.
2999 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3000 SDValue Op = getValue(I.getOperand(i));
3001 assert(TLI.isTypeLegal(Op.getValueType()) &&
3002 "Intrinsic uses a non-legal type?");
3003 Ops.push_back(Op);
3004 }
3005
Owen Andersone50ed302009-08-10 22:56:29 +00003006 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00003007 ComputeValueVTs(TLI, I.getType(), ValueVTs);
3008#ifndef NDEBUG
3009 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
3010 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
3011 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003012 }
Bob Wilson8d919552009-07-31 22:41:21 +00003013#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00003014
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003015 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00003016 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003017
Bob Wilson8d919552009-07-31 22:41:21 +00003018 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003019
3020 // Create the node.
3021 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003022 if (IsTgtIntrinsic) {
3023 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00003024 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003025 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00003026 Info.memVT, Info.ptrVal, Info.offset,
3027 Info.align, Info.vol,
3028 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00003029 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003030 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003031 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00003032 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003033 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003034 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003035 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00003036 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00003037 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00003038 }
3039
Bill Wendling187361b2010-01-23 10:26:57 +00003040 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003041
3042 if (HasChain) {
3043 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
3044 if (OnlyLoad)
3045 PendingLoads.push_back(Chain);
3046 else
3047 DAG.setRoot(Chain);
3048 }
Bill Wendling856ff412009-12-22 00:12:37 +00003049
Benjamin Kramerf0127052010-01-05 13:12:22 +00003050 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003051 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00003052 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003053 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Bill Wendling187361b2010-01-23 10:26:57 +00003054 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003055 }
Bill Wendling856ff412009-12-22 00:12:37 +00003056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003057 setValue(&I, Result);
3058 }
3059}
3060
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003061/// GetSignificand - Get the significand and build it into a floating-point
3062/// number with exponent of 1:
3063///
3064/// Op = (Op & 0x007fffff) | 0x3f800000;
3065///
3066/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003067static SDValue
Bill Wendling856ff412009-12-22 00:12:37 +00003068GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003069 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3070 DAG.getConstant(0x007fffff, MVT::i32));
3071 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
3072 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003073 SDValue Res = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
3074
Bill Wendling187361b2010-01-23 10:26:57 +00003075 DAG.AssignOrdering(t1.getNode(), Order);
3076 DAG.AssignOrdering(t2.getNode(), Order);
3077 DAG.AssignOrdering(Res.getNode(), Order);
Bill Wendling856ff412009-12-22 00:12:37 +00003078 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003079}
3080
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003081/// GetExponent - Get the exponent:
3082///
Bill Wendlinge9a72862009-01-20 21:17:57 +00003083/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003084///
3085/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00003086static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00003087GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling856ff412009-12-22 00:12:37 +00003088 DebugLoc dl, unsigned Order) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003089 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
3090 DAG.getConstant(0x7f800000, MVT::i32));
3091 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00003092 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00003093 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
3094 DAG.getConstant(127, MVT::i32));
Bill Wendling856ff412009-12-22 00:12:37 +00003095 SDValue Res = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
3096
Bill Wendling187361b2010-01-23 10:26:57 +00003097 DAG.AssignOrdering(t0.getNode(), Order);
3098 DAG.AssignOrdering(t1.getNode(), Order);
3099 DAG.AssignOrdering(t2.getNode(), Order);
3100 DAG.AssignOrdering(Res.getNode(), Order);
Bill Wendling856ff412009-12-22 00:12:37 +00003101 return Res;
Bill Wendling39150252008-09-09 20:39:27 +00003102}
3103
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003104/// getF32Constant - Get 32-bit floating point constant.
3105static SDValue
3106getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003108}
3109
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003110/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003111/// visitIntrinsicCall: I is a call instruction
3112/// Op is the associated NodeType for I
3113const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003114SelectionDAGBuilder::implVisitBinaryAtomic(CallInst& I, ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003115 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003116 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00003117 DAG.getAtomic(Op, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003118 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003119 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003120 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00003121 getValue(I.getOperand(2)),
3122 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003123 setValue(&I, L);
3124 DAG.setRoot(L.getValue(1));
Bill Wendling187361b2010-01-23 10:26:57 +00003125 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003126 return 0;
3127}
3128
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003129// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00003130const char *
Dan Gohman2048b852009-11-23 18:04:58 +00003131SelectionDAGBuilder::implVisitAluOverflow(CallInst &I, ISD::NodeType Op) {
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003132 SDValue Op1 = getValue(I.getOperand(1));
3133 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00003134
Owen Anderson825b72b2009-08-11 20:47:22 +00003135 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Dan Gohmanfc166572009-04-09 23:54:40 +00003136 SDValue Result = DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2);
Bill Wendling74c37652008-12-09 22:08:41 +00003137
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003138 setValue(&I, Result);
Bill Wendling187361b2010-01-23 10:26:57 +00003139 DAG.AssignOrdering(Result.getNode(), SDNodeOrder);
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00003140 return 0;
3141}
Bill Wendling74c37652008-12-09 22:08:41 +00003142
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003143/// visitExp - Lower an exp intrinsic. Handles the special sequences for
3144/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003145void
Dan Gohman2048b852009-11-23 18:04:58 +00003146SelectionDAGBuilder::visitExp(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003147 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003148 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003149
Owen Anderson825b72b2009-08-11 20:47:22 +00003150 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003151 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3152 SDValue Op = getValue(I.getOperand(1));
3153
3154 // Put the exponent in the right bit position for later addition to the
3155 // final result:
3156 //
3157 // #define LOG2OFe 1.4426950f
3158 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00003159 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003160 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003161 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003162
3163 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003164 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3165 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003166
Bill Wendling187361b2010-01-23 10:26:57 +00003167 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3168 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3169 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3170 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003171
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003172 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003173 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003174 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling187361b2010-01-23 10:26:57 +00003175 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003176
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003177 if (LimitFloatPrecision <= 6) {
3178 // For floating-point precision of 6:
3179 //
3180 // TwoToFractionalPartOfX =
3181 // 0.997535578f +
3182 // (0.735607626f + 0.252464424f * x) * x;
3183 //
3184 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003185 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003186 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003188 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003189 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3190 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003191 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003192 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003193
3194 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003195 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003196 TwoToFracPartOfX, IntegerPartOfX);
3197
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendling856ff412009-12-22 00:12:37 +00003199
Bill Wendling187361b2010-01-23 10:26:57 +00003200 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3201 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3202 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3203 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3204 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3205 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3206 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003207 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3208 // For floating-point precision of 12:
3209 //
3210 // TwoToFractionalPartOfX =
3211 // 0.999892986f +
3212 // (0.696457318f +
3213 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3214 //
3215 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003216 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003217 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003218 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003219 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003220 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3221 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003222 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003223 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3224 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003225 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003226 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003227
3228 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003229 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003230 TwoToFracPartOfX, IntegerPartOfX);
3231
Owen Anderson825b72b2009-08-11 20:47:22 +00003232 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendling856ff412009-12-22 00:12:37 +00003233
Bill Wendling187361b2010-01-23 10:26:57 +00003234 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3235 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3236 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3237 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3238 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3239 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3240 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3241 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3242 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003243 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3244 // For floating-point precision of 18:
3245 //
3246 // TwoToFractionalPartOfX =
3247 // 0.999999982f +
3248 // (0.693148872f +
3249 // (0.240227044f +
3250 // (0.554906021e-1f +
3251 // (0.961591928e-2f +
3252 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3253 //
3254 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003255 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003256 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003257 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003258 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3260 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003261 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003262 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3263 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003264 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003265 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3266 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003267 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003268 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3269 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003270 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003271 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3272 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003273 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003274 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003275 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003276
3277 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003278 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003279 TwoToFracPartOfX, IntegerPartOfX);
3280
Owen Anderson825b72b2009-08-11 20:47:22 +00003281 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendling856ff412009-12-22 00:12:37 +00003282
Bill Wendling187361b2010-01-23 10:26:57 +00003283 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3284 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3285 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3286 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3287 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3288 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3289 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3290 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3291 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3292 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3293 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3294 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3295 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3296 DAG.AssignOrdering(TwoToFracPartOfX.getNode(), SDNodeOrder);
3297 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003298 }
3299 } else {
3300 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003301 result = DAG.getNode(ISD::FEXP, dl,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003302 getValue(I.getOperand(1)).getValueType(),
3303 getValue(I.getOperand(1)));
Bill Wendling187361b2010-01-23 10:26:57 +00003304 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003305 }
3306
Dale Johannesen59e577f2008-09-05 18:38:42 +00003307 setValue(&I, result);
3308}
3309
Bill Wendling39150252008-09-09 20:39:27 +00003310/// visitLog - Lower a log intrinsic. Handles the special sequences for
3311/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003312void
Dan Gohman2048b852009-11-23 18:04:58 +00003313SelectionDAGBuilder::visitLog(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003314 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003315 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003316
Owen Anderson825b72b2009-08-11 20:47:22 +00003317 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003318 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3319 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003320 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003321
Bill Wendling187361b2010-01-23 10:26:57 +00003322 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003323
Bill Wendling39150252008-09-09 20:39:27 +00003324 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling856ff412009-12-22 00:12:37 +00003325 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003326 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003327 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003328
Bill Wendling187361b2010-01-23 10:26:57 +00003329 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003330
Bill Wendling39150252008-09-09 20:39:27 +00003331 // Get the significand and build it into a floating-point number with
3332 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003333 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003334
3335 if (LimitFloatPrecision <= 6) {
3336 // For floating-point precision of 6:
3337 //
3338 // LogofMantissa =
3339 // -1.1609546f +
3340 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003341 //
Bill Wendling39150252008-09-09 20:39:27 +00003342 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003343 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003344 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003345 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003346 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003347 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3348 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003349 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003350
Scott Michelfdc40a02009-02-17 22:15:04 +00003351 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003352 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003353
Bill Wendling187361b2010-01-23 10:26:57 +00003354 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3355 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3356 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3357 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3358 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003359 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3360 // For floating-point precision of 12:
3361 //
3362 // LogOfMantissa =
3363 // -1.7417939f +
3364 // (2.8212026f +
3365 // (-1.4699568f +
3366 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3367 //
3368 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003369 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003370 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003371 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003372 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003373 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3374 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003375 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3377 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003378 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003379 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3380 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003381 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003382
Scott Michelfdc40a02009-02-17 22:15:04 +00003383 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003384 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003385
Bill Wendling187361b2010-01-23 10:26:57 +00003386 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3387 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3388 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3389 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3390 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3391 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3392 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3393 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3394 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003395 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3396 // For floating-point precision of 18:
3397 //
3398 // LogOfMantissa =
3399 // -2.1072184f +
3400 // (4.2372794f +
3401 // (-3.7029485f +
3402 // (2.2781945f +
3403 // (-0.87823314f +
3404 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3405 //
3406 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003407 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003408 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003409 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003410 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003411 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3412 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003413 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003414 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3415 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003416 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003417 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3418 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003419 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003420 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3421 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003422 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003423 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3424 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003425 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003426
Scott Michelfdc40a02009-02-17 22:15:04 +00003427 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003428 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003429
Bill Wendling187361b2010-01-23 10:26:57 +00003430 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3431 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3432 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3433 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3434 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3435 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3436 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3437 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3438 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3439 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3440 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3441 DAG.AssignOrdering(LogOfMantissa.getNode(), SDNodeOrder);
3442 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003443 }
3444 } else {
3445 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003446 result = DAG.getNode(ISD::FLOG, dl,
Bill Wendling39150252008-09-09 20:39:27 +00003447 getValue(I.getOperand(1)).getValueType(),
3448 getValue(I.getOperand(1)));
Bill Wendling187361b2010-01-23 10:26:57 +00003449 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling39150252008-09-09 20:39:27 +00003450 }
3451
Dale Johannesen59e577f2008-09-05 18:38:42 +00003452 setValue(&I, result);
3453}
3454
Bill Wendling3eb59402008-09-09 00:28:24 +00003455/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3456/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003457void
Dan Gohman2048b852009-11-23 18:04:58 +00003458SelectionDAGBuilder::visitLog2(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003459 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003460 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003461
Owen Anderson825b72b2009-08-11 20:47:22 +00003462 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003463 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3464 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003465 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003466
Bill Wendling187361b2010-01-23 10:26:57 +00003467 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003468
Bill Wendling39150252008-09-09 20:39:27 +00003469 // Get the exponent.
Bill Wendling856ff412009-12-22 00:12:37 +00003470 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
3471
Bill Wendling187361b2010-01-23 10:26:57 +00003472 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003473
3474 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003475 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003476 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003477
Bill Wendling3eb59402008-09-09 00:28:24 +00003478 // Different possible minimax approximations of significand in
3479 // floating-point for various degrees of accuracy over [1,2].
3480 if (LimitFloatPrecision <= 6) {
3481 // For floating-point precision of 6:
3482 //
3483 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3484 //
3485 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003486 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003487 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003488 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003489 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003490 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3491 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003492 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003493
Scott Michelfdc40a02009-02-17 22:15:04 +00003494 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003495 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003496
Bill Wendling187361b2010-01-23 10:26:57 +00003497 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3498 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3499 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3500 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3501 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003502 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3503 // For floating-point precision of 12:
3504 //
3505 // Log2ofMantissa =
3506 // -2.51285454f +
3507 // (4.07009056f +
3508 // (-2.12067489f +
3509 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003510 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003511 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003512 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003513 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003514 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003515 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003516 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3517 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003518 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003519 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3520 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003521 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003522 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3523 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003524 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003525
Scott Michelfdc40a02009-02-17 22:15:04 +00003526 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003527 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003528
Bill Wendling187361b2010-01-23 10:26:57 +00003529 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3530 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3531 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3532 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3533 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3534 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3535 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3536 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3537 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003538 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3539 // For floating-point precision of 18:
3540 //
3541 // Log2ofMantissa =
3542 // -3.0400495f +
3543 // (6.1129976f +
3544 // (-5.3420409f +
3545 // (3.2865683f +
3546 // (-1.2669343f +
3547 // (0.27515199f -
3548 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3549 //
3550 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003551 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003552 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003553 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003554 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003555 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3556 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003557 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3559 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003560 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003561 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3562 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003563 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003564 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3565 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003566 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003567 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3568 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003569 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003570
Scott Michelfdc40a02009-02-17 22:15:04 +00003571 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003572 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003573
Bill Wendling187361b2010-01-23 10:26:57 +00003574 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3575 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3576 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3577 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3578 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3579 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3580 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3581 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3582 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3583 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3584 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3585 DAG.AssignOrdering(Log2ofMantissa.getNode(), SDNodeOrder);
3586 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003587 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003588 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003589 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003590 result = DAG.getNode(ISD::FLOG2, dl,
Dale Johannesen853244f2008-09-05 23:49:37 +00003591 getValue(I.getOperand(1)).getValueType(),
3592 getValue(I.getOperand(1)));
Bill Wendling187361b2010-01-23 10:26:57 +00003593 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen853244f2008-09-05 23:49:37 +00003594 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003595
Dale Johannesen59e577f2008-09-05 18:38:42 +00003596 setValue(&I, result);
3597}
3598
Bill Wendling3eb59402008-09-09 00:28:24 +00003599/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3600/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003601void
Dan Gohman2048b852009-11-23 18:04:58 +00003602SelectionDAGBuilder::visitLog10(CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003603 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003604 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003605
Owen Anderson825b72b2009-08-11 20:47:22 +00003606 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003607 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3608 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003609 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003610
Bill Wendling187361b2010-01-23 10:26:57 +00003611 DAG.AssignOrdering(Op1.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003612
Bill Wendling39150252008-09-09 20:39:27 +00003613 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling856ff412009-12-22 00:12:37 +00003614 SDValue Exp = GetExponent(DAG, Op1, TLI, dl, SDNodeOrder);
Owen Anderson825b72b2009-08-11 20:47:22 +00003615 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003616 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003617
Bill Wendling187361b2010-01-23 10:26:57 +00003618 DAG.AssignOrdering(LogOfExponent.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003619
Bill Wendling3eb59402008-09-09 00:28:24 +00003620 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003621 // exponent of 1.
Bill Wendling856ff412009-12-22 00:12:37 +00003622 SDValue X = GetSignificand(DAG, Op1, dl, SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003623
3624 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003625 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003626 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003627 // Log10ofMantissa =
3628 // -0.50419619f +
3629 // (0.60948995f - 0.10380950f * x) * x;
3630 //
3631 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003632 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003633 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003634 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003635 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003636 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3637 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003638 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003639
Scott Michelfdc40a02009-02-17 22:15:04 +00003640 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003641 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003642
Bill Wendling187361b2010-01-23 10:26:57 +00003643 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3644 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3645 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3646 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3647 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003648 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3649 // For floating-point precision of 12:
3650 //
3651 // Log10ofMantissa =
3652 // -0.64831180f +
3653 // (0.91751397f +
3654 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3655 //
3656 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003657 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003658 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003659 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003660 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003661 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3662 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003663 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003664 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3665 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003666 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003667
Scott Michelfdc40a02009-02-17 22:15:04 +00003668 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003669 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003670
Bill Wendling187361b2010-01-23 10:26:57 +00003671 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3672 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3673 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3674 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3675 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3676 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3677 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003678 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003679 // For floating-point precision of 18:
3680 //
3681 // Log10ofMantissa =
3682 // -0.84299375f +
3683 // (1.5327582f +
3684 // (-1.0688956f +
3685 // (0.49102474f +
3686 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3687 //
3688 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003689 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003690 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003691 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003692 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003693 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3694 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003695 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003696 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3697 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003698 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003699 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3700 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003701 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003702 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3703 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003704 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +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, Log10ofMantissa);
Bill Wendling856ff412009-12-22 00:12:37 +00003708
Bill Wendling187361b2010-01-23 10:26:57 +00003709 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3710 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3711 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3712 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3713 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3714 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3715 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3716 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3717 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3718 DAG.AssignOrdering(Log10ofMantissa.getNode(), SDNodeOrder);
3719 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendling3eb59402008-09-09 00:28:24 +00003720 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003721 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003722 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003723 result = DAG.getNode(ISD::FLOG10, dl,
Dale Johannesen852680a2008-09-05 21:27:19 +00003724 getValue(I.getOperand(1)).getValueType(),
3725 getValue(I.getOperand(1)));
Bill Wendling187361b2010-01-23 10:26:57 +00003726 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen852680a2008-09-05 21:27:19 +00003727 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003728
Dale Johannesen59e577f2008-09-05 18:38:42 +00003729 setValue(&I, result);
3730}
3731
Bill Wendlinge10c8142008-09-09 22:39:21 +00003732/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3733/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003734void
Dan Gohman2048b852009-11-23 18:04:58 +00003735SelectionDAGBuilder::visitExp2(CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003736 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003737 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003738
Owen Anderson825b72b2009-08-11 20:47:22 +00003739 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003740 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3741 SDValue Op = getValue(I.getOperand(1));
3742
Owen Anderson825b72b2009-08-11 20:47:22 +00003743 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003744
Bill Wendling187361b2010-01-23 10:26:57 +00003745 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003746
Bill Wendlinge10c8142008-09-09 22:39:21 +00003747 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003748 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3749 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003750
3751 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003752 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003753 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003754
Bill Wendling187361b2010-01-23 10:26:57 +00003755 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3756 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
3757 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003758
Bill Wendlinge10c8142008-09-09 22:39:21 +00003759 if (LimitFloatPrecision <= 6) {
3760 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003761 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003762 // TwoToFractionalPartOfX =
3763 // 0.997535578f +
3764 // (0.735607626f + 0.252464424f * x) * x;
3765 //
3766 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003767 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003768 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003769 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003770 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003771 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3772 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003773 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003774 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003775 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003776 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003777
Scott Michelfdc40a02009-02-17 22:15:04 +00003778 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003779 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003780
Bill Wendling187361b2010-01-23 10:26:57 +00003781 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3782 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3783 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3784 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3785 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3786 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3787 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003788 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3789 // For floating-point precision of 12:
3790 //
3791 // TwoToFractionalPartOfX =
3792 // 0.999892986f +
3793 // (0.696457318f +
3794 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3795 //
3796 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003797 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003798 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003799 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003800 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003801 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3802 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003803 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003804 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3805 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003806 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003807 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003808 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003809 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003810
Scott Michelfdc40a02009-02-17 22:15:04 +00003811 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003812 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003813
Bill Wendling187361b2010-01-23 10:26:57 +00003814 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3815 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3816 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3817 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3818 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3819 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3820 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3821 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3822 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003823 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3824 // For floating-point precision of 18:
3825 //
3826 // TwoToFractionalPartOfX =
3827 // 0.999999982f +
3828 // (0.693148872f +
3829 // (0.240227044f +
3830 // (0.554906021e-1f +
3831 // (0.961591928e-2f +
3832 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3833 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003834 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003835 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003836 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003837 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003838 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3839 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003840 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003841 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3842 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003843 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003844 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3845 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003846 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003847 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3848 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003849 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003850 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3851 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003852 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003853 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003854 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003855 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003856
Scott Michelfdc40a02009-02-17 22:15:04 +00003857 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003858 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003859
Bill Wendling187361b2010-01-23 10:26:57 +00003860 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3861 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3862 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3863 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3864 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3865 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3866 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3867 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
3868 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
3869 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
3870 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
3871 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
3872 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
3873 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3874 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003875 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003876 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003877 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003878 result = DAG.getNode(ISD::FEXP2, dl,
Dale Johannesen601d3c02008-09-05 01:48:15 +00003879 getValue(I.getOperand(1)).getValueType(),
3880 getValue(I.getOperand(1)));
Bill Wendling187361b2010-01-23 10:26:57 +00003881 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Dale Johannesen601d3c02008-09-05 01:48:15 +00003882 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003883
Dale Johannesen601d3c02008-09-05 01:48:15 +00003884 setValue(&I, result);
3885}
3886
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003887/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3888/// limited-precision mode with x == 10.0f.
3889void
Dan Gohman2048b852009-11-23 18:04:58 +00003890SelectionDAGBuilder::visitPow(CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003891 SDValue result;
3892 Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003893 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003894 bool IsExp10 = false;
3895
Owen Anderson825b72b2009-08-11 20:47:22 +00003896 if (getValue(Val).getValueType() == MVT::f32 &&
3897 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003898 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3899 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3900 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3901 APFloat Ten(10.0f);
3902 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3903 }
3904 }
3905 }
3906
3907 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3908 SDValue Op = getValue(I.getOperand(2));
3909
3910 // Put the exponent in the right bit position for later addition to the
3911 // final result:
3912 //
3913 // #define LOG2OF10 3.3219281f
3914 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003915 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003916 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003917 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003918
3919 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003920 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3921 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003922
Bill Wendling187361b2010-01-23 10:26:57 +00003923 DAG.AssignOrdering(t0.getNode(), SDNodeOrder);
3924 DAG.AssignOrdering(t1.getNode(), SDNodeOrder);
3925 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
3926 DAG.AssignOrdering(X.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003927
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003928 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003929 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003930 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003931
Bill Wendling187361b2010-01-23 10:26:57 +00003932 DAG.AssignOrdering(IntegerPartOfX.getNode(), SDNodeOrder);
Bill Wendling856ff412009-12-22 00:12:37 +00003933
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003934 if (LimitFloatPrecision <= 6) {
3935 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003936 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003937 // twoToFractionalPartOfX =
3938 // 0.997535578f +
3939 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003940 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003941 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003942 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003943 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003944 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003945 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003946 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3947 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003948 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003949 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003950 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003951 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003952
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003953 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003954 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003955
Bill Wendling187361b2010-01-23 10:26:57 +00003956 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3957 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3958 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3959 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3960 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3961 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3962 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003963 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3964 // For floating-point precision of 12:
3965 //
3966 // TwoToFractionalPartOfX =
3967 // 0.999892986f +
3968 // (0.696457318f +
3969 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3970 //
3971 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003972 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003973 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003974 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003975 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003976 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3977 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003978 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003979 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3980 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003981 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003982 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003983 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003984 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003985
Scott Michelfdc40a02009-02-17 22:15:04 +00003986 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003987 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00003988
Bill Wendling187361b2010-01-23 10:26:57 +00003989 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
3990 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
3991 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
3992 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
3993 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
3994 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
3995 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
3996 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
3997 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003998 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3999 // For floating-point precision of 18:
4000 //
4001 // TwoToFractionalPartOfX =
4002 // 0.999999982f +
4003 // (0.693148872f +
4004 // (0.240227044f +
4005 // (0.554906021e-1f +
4006 // (0.961591928e-2f +
4007 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
4008 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00004009 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004010 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00004011 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004012 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00004013 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
4014 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004015 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00004016 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
4017 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004018 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00004019 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
4020 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004021 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00004022 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
4023 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004024 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00004025 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
4026 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00004027 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00004028 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004029 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00004030 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004031
Scott Michelfdc40a02009-02-17 22:15:04 +00004032 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00004033 MVT::f32, TwoToFractionalPartOfX);
Bill Wendling856ff412009-12-22 00:12:37 +00004034
Bill Wendling187361b2010-01-23 10:26:57 +00004035 DAG.AssignOrdering(t2.getNode(), SDNodeOrder);
4036 DAG.AssignOrdering(t3.getNode(), SDNodeOrder);
4037 DAG.AssignOrdering(t4.getNode(), SDNodeOrder);
4038 DAG.AssignOrdering(t5.getNode(), SDNodeOrder);
4039 DAG.AssignOrdering(t6.getNode(), SDNodeOrder);
4040 DAG.AssignOrdering(t7.getNode(), SDNodeOrder);
4041 DAG.AssignOrdering(t8.getNode(), SDNodeOrder);
4042 DAG.AssignOrdering(t9.getNode(), SDNodeOrder);
4043 DAG.AssignOrdering(t10.getNode(), SDNodeOrder);
4044 DAG.AssignOrdering(t11.getNode(), SDNodeOrder);
4045 DAG.AssignOrdering(t12.getNode(), SDNodeOrder);
4046 DAG.AssignOrdering(t13.getNode(), SDNodeOrder);
4047 DAG.AssignOrdering(t14.getNode(), SDNodeOrder);
4048 DAG.AssignOrdering(TwoToFractionalPartOfX.getNode(), SDNodeOrder);
4049 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004050 }
4051 } else {
4052 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004053 result = DAG.getNode(ISD::FPOW, dl,
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004054 getValue(I.getOperand(1)).getValueType(),
4055 getValue(I.getOperand(1)),
4056 getValue(I.getOperand(2)));
Bill Wendling187361b2010-01-23 10:26:57 +00004057 DAG.AssignOrdering(result.getNode(), SDNodeOrder);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004058 }
4059
4060 setValue(&I, result);
4061}
4062
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004063
4064/// ExpandPowI - Expand a llvm.powi intrinsic.
4065static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
4066 SelectionDAG &DAG) {
4067 // If RHS is a constant, we can expand this out to a multiplication tree,
4068 // otherwise we end up lowering to a call to __powidf2 (for example). When
4069 // optimizing for size, we only want to do this if the expansion would produce
4070 // a small number of multiplies, otherwise we do the full expansion.
4071 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
4072 // Get the exponent as a positive value.
4073 unsigned Val = RHSC->getSExtValue();
4074 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004075
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004076 // powi(x, 0) -> 1.0
4077 if (Val == 0)
4078 return DAG.getConstantFP(1.0, LHS.getValueType());
4079
4080 Function *F = DAG.getMachineFunction().getFunction();
4081 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
4082 // If optimizing for size, don't insert too many multiplies. This
4083 // inserts up to 5 multiplies.
4084 CountPopulation_32(Val)+Log2_32(Val) < 7) {
4085 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004086 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004087 // powi(x,15) generates one more multiply than it should), but this has
4088 // the benefit of being both really simple and much better than a libcall.
4089 SDValue Res; // Logically starts equal to 1.0
4090 SDValue CurSquare = LHS;
4091 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004092 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004093 if (Res.getNode())
4094 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
4095 else
4096 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00004097 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004098
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004099 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
4100 CurSquare, CurSquare);
4101 Val >>= 1;
4102 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004103
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004104 // If the original was negative, invert the result, producing 1/(x*x*x).
4105 if (RHSC->getSExtValue() < 0)
4106 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
4107 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
4108 return Res;
4109 }
4110 }
4111
4112 // Otherwise, expand to a libcall.
4113 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
4114}
4115
4116
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004117/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
4118/// we want to emit this as a call to a named external function, return the name
4119/// otherwise lower it and return null.
4120const char *
Dan Gohman2048b852009-11-23 18:04:58 +00004121SelectionDAGBuilder::visitIntrinsicCall(CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004122 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004123 SDValue Res;
4124
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004125 switch (Intrinsic) {
4126 default:
4127 // By default, turn this into a target intrinsic node.
4128 visitTargetIntrinsic(I, Intrinsic);
4129 return 0;
4130 case Intrinsic::vastart: visitVAStart(I); return 0;
4131 case Intrinsic::vaend: visitVAEnd(I); return 0;
4132 case Intrinsic::vacopy: visitVACopy(I); return 0;
4133 case Intrinsic::returnaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004134 Res = DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
4135 getValue(I.getOperand(1)));
4136 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004137 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004138 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00004139 case Intrinsic::frameaddress:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004140 Res = DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
4141 getValue(I.getOperand(1)));
4142 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004143 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004144 return 0;
4145 case Intrinsic::setjmp:
4146 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004147 case Intrinsic::longjmp:
4148 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00004149 case Intrinsic::memcpy: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004150 SDValue Op1 = getValue(I.getOperand(1));
4151 SDValue Op2 = getValue(I.getOperand(2));
4152 SDValue Op3 = getValue(I.getOperand(3));
4153 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004154 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4155 I.getOperand(1), 0, I.getOperand(2), 0);
4156 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004157 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004158 return 0;
4159 }
Chris Lattner824b9582008-11-21 16:42:48 +00004160 case Intrinsic::memset: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004161 SDValue Op1 = getValue(I.getOperand(1));
4162 SDValue Op2 = getValue(I.getOperand(2));
4163 SDValue Op3 = getValue(I.getOperand(3));
4164 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004165 Res = DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align,
4166 I.getOperand(1), 0);
4167 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004168 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004169 return 0;
4170 }
Chris Lattner824b9582008-11-21 16:42:48 +00004171 case Intrinsic::memmove: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004172 SDValue Op1 = getValue(I.getOperand(1));
4173 SDValue Op2 = getValue(I.getOperand(2));
4174 SDValue Op3 = getValue(I.getOperand(3));
4175 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
4176
4177 // If the source and destination are known to not be aliases, we can
4178 // lower memmove as memcpy.
4179 uint64_t Size = -1ULL;
4180 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004181 Size = C->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004182 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
4183 AliasAnalysis::NoAlias) {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004184 Res = DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, false,
4185 I.getOperand(1), 0, I.getOperand(2), 0);
4186 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004187 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004188 return 0;
4189 }
4190
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004191 Res = DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align,
4192 I.getOperand(1), 0, I.getOperand(2), 0);
4193 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004194 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004195 return 0;
4196 }
Bill Wendling92c1e122009-02-13 02:16:35 +00004197 case Intrinsic::dbg_declare: {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004198 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00004199 // FIXME: Variable debug info is not supported here.
4200 return 0;
Devang Patel24f20e02009-08-22 17:12:53 +00004201 DwarfWriter *DW = DAG.getDwarfWriter();
4202 if (!DW)
4203 return 0;
Devang Patel7e1e31f2009-07-02 22:43:26 +00004204 DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00004205 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00004206 return 0;
4207
Devang Patelac1ceb32009-10-09 22:42:28 +00004208 MDNode *Variable = DI.getVariable();
Devang Patel24f20e02009-08-22 17:12:53 +00004209 Value *Address = DI.getAddress();
4210 if (BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
4211 Address = BCI->getOperand(0);
4212 AllocaInst *AI = dyn_cast<AllocaInst>(Address);
4213 // Don't handle byval struct arguments or VLAs, for example.
4214 if (!AI)
4215 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00004216 DenseMap<const AllocaInst*, int>::iterator SI =
4217 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004218 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00004219 return 0; // VLAs.
4220 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00004221
Chris Lattner3990b122009-12-28 23:41:32 +00004222 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo())
4223 if (MDNode *Dbg = DI.getMetadata("dbg"))
Chris Lattner0eb41982009-12-28 20:45:51 +00004224 MMI->setVariableDbgInfo(Variable, FI, Dbg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004225 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00004226 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004227 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004228 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00004229 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00004230 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004231 SDValue Ops[1];
4232 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004233 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004234 setValue(&I, Op);
4235 DAG.setRoot(Op.getValue(1));
Bill Wendling187361b2010-01-23 10:26:57 +00004236 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004237 return 0;
4238 }
4239
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004240 case Intrinsic::eh_selector: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004241 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004242
Chris Lattner3a5815f2009-09-17 23:54:54 +00004243 if (CurMBB->isLandingPad())
4244 AddCatchInfo(I, MMI, CurMBB);
4245 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004246#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00004247 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004248#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00004249 // FIXME: Mark exception selector register as live in. Hack for PR1508.
4250 unsigned Reg = TLI.getExceptionSelectorRegister();
4251 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004252 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004253
Chris Lattner3a5815f2009-09-17 23:54:54 +00004254 // Insert the EHSELECTION instruction.
4255 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
4256 SDValue Ops[2];
4257 Ops[0] = getValue(I.getOperand(1));
4258 Ops[1] = getRoot();
4259 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
4260
4261 DAG.setRoot(Op.getValue(1));
4262
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004263 Res = DAG.getSExtOrTrunc(Op, dl, MVT::i32);
4264 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004265 DAG.AssignOrdering(Op.getNode(), SDNodeOrder);
4266 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004267 return 0;
4268 }
4269
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00004270 case Intrinsic::eh_typeid_for: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004271 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004272
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004273 if (MMI) {
4274 // Find the type id for the given typeinfo.
4275 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004276 unsigned TypeID = MMI->getTypeIDFor(GV);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004277 Res = DAG.getConstant(TypeID, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004278 } else {
4279 // Return something different to eh_selector.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004280 Res = DAG.getConstant(1, MVT::i32);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004281 }
4282
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004283 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004284 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004285 return 0;
4286 }
4287
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004288 case Intrinsic::eh_return_i32:
4289 case Intrinsic::eh_return_i64:
4290 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004291 MMI->setCallsEHReturn(true);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004292 Res = DAG.getNode(ISD::EH_RETURN, dl,
4293 MVT::Other,
4294 getControlRoot(),
4295 getValue(I.getOperand(1)),
4296 getValue(I.getOperand(2)));
4297 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004298 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004299 } else {
4300 setValue(&I, DAG.getConstant(0, TLI.getPointerTy()));
4301 }
4302
4303 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004304 case Intrinsic::eh_unwind_init:
4305 if (MachineModuleInfo *MMI = DAG.getMachineModuleInfo()) {
4306 MMI->setCallsUnwindInit(true);
4307 }
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004308 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004309 case Intrinsic::eh_dwarf_cfa: {
Owen Andersone50ed302009-08-10 22:56:29 +00004310 EVT VT = getValue(I.getOperand(1)).getValueType();
Duncan Sands3a66a682009-10-13 21:04:12 +00004311 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
4312 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004313 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004314 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004315 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004316 TLI.getPointerTy()),
4317 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004318 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004319 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004320 DAG.getConstant(0, TLI.getPointerTy()));
4321 Res = DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
4322 FA, Offset);
4323 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004324 DAG.AssignOrdering(CfaArg.getNode(), SDNodeOrder);
4325 DAG.AssignOrdering(Offset.getNode(), SDNodeOrder);
4326 DAG.AssignOrdering(FA.getNode(), SDNodeOrder);
4327 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00004328 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004329 }
Mon P Wang77cdf302008-11-10 20:54:11 +00004330 case Intrinsic::convertff:
4331 case Intrinsic::convertfsi:
4332 case Intrinsic::convertfui:
4333 case Intrinsic::convertsif:
4334 case Intrinsic::convertuif:
4335 case Intrinsic::convertss:
4336 case Intrinsic::convertsu:
4337 case Intrinsic::convertus:
4338 case Intrinsic::convertuu: {
4339 ISD::CvtCode Code = ISD::CVT_INVALID;
4340 switch (Intrinsic) {
4341 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
4342 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
4343 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
4344 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
4345 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
4346 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
4347 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
4348 case Intrinsic::convertus: Code = ISD::CVT_US; break;
4349 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
4350 }
Owen Andersone50ed302009-08-10 22:56:29 +00004351 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004352 Value *Op1 = I.getOperand(1);
4353 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
4354 DAG.getValueType(DestVT),
4355 DAG.getValueType(getValue(Op1).getValueType()),
4356 getValue(I.getOperand(2)),
4357 getValue(I.getOperand(3)),
4358 Code);
4359 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004360 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Mon P Wang77cdf302008-11-10 20:54:11 +00004361 return 0;
4362 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004363 case Intrinsic::sqrt:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004364 Res = DAG.getNode(ISD::FSQRT, dl,
4365 getValue(I.getOperand(1)).getValueType(),
4366 getValue(I.getOperand(1)));
4367 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004368 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004369 return 0;
4370 case Intrinsic::powi:
Chris Lattnerf031e8a2010-01-01 03:32:16 +00004371 Res = ExpandPowI(dl, getValue(I.getOperand(1)), getValue(I.getOperand(2)),
4372 DAG);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004373 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004374 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004375 return 0;
4376 case Intrinsic::sin:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004377 Res = DAG.getNode(ISD::FSIN, dl,
4378 getValue(I.getOperand(1)).getValueType(),
4379 getValue(I.getOperand(1)));
4380 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004381 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004382 return 0;
4383 case Intrinsic::cos:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004384 Res = DAG.getNode(ISD::FCOS, dl,
4385 getValue(I.getOperand(1)).getValueType(),
4386 getValue(I.getOperand(1)));
4387 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004388 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004389 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004390 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004391 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004392 return 0;
4393 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004394 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004395 return 0;
4396 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004397 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004398 return 0;
4399 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00004400 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004401 return 0;
4402 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00004403 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00004404 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004405 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00004406 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004407 return 0;
4408 case Intrinsic::pcmarker: {
4409 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004410 Res = DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp);
4411 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004412 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004413 return 0;
4414 }
4415 case Intrinsic::readcyclecounter: {
4416 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004417 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
4418 DAG.getVTList(MVT::i64, MVT::Other),
4419 &Op, 1);
4420 setValue(&I, Res);
4421 DAG.setRoot(Res.getValue(1));
Bill Wendling187361b2010-01-23 10:26:57 +00004422 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004423 return 0;
4424 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004425 case Intrinsic::bswap:
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004426 Res = DAG.getNode(ISD::BSWAP, dl,
4427 getValue(I.getOperand(1)).getValueType(),
4428 getValue(I.getOperand(1)));
4429 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004430 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004431 return 0;
4432 case Intrinsic::cttz: {
4433 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004434 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004435 Res = DAG.getNode(ISD::CTTZ, dl, Ty, Arg);
4436 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004437 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004438 return 0;
4439 }
4440 case Intrinsic::ctlz: {
4441 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004442 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004443 Res = DAG.getNode(ISD::CTLZ, dl, Ty, Arg);
4444 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004445 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004446 return 0;
4447 }
4448 case Intrinsic::ctpop: {
4449 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004450 EVT Ty = Arg.getValueType();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004451 Res = DAG.getNode(ISD::CTPOP, dl, Ty, Arg);
4452 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004453 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004454 return 0;
4455 }
4456 case Intrinsic::stacksave: {
4457 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004458 Res = DAG.getNode(ISD::STACKSAVE, dl,
4459 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4460 setValue(&I, Res);
4461 DAG.setRoot(Res.getValue(1));
Bill Wendling187361b2010-01-23 10:26:57 +00004462 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004463 return 0;
4464 }
4465 case Intrinsic::stackrestore: {
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004466 Res = getValue(I.getOperand(1));
4467 Res = DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res);
4468 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004469 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004470 return 0;
4471 }
Bill Wendling57344502008-11-18 11:01:33 +00004472 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004473 // Emit code into the DAG to store the stack guard onto the stack.
4474 MachineFunction &MF = DAG.getMachineFunction();
4475 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004476 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004477
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004478 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4479 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004480
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004481 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004482 MFI->setStackProtectorIndex(FI);
4483
4484 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4485
4486 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004487 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4488 PseudoSourceValue::getFixedStack(FI),
4489 0, true);
4490 setValue(&I, Res);
4491 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004492 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004493 return 0;
4494 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004495 case Intrinsic::objectsize: {
4496 // If we don't know by now, we're never going to know.
4497 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
4498
4499 assert(CI && "Non-constant type in __builtin_object_size?");
4500
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004501 SDValue Arg = getValue(I.getOperand(0));
4502 EVT Ty = Arg.getValueType();
4503
Eric Christopherd060b252009-12-23 02:51:48 +00004504 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004505 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004506 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004507 Res = DAG.getConstant(0, Ty);
4508
4509 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004510 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004511 return 0;
4512 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004513 case Intrinsic::var_annotation:
4514 // Discard annotate attributes
4515 return 0;
4516
4517 case Intrinsic::init_trampoline: {
4518 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
4519
4520 SDValue Ops[6];
4521 Ops[0] = getRoot();
4522 Ops[1] = getValue(I.getOperand(1));
4523 Ops[2] = getValue(I.getOperand(2));
4524 Ops[3] = getValue(I.getOperand(3));
4525 Ops[4] = DAG.getSrcValue(I.getOperand(1));
4526 Ops[5] = DAG.getSrcValue(F);
4527
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004528 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4529 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4530 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004531
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004532 setValue(&I, Res);
4533 DAG.setRoot(Res.getValue(1));
Bill Wendling187361b2010-01-23 10:26:57 +00004534 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004535 return 0;
4536 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004537 case Intrinsic::gcroot:
4538 if (GFI) {
4539 Value *Alloca = I.getOperand(1);
4540 Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004541
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004542 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4543 GFI->addStackRoot(FI->getIndex(), TypeMap);
4544 }
4545 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004546 case Intrinsic::gcread:
4547 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004548 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004549 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004550 case Intrinsic::flt_rounds:
4551 Res = DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32);
4552 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004553 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004554 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004555 case Intrinsic::trap:
4556 Res = DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot());
4557 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004558 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004559 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004560 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004561 return implVisitAluOverflow(I, ISD::UADDO);
4562 case Intrinsic::sadd_with_overflow:
4563 return implVisitAluOverflow(I, ISD::SADDO);
4564 case Intrinsic::usub_with_overflow:
4565 return implVisitAluOverflow(I, ISD::USUBO);
4566 case Intrinsic::ssub_with_overflow:
4567 return implVisitAluOverflow(I, ISD::SSUBO);
4568 case Intrinsic::umul_with_overflow:
4569 return implVisitAluOverflow(I, ISD::UMULO);
4570 case Intrinsic::smul_with_overflow:
4571 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004572
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004573 case Intrinsic::prefetch: {
4574 SDValue Ops[4];
4575 Ops[0] = getRoot();
4576 Ops[1] = getValue(I.getOperand(1));
4577 Ops[2] = getValue(I.getOperand(2));
4578 Ops[3] = getValue(I.getOperand(3));
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004579 Res = DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4);
4580 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004581 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004582 return 0;
4583 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004584
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004585 case Intrinsic::memory_barrier: {
4586 SDValue Ops[6];
4587 Ops[0] = getRoot();
4588 for (int x = 1; x < 6; ++x)
4589 Ops[x] = getValue(I.getOperand(x));
4590
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004591 Res = DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6);
4592 DAG.setRoot(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004593 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004594 return 0;
4595 }
4596 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004597 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004598 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004599 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004600 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
4601 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004602 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004603 getValue(I.getOperand(2)),
4604 getValue(I.getOperand(3)),
4605 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004606 setValue(&I, L);
4607 DAG.setRoot(L.getValue(1));
Bill Wendling187361b2010-01-23 10:26:57 +00004608 DAG.AssignOrdering(L.getNode(), SDNodeOrder);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004609 return 0;
4610 }
4611 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004612 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004613 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004614 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004615 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004616 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004617 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004618 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004619 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004620 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004621 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004622 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004623 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004624 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004625 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004626 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004627 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004628 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004629 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004630 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004631 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004632 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004633
4634 case Intrinsic::invariant_start:
4635 case Intrinsic::lifetime_start:
4636 // Discard region information.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004637 Res = DAG.getUNDEF(TLI.getPointerTy());
4638 setValue(&I, Res);
Bill Wendling187361b2010-01-23 10:26:57 +00004639 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004640 return 0;
4641 case Intrinsic::invariant_end:
4642 case Intrinsic::lifetime_end:
4643 // Discard region information.
4644 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004645 }
4646}
4647
Dan Gohman98ca4f22009-08-05 01:29:28 +00004648/// Test if the given instruction is in a position to be optimized
4649/// with a tail-call. This roughly means that it's in a block with
4650/// a return and there's nothing that needs to be scheduled
4651/// between it and the return.
4652///
4653/// This function only tests target-independent requirements.
Dan Gohman98ca4f22009-08-05 01:29:28 +00004654static bool
Dan Gohman01205a82009-11-13 18:49:38 +00004655isInTailCallPosition(const Instruction *I, Attributes CalleeRetAttr,
Dan Gohman98ca4f22009-08-05 01:29:28 +00004656 const TargetLowering &TLI) {
4657 const BasicBlock *ExitBB = I->getParent();
4658 const TerminatorInst *Term = ExitBB->getTerminator();
4659 const ReturnInst *Ret = dyn_cast<ReturnInst>(Term);
4660 const Function *F = ExitBB->getParent();
4661
4662 // The block must end in a return statement or an unreachable.
4663 if (!Ret && !isa<UnreachableInst>(Term)) return false;
4664
4665 // If I will have a chain, make sure no other instruction that will have a
4666 // chain interposes between I and the return.
4667 if (I->mayHaveSideEffects() || I->mayReadFromMemory() ||
4668 !I->isSafeToSpeculativelyExecute())
4669 for (BasicBlock::const_iterator BBI = prior(prior(ExitBB->end())); ;
4670 --BBI) {
4671 if (&*BBI == I)
4672 break;
4673 if (BBI->mayHaveSideEffects() || BBI->mayReadFromMemory() ||
4674 !BBI->isSafeToSpeculativelyExecute())
4675 return false;
4676 }
4677
4678 // If the block ends with a void return or unreachable, it doesn't matter
4679 // what the call's return type is.
4680 if (!Ret || Ret->getNumOperands() == 0) return true;
4681
Dan Gohmaned9bab32009-11-14 02:06:30 +00004682 // If the return value is undef, it doesn't matter what the call's
4683 // return type is.
4684 if (isa<UndefValue>(Ret->getOperand(0))) return true;
4685
Dan Gohman98ca4f22009-08-05 01:29:28 +00004686 // Conservatively require the attributes of the call to match those of
Dan Gohman01205a82009-11-13 18:49:38 +00004687 // the return. Ignore noalias because it doesn't affect the call sequence.
4688 unsigned CallerRetAttr = F->getAttributes().getRetAttributes();
4689 if ((CalleeRetAttr ^ CallerRetAttr) & ~Attribute::NoAlias)
Dan Gohman98ca4f22009-08-05 01:29:28 +00004690 return false;
4691
4692 // Otherwise, make sure the unmodified return value of I is the return value.
4693 for (const Instruction *U = dyn_cast<Instruction>(Ret->getOperand(0)); ;
4694 U = dyn_cast<Instruction>(U->getOperand(0))) {
4695 if (!U)
4696 return false;
4697 if (!U->hasOneUse())
4698 return false;
4699 if (U == I)
4700 break;
4701 // Check for a truly no-op truncate.
4702 if (isa<TruncInst>(U) &&
4703 TLI.isTruncateFree(U->getOperand(0)->getType(), U->getType()))
4704 continue;
4705 // Check for a truly no-op bitcast.
4706 if (isa<BitCastInst>(U) &&
4707 (U->getOperand(0)->getType() == U->getType() ||
4708 (isa<PointerType>(U->getOperand(0)->getType()) &&
4709 isa<PointerType>(U->getType()))))
4710 continue;
4711 // Otherwise it's not a true no-op.
4712 return false;
4713 }
4714
4715 return true;
4716}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004717
Dan Gohman2048b852009-11-23 18:04:58 +00004718void SelectionDAGBuilder::LowerCallTo(CallSite CS, SDValue Callee,
4719 bool isTailCall,
4720 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004721 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4722 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004723 const Type *RetTy = FTy->getReturnType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004724 MachineModuleInfo *MMI = DAG.getMachineModuleInfo();
4725 unsigned BeginLabel = 0, EndLabel = 0;
4726
4727 TargetLowering::ArgListTy Args;
4728 TargetLowering::ArgListEntry Entry;
4729 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004730
4731 // Check whether the function can return without sret-demotion.
4732 SmallVector<EVT, 4> OutVTs;
4733 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4734 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004735 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004736 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004737
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004738 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004739 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4740
4741 SDValue DemoteStackSlot;
4742
4743 if (!CanLowerReturn) {
4744 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4745 FTy->getReturnType());
4746 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4747 FTy->getReturnType());
4748 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004749 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004750 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4751
4752 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4753 Entry.Node = DemoteStackSlot;
4754 Entry.Ty = StackSlotPtrType;
4755 Entry.isSExt = false;
4756 Entry.isZExt = false;
4757 Entry.isInReg = false;
4758 Entry.isSRet = true;
4759 Entry.isNest = false;
4760 Entry.isByVal = false;
4761 Entry.Alignment = Align;
4762 Args.push_back(Entry);
4763 RetTy = Type::getVoidTy(FTy->getContext());
4764 }
4765
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004766 for (CallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004767 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768 SDValue ArgNode = getValue(*i);
4769 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4770
4771 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004772 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4773 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4774 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4775 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4776 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4777 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004778 Entry.Alignment = CS.getParamAlignment(attrInd);
4779 Args.push_back(Entry);
4780 }
4781
4782 if (LandingPad && MMI) {
4783 // Insert a label before the invoke call to mark the try range. This can be
4784 // used to detect deletion of the invoke via the MachineModuleInfo.
4785 BeginLabel = MMI->NextLabelID();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004786
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004787 // Both PendingLoads and PendingExports must be flushed here;
4788 // this call might not return.
4789 (void)getRoot();
Bill Wendling0d580132009-12-23 01:28:19 +00004790 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4791 getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004792 }
4793
Dan Gohman98ca4f22009-08-05 01:29:28 +00004794 // Check if target-independent constraints permit a tail call here.
4795 // Target-dependent constraints are checked within TLI.LowerCallTo.
4796 if (isTailCall &&
4797 !isInTailCallPosition(CS.getInstruction(),
4798 CS.getAttributes().getRetAttributes(),
4799 TLI))
4800 isTailCall = false;
4801
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004802 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004803 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004804 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004805 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004806 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004807 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004808 isTailCall,
4809 !CS.getInstruction()->use_empty(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00004810 Callee, Args, DAG, getCurDebugLoc(), SDNodeOrder);
Dan Gohman98ca4f22009-08-05 01:29:28 +00004811 assert((isTailCall || Result.second.getNode()) &&
4812 "Non-null chain expected with non-tail call!");
4813 assert((Result.second.getNode() || !Result.first.getNode()) &&
4814 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004815 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004816 setValue(CS.getInstruction(), Result.first);
Bill Wendling187361b2010-01-23 10:26:57 +00004817 DAG.AssignOrdering(Result.first.getNode(), SDNodeOrder);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004818 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004819 // The instruction result is the result of loading from the
4820 // hidden sret parameter.
4821 SmallVector<EVT, 1> PVTs;
4822 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4823
4824 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4825 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4826 EVT PtrVT = PVTs[0];
4827 unsigned NumValues = OutVTs.size();
4828 SmallVector<SDValue, 4> Values(NumValues);
4829 SmallVector<SDValue, 4> Chains(NumValues);
4830
4831 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004832 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4833 DemoteStackSlot,
4834 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004835 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
Bill Wendlinge80ae832009-12-22 00:50:32 +00004836 Add, NULL, Offsets[i], false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004837 Values[i] = L;
4838 Chains[i] = L.getValue(1);
4839 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004840
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004841 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4842 MVT::Other, &Chains[0], NumValues);
4843 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004844
4845 // Collect the legal value parts into potentially illegal values
4846 // that correspond to the original function's return values.
4847 SmallVector<EVT, 4> RetTys;
4848 RetTy = FTy->getReturnType();
4849 ComputeValueVTs(TLI, RetTy, RetTys);
4850 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4851 SmallVector<SDValue, 4> ReturnValues;
4852 unsigned CurReg = 0;
4853 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4854 EVT VT = RetTys[I];
4855 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4856 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4857
4858 SDValue ReturnValue =
4859 getCopyFromParts(DAG, getCurDebugLoc(), SDNodeOrder, &Values[CurReg], NumRegs,
4860 RegisterVT, VT, AssertOp);
4861 ReturnValues.push_back(ReturnValue);
Bill Wendling187361b2010-01-23 10:26:57 +00004862 DAG.AssignOrdering(ReturnValue.getNode(), SDNodeOrder);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004863 CurReg += NumRegs;
4864 }
4865 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4866 DAG.getVTList(&RetTys[0], RetTys.size()),
4867 &ReturnValues[0], ReturnValues.size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004868
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004869 setValue(CS.getInstruction(), Res);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004870
Bill Wendling187361b2010-01-23 10:26:57 +00004871 DAG.AssignOrdering(Chain.getNode(), SDNodeOrder);
4872 DAG.AssignOrdering(Res.getNode(), SDNodeOrder);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004873 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004874
4875 // As a special case, a null chain means that a tail call has been emitted and
4876 // the DAG root is already updated.
4877 if (Result.second.getNode()) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00004878 DAG.setRoot(Result.second);
Bill Wendling187361b2010-01-23 10:26:57 +00004879 DAG.AssignOrdering(Result.second.getNode(), SDNodeOrder);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004880 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00004881 HasTailCall = true;
Bill Wendlinge80ae832009-12-22 00:50:32 +00004882 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004883
4884 if (LandingPad && MMI) {
4885 // Insert a label at the end of the invoke call to mark the try range. This
4886 // can be used to detect deletion of the invoke via the MachineModuleInfo.
4887 EndLabel = MMI->NextLabelID();
Bill Wendling0d580132009-12-23 01:28:19 +00004888 DAG.setRoot(DAG.getLabel(ISD::EH_LABEL, getCurDebugLoc(),
4889 getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004890
4891 // Inform MachineModuleInfo of range.
4892 MMI->addInvoke(LandingPad, BeginLabel, EndLabel);
4893 }
4894}
4895
Chris Lattner8047d9a2009-12-24 00:37:38 +00004896/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4897/// value is equal or not-equal to zero.
4898static bool IsOnlyUsedInZeroEqualityComparison(Value *V) {
4899 for (Value::use_iterator UI = V->use_begin(), E = V->use_end();
4900 UI != E; ++UI) {
4901 if (ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
4902 if (IC->isEquality())
4903 if (Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
4904 if (C->isNullValue())
4905 continue;
4906 // Unknown instruction.
4907 return false;
4908 }
4909 return true;
4910}
4911
Chris Lattner04b091a2009-12-24 01:07:17 +00004912static SDValue getMemCmpLoad(Value *PtrVal, MVT LoadVT, const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004913 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004914
Chris Lattner8047d9a2009-12-24 00:37:38 +00004915 // Check to see if this load can be trivially constant folded, e.g. if the
4916 // input is from a string literal.
4917 if (Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
4918 // Cast pointer to the type we really want to load.
4919 LoadInput = ConstantExpr::getBitCast(LoadInput,
4920 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004921
Chris Lattner8047d9a2009-12-24 00:37:38 +00004922 if (Constant *LoadCst = ConstantFoldLoadFromConstPtr(LoadInput, Builder.TD))
4923 return Builder.getValue(LoadCst);
4924 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004925
Chris Lattner8047d9a2009-12-24 00:37:38 +00004926 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4927 // still constant memory, the input chain can be the entry node.
4928 SDValue Root;
4929 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004930
Chris Lattner8047d9a2009-12-24 00:37:38 +00004931 // Do not serialize (non-volatile) loads of constant memory with anything.
4932 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4933 Root = Builder.DAG.getEntryNode();
4934 ConstantMemory = true;
4935 } else {
4936 // Do not serialize non-volatile loads against each other.
4937 Root = Builder.DAG.getRoot();
4938 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004939
Chris Lattner8047d9a2009-12-24 00:37:38 +00004940 SDValue Ptr = Builder.getValue(PtrVal);
4941 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4942 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
4943 false /*volatile*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004944
Chris Lattner8047d9a2009-12-24 00:37:38 +00004945 if (!ConstantMemory)
4946 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4947 return LoadVal;
4948}
4949
4950
4951/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4952/// If so, return true and lower it, otherwise return false and it will be
4953/// lowered like a normal call.
4954bool SelectionDAGBuilder::visitMemCmpCall(CallInst &I) {
4955 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4956 if (I.getNumOperands() != 4)
4957 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004958
Chris Lattner8047d9a2009-12-24 00:37:38 +00004959 Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
4960 if (!isa<PointerType>(LHS->getType()) || !isa<PointerType>(RHS->getType()) ||
4961 !isa<IntegerType>(I.getOperand(3)->getType()) ||
4962 !isa<IntegerType>(I.getType()))
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004963 return false;
4964
Chris Lattner8047d9a2009-12-24 00:37:38 +00004965 ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004966
Chris Lattner8047d9a2009-12-24 00:37:38 +00004967 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4968 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004969 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4970 bool ActuallyDoIt = true;
4971 MVT LoadVT;
4972 const Type *LoadTy;
4973 switch (Size->getZExtValue()) {
4974 default:
4975 LoadVT = MVT::Other;
4976 LoadTy = 0;
4977 ActuallyDoIt = false;
4978 break;
4979 case 2:
4980 LoadVT = MVT::i16;
4981 LoadTy = Type::getInt16Ty(Size->getContext());
4982 break;
4983 case 4:
4984 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004985 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004986 break;
4987 case 8:
4988 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004989 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004990 break;
4991 /*
4992 case 16:
4993 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004994 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004995 LoadTy = VectorType::get(LoadTy, 4);
4996 break;
4997 */
4998 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004999
Chris Lattner04b091a2009-12-24 01:07:17 +00005000 // This turns into unaligned loads. We only do this if the target natively
5001 // supports the MVT we'll be loading or if it is small enough (<= 4) that
5002 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005003
Chris Lattner04b091a2009-12-24 01:07:17 +00005004 // Require that we can find a legal MVT, and only do this if the target
5005 // supports unaligned loads of that type. Expanding into byte loads would
5006 // bloat the code.
5007 if (ActuallyDoIt && Size->getZExtValue() > 4) {
5008 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
5009 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
5010 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
5011 ActuallyDoIt = false;
5012 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005013
Chris Lattner04b091a2009-12-24 01:07:17 +00005014 if (ActuallyDoIt) {
5015 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
5016 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005017
Chris Lattner04b091a2009-12-24 01:07:17 +00005018 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
5019 ISD::SETNE);
5020 EVT CallVT = TLI.getValueType(I.getType(), true);
5021 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
5022 return true;
5023 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005024 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005025
5026
Chris Lattner8047d9a2009-12-24 00:37:38 +00005027 return false;
5028}
5029
5030
Dan Gohman2048b852009-11-23 18:04:58 +00005031void SelectionDAGBuilder::visitCall(CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005032 const char *RenameFn = 0;
5033 if (Function *F = I.getCalledFunction()) {
5034 if (F->isDeclaration()) {
Dale Johannesen49de9822009-02-05 01:49:45 +00005035 const TargetIntrinsicInfo *II = TLI.getTargetMachine().getIntrinsicInfo();
5036 if (II) {
5037 if (unsigned IID = II->getIntrinsicID(F)) {
5038 RenameFn = visitIntrinsicCall(I, IID);
5039 if (!RenameFn)
5040 return;
5041 }
5042 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005043 if (unsigned IID = F->getIntrinsicID()) {
5044 RenameFn = visitIntrinsicCall(I, IID);
5045 if (!RenameFn)
5046 return;
5047 }
5048 }
5049
5050 // Check for well-known libc/libm calls. If the function is internal, it
5051 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005052 if (!F->hasLocalLinkage() && F->hasName()) {
5053 StringRef Name = F->getName();
5054 if (Name == "copysign" || Name == "copysignf") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005055 if (I.getNumOperands() == 3 && // Basic sanity checks.
5056 I.getOperand(1)->getType()->isFloatingPoint() &&
5057 I.getType() == I.getOperand(1)->getType() &&
5058 I.getType() == I.getOperand(2)->getType()) {
5059 SDValue LHS = getValue(I.getOperand(1));
5060 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00005061 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
5062 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005063 return;
5064 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005065 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005066 if (I.getNumOperands() == 2 && // Basic sanity checks.
5067 I.getOperand(1)->getType()->isFloatingPoint() &&
5068 I.getType() == I.getOperand(1)->getType()) {
5069 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005070 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
5071 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005072 return;
5073 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005074 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005075 if (I.getNumOperands() == 2 && // Basic sanity checks.
5076 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005077 I.getType() == I.getOperand(1)->getType() &&
5078 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005079 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005080 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
5081 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005082 return;
5083 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00005084 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005085 if (I.getNumOperands() == 2 && // Basic sanity checks.
5086 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005087 I.getType() == I.getOperand(1)->getType() &&
5088 I.onlyReadsMemory()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005089 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005090 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
5091 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005092 return;
5093 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005094 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
5095 if (I.getNumOperands() == 2 && // Basic sanity checks.
5096 I.getOperand(1)->getType()->isFloatingPoint() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00005097 I.getType() == I.getOperand(1)->getType() &&
5098 I.onlyReadsMemory()) {
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005099 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00005100 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
5101 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00005102 return;
5103 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00005104 } else if (Name == "memcmp") {
5105 if (visitMemCmpCall(I))
5106 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005107 }
5108 }
5109 } else if (isa<InlineAsm>(I.getOperand(0))) {
5110 visitInlineAsm(&I);
5111 return;
5112 }
5113
5114 SDValue Callee;
5115 if (!RenameFn)
5116 Callee = getValue(I.getOperand(0));
5117 else
Bill Wendling056292f2008-09-16 21:48:12 +00005118 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005119
Bill Wendling0d580132009-12-23 01:28:19 +00005120 // Check if we can potentially perform a tail call. More detailed checking is
5121 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00005122 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005123}
5124
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005125/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005126/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005127/// Chain/Flag as the input and updates them for the output Chain/Flag.
5128/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005129SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005130 unsigned Order, SDValue &Chain,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005131 SDValue *Flag) const {
5132 // Assemble the legal parts into the final values.
5133 SmallVector<SDValue, 4> Values(ValueVTs.size());
5134 SmallVector<SDValue, 8> Parts;
5135 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
5136 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00005137 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005138 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005139 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005140
5141 Parts.resize(NumRegs);
5142 for (unsigned i = 0; i != NumRegs; ++i) {
5143 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00005144 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005145 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00005146 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005147 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005148 *Flag = P.getValue(2);
5149 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005150
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005151 Chain = P.getValue(1);
Bill Wendling187361b2010-01-23 10:26:57 +00005152 DAG.AssignOrdering(P.getNode(), Order);
Bill Wendlingec72e322009-12-22 01:11:43 +00005153
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005154 // If the source register was virtual and if we know something about it,
5155 // add an assert node.
5156 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
5157 RegisterVT.isInteger() && !RegisterVT.isVector()) {
5158 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
5159 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5160 if (FLI.LiveOutRegInfo.size() > SlotNo) {
5161 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005162
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005163 unsigned RegSize = RegisterVT.getSizeInBits();
5164 unsigned NumSignBits = LOI.NumSignBits;
5165 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005166
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005167 // FIXME: We capture more information than the dag can represent. For
5168 // now, just use the tightest assertzext/assertsext possible.
5169 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00005170 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005171 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00005172 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005173 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00005174 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005175 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005176 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00005177 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00005178 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005179 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005180 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00005181 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00005182 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005183 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005184 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00005185 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00005186 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005187
Owen Anderson825b72b2009-08-11 20:47:22 +00005188 if (FromVT != MVT::Other) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005189 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005190 RegisterVT, P, DAG.getValueType(FromVT));
Bill Wendling187361b2010-01-23 10:26:57 +00005191 DAG.AssignOrdering(P.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005192 }
5193 }
5194 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005195
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005196 Parts[i] = P;
5197 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005198
Bill Wendling3ea3c242009-12-22 02:10:19 +00005199 Values[Value] = getCopyFromParts(DAG, dl, Order, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00005200 NumRegs, RegisterVT, ValueVT);
Bill Wendling187361b2010-01-23 10:26:57 +00005201 DAG.AssignOrdering(Values[Value].getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005202 Part += NumRegs;
5203 Parts.clear();
5204 }
5205
Bill Wendlingec72e322009-12-22 01:11:43 +00005206 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5207 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
5208 &Values[0], ValueVTs.size());
Bill Wendling187361b2010-01-23 10:26:57 +00005209 DAG.AssignOrdering(Res.getNode(), Order);
Bill Wendlingec72e322009-12-22 01:11:43 +00005210 return Res;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005211}
5212
5213/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005214/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005215/// Chain/Flag as the input and updates them for the output Chain/Flag.
5216/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005217void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +00005218 unsigned Order, SDValue &Chain,
5219 SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 // Get the list of the values's legal parts.
5221 unsigned NumRegs = Regs.size();
5222 SmallVector<SDValue, 8> Parts(NumRegs);
5223 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005224 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005225 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00005226 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005227
Bill Wendling3ea3c242009-12-22 02:10:19 +00005228 getCopyToParts(DAG, dl, Order,
5229 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005230 &Parts[Part], NumParts, RegisterVT);
5231 Part += NumParts;
5232 }
5233
5234 // Copy the parts into the registers.
5235 SmallVector<SDValue, 8> Chains(NumRegs);
5236 for (unsigned i = 0; i != NumRegs; ++i) {
5237 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00005238 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005239 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00005240 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00005241 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005242 *Flag = Part.getValue(1);
5243 }
Bill Wendlingec72e322009-12-22 01:11:43 +00005244
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005245 Chains[i] = Part.getValue(0);
Bill Wendling187361b2010-01-23 10:26:57 +00005246 DAG.AssignOrdering(Part.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005247 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005248
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005249 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005250 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005251 // flagged to it. That is the CopyToReg nodes and the user are considered
5252 // a single scheduling unit. If we create a TokenFactor and return it as
5253 // chain, then the TokenFactor is both a predecessor (operand) of the
5254 // user as well as a successor (the TF operands are flagged to the user).
5255 // c1, f1 = CopyToReg
5256 // c2, f2 = CopyToReg
5257 // c3 = TokenFactor c1, c2
5258 // ...
5259 // = op c3, ..., f2
5260 Chain = Chains[NumRegs-1];
5261 else
Owen Anderson825b72b2009-08-11 20:47:22 +00005262 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Bill Wendlingec72e322009-12-22 01:11:43 +00005263
Bill Wendling187361b2010-01-23 10:26:57 +00005264 DAG.AssignOrdering(Chain.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005265}
5266
5267/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005268/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005269/// values added into it.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005270void RegsForValue::AddInlineAsmOperands(unsigned Code,
5271 bool HasMatching,unsigned MatchingIdx,
Bill Wendling651ad132009-12-22 01:25:10 +00005272 SelectionDAG &DAG, unsigned Order,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005273 std::vector<SDValue> &Ops) const {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005274 assert(Regs.size() < (1 << 13) && "Too many inline asm outputs!");
5275 unsigned Flag = Code | (Regs.size() << 3);
5276 if (HasMatching)
5277 Flag |= 0x80000000 | (MatchingIdx << 16);
Dale Johannesen99499332009-12-23 07:32:51 +00005278 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00005279 Ops.push_back(Res);
5280
Bill Wendling187361b2010-01-23 10:26:57 +00005281 DAG.AssignOrdering(Res.getNode(), Order);
Bill Wendling651ad132009-12-22 01:25:10 +00005282
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005283 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00005284 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00005285 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00005286 for (unsigned i = 0; i != NumRegs; ++i) {
5287 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling651ad132009-12-22 01:25:10 +00005288 SDValue Res = DAG.getRegister(Regs[Reg++], RegisterVT);
5289 Ops.push_back(Res);
Bill Wendling187361b2010-01-23 10:26:57 +00005290 DAG.AssignOrdering(Res.getNode(), Order);
Chris Lattner58f15c42008-10-17 16:21:11 +00005291 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005292 }
5293}
5294
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005295/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005296/// i.e. it isn't a stack pointer or some other special register, return the
5297/// register class for the register. Otherwise, return null.
5298static const TargetRegisterClass *
5299isAllocatableRegister(unsigned Reg, MachineFunction &MF,
5300 const TargetLowering &TLI,
5301 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005302 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005303 const TargetRegisterClass *FoundRC = 0;
5304 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
5305 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00005306 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005307
5308 const TargetRegisterClass *RC = *RCI;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005309 // If none of the the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005310 // can't use it. For example, 64-bit reg classes on 32-bit targets.
5311 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
5312 I != E; ++I) {
5313 if (TLI.isTypeLegal(*I)) {
5314 // If we have already found this register in a different register class,
5315 // choose the one with the largest VT specified. For example, on
5316 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00005317 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005318 ThisVT = *I;
5319 break;
5320 }
5321 }
5322 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005323
Owen Anderson825b72b2009-08-11 20:47:22 +00005324 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005325
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005326 // NOTE: This isn't ideal. In particular, this might allocate the
5327 // frame pointer in functions that need it (due to them not being taken
5328 // out of allocation, because a variable sized allocation hasn't been seen
5329 // yet). This is a slight code pessimization, but should still work.
5330 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
5331 E = RC->allocation_order_end(MF); I != E; ++I)
5332 if (*I == Reg) {
5333 // We found a matching register class. Keep looking at others in case
5334 // we find one with larger registers that this physreg is also in.
5335 FoundRC = RC;
5336 FoundVT = ThisVT;
5337 break;
5338 }
5339 }
5340 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005341}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005342
5343
5344namespace llvm {
5345/// AsmOperandInfo - This contains information for each constraint that we are
5346/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00005347class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00005348 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00005349public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005350 /// CallOperand - If this is the result output operand or a clobber
5351 /// this is null, otherwise it is the incoming operand to the CallInst.
5352 /// This gets modified as the asm is processed.
5353 SDValue CallOperand;
5354
5355 /// AssignedRegs - If this is a register or register class operand, this
5356 /// contains the set of register corresponding to the operand.
5357 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005358
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005359 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
5360 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
5361 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005362
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005363 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
5364 /// busy in OutputRegs/InputRegs.
5365 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005366 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005367 std::set<unsigned> &InputRegs,
5368 const TargetRegisterInfo &TRI) const {
5369 if (isOutReg) {
5370 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5371 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
5372 }
5373 if (isInReg) {
5374 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
5375 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
5376 }
5377 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005378
Owen Andersone50ed302009-08-10 22:56:29 +00005379 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00005380 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00005381 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005382 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00005383 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00005384 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00005385 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005386
Chris Lattner81249c92008-10-17 17:05:25 +00005387 if (isa<BasicBlock>(CallOperandVal))
5388 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005389
Chris Lattner81249c92008-10-17 17:05:25 +00005390 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005391
Chris Lattner81249c92008-10-17 17:05:25 +00005392 // If this is an indirect operand, the operand is a pointer to the
5393 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00005394 if (isIndirect) {
5395 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
5396 if (!PtrTy)
5397 llvm_report_error("Indirect operand for inline asm not a pointer!");
5398 OpTy = PtrTy->getElementType();
5399 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005400
Chris Lattner81249c92008-10-17 17:05:25 +00005401 // If OpTy is not a single value, it may be a struct/union that we
5402 // can tile with integers.
5403 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
5404 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
5405 switch (BitSize) {
5406 default: break;
5407 case 1:
5408 case 8:
5409 case 16:
5410 case 32:
5411 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00005412 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00005413 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00005414 break;
5415 }
5416 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005417
Chris Lattner81249c92008-10-17 17:05:25 +00005418 return TLI.getValueType(OpTy, true);
5419 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005420
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005421private:
5422 /// MarkRegAndAliases - Mark the specified register and all aliases in the
5423 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005424 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005425 const TargetRegisterInfo &TRI) {
5426 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
5427 Regs.insert(Reg);
5428 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
5429 for (; *Aliases; ++Aliases)
5430 Regs.insert(*Aliases);
5431 }
5432};
5433} // end llvm namespace.
5434
5435
5436/// GetRegistersForValue - Assign registers (virtual or physical) for the
5437/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00005438/// register allocator to handle the assignment process. However, if the asm
5439/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005440/// allocation. This produces generally horrible, but correct, code.
5441///
5442/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005443/// Input and OutputRegs are the set of already allocated physical registers.
5444///
Dan Gohman2048b852009-11-23 18:04:58 +00005445void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005446GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005447 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005448 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00005449 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00005450
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005451 // Compute whether this value requires an input register, an output register,
5452 // or both.
5453 bool isOutReg = false;
5454 bool isInReg = false;
5455 switch (OpInfo.Type) {
5456 case InlineAsm::isOutput:
5457 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005458
5459 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005460 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00005461 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005462 break;
5463 case InlineAsm::isInput:
5464 isInReg = true;
5465 isOutReg = false;
5466 break;
5467 case InlineAsm::isClobber:
5468 isOutReg = true;
5469 isInReg = true;
5470 break;
5471 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005472
5473
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005474 MachineFunction &MF = DAG.getMachineFunction();
5475 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005476
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005477 // If this is a constraint for a single physreg, or a constraint for a
5478 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005479 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005480 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
5481 OpInfo.ConstraintVT);
5482
5483 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00005484 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00005485 // If this is a FP input in an integer register (or visa versa) insert a bit
5486 // cast of the input value. More generally, handle any case where the input
5487 // value disagrees with the register class we plan to stick this in.
5488 if (OpInfo.Type == InlineAsm::isInput &&
5489 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00005490 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00005491 // types are identical size, use a bitcast to convert (e.g. two differing
5492 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00005493 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00005494 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005495 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005496 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005497 OpInfo.ConstraintVT = RegVT;
5498 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
5499 // If the input is a FP value and we want it in FP registers, do a
5500 // bitcast to the corresponding integer type. This turns an f64 value
5501 // into i64, which can be passed with two i32 values on a 32-bit
5502 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005503 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00005504 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005505 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005506 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00005507 OpInfo.ConstraintVT = RegVT;
5508 }
Bill Wendling651ad132009-12-22 01:25:10 +00005509
Bill Wendling187361b2010-01-23 10:26:57 +00005510 DAG.AssignOrdering(OpInfo.CallOperand.getNode(), SDNodeOrder);
Chris Lattner01426e12008-10-21 00:45:36 +00005511 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005512
Owen Anderson23b9b192009-08-12 00:36:31 +00005513 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00005514 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005515
Owen Andersone50ed302009-08-10 22:56:29 +00005516 EVT RegVT;
5517 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005518
5519 // If this is a constraint for a specific physical register, like {r17},
5520 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005521 if (unsigned AssignedReg = PhysReg.first) {
5522 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00005523 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005524 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005525
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005526 // Get the actual register value type. This is important, because the user
5527 // may have asked for (e.g.) the AX register in i32 type. We need to
5528 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005529 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005530
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005531 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005532 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005533
5534 // If this is an expanded reference, add the rest of the regs to Regs.
5535 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005536 TargetRegisterClass::iterator I = RC->begin();
5537 for (; *I != AssignedReg; ++I)
5538 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005539
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005540 // Already added the first reg.
5541 --NumRegs; ++I;
5542 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005543 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005544 Regs.push_back(*I);
5545 }
5546 }
Bill Wendling651ad132009-12-22 01:25:10 +00005547
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005548 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5549 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5550 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5551 return;
5552 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005553
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005554 // Otherwise, if this was a reference to an LLVM register class, create vregs
5555 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005556 if (const TargetRegisterClass *RC = PhysReg.second) {
5557 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005558 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005559 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005560
Evan Chengfb112882009-03-23 08:01:15 +00005561 // Create the appropriate number of virtual registers.
5562 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5563 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005564 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005565
Evan Chengfb112882009-03-23 08:01:15 +00005566 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5567 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005568 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005569
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005570 // This is a reference to a register class that doesn't directly correspond
5571 // to an LLVM register class. Allocate NumRegs consecutive, available,
5572 // registers from the class.
5573 std::vector<unsigned> RegClassRegs
5574 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5575 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005576
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005577 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5578 unsigned NumAllocated = 0;
5579 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5580 unsigned Reg = RegClassRegs[i];
5581 // See if this register is available.
5582 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5583 (isInReg && InputRegs.count(Reg))) { // Already used.
5584 // Make sure we find consecutive registers.
5585 NumAllocated = 0;
5586 continue;
5587 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005588
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005589 // Check to see if this register is allocatable (i.e. don't give out the
5590 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005591 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5592 if (!RC) { // Couldn't allocate this register.
5593 // Reset NumAllocated to make sure we return consecutive registers.
5594 NumAllocated = 0;
5595 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005596 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005597
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005598 // Okay, this register is good, we can use it.
5599 ++NumAllocated;
5600
5601 // If we allocated enough consecutive registers, succeed.
5602 if (NumAllocated == NumRegs) {
5603 unsigned RegStart = (i-NumAllocated)+1;
5604 unsigned RegEnd = i+1;
5605 // Mark all of the allocated registers used.
5606 for (unsigned i = RegStart; i != RegEnd; ++i)
5607 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005608
5609 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005610 OpInfo.ConstraintVT);
5611 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5612 return;
5613 }
5614 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005615
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005616 // Otherwise, we couldn't allocate enough registers for this.
5617}
5618
Evan Chengda43bcf2008-09-24 00:05:32 +00005619/// hasInlineAsmMemConstraint - Return true if the inline asm instruction being
5620/// processed uses a memory 'm' constraint.
5621static bool
5622hasInlineAsmMemConstraint(std::vector<InlineAsm::ConstraintInfo> &CInfos,
Dan Gohmane9530ec2009-01-15 16:58:17 +00005623 const TargetLowering &TLI) {
Evan Chengda43bcf2008-09-24 00:05:32 +00005624 for (unsigned i = 0, e = CInfos.size(); i != e; ++i) {
5625 InlineAsm::ConstraintInfo &CI = CInfos[i];
5626 for (unsigned j = 0, ee = CI.Codes.size(); j != ee; ++j) {
5627 TargetLowering::ConstraintType CType = TLI.getConstraintType(CI.Codes[j]);
5628 if (CType == TargetLowering::C_Memory)
5629 return true;
5630 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005631
Chris Lattner6c147292009-04-30 00:48:50 +00005632 // Indirect operand accesses access memory.
5633 if (CI.isIndirect)
5634 return true;
Evan Chengda43bcf2008-09-24 00:05:32 +00005635 }
5636
5637 return false;
5638}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005639
5640/// visitInlineAsm - Handle a call to an InlineAsm object.
5641///
Dan Gohman2048b852009-11-23 18:04:58 +00005642void SelectionDAGBuilder::visitInlineAsm(CallSite CS) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005643 InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
5644
5645 /// ConstraintOperands - Information about all of the constraints.
5646 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005647
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005648 std::set<unsigned> OutputRegs, InputRegs;
5649
5650 // Do a prepass over the constraints, canonicalizing them, and building up the
5651 // ConstraintOperands list.
5652 std::vector<InlineAsm::ConstraintInfo>
5653 ConstraintInfos = IA->ParseConstraints();
5654
Evan Chengda43bcf2008-09-24 00:05:32 +00005655 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005656
Chris Lattner6c147292009-04-30 00:48:50 +00005657 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005658
Chris Lattner6c147292009-04-30 00:48:50 +00005659 // We won't need to flush pending loads if this asm doesn't touch
5660 // memory and is nonvolatile.
5661 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005662 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005663 else
5664 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005665
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005666 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5667 unsigned ResNo = 0; // ResNo - The result number of the next output.
5668 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5669 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5670 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005671
Owen Anderson825b72b2009-08-11 20:47:22 +00005672 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005673
5674 // Compute the value type for each operand.
5675 switch (OpInfo.Type) {
5676 case InlineAsm::isOutput:
5677 // Indirect outputs just consume an argument.
5678 if (OpInfo.isIndirect) {
5679 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5680 break;
5681 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005682
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005683 // The return value of the call is this value. As such, there is no
5684 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005685 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005686 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005687 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5688 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5689 } else {
5690 assert(ResNo == 0 && "Asm only has one result!");
5691 OpVT = TLI.getValueType(CS.getType());
5692 }
5693 ++ResNo;
5694 break;
5695 case InlineAsm::isInput:
5696 OpInfo.CallOperandVal = CS.getArgument(ArgNo++);
5697 break;
5698 case InlineAsm::isClobber:
5699 // Nothing to do.
5700 break;
5701 }
5702
5703 // If this is an input or an indirect output, process the call argument.
5704 // BasicBlocks are labels, currently appearing only in asm's.
5705 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005706 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005707 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5708
Chris Lattner81249c92008-10-17 17:05:25 +00005709 if (BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005710 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005711 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005712 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005713 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005714
Owen Anderson1d0be152009-08-13 21:58:54 +00005715 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005716 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005717
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005718 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005719 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005720
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005721 // Second pass over the constraints: compute which constraint option to use
5722 // and assign registers to constraints that want a specific physreg.
5723 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5724 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005725
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005726 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005727 // matching input. If their types mismatch, e.g. one is an integer, the
5728 // other is floating point, or their sizes are different, flag it as an
5729 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005730 if (OpInfo.hasMatchingInput()) {
5731 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
5732 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005733 if ((OpInfo.ConstraintVT.isInteger() !=
5734 Input.ConstraintVT.isInteger()) ||
5735 (OpInfo.ConstraintVT.getSizeInBits() !=
5736 Input.ConstraintVT.getSizeInBits())) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005737 llvm_report_error("Unsupported asm: input constraint"
Torok Edwin7d696d82009-07-11 13:10:19 +00005738 " with a matching output constraint of incompatible"
5739 " type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005740 }
5741 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005742 }
5743 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005744
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005745 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005746 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005747
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005748 // If this is a memory input, and if the operand is not indirect, do what we
5749 // need to to provide an address for the memory input.
5750 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5751 !OpInfo.isIndirect) {
5752 assert(OpInfo.Type == InlineAsm::isInput &&
5753 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005754
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005755 // Memory operands really want the address of the value. If we don't have
5756 // an indirect input, put it in the constpool if we can, otherwise spill
5757 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005758
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005759 // If the operand is a float, integer, or vector constant, spill to a
5760 // constant pool entry to get its address.
5761 Value *OpVal = OpInfo.CallOperandVal;
5762 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5763 isa<ConstantVector>(OpVal)) {
5764 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5765 TLI.getPointerTy());
5766 } else {
5767 // Otherwise, create a stack slot and emit a store to it before the
5768 // asm.
5769 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005770 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005771 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5772 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005773 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005774 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005775 Chain = DAG.getStore(Chain, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005776 OpInfo.CallOperand, StackSlot, NULL, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005777 OpInfo.CallOperand = StackSlot;
5778 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005779
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005780 // There is no longer a Value* corresponding to this operand.
5781 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005782
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005783 // It is now an indirect operand.
5784 OpInfo.isIndirect = true;
5785 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005786
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005787 // If this constraint is for a specific register, allocate it before
5788 // anything else.
5789 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005790 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005791 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005792
Bill Wendling651ad132009-12-22 01:25:10 +00005793 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005794
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005795 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005796 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005797 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5798 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005799
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005800 // C_Register operands have already been allocated, Other/Memory don't need
5801 // to be.
5802 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005803 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005804 }
5805
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005806 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5807 std::vector<SDValue> AsmNodeOperands;
5808 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5809 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005810 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5811 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005812
5813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005814 // Loop over all of the inputs, copying the operand values into the
5815 // appropriate registers and processing the output regs.
5816 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005817
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005818 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5819 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005820
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005821 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5822 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5823
5824 switch (OpInfo.Type) {
5825 case InlineAsm::isOutput: {
5826 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5827 OpInfo.ConstraintType != TargetLowering::C_Register) {
5828 // Memory output, or 'other' output (e.g. 'X' constraint).
5829 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5830
5831 // Add information to the INLINEASM node to know about this output.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005832 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5833 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005834 TLI.getPointerTy()));
5835 AsmNodeOperands.push_back(OpInfo.CallOperand);
5836 break;
5837 }
5838
5839 // Otherwise, this is a register or register class output.
5840
5841 // Copy the output from the appropriate register. Find a register that
5842 // we can use.
5843 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005844 llvm_report_error("Couldn't allocate output reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005845 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005846 }
5847
5848 // If this is an indirect operand, store through the pointer after the
5849 // asm.
5850 if (OpInfo.isIndirect) {
5851 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5852 OpInfo.CallOperandVal));
5853 } else {
5854 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005855 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005856 // Concatenate this output onto the outputs list.
5857 RetValRegs.append(OpInfo.AssignedRegs);
5858 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005859
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005860 // Add information to the INLINEASM node to know that this register is
5861 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005862 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
5863 6 /* EARLYCLOBBER REGDEF */ :
5864 2 /* REGDEF */ ,
Evan Chengfb112882009-03-23 08:01:15 +00005865 false,
5866 0,
Bill Wendling651ad132009-12-22 01:25:10 +00005867 DAG, SDNodeOrder,
5868 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005869 break;
5870 }
5871 case InlineAsm::isInput: {
5872 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005873
Chris Lattner6bdcda32008-10-17 16:47:46 +00005874 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005875 // If this is required to match an output register we have already set,
5876 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005877 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005878
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005879 // Scan until we find the definition we already emitted of this operand.
5880 // When we find it, create a RegsForValue operand.
5881 unsigned CurOp = 2; // The first operand.
5882 for (; OperandNo; --OperandNo) {
5883 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005884 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005885 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005886 assert(((OpFlag & 7) == 2 /*REGDEF*/ ||
5887 (OpFlag & 7) == 6 /*EARLYCLOBBER REGDEF*/ ||
5888 (OpFlag & 7) == 4 /*MEM*/) &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005889 "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005890 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005891 }
5892
Evan Cheng697cbbf2009-03-20 18:03:34 +00005893 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005894 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005895 if ((OpFlag & 7) == 2 /*REGDEF*/
5896 || (OpFlag & 7) == 6 /* EARLYCLOBBER REGDEF */) {
5897 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Dan Gohman15480bd2009-06-15 22:32:41 +00005898 if (OpInfo.isIndirect) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005899 llvm_report_error("Don't know how to handle tied indirect "
Torok Edwin7d696d82009-07-11 13:10:19 +00005900 "register inputs yet!");
Dan Gohman15480bd2009-06-15 22:32:41 +00005901 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005902 RegsForValue MatchedRegs;
5903 MatchedRegs.TLI = &TLI;
5904 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005905 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005906 MatchedRegs.RegVTs.push_back(RegVT);
5907 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005908 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005909 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005910 MatchedRegs.Regs.push_back
5911 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005912
5913 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005914 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00005915 SDNodeOrder, Chain, &Flag);
Evan Chengfb112882009-03-23 08:01:15 +00005916 MatchedRegs.AddInlineAsmOperands(1 /*REGUSE*/,
5917 true, OpInfo.getMatchedOperand(),
Bill Wendling651ad132009-12-22 01:25:10 +00005918 DAG, SDNodeOrder, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005919 break;
5920 } else {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005921 assert(((OpFlag & 7) == 4) && "Unknown matching constraint!");
5922 assert((InlineAsm::getNumOperandRegisters(OpFlag)) == 1 &&
5923 "Unexpected number of operands");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005924 // Add information to the INLINEASM node to know about this input.
Evan Chengfb112882009-03-23 08:01:15 +00005925 // See InlineAsm.h isUseOperandTiedToDef.
5926 OpFlag |= 0x80000000 | (OpInfo.getMatchedOperand() << 16);
Evan Cheng697cbbf2009-03-20 18:03:34 +00005927 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005928 TLI.getPointerTy()));
5929 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5930 break;
5931 }
5932 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005933
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005934 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005935 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005936 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005937
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005938 std::vector<SDValue> Ops;
5939 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005940 hasMemory, Ops, DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005941 if (Ops.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005942 llvm_report_error("Invalid operand for inline asm"
Torok Edwin7d696d82009-07-11 13:10:19 +00005943 " constraint '" + OpInfo.ConstraintCode + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005944 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005945
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005946 // Add information to the INLINEASM node to know about this input.
5947 unsigned ResOpType = 3 /*IMM*/ | (Ops.size() << 3);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005948 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005949 TLI.getPointerTy()));
5950 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5951 break;
5952 } else if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
5953 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5954 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5955 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005956
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005957 // Add information to the INLINEASM node to know about this input.
Dale Johannesen86b49f82008-09-24 01:07:17 +00005958 unsigned ResOpType = 4/*MEM*/ | (1<<3);
5959 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005960 TLI.getPointerTy()));
5961 AsmNodeOperands.push_back(InOperandVal);
5962 break;
5963 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005965 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5966 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5967 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005968 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005969 "Don't know how to handle indirect register inputs yet!");
5970
5971 // Copy the input into the appropriate registers.
Evan Chengaa765b82008-09-25 00:14:04 +00005972 if (OpInfo.AssignedRegs.Regs.empty()) {
Benjamin Kramerd5fe92e2009-08-03 13:33:33 +00005973 llvm_report_error("Couldn't allocate input reg for"
Torok Edwin7d696d82009-07-11 13:10:19 +00005974 " constraint '"+ OpInfo.ConstraintCode +"'!");
Evan Chengaa765b82008-09-25 00:14:04 +00005975 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005976
Dale Johannesen66978ee2009-01-31 02:22:37 +00005977 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00005978 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005979
Evan Cheng697cbbf2009-03-20 18:03:34 +00005980 OpInfo.AssignedRegs.AddInlineAsmOperands(1/*REGUSE*/, false, 0,
Bill Wendling651ad132009-12-22 01:25:10 +00005981 DAG, SDNodeOrder,
5982 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983 break;
5984 }
5985 case InlineAsm::isClobber: {
5986 // Add the clobbered value to the operand list, so that the register
5987 // allocator is aware that the physreg got clobbered.
5988 if (!OpInfo.AssignedRegs.Regs.empty())
Dale Johannesen91aac102008-09-17 21:13:11 +00005989 OpInfo.AssignedRegs.AddInlineAsmOperands(6 /* EARLYCLOBBER REGDEF */,
Bill Wendling651ad132009-12-22 01:25:10 +00005990 false, 0, DAG, SDNodeOrder,
5991 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005992 break;
5993 }
5994 }
5995 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005996
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005997 // Finish up input operands.
5998 AsmNodeOperands[0] = Chain;
5999 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006000
Dale Johannesen66978ee2009-01-31 02:22:37 +00006001 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00006002 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006003 &AsmNodeOperands[0], AsmNodeOperands.size());
6004 Flag = Chain.getValue(1);
6005
6006 // If this asm returns a register value, copy the result from that register
6007 // and set it as the value of the call.
6008 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00006009 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006010 SDNodeOrder, Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006011
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006012 // FIXME: Why don't we do this for inline asms with MRVs?
6013 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00006014 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006015
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006016 // If any of the results of the inline asm is a vector, it may have the
6017 // wrong width/num elts. This can happen for register classes that can
6018 // contain multiple different value types. The preg or vreg allocated may
6019 // not have the same VT as was expected. Convert it to the right type
6020 // with bit_convert.
6021 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00006022 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00006023 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006024
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006025 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006026 ResultType.isInteger() && Val.getValueType().isInteger()) {
6027 // If a result value was tied to an input value, the computed result may
6028 // have a wider width than the expected result. Extract the relevant
6029 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00006030 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00006031 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006032
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006033 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00006034 }
Dan Gohman95915732008-10-18 01:03:45 +00006035
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006036 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00006037 // Don't need to use this as a chain in this case.
6038 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
6039 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006040 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006041
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006042 std::vector<std::pair<SDValue, Value*> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006043
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006044 // Process indirect outputs, first output all of the flagged copies out of
6045 // physregs.
6046 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
6047 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
6048 Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00006049 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendlingec72e322009-12-22 01:11:43 +00006050 SDNodeOrder, Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006051 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
Chris Lattner6c147292009-04-30 00:48:50 +00006052
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006053 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006054
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006055 // Emit the non-flagged stores from the physregs.
6056 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00006057 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
6058 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
6059 StoresToEmit[i].first,
6060 getValue(StoresToEmit[i].second),
6061 StoresToEmit[i].second, 0);
6062 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00006063 }
6064
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006065 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00006066 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006067 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00006068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006069 DAG.setRoot(Chain);
6070}
6071
Dan Gohman2048b852009-11-23 18:04:58 +00006072void SelectionDAGBuilder::visitVAStart(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006073 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
6074 MVT::Other, getRoot(),
6075 getValue(I.getOperand(1)),
6076 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006077}
6078
Dan Gohman2048b852009-11-23 18:04:58 +00006079void SelectionDAGBuilder::visitVAArg(VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00006080 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
6081 getRoot(), getValue(I.getOperand(0)),
6082 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006083 setValue(&I, V);
6084 DAG.setRoot(V.getValue(1));
6085}
6086
Dan Gohman2048b852009-11-23 18:04:58 +00006087void SelectionDAGBuilder::visitVAEnd(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006088 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
6089 MVT::Other, getRoot(),
6090 getValue(I.getOperand(1)),
6091 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006092}
6093
Dan Gohman2048b852009-11-23 18:04:58 +00006094void SelectionDAGBuilder::visitVACopy(CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00006095 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
6096 MVT::Other, getRoot(),
6097 getValue(I.getOperand(1)),
6098 getValue(I.getOperand(2)),
6099 DAG.getSrcValue(I.getOperand(1)),
6100 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006101}
6102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006103/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00006104/// implementation, which just calls LowerCall.
6105/// FIXME: When all targets are
6106/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006107std::pair<SDValue, SDValue>
6108TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
6109 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00006110 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00006111 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00006112 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006113 SDValue Callee,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006114 ArgListTy &Args, SelectionDAG &DAG, DebugLoc dl,
6115 unsigned Order) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006116 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006117 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006118 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00006119 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006120 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
6121 for (unsigned Value = 0, NumValues = ValueVTs.size();
6122 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006123 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006124 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00006125 SDValue Op = SDValue(Args[i].Node.getNode(),
6126 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006127 ISD::ArgFlagsTy Flags;
6128 unsigned OriginalAlignment =
6129 getTargetData()->getABITypeAlignment(ArgTy);
6130
6131 if (Args[i].isZExt)
6132 Flags.setZExt();
6133 if (Args[i].isSExt)
6134 Flags.setSExt();
6135 if (Args[i].isInReg)
6136 Flags.setInReg();
6137 if (Args[i].isSRet)
6138 Flags.setSRet();
6139 if (Args[i].isByVal) {
6140 Flags.setByVal();
6141 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
6142 const Type *ElementTy = Ty->getElementType();
6143 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00006144 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006145 // For ByVal, alignment should come from FE. BE will guess if this
6146 // info is not there but there are cases it cannot get right.
6147 if (Args[i].Alignment)
6148 FrameAlign = Args[i].Alignment;
6149 Flags.setByValAlign(FrameAlign);
6150 Flags.setByValSize(FrameSize);
6151 }
6152 if (Args[i].isNest)
6153 Flags.setNest();
6154 Flags.setOrigAlign(OriginalAlignment);
6155
Owen Anderson23b9b192009-08-12 00:36:31 +00006156 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
6157 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006158 SmallVector<SDValue, 4> Parts(NumParts);
6159 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
6160
6161 if (Args[i].isSExt)
6162 ExtendKind = ISD::SIGN_EXTEND;
6163 else if (Args[i].isZExt)
6164 ExtendKind = ISD::ZERO_EXTEND;
6165
Bill Wendling3ea3c242009-12-22 02:10:19 +00006166 getCopyToParts(DAG, dl, Order, Op, &Parts[0], NumParts,
6167 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006168
Dan Gohman98ca4f22009-08-05 01:29:28 +00006169 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006170 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00006171 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
6172 if (NumParts > 1 && j == 0)
6173 MyFlags.Flags.setSplit();
6174 else if (j != 0)
6175 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006176
Dan Gohman98ca4f22009-08-05 01:29:28 +00006177 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006178 }
6179 }
6180 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006181
Dan Gohman98ca4f22009-08-05 01:29:28 +00006182 // Handle the incoming return values from the call.
6183 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00006184 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006185 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006186 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006187 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006188 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6189 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006190 for (unsigned i = 0; i != NumRegs; ++i) {
6191 ISD::InputArg MyFlags;
6192 MyFlags.VT = RegisterVT;
6193 MyFlags.Used = isReturnValueUsed;
6194 if (RetSExt)
6195 MyFlags.Flags.setSExt();
6196 if (RetZExt)
6197 MyFlags.Flags.setZExt();
6198 if (isInreg)
6199 MyFlags.Flags.setInReg();
6200 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006201 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006202 }
6203
Dan Gohman98ca4f22009-08-05 01:29:28 +00006204 SmallVector<SDValue, 4> InVals;
6205 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
6206 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006207
6208 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006209 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006210 "LowerCall didn't return a valid chain!");
6211 assert((!isTailCall || InVals.empty()) &&
6212 "LowerCall emitted a return value for a tail call!");
6213 assert((isTailCall || InVals.size() == Ins.size()) &&
6214 "LowerCall didn't emit the correct number of values!");
6215 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6216 assert(InVals[i].getNode() &&
6217 "LowerCall emitted a null value!");
6218 assert(Ins[i].VT == InVals[i].getValueType() &&
6219 "LowerCall emitted a value with the wrong type!");
6220 });
Dan Gohman98ca4f22009-08-05 01:29:28 +00006221
Bill Wendling187361b2010-01-23 10:26:57 +00006222 DAG.AssignOrdering(Chain.getNode(), Order);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006223
Dan Gohman98ca4f22009-08-05 01:29:28 +00006224 // For a tail call, the return value is merely live-out and there aren't
6225 // any nodes in the DAG representing it. Return a special value to
6226 // indicate that a tail call has been emitted and no more Instructions
6227 // should be processed in the current block.
6228 if (isTailCall) {
6229 DAG.setRoot(Chain);
6230 return std::make_pair(SDValue(), SDValue());
6231 }
6232
6233 // Collect the legal value parts into potentially illegal values
6234 // that correspond to the original function's return values.
6235 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6236 if (RetSExt)
6237 AssertOp = ISD::AssertSext;
6238 else if (RetZExt)
6239 AssertOp = ISD::AssertZext;
6240 SmallVector<SDValue, 4> ReturnValues;
6241 unsigned CurReg = 0;
6242 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00006243 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00006244 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
6245 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006246
6247 SDValue ReturnValue =
Bill Wendling3ea3c242009-12-22 02:10:19 +00006248 getCopyFromParts(DAG, dl, Order, &InVals[CurReg], NumRegs,
6249 RegisterVT, VT, AssertOp);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006250 ReturnValues.push_back(ReturnValue);
Bill Wendling187361b2010-01-23 10:26:57 +00006251 DAG.AssignOrdering(ReturnValue.getNode(), Order);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006252 CurReg += NumRegs;
6253 }
6254
6255 // For a function returning void, there is no return value. We can't create
6256 // such a node, so we just return a null return value in that case. In
6257 // that case, nothing will actualy look at the value.
6258 if (ReturnValues.empty())
6259 return std::make_pair(SDValue(), Chain);
6260
6261 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
6262 DAG.getVTList(&RetTys[0], RetTys.size()),
6263 &ReturnValues[0], ReturnValues.size());
Bill Wendling187361b2010-01-23 10:26:57 +00006264 DAG.AssignOrdering(Res.getNode(), Order);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006265 return std::make_pair(Res, Chain);
6266}
6267
Duncan Sands9fbc7e22009-01-21 09:00:29 +00006268void TargetLowering::LowerOperationWrapper(SDNode *N,
6269 SmallVectorImpl<SDValue> &Results,
6270 SelectionDAG &DAG) {
6271 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00006272 if (Res.getNode())
6273 Results.push_back(Res);
6274}
6275
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006276SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006277 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006278 return SDValue();
6279}
6280
Dan Gohman2048b852009-11-23 18:04:58 +00006281void SelectionDAGBuilder::CopyValueToVirtualRegister(Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006282 SDValue Op = getValue(V);
6283 assert((Op.getOpcode() != ISD::CopyFromReg ||
6284 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
6285 "Copy from a reg to the same reg!");
6286 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
6287
Owen Anderson23b9b192009-08-12 00:36:31 +00006288 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006289 SDValue Chain = DAG.getEntryNode();
Bill Wendlingec72e322009-12-22 01:11:43 +00006290 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), SDNodeOrder, Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006291 PendingExports.push_back(Chain);
6292}
6293
6294#include "llvm/CodeGen/SelectionDAGISel.h"
6295
Dan Gohman8c2b5252009-10-30 01:27:03 +00006296void SelectionDAGISel::LowerArguments(BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006297 // If this is the entry block, emit arguments.
6298 Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00006299 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006300 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00006301 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006302 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006303 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006304
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006305 // Check whether the function can return without sret-demotion.
6306 SmallVector<EVT, 4> OutVTs;
6307 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006308 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006309 OutVTs, OutsFlags, TLI);
6310 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
6311
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006312 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00006313 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006314 if (!FLI.CanLowerReturn) {
6315 // Put in an sret pointer parameter before all the other parameters.
6316 SmallVector<EVT, 1> ValueVTs;
6317 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6318
6319 // NOTE: Assuming that a pointer will never break down to more than one VT
6320 // or one register.
6321 ISD::ArgFlagsTy Flags;
6322 Flags.setSRet();
6323 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
6324 ISD::InputArg RetArg(Flags, RegisterVT, true);
6325 Ins.push_back(RetArg);
6326 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00006327
Dan Gohman98ca4f22009-08-05 01:29:28 +00006328 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006329 unsigned Idx = 1;
6330 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end();
6331 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00006332 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006333 ComputeValueVTs(TLI, I->getType(), ValueVTs);
6334 bool isArgValueUsed = !I->use_empty();
6335 for (unsigned Value = 0, NumValues = ValueVTs.size();
6336 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006337 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00006338 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00006339 ISD::ArgFlagsTy Flags;
6340 unsigned OriginalAlignment =
6341 TD->getABITypeAlignment(ArgTy);
6342
6343 if (F.paramHasAttr(Idx, Attribute::ZExt))
6344 Flags.setZExt();
6345 if (F.paramHasAttr(Idx, Attribute::SExt))
6346 Flags.setSExt();
6347 if (F.paramHasAttr(Idx, Attribute::InReg))
6348 Flags.setInReg();
6349 if (F.paramHasAttr(Idx, Attribute::StructRet))
6350 Flags.setSRet();
6351 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
6352 Flags.setByVal();
6353 const PointerType *Ty = cast<PointerType>(I->getType());
6354 const Type *ElementTy = Ty->getElementType();
6355 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
6356 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
6357 // For ByVal, alignment should be passed from FE. BE will guess if
6358 // this info is not there but there are cases it cannot get right.
6359 if (F.getParamAlignment(Idx))
6360 FrameAlign = F.getParamAlignment(Idx);
6361 Flags.setByValAlign(FrameAlign);
6362 Flags.setByValSize(FrameSize);
6363 }
6364 if (F.paramHasAttr(Idx, Attribute::Nest))
6365 Flags.setNest();
6366 Flags.setOrigAlign(OriginalAlignment);
6367
Owen Anderson23b9b192009-08-12 00:36:31 +00006368 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6369 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006370 for (unsigned i = 0; i != NumRegs; ++i) {
6371 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
6372 if (NumRegs > 1 && i == 0)
6373 MyFlags.Flags.setSplit();
6374 // if it isn't first piece, alignment must be 1
6375 else if (i > 0)
6376 MyFlags.Flags.setOrigAlign(1);
6377 Ins.push_back(MyFlags);
6378 }
6379 }
6380 }
6381
6382 // Call the target to set up the argument values.
6383 SmallVector<SDValue, 8> InVals;
6384 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
6385 F.isVarArg(), Ins,
6386 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00006387
6388 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00006389 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00006390 "LowerFormalArguments didn't return a valid chain!");
6391 assert(InVals.size() == Ins.size() &&
6392 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00006393 DEBUG({
6394 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
6395 assert(InVals[i].getNode() &&
6396 "LowerFormalArguments emitted a null value!");
6397 assert(Ins[i].VT == InVals[i].getValueType() &&
6398 "LowerFormalArguments emitted a value with the wrong type!");
6399 }
6400 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00006401
Dan Gohman5e866062009-08-06 15:37:27 +00006402 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00006403 DAG.setRoot(NewRoot);
6404
6405 // Set up the argument values.
6406 unsigned i = 0;
6407 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006408 if (!FLI.CanLowerReturn) {
6409 // Create a virtual register for the sret pointer, and put in a copy
6410 // from the sret argument into it.
6411 SmallVector<EVT, 1> ValueVTs;
6412 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
6413 EVT VT = ValueVTs[0];
6414 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6415 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling3ea58b62009-12-22 21:35:02 +00006416 SDValue ArgValue = getCopyFromParts(DAG, dl, 0, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00006417 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006418
Dan Gohman2048b852009-11-23 18:04:58 +00006419 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006420 MachineRegisterInfo& RegInfo = MF.getRegInfo();
6421 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
6422 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00006423 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
6424 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006425 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00006426
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00006427 // i indexes lowered arguments. Bump it past the hidden sret argument.
6428 // Idx indexes LLVM arguments. Don't touch it.
6429 ++i;
6430 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006431
Dan Gohman98ca4f22009-08-05 01:29:28 +00006432 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
6433 ++I, ++Idx) {
6434 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00006435 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00006436 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006437 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00006438 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00006439 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00006440 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
6441 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00006442
6443 if (!I->use_empty()) {
6444 ISD::NodeType AssertOp = ISD::DELETED_NODE;
6445 if (F.paramHasAttr(Idx, Attribute::SExt))
6446 AssertOp = ISD::AssertSext;
6447 else if (F.paramHasAttr(Idx, Attribute::ZExt))
6448 AssertOp = ISD::AssertZext;
6449
Bill Wendling3ea58b62009-12-22 21:35:02 +00006450 ArgValues.push_back(getCopyFromParts(DAG, dl, 0, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00006451 NumParts, PartVT, VT,
6452 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00006453 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006454
Dan Gohman98ca4f22009-08-05 01:29:28 +00006455 i += NumParts;
6456 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006457
Dan Gohman98ca4f22009-08-05 01:29:28 +00006458 if (!I->use_empty()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +00006459 SDValue Res = DAG.getMergeValues(&ArgValues[0], NumValues,
6460 SDB->getCurDebugLoc());
6461 SDB->setValue(I, Res);
6462
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006463 // If this argument is live outside of the entry block, insert a copy from
6464 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00006465 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006466 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006467 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00006468
Dan Gohman98ca4f22009-08-05 01:29:28 +00006469 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006470
6471 // Finally, if the target has anything special to do, allow it to do so.
6472 // FIXME: this should insert code into the DAG!
Dan Gohman2048b852009-11-23 18:04:58 +00006473 EmitFunctionEntryCode(F, SDB->DAG.getMachineFunction());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006474}
6475
6476/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
6477/// ensure constants are generated when needed. Remember the virtual registers
6478/// that need to be added to the Machine PHI nodes as input. We cannot just
6479/// directly add them, because expansion might result in multiple MBB's for one
6480/// BB. As such, the start of the BB might correspond to a different MBB than
6481/// the end.
6482///
6483void
6484SelectionDAGISel::HandlePHINodesInSuccessorBlocks(BasicBlock *LLVMBB) {
6485 TerminatorInst *TI = LLVMBB->getTerminator();
6486
6487 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
6488
6489 // Check successor nodes' PHI nodes that expect a constant to be available
6490 // from this block.
6491 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6492 BasicBlock *SuccBB = TI->getSuccessor(succ);
6493 if (!isa<PHINode>(SuccBB->begin())) continue;
6494 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006495
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006496 // If this terminator has multiple identical successors (common for
6497 // switches), only handle each succ once.
6498 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006499
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006500 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6501 PHINode *PN;
6502
6503 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6504 // nodes and Machine PHI nodes, but the incoming operands have not been
6505 // emitted yet.
6506 for (BasicBlock::iterator I = SuccBB->begin();
6507 (PN = dyn_cast<PHINode>(I)); ++I) {
6508 // Ignore dead phi's.
6509 if (PN->use_empty()) continue;
6510
6511 unsigned Reg;
6512 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6513
6514 if (Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00006515 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006516 if (RegOut == 0) {
6517 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00006518 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006519 }
6520 Reg = RegOut;
6521 } else {
6522 Reg = FuncInfo->ValueMap[PHIOp];
6523 if (Reg == 0) {
6524 assert(isa<AllocaInst>(PHIOp) &&
6525 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
6526 "Didn't codegen value into a register!??");
6527 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00006528 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006529 }
6530 }
6531
6532 // Remember that this register needs to added to the machine PHI node as
6533 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00006534 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006535 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
6536 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00006537 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00006538 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006539 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00006540 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006541 Reg += NumRegisters;
6542 }
6543 }
6544 }
Dan Gohman2048b852009-11-23 18:04:58 +00006545 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006546}
6547
Dan Gohman3df24e62008-09-03 23:12:08 +00006548/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
6549/// supports legal types, and it emits MachineInstrs directly instead of
6550/// creating SelectionDAG nodes.
6551///
6552bool
6553SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(BasicBlock *LLVMBB,
6554 FastISel *F) {
6555 TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006556
Dan Gohman3df24e62008-09-03 23:12:08 +00006557 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006558 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006559
6560 // Check successor nodes' PHI nodes that expect a constant to be available
6561 // from this block.
6562 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
6563 BasicBlock *SuccBB = TI->getSuccessor(succ);
6564 if (!isa<PHINode>(SuccBB->begin())) continue;
6565 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006566
Dan Gohman3df24e62008-09-03 23:12:08 +00006567 // If this terminator has multiple identical successors (common for
6568 // switches), only handle each succ once.
6569 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006570
Dan Gohman3df24e62008-09-03 23:12:08 +00006571 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
6572 PHINode *PN;
6573
6574 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6575 // nodes and Machine PHI nodes, but the incoming operands have not been
6576 // emitted yet.
6577 for (BasicBlock::iterator I = SuccBB->begin();
6578 (PN = dyn_cast<PHINode>(I)); ++I) {
6579 // Ignore dead phi's.
6580 if (PN->use_empty()) continue;
6581
6582 // Only handle legal types. Two interesting things to note here. First,
6583 // by bailing out early, we may leave behind some dead instructions,
6584 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6585 // own moves. Second, this check is necessary becuase FastISel doesn't
6586 // use CreateRegForValue to create registers, so it always creates
6587 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006588 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006589 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6590 // Promote MVT::i1.
6591 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006592 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006593 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006594 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006595 return false;
6596 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006597 }
6598
6599 Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
6600
6601 unsigned Reg = F->getRegForValue(PHIOp);
6602 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006603 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006604 return false;
6605 }
Dan Gohman2048b852009-11-23 18:04:58 +00006606 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006607 }
6608 }
6609
6610 return true;
6611}