blob: 822c5edb54af2b78bd8cdf15ad22a77f1adccf89 [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"
Devang Patel00190342010-03-15 19:15:44 +000015#include "SDNodeDbgValue.h"
Dan Gohman2048b852009-11-23 18:04:58 +000016#include "SelectionDAGBuilder.h"
Dan Gohman6277eb22009-11-23 17:16:22 +000017#include "FunctionLoweringInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000018#include "llvm/ADT/BitVector.h"
Dan Gohman5b229802008-09-04 20:49:27 +000019#include "llvm/ADT/SmallSet.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000020#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner8047d9a2009-12-24 00:37:38 +000021#include "llvm/Analysis/ConstantFolding.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000022#include "llvm/Constants.h"
23#include "llvm/CallingConv.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Function.h"
26#include "llvm/GlobalVariable.h"
27#include "llvm/InlineAsm.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/IntrinsicInst.h"
Chris Lattner6129c372010-04-08 00:09:16 +000031#include "llvm/LLVMContext.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000032#include "llvm/Module.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000033#include "llvm/CodeGen/FastISel.h"
34#include "llvm/CodeGen/GCStrategy.h"
35#include "llvm/CodeGen/GCMetadata.h"
36#include "llvm/CodeGen/MachineFunction.h"
37#include "llvm/CodeGen/MachineFrameInfo.h"
38#include "llvm/CodeGen/MachineInstrBuilder.h"
39#include "llvm/CodeGen/MachineJumpTableInfo.h"
40#include "llvm/CodeGen/MachineModuleInfo.h"
41#include "llvm/CodeGen/MachineRegisterInfo.h"
Bill Wendlingb2a42982008-11-06 02:29:10 +000042#include "llvm/CodeGen/PseudoSourceValue.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000043#include "llvm/CodeGen/SelectionDAG.h"
Devang Patel83489bb2009-01-13 00:35:13 +000044#include "llvm/Analysis/DebugInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000045#include "llvm/Target/TargetRegisterInfo.h"
46#include "llvm/Target/TargetData.h"
47#include "llvm/Target/TargetFrameInfo.h"
48#include "llvm/Target/TargetInstrInfo.h"
Dale Johannesen49de9822009-02-05 01:49:45 +000049#include "llvm/Target/TargetIntrinsicInfo.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000050#include "llvm/Target/TargetLowering.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000051#include "llvm/Target/TargetOptions.h"
52#include "llvm/Support/Compiler.h"
Mikhail Glushenkov2388a582009-01-16 07:02:28 +000053#include "llvm/Support/CommandLine.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000054#include "llvm/Support/Debug.h"
Torok Edwin7d696d82009-07-11 13:10:19 +000055#include "llvm/Support/ErrorHandling.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000056#include "llvm/Support/MathExtras.h"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +000057#include "llvm/Support/raw_ostream.h"
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000058#include <algorithm>
59using namespace llvm;
60
Dale Johannesen601d3c02008-09-05 01:48:15 +000061/// LimitFloatPrecision - Generate low-precision inline sequences for
62/// some float libcalls (6, 8 or 12 bits).
63static unsigned LimitFloatPrecision;
64
65static cl::opt<unsigned, true>
66LimitFPPrecision("limit-float-precision",
67 cl::desc("Generate low-precision inline sequences "
68 "for some float libcalls"),
69 cl::location(LimitFloatPrecision),
70 cl::init(0));
71
Dan Gohmanf9bd4502009-11-23 17:46:23 +000072namespace {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000073 /// RegsForValue - This struct represents the registers (physical or virtual)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +000074 /// that a particular set of values is assigned, and the type information
75 /// about the value. The most common situation is to represent one value at a
76 /// time, but struct or array values are handled element-wise as multiple
77 /// values. The splitting of aggregates is performed recursively, so that we
78 /// never have aggregate-typed registers. The values at this point do not
79 /// necessarily have legal types, so each value may require one or more
80 /// registers of some legal type.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000081 ///
Dan Gohmanf9bd4502009-11-23 17:46:23 +000082 struct RegsForValue {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000083 /// TLI - The TargetLowering object.
84 ///
85 const TargetLowering *TLI;
86
87 /// ValueVTs - The value types of the values, which may not be legal, and
88 /// may need be promoted or synthesized from one or more registers.
89 ///
Owen Andersone50ed302009-08-10 22:56:29 +000090 SmallVector<EVT, 4> ValueVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +000091
Dan Gohmanf0cbcd42008-09-03 16:12:24 +000092 /// RegVTs - The value types of the registers. This is the same size as
93 /// ValueVTs and it records, for each value, what the type of the assigned
94 /// register or registers are. (Individual values are never synthesized
95 /// from more than one type of register.)
96 ///
97 /// With virtual registers, the contents of RegVTs is redundant with TLI's
98 /// getRegisterType member function, however when with physical registers
99 /// it is necessary to have a separate record of the types.
100 ///
Owen Andersone50ed302009-08-10 22:56:29 +0000101 SmallVector<EVT, 4> RegVTs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000102
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000103 /// Regs - This list holds the registers assigned to the values.
104 /// Each legal or promoted value requires one register, and each
105 /// expanded value requires multiple registers.
106 ///
107 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000108
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000109 RegsForValue() : TLI(0) {}
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000110
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000111 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000112 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000113 EVT regvt, EVT valuevt)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000114 : TLI(&tli), ValueVTs(1, valuevt), RegVTs(1, regvt), Regs(regs) {}
115 RegsForValue(const TargetLowering &tli,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000116 const SmallVector<unsigned, 4> &regs,
Owen Andersone50ed302009-08-10 22:56:29 +0000117 const SmallVector<EVT, 4> &regvts,
118 const SmallVector<EVT, 4> &valuevts)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000119 : TLI(&tli), ValueVTs(valuevts), RegVTs(regvts), Regs(regs) {}
Owen Anderson23b9b192009-08-12 00:36:31 +0000120 RegsForValue(LLVMContext &Context, const TargetLowering &tli,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000121 unsigned Reg, const Type *Ty) : TLI(&tli) {
122 ComputeValueVTs(tli, Ty, ValueVTs);
123
124 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +0000125 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +0000126 unsigned NumRegs = TLI->getNumRegisters(Context, ValueVT);
127 EVT RegisterVT = TLI->getRegisterType(Context, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000128 for (unsigned i = 0; i != NumRegs; ++i)
129 Regs.push_back(Reg + i);
130 RegVTs.push_back(RegisterVT);
131 Reg += NumRegs;
132 }
133 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000134
Evan Cheng8112b532010-02-10 01:21:02 +0000135 /// areValueTypesLegal - Return true if types of all the values are legal.
136 bool areValueTypesLegal() {
137 for (unsigned Value = 0, e = ValueVTs.size(); Value != e; ++Value) {
138 EVT RegisterVT = RegVTs[Value];
139 if (!TLI->isTypeLegal(RegisterVT))
140 return false;
141 }
142 return true;
143 }
144
145
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000146 /// append - Add the specified values to this one.
147 void append(const RegsForValue &RHS) {
148 TLI = RHS.TLI;
149 ValueVTs.append(RHS.ValueVTs.begin(), RHS.ValueVTs.end());
150 RegVTs.append(RHS.RegVTs.begin(), RHS.RegVTs.end());
151 Regs.append(RHS.Regs.begin(), RHS.Regs.end());
152 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000153
154
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000155 /// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000156 /// this value and returns the result as a ValueVTs value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000157 /// Chain/Flag as the input and updates them for the output Chain/Flag.
158 /// If the Flag pointer is NULL, no flag is used.
Bill Wendling46ada192010-03-02 01:55:18 +0000159 SDValue getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendlingec72e322009-12-22 01:11:43 +0000160 SDValue &Chain, SDValue *Flag) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000161
162 /// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000163 /// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000164 /// Chain/Flag as the input and updates them for the output Chain/Flag.
165 /// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +0000166 void getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +0000167 SDValue &Chain, SDValue *Flag) const;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000168
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000169 /// AddInlineAsmOperands - Add this value to the specified inlineasm node
Evan Cheng697cbbf2009-03-20 18:03:34 +0000170 /// operand list. This adds the code marker, matching input operand index
171 /// (if applicable), and includes the number of values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +0000172 void AddInlineAsmOperands(unsigned Kind,
Evan Cheng697cbbf2009-03-20 18:03:34 +0000173 bool HasMatching, unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +0000174 SelectionDAG &DAG,
Bill Wendling651ad132009-12-22 01:25:10 +0000175 std::vector<SDValue> &Ops) const;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000176 };
177}
178
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000179/// getCopyFromParts - Create a value that contains the specified legal parts
180/// combined into the value they represent. If the parts combine to a type
181/// larger then ValueVT then AssertOp can be used to specify whether the extra
182/// bits are known to be zero (ISD::AssertZext) or sign extended from ValueVT
183/// (ISD::AssertSext).
Bill Wendling46ada192010-03-02 01:55:18 +0000184static SDValue getCopyFromParts(SelectionDAG &DAG, DebugLoc dl,
Dale Johannesen66978ee2009-01-31 02:22:37 +0000185 const SDValue *Parts,
Owen Andersone50ed302009-08-10 22:56:29 +0000186 unsigned NumParts, EVT PartVT, EVT ValueVT,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000187 ISD::NodeType AssertOp = ISD::DELETED_NODE) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000188 assert(NumParts > 0 && "No parts to assemble!");
Dan Gohmane9530ec2009-01-15 16:58:17 +0000189 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000190 SDValue Val = Parts[0];
191
192 if (NumParts > 1) {
193 // Assemble the value from multiple parts.
Eli Friedman2ac8b322009-05-20 06:02:09 +0000194 if (!ValueVT.isVector() && ValueVT.isInteger()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000195 unsigned PartBits = PartVT.getSizeInBits();
196 unsigned ValueBits = ValueVT.getSizeInBits();
197
198 // Assemble the power of 2 part.
199 unsigned RoundParts = NumParts & (NumParts - 1) ?
200 1 << Log2_32(NumParts) : NumParts;
201 unsigned RoundBits = PartBits * RoundParts;
Owen Andersone50ed302009-08-10 22:56:29 +0000202 EVT RoundVT = RoundBits == ValueBits ?
Owen Anderson23b9b192009-08-12 00:36:31 +0000203 ValueVT : EVT::getIntegerVT(*DAG.getContext(), RoundBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000204 SDValue Lo, Hi;
205
Owen Anderson23b9b192009-08-12 00:36:31 +0000206 EVT HalfVT = EVT::getIntegerVT(*DAG.getContext(), RoundBits/2);
Duncan Sandsd22ec5f2008-10-29 14:22:20 +0000207
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000208 if (RoundParts > 2) {
Bill Wendling46ada192010-03-02 01:55:18 +0000209 Lo = getCopyFromParts(DAG, dl, Parts, RoundParts / 2,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000210 PartVT, HalfVT);
Bill Wendling46ada192010-03-02 01:55:18 +0000211 Hi = getCopyFromParts(DAG, dl, Parts + RoundParts / 2,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000212 RoundParts / 2, PartVT, HalfVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000213 } else {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000214 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[0]);
215 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, HalfVT, Parts[1]);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000216 }
Bill Wendling3ea3c242009-12-22 02:10:19 +0000217
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000218 if (TLI.isBigEndian())
219 std::swap(Lo, Hi);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000220
Dale Johannesen66978ee2009-01-31 02:22:37 +0000221 Val = DAG.getNode(ISD::BUILD_PAIR, dl, RoundVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000222
223 if (RoundParts < NumParts) {
224 // Assemble the trailing non-power-of-2 part.
225 unsigned OddParts = NumParts - RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000226 EVT OddVT = EVT::getIntegerVT(*DAG.getContext(), OddParts * PartBits);
Bill Wendling46ada192010-03-02 01:55:18 +0000227 Hi = getCopyFromParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000228 Parts + RoundParts, OddParts, PartVT, OddVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000229
230 // Combine the round and odd parts.
231 Lo = Val;
232 if (TLI.isBigEndian())
233 std::swap(Lo, Hi);
Owen Anderson23b9b192009-08-12 00:36:31 +0000234 EVT TotalVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000235 Hi = DAG.getNode(ISD::ANY_EXTEND, dl, TotalVT, Hi);
236 Hi = DAG.getNode(ISD::SHL, dl, TotalVT, Hi,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000237 DAG.getConstant(Lo.getValueType().getSizeInBits(),
Duncan Sands92abc622009-01-31 15:50:11 +0000238 TLI.getPointerTy()));
Dale Johannesen66978ee2009-01-31 02:22:37 +0000239 Lo = DAG.getNode(ISD::ZERO_EXTEND, dl, TotalVT, Lo);
240 Val = DAG.getNode(ISD::OR, dl, TotalVT, Lo, Hi);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000241 }
Eli Friedman2ac8b322009-05-20 06:02:09 +0000242 } else if (ValueVT.isVector()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000243 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000244 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000245 unsigned NumIntermediates;
246 unsigned NumRegs =
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000247 TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT, IntermediateVT,
Owen Anderson23b9b192009-08-12 00:36:31 +0000248 NumIntermediates, RegisterVT);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000249 assert(NumRegs == NumParts
250 && "Part count doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000251 NumParts = NumRegs; // Silence a compiler warning.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000252 assert(RegisterVT == PartVT
253 && "Part type doesn't match vector breakdown!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000254 assert(RegisterVT == Parts[0].getValueType() &&
255 "Part type doesn't match part!");
256
257 // Assemble the parts into intermediate operands.
258 SmallVector<SDValue, 8> Ops(NumIntermediates);
259 if (NumIntermediates == NumParts) {
260 // If the register was not expanded, truncate or copy the value,
261 // as appropriate.
262 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000263 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i], 1,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000264 PartVT, IntermediateVT);
265 } else if (NumParts > 0) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000266 // If the intermediate type was expanded, build the intermediate
267 // operands from the parts.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000268 assert(NumParts % NumIntermediates == 0 &&
269 "Must expand into a divisible number of parts!");
270 unsigned Factor = NumParts / NumIntermediates;
271 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000272 Ops[i] = getCopyFromParts(DAG, dl, &Parts[i * Factor], Factor,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000273 PartVT, IntermediateVT);
274 }
275
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000276 // Build a vector with BUILD_VECTOR or CONCAT_VECTORS from the
277 // intermediate operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000278 Val = DAG.getNode(IntermediateVT.isVector() ?
Dale Johannesen66978ee2009-01-31 02:22:37 +0000279 ISD::CONCAT_VECTORS : ISD::BUILD_VECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000280 ValueVT, &Ops[0], NumIntermediates);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000281 } else if (PartVT.isFloatingPoint()) {
282 // FP split into multiple FP parts (for ppcf128)
Owen Anderson825b72b2009-08-11 20:47:22 +0000283 assert(ValueVT == EVT(MVT::ppcf128) && PartVT == EVT(MVT::f64) &&
Eli Friedman2ac8b322009-05-20 06:02:09 +0000284 "Unexpected split");
285 SDValue Lo, Hi;
Owen Anderson825b72b2009-08-11 20:47:22 +0000286 Lo = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[0]);
287 Hi = DAG.getNode(ISD::BIT_CONVERT, dl, EVT(MVT::f64), Parts[1]);
Eli Friedman2ac8b322009-05-20 06:02:09 +0000288 if (TLI.isBigEndian())
289 std::swap(Lo, Hi);
290 Val = DAG.getNode(ISD::BUILD_PAIR, dl, ValueVT, Lo, Hi);
291 } else {
292 // FP split into integer parts (soft fp)
293 assert(ValueVT.isFloatingPoint() && PartVT.isInteger() &&
294 !PartVT.isVector() && "Unexpected split");
Owen Anderson23b9b192009-08-12 00:36:31 +0000295 EVT IntVT = EVT::getIntegerVT(*DAG.getContext(), ValueVT.getSizeInBits());
Bill Wendling46ada192010-03-02 01:55:18 +0000296 Val = getCopyFromParts(DAG, dl, Parts, NumParts, PartVT, IntVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000297 }
298 }
299
300 // There is now one part, held in Val. Correct it to match ValueVT.
301 PartVT = Val.getValueType();
302
303 if (PartVT == ValueVT)
304 return Val;
305
306 if (PartVT.isVector()) {
307 assert(ValueVT.isVector() && "Unknown vector conversion!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000308 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000309 }
310
311 if (ValueVT.isVector()) {
312 assert(ValueVT.getVectorElementType() == PartVT &&
313 ValueVT.getVectorNumElements() == 1 &&
314 "Only trivial scalar-to-vector conversions should get here!");
Bill Wendling4533cac2010-01-28 21:51:40 +0000315 return DAG.getNode(ISD::BUILD_VECTOR, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000316 }
317
318 if (PartVT.isInteger() &&
319 ValueVT.isInteger()) {
320 if (ValueVT.bitsLT(PartVT)) {
321 // For a truncate, see if we have any information to
322 // indicate whether the truncated bits will always be
323 // zero or sign-extension.
324 if (AssertOp != ISD::DELETED_NODE)
Dale Johannesen66978ee2009-01-31 02:22:37 +0000325 Val = DAG.getNode(AssertOp, dl, PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000326 DAG.getValueType(ValueVT));
Bill Wendling4533cac2010-01-28 21:51:40 +0000327 return DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000328 } else {
Bill Wendling4533cac2010-01-28 21:51:40 +0000329 return DAG.getNode(ISD::ANY_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000330 }
331 }
332
333 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
Bill Wendling3ea3c242009-12-22 02:10:19 +0000334 if (ValueVT.bitsLT(Val.getValueType())) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000335 // FP_ROUND's are always exact here.
Bill Wendling4533cac2010-01-28 21:51:40 +0000336 return DAG.getNode(ISD::FP_ROUND, dl, ValueVT, Val,
337 DAG.getIntPtrConstant(1));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000338 }
339
Bill Wendling4533cac2010-01-28 21:51:40 +0000340 return DAG.getNode(ISD::FP_EXTEND, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000341 }
342
Bill Wendling4533cac2010-01-28 21:51:40 +0000343 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits())
344 return DAG.getNode(ISD::BIT_CONVERT, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000345
Torok Edwinc23197a2009-07-14 16:55:14 +0000346 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000347 return SDValue();
348}
349
350/// getCopyToParts - Create a series of nodes that contain the specified value
351/// split into legal parts. If the parts contain more bits than Val, then, for
352/// integers, ExtendKind can be used to specify how to generate the extra bits.
Bill Wendling46ada192010-03-02 01:55:18 +0000353static void getCopyToParts(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000354 SDValue Val, SDValue *Parts, unsigned NumParts,
355 EVT PartVT,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000356 ISD::NodeType ExtendKind = ISD::ANY_EXTEND) {
Dan Gohmane9530ec2009-01-15 16:58:17 +0000357 const TargetLowering &TLI = DAG.getTargetLoweringInfo();
Owen Andersone50ed302009-08-10 22:56:29 +0000358 EVT PtrVT = TLI.getPointerTy();
359 EVT ValueVT = Val.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000360 unsigned PartBits = PartVT.getSizeInBits();
Dale Johannesen8a36f502009-02-25 22:39:13 +0000361 unsigned OrigNumParts = NumParts;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000362 assert(TLI.isTypeLegal(PartVT) && "Copying to an illegal type!");
363
364 if (!NumParts)
365 return;
366
367 if (!ValueVT.isVector()) {
368 if (PartVT == ValueVT) {
369 assert(NumParts == 1 && "No-op copy with multiple parts!");
370 Parts[0] = Val;
371 return;
372 }
373
374 if (NumParts * PartBits > ValueVT.getSizeInBits()) {
375 // If the parts cover more bits than the value has, promote the value.
376 if (PartVT.isFloatingPoint() && ValueVT.isFloatingPoint()) {
377 assert(NumParts == 1 && "Do not know what to promote to!");
Dale Johannesen66978ee2009-01-31 02:22:37 +0000378 Val = DAG.getNode(ISD::FP_EXTEND, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000379 } else if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000380 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000381 Val = DAG.getNode(ExtendKind, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000382 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000383 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000384 }
385 } else if (PartBits == ValueVT.getSizeInBits()) {
386 // Different types of the same size.
387 assert(NumParts == 1 && PartVT != ValueVT);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000388 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000389 } else if (NumParts * PartBits < ValueVT.getSizeInBits()) {
390 // If the parts cover less bits than value has, truncate the value.
391 if (PartVT.isInteger() && ValueVT.isInteger()) {
Owen Anderson23b9b192009-08-12 00:36:31 +0000392 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000393 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000394 } else {
Torok Edwinc23197a2009-07-14 16:55:14 +0000395 llvm_unreachable("Unknown mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000396 }
397 }
398
399 // The value may have changed - recompute ValueVT.
400 ValueVT = Val.getValueType();
401 assert(NumParts * PartBits == ValueVT.getSizeInBits() &&
402 "Failed to tile the value with PartVT!");
403
404 if (NumParts == 1) {
405 assert(PartVT == ValueVT && "Type conversion failed!");
406 Parts[0] = Val;
407 return;
408 }
409
410 // Expand the value into multiple parts.
411 if (NumParts & (NumParts - 1)) {
412 // The number of parts is not a power of 2. Split off and copy the tail.
413 assert(PartVT.isInteger() && ValueVT.isInteger() &&
414 "Do not know what to expand to!");
415 unsigned RoundParts = 1 << Log2_32(NumParts);
416 unsigned RoundBits = RoundParts * PartBits;
417 unsigned OddParts = NumParts - RoundParts;
Dale Johannesen66978ee2009-01-31 02:22:37 +0000418 SDValue OddVal = DAG.getNode(ISD::SRL, dl, ValueVT, Val,
Duncan Sands0b3aa262009-01-28 14:42:54 +0000419 DAG.getConstant(RoundBits,
Duncan Sands92abc622009-01-31 15:50:11 +0000420 TLI.getPointerTy()));
Bill Wendling46ada192010-03-02 01:55:18 +0000421 getCopyToParts(DAG, dl, OddVal, Parts + RoundParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +0000422 OddParts, PartVT);
423
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000424 if (TLI.isBigEndian())
425 // The odd parts were reversed by getCopyToParts - unreverse them.
426 std::reverse(Parts + RoundParts, Parts + NumParts);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000427
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000428 NumParts = RoundParts;
Owen Anderson23b9b192009-08-12 00:36:31 +0000429 ValueVT = EVT::getIntegerVT(*DAG.getContext(), NumParts * PartBits);
Dale Johannesen66978ee2009-01-31 02:22:37 +0000430 Val = DAG.getNode(ISD::TRUNCATE, dl, ValueVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000431 }
432
433 // The number of parts is a power of 2. Repeatedly bisect the value using
434 // EXTRACT_ELEMENT.
Scott Michelfdc40a02009-02-17 22:15:04 +0000435 Parts[0] = DAG.getNode(ISD::BIT_CONVERT, dl,
Chris Lattnerf031e8a2010-01-01 03:32:16 +0000436 EVT::getIntegerVT(*DAG.getContext(),
437 ValueVT.getSizeInBits()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000438 Val);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000439
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000440 for (unsigned StepSize = NumParts; StepSize > 1; StepSize /= 2) {
441 for (unsigned i = 0; i < NumParts; i += StepSize) {
442 unsigned ThisBits = StepSize * PartBits / 2;
Owen Anderson23b9b192009-08-12 00:36:31 +0000443 EVT ThisVT = EVT::getIntegerVT(*DAG.getContext(), ThisBits);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000444 SDValue &Part0 = Parts[i];
445 SDValue &Part1 = Parts[i+StepSize/2];
446
Scott Michelfdc40a02009-02-17 22:15:04 +0000447 Part1 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000448 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000449 DAG.getConstant(1, PtrVT));
Scott Michelfdc40a02009-02-17 22:15:04 +0000450 Part0 = DAG.getNode(ISD::EXTRACT_ELEMENT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000451 ThisVT, Part0,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000452 DAG.getConstant(0, PtrVT));
453
454 if (ThisBits == PartBits && ThisVT != PartVT) {
Scott Michelfdc40a02009-02-17 22:15:04 +0000455 Part0 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000456 PartVT, Part0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000457 Part1 = DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000458 PartVT, Part1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000459 }
460 }
461 }
462
463 if (TLI.isBigEndian())
Dale Johannesen8a36f502009-02-25 22:39:13 +0000464 std::reverse(Parts, Parts + OrigNumParts);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000465
466 return;
467 }
468
469 // Vector ValueVT.
470 if (NumParts == 1) {
471 if (PartVT != ValueVT) {
Bob Wilson5afffae2009-12-18 01:03:29 +0000472 if (PartVT.getSizeInBits() == ValueVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +0000473 Val = DAG.getNode(ISD::BIT_CONVERT, dl, PartVT, Val);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000474 } else {
475 assert(ValueVT.getVectorElementType() == PartVT &&
476 ValueVT.getVectorNumElements() == 1 &&
477 "Only trivial vector-to-scalar conversions should get here!");
Scott Michelfdc40a02009-02-17 22:15:04 +0000478 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +0000479 PartVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000480 DAG.getConstant(0, PtrVT));
481 }
482 }
483
484 Parts[0] = Val;
485 return;
486 }
487
488 // Handle a multi-element vector.
Owen Andersone50ed302009-08-10 22:56:29 +0000489 EVT IntermediateVT, RegisterVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000490 unsigned NumIntermediates;
Owen Anderson23b9b192009-08-12 00:36:31 +0000491 unsigned NumRegs = TLI.getVectorTypeBreakdown(*DAG.getContext(), ValueVT,
492 IntermediateVT, NumIntermediates, RegisterVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000493 unsigned NumElements = ValueVT.getVectorNumElements();
494
495 assert(NumRegs == NumParts && "Part count doesn't match vector breakdown!");
496 NumParts = NumRegs; // Silence a compiler warning.
497 assert(RegisterVT == PartVT && "Part type doesn't match vector breakdown!");
498
499 // Split the vector into intermediate operands.
500 SmallVector<SDValue, 8> Ops(NumIntermediates);
Bill Wendling3ea3c242009-12-22 02:10:19 +0000501 for (unsigned i = 0; i != NumIntermediates; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000502 if (IntermediateVT.isVector())
Scott Michelfdc40a02009-02-17 22:15:04 +0000503 Ops[i] = DAG.getNode(ISD::EXTRACT_SUBVECTOR, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000504 IntermediateVT, Val,
505 DAG.getConstant(i * (NumElements / NumIntermediates),
506 PtrVT));
507 else
Scott Michelfdc40a02009-02-17 22:15:04 +0000508 Ops[i] = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000509 IntermediateVT, Val,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000510 DAG.getConstant(i, PtrVT));
Bill Wendling3ea3c242009-12-22 02:10:19 +0000511 }
512
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000513 // Split the intermediate operands into legal parts.
514 if (NumParts == NumIntermediates) {
515 // If the register was not expanded, promote or copy the value,
516 // as appropriate.
517 for (unsigned i = 0; i != NumParts; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000518 getCopyToParts(DAG, dl, Ops[i], &Parts[i], 1, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000519 } else if (NumParts > 0) {
520 // If the intermediate type was expanded, split each the value into
521 // legal parts.
522 assert(NumParts % NumIntermediates == 0 &&
523 "Must expand into a divisible number of parts!");
524 unsigned Factor = NumParts / NumIntermediates;
525 for (unsigned i = 0; i != NumIntermediates; ++i)
Bill Wendling46ada192010-03-02 01:55:18 +0000526 getCopyToParts(DAG, dl, Ops[i], &Parts[i*Factor], Factor, PartVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000527 }
528}
529
530
Dan Gohman2048b852009-11-23 18:04:58 +0000531void SelectionDAGBuilder::init(GCFunctionInfo *gfi, AliasAnalysis &aa) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000532 AA = &aa;
533 GFI = gfi;
534 TD = DAG.getTarget().getTargetData();
535}
536
Dan Gohmanb02b62a2010-04-14 18:24:06 +0000537/// clear - Clear out the current SelectionDAG and the associated
Dan Gohman2048b852009-11-23 18:04:58 +0000538/// state and prepare this SelectionDAGBuilder object to be used
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000539/// for a new block. This doesn't clear out information about
540/// additional blocks that are needed to complete switch lowering
541/// or PHI node updating; that information is cleared out as it is
542/// consumed.
Dan Gohman2048b852009-11-23 18:04:58 +0000543void SelectionDAGBuilder::clear() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000544 NodeMap.clear();
545 PendingLoads.clear();
546 PendingExports.clear();
Evan Chengfb2e7522009-09-18 21:02:19 +0000547 EdgeMapping.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000548 DAG.clear();
Chris Lattnera4f2bb02010-04-02 20:17:23 +0000549 CurDebugLoc = DebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000550 HasTailCall = false;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000551}
552
553/// getRoot - Return the current virtual root of the Selection DAG,
554/// flushing any PendingLoad items. This must be done before emitting
555/// a store or any other node that may need to be ordered after any
556/// prior load instructions.
557///
Dan Gohman2048b852009-11-23 18:04:58 +0000558SDValue SelectionDAGBuilder::getRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000559 if (PendingLoads.empty())
560 return DAG.getRoot();
561
562 if (PendingLoads.size() == 1) {
563 SDValue Root = PendingLoads[0];
564 DAG.setRoot(Root);
565 PendingLoads.clear();
566 return Root;
567 }
568
569 // Otherwise, we have to make a token factor node.
Owen Anderson825b72b2009-08-11 20:47:22 +0000570 SDValue Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000571 &PendingLoads[0], PendingLoads.size());
572 PendingLoads.clear();
573 DAG.setRoot(Root);
574 return Root;
575}
576
577/// getControlRoot - Similar to getRoot, but instead of flushing all the
578/// PendingLoad items, flush all the PendingExports items. It is necessary
579/// to do this before emitting a terminator instruction.
580///
Dan Gohman2048b852009-11-23 18:04:58 +0000581SDValue SelectionDAGBuilder::getControlRoot() {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000582 SDValue Root = DAG.getRoot();
583
584 if (PendingExports.empty())
585 return Root;
586
587 // Turn all of the CopyToReg chains into one factored node.
588 if (Root.getOpcode() != ISD::EntryToken) {
589 unsigned i = 0, e = PendingExports.size();
590 for (; i != e; ++i) {
591 assert(PendingExports[i].getNode()->getNumOperands() > 1);
592 if (PendingExports[i].getNode()->getOperand(0) == Root)
593 break; // Don't add the root if we already indirectly depend on it.
594 }
595
596 if (i == e)
597 PendingExports.push_back(Root);
598 }
599
Owen Anderson825b72b2009-08-11 20:47:22 +0000600 Root = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000601 &PendingExports[0],
602 PendingExports.size());
603 PendingExports.clear();
604 DAG.setRoot(Root);
605 return Root;
606}
607
Bill Wendling4533cac2010-01-28 21:51:40 +0000608void SelectionDAGBuilder::AssignOrderingToNode(const SDNode *Node) {
609 if (DAG.GetOrdering(Node) != 0) return; // Already has ordering.
610 DAG.AssignOrdering(Node, SDNodeOrder);
611
612 for (unsigned I = 0, E = Node->getNumOperands(); I != E; ++I)
613 AssignOrderingToNode(Node->getOperand(I).getNode());
614}
615
Dan Gohman46510a72010-04-15 01:51:59 +0000616void SelectionDAGBuilder::visit(const Instruction &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000617 visit(I.getOpcode(), I);
618}
619
Dan Gohman46510a72010-04-15 01:51:59 +0000620void SelectionDAGBuilder::visit(unsigned Opcode, const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000621 // Note: this doesn't use InstVisitor, because it has to work with
622 // ConstantExpr's in addition to instructions.
623 switch (Opcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000624 default: llvm_unreachable("Unknown instruction type encountered!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000625 // Build the switch statement using the Instruction.def file.
626#define HANDLE_INST(NUM, OPCODE, CLASS) \
Bill Wendling4533cac2010-01-28 21:51:40 +0000627 case Instruction::OPCODE: visit##OPCODE((CLASS&)I); break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000628#include "llvm/Instruction.def"
629 }
Bill Wendling4533cac2010-01-28 21:51:40 +0000630
631 // Assign the ordering to the freshly created DAG nodes.
632 if (NodeMap.count(&I)) {
633 ++SDNodeOrder;
634 AssignOrderingToNode(getValue(&I).getNode());
635 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000636}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000637
Dan Gohman2048b852009-11-23 18:04:58 +0000638SDValue SelectionDAGBuilder::getValue(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000639 SDValue &N = NodeMap[V];
640 if (N.getNode()) return N;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000641
Dan Gohman383b5f62010-04-17 15:32:28 +0000642 if (const Constant *C = dyn_cast<Constant>(V)) {
Owen Andersone50ed302009-08-10 22:56:29 +0000643 EVT VT = TLI.getValueType(V->getType(), true);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000644
Dan Gohman383b5f62010-04-17 15:32:28 +0000645 if (const ConstantInt *CI = dyn_cast<ConstantInt>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000646 return N = DAG.getConstant(*CI, VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000647
Dan Gohman383b5f62010-04-17 15:32:28 +0000648 if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000649 return N = DAG.getGlobalAddress(GV, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000650
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000651 if (isa<ConstantPointerNull>(C))
652 return N = DAG.getConstant(0, TLI.getPointerTy());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000653
Dan Gohman383b5f62010-04-17 15:32:28 +0000654 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
Dan Gohman4fbd7962008-09-12 18:08:03 +0000655 return N = DAG.getConstantFP(*CFP, VT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000656
Nate Begeman9008ca62009-04-27 18:41:29 +0000657 if (isa<UndefValue>(C) && !V->getType()->isAggregateType())
Dale Johannesene8d72302009-02-06 23:05:02 +0000658 return N = DAG.getUNDEF(VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000659
Dan Gohman383b5f62010-04-17 15:32:28 +0000660 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000661 visit(CE->getOpcode(), *CE);
662 SDValue N1 = NodeMap[V];
Dan Gohmanac7d05c2010-04-16 16:55:18 +0000663 assert(N1.getNode() && "visit didn't populate the NodeMap!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000664 return N1;
665 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000666
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000667 if (isa<ConstantStruct>(C) || isa<ConstantArray>(C)) {
668 SmallVector<SDValue, 4> Constants;
669 for (User::const_op_iterator OI = C->op_begin(), OE = C->op_end();
670 OI != OE; ++OI) {
671 SDNode *Val = getValue(*OI).getNode();
Dan Gohmaned48caf2009-09-08 01:44:02 +0000672 // If the operand is an empty aggregate, there are no values.
673 if (!Val) continue;
674 // Add each leaf value from the operand to the Constants list
675 // to form a flattened list of all the values.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000676 for (unsigned i = 0, e = Val->getNumValues(); i != e; ++i)
677 Constants.push_back(SDValue(Val, i));
678 }
Bill Wendling87710f02009-12-21 23:47:40 +0000679
Bill Wendling4533cac2010-01-28 21:51:40 +0000680 return DAG.getMergeValues(&Constants[0], Constants.size(),
681 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000682 }
683
Duncan Sands1df98592010-02-16 11:11:14 +0000684 if (C->getType()->isStructTy() || C->getType()->isArrayTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000685 assert((isa<ConstantAggregateZero>(C) || isa<UndefValue>(C)) &&
686 "Unknown struct or array constant!");
687
Owen Andersone50ed302009-08-10 22:56:29 +0000688 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000689 ComputeValueVTs(TLI, C->getType(), ValueVTs);
690 unsigned NumElts = ValueVTs.size();
691 if (NumElts == 0)
692 return SDValue(); // empty struct
693 SmallVector<SDValue, 4> Constants(NumElts);
694 for (unsigned i = 0; i != NumElts; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +0000695 EVT EltVT = ValueVTs[i];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000696 if (isa<UndefValue>(C))
Dale Johannesene8d72302009-02-06 23:05:02 +0000697 Constants[i] = DAG.getUNDEF(EltVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000698 else if (EltVT.isFloatingPoint())
699 Constants[i] = DAG.getConstantFP(0, EltVT);
700 else
701 Constants[i] = DAG.getConstant(0, EltVT);
702 }
Bill Wendling87710f02009-12-21 23:47:40 +0000703
Bill Wendling4533cac2010-01-28 21:51:40 +0000704 return DAG.getMergeValues(&Constants[0], NumElts,
705 getCurDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000706 }
707
Dan Gohman383b5f62010-04-17 15:32:28 +0000708 if (const BlockAddress *BA = dyn_cast<BlockAddress>(C))
Dan Gohman29cbade2009-11-20 23:18:13 +0000709 return DAG.getBlockAddress(BA, VT);
Dan Gohman8c2b5252009-10-30 01:27:03 +0000710
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000711 const VectorType *VecTy = cast<VectorType>(V->getType());
712 unsigned NumElements = VecTy->getNumElements();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000713
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000714 // Now that we know the number and type of the elements, get that number of
715 // elements into the Ops array based on what kind of constant it is.
716 SmallVector<SDValue, 16> Ops;
Dan Gohman383b5f62010-04-17 15:32:28 +0000717 if (const ConstantVector *CP = dyn_cast<ConstantVector>(C)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000718 for (unsigned i = 0; i != NumElements; ++i)
719 Ops.push_back(getValue(CP->getOperand(i)));
720 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +0000721 assert(isa<ConstantAggregateZero>(C) && "Unknown vector constant!");
Owen Andersone50ed302009-08-10 22:56:29 +0000722 EVT EltVT = TLI.getValueType(VecTy->getElementType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000723
724 SDValue Op;
Nate Begeman9008ca62009-04-27 18:41:29 +0000725 if (EltVT.isFloatingPoint())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000726 Op = DAG.getConstantFP(0, EltVT);
727 else
728 Op = DAG.getConstant(0, EltVT);
729 Ops.assign(NumElements, Op);
730 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000731
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000732 // Create a BUILD_VECTOR node.
Bill Wendling4533cac2010-01-28 21:51:40 +0000733 return NodeMap[V] = DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
734 VT, &Ops[0], Ops.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000735 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000736
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000737 // If this is a static alloca, generate it as the frameindex instead of
738 // computation.
739 if (const AllocaInst *AI = dyn_cast<AllocaInst>(V)) {
740 DenseMap<const AllocaInst*, int>::iterator SI =
741 FuncInfo.StaticAllocaMap.find(AI);
742 if (SI != FuncInfo.StaticAllocaMap.end())
743 return DAG.getFrameIndex(SI->second, TLI.getPointerTy());
744 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000745
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000746 unsigned InReg = FuncInfo.ValueMap[V];
747 assert(InReg && "Value not in map!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000748
Owen Anderson23b9b192009-08-12 00:36:31 +0000749 RegsForValue RFV(*DAG.getContext(), TLI, InReg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000750 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +0000751 return RFV.getCopyFromRegs(DAG, getCurDebugLoc(), Chain, NULL);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000752}
753
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000754/// Get the EVTs and ArgFlags collections that represent the legalized return
755/// type of the given function. This does not require a DAG or a return value,
756/// and is suitable for use before any DAGs for the function are constructed.
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000757static void getReturnInfo(const Type* ReturnType,
758 Attributes attr, SmallVectorImpl<EVT> &OutVTs,
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000759 SmallVectorImpl<ISD::ArgFlagsTy> &OutFlags,
Dan Gohmand858e902010-04-17 15:26:15 +0000760 const TargetLowering &TLI,
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000761 SmallVectorImpl<uint64_t> *Offsets = 0) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000762 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000763 ComputeValueVTs(TLI, ReturnType, ValueVTs);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000764 unsigned NumValues = ValueVTs.size();
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000765 if (NumValues == 0) return;
766 unsigned Offset = 0;
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000767
768 for (unsigned j = 0, f = NumValues; j != f; ++j) {
769 EVT VT = ValueVTs[j];
770 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000771
772 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000773 ExtendKind = ISD::SIGN_EXTEND;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000774 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000775 ExtendKind = ISD::ZERO_EXTEND;
776
777 // FIXME: C calling convention requires the return type to be promoted to
778 // at least 32-bit. But this is not necessary for non-C calling
779 // conventions. The frontend should mark functions whose return values
780 // require promoting with signext or zeroext attributes.
781 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000782 EVT MinVT = TLI.getRegisterType(ReturnType->getContext(), MVT::i32);
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000783 if (VT.bitsLT(MinVT))
784 VT = MinVT;
785 }
786
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000787 unsigned NumParts = TLI.getNumRegisters(ReturnType->getContext(), VT);
788 EVT PartVT = TLI.getRegisterType(ReturnType->getContext(), VT);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000789 unsigned PartSize = TLI.getTargetData()->getTypeAllocSize(
790 PartVT.getTypeForEVT(ReturnType->getContext()));
791
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000792 // 'inreg' on function refers to return value
793 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000794 if (attr & Attribute::InReg)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000795 Flags.setInReg();
796
797 // Propagate extension type if any
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000798 if (attr & Attribute::SExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000799 Flags.setSExt();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000800 else if (attr & Attribute::ZExt)
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000801 Flags.setZExt();
802
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000803 for (unsigned i = 0; i < NumParts; ++i) {
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000804 OutVTs.push_back(PartVT);
805 OutFlags.push_back(Flags);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +0000806 if (Offsets)
807 {
808 Offsets->push_back(Offset);
809 Offset += PartSize;
810 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +0000811 }
812 }
813}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000814
Dan Gohman46510a72010-04-15 01:51:59 +0000815void SelectionDAGBuilder::visitRet(const ReturnInst &I) {
Dan Gohman98ca4f22009-08-05 01:29:28 +0000816 SDValue Chain = getControlRoot();
817 SmallVector<ISD::OutputArg, 8> Outs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000818 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000819
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000820 if (!FLI.CanLowerReturn) {
821 unsigned DemoteReg = FLI.DemoteRegister;
822 const Function *F = I.getParent()->getParent();
823
824 // Emit a store of the return value through the virtual register.
825 // Leave Outs empty so that LowerReturn won't try to load return
826 // registers the usual way.
827 SmallVector<EVT, 1> PtrValueVTs;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000828 ComputeValueVTs(TLI, PointerType::getUnqual(F->getReturnType()),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000829 PtrValueVTs);
830
831 SDValue RetPtr = DAG.getRegister(DemoteReg, PtrValueVTs[0]);
832 SDValue RetOp = getValue(I.getOperand(0));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000833
Owen Andersone50ed302009-08-10 22:56:29 +0000834 SmallVector<EVT, 4> ValueVTs;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000835 SmallVector<uint64_t, 4> Offsets;
836 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs, &Offsets);
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000837 unsigned NumValues = ValueVTs.size();
Dan Gohman7ea1ca62008-10-21 20:00:42 +0000838
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000839 SmallVector<SDValue, 4> Chains(NumValues);
840 EVT PtrVT = PtrValueVTs[0];
Bill Wendling87710f02009-12-21 23:47:40 +0000841 for (unsigned i = 0; i != NumValues; ++i) {
842 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, RetPtr,
843 DAG.getConstant(Offsets[i], PtrVT));
844 Chains[i] =
845 DAG.getStore(Chain, getCurDebugLoc(),
846 SDValue(RetOp.getNode(), RetOp.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +0000847 Add, NULL, Offsets[i], false, false, 0);
Bill Wendling87710f02009-12-21 23:47:40 +0000848 }
849
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000850 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
851 MVT::Other, &Chains[0], NumValues);
Chris Lattner25d58372010-02-28 18:53:13 +0000852 } else if (I.getNumOperands() != 0) {
853 SmallVector<EVT, 4> ValueVTs;
854 ComputeValueVTs(TLI, I.getOperand(0)->getType(), ValueVTs);
855 unsigned NumValues = ValueVTs.size();
856 if (NumValues) {
857 SDValue RetOp = getValue(I.getOperand(0));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000858 for (unsigned j = 0, f = NumValues; j != f; ++j) {
859 EVT VT = ValueVTs[j];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000860
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000861 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000862
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000863 const Function *F = I.getParent()->getParent();
864 if (F->paramHasAttr(0, Attribute::SExt))
865 ExtendKind = ISD::SIGN_EXTEND;
866 else if (F->paramHasAttr(0, Attribute::ZExt))
867 ExtendKind = ISD::ZERO_EXTEND;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000868
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +0000869 // FIXME: C calling convention requires the return type to be promoted
870 // to at least 32-bit. But this is not necessary for non-C calling
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000871 // conventions. The frontend should mark functions whose return values
872 // require promoting with signext or zeroext attributes.
873 if (ExtendKind != ISD::ANY_EXTEND && VT.isInteger()) {
874 EVT MinVT = TLI.getRegisterType(*DAG.getContext(), MVT::i32);
875 if (VT.bitsLT(MinVT))
876 VT = MinVT;
877 }
878
879 unsigned NumParts = TLI.getNumRegisters(*DAG.getContext(), VT);
880 EVT PartVT = TLI.getRegisterType(*DAG.getContext(), VT);
881 SmallVector<SDValue, 4> Parts(NumParts);
Bill Wendling46ada192010-03-02 01:55:18 +0000882 getCopyToParts(DAG, getCurDebugLoc(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +0000883 SDValue(RetOp.getNode(), RetOp.getResNo() + j),
884 &Parts[0], NumParts, PartVT, ExtendKind);
885
886 // 'inreg' on function refers to return value
887 ISD::ArgFlagsTy Flags = ISD::ArgFlagsTy();
888 if (F->paramHasAttr(0, Attribute::InReg))
889 Flags.setInReg();
890
891 // Propagate extension type if any
892 if (F->paramHasAttr(0, Attribute::SExt))
893 Flags.setSExt();
894 else if (F->paramHasAttr(0, Attribute::ZExt))
895 Flags.setZExt();
896
897 for (unsigned i = 0; i < NumParts; ++i)
898 Outs.push_back(ISD::OutputArg(Flags, Parts[i], /*isfixed=*/true));
Evan Cheng3927f432009-03-25 20:20:11 +0000899 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000900 }
901 }
Dan Gohman98ca4f22009-08-05 01:29:28 +0000902
903 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000904 CallingConv::ID CallConv =
905 DAG.getMachineFunction().getFunction()->getCallingConv();
Dan Gohman98ca4f22009-08-05 01:29:28 +0000906 Chain = TLI.LowerReturn(Chain, CallConv, isVarArg,
907 Outs, getCurDebugLoc(), DAG);
Dan Gohman5e866062009-08-06 15:37:27 +0000908
909 // Verify that the target's LowerReturn behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +0000910 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +0000911 "LowerReturn didn't return a valid chain!");
912
913 // Update the DAG with the new chain value resulting from return lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000914 DAG.setRoot(Chain);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000915}
916
Dan Gohmanad62f532009-04-23 23:13:24 +0000917/// CopyToExportRegsIfNeeded - If the given value has virtual registers
918/// created for it, emit nodes to copy the value into the virtual
919/// registers.
Dan Gohman46510a72010-04-15 01:51:59 +0000920void SelectionDAGBuilder::CopyToExportRegsIfNeeded(const Value *V) {
Dan Gohman33b7a292010-04-16 17:15:02 +0000921 DenseMap<const Value *, unsigned>::iterator VMI = FuncInfo.ValueMap.find(V);
922 if (VMI != FuncInfo.ValueMap.end()) {
923 assert(!V->use_empty() && "Unused value assigned virtual registers!");
924 CopyValueToVirtualRegister(V, VMI->second);
Dan Gohmanad62f532009-04-23 23:13:24 +0000925 }
926}
927
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000928/// ExportFromCurrentBlock - If this condition isn't known to be exported from
929/// the current basic block, add it to ValueMap now so that we'll get a
930/// CopyTo/FromReg.
Dan Gohman46510a72010-04-15 01:51:59 +0000931void SelectionDAGBuilder::ExportFromCurrentBlock(const Value *V) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000932 // No need to export constants.
933 if (!isa<Instruction>(V) && !isa<Argument>(V)) return;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000934
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000935 // Already exported?
936 if (FuncInfo.isExportedInst(V)) return;
937
938 unsigned Reg = FuncInfo.InitializeRegForValue(V);
939 CopyValueToVirtualRegister(V, Reg);
940}
941
Dan Gohman46510a72010-04-15 01:51:59 +0000942bool SelectionDAGBuilder::isExportableFromCurrentBlock(const Value *V,
Dan Gohman2048b852009-11-23 18:04:58 +0000943 const BasicBlock *FromBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000944 // The operands of the setcc have to be in this block. We don't know
945 // how to export them from some other block.
Dan Gohman46510a72010-04-15 01:51:59 +0000946 if (const Instruction *VI = dyn_cast<Instruction>(V)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000947 // Can export from current BB.
948 if (VI->getParent() == FromBB)
949 return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000950
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000951 // Is already exported, noop.
952 return FuncInfo.isExportedInst(V);
953 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000954
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000955 // If this is an argument, we can export it if the BB is the entry block or
956 // if it is already exported.
957 if (isa<Argument>(V)) {
958 if (FromBB == &FromBB->getParent()->getEntryBlock())
959 return true;
960
961 // Otherwise, can only export this if it is already exported.
962 return FuncInfo.isExportedInst(V);
963 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +0000964
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000965 // Otherwise, constants can always be exported.
966 return true;
967}
968
969static bool InBlock(const Value *V, const BasicBlock *BB) {
970 if (const Instruction *I = dyn_cast<Instruction>(V))
971 return I->getParent() == BB;
972 return true;
973}
974
Dan Gohmanc2277342008-10-17 21:16:08 +0000975/// EmitBranchForMergedCondition - Helper method for FindMergedConditions.
976/// This function emits a branch and is used at the leaves of an OR or an
977/// AND operator tree.
978///
979void
Dan Gohman46510a72010-04-15 01:51:59 +0000980SelectionDAGBuilder::EmitBranchForMergedCondition(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +0000981 MachineBasicBlock *TBB,
982 MachineBasicBlock *FBB,
983 MachineBasicBlock *CurBB) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000984 const BasicBlock *BB = CurBB->getBasicBlock();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000985
Dan Gohmanc2277342008-10-17 21:16:08 +0000986 // If the leaf of the tree is a comparison, merge the condition into
987 // the caseblock.
Dan Gohman46510a72010-04-15 01:51:59 +0000988 if (const CmpInst *BOp = dyn_cast<CmpInst>(Cond)) {
Dan Gohmanc2277342008-10-17 21:16:08 +0000989 // The operands of the cmp have to be in this block. We don't know
990 // how to export them from some other block. If this is the first block
991 // of the sequence, no exporting is needed.
992 if (CurBB == CurMBB ||
993 (isExportableFromCurrentBlock(BOp->getOperand(0), BB) &&
994 isExportableFromCurrentBlock(BOp->getOperand(1), BB))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +0000995 ISD::CondCode Condition;
Dan Gohman46510a72010-04-15 01:51:59 +0000996 if (const ICmpInst *IC = dyn_cast<ICmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000997 Condition = getICmpCondCode(IC->getPredicate());
Dan Gohman46510a72010-04-15 01:51:59 +0000998 } else if (const FCmpInst *FC = dyn_cast<FCmpInst>(Cond)) {
Dan Gohman8c1a6ca2008-10-17 18:18:45 +0000999 Condition = getFCmpCondCode(FC->getPredicate());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001000 } else {
1001 Condition = ISD::SETEQ; // silence warning.
Torok Edwinc23197a2009-07-14 16:55:14 +00001002 llvm_unreachable("Unknown compare instruction");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001003 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001004
1005 CaseBlock CB(Condition, BOp->getOperand(0),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001006 BOp->getOperand(1), NULL, TBB, FBB, CurBB);
1007 SwitchCases.push_back(CB);
1008 return;
1009 }
Dan Gohmanc2277342008-10-17 21:16:08 +00001010 }
1011
1012 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001013 CaseBlock CB(ISD::SETEQ, Cond, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanc2277342008-10-17 21:16:08 +00001014 NULL, TBB, FBB, CurBB);
1015 SwitchCases.push_back(CB);
1016}
1017
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001018/// FindMergedConditions - If Cond is an expression like
Dan Gohman46510a72010-04-15 01:51:59 +00001019void SelectionDAGBuilder::FindMergedConditions(const Value *Cond,
Dan Gohman2048b852009-11-23 18:04:58 +00001020 MachineBasicBlock *TBB,
1021 MachineBasicBlock *FBB,
1022 MachineBasicBlock *CurBB,
1023 unsigned Opc) {
Dan Gohmanc2277342008-10-17 21:16:08 +00001024 // If this node is not part of the or/and tree, emit it as a branch.
Dan Gohman46510a72010-04-15 01:51:59 +00001025 const Instruction *BOp = dyn_cast<Instruction>(Cond);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001026 if (!BOp || !(isa<BinaryOperator>(BOp) || isa<CmpInst>(BOp)) ||
Dan Gohmanc2277342008-10-17 21:16:08 +00001027 (unsigned)BOp->getOpcode() != Opc || !BOp->hasOneUse() ||
1028 BOp->getParent() != CurBB->getBasicBlock() ||
1029 !InBlock(BOp->getOperand(0), CurBB->getBasicBlock()) ||
1030 !InBlock(BOp->getOperand(1), CurBB->getBasicBlock())) {
1031 EmitBranchForMergedCondition(Cond, TBB, FBB, CurBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001032 return;
1033 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001034
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001035 // Create TmpBB after CurBB.
1036 MachineFunction::iterator BBI = CurBB;
1037 MachineFunction &MF = DAG.getMachineFunction();
1038 MachineBasicBlock *TmpBB = MF.CreateMachineBasicBlock(CurBB->getBasicBlock());
1039 CurBB->getParent()->insert(++BBI, TmpBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001040
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001041 if (Opc == Instruction::Or) {
1042 // Codegen X | Y as:
1043 // jmp_if_X TBB
1044 // jmp TmpBB
1045 // TmpBB:
1046 // jmp_if_Y TBB
1047 // jmp FBB
1048 //
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001049
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001050 // Emit the LHS condition.
1051 FindMergedConditions(BOp->getOperand(0), TBB, TmpBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001052
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001053 // Emit the RHS condition into TmpBB.
1054 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1055 } else {
1056 assert(Opc == Instruction::And && "Unknown merge op!");
1057 // Codegen X & Y as:
1058 // jmp_if_X TmpBB
1059 // jmp FBB
1060 // TmpBB:
1061 // jmp_if_Y TBB
1062 // jmp FBB
1063 //
1064 // This requires creation of TmpBB after CurBB.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001065
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001066 // Emit the LHS condition.
1067 FindMergedConditions(BOp->getOperand(0), TmpBB, FBB, CurBB, Opc);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001068
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001069 // Emit the RHS condition into TmpBB.
1070 FindMergedConditions(BOp->getOperand(1), TBB, FBB, TmpBB, Opc);
1071 }
1072}
1073
1074/// If the set of cases should be emitted as a series of branches, return true.
1075/// If we should emit this as a bunch of and/or'd together conditions, return
1076/// false.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001077bool
Dan Gohman2048b852009-11-23 18:04:58 +00001078SelectionDAGBuilder::ShouldEmitAsBranches(const std::vector<CaseBlock> &Cases){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001079 if (Cases.size() != 2) return true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001080
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001081 // If this is two comparisons of the same values or'd or and'd together, they
1082 // will get folded into a single comparison, so don't emit two blocks.
1083 if ((Cases[0].CmpLHS == Cases[1].CmpLHS &&
1084 Cases[0].CmpRHS == Cases[1].CmpRHS) ||
1085 (Cases[0].CmpRHS == Cases[1].CmpLHS &&
1086 Cases[0].CmpLHS == Cases[1].CmpRHS)) {
1087 return false;
1088 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001089
Chris Lattner133ce872010-01-02 00:00:03 +00001090 // Handle: (X != null) | (Y != null) --> (X|Y) != 0
1091 // Handle: (X == null) & (Y == null) --> (X|Y) == 0
1092 if (Cases[0].CmpRHS == Cases[1].CmpRHS &&
1093 Cases[0].CC == Cases[1].CC &&
1094 isa<Constant>(Cases[0].CmpRHS) &&
1095 cast<Constant>(Cases[0].CmpRHS)->isNullValue()) {
1096 if (Cases[0].CC == ISD::SETEQ && Cases[0].TrueBB == Cases[1].ThisBB)
1097 return false;
1098 if (Cases[0].CC == ISD::SETNE && Cases[0].FalseBB == Cases[1].ThisBB)
1099 return false;
1100 }
1101
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001102 return true;
1103}
1104
Dan Gohman46510a72010-04-15 01:51:59 +00001105void SelectionDAGBuilder::visitBr(const BranchInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001106 // Update machine-CFG edges.
1107 MachineBasicBlock *Succ0MBB = FuncInfo.MBBMap[I.getSuccessor(0)];
1108
1109 // Figure out which block is immediately after the current one.
1110 MachineBasicBlock *NextBlock = 0;
1111 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001112 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001113 NextBlock = BBI;
1114
1115 if (I.isUnconditional()) {
1116 // Update machine-CFG edges.
1117 CurMBB->addSuccessor(Succ0MBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001118
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001119 // If this is not a fall-through branch, emit the branch.
Bill Wendling4533cac2010-01-28 21:51:40 +00001120 if (Succ0MBB != NextBlock)
1121 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001122 MVT::Other, getControlRoot(),
Bill Wendling4533cac2010-01-28 21:51:40 +00001123 DAG.getBasicBlock(Succ0MBB)));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001124
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001125 return;
1126 }
1127
1128 // If this condition is one of the special cases we handle, do special stuff
1129 // now.
Dan Gohman46510a72010-04-15 01:51:59 +00001130 const Value *CondVal = I.getCondition();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001131 MachineBasicBlock *Succ1MBB = FuncInfo.MBBMap[I.getSuccessor(1)];
1132
1133 // If this is a series of conditions that are or'd or and'd together, emit
1134 // this as a sequence of branches instead of setcc's with and/or operations.
1135 // For example, instead of something like:
1136 // cmp A, B
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001137 // C = seteq
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001138 // cmp D, E
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001139 // F = setle
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001140 // or C, F
1141 // jnz foo
1142 // Emit:
1143 // cmp A, B
1144 // je foo
1145 // cmp D, E
1146 // jle foo
1147 //
Dan Gohman46510a72010-04-15 01:51:59 +00001148 if (const BinaryOperator *BOp = dyn_cast<BinaryOperator>(CondVal)) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001149 if (BOp->hasOneUse() &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001150 (BOp->getOpcode() == Instruction::And ||
1151 BOp->getOpcode() == Instruction::Or)) {
1152 FindMergedConditions(BOp, Succ0MBB, Succ1MBB, CurMBB, BOp->getOpcode());
1153 // If the compares in later blocks need to use values not currently
1154 // exported from this block, export them now. This block should always
1155 // be the first entry.
1156 assert(SwitchCases[0].ThisBB == CurMBB && "Unexpected lowering!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001157
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001158 // Allow some cases to be rejected.
1159 if (ShouldEmitAsBranches(SwitchCases)) {
1160 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i) {
1161 ExportFromCurrentBlock(SwitchCases[i].CmpLHS);
1162 ExportFromCurrentBlock(SwitchCases[i].CmpRHS);
1163 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001164
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001165 // Emit the branch for this block.
1166 visitSwitchCase(SwitchCases[0]);
1167 SwitchCases.erase(SwitchCases.begin());
1168 return;
1169 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001170
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001171 // Okay, we decided not to do this, remove any inserted MBB's and clear
1172 // SwitchCases.
1173 for (unsigned i = 1, e = SwitchCases.size(); i != e; ++i)
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001174 FuncInfo.MF->erase(SwitchCases[i].ThisBB);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001175
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001176 SwitchCases.clear();
1177 }
1178 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001179
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001180 // Create a CaseBlock record representing this branch.
Owen Anderson5defacc2009-07-31 17:39:07 +00001181 CaseBlock CB(ISD::SETEQ, CondVal, ConstantInt::getTrue(*DAG.getContext()),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001182 NULL, Succ0MBB, Succ1MBB, CurMBB);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001183
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001184 // Use visitSwitchCase to actually insert the fast branch sequence for this
1185 // cond branch.
1186 visitSwitchCase(CB);
1187}
1188
1189/// visitSwitchCase - Emits the necessary code to represent a single node in
1190/// the binary search tree resulting from lowering a switch instruction.
Dan Gohman2048b852009-11-23 18:04:58 +00001191void SelectionDAGBuilder::visitSwitchCase(CaseBlock &CB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001192 SDValue Cond;
1193 SDValue CondLHS = getValue(CB.CmpLHS);
Dale Johannesenf5d97892009-02-04 01:48:28 +00001194 DebugLoc dl = getCurDebugLoc();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001195
1196 // Build the setcc now.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001197 if (CB.CmpMHS == NULL) {
1198 // Fold "(X == true)" to X and "(X == false)" to !X to
1199 // handle common cases produced by branch lowering.
Owen Anderson5defacc2009-07-31 17:39:07 +00001200 if (CB.CmpRHS == ConstantInt::getTrue(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001201 CB.CC == ISD::SETEQ)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001202 Cond = CondLHS;
Owen Anderson5defacc2009-07-31 17:39:07 +00001203 else if (CB.CmpRHS == ConstantInt::getFalse(*DAG.getContext()) &&
Owen Andersonf53c3712009-07-21 02:47:59 +00001204 CB.CC == ISD::SETEQ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001205 SDValue True = DAG.getConstant(1, CondLHS.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001206 Cond = DAG.getNode(ISD::XOR, dl, CondLHS.getValueType(), CondLHS, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001207 } else
Owen Anderson825b72b2009-08-11 20:47:22 +00001208 Cond = DAG.getSetCC(dl, MVT::i1, CondLHS, getValue(CB.CmpRHS), CB.CC);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001209 } else {
1210 assert(CB.CC == ISD::SETLE && "Can handle only LE ranges now");
1211
Anton Korobeynikov23218582008-12-23 22:25:27 +00001212 const APInt& Low = cast<ConstantInt>(CB.CmpLHS)->getValue();
1213 const APInt& High = cast<ConstantInt>(CB.CmpRHS)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001214
1215 SDValue CmpOp = getValue(CB.CmpMHS);
Owen Andersone50ed302009-08-10 22:56:29 +00001216 EVT VT = CmpOp.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001217
1218 if (cast<ConstantInt>(CB.CmpLHS)->isMinValue(true)) {
Owen Anderson825b72b2009-08-11 20:47:22 +00001219 Cond = DAG.getSetCC(dl, MVT::i1, CmpOp, DAG.getConstant(High, VT),
Dale Johannesenf5d97892009-02-04 01:48:28 +00001220 ISD::SETLE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001221 } else {
Dale Johannesenf5d97892009-02-04 01:48:28 +00001222 SDValue SUB = DAG.getNode(ISD::SUB, dl,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001223 VT, CmpOp, DAG.getConstant(Low, VT));
Owen Anderson825b72b2009-08-11 20:47:22 +00001224 Cond = DAG.getSetCC(dl, MVT::i1, SUB,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001225 DAG.getConstant(High-Low, VT), ISD::SETULE);
1226 }
1227 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001228
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001229 // Update successor info
1230 CurMBB->addSuccessor(CB.TrueBB);
1231 CurMBB->addSuccessor(CB.FalseBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001232
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001233 // Set NextBlock to be the MBB immediately after the current one, if any.
1234 // This is used to avoid emitting unnecessary branches to the next block.
1235 MachineBasicBlock *NextBlock = 0;
1236 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001237 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001238 NextBlock = BBI;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001240 // If the lhs block is the next block, invert the condition so that we can
1241 // fall through to the lhs instead of the rhs block.
1242 if (CB.TrueBB == NextBlock) {
1243 std::swap(CB.TrueBB, CB.FalseBB);
1244 SDValue True = DAG.getConstant(1, Cond.getValueType());
Dale Johannesenf5d97892009-02-04 01:48:28 +00001245 Cond = DAG.getNode(ISD::XOR, dl, Cond.getValueType(), Cond, True);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001246 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001247
Dale Johannesenf5d97892009-02-04 01:48:28 +00001248 SDValue BrCond = DAG.getNode(ISD::BRCOND, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00001249 MVT::Other, getControlRoot(), Cond,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001250 DAG.getBasicBlock(CB.TrueBB));
Bill Wendling87710f02009-12-21 23:47:40 +00001251
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001252 // If the branch was constant folded, fix up the CFG.
1253 if (BrCond.getOpcode() == ISD::BR) {
1254 CurMBB->removeSuccessor(CB.FalseBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001255 } else {
1256 // Otherwise, go ahead and insert the false branch.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001257 if (BrCond == getControlRoot())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001258 CurMBB->removeSuccessor(CB.TrueBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001259
Bill Wendling4533cac2010-01-28 21:51:40 +00001260 if (CB.FalseBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001261 BrCond = DAG.getNode(ISD::BR, dl, MVT::Other, BrCond,
1262 DAG.getBasicBlock(CB.FalseBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001263 }
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001264
1265 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001266}
1267
1268/// visitJumpTable - Emit JumpTable node in the current MBB
Dan Gohman2048b852009-11-23 18:04:58 +00001269void SelectionDAGBuilder::visitJumpTable(JumpTable &JT) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001270 // Emit the code for the jump table
1271 assert(JT.Reg != -1U && "Should lower JT Header first!");
Owen Andersone50ed302009-08-10 22:56:29 +00001272 EVT PTy = TLI.getPointerTy();
Dale Johannesena04b7572009-02-03 23:04:43 +00001273 SDValue Index = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(),
1274 JT.Reg, PTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001275 SDValue Table = DAG.getJumpTable(JT.JTI, PTy);
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001276 SDValue BrJumpTable = DAG.getNode(ISD::BR_JT, getCurDebugLoc(),
1277 MVT::Other, Index.getValue(1),
1278 Table, Index);
1279 DAG.setRoot(BrJumpTable);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001280}
1281
1282/// visitJumpTableHeader - This function emits necessary code to produce index
1283/// in the JumpTable from switch case.
Dan Gohman2048b852009-11-23 18:04:58 +00001284void SelectionDAGBuilder::visitJumpTableHeader(JumpTable &JT,
1285 JumpTableHeader &JTH) {
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001286 // Subtract the lowest switch case value from the value being switched on and
1287 // conditional branch to default mbb if the result is greater than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001288 // difference between smallest and largest cases.
1289 SDValue SwitchOp = getValue(JTH.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001290 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001291 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001292 DAG.getConstant(JTH.First, VT));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001293
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001294 // The SDNode we just created, which holds the value being switched on minus
Dan Gohmanf451cb82010-02-10 16:03:48 +00001295 // the smallest case value, needs to be copied to a virtual register so it
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001296 // can be used as an index into the jump table in a subsequent basic block.
1297 // This value may be smaller or larger than the target's pointer type, and
1298 // therefore require extension or truncating.
Bill Wendling87710f02009-12-21 23:47:40 +00001299 SwitchOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(), TLI.getPointerTy());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001300
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001301 unsigned JumpTableReg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001302 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1303 JumpTableReg, SwitchOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001304 JT.Reg = JumpTableReg;
1305
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001306 // Emit the range check for the jump table, and branch to the default block
1307 // for the switch statement if the value being switched on exceeds the largest
1308 // case in the switch.
Dale Johannesenf5d97892009-02-04 01:48:28 +00001309 SDValue CMP = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001310 TLI.getSetCCResultType(Sub.getValueType()), Sub,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001311 DAG.getConstant(JTH.Last-JTH.First,VT),
1312 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001313
1314 // Set NextBlock to be the MBB immediately after the current one, if any.
1315 // This is used to avoid emitting unnecessary branches to the next block.
1316 MachineBasicBlock *NextBlock = 0;
1317 MachineFunction::iterator BBI = CurMBB;
Bill Wendling87710f02009-12-21 23:47:40 +00001318
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001319 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001320 NextBlock = BBI;
1321
Dale Johannesen66978ee2009-01-31 02:22:37 +00001322 SDValue BrCond = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001323 MVT::Other, CopyTo, CMP,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001324 DAG.getBasicBlock(JT.Default));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001325
Bill Wendling4533cac2010-01-28 21:51:40 +00001326 if (JT.MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001327 BrCond = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrCond,
1328 DAG.getBasicBlock(JT.MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001329
Bill Wendling87710f02009-12-21 23:47:40 +00001330 DAG.setRoot(BrCond);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001331}
1332
1333/// visitBitTestHeader - This function emits necessary code to produce value
1334/// suitable for "bit tests"
Dan Gohman2048b852009-11-23 18:04:58 +00001335void SelectionDAGBuilder::visitBitTestHeader(BitTestBlock &B) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001336 // Subtract the minimum value
1337 SDValue SwitchOp = getValue(B.SValue);
Owen Andersone50ed302009-08-10 22:56:29 +00001338 EVT VT = SwitchOp.getValueType();
Bill Wendling87710f02009-12-21 23:47:40 +00001339 SDValue Sub = DAG.getNode(ISD::SUB, getCurDebugLoc(), VT, SwitchOp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001340 DAG.getConstant(B.First, VT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001341
1342 // Check range
Dale Johannesenf5d97892009-02-04 01:48:28 +00001343 SDValue RangeCmp = DAG.getSetCC(getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00001344 TLI.getSetCCResultType(Sub.getValueType()),
1345 Sub, DAG.getConstant(B.Range, VT),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001346 ISD::SETUGT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001347
Bill Wendling87710f02009-12-21 23:47:40 +00001348 SDValue ShiftOp = DAG.getZExtOrTrunc(Sub, getCurDebugLoc(),
1349 TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001350
Duncan Sands92abc622009-01-31 15:50:11 +00001351 B.Reg = FuncInfo.MakeReg(TLI.getPointerTy());
Dale Johannesena04b7572009-02-03 23:04:43 +00001352 SDValue CopyTo = DAG.getCopyToReg(getControlRoot(), getCurDebugLoc(),
1353 B.Reg, ShiftOp);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001354
1355 // Set NextBlock to be the MBB immediately after the current one, if any.
1356 // This is used to avoid emitting unnecessary branches to the next block.
1357 MachineBasicBlock *NextBlock = 0;
1358 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001359 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001360 NextBlock = BBI;
1361
1362 MachineBasicBlock* MBB = B.Cases[0].ThisBB;
1363
1364 CurMBB->addSuccessor(B.Default);
1365 CurMBB->addSuccessor(MBB);
1366
Dale Johannesen66978ee2009-01-31 02:22:37 +00001367 SDValue BrRange = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001368 MVT::Other, CopyTo, RangeCmp,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001369 DAG.getBasicBlock(B.Default));
Anton Korobeynikov23218582008-12-23 22:25:27 +00001370
Bill Wendling4533cac2010-01-28 21:51:40 +00001371 if (MBB != NextBlock)
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001372 BrRange = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, CopyTo,
1373 DAG.getBasicBlock(MBB));
Bill Wendling3b7a41c2009-12-21 19:59:38 +00001374
Bill Wendling87710f02009-12-21 23:47:40 +00001375 DAG.setRoot(BrRange);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001376}
1377
1378/// visitBitTestCase - this function produces one "bit test"
Dan Gohman2048b852009-11-23 18:04:58 +00001379void SelectionDAGBuilder::visitBitTestCase(MachineBasicBlock* NextMBB,
1380 unsigned Reg,
1381 BitTestCase &B) {
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001382 // Make desired shift
Dale Johannesena04b7572009-02-03 23:04:43 +00001383 SDValue ShiftOp = DAG.getCopyFromReg(getControlRoot(), getCurDebugLoc(), Reg,
Duncan Sands92abc622009-01-31 15:50:11 +00001384 TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001385 SDValue SwitchVal = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001386 TLI.getPointerTy(),
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001387 DAG.getConstant(1, TLI.getPointerTy()),
1388 ShiftOp);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001389
Anton Korobeynikov36c826a2009-01-26 19:26:01 +00001390 // Emit bit tests and jumps
Scott Michelfdc40a02009-02-17 22:15:04 +00001391 SDValue AndOp = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00001392 TLI.getPointerTy(), SwitchVal,
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001393 DAG.getConstant(B.Mask, TLI.getPointerTy()));
Dale Johannesenf5d97892009-02-04 01:48:28 +00001394 SDValue AndCmp = DAG.getSetCC(getCurDebugLoc(),
1395 TLI.getSetCCResultType(AndOp.getValueType()),
Duncan Sands5480c042009-01-01 15:52:00 +00001396 AndOp, DAG.getConstant(0, TLI.getPointerTy()),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001397 ISD::SETNE);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001398
1399 CurMBB->addSuccessor(B.TargetBB);
1400 CurMBB->addSuccessor(NextMBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001401
Dale Johannesen66978ee2009-01-31 02:22:37 +00001402 SDValue BrAnd = DAG.getNode(ISD::BRCOND, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00001403 MVT::Other, getControlRoot(),
Anton Korobeynikov1bfe2372008-12-23 22:25:45 +00001404 AndCmp, DAG.getBasicBlock(B.TargetBB));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001405
1406 // Set NextBlock to be the MBB immediately after the current one, if any.
1407 // This is used to avoid emitting unnecessary branches to the next block.
1408 MachineBasicBlock *NextBlock = 0;
1409 MachineFunction::iterator BBI = CurMBB;
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001410 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001411 NextBlock = BBI;
1412
Bill Wendling4533cac2010-01-28 21:51:40 +00001413 if (NextMBB != NextBlock)
Bill Wendling0777e922009-12-21 21:59:52 +00001414 BrAnd = DAG.getNode(ISD::BR, getCurDebugLoc(), MVT::Other, BrAnd,
1415 DAG.getBasicBlock(NextMBB));
Bill Wendling0777e922009-12-21 21:59:52 +00001416
Bill Wendling87710f02009-12-21 23:47:40 +00001417 DAG.setRoot(BrAnd);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001418}
1419
Dan Gohman46510a72010-04-15 01:51:59 +00001420void SelectionDAGBuilder::visitInvoke(const InvokeInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001421 // Retrieve successors.
1422 MachineBasicBlock *Return = FuncInfo.MBBMap[I.getSuccessor(0)];
1423 MachineBasicBlock *LandingPad = FuncInfo.MBBMap[I.getSuccessor(1)];
1424
Gabor Greifb67e6b32009-01-15 11:10:44 +00001425 const Value *Callee(I.getCalledValue());
1426 if (isa<InlineAsm>(Callee))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001427 visitInlineAsm(&I);
1428 else
Gabor Greifb67e6b32009-01-15 11:10:44 +00001429 LowerCallTo(&I, getValue(Callee), false, LandingPad);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001430
1431 // If the value of the invoke is used outside of its defining block, make it
1432 // available as a virtual register.
Dan Gohmanad62f532009-04-23 23:13:24 +00001433 CopyToExportRegsIfNeeded(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001434
1435 // Update successor info
1436 CurMBB->addSuccessor(Return);
1437 CurMBB->addSuccessor(LandingPad);
1438
1439 // Drop into normal successor.
Bill Wendling4533cac2010-01-28 21:51:40 +00001440 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1441 MVT::Other, getControlRoot(),
1442 DAG.getBasicBlock(Return)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001443}
1444
Dan Gohman46510a72010-04-15 01:51:59 +00001445void SelectionDAGBuilder::visitUnwind(const UnwindInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001446}
1447
1448/// handleSmallSwitchCaseRange - Emit a series of specific tests (suitable for
1449/// small case ranges).
Dan Gohman2048b852009-11-23 18:04:58 +00001450bool SelectionDAGBuilder::handleSmallSwitchRange(CaseRec& CR,
1451 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001452 const Value* SV,
Dan Gohman2048b852009-11-23 18:04:58 +00001453 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001454 Case& BackCase = *(CR.Range.second-1);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001455
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001456 // Size is the number of Cases represented by this range.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001457 size_t Size = CR.Range.second - CR.Range.first;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001458 if (Size > 3)
Anton Korobeynikov23218582008-12-23 22:25:27 +00001459 return false;
1460
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001461 // Get the MachineFunction which holds the current MBB. This is used when
1462 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001463 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001464
1465 // Figure out which block is immediately after the current one.
1466 MachineBasicBlock *NextBlock = 0;
1467 MachineFunction::iterator BBI = CR.CaseBB;
1468
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001469 if (++BBI != FuncInfo.MF->end())
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001470 NextBlock = BBI;
1471
1472 // TODO: If any two of the cases has the same destination, and if one value
1473 // is the same as the other, but has one bit unset that the other has set,
1474 // use bit manipulation to do two compares at once. For example:
1475 // "if (X == 6 || X == 4)" -> "if ((X|2) == 6)"
Anton Korobeynikov23218582008-12-23 22:25:27 +00001476
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001477 // Rearrange the case blocks so that the last one falls through if possible.
1478 if (NextBlock && Default != NextBlock && BackCase.BB != NextBlock) {
1479 // The last case block won't fall through into 'NextBlock' if we emit the
1480 // branches in this order. See if rearranging a case value would help.
1481 for (CaseItr I = CR.Range.first, E = CR.Range.second-1; I != E; ++I) {
1482 if (I->BB == NextBlock) {
1483 std::swap(*I, BackCase);
1484 break;
1485 }
1486 }
1487 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001488
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001489 // Create a CaseBlock record representing a conditional branch to
1490 // the Case's target mbb if the value being switched on SV is equal
1491 // to C.
1492 MachineBasicBlock *CurBlock = CR.CaseBB;
1493 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++I) {
1494 MachineBasicBlock *FallThrough;
1495 if (I != E-1) {
1496 FallThrough = CurMF->CreateMachineBasicBlock(CurBlock->getBasicBlock());
1497 CurMF->insert(BBI, FallThrough);
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001498
1499 // Put SV in a virtual register to make it available from the new blocks.
1500 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001501 } else {
1502 // If the last case doesn't match, go to the default block.
1503 FallThrough = Default;
1504 }
1505
Dan Gohman46510a72010-04-15 01:51:59 +00001506 const Value *RHS, *LHS, *MHS;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001507 ISD::CondCode CC;
1508 if (I->High == I->Low) {
1509 // This is just small small case range :) containing exactly 1 case
1510 CC = ISD::SETEQ;
1511 LHS = SV; RHS = I->High; MHS = NULL;
1512 } else {
1513 CC = ISD::SETLE;
1514 LHS = I->Low; MHS = SV; RHS = I->High;
1515 }
1516 CaseBlock CB(CC, LHS, RHS, MHS, I->BB, FallThrough, CurBlock);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001517
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001518 // If emitting the first comparison, just call visitSwitchCase to emit the
1519 // code into the current block. Otherwise, push the CaseBlock onto the
1520 // vector to be later processed by SDISel, and insert the node's MBB
1521 // before the next MBB.
1522 if (CurBlock == CurMBB)
1523 visitSwitchCase(CB);
1524 else
1525 SwitchCases.push_back(CB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001526
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001527 CurBlock = FallThrough;
1528 }
1529
1530 return true;
1531}
1532
1533static inline bool areJTsAllowed(const TargetLowering &TLI) {
1534 return !DisableJumpTables &&
Owen Anderson825b72b2009-08-11 20:47:22 +00001535 (TLI.isOperationLegalOrCustom(ISD::BR_JT, MVT::Other) ||
1536 TLI.isOperationLegalOrCustom(ISD::BRIND, MVT::Other));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001537}
Anton Korobeynikov23218582008-12-23 22:25:27 +00001538
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001539static APInt ComputeRange(const APInt &First, const APInt &Last) {
1540 APInt LastExt(Last), FirstExt(First);
1541 uint32_t BitWidth = std::max(Last.getBitWidth(), First.getBitWidth()) + 1;
1542 LastExt.sext(BitWidth); FirstExt.sext(BitWidth);
1543 return (LastExt - FirstExt + 1ULL);
1544}
1545
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001546/// handleJTSwitchCase - Emit jumptable for current switch case range
Dan Gohman2048b852009-11-23 18:04:58 +00001547bool SelectionDAGBuilder::handleJTSwitchCase(CaseRec& CR,
1548 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001549 const Value* SV,
Dan Gohman2048b852009-11-23 18:04:58 +00001550 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001551 Case& FrontCase = *CR.Range.first;
1552 Case& BackCase = *(CR.Range.second-1);
1553
Chris Lattnere880efe2009-11-07 07:50:34 +00001554 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1555 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001556
Chris Lattnere880efe2009-11-07 07:50:34 +00001557 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001558 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1559 I!=E; ++I)
1560 TSize += I->size();
1561
Dan Gohmane0567812010-04-08 23:03:40 +00001562 if (!areJTsAllowed(TLI) || TSize.ult(4))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001563 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001564
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001565 APInt Range = ComputeRange(First, Last);
Chris Lattnere880efe2009-11-07 07:50:34 +00001566 double Density = TSize.roundToDouble() / Range.roundToDouble();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001567 if (Density < 0.4)
1568 return false;
1569
David Greene4b69d992010-01-05 01:24:57 +00001570 DEBUG(dbgs() << "Lowering jump table\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001571 << "First entry: " << First << ". Last entry: " << Last << '\n'
1572 << "Range: " << Range
1573 << "Size: " << TSize << ". Density: " << Density << "\n\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001574
1575 // Get the MachineFunction which holds the current MBB. This is used when
1576 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001577 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001578
1579 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001580 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001581 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001582
1583 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1584
1585 // Create a new basic block to hold the code for loading the address
1586 // of the jump table, and jumping to it. Update successor information;
1587 // we will either branch to the default case for the switch, or the jump
1588 // table.
1589 MachineBasicBlock *JumpTableBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1590 CurMF->insert(BBI, JumpTableBB);
1591 CR.CaseBB->addSuccessor(Default);
1592 CR.CaseBB->addSuccessor(JumpTableBB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001593
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001594 // Build a vector of destination BBs, corresponding to each target
1595 // of the jump table. If the value of the jump table slot corresponds to
1596 // a case statement, push the case's BB onto the vector, otherwise, push
1597 // the default BB.
1598 std::vector<MachineBasicBlock*> DestBBs;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001599 APInt TEI = First;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001600 for (CaseItr I = CR.Range.first, E = CR.Range.second; I != E; ++TEI) {
Chris Lattner071c62f2010-01-25 23:26:13 +00001601 const APInt &Low = cast<ConstantInt>(I->Low)->getValue();
1602 const APInt &High = cast<ConstantInt>(I->High)->getValue();
Anton Korobeynikov23218582008-12-23 22:25:27 +00001603
1604 if (Low.sle(TEI) && TEI.sle(High)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001605 DestBBs.push_back(I->BB);
1606 if (TEI==High)
1607 ++I;
1608 } else {
1609 DestBBs.push_back(Default);
1610 }
1611 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001612
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001613 // Update successor info. Add one edge to each unique successor.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001614 BitVector SuccsHandled(CR.CaseBB->getParent()->getNumBlockIDs());
1615 for (std::vector<MachineBasicBlock*>::iterator I = DestBBs.begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001616 E = DestBBs.end(); I != E; ++I) {
1617 if (!SuccsHandled[(*I)->getNumber()]) {
1618 SuccsHandled[(*I)->getNumber()] = true;
1619 JumpTableBB->addSuccessor(*I);
1620 }
1621 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001622
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001623 // Create a jump table index for this jump table.
Chris Lattner071c62f2010-01-25 23:26:13 +00001624 unsigned JTEncoding = TLI.getJumpTableEncoding();
1625 unsigned JTI = CurMF->getOrCreateJumpTableInfo(JTEncoding)
Bob Wilsond1ec31d2010-03-18 18:42:41 +00001626 ->createJumpTableIndex(DestBBs);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001627
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001628 // Set the jump table information so that we can codegen it as a second
1629 // MachineBasicBlock
1630 JumpTable JT(-1U, JTI, JumpTableBB, Default);
1631 JumpTableHeader JTH(First, Last, SV, CR.CaseBB, (CR.CaseBB == CurMBB));
1632 if (CR.CaseBB == CurMBB)
1633 visitJumpTableHeader(JT, JTH);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001634
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001635 JTCases.push_back(JumpTableBlock(JTH, JT));
1636
1637 return true;
1638}
1639
1640/// handleBTSplitSwitchCase - emit comparison and split binary search tree into
1641/// 2 subtrees.
Dan Gohman2048b852009-11-23 18:04:58 +00001642bool SelectionDAGBuilder::handleBTSplitSwitchCase(CaseRec& CR,
1643 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001644 const Value* SV,
Dan Gohman2048b852009-11-23 18:04:58 +00001645 MachineBasicBlock* Default) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001646 // Get the MachineFunction which holds the current MBB. This is used when
1647 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001648 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001649
1650 // Figure out which block is immediately after the current one.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001651 MachineFunction::iterator BBI = CR.CaseBB;
Duncan Sands51498522009-09-06 18:03:32 +00001652 ++BBI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001653
1654 Case& FrontCase = *CR.Range.first;
1655 Case& BackCase = *(CR.Range.second-1);
1656 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1657
1658 // Size is the number of Cases represented by this range.
1659 unsigned Size = CR.Range.second - CR.Range.first;
1660
Chris Lattnere880efe2009-11-07 07:50:34 +00001661 const APInt &First = cast<ConstantInt>(FrontCase.Low)->getValue();
1662 const APInt &Last = cast<ConstantInt>(BackCase.High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001663 double FMetric = 0;
1664 CaseItr Pivot = CR.Range.first + Size/2;
1665
1666 // Select optimal pivot, maximizing sum density of LHS and RHS. This will
1667 // (heuristically) allow us to emit JumpTable's later.
Chris Lattnere880efe2009-11-07 07:50:34 +00001668 APInt TSize(First.getBitWidth(), 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001669 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1670 I!=E; ++I)
1671 TSize += I->size();
1672
Chris Lattnere880efe2009-11-07 07:50:34 +00001673 APInt LSize = FrontCase.size();
1674 APInt RSize = TSize-LSize;
David Greene4b69d992010-01-05 01:24:57 +00001675 DEBUG(dbgs() << "Selecting best pivot: \n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001676 << "First: " << First << ", Last: " << Last <<'\n'
1677 << "LSize: " << LSize << ", RSize: " << RSize << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001678 for (CaseItr I = CR.Range.first, J=I+1, E = CR.Range.second;
1679 J!=E; ++I, ++J) {
Chris Lattnere880efe2009-11-07 07:50:34 +00001680 const APInt &LEnd = cast<ConstantInt>(I->High)->getValue();
1681 const APInt &RBegin = cast<ConstantInt>(J->Low)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001682 APInt Range = ComputeRange(LEnd, RBegin);
1683 assert((Range - 2ULL).isNonNegative() &&
1684 "Invalid case distance");
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001685 double LDensity = (double)LSize.roundToDouble() /
Chris Lattnere880efe2009-11-07 07:50:34 +00001686 (LEnd - First + 1ULL).roundToDouble();
1687 double RDensity = (double)RSize.roundToDouble() /
1688 (Last - RBegin + 1ULL).roundToDouble();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001689 double Metric = Range.logBase2()*(LDensity+RDensity);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001690 // Should always split in some non-trivial place
David Greene4b69d992010-01-05 01:24:57 +00001691 DEBUG(dbgs() <<"=>Step\n"
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001692 << "LEnd: " << LEnd << ", RBegin: " << RBegin << '\n'
1693 << "LDensity: " << LDensity
1694 << ", RDensity: " << RDensity << '\n'
1695 << "Metric: " << Metric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001696 if (FMetric < Metric) {
1697 Pivot = J;
1698 FMetric = Metric;
David Greene4b69d992010-01-05 01:24:57 +00001699 DEBUG(dbgs() << "Current metric set to: " << FMetric << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001700 }
1701
1702 LSize += J->size();
1703 RSize -= J->size();
1704 }
1705 if (areJTsAllowed(TLI)) {
1706 // If our case is dense we *really* should handle it earlier!
1707 assert((FMetric > 0) && "Should handle dense range earlier!");
1708 } else {
1709 Pivot = CR.Range.first + Size/2;
1710 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001711
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001712 CaseRange LHSR(CR.Range.first, Pivot);
1713 CaseRange RHSR(Pivot, CR.Range.second);
1714 Constant *C = Pivot->Low;
1715 MachineBasicBlock *FalseBB = 0, *TrueBB = 0;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001716
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001717 // We know that we branch to the LHS if the Value being switched on is
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001718 // less than the Pivot value, C. We use this to optimize our binary
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001719 // tree a bit, by recognizing that if SV is greater than or equal to the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001720 // LHS's Case Value, and that Case Value is exactly one less than the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001721 // Pivot's Value, then we can branch directly to the LHS's Target,
1722 // rather than creating a leaf node for it.
1723 if ((LHSR.second - LHSR.first) == 1 &&
1724 LHSR.first->High == CR.GE &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001725 cast<ConstantInt>(C)->getValue() ==
1726 (cast<ConstantInt>(CR.GE)->getValue() + 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001727 TrueBB = LHSR.first->BB;
1728 } else {
1729 TrueBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1730 CurMF->insert(BBI, TrueBB);
1731 WorkList.push_back(CaseRec(TrueBB, C, CR.GE, LHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001732
1733 // Put SV in a virtual register to make it available from the new blocks.
1734 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001735 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001736
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001737 // Similar to the optimization above, if the Value being switched on is
1738 // known to be less than the Constant CR.LT, and the current Case Value
1739 // is CR.LT - 1, then we can branch directly to the target block for
1740 // the current Case Value, rather than emitting a RHS leaf node for it.
1741 if ((RHSR.second - RHSR.first) == 1 && CR.LT &&
Anton Korobeynikov23218582008-12-23 22:25:27 +00001742 cast<ConstantInt>(RHSR.first->Low)->getValue() ==
1743 (cast<ConstantInt>(CR.LT)->getValue() - 1LL)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001744 FalseBB = RHSR.first->BB;
1745 } else {
1746 FalseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1747 CurMF->insert(BBI, FalseBB);
1748 WorkList.push_back(CaseRec(FalseBB,CR.LT,C,RHSR));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001749
1750 // Put SV in a virtual register to make it available from the new blocks.
1751 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001752 }
1753
1754 // Create a CaseBlock record representing a conditional branch to
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00001755 // the LHS node if the value being switched on SV is less than C.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001756 // Otherwise, branch to LHS.
1757 CaseBlock CB(ISD::SETLT, SV, C, NULL, TrueBB, FalseBB, CR.CaseBB);
1758
1759 if (CR.CaseBB == CurMBB)
1760 visitSwitchCase(CB);
1761 else
1762 SwitchCases.push_back(CB);
1763
1764 return true;
1765}
1766
1767/// handleBitTestsSwitchCase - if current case range has few destination and
1768/// range span less, than machine word bitwidth, encode case range into series
1769/// of masks and emit bit tests with these masks.
Dan Gohman2048b852009-11-23 18:04:58 +00001770bool SelectionDAGBuilder::handleBitTestsSwitchCase(CaseRec& CR,
1771 CaseRecVector& WorkList,
Dan Gohman46510a72010-04-15 01:51:59 +00001772 const Value* SV,
Dan Gohman2048b852009-11-23 18:04:58 +00001773 MachineBasicBlock* Default){
Owen Andersone50ed302009-08-10 22:56:29 +00001774 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00001775 unsigned IntPtrBits = PTy.getSizeInBits();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001776
1777 Case& FrontCase = *CR.Range.first;
1778 Case& BackCase = *(CR.Range.second-1);
1779
1780 // Get the MachineFunction which holds the current MBB. This is used when
1781 // inserting any additional MBBs necessary to represent the switch.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00001782 MachineFunction *CurMF = FuncInfo.MF;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001783
Anton Korobeynikovd34167a2009-05-08 18:51:34 +00001784 // If target does not have legal shift left, do not emit bit tests at all.
1785 if (!TLI.isOperationLegal(ISD::SHL, TLI.getPointerTy()))
1786 return false;
1787
Anton Korobeynikov23218582008-12-23 22:25:27 +00001788 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001789 for (CaseItr I = CR.Range.first, E = CR.Range.second;
1790 I!=E; ++I) {
1791 // Single case counts one, case range - two.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001792 numCmps += (I->Low == I->High ? 1 : 2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001793 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001794
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001795 // Count unique destinations
1796 SmallSet<MachineBasicBlock*, 4> Dests;
1797 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1798 Dests.insert(I->BB);
1799 if (Dests.size() > 3)
1800 // Don't bother the code below, if there are too much unique destinations
1801 return false;
1802 }
David Greene4b69d992010-01-05 01:24:57 +00001803 DEBUG(dbgs() << "Total number of unique destinations: "
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00001804 << Dests.size() << '\n'
1805 << "Total number of comparisons: " << numCmps << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001806
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001807 // Compute span of values.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001808 const APInt& minValue = cast<ConstantInt>(FrontCase.Low)->getValue();
1809 const APInt& maxValue = cast<ConstantInt>(BackCase.High)->getValue();
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001810 APInt cmpRange = maxValue - minValue;
1811
David Greene4b69d992010-01-05 01:24:57 +00001812 DEBUG(dbgs() << "Compare range: " << cmpRange << '\n'
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001813 << "Low bound: " << minValue << '\n'
1814 << "High bound: " << maxValue << '\n');
Anton Korobeynikov23218582008-12-23 22:25:27 +00001815
Dan Gohmane0567812010-04-08 23:03:40 +00001816 if (cmpRange.uge(IntPtrBits) ||
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001817 (!(Dests.size() == 1 && numCmps >= 3) &&
1818 !(Dests.size() == 2 && numCmps >= 5) &&
1819 !(Dests.size() >= 3 && numCmps >= 6)))
1820 return false;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001821
David Greene4b69d992010-01-05 01:24:57 +00001822 DEBUG(dbgs() << "Emitting bit tests\n");
Anton Korobeynikov23218582008-12-23 22:25:27 +00001823 APInt lowBound = APInt::getNullValue(cmpRange.getBitWidth());
1824
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001825 // Optimize the case where all the case values fit in a
1826 // word without having to subtract minValue. In this case,
1827 // we can optimize away the subtraction.
Dan Gohmane0567812010-04-08 23:03:40 +00001828 if (minValue.isNonNegative() && maxValue.slt(IntPtrBits)) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001829 cmpRange = maxValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001830 } else {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001831 lowBound = minValue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001832 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001833
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001834 CaseBitsVector CasesBits;
1835 unsigned i, count = 0;
1836
1837 for (CaseItr I = CR.Range.first, E = CR.Range.second; I!=E; ++I) {
1838 MachineBasicBlock* Dest = I->BB;
1839 for (i = 0; i < count; ++i)
1840 if (Dest == CasesBits[i].BB)
1841 break;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001842
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001843 if (i == count) {
1844 assert((count < 3) && "Too much destinations to test!");
1845 CasesBits.push_back(CaseBits(0, Dest, 0));
1846 count++;
1847 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001848
1849 const APInt& lowValue = cast<ConstantInt>(I->Low)->getValue();
1850 const APInt& highValue = cast<ConstantInt>(I->High)->getValue();
1851
1852 uint64_t lo = (lowValue - lowBound).getZExtValue();
1853 uint64_t hi = (highValue - lowBound).getZExtValue();
1854
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001855 for (uint64_t j = lo; j <= hi; j++) {
1856 CasesBits[i].Mask |= 1ULL << j;
1857 CasesBits[i].Bits++;
1858 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001859
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001860 }
1861 std::sort(CasesBits.begin(), CasesBits.end(), CaseBitsCmp());
Anton Korobeynikov23218582008-12-23 22:25:27 +00001862
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001863 BitTestInfo BTC;
1864
1865 // Figure out which block is immediately after the current one.
1866 MachineFunction::iterator BBI = CR.CaseBB;
1867 ++BBI;
1868
1869 const BasicBlock *LLVMBB = CR.CaseBB->getBasicBlock();
1870
David Greene4b69d992010-01-05 01:24:57 +00001871 DEBUG(dbgs() << "Cases:\n");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001872 for (unsigned i = 0, e = CasesBits.size(); i!=e; ++i) {
David Greene4b69d992010-01-05 01:24:57 +00001873 DEBUG(dbgs() << "Mask: " << CasesBits[i].Mask
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001874 << ", Bits: " << CasesBits[i].Bits
1875 << ", BB: " << CasesBits[i].BB << '\n');
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001876
1877 MachineBasicBlock *CaseBB = CurMF->CreateMachineBasicBlock(LLVMBB);
1878 CurMF->insert(BBI, CaseBB);
1879 BTC.push_back(BitTestCase(CasesBits[i].Mask,
1880 CaseBB,
1881 CasesBits[i].BB));
Dan Gohman8e5c0da2009-04-09 02:33:36 +00001882
1883 // Put SV in a virtual register to make it available from the new blocks.
1884 ExportFromCurrentBlock(SV);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001885 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001886
1887 BitTestBlock BTB(lowBound, cmpRange, SV,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001888 -1U, (CR.CaseBB == CurMBB),
1889 CR.CaseBB, Default, BTC);
1890
1891 if (CR.CaseBB == CurMBB)
1892 visitBitTestHeader(BTB);
Anton Korobeynikov23218582008-12-23 22:25:27 +00001893
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001894 BitTestCases.push_back(BTB);
1895
1896 return true;
1897}
1898
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001899/// Clusterify - Transform simple list of Cases into list of CaseRange's
Dan Gohman2048b852009-11-23 18:04:58 +00001900size_t SelectionDAGBuilder::Clusterify(CaseVector& Cases,
1901 const SwitchInst& SI) {
Anton Korobeynikov23218582008-12-23 22:25:27 +00001902 size_t numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001903
1904 // Start with "simple" cases
Anton Korobeynikov23218582008-12-23 22:25:27 +00001905 for (size_t i = 1; i < SI.getNumSuccessors(); ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001906 MachineBasicBlock *SMBB = FuncInfo.MBBMap[SI.getSuccessor(i)];
1907 Cases.push_back(Case(SI.getSuccessorValue(i),
1908 SI.getSuccessorValue(i),
1909 SMBB));
1910 }
1911 std::sort(Cases.begin(), Cases.end(), CaseCmp());
1912
1913 // Merge case into clusters
Anton Korobeynikov23218582008-12-23 22:25:27 +00001914 if (Cases.size() >= 2)
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001915 // Must recompute end() each iteration because it may be
1916 // invalidated by erase if we hold on to it
Anton Korobeynikov23218582008-12-23 22:25:27 +00001917 for (CaseItr I = Cases.begin(), J = ++(Cases.begin()); J != Cases.end(); ) {
1918 const APInt& nextValue = cast<ConstantInt>(J->Low)->getValue();
1919 const APInt& currentValue = cast<ConstantInt>(I->High)->getValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001920 MachineBasicBlock* nextBB = J->BB;
1921 MachineBasicBlock* currentBB = I->BB;
1922
1923 // If the two neighboring cases go to the same destination, merge them
1924 // into a single case.
Anton Korobeynikov23218582008-12-23 22:25:27 +00001925 if ((nextValue - currentValue == 1) && (currentBB == nextBB)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001926 I->High = J->High;
1927 J = Cases.erase(J);
1928 } else {
1929 I = J++;
1930 }
1931 }
1932
1933 for (CaseItr I=Cases.begin(), E=Cases.end(); I!=E; ++I, ++numCmps) {
1934 if (I->Low != I->High)
1935 // A range counts double, since it requires two compares.
1936 ++numCmps;
1937 }
1938
1939 return numCmps;
1940}
1941
Dan Gohman46510a72010-04-15 01:51:59 +00001942void SelectionDAGBuilder::visitSwitch(const SwitchInst &SI) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001943 // Figure out which block is immediately after the current one.
1944 MachineBasicBlock *NextBlock = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001945 MachineBasicBlock *Default = FuncInfo.MBBMap[SI.getDefaultDest()];
1946
1947 // If there is only the default destination, branch to it if it is not the
1948 // next basic block. Otherwise, just fall through.
1949 if (SI.getNumOperands() == 2) {
1950 // Update machine-CFG edges.
1951
1952 // If this is not a fall-through branch, emit the branch.
1953 CurMBB->addSuccessor(Default);
Bill Wendling4533cac2010-01-28 21:51:40 +00001954 if (Default != NextBlock)
1955 DAG.setRoot(DAG.getNode(ISD::BR, getCurDebugLoc(),
1956 MVT::Other, getControlRoot(),
1957 DAG.getBasicBlock(Default)));
Bill Wendling49fcff82009-12-21 22:30:11 +00001958
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001959 return;
1960 }
Anton Korobeynikov23218582008-12-23 22:25:27 +00001961
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001962 // If there are any non-default case statements, create a vector of Cases
1963 // representing each one, and sort the vector so that we can efficiently
1964 // create a binary search tree from them.
1965 CaseVector Cases;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001966 size_t numCmps = Clusterify(Cases, SI);
David Greene4b69d992010-01-05 01:24:57 +00001967 DEBUG(dbgs() << "Clusterify finished. Total clusters: " << Cases.size()
Anton Korobeynikov56d245b2008-12-23 22:26:18 +00001968 << ". Total compares: " << numCmps << '\n');
Devang Patel8a84e442009-01-05 17:31:22 +00001969 numCmps = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001970
1971 // Get the Value to be switched on and default basic blocks, which will be
1972 // inserted into CaseBlock records, representing basic blocks in the binary
1973 // search tree.
Dan Gohman46510a72010-04-15 01:51:59 +00001974 const Value *SV = SI.getOperand(0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001975
1976 // Push the initial CaseRec onto the worklist
1977 CaseRecVector WorkList;
1978 WorkList.push_back(CaseRec(CurMBB,0,0,CaseRange(Cases.begin(),Cases.end())));
1979
1980 while (!WorkList.empty()) {
1981 // Grab a record representing a case range to process off the worklist
1982 CaseRec CR = WorkList.back();
1983 WorkList.pop_back();
1984
1985 if (handleBitTestsSwitchCase(CR, WorkList, SV, Default))
1986 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001987
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001988 // If the range has few cases (two or less) emit a series of specific
1989 // tests.
1990 if (handleSmallSwitchRange(CR, WorkList, SV, Default))
1991 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001992
Anton Korobeynikove2f95e92008-12-23 22:26:01 +00001993 // If the switch has more than 5 blocks, and at least 40% dense, and the
1994 // target supports indirect branches, then emit a jump table rather than
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001995 // lowering the switch to a binary tree of conditional branches.
1996 if (handleJTSwitchCase(CR, WorkList, SV, Default))
1997 continue;
Anton Korobeynikov23218582008-12-23 22:25:27 +00001998
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00001999 // Emit binary tree. We need to pick a pivot, and push left and right ranges
2000 // onto the worklist. Leafs are handled via handleSmallSwitchRange() call.
2001 handleBTSplitSwitchCase(CR, WorkList, SV, Default);
2002 }
2003}
2004
Dan Gohman46510a72010-04-15 01:51:59 +00002005void SelectionDAGBuilder::visitIndirectBr(const IndirectBrInst &I) {
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002006 // Update machine-CFG edges with unique successors.
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002007 SmallVector<BasicBlock*, 32> succs;
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002008 succs.reserve(I.getNumSuccessors());
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002009 for (unsigned i = 0, e = I.getNumSuccessors(); i != e; ++i)
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002010 succs.push_back(I.getSuccessor(i));
Jakob Stoklund Olesenb5b90ed2010-02-11 18:06:56 +00002011 array_pod_sort(succs.begin(), succs.end());
Jakob Stoklund Olesen598b24c2010-02-11 00:34:18 +00002012 succs.erase(std::unique(succs.begin(), succs.end()), succs.end());
2013 for (unsigned i = 0, e = succs.size(); i != e; ++i)
2014 CurMBB->addSuccessor(FuncInfo.MBBMap[succs[i]]);
Dan Gohmaneef55dc2009-10-27 22:10:34 +00002015
Bill Wendling4533cac2010-01-28 21:51:40 +00002016 DAG.setRoot(DAG.getNode(ISD::BRIND, getCurDebugLoc(),
2017 MVT::Other, getControlRoot(),
2018 getValue(I.getAddress())));
Bill Wendling49fcff82009-12-21 22:30:11 +00002019}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002020
Dan Gohman46510a72010-04-15 01:51:59 +00002021void SelectionDAGBuilder::visitFSub(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002022 // -0.0 - X --> fneg
2023 const Type *Ty = I.getType();
Duncan Sands1df98592010-02-16 11:11:14 +00002024 if (Ty->isVectorTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002025 if (ConstantVector *CV = dyn_cast<ConstantVector>(I.getOperand(0))) {
2026 const VectorType *DestTy = cast<VectorType>(I.getType());
2027 const Type *ElTy = DestTy->getElementType();
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002028 unsigned VL = DestTy->getNumElements();
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002029 std::vector<Constant*> NZ(VL, ConstantFP::getNegativeZero(ElTy));
Owen Andersonaf7ec972009-07-28 21:19:26 +00002030 Constant *CNZ = ConstantVector::get(&NZ[0], NZ.size());
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002031 if (CV == CNZ) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002032 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002033 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2034 Op2.getValueType(), Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002035 return;
2036 }
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002037 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002038 }
Bill Wendling49fcff82009-12-21 22:30:11 +00002039
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002040 if (ConstantFP *CFP = dyn_cast<ConstantFP>(I.getOperand(0)))
Owen Anderson6f83c9c2009-07-27 20:59:43 +00002041 if (CFP->isExactlyValue(ConstantFP::getNegativeZero(Ty)->getValueAPF())) {
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002042 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002043 setValue(&I, DAG.getNode(ISD::FNEG, getCurDebugLoc(),
2044 Op2.getValueType(), Op2));
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002045 return;
2046 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002047
Dan Gohmanae3a0be2009-06-04 22:49:04 +00002048 visitBinary(I, ISD::FSUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002049}
2050
Dan Gohman46510a72010-04-15 01:51:59 +00002051void SelectionDAGBuilder::visitBinary(const User &I, unsigned OpCode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002052 SDValue Op1 = getValue(I.getOperand(0));
2053 SDValue Op2 = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00002054 setValue(&I, DAG.getNode(OpCode, getCurDebugLoc(),
2055 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002056}
2057
Dan Gohman46510a72010-04-15 01:51:59 +00002058void SelectionDAGBuilder::visitShift(const User &I, unsigned Opcode) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002059 SDValue Op1 = getValue(I.getOperand(0));
2060 SDValue Op2 = getValue(I.getOperand(1));
Duncan Sands1df98592010-02-16 11:11:14 +00002061 if (!I.getType()->isVectorTy() &&
Dan Gohman57fc82d2009-04-09 03:51:29 +00002062 Op2.getValueType() != TLI.getShiftAmountTy()) {
2063 // If the operand is smaller than the shift count type, promote it.
Owen Andersone50ed302009-08-10 22:56:29 +00002064 EVT PTy = TLI.getPointerTy();
2065 EVT STy = TLI.getShiftAmountTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002066 if (STy.bitsGT(Op2.getValueType()))
Dan Gohman57fc82d2009-04-09 03:51:29 +00002067 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
2068 TLI.getShiftAmountTy(), Op2);
2069 // If the operand is larger than the shift count type but the shift
2070 // count type has enough bits to represent any shift value, truncate
2071 // it now. This is a common case and it exposes the truncate to
2072 // optimization early.
Owen Anderson77547be2009-08-10 18:56:59 +00002073 else if (STy.getSizeInBits() >=
Dan Gohman57fc82d2009-04-09 03:51:29 +00002074 Log2_32_Ceil(Op2.getValueType().getSizeInBits()))
2075 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2076 TLI.getShiftAmountTy(), Op2);
2077 // Otherwise we'll need to temporarily settle for some other
2078 // convenient type; type legalization will make adjustments as
2079 // needed.
Owen Anderson77547be2009-08-10 18:56:59 +00002080 else if (PTy.bitsLT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002081 Op2 = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002082 TLI.getPointerTy(), Op2);
Owen Anderson77547be2009-08-10 18:56:59 +00002083 else if (PTy.bitsGT(Op2.getValueType()))
Scott Michelfdc40a02009-02-17 22:15:04 +00002084 Op2 = DAG.getNode(ISD::ANY_EXTEND, getCurDebugLoc(),
Duncan Sands92abc622009-01-31 15:50:11 +00002085 TLI.getPointerTy(), Op2);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002086 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002087
Bill Wendling4533cac2010-01-28 21:51:40 +00002088 setValue(&I, DAG.getNode(Opcode, getCurDebugLoc(),
2089 Op1.getValueType(), Op1, Op2));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002090}
2091
Dan Gohman46510a72010-04-15 01:51:59 +00002092void SelectionDAGBuilder::visitICmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002093 ICmpInst::Predicate predicate = ICmpInst::BAD_ICMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002094 if (const ICmpInst *IC = dyn_cast<ICmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002095 predicate = IC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002096 else if (const ConstantExpr *IC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002097 predicate = ICmpInst::Predicate(IC->getPredicate());
2098 SDValue Op1 = getValue(I.getOperand(0));
2099 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002100 ISD::CondCode Opcode = getICmpCondCode(predicate);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002101
Owen Andersone50ed302009-08-10 22:56:29 +00002102 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002103 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Opcode));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002104}
2105
Dan Gohman46510a72010-04-15 01:51:59 +00002106void SelectionDAGBuilder::visitFCmp(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002107 FCmpInst::Predicate predicate = FCmpInst::BAD_FCMP_PREDICATE;
Dan Gohman46510a72010-04-15 01:51:59 +00002108 if (const FCmpInst *FC = dyn_cast<FCmpInst>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002109 predicate = FC->getPredicate();
Dan Gohman46510a72010-04-15 01:51:59 +00002110 else if (const ConstantExpr *FC = dyn_cast<ConstantExpr>(&I))
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002111 predicate = FCmpInst::Predicate(FC->getPredicate());
2112 SDValue Op1 = getValue(I.getOperand(0));
2113 SDValue Op2 = getValue(I.getOperand(1));
Dan Gohman8c1a6ca2008-10-17 18:18:45 +00002114 ISD::CondCode Condition = getFCmpCondCode(predicate);
Owen Andersone50ed302009-08-10 22:56:29 +00002115 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002116 setValue(&I, DAG.getSetCC(getCurDebugLoc(), DestVT, Op1, Op2, Condition));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002117}
2118
Dan Gohman46510a72010-04-15 01:51:59 +00002119void SelectionDAGBuilder::visitSelect(const User &I) {
Owen Andersone50ed302009-08-10 22:56:29 +00002120 SmallVector<EVT, 4> ValueVTs;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002121 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2122 unsigned NumValues = ValueVTs.size();
Bill Wendling49fcff82009-12-21 22:30:11 +00002123 if (NumValues == 0) return;
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002124
Bill Wendling49fcff82009-12-21 22:30:11 +00002125 SmallVector<SDValue, 4> Values(NumValues);
2126 SDValue Cond = getValue(I.getOperand(0));
2127 SDValue TrueVal = getValue(I.getOperand(1));
2128 SDValue FalseVal = getValue(I.getOperand(2));
Dan Gohman7ea1ca62008-10-21 20:00:42 +00002129
Bill Wendling4533cac2010-01-28 21:51:40 +00002130 for (unsigned i = 0; i != NumValues; ++i)
Bill Wendling49fcff82009-12-21 22:30:11 +00002131 Values[i] = DAG.getNode(ISD::SELECT, getCurDebugLoc(),
Chris Lattnerb3e87b22010-03-12 07:15:36 +00002132 TrueVal.getNode()->getValueType(TrueVal.getResNo()+i),
2133 Cond,
Bill Wendling49fcff82009-12-21 22:30:11 +00002134 SDValue(TrueVal.getNode(),
2135 TrueVal.getResNo() + i),
2136 SDValue(FalseVal.getNode(),
2137 FalseVal.getResNo() + i));
2138
Bill Wendling4533cac2010-01-28 21:51:40 +00002139 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2140 DAG.getVTList(&ValueVTs[0], NumValues),
2141 &Values[0], NumValues));
Bill Wendling49fcff82009-12-21 22:30:11 +00002142}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002143
Dan Gohman46510a72010-04-15 01:51:59 +00002144void SelectionDAGBuilder::visitTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002145 // TruncInst cannot be a no-op cast because sizeof(src) > sizeof(dest).
2146 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002147 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002148 setValue(&I, DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002149}
2150
Dan Gohman46510a72010-04-15 01:51:59 +00002151void SelectionDAGBuilder::visitZExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002152 // ZExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2153 // ZExt also can't be a cast to bool for same reason. So, nothing much to do
2154 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002155 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002156 setValue(&I, DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002157}
2158
Dan Gohman46510a72010-04-15 01:51:59 +00002159void SelectionDAGBuilder::visitSExt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002160 // SExt cannot be a no-op cast because sizeof(src) < sizeof(dest).
2161 // SExt also can't be a cast to bool for same reason. So, nothing much to do
2162 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002163 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002164 setValue(&I, DAG.getNode(ISD::SIGN_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002165}
2166
Dan Gohman46510a72010-04-15 01:51:59 +00002167void SelectionDAGBuilder::visitFPTrunc(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002168 // FPTrunc is never a no-op cast, no need to check
2169 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002170 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002171 setValue(&I, DAG.getNode(ISD::FP_ROUND, getCurDebugLoc(),
2172 DestVT, N, DAG.getIntPtrConstant(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002173}
2174
Dan Gohman46510a72010-04-15 01:51:59 +00002175void SelectionDAGBuilder::visitFPExt(const User &I){
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002176 // FPTrunc is never a no-op cast, no need to check
2177 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002178 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002179 setValue(&I, DAG.getNode(ISD::FP_EXTEND, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002180}
2181
Dan Gohman46510a72010-04-15 01:51:59 +00002182void SelectionDAGBuilder::visitFPToUI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002183 // FPToUI is never a no-op cast, no need to check
2184 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002185 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002186 setValue(&I, DAG.getNode(ISD::FP_TO_UINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002187}
2188
Dan Gohman46510a72010-04-15 01:51:59 +00002189void SelectionDAGBuilder::visitFPToSI(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002190 // FPToSI is never a no-op cast, no need to check
2191 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002192 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002193 setValue(&I, DAG.getNode(ISD::FP_TO_SINT, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002194}
2195
Dan Gohman46510a72010-04-15 01:51:59 +00002196void SelectionDAGBuilder::visitUIToFP(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002197 // UIToFP is never a no-op cast, no need to check
2198 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002199 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002200 setValue(&I, DAG.getNode(ISD::UINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002201}
2202
Dan Gohman46510a72010-04-15 01:51:59 +00002203void SelectionDAGBuilder::visitSIToFP(const User &I){
Bill Wendling181b6272008-10-19 20:34:04 +00002204 // SIToFP is never a no-op cast, no need to check
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002205 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002206 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002207 setValue(&I, DAG.getNode(ISD::SINT_TO_FP, getCurDebugLoc(), DestVT, N));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002208}
2209
Dan Gohman46510a72010-04-15 01:51:59 +00002210void SelectionDAGBuilder::visitPtrToInt(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002211 // What to do depends on the size of the integer and the size of the pointer.
2212 // We can either truncate, zero extend, or no-op, accordingly.
2213 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002214 EVT SrcVT = N.getValueType();
2215 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002216 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002217}
2218
Dan Gohman46510a72010-04-15 01:51:59 +00002219void SelectionDAGBuilder::visitIntToPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002220 // What to do depends on the size of the integer and the size of the pointer.
2221 // We can either truncate, zero extend, or no-op, accordingly.
2222 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002223 EVT SrcVT = N.getValueType();
2224 EVT DestVT = TLI.getValueType(I.getType());
Bill Wendling4533cac2010-01-28 21:51:40 +00002225 setValue(&I, DAG.getZExtOrTrunc(N, getCurDebugLoc(), DestVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002226}
2227
Dan Gohman46510a72010-04-15 01:51:59 +00002228void SelectionDAGBuilder::visitBitCast(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002229 SDValue N = getValue(I.getOperand(0));
Owen Andersone50ed302009-08-10 22:56:29 +00002230 EVT DestVT = TLI.getValueType(I.getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002231
Bill Wendling49fcff82009-12-21 22:30:11 +00002232 // BitCast assures us that source and destination are the same size so this is
2233 // either a BIT_CONVERT or a no-op.
Bill Wendling4533cac2010-01-28 21:51:40 +00002234 if (DestVT != N.getValueType())
2235 setValue(&I, DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
2236 DestVT, N)); // convert types.
2237 else
Bill Wendling49fcff82009-12-21 22:30:11 +00002238 setValue(&I, N); // noop cast.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002239}
2240
Dan Gohman46510a72010-04-15 01:51:59 +00002241void SelectionDAGBuilder::visitInsertElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002242 SDValue InVec = getValue(I.getOperand(0));
2243 SDValue InVal = getValue(I.getOperand(1));
Scott Michelfdc40a02009-02-17 22:15:04 +00002244 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002245 TLI.getPointerTy(),
2246 getValue(I.getOperand(2)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002247 setValue(&I, DAG.getNode(ISD::INSERT_VECTOR_ELT, getCurDebugLoc(),
2248 TLI.getValueType(I.getType()),
2249 InVec, InVal, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002250}
2251
Dan Gohman46510a72010-04-15 01:51:59 +00002252void SelectionDAGBuilder::visitExtractElement(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002253 SDValue InVec = getValue(I.getOperand(0));
Scott Michelfdc40a02009-02-17 22:15:04 +00002254 SDValue InIdx = DAG.getNode(ISD::ZERO_EXTEND, getCurDebugLoc(),
Bill Wendling87710f02009-12-21 23:47:40 +00002255 TLI.getPointerTy(),
2256 getValue(I.getOperand(1)));
Bill Wendling4533cac2010-01-28 21:51:40 +00002257 setValue(&I, DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2258 TLI.getValueType(I.getType()), InVec, InIdx));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002259}
2260
Mon P Wangaeb06d22008-11-10 04:46:22 +00002261// Utility for visitShuffleVector - Returns true if the mask is mask starting
2262// from SIndx and increasing to the element length (undefs are allowed).
Nate Begeman5a5ca152009-04-29 05:20:52 +00002263static bool SequentialMask(SmallVectorImpl<int> &Mask, unsigned SIndx) {
2264 unsigned MaskNumElts = Mask.size();
2265 for (unsigned i = 0; i != MaskNumElts; ++i)
2266 if ((Mask[i] >= 0) && (Mask[i] != (int)(i + SIndx)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002267 return false;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002268 return true;
2269}
2270
Dan Gohman46510a72010-04-15 01:51:59 +00002271void SelectionDAGBuilder::visitShuffleVector(const User &I) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002272 SmallVector<int, 8> Mask;
Mon P Wang230e4fa2008-11-21 04:25:21 +00002273 SDValue Src1 = getValue(I.getOperand(0));
2274 SDValue Src2 = getValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002275
Nate Begeman9008ca62009-04-27 18:41:29 +00002276 // Convert the ConstantVector mask operand into an array of ints, with -1
2277 // representing undef values.
2278 SmallVector<Constant*, 8> MaskElts;
Chris Lattnerb29d5962010-02-01 20:48:08 +00002279 cast<Constant>(I.getOperand(2))->getVectorElements(MaskElts);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002280 unsigned MaskNumElts = MaskElts.size();
2281 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002282 if (isa<UndefValue>(MaskElts[i]))
2283 Mask.push_back(-1);
2284 else
2285 Mask.push_back(cast<ConstantInt>(MaskElts[i])->getSExtValue());
2286 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002287
Owen Andersone50ed302009-08-10 22:56:29 +00002288 EVT VT = TLI.getValueType(I.getType());
2289 EVT SrcVT = Src1.getValueType();
Nate Begeman5a5ca152009-04-29 05:20:52 +00002290 unsigned SrcNumElts = SrcVT.getVectorNumElements();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002291
Mon P Wangc7849c22008-11-16 05:06:27 +00002292 if (SrcNumElts == MaskNumElts) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002293 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2294 &Mask[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002295 return;
2296 }
2297
2298 // Normalize the shuffle vector since mask and vector length don't match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002299 if (SrcNumElts < MaskNumElts && MaskNumElts % SrcNumElts == 0) {
2300 // Mask is longer than the source vectors and is a multiple of the source
2301 // vectors. We can use concatenate vector to make the mask and vectors
Mon P Wang230e4fa2008-11-21 04:25:21 +00002302 // lengths match.
Mon P Wangc7849c22008-11-16 05:06:27 +00002303 if (SrcNumElts*2 == MaskNumElts && SequentialMask(Mask, 0)) {
2304 // The shuffle is concatenating two vectors together.
Bill Wendling4533cac2010-01-28 21:51:40 +00002305 setValue(&I, DAG.getNode(ISD::CONCAT_VECTORS, getCurDebugLoc(),
2306 VT, Src1, Src2));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002307 return;
2308 }
2309
Mon P Wangc7849c22008-11-16 05:06:27 +00002310 // Pad both vectors with undefs to make them the same length as the mask.
2311 unsigned NumConcat = MaskNumElts / SrcNumElts;
Nate Begeman9008ca62009-04-27 18:41:29 +00002312 bool Src1U = Src1.getOpcode() == ISD::UNDEF;
2313 bool Src2U = Src2.getOpcode() == ISD::UNDEF;
Dale Johannesene8d72302009-02-06 23:05:02 +00002314 SDValue UndefVal = DAG.getUNDEF(SrcVT);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002315
Nate Begeman9008ca62009-04-27 18:41:29 +00002316 SmallVector<SDValue, 8> MOps1(NumConcat, UndefVal);
2317 SmallVector<SDValue, 8> MOps2(NumConcat, UndefVal);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002318 MOps1[0] = Src1;
2319 MOps2[0] = Src2;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002320
2321 Src1 = Src1U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
2322 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002323 &MOps1[0], NumConcat);
2324 Src2 = Src2U ? DAG.getUNDEF(VT) : DAG.getNode(ISD::CONCAT_VECTORS,
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002325 getCurDebugLoc(), VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002326 &MOps2[0], NumConcat);
Mon P Wang230e4fa2008-11-21 04:25:21 +00002327
Mon P Wangaeb06d22008-11-10 04:46:22 +00002328 // Readjust mask for new input vector length.
Nate Begeman9008ca62009-04-27 18:41:29 +00002329 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002330 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002331 int Idx = Mask[i];
Nate Begeman5a5ca152009-04-29 05:20:52 +00002332 if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002333 MappedOps.push_back(Idx);
2334 else
2335 MappedOps.push_back(Idx + MaskNumElts - SrcNumElts);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002336 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002337
Bill Wendling4533cac2010-01-28 21:51:40 +00002338 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2339 &MappedOps[0]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002340 return;
2341 }
2342
Mon P Wangc7849c22008-11-16 05:06:27 +00002343 if (SrcNumElts > MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002344 // Analyze the access pattern of the vector to see if we can extract
2345 // two subvectors and do the shuffle. The analysis is done by calculating
2346 // the range of elements the mask access on both vectors.
2347 int MinRange[2] = { SrcNumElts+1, SrcNumElts+1};
2348 int MaxRange[2] = {-1, -1};
2349
Nate Begeman5a5ca152009-04-29 05:20:52 +00002350 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002351 int Idx = Mask[i];
2352 int Input = 0;
2353 if (Idx < 0)
2354 continue;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002355
Nate Begeman5a5ca152009-04-29 05:20:52 +00002356 if (Idx >= (int)SrcNumElts) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002357 Input = 1;
2358 Idx -= SrcNumElts;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002359 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002360 if (Idx > MaxRange[Input])
2361 MaxRange[Input] = Idx;
2362 if (Idx < MinRange[Input])
2363 MinRange[Input] = Idx;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002364 }
Mon P Wangaeb06d22008-11-10 04:46:22 +00002365
Mon P Wangc7849c22008-11-16 05:06:27 +00002366 // Check if the access is smaller than the vector size and can we find
2367 // a reasonable extract index.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002368 int RangeUse[2] = { 2, 2 }; // 0 = Unused, 1 = Extract, 2 = Can not
2369 // Extract.
Mon P Wangc7849c22008-11-16 05:06:27 +00002370 int StartIdx[2]; // StartIdx to extract from
2371 for (int Input=0; Input < 2; ++Input) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002372 if (MinRange[Input] == (int)(SrcNumElts+1) && MaxRange[Input] == -1) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002373 RangeUse[Input] = 0; // Unused
2374 StartIdx[Input] = 0;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002375 } else if (MaxRange[Input] - MinRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002376 // Fits within range but we should see if we can find a good
Mon P Wang230e4fa2008-11-21 04:25:21 +00002377 // start index that is a multiple of the mask length.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002378 if (MaxRange[Input] < (int)MaskNumElts) {
Mon P Wangc7849c22008-11-16 05:06:27 +00002379 RangeUse[Input] = 1; // Extract from beginning of the vector
2380 StartIdx[Input] = 0;
2381 } else {
2382 StartIdx[Input] = (MinRange[Input]/MaskNumElts)*MaskNumElts;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002383 if (MaxRange[Input] - StartIdx[Input] < (int)MaskNumElts &&
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002384 StartIdx[Input] + MaskNumElts < SrcNumElts)
Mon P Wangc7849c22008-11-16 05:06:27 +00002385 RangeUse[Input] = 1; // Extract from a multiple of the mask length.
Mon P Wangc7849c22008-11-16 05:06:27 +00002386 }
Mon P Wang230e4fa2008-11-21 04:25:21 +00002387 }
Mon P Wangc7849c22008-11-16 05:06:27 +00002388 }
2389
Bill Wendling636e2582009-08-21 18:16:06 +00002390 if (RangeUse[0] == 0 && RangeUse[1] == 0) {
Bill Wendling4533cac2010-01-28 21:51:40 +00002391 setValue(&I, DAG.getUNDEF(VT)); // Vectors are not used.
Mon P Wangc7849c22008-11-16 05:06:27 +00002392 return;
2393 }
2394 else if (RangeUse[0] < 2 && RangeUse[1] < 2) {
2395 // Extract appropriate subvector and generate a vector shuffle
2396 for (int Input=0; Input < 2; ++Input) {
Bill Wendling87710f02009-12-21 23:47:40 +00002397 SDValue &Src = Input == 0 ? Src1 : Src2;
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002398 if (RangeUse[Input] == 0)
Dale Johannesene8d72302009-02-06 23:05:02 +00002399 Src = DAG.getUNDEF(VT);
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002400 else
Dale Johannesen66978ee2009-01-31 02:22:37 +00002401 Src = DAG.getNode(ISD::EXTRACT_SUBVECTOR, getCurDebugLoc(), VT,
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002402 Src, DAG.getIntPtrConstant(StartIdx[Input]));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002403 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002404
Mon P Wangc7849c22008-11-16 05:06:27 +00002405 // Calculate new mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00002406 SmallVector<int, 8> MappedOps;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002407 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002408 int Idx = Mask[i];
2409 if (Idx < 0)
2410 MappedOps.push_back(Idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002411 else if (Idx < (int)SrcNumElts)
Nate Begeman9008ca62009-04-27 18:41:29 +00002412 MappedOps.push_back(Idx - StartIdx[0]);
2413 else
2414 MappedOps.push_back(Idx - SrcNumElts - StartIdx[1] + MaskNumElts);
Mon P Wangc7849c22008-11-16 05:06:27 +00002415 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002416
Bill Wendling4533cac2010-01-28 21:51:40 +00002417 setValue(&I, DAG.getVectorShuffle(VT, getCurDebugLoc(), Src1, Src2,
2418 &MappedOps[0]));
Mon P Wangc7849c22008-11-16 05:06:27 +00002419 return;
Mon P Wangaeb06d22008-11-10 04:46:22 +00002420 }
2421 }
2422
Mon P Wangc7849c22008-11-16 05:06:27 +00002423 // We can't use either concat vectors or extract subvectors so fall back to
2424 // replacing the shuffle with extract and build vector.
2425 // to insert and build vector.
Owen Andersone50ed302009-08-10 22:56:29 +00002426 EVT EltVT = VT.getVectorElementType();
2427 EVT PtrVT = TLI.getPointerTy();
Mon P Wangaeb06d22008-11-10 04:46:22 +00002428 SmallVector<SDValue,8> Ops;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002429 for (unsigned i = 0; i != MaskNumElts; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002430 if (Mask[i] < 0) {
Dale Johannesene8d72302009-02-06 23:05:02 +00002431 Ops.push_back(DAG.getUNDEF(EltVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002432 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002433 int Idx = Mask[i];
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002434 SDValue Res;
2435
Nate Begeman5a5ca152009-04-29 05:20:52 +00002436 if (Idx < (int)SrcNumElts)
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002437 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2438 EltVT, Src1, DAG.getConstant(Idx, PtrVT));
Mon P Wangaeb06d22008-11-10 04:46:22 +00002439 else
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002440 Res = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, getCurDebugLoc(),
2441 EltVT, Src2,
2442 DAG.getConstant(Idx - SrcNumElts, PtrVT));
2443
2444 Ops.push_back(Res);
Mon P Wangaeb06d22008-11-10 04:46:22 +00002445 }
2446 }
Bill Wendlingb85b6e82009-12-21 22:42:14 +00002447
Bill Wendling4533cac2010-01-28 21:51:40 +00002448 setValue(&I, DAG.getNode(ISD::BUILD_VECTOR, getCurDebugLoc(),
2449 VT, &Ops[0], Ops.size()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002450}
2451
Dan Gohman46510a72010-04-15 01:51:59 +00002452void SelectionDAGBuilder::visitInsertValue(const InsertValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002453 const Value *Op0 = I.getOperand(0);
2454 const Value *Op1 = I.getOperand(1);
2455 const Type *AggTy = I.getType();
2456 const Type *ValTy = Op1->getType();
2457 bool IntoUndef = isa<UndefValue>(Op0);
2458 bool FromUndef = isa<UndefValue>(Op1);
2459
2460 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2461 I.idx_begin(), I.idx_end());
2462
Owen Andersone50ed302009-08-10 22:56:29 +00002463 SmallVector<EVT, 4> AggValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002464 ComputeValueVTs(TLI, AggTy, AggValueVTs);
Owen Andersone50ed302009-08-10 22:56:29 +00002465 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002466 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2467
2468 unsigned NumAggValues = AggValueVTs.size();
2469 unsigned NumValValues = ValValueVTs.size();
2470 SmallVector<SDValue, 4> Values(NumAggValues);
2471
2472 SDValue Agg = getValue(Op0);
2473 SDValue Val = getValue(Op1);
2474 unsigned i = 0;
2475 // Copy the beginning value(s) from the original aggregate.
2476 for (; i != LinearIndex; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002477 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002478 SDValue(Agg.getNode(), Agg.getResNo() + i);
2479 // Copy values from the inserted value(s).
2480 for (; i != LinearIndex + NumValValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002481 Values[i] = FromUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002482 SDValue(Val.getNode(), Val.getResNo() + i - LinearIndex);
2483 // Copy remaining value(s) from the original aggregate.
2484 for (; i != NumAggValues; ++i)
Dale Johannesene8d72302009-02-06 23:05:02 +00002485 Values[i] = IntoUndef ? DAG.getUNDEF(AggValueVTs[i]) :
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002486 SDValue(Agg.getNode(), Agg.getResNo() + i);
2487
Bill Wendling4533cac2010-01-28 21:51:40 +00002488 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2489 DAG.getVTList(&AggValueVTs[0], NumAggValues),
2490 &Values[0], NumAggValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002491}
2492
Dan Gohman46510a72010-04-15 01:51:59 +00002493void SelectionDAGBuilder::visitExtractValue(const ExtractValueInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002494 const Value *Op0 = I.getOperand(0);
2495 const Type *AggTy = Op0->getType();
2496 const Type *ValTy = I.getType();
2497 bool OutOfUndef = isa<UndefValue>(Op0);
2498
2499 unsigned LinearIndex = ComputeLinearIndex(TLI, AggTy,
2500 I.idx_begin(), I.idx_end());
2501
Owen Andersone50ed302009-08-10 22:56:29 +00002502 SmallVector<EVT, 4> ValValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002503 ComputeValueVTs(TLI, ValTy, ValValueVTs);
2504
2505 unsigned NumValValues = ValValueVTs.size();
2506 SmallVector<SDValue, 4> Values(NumValValues);
2507
2508 SDValue Agg = getValue(Op0);
2509 // Copy out the selected value(s).
2510 for (unsigned i = LinearIndex; i != LinearIndex + NumValValues; ++i)
2511 Values[i - LinearIndex] =
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002512 OutOfUndef ?
Dale Johannesene8d72302009-02-06 23:05:02 +00002513 DAG.getUNDEF(Agg.getNode()->getValueType(Agg.getResNo() + i)) :
Bill Wendlingf0a2d0c2008-11-20 07:24:30 +00002514 SDValue(Agg.getNode(), Agg.getResNo() + i);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002515
Bill Wendling4533cac2010-01-28 21:51:40 +00002516 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2517 DAG.getVTList(&ValValueVTs[0], NumValValues),
2518 &Values[0], NumValValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002519}
2520
Dan Gohman46510a72010-04-15 01:51:59 +00002521void SelectionDAGBuilder::visitGetElementPtr(const User &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002522 SDValue N = getValue(I.getOperand(0));
2523 const Type *Ty = I.getOperand(0)->getType();
2524
Dan Gohman46510a72010-04-15 01:51:59 +00002525 for (GetElementPtrInst::const_op_iterator OI = I.op_begin()+1, E = I.op_end();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002526 OI != E; ++OI) {
Dan Gohman46510a72010-04-15 01:51:59 +00002527 const Value *Idx = *OI;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002528 if (const StructType *StTy = dyn_cast<StructType>(Ty)) {
2529 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2530 if (Field) {
2531 // N = N + Offset
2532 uint64_t Offset = TD->getStructLayout(StTy)->getElementOffset(Field);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002533 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002534 DAG.getIntPtrConstant(Offset));
2535 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002536
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002537 Ty = StTy->getElementType(Field);
Chris Lattner93b122d2010-03-16 21:25:55 +00002538 } else if (const UnionType *UnTy = dyn_cast<UnionType>(Ty)) {
2539 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
2540
2541 // Offset canonically 0 for unions, but type changes
2542 Ty = UnTy->getElementType(Field);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002543 } else {
2544 Ty = cast<SequentialType>(Ty)->getElementType();
2545
2546 // If this is a constant subscript, handle it quickly.
Dan Gohman46510a72010-04-15 01:51:59 +00002547 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Idx)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002548 if (CI->getZExtValue() == 0) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002549 uint64_t Offs =
Duncan Sands777d2302009-05-09 07:06:46 +00002550 TD->getTypeAllocSize(Ty)*cast<ConstantInt>(CI)->getSExtValue();
Evan Cheng65b52df2009-02-09 21:01:06 +00002551 SDValue OffsVal;
Owen Andersone50ed302009-08-10 22:56:29 +00002552 EVT PTy = TLI.getPointerTy();
Owen Anderson77547be2009-08-10 18:56:59 +00002553 unsigned PtrBits = PTy.getSizeInBits();
Bill Wendlinge1a90422009-12-21 23:10:19 +00002554 if (PtrBits < 64)
Evan Cheng65b52df2009-02-09 21:01:06 +00002555 OffsVal = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(),
2556 TLI.getPointerTy(),
Owen Anderson825b72b2009-08-11 20:47:22 +00002557 DAG.getConstant(Offs, MVT::i64));
Bill Wendlinge1a90422009-12-21 23:10:19 +00002558 else
Evan Chengb1032a82009-02-09 20:54:38 +00002559 OffsVal = DAG.getIntPtrConstant(Offs);
Bill Wendlinge1a90422009-12-21 23:10:19 +00002560
Dale Johannesen66978ee2009-01-31 02:22:37 +00002561 N = DAG.getNode(ISD::ADD, getCurDebugLoc(), N.getValueType(), N,
Evan Chengb1032a82009-02-09 20:54:38 +00002562 OffsVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002563 continue;
2564 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002565
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002566 // N = N + Idx * ElementSize;
Dan Gohman7abbd042009-10-23 17:57:43 +00002567 APInt ElementSize = APInt(TLI.getPointerTy().getSizeInBits(),
2568 TD->getTypeAllocSize(Ty));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002569 SDValue IdxN = getValue(Idx);
2570
2571 // If the index is smaller or larger than intptr_t, truncate or extend
2572 // it.
Duncan Sands3a66a682009-10-13 21:04:12 +00002573 IdxN = DAG.getSExtOrTrunc(IdxN, getCurDebugLoc(), N.getValueType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002574
2575 // If this is a multiply by a power of two, turn it into a shl
2576 // immediately. This is a very common case.
2577 if (ElementSize != 1) {
Dan Gohman7abbd042009-10-23 17:57:43 +00002578 if (ElementSize.isPowerOf2()) {
2579 unsigned Amt = ElementSize.logBase2();
Scott Michelfdc40a02009-02-17 22:15:04 +00002580 IdxN = DAG.getNode(ISD::SHL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002581 N.getValueType(), IdxN,
Duncan Sands92abc622009-01-31 15:50:11 +00002582 DAG.getConstant(Amt, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002583 } else {
Dan Gohman7abbd042009-10-23 17:57:43 +00002584 SDValue Scale = DAG.getConstant(ElementSize, TLI.getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00002585 IdxN = DAG.getNode(ISD::MUL, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002586 N.getValueType(), IdxN, Scale);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002587 }
2588 }
2589
Scott Michelfdc40a02009-02-17 22:15:04 +00002590 N = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002591 N.getValueType(), N, IdxN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002592 }
2593 }
Bill Wendlinge1a90422009-12-21 23:10:19 +00002594
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002595 setValue(&I, N);
2596}
2597
Dan Gohman46510a72010-04-15 01:51:59 +00002598void SelectionDAGBuilder::visitAlloca(const AllocaInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002599 // If this is a fixed sized alloca in the entry block of the function,
2600 // allocate it statically on the stack.
2601 if (FuncInfo.StaticAllocaMap.count(&I))
2602 return; // getValue will auto-populate this.
2603
2604 const Type *Ty = I.getAllocatedType();
Duncan Sands777d2302009-05-09 07:06:46 +00002605 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002606 unsigned Align =
2607 std::max((unsigned)TLI.getTargetData()->getPrefTypeAlignment(Ty),
2608 I.getAlignment());
2609
2610 SDValue AllocSize = getValue(I.getArraySize());
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002611
Chris Lattner0b18e592009-03-17 19:36:00 +00002612 AllocSize = DAG.getNode(ISD::MUL, getCurDebugLoc(), AllocSize.getValueType(),
2613 AllocSize,
2614 DAG.getConstant(TySize, AllocSize.getValueType()));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002615
Owen Andersone50ed302009-08-10 22:56:29 +00002616 EVT IntPtr = TLI.getPointerTy();
Duncan Sands3a66a682009-10-13 21:04:12 +00002617 AllocSize = DAG.getZExtOrTrunc(AllocSize, getCurDebugLoc(), IntPtr);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00002618
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002619 // Handle alignment. If the requested alignment is less than or equal to
2620 // the stack alignment, ignore it. If the size is greater than or equal to
2621 // the stack alignment, we note this in the DYNAMIC_STACKALLOC node.
Dan Gohman55e59c12010-04-19 19:05:59 +00002622 unsigned StackAlign = TM.getFrameInfo()->getStackAlignment();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002623 if (Align <= StackAlign)
2624 Align = 0;
2625
2626 // Round the size of the allocation up to the stack alignment size
2627 // by add SA-1 to the size.
Scott Michelfdc40a02009-02-17 22:15:04 +00002628 AllocSize = DAG.getNode(ISD::ADD, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002629 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002630 DAG.getIntPtrConstant(StackAlign-1));
Bill Wendling856ff412009-12-22 00:12:37 +00002631
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002632 // Mask out the low bits for alignment purposes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002633 AllocSize = DAG.getNode(ISD::AND, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002634 AllocSize.getValueType(), AllocSize,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002635 DAG.getIntPtrConstant(~(uint64_t)(StackAlign-1)));
2636
2637 SDValue Ops[] = { getRoot(), AllocSize, DAG.getIntPtrConstant(Align) };
Owen Anderson825b72b2009-08-11 20:47:22 +00002638 SDVTList VTs = DAG.getVTList(AllocSize.getValueType(), MVT::Other);
Scott Michelfdc40a02009-02-17 22:15:04 +00002639 SDValue DSA = DAG.getNode(ISD::DYNAMIC_STACKALLOC, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002640 VTs, Ops, 3);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002641 setValue(&I, DSA);
2642 DAG.setRoot(DSA.getValue(1));
Bill Wendling856ff412009-12-22 00:12:37 +00002643
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002644 // Inform the Frame Information that we have just allocated a variable-sized
2645 // object.
Dan Gohman0d24bfb2009-08-15 02:06:22 +00002646 FuncInfo.MF->getFrameInfo()->CreateVariableSizedObject();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002647}
2648
Dan Gohman46510a72010-04-15 01:51:59 +00002649void SelectionDAGBuilder::visitLoad(const LoadInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002650 const Value *SV = I.getOperand(0);
2651 SDValue Ptr = getValue(SV);
2652
2653 const Type *Ty = I.getType();
David Greene1e559442010-02-15 17:00:31 +00002654
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002655 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002656 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002657 unsigned Alignment = I.getAlignment();
2658
Owen Andersone50ed302009-08-10 22:56:29 +00002659 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002660 SmallVector<uint64_t, 4> Offsets;
2661 ComputeValueVTs(TLI, Ty, ValueVTs, &Offsets);
2662 unsigned NumValues = ValueVTs.size();
2663 if (NumValues == 0)
2664 return;
2665
2666 SDValue Root;
2667 bool ConstantMemory = false;
2668 if (I.isVolatile())
2669 // Serialize volatile loads with other side effects.
2670 Root = getRoot();
2671 else if (AA->pointsToConstantMemory(SV)) {
2672 // Do not serialize (non-volatile) loads of constant memory with anything.
2673 Root = DAG.getEntryNode();
2674 ConstantMemory = true;
2675 } else {
2676 // Do not serialize non-volatile loads against each other.
2677 Root = DAG.getRoot();
2678 }
2679
2680 SmallVector<SDValue, 4> Values(NumValues);
2681 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002682 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002683 for (unsigned i = 0; i != NumValues; ++i) {
Bill Wendling856ff412009-12-22 00:12:37 +00002684 SDValue A = DAG.getNode(ISD::ADD, getCurDebugLoc(),
2685 PtrVT, Ptr,
2686 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002687 SDValue L = DAG.getLoad(ValueVTs[i], getCurDebugLoc(), Root,
David Greene1e559442010-02-15 17:00:31 +00002688 A, SV, Offsets[i], isVolatile,
2689 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002690
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002691 Values[i] = L;
2692 Chains[i] = L.getValue(1);
2693 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002694
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002695 if (!ConstantMemory) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002696 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
Bill Wendling856ff412009-12-22 00:12:37 +00002697 MVT::Other, &Chains[0], NumValues);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002698 if (isVolatile)
2699 DAG.setRoot(Chain);
2700 else
2701 PendingLoads.push_back(Chain);
2702 }
2703
Bill Wendling4533cac2010-01-28 21:51:40 +00002704 setValue(&I, DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
2705 DAG.getVTList(&ValueVTs[0], NumValues),
2706 &Values[0], NumValues));
Bill Wendling856ff412009-12-22 00:12:37 +00002707}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002708
Dan Gohman46510a72010-04-15 01:51:59 +00002709void SelectionDAGBuilder::visitStore(const StoreInst &I) {
2710 const Value *SrcV = I.getOperand(0);
2711 const Value *PtrV = I.getOperand(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002712
Owen Andersone50ed302009-08-10 22:56:29 +00002713 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002714 SmallVector<uint64_t, 4> Offsets;
2715 ComputeValueVTs(TLI, SrcV->getType(), ValueVTs, &Offsets);
2716 unsigned NumValues = ValueVTs.size();
2717 if (NumValues == 0)
2718 return;
2719
2720 // Get the lowered operands. Note that we do this after
2721 // checking if NumResults is zero, because with zero results
2722 // the operands won't have values in the map.
2723 SDValue Src = getValue(SrcV);
2724 SDValue Ptr = getValue(PtrV);
2725
2726 SDValue Root = getRoot();
2727 SmallVector<SDValue, 4> Chains(NumValues);
Owen Andersone50ed302009-08-10 22:56:29 +00002728 EVT PtrVT = Ptr.getValueType();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002729 bool isVolatile = I.isVolatile();
David Greene1e559442010-02-15 17:00:31 +00002730 bool isNonTemporal = I.getMetadata("nontemporal") != 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002731 unsigned Alignment = I.getAlignment();
Bill Wendling856ff412009-12-22 00:12:37 +00002732
2733 for (unsigned i = 0; i != NumValues; ++i) {
2734 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT, Ptr,
2735 DAG.getConstant(Offsets[i], PtrVT));
Dale Johannesen66978ee2009-01-31 02:22:37 +00002736 Chains[i] = DAG.getStore(Root, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00002737 SDValue(Src.getNode(), Src.getResNo() + i),
David Greene1e559442010-02-15 17:00:31 +00002738 Add, PtrV, Offsets[i], isVolatile,
2739 isNonTemporal, Alignment);
Bill Wendling856ff412009-12-22 00:12:37 +00002740 }
2741
Bill Wendling4533cac2010-01-28 21:51:40 +00002742 DAG.setRoot(DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
2743 MVT::Other, &Chains[0], NumValues));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002744}
2745
2746/// visitTargetIntrinsic - Lower a call of a target intrinsic to an INTRINSIC
2747/// node.
Dan Gohman46510a72010-04-15 01:51:59 +00002748void SelectionDAGBuilder::visitTargetIntrinsic(const CallInst &I,
Dan Gohman2048b852009-11-23 18:04:58 +00002749 unsigned Intrinsic) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002750 bool HasChain = !I.doesNotAccessMemory();
2751 bool OnlyLoad = HasChain && I.onlyReadsMemory();
2752
2753 // Build the operand list.
2754 SmallVector<SDValue, 8> Ops;
2755 if (HasChain) { // If this intrinsic has side-effects, chainify it.
2756 if (OnlyLoad) {
2757 // We don't need to serialize loads against other loads.
2758 Ops.push_back(DAG.getRoot());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002759 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002760 Ops.push_back(getRoot());
2761 }
2762 }
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002763
2764 // Info is set by getTgtMemInstrinsic
2765 TargetLowering::IntrinsicInfo Info;
2766 bool IsTgtIntrinsic = TLI.getTgtMemIntrinsic(Info, I, Intrinsic);
2767
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002768 // Add the intrinsic ID as an integer operand if it's not a target intrinsic.
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002769 if (!IsTgtIntrinsic)
2770 Ops.push_back(DAG.getConstant(Intrinsic, TLI.getPointerTy()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002771
2772 // Add all operands of the call to the operand list.
Eric Christopher551754c2010-04-16 23:37:20 +00002773 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002774 SDValue Op = getValue(I.getOperand(i));
2775 assert(TLI.isTypeLegal(Op.getValueType()) &&
2776 "Intrinsic uses a non-legal type?");
2777 Ops.push_back(Op);
2778 }
2779
Owen Andersone50ed302009-08-10 22:56:29 +00002780 SmallVector<EVT, 4> ValueVTs;
Bob Wilson8d919552009-07-31 22:41:21 +00002781 ComputeValueVTs(TLI, I.getType(), ValueVTs);
2782#ifndef NDEBUG
2783 for (unsigned Val = 0, E = ValueVTs.size(); Val != E; ++Val) {
2784 assert(TLI.isTypeLegal(ValueVTs[Val]) &&
2785 "Intrinsic uses a non-legal type?");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002786 }
Bob Wilson8d919552009-07-31 22:41:21 +00002787#endif // NDEBUG
Bill Wendling856ff412009-12-22 00:12:37 +00002788
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002789 if (HasChain)
Owen Anderson825b72b2009-08-11 20:47:22 +00002790 ValueVTs.push_back(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002791
Bob Wilson8d919552009-07-31 22:41:21 +00002792 SDVTList VTs = DAG.getVTList(ValueVTs.data(), ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002793
2794 // Create the node.
2795 SDValue Result;
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002796 if (IsTgtIntrinsic) {
2797 // This is target intrinsic that touches memory
Dale Johannesen66978ee2009-01-31 02:22:37 +00002798 Result = DAG.getMemIntrinsicNode(Info.opc, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002799 VTs, &Ops[0], Ops.size(),
Mon P Wang3efcd4a2008-11-01 20:24:53 +00002800 Info.memVT, Info.ptrVal, Info.offset,
2801 Info.align, Info.vol,
2802 Info.readMem, Info.writeMem);
Bill Wendling856ff412009-12-22 00:12:37 +00002803 } else if (!HasChain) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002804 Result = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002805 VTs, &Ops[0], Ops.size());
Benjamin Kramerf0127052010-01-05 13:12:22 +00002806 } else if (!I.getType()->isVoidTy()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002807 Result = DAG.getNode(ISD::INTRINSIC_W_CHAIN, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002808 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002809 } else {
Scott Michelfdc40a02009-02-17 22:15:04 +00002810 Result = DAG.getNode(ISD::INTRINSIC_VOID, getCurDebugLoc(),
Dan Gohmanfc166572009-04-09 23:54:40 +00002811 VTs, &Ops[0], Ops.size());
Bill Wendling856ff412009-12-22 00:12:37 +00002812 }
2813
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002814 if (HasChain) {
2815 SDValue Chain = Result.getValue(Result.getNode()->getNumValues()-1);
2816 if (OnlyLoad)
2817 PendingLoads.push_back(Chain);
2818 else
2819 DAG.setRoot(Chain);
2820 }
Bill Wendling856ff412009-12-22 00:12:37 +00002821
Benjamin Kramerf0127052010-01-05 13:12:22 +00002822 if (!I.getType()->isVoidTy()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002823 if (const VectorType *PTy = dyn_cast<VectorType>(I.getType())) {
Owen Andersone50ed302009-08-10 22:56:29 +00002824 EVT VT = TLI.getValueType(PTy);
Dale Johannesen66978ee2009-01-31 02:22:37 +00002825 Result = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(), VT, Result);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002826 }
Bill Wendling856ff412009-12-22 00:12:37 +00002827
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002828 setValue(&I, Result);
2829 }
2830}
2831
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002832/// GetSignificand - Get the significand and build it into a floating-point
2833/// number with exponent of 1:
2834///
2835/// Op = (Op & 0x007fffff) | 0x3f800000;
2836///
2837/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002838static SDValue
Bill Wendling46ada192010-03-02 01:55:18 +00002839GetSignificand(SelectionDAG &DAG, SDValue Op, DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002840 SDValue t1 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2841 DAG.getConstant(0x007fffff, MVT::i32));
2842 SDValue t2 = DAG.getNode(ISD::OR, dl, MVT::i32, t1,
2843 DAG.getConstant(0x3f800000, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002844 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002845}
2846
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002847/// GetExponent - Get the exponent:
2848///
Bill Wendlinge9a72862009-01-20 21:17:57 +00002849/// (float)(int)(((Op & 0x7f800000) >> 23) - 127);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002850///
2851/// where Op is the hexidecimal representation of floating point value.
Bill Wendling39150252008-09-09 20:39:27 +00002852static SDValue
Dale Johannesen66978ee2009-01-31 02:22:37 +00002853GetExponent(SelectionDAG &DAG, SDValue Op, const TargetLowering &TLI,
Bill Wendling46ada192010-03-02 01:55:18 +00002854 DebugLoc dl) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002855 SDValue t0 = DAG.getNode(ISD::AND, dl, MVT::i32, Op,
2856 DAG.getConstant(0x7f800000, MVT::i32));
2857 SDValue t1 = DAG.getNode(ISD::SRL, dl, MVT::i32, t0,
Duncan Sands92abc622009-01-31 15:50:11 +00002858 DAG.getConstant(23, TLI.getPointerTy()));
Owen Anderson825b72b2009-08-11 20:47:22 +00002859 SDValue t2 = DAG.getNode(ISD::SUB, dl, MVT::i32, t1,
2860 DAG.getConstant(127, MVT::i32));
Bill Wendling4533cac2010-01-28 21:51:40 +00002861 return DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, t2);
Bill Wendling39150252008-09-09 20:39:27 +00002862}
2863
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002864/// getF32Constant - Get 32-bit floating point constant.
2865static SDValue
2866getF32Constant(SelectionDAG &DAG, unsigned Flt) {
Owen Anderson825b72b2009-08-11 20:47:22 +00002867 return DAG.getConstantFP(APFloat(APInt(32, Flt)), MVT::f32);
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002868}
2869
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002870/// Inlined utility function to implement binary input atomic intrinsics for
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002871/// visitIntrinsicCall: I is a call instruction
2872/// Op is the associated NodeType for I
2873const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002874SelectionDAGBuilder::implVisitBinaryAtomic(const CallInst& I,
2875 ISD::NodeType Op) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002876 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002877 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00002878 DAG.getAtomic(Op, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00002879 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00002880 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00002881 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00002882 getValue(I.getOperand(2)),
2883 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00002884 setValue(&I, L);
2885 DAG.setRoot(L.getValue(1));
2886 return 0;
2887}
2888
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002889// implVisitAluOverflow - Lower arithmetic overflow instrinsics.
Bill Wendling74c37652008-12-09 22:08:41 +00002890const char *
Dan Gohman46510a72010-04-15 01:51:59 +00002891SelectionDAGBuilder::implVisitAluOverflow(const CallInst &I, ISD::NodeType Op) {
Eric Christopher551754c2010-04-16 23:37:20 +00002892 SDValue Op1 = getValue(I.getOperand(1));
2893 SDValue Op2 = getValue(I.getOperand(2));
Bill Wendling74c37652008-12-09 22:08:41 +00002894
Owen Anderson825b72b2009-08-11 20:47:22 +00002895 SDVTList VTs = DAG.getVTList(Op1.getValueType(), MVT::i1);
Bill Wendling4533cac2010-01-28 21:51:40 +00002896 setValue(&I, DAG.getNode(Op, getCurDebugLoc(), VTs, Op1, Op2));
Bill Wendling2ce4e5c2008-12-10 00:28:22 +00002897 return 0;
2898}
Bill Wendling74c37652008-12-09 22:08:41 +00002899
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002900/// visitExp - Lower an exp intrinsic. Handles the special sequences for
2901/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00002902void
Dan Gohman46510a72010-04-15 01:51:59 +00002903SelectionDAGBuilder::visitExp(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00002904 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00002905 DebugLoc dl = getCurDebugLoc();
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002906
Eric Christopher551754c2010-04-16 23:37:20 +00002907 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002908 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00002909 SDValue Op = getValue(I.getOperand(1));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002910
2911 // Put the exponent in the right bit position for later addition to the
2912 // final result:
2913 //
2914 // #define LOG2OFe 1.4426950f
2915 // IntegerPartOfX = ((int32_t)(X * LOG2OFe));
Owen Anderson825b72b2009-08-11 20:47:22 +00002916 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002917 getF32Constant(DAG, 0x3fb8aa3b));
Owen Anderson825b72b2009-08-11 20:47:22 +00002918 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002919
2920 // FractionalPartOfX = (X * LOG2OFe) - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00002921 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
2922 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002923
2924 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00002925 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00002926 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendling856ff412009-12-22 00:12:37 +00002927
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002928 if (LimitFloatPrecision <= 6) {
2929 // For floating-point precision of 6:
2930 //
2931 // TwoToFractionalPartOfX =
2932 // 0.997535578f +
2933 // (0.735607626f + 0.252464424f * x) * x;
2934 //
2935 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002936 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002937 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00002938 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002939 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00002940 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2941 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002942 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002943 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t5);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002944
2945 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002946 SDValue t6 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002947 TwoToFracPartOfX, IntegerPartOfX);
2948
Owen Anderson825b72b2009-08-11 20:47:22 +00002949 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t6);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002950 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
2951 // For floating-point precision of 12:
2952 //
2953 // TwoToFractionalPartOfX =
2954 // 0.999892986f +
2955 // (0.696457318f +
2956 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
2957 //
2958 // 0.000107046256 error, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002959 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002960 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002961 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002962 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00002963 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2964 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002965 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00002966 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2967 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002968 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00002969 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,MVT::i32, t7);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002970
2971 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00002972 SDValue t8 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002973 TwoToFracPartOfX, IntegerPartOfX);
2974
Owen Anderson825b72b2009-08-11 20:47:22 +00002975 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t8);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00002976 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
2977 // For floating-point precision of 18:
2978 //
2979 // TwoToFractionalPartOfX =
2980 // 0.999999982f +
2981 // (0.693148872f +
2982 // (0.240227044f +
2983 // (0.554906021e-1f +
2984 // (0.961591928e-2f +
2985 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
2986 //
2987 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00002988 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002989 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00002990 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002991 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00002992 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
2993 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002994 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00002995 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
2996 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00002997 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00002998 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
2999 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003000 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003001 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3002 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003003 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003004 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3005 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003006 getF32Constant(DAG, 0x3f800000));
Scott Michelfdc40a02009-02-17 22:15:04 +00003007 SDValue TwoToFracPartOfX = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003008 MVT::i32, t13);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003009
3010 // Add the exponent into the result in integer domain.
Owen Anderson825b72b2009-08-11 20:47:22 +00003011 SDValue t14 = DAG.getNode(ISD::ADD, dl, MVT::i32,
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003012 TwoToFracPartOfX, IntegerPartOfX);
3013
Owen Anderson825b72b2009-08-11 20:47:22 +00003014 result = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, t14);
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003015 }
3016 } else {
3017 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003018 result = DAG.getNode(ISD::FEXP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003019 getValue(I.getOperand(1)).getValueType(),
3020 getValue(I.getOperand(1)));
Bill Wendlingb4ec2832008-09-09 22:13:54 +00003021 }
3022
Dale Johannesen59e577f2008-09-05 18:38:42 +00003023 setValue(&I, result);
3024}
3025
Bill Wendling39150252008-09-09 20:39:27 +00003026/// visitLog - Lower a log intrinsic. Handles the special sequences for
3027/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003028void
Dan Gohman46510a72010-04-15 01:51:59 +00003029SelectionDAGBuilder::visitLog(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003030 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003031 DebugLoc dl = getCurDebugLoc();
Bill Wendling39150252008-09-09 20:39:27 +00003032
Eric Christopher551754c2010-04-16 23:37:20 +00003033 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling39150252008-09-09 20:39:27 +00003034 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003035 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003036 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling39150252008-09-09 20:39:27 +00003037
3038 // Scale the exponent by log(2) [0.69314718f].
Bill Wendling46ada192010-03-02 01:55:18 +00003039 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003040 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003041 getF32Constant(DAG, 0x3f317218));
Bill Wendling39150252008-09-09 20:39:27 +00003042
3043 // Get the significand and build it into a floating-point number with
3044 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003045 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling39150252008-09-09 20:39:27 +00003046
3047 if (LimitFloatPrecision <= 6) {
3048 // For floating-point precision of 6:
3049 //
3050 // LogofMantissa =
3051 // -1.1609546f +
3052 // (1.4034025f - 0.23903021f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003053 //
Bill Wendling39150252008-09-09 20:39:27 +00003054 // error 0.0034276066, which is better than 8 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003055 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003056 getF32Constant(DAG, 0xbe74c456));
Owen Anderson825b72b2009-08-11 20:47:22 +00003057 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003058 getF32Constant(DAG, 0x3fb3a2b1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003059 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3060 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003061 getF32Constant(DAG, 0x3f949a29));
Bill Wendling39150252008-09-09 20:39:27 +00003062
Scott Michelfdc40a02009-02-17 22:15:04 +00003063 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003064 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003065 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3066 // For floating-point precision of 12:
3067 //
3068 // LogOfMantissa =
3069 // -1.7417939f +
3070 // (2.8212026f +
3071 // (-1.4699568f +
3072 // (0.44717955f - 0.56570851e-1f * x) * x) * x) * x;
3073 //
3074 // error 0.000061011436, which is 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003075 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003076 getF32Constant(DAG, 0xbd67b6d6));
Owen Anderson825b72b2009-08-11 20:47:22 +00003077 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003078 getF32Constant(DAG, 0x3ee4f4b8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003079 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3080 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003081 getF32Constant(DAG, 0x3fbc278b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003082 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3083 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003084 getF32Constant(DAG, 0x40348e95));
Owen Anderson825b72b2009-08-11 20:47:22 +00003085 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3086 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003087 getF32Constant(DAG, 0x3fdef31a));
Bill Wendling39150252008-09-09 20:39:27 +00003088
Scott Michelfdc40a02009-02-17 22:15:04 +00003089 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003090 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003091 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3092 // For floating-point precision of 18:
3093 //
3094 // LogOfMantissa =
3095 // -2.1072184f +
3096 // (4.2372794f +
3097 // (-3.7029485f +
3098 // (2.2781945f +
3099 // (-0.87823314f +
3100 // (0.19073739f - 0.17809712e-1f * x) * x) * x) * x) * x)*x;
3101 //
3102 // error 0.0000023660568, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003103 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003104 getF32Constant(DAG, 0xbc91e5ac));
Owen Anderson825b72b2009-08-11 20:47:22 +00003105 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003106 getF32Constant(DAG, 0x3e4350aa));
Owen Anderson825b72b2009-08-11 20:47:22 +00003107 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3108 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003109 getF32Constant(DAG, 0x3f60d3e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003110 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3111 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003112 getF32Constant(DAG, 0x4011cdf0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003113 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3114 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003115 getF32Constant(DAG, 0x406cfd1c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003116 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3117 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003118 getF32Constant(DAG, 0x408797cb));
Owen Anderson825b72b2009-08-11 20:47:22 +00003119 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3120 SDValue LogOfMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003121 getF32Constant(DAG, 0x4006dcab));
Bill Wendling39150252008-09-09 20:39:27 +00003122
Scott Michelfdc40a02009-02-17 22:15:04 +00003123 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003124 MVT::f32, LogOfExponent, LogOfMantissa);
Bill Wendling39150252008-09-09 20:39:27 +00003125 }
3126 } else {
3127 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003128 result = DAG.getNode(ISD::FLOG, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003129 getValue(I.getOperand(1)).getValueType(),
3130 getValue(I.getOperand(1)));
Bill Wendling39150252008-09-09 20:39:27 +00003131 }
3132
Dale Johannesen59e577f2008-09-05 18:38:42 +00003133 setValue(&I, result);
3134}
3135
Bill Wendling3eb59402008-09-09 00:28:24 +00003136/// visitLog2 - Lower a log2 intrinsic. Handles the special sequences for
3137/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003138void
Dan Gohman46510a72010-04-15 01:51:59 +00003139SelectionDAGBuilder::visitLog2(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003140 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003141 DebugLoc dl = getCurDebugLoc();
Bill Wendling3eb59402008-09-09 00:28:24 +00003142
Eric Christopher551754c2010-04-16 23:37:20 +00003143 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003144 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003145 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003146 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003147
Bill Wendling39150252008-09-09 20:39:27 +00003148 // Get the exponent.
Bill Wendling46ada192010-03-02 01:55:18 +00003149 SDValue LogOfExponent = GetExponent(DAG, Op1, TLI, dl);
Bill Wendling856ff412009-12-22 00:12:37 +00003150
Bill Wendling3eb59402008-09-09 00:28:24 +00003151 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003152 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003153 SDValue X = GetSignificand(DAG, Op1, dl);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003154
Bill Wendling3eb59402008-09-09 00:28:24 +00003155 // Different possible minimax approximations of significand in
3156 // floating-point for various degrees of accuracy over [1,2].
3157 if (LimitFloatPrecision <= 6) {
3158 // For floating-point precision of 6:
3159 //
3160 // Log2ofMantissa = -1.6749035f + (2.0246817f - .34484768f * x) * x;
3161 //
3162 // error 0.0049451742, which is more than 7 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003163 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003164 getF32Constant(DAG, 0xbeb08fe0));
Owen Anderson825b72b2009-08-11 20:47:22 +00003165 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003166 getF32Constant(DAG, 0x40019463));
Owen Anderson825b72b2009-08-11 20:47:22 +00003167 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3168 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003169 getF32Constant(DAG, 0x3fd6633d));
Bill Wendling3eb59402008-09-09 00:28:24 +00003170
Scott Michelfdc40a02009-02-17 22:15:04 +00003171 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003172 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003173 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3174 // For floating-point precision of 12:
3175 //
3176 // Log2ofMantissa =
3177 // -2.51285454f +
3178 // (4.07009056f +
3179 // (-2.12067489f +
3180 // (.645142248f - 0.816157886e-1f * x) * x) * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003181 //
Bill Wendling3eb59402008-09-09 00:28:24 +00003182 // error 0.0000876136000, which is better than 13 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003183 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003184 getF32Constant(DAG, 0xbda7262e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003185 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003186 getF32Constant(DAG, 0x3f25280b));
Owen Anderson825b72b2009-08-11 20:47:22 +00003187 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3188 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003189 getF32Constant(DAG, 0x4007b923));
Owen Anderson825b72b2009-08-11 20:47:22 +00003190 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3191 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003192 getF32Constant(DAG, 0x40823e2f));
Owen Anderson825b72b2009-08-11 20:47:22 +00003193 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3194 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003195 getF32Constant(DAG, 0x4020d29c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003196
Scott Michelfdc40a02009-02-17 22:15:04 +00003197 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003198 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003199 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3200 // For floating-point precision of 18:
3201 //
3202 // Log2ofMantissa =
3203 // -3.0400495f +
3204 // (6.1129976f +
3205 // (-5.3420409f +
3206 // (3.2865683f +
3207 // (-1.2669343f +
3208 // (0.27515199f -
3209 // 0.25691327e-1f * x) * x) * x) * x) * x) * x;
3210 //
3211 // error 0.0000018516, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003212 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003213 getF32Constant(DAG, 0xbcd2769e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003214 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003215 getF32Constant(DAG, 0x3e8ce0b9));
Owen Anderson825b72b2009-08-11 20:47:22 +00003216 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3217 SDValue t3 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003218 getF32Constant(DAG, 0x3fa22ae7));
Owen Anderson825b72b2009-08-11 20:47:22 +00003219 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3220 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003221 getF32Constant(DAG, 0x40525723));
Owen Anderson825b72b2009-08-11 20:47:22 +00003222 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3223 SDValue t7 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003224 getF32Constant(DAG, 0x40aaf200));
Owen Anderson825b72b2009-08-11 20:47:22 +00003225 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3226 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003227 getF32Constant(DAG, 0x40c39dad));
Owen Anderson825b72b2009-08-11 20:47:22 +00003228 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3229 SDValue Log2ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003230 getF32Constant(DAG, 0x4042902c));
Bill Wendling3eb59402008-09-09 00:28:24 +00003231
Scott Michelfdc40a02009-02-17 22:15:04 +00003232 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003233 MVT::f32, LogOfExponent, Log2ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003234 }
Dale Johannesen853244f2008-09-05 23:49:37 +00003235 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003236 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003237 result = DAG.getNode(ISD::FLOG2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003238 getValue(I.getOperand(1)).getValueType(),
3239 getValue(I.getOperand(1)));
Dale Johannesen853244f2008-09-05 23:49:37 +00003240 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003241
Dale Johannesen59e577f2008-09-05 18:38:42 +00003242 setValue(&I, result);
3243}
3244
Bill Wendling3eb59402008-09-09 00:28:24 +00003245/// visitLog10 - Lower a log10 intrinsic. Handles the special sequences for
3246/// limited-precision mode.
Dale Johannesen59e577f2008-09-05 18:38:42 +00003247void
Dan Gohman46510a72010-04-15 01:51:59 +00003248SelectionDAGBuilder::visitLog10(const CallInst &I) {
Dale Johannesen59e577f2008-09-05 18:38:42 +00003249 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003250 DebugLoc dl = getCurDebugLoc();
Bill Wendling181b6272008-10-19 20:34:04 +00003251
Eric Christopher551754c2010-04-16 23:37:20 +00003252 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendling3eb59402008-09-09 00:28:24 +00003253 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003254 SDValue Op = getValue(I.getOperand(1));
Owen Anderson825b72b2009-08-11 20:47:22 +00003255 SDValue Op1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, Op);
Bill Wendling3eb59402008-09-09 00:28:24 +00003256
Bill Wendling39150252008-09-09 20:39:27 +00003257 // Scale the exponent by log10(2) [0.30102999f].
Bill Wendling46ada192010-03-02 01:55:18 +00003258 SDValue Exp = GetExponent(DAG, Op1, TLI, dl);
Owen Anderson825b72b2009-08-11 20:47:22 +00003259 SDValue LogOfExponent = DAG.getNode(ISD::FMUL, dl, MVT::f32, Exp,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003260 getF32Constant(DAG, 0x3e9a209a));
Bill Wendling3eb59402008-09-09 00:28:24 +00003261
3262 // Get the significand and build it into a floating-point number with
Bill Wendling39150252008-09-09 20:39:27 +00003263 // exponent of 1.
Bill Wendling46ada192010-03-02 01:55:18 +00003264 SDValue X = GetSignificand(DAG, Op1, dl);
Bill Wendling3eb59402008-09-09 00:28:24 +00003265
3266 if (LimitFloatPrecision <= 6) {
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003267 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003268 //
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003269 // Log10ofMantissa =
3270 // -0.50419619f +
3271 // (0.60948995f - 0.10380950f * x) * x;
3272 //
3273 // error 0.0014886165, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003274 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003275 getF32Constant(DAG, 0xbdd49a13));
Owen Anderson825b72b2009-08-11 20:47:22 +00003276 SDValue t1 = DAG.getNode(ISD::FADD, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003277 getF32Constant(DAG, 0x3f1c0789));
Owen Anderson825b72b2009-08-11 20:47:22 +00003278 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3279 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003280 getF32Constant(DAG, 0x3f011300));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003281
Scott Michelfdc40a02009-02-17 22:15:04 +00003282 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003283 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003284 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3285 // For floating-point precision of 12:
3286 //
3287 // Log10ofMantissa =
3288 // -0.64831180f +
3289 // (0.91751397f +
3290 // (-0.31664806f + 0.47637168e-1f * x) * x) * x;
3291 //
3292 // error 0.00019228036, which is better than 12 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003293 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003294 getF32Constant(DAG, 0x3d431f31));
Owen Anderson825b72b2009-08-11 20:47:22 +00003295 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003296 getF32Constant(DAG, 0x3ea21fb2));
Owen Anderson825b72b2009-08-11 20:47:22 +00003297 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3298 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003299 getF32Constant(DAG, 0x3f6ae232));
Owen Anderson825b72b2009-08-11 20:47:22 +00003300 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3301 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003302 getF32Constant(DAG, 0x3f25f7c3));
Bill Wendling3eb59402008-09-09 00:28:24 +00003303
Scott Michelfdc40a02009-02-17 22:15:04 +00003304 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003305 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003306 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003307 // For floating-point precision of 18:
3308 //
3309 // Log10ofMantissa =
3310 // -0.84299375f +
3311 // (1.5327582f +
3312 // (-1.0688956f +
3313 // (0.49102474f +
3314 // (-0.12539807f + 0.13508273e-1f * x) * x) * x) * x) * x;
3315 //
3316 // error 0.0000037995730, which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003317 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003318 getF32Constant(DAG, 0x3c5d51ce));
Owen Anderson825b72b2009-08-11 20:47:22 +00003319 SDValue t1 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003320 getF32Constant(DAG, 0x3e00685a));
Owen Anderson825b72b2009-08-11 20:47:22 +00003321 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t1, X);
3322 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003323 getF32Constant(DAG, 0x3efb6798));
Owen Anderson825b72b2009-08-11 20:47:22 +00003324 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3325 SDValue t5 = DAG.getNode(ISD::FSUB, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003326 getF32Constant(DAG, 0x3f88d192));
Owen Anderson825b72b2009-08-11 20:47:22 +00003327 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3328 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003329 getF32Constant(DAG, 0x3fc4316c));
Owen Anderson825b72b2009-08-11 20:47:22 +00003330 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3331 SDValue Log10ofMantissa = DAG.getNode(ISD::FSUB, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003332 getF32Constant(DAG, 0x3f57ce70));
Bill Wendlingbd297bc2008-09-09 18:42:23 +00003333
Scott Michelfdc40a02009-02-17 22:15:04 +00003334 result = DAG.getNode(ISD::FADD, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003335 MVT::f32, LogOfExponent, Log10ofMantissa);
Bill Wendling3eb59402008-09-09 00:28:24 +00003336 }
Dale Johannesen852680a2008-09-05 21:27:19 +00003337 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003338 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003339 result = DAG.getNode(ISD::FLOG10, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003340 getValue(I.getOperand(1)).getValueType(),
3341 getValue(I.getOperand(1)));
Dale Johannesen852680a2008-09-05 21:27:19 +00003342 }
Bill Wendling3eb59402008-09-09 00:28:24 +00003343
Dale Johannesen59e577f2008-09-05 18:38:42 +00003344 setValue(&I, result);
3345}
3346
Bill Wendlinge10c8142008-09-09 22:39:21 +00003347/// visitExp2 - Lower an exp2 intrinsic. Handles the special sequences for
3348/// limited-precision mode.
Dale Johannesen601d3c02008-09-05 01:48:15 +00003349void
Dan Gohman46510a72010-04-15 01:51:59 +00003350SelectionDAGBuilder::visitExp2(const CallInst &I) {
Dale Johannesen601d3c02008-09-05 01:48:15 +00003351 SDValue result;
Dale Johannesen66978ee2009-01-31 02:22:37 +00003352 DebugLoc dl = getCurDebugLoc();
Bill Wendlinge10c8142008-09-09 22:39:21 +00003353
Eric Christopher551754c2010-04-16 23:37:20 +00003354 if (getValue(I.getOperand(1)).getValueType() == MVT::f32 &&
Bill Wendlinge10c8142008-09-09 22:39:21 +00003355 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003356 SDValue Op = getValue(I.getOperand(1));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003357
Owen Anderson825b72b2009-08-11 20:47:22 +00003358 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, Op);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003359
3360 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003361 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3362 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, Op, t1);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003363
3364 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003365 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003366 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlinge10c8142008-09-09 22:39:21 +00003367
3368 if (LimitFloatPrecision <= 6) {
3369 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003370 //
Bill Wendlinge10c8142008-09-09 22:39:21 +00003371 // TwoToFractionalPartOfX =
3372 // 0.997535578f +
3373 // (0.735607626f + 0.252464424f * x) * x;
3374 //
3375 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003376 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003377 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003378 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003379 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003380 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3381 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003382 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003383 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003384 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003385 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003386
Scott Michelfdc40a02009-02-17 22:15:04 +00003387 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003388 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003389 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3390 // For floating-point precision of 12:
3391 //
3392 // TwoToFractionalPartOfX =
3393 // 0.999892986f +
3394 // (0.696457318f +
3395 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3396 //
3397 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003398 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003399 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003400 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003401 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003402 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3403 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003404 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003405 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3406 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003407 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003408 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003409 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003410 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003411
Scott Michelfdc40a02009-02-17 22:15:04 +00003412 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003413 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003414 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3415 // For floating-point precision of 18:
3416 //
3417 // TwoToFractionalPartOfX =
3418 // 0.999999982f +
3419 // (0.693148872f +
3420 // (0.240227044f +
3421 // (0.554906021e-1f +
3422 // (0.961591928e-2f +
3423 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3424 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003425 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003426 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003427 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003428 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003429 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3430 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003431 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003432 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3433 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003434 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003435 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3436 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003437 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003438 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3439 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003440 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003441 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3442 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003443 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003444 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003445 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003446 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003447
Scott Michelfdc40a02009-02-17 22:15:04 +00003448 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003449 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlinge10c8142008-09-09 22:39:21 +00003450 }
Dale Johannesen601d3c02008-09-05 01:48:15 +00003451 } else {
Bill Wendling3eb59402008-09-09 00:28:24 +00003452 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003453 result = DAG.getNode(ISD::FEXP2, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003454 getValue(I.getOperand(1)).getValueType(),
3455 getValue(I.getOperand(1)));
Dale Johannesen601d3c02008-09-05 01:48:15 +00003456 }
Bill Wendlinge10c8142008-09-09 22:39:21 +00003457
Dale Johannesen601d3c02008-09-05 01:48:15 +00003458 setValue(&I, result);
3459}
3460
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003461/// visitPow - Lower a pow intrinsic. Handles the special sequences for
3462/// limited-precision mode with x == 10.0f.
3463void
Dan Gohman46510a72010-04-15 01:51:59 +00003464SelectionDAGBuilder::visitPow(const CallInst &I) {
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003465 SDValue result;
Eric Christopher551754c2010-04-16 23:37:20 +00003466 const Value *Val = I.getOperand(1);
Dale Johannesen66978ee2009-01-31 02:22:37 +00003467 DebugLoc dl = getCurDebugLoc();
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003468 bool IsExp10 = false;
3469
Owen Anderson825b72b2009-08-11 20:47:22 +00003470 if (getValue(Val).getValueType() == MVT::f32 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003471 getValue(I.getOperand(2)).getValueType() == MVT::f32 &&
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003472 LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
3473 if (Constant *C = const_cast<Constant*>(dyn_cast<Constant>(Val))) {
3474 if (ConstantFP *CFP = dyn_cast<ConstantFP>(C)) {
3475 APFloat Ten(10.0f);
3476 IsExp10 = CFP->getValueAPF().bitwiseIsEqual(Ten);
3477 }
3478 }
3479 }
3480
3481 if (IsExp10 && LimitFloatPrecision > 0 && LimitFloatPrecision <= 18) {
Eric Christopher551754c2010-04-16 23:37:20 +00003482 SDValue Op = getValue(I.getOperand(2));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003483
3484 // Put the exponent in the right bit position for later addition to the
3485 // final result:
3486 //
3487 // #define LOG2OF10 3.3219281f
3488 // IntegerPartOfX = (int32_t)(x * LOG2OF10);
Owen Anderson825b72b2009-08-11 20:47:22 +00003489 SDValue t0 = DAG.getNode(ISD::FMUL, dl, MVT::f32, Op,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003490 getF32Constant(DAG, 0x40549a78));
Owen Anderson825b72b2009-08-11 20:47:22 +00003491 SDValue IntegerPartOfX = DAG.getNode(ISD::FP_TO_SINT, dl, MVT::i32, t0);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003492
3493 // FractionalPartOfX = x - (float)IntegerPartOfX;
Owen Anderson825b72b2009-08-11 20:47:22 +00003494 SDValue t1 = DAG.getNode(ISD::SINT_TO_FP, dl, MVT::f32, IntegerPartOfX);
3495 SDValue X = DAG.getNode(ISD::FSUB, dl, MVT::f32, t0, t1);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003496
3497 // IntegerPartOfX <<= 23;
Owen Anderson825b72b2009-08-11 20:47:22 +00003498 IntegerPartOfX = DAG.getNode(ISD::SHL, dl, MVT::i32, IntegerPartOfX,
Duncan Sands92abc622009-01-31 15:50:11 +00003499 DAG.getConstant(23, TLI.getPointerTy()));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003500
3501 if (LimitFloatPrecision <= 6) {
3502 // For floating-point precision of 6:
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003503 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003504 // twoToFractionalPartOfX =
3505 // 0.997535578f +
3506 // (0.735607626f + 0.252464424f * x) * x;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003507 //
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003508 // error 0.0144103317, which is 6 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003509 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003510 getF32Constant(DAG, 0x3e814304));
Owen Anderson825b72b2009-08-11 20:47:22 +00003511 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003512 getF32Constant(DAG, 0x3f3c50c8));
Owen Anderson825b72b2009-08-11 20:47:22 +00003513 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3514 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003515 getF32Constant(DAG, 0x3f7f5e7e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003516 SDValue t6 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t5);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003517 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003518 DAG.getNode(ISD::ADD, dl, MVT::i32, t6, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003519
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003520 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003521 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003522 } else if (LimitFloatPrecision > 6 && LimitFloatPrecision <= 12) {
3523 // For floating-point precision of 12:
3524 //
3525 // TwoToFractionalPartOfX =
3526 // 0.999892986f +
3527 // (0.696457318f +
3528 // (0.224338339f + 0.792043434e-1f * x) * x) * x;
3529 //
3530 // error 0.000107046256, which is 13 to 14 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003531 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003532 getF32Constant(DAG, 0x3da235e3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003533 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003534 getF32Constant(DAG, 0x3e65b8f3));
Owen Anderson825b72b2009-08-11 20:47:22 +00003535 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3536 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003537 getF32Constant(DAG, 0x3f324b07));
Owen Anderson825b72b2009-08-11 20:47:22 +00003538 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3539 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003540 getF32Constant(DAG, 0x3f7ff8fd));
Owen Anderson825b72b2009-08-11 20:47:22 +00003541 SDValue t8 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t7);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003542 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003543 DAG.getNode(ISD::ADD, dl, MVT::i32, t8, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003544
Scott Michelfdc40a02009-02-17 22:15:04 +00003545 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003546 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003547 } else { // LimitFloatPrecision > 12 && LimitFloatPrecision <= 18
3548 // For floating-point precision of 18:
3549 //
3550 // TwoToFractionalPartOfX =
3551 // 0.999999982f +
3552 // (0.693148872f +
3553 // (0.240227044f +
3554 // (0.554906021e-1f +
3555 // (0.961591928e-2f +
3556 // (0.136028312e-2f + 0.157059148e-3f *x)*x)*x)*x)*x)*x;
3557 // error 2.47208000*10^(-7), which is better than 18 bits
Owen Anderson825b72b2009-08-11 20:47:22 +00003558 SDValue t2 = DAG.getNode(ISD::FMUL, dl, MVT::f32, X,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003559 getF32Constant(DAG, 0x3924b03e));
Owen Anderson825b72b2009-08-11 20:47:22 +00003560 SDValue t3 = DAG.getNode(ISD::FADD, dl, MVT::f32, t2,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003561 getF32Constant(DAG, 0x3ab24b87));
Owen Anderson825b72b2009-08-11 20:47:22 +00003562 SDValue t4 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t3, X);
3563 SDValue t5 = DAG.getNode(ISD::FADD, dl, MVT::f32, t4,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003564 getF32Constant(DAG, 0x3c1d8c17));
Owen Anderson825b72b2009-08-11 20:47:22 +00003565 SDValue t6 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t5, X);
3566 SDValue t7 = DAG.getNode(ISD::FADD, dl, MVT::f32, t6,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003567 getF32Constant(DAG, 0x3d634a1d));
Owen Anderson825b72b2009-08-11 20:47:22 +00003568 SDValue t8 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t7, X);
3569 SDValue t9 = DAG.getNode(ISD::FADD, dl, MVT::f32, t8,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003570 getF32Constant(DAG, 0x3e75fe14));
Owen Anderson825b72b2009-08-11 20:47:22 +00003571 SDValue t10 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t9, X);
3572 SDValue t11 = DAG.getNode(ISD::FADD, dl, MVT::f32, t10,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003573 getF32Constant(DAG, 0x3f317234));
Owen Anderson825b72b2009-08-11 20:47:22 +00003574 SDValue t12 = DAG.getNode(ISD::FMUL, dl, MVT::f32, t11, X);
3575 SDValue t13 = DAG.getNode(ISD::FADD, dl, MVT::f32, t12,
Bill Wendlingcd4c73a2008-09-22 00:44:35 +00003576 getF32Constant(DAG, 0x3f800000));
Owen Anderson825b72b2009-08-11 20:47:22 +00003577 SDValue t14 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32, t13);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003578 SDValue TwoToFractionalPartOfX =
Owen Anderson825b72b2009-08-11 20:47:22 +00003579 DAG.getNode(ISD::ADD, dl, MVT::i32, t14, IntegerPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003580
Scott Michelfdc40a02009-02-17 22:15:04 +00003581 result = DAG.getNode(ISD::BIT_CONVERT, dl,
Owen Anderson825b72b2009-08-11 20:47:22 +00003582 MVT::f32, TwoToFractionalPartOfX);
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003583 }
3584 } else {
3585 // No special expansion.
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003586 result = DAG.getNode(ISD::FPOW, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003587 getValue(I.getOperand(1)).getValueType(),
3588 getValue(I.getOperand(1)),
3589 getValue(I.getOperand(2)));
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003590 }
3591
3592 setValue(&I, result);
3593}
3594
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003595
3596/// ExpandPowI - Expand a llvm.powi intrinsic.
3597static SDValue ExpandPowI(DebugLoc DL, SDValue LHS, SDValue RHS,
3598 SelectionDAG &DAG) {
3599 // If RHS is a constant, we can expand this out to a multiplication tree,
3600 // otherwise we end up lowering to a call to __powidf2 (for example). When
3601 // optimizing for size, we only want to do this if the expansion would produce
3602 // a small number of multiplies, otherwise we do the full expansion.
3603 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
3604 // Get the exponent as a positive value.
3605 unsigned Val = RHSC->getSExtValue();
3606 if ((int)Val < 0) Val = -Val;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003607
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003608 // powi(x, 0) -> 1.0
3609 if (Val == 0)
3610 return DAG.getConstantFP(1.0, LHS.getValueType());
3611
Dan Gohmanae541aa2010-04-15 04:33:49 +00003612 const Function *F = DAG.getMachineFunction().getFunction();
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003613 if (!F->hasFnAttr(Attribute::OptimizeForSize) ||
3614 // If optimizing for size, don't insert too many multiplies. This
3615 // inserts up to 5 multiplies.
3616 CountPopulation_32(Val)+Log2_32(Val) < 7) {
3617 // We use the simple binary decomposition method to generate the multiply
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003618 // sequence. There are more optimal ways to do this (for example,
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003619 // powi(x,15) generates one more multiply than it should), but this has
3620 // the benefit of being both really simple and much better than a libcall.
3621 SDValue Res; // Logically starts equal to 1.0
3622 SDValue CurSquare = LHS;
3623 while (Val) {
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003624 if (Val & 1) {
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003625 if (Res.getNode())
3626 Res = DAG.getNode(ISD::FMUL, DL,Res.getValueType(), Res, CurSquare);
3627 else
3628 Res = CurSquare; // 1.0*CurSquare.
Mikhail Glushenkovbfdfea82010-01-01 04:41:36 +00003629 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003630
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003631 CurSquare = DAG.getNode(ISD::FMUL, DL, CurSquare.getValueType(),
3632 CurSquare, CurSquare);
3633 Val >>= 1;
3634 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003635
Chris Lattnerf031e8a2010-01-01 03:32:16 +00003636 // If the original was negative, invert the result, producing 1/(x*x*x).
3637 if (RHSC->getSExtValue() < 0)
3638 Res = DAG.getNode(ISD::FDIV, DL, LHS.getValueType(),
3639 DAG.getConstantFP(1.0, LHS.getValueType()), Res);
3640 return Res;
3641 }
3642 }
3643
3644 // Otherwise, expand to a libcall.
3645 return DAG.getNode(ISD::FPOWI, DL, LHS.getValueType(), LHS, RHS);
3646}
3647
3648
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003649/// visitIntrinsicCall - Lower the call to the specified intrinsic function. If
3650/// we want to emit this as a call to a named external function, return the name
3651/// otherwise lower it and return null.
3652const char *
Dan Gohman46510a72010-04-15 01:51:59 +00003653SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I, unsigned Intrinsic) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00003654 DebugLoc dl = getCurDebugLoc();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003655 SDValue Res;
3656
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003657 switch (Intrinsic) {
3658 default:
3659 // By default, turn this into a target intrinsic node.
3660 visitTargetIntrinsic(I, Intrinsic);
3661 return 0;
3662 case Intrinsic::vastart: visitVAStart(I); return 0;
3663 case Intrinsic::vaend: visitVAEnd(I); return 0;
3664 case Intrinsic::vacopy: visitVACopy(I); return 0;
3665 case Intrinsic::returnaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003666 setValue(&I, DAG.getNode(ISD::RETURNADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003667 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003668 return 0;
Bill Wendlingd5d81912008-09-26 22:10:44 +00003669 case Intrinsic::frameaddress:
Bill Wendling4533cac2010-01-28 21:51:40 +00003670 setValue(&I, DAG.getNode(ISD::FRAMEADDR, dl, TLI.getPointerTy(),
Eric Christopher551754c2010-04-16 23:37:20 +00003671 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003672 return 0;
3673 case Intrinsic::setjmp:
3674 return "_setjmp"+!TLI.usesUnderscoreSetJmp();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003675 case Intrinsic::longjmp:
3676 return "_longjmp"+!TLI.usesUnderscoreLongJmp();
Chris Lattner824b9582008-11-21 16:42:48 +00003677 case Intrinsic::memcpy: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003678 // Assert for address < 256 since we support only user defined address
3679 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003680 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003681 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003682 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003683 < 256 &&
3684 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003685 SDValue Op1 = getValue(I.getOperand(1));
3686 SDValue Op2 = getValue(I.getOperand(2));
3687 SDValue Op3 = getValue(I.getOperand(3));
3688 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3689 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003690 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol, false,
Eric Christopher551754c2010-04-16 23:37:20 +00003691 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003692 return 0;
3693 }
Chris Lattner824b9582008-11-21 16:42:48 +00003694 case Intrinsic::memset: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003695 // Assert for address < 256 since we support only user defined address
3696 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003697 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003698 < 256 &&
3699 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003700 SDValue Op1 = getValue(I.getOperand(1));
3701 SDValue Op2 = getValue(I.getOperand(2));
3702 SDValue Op3 = getValue(I.getOperand(3));
3703 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3704 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Mon P Wang20adc9d2010-04-04 03:10:48 +00003705 DAG.setRoot(DAG.getMemset(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003706 I.getOperand(1), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003707 return 0;
3708 }
Chris Lattner824b9582008-11-21 16:42:48 +00003709 case Intrinsic::memmove: {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003710 // Assert for address < 256 since we support only user defined address
3711 // spaces.
Eric Christopher551754c2010-04-16 23:37:20 +00003712 assert(cast<PointerType>(I.getOperand(1)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003713 < 256 &&
Eric Christopher551754c2010-04-16 23:37:20 +00003714 cast<PointerType>(I.getOperand(2)->getType())->getAddressSpace()
Mon P Wang20adc9d2010-04-04 03:10:48 +00003715 < 256 &&
3716 "Unknown address space");
Eric Christopher551754c2010-04-16 23:37:20 +00003717 SDValue Op1 = getValue(I.getOperand(1));
3718 SDValue Op2 = getValue(I.getOperand(2));
3719 SDValue Op3 = getValue(I.getOperand(3));
3720 unsigned Align = cast<ConstantInt>(I.getOperand(4))->getZExtValue();
3721 bool isVol = cast<ConstantInt>(I.getOperand(5))->getZExtValue();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003722
3723 // If the source and destination are known to not be aliases, we can
3724 // lower memmove as memcpy.
3725 uint64_t Size = -1ULL;
3726 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op3))
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003727 Size = C->getZExtValue();
Eric Christopher551754c2010-04-16 23:37:20 +00003728 if (AA->alias(I.getOperand(1), Size, I.getOperand(2), Size) ==
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003729 AliasAnalysis::NoAlias) {
Mon P Wang20adc9d2010-04-04 03:10:48 +00003730 DAG.setRoot(DAG.getMemcpy(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003731 false, I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003732 return 0;
3733 }
3734
Mon P Wang20adc9d2010-04-04 03:10:48 +00003735 DAG.setRoot(DAG.getMemmove(getRoot(), dl, Op1, Op2, Op3, Align, isVol,
Eric Christopher551754c2010-04-16 23:37:20 +00003736 I.getOperand(1), 0, I.getOperand(2), 0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003737 return 0;
3738 }
Bill Wendling92c1e122009-02-13 02:16:35 +00003739 case Intrinsic::dbg_declare: {
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003740 // FIXME: currently, we get here only if OptLevel != CodeGenOpt::None.
3741 // The real handling of this intrinsic is in FastISel.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003742 if (OptLevel != CodeGenOpt::None)
Devang Patel7e1e31f2009-07-02 22:43:26 +00003743 // FIXME: Variable debug info is not supported here.
3744 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003745 const DbgDeclareInst &DI = cast<DbgDeclareInst>(I);
Chris Lattnerbf0ca2b2009-12-29 09:32:19 +00003746 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
Devang Patel7e1e31f2009-07-02 22:43:26 +00003747 return 0;
3748
Devang Patelac1ceb32009-10-09 22:42:28 +00003749 MDNode *Variable = DI.getVariable();
Dan Gohman46510a72010-04-15 01:51:59 +00003750 const Value *Address = DI.getAddress();
Dale Johannesen8ac38f22010-02-08 21:53:27 +00003751 if (!Address)
3752 return 0;
Dan Gohman46510a72010-04-15 01:51:59 +00003753 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(Address))
Devang Patel24f20e02009-08-22 17:12:53 +00003754 Address = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003755 const AllocaInst *AI = dyn_cast<AllocaInst>(Address);
Devang Patel24f20e02009-08-22 17:12:53 +00003756 // Don't handle byval struct arguments or VLAs, for example.
3757 if (!AI)
3758 return 0;
Devang Patelbd1d6a82009-09-05 00:34:14 +00003759 DenseMap<const AllocaInst*, int>::iterator SI =
3760 FuncInfo.StaticAllocaMap.find(AI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00003761 if (SI == FuncInfo.StaticAllocaMap.end())
Devang Patelbd1d6a82009-09-05 00:34:14 +00003762 return 0; // VLAs.
3763 int FI = SI->second;
Devang Patel70d75ca2009-11-12 19:02:56 +00003764
Chris Lattner512063d2010-04-05 06:19:28 +00003765 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3766 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3767 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003768 return 0;
Bill Wendling92c1e122009-02-13 02:16:35 +00003769 }
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003770 case Intrinsic::dbg_value: {
Dan Gohman46510a72010-04-15 01:51:59 +00003771 const DbgValueInst &DI = cast<DbgValueInst>(I);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003772 if (!DIDescriptor::ValidDebugInfo(DI.getVariable(), CodeGenOpt::None))
3773 return 0;
3774
3775 MDNode *Variable = DI.getVariable();
Devang Patel00190342010-03-15 19:15:44 +00003776 uint64_t Offset = DI.getOffset();
Dan Gohman46510a72010-04-15 01:51:59 +00003777 const Value *V = DI.getValue();
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003778 if (!V)
3779 return 0;
Devang Patel00190342010-03-15 19:15:44 +00003780
3781 // Build an entry in DbgOrdering. Debug info input nodes get an SDNodeOrder
3782 // but do not always have a corresponding SDNode built. The SDNodeOrder
3783 // absolute, but not relative, values are different depending on whether
3784 // debug info exists.
3785 ++SDNodeOrder;
3786 if (isa<ConstantInt>(V) || isa<ConstantFP>(V)) {
Evan Cheng31441b72010-03-29 20:48:30 +00003787 DAG.AddDbgValue(DAG.getDbgValue(Variable, V, Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003788 } else {
3789 SDValue &N = NodeMap[V];
Evan Cheng31441b72010-03-29 20:48:30 +00003790 if (N.getNode())
3791 DAG.AddDbgValue(DAG.getDbgValue(Variable, N.getNode(),
3792 N.getResNo(), Offset, dl, SDNodeOrder),
3793 N.getNode());
3794 else
Devang Patel00190342010-03-15 19:15:44 +00003795 // We may expand this to cover more cases. One case where we have no
3796 // data available is an unreferenced parameter; we need this fallback.
Evan Cheng31441b72010-03-29 20:48:30 +00003797 DAG.AddDbgValue(DAG.getDbgValue(Variable,
Devang Patel00190342010-03-15 19:15:44 +00003798 UndefValue::get(V->getType()),
Evan Cheng31441b72010-03-29 20:48:30 +00003799 Offset, dl, SDNodeOrder));
Devang Patel00190342010-03-15 19:15:44 +00003800 }
3801
3802 // Build a debug info table entry.
Dan Gohman46510a72010-04-15 01:51:59 +00003803 if (const BitCastInst *BCI = dyn_cast<BitCastInst>(V))
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003804 V = BCI->getOperand(0);
Dan Gohman46510a72010-04-15 01:51:59 +00003805 const AllocaInst *AI = dyn_cast<AllocaInst>(V);
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003806 // Don't handle byval struct arguments or VLAs, for example.
3807 if (!AI)
3808 return 0;
3809 DenseMap<const AllocaInst*, int>::iterator SI =
3810 FuncInfo.StaticAllocaMap.find(AI);
3811 if (SI == FuncInfo.StaticAllocaMap.end())
3812 return 0; // VLAs.
3813 int FI = SI->second;
Chris Lattnerde4845c2010-04-02 19:42:39 +00003814
Chris Lattner512063d2010-04-05 06:19:28 +00003815 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
3816 if (!DI.getDebugLoc().isUnknown() && MMI.hasDebugInfo())
3817 MMI.setVariableDbgInfo(Variable, FI, DI.getDebugLoc());
Dale Johannesen904c2fa2010-02-01 19:54:53 +00003818 return 0;
3819 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003820 case Intrinsic::eh_exception: {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003821 // Insert the EXCEPTIONADDR instruction.
Duncan Sandsb0f1e172009-05-22 20:36:31 +00003822 assert(CurMBB->isLandingPad() &&"Call to eh.exception not in landing pad!");
Owen Anderson825b72b2009-08-11 20:47:22 +00003823 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003824 SDValue Ops[1];
3825 Ops[0] = DAG.getRoot();
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003826 SDValue Op = DAG.getNode(ISD::EXCEPTIONADDR, dl, VTs, Ops, 1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003827 setValue(&I, Op);
3828 DAG.setRoot(Op.getValue(1));
3829 return 0;
3830 }
3831
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003832 case Intrinsic::eh_selector: {
Chris Lattner512063d2010-04-05 06:19:28 +00003833 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner3a5815f2009-09-17 23:54:54 +00003834 if (CurMBB->isLandingPad())
Chris Lattner512063d2010-04-05 06:19:28 +00003835 AddCatchInfo(I, &MMI, CurMBB);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003836 else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003837#ifndef NDEBUG
Chris Lattner3a5815f2009-09-17 23:54:54 +00003838 FuncInfo.CatchInfoLost.insert(&I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003839#endif
Chris Lattner3a5815f2009-09-17 23:54:54 +00003840 // FIXME: Mark exception selector register as live in. Hack for PR1508.
3841 unsigned Reg = TLI.getExceptionSelectorRegister();
3842 if (Reg) CurMBB->addLiveIn(Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003843 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00003844
Chris Lattner3a5815f2009-09-17 23:54:54 +00003845 // Insert the EHSELECTION instruction.
3846 SDVTList VTs = DAG.getVTList(TLI.getPointerTy(), MVT::Other);
3847 SDValue Ops[2];
Eric Christopher551754c2010-04-16 23:37:20 +00003848 Ops[0] = getValue(I.getOperand(1));
Chris Lattner3a5815f2009-09-17 23:54:54 +00003849 Ops[1] = getRoot();
3850 SDValue Op = DAG.getNode(ISD::EHSELECTION, dl, VTs, Ops, 2);
Chris Lattner3a5815f2009-09-17 23:54:54 +00003851 DAG.setRoot(Op.getValue(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003852 setValue(&I, DAG.getSExtOrTrunc(Op, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003853 return 0;
3854 }
3855
Duncan Sandsb01bbdc2009-10-14 16:11:37 +00003856 case Intrinsic::eh_typeid_for: {
Chris Lattner512063d2010-04-05 06:19:28 +00003857 // Find the type id for the given typeinfo.
Eric Christopher551754c2010-04-16 23:37:20 +00003858 GlobalVariable *GV = ExtractTypeInfo(I.getOperand(1));
Chris Lattner512063d2010-04-05 06:19:28 +00003859 unsigned TypeID = DAG.getMachineFunction().getMMI().getTypeIDFor(GV);
3860 Res = DAG.getConstant(TypeID, MVT::i32);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003861 setValue(&I, Res);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003862 return 0;
3863 }
3864
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003865 case Intrinsic::eh_return_i32:
3866 case Intrinsic::eh_return_i64:
Chris Lattner512063d2010-04-05 06:19:28 +00003867 DAG.getMachineFunction().getMMI().setCallsEHReturn(true);
3868 DAG.setRoot(DAG.getNode(ISD::EH_RETURN, dl,
3869 MVT::Other,
3870 getControlRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00003871 getValue(I.getOperand(1)),
3872 getValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003873 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003874 case Intrinsic::eh_unwind_init:
Chris Lattner512063d2010-04-05 06:19:28 +00003875 DAG.getMachineFunction().getMMI().setCallsUnwindInit(true);
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003876 return 0;
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003877 case Intrinsic::eh_dwarf_cfa: {
Eric Christopher551754c2010-04-16 23:37:20 +00003878 EVT VT = getValue(I.getOperand(1)).getValueType();
3879 SDValue CfaArg = DAG.getSExtOrTrunc(getValue(I.getOperand(1)), dl,
Duncan Sands3a66a682009-10-13 21:04:12 +00003880 TLI.getPointerTy());
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003881 SDValue Offset = DAG.getNode(ISD::ADD, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003882 TLI.getPointerTy(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00003883 DAG.getNode(ISD::FRAME_TO_ARGS_OFFSET, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003884 TLI.getPointerTy()),
3885 CfaArg);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003886 SDValue FA = DAG.getNode(ISD::FRAMEADDR, dl,
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003887 TLI.getPointerTy(),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003888 DAG.getConstant(0, TLI.getPointerTy()));
Bill Wendling4533cac2010-01-28 21:51:40 +00003889 setValue(&I, DAG.getNode(ISD::ADD, dl, TLI.getPointerTy(),
3890 FA, Offset));
Anton Korobeynikova0e8a1e2008-09-08 21:13:56 +00003891 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003892 }
Jim Grosbachca752c92010-01-28 01:45:32 +00003893 case Intrinsic::eh_sjlj_callsite: {
Chris Lattner512063d2010-04-05 06:19:28 +00003894 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Eric Christopher551754c2010-04-16 23:37:20 +00003895 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(1));
Jim Grosbachca752c92010-01-28 01:45:32 +00003896 assert(CI && "Non-constant call site value in eh.sjlj.callsite!");
Chris Lattner512063d2010-04-05 06:19:28 +00003897 assert(MMI.getCurrentCallSite() == 0 && "Overlapping call sites!");
Jim Grosbachca752c92010-01-28 01:45:32 +00003898
Chris Lattner512063d2010-04-05 06:19:28 +00003899 MMI.setCurrentCallSite(CI->getZExtValue());
Jim Grosbachca752c92010-01-28 01:45:32 +00003900 return 0;
3901 }
3902
Mon P Wang77cdf302008-11-10 20:54:11 +00003903 case Intrinsic::convertff:
3904 case Intrinsic::convertfsi:
3905 case Intrinsic::convertfui:
3906 case Intrinsic::convertsif:
3907 case Intrinsic::convertuif:
3908 case Intrinsic::convertss:
3909 case Intrinsic::convertsu:
3910 case Intrinsic::convertus:
3911 case Intrinsic::convertuu: {
3912 ISD::CvtCode Code = ISD::CVT_INVALID;
3913 switch (Intrinsic) {
3914 case Intrinsic::convertff: Code = ISD::CVT_FF; break;
3915 case Intrinsic::convertfsi: Code = ISD::CVT_FS; break;
3916 case Intrinsic::convertfui: Code = ISD::CVT_FU; break;
3917 case Intrinsic::convertsif: Code = ISD::CVT_SF; break;
3918 case Intrinsic::convertuif: Code = ISD::CVT_UF; break;
3919 case Intrinsic::convertss: Code = ISD::CVT_SS; break;
3920 case Intrinsic::convertsu: Code = ISD::CVT_SU; break;
3921 case Intrinsic::convertus: Code = ISD::CVT_US; break;
3922 case Intrinsic::convertuu: Code = ISD::CVT_UU; break;
3923 }
Owen Andersone50ed302009-08-10 22:56:29 +00003924 EVT DestVT = TLI.getValueType(I.getType());
Eric Christopher551754c2010-04-16 23:37:20 +00003925 const Value *Op1 = I.getOperand(1);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003926 Res = DAG.getConvertRndSat(DestVT, getCurDebugLoc(), getValue(Op1),
3927 DAG.getValueType(DestVT),
3928 DAG.getValueType(getValue(Op1).getValueType()),
3929 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00003930 getValue(I.getOperand(3)),
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003931 Code);
3932 setValue(&I, Res);
Mon P Wang77cdf302008-11-10 20:54:11 +00003933 return 0;
3934 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003935 case Intrinsic::sqrt:
Bill Wendling4533cac2010-01-28 21:51:40 +00003936 setValue(&I, DAG.getNode(ISD::FSQRT, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003937 getValue(I.getOperand(1)).getValueType(),
3938 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003939 return 0;
3940 case Intrinsic::powi:
Eric Christopher551754c2010-04-16 23:37:20 +00003941 setValue(&I, ExpandPowI(dl, getValue(I.getOperand(1)),
3942 getValue(I.getOperand(2)), DAG));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003943 return 0;
3944 case Intrinsic::sin:
Bill Wendling4533cac2010-01-28 21:51:40 +00003945 setValue(&I, DAG.getNode(ISD::FSIN, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003946 getValue(I.getOperand(1)).getValueType(),
3947 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003948 return 0;
3949 case Intrinsic::cos:
Bill Wendling4533cac2010-01-28 21:51:40 +00003950 setValue(&I, DAG.getNode(ISD::FCOS, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003951 getValue(I.getOperand(1)).getValueType(),
3952 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003953 return 0;
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003954 case Intrinsic::log:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003955 visitLog(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003956 return 0;
3957 case Intrinsic::log2:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003958 visitLog2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003959 return 0;
3960 case Intrinsic::log10:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003961 visitLog10(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003962 return 0;
3963 case Intrinsic::exp:
Dale Johannesen59e577f2008-09-05 18:38:42 +00003964 visitExp(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003965 return 0;
3966 case Intrinsic::exp2:
Dale Johannesen601d3c02008-09-05 01:48:15 +00003967 visitExp2(I);
Dale Johannesen7794f2a2008-09-04 00:47:13 +00003968 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003969 case Intrinsic::pow:
Bill Wendlingaeb5c7b2008-09-10 00:20:20 +00003970 visitPow(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003971 return 0;
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00003972 case Intrinsic::convert_to_fp16:
3973 setValue(&I, DAG.getNode(ISD::FP32_TO_FP16, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003974 MVT::i16, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00003975 return 0;
3976 case Intrinsic::convert_from_fp16:
3977 setValue(&I, DAG.getNode(ISD::FP16_TO_FP32, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003978 MVT::f32, getValue(I.getOperand(1))));
Anton Korobeynikovbe5b0322010-03-14 18:42:15 +00003979 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003980 case Intrinsic::pcmarker: {
Eric Christopher551754c2010-04-16 23:37:20 +00003981 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00003982 DAG.setRoot(DAG.getNode(ISD::PCMARKER, dl, MVT::Other, getRoot(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003983 return 0;
3984 }
3985 case Intrinsic::readcyclecounter: {
3986 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00003987 Res = DAG.getNode(ISD::READCYCLECOUNTER, dl,
3988 DAG.getVTList(MVT::i64, MVT::Other),
3989 &Op, 1);
3990 setValue(&I, Res);
3991 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003992 return 0;
3993 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003994 case Intrinsic::bswap:
Bill Wendling4533cac2010-01-28 21:51:40 +00003995 setValue(&I, DAG.getNode(ISD::BSWAP, dl,
Eric Christopher551754c2010-04-16 23:37:20 +00003996 getValue(I.getOperand(1)).getValueType(),
3997 getValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00003998 return 0;
3999 case Intrinsic::cttz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004000 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004001 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004002 setValue(&I, DAG.getNode(ISD::CTTZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004003 return 0;
4004 }
4005 case Intrinsic::ctlz: {
Eric Christopher551754c2010-04-16 23:37:20 +00004006 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004007 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004008 setValue(&I, DAG.getNode(ISD::CTLZ, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004009 return 0;
4010 }
4011 case Intrinsic::ctpop: {
Eric Christopher551754c2010-04-16 23:37:20 +00004012 SDValue Arg = getValue(I.getOperand(1));
Owen Andersone50ed302009-08-10 22:56:29 +00004013 EVT Ty = Arg.getValueType();
Bill Wendling4533cac2010-01-28 21:51:40 +00004014 setValue(&I, DAG.getNode(ISD::CTPOP, dl, Ty, Arg));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004015 return 0;
4016 }
4017 case Intrinsic::stacksave: {
4018 SDValue Op = getRoot();
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004019 Res = DAG.getNode(ISD::STACKSAVE, dl,
4020 DAG.getVTList(TLI.getPointerTy(), MVT::Other), &Op, 1);
4021 setValue(&I, Res);
4022 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004023 return 0;
4024 }
4025 case Intrinsic::stackrestore: {
Eric Christopher551754c2010-04-16 23:37:20 +00004026 Res = getValue(I.getOperand(1));
Bill Wendling4533cac2010-01-28 21:51:40 +00004027 DAG.setRoot(DAG.getNode(ISD::STACKRESTORE, dl, MVT::Other, getRoot(), Res));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004028 return 0;
4029 }
Bill Wendling57344502008-11-18 11:01:33 +00004030 case Intrinsic::stackprotector: {
Bill Wendlingb2a42982008-11-06 02:29:10 +00004031 // Emit code into the DAG to store the stack guard onto the stack.
4032 MachineFunction &MF = DAG.getMachineFunction();
4033 MachineFrameInfo *MFI = MF.getFrameInfo();
Owen Andersone50ed302009-08-10 22:56:29 +00004034 EVT PtrTy = TLI.getPointerTy();
Bill Wendlingb2a42982008-11-06 02:29:10 +00004035
Eric Christopher551754c2010-04-16 23:37:20 +00004036 SDValue Src = getValue(I.getOperand(1)); // The guard's value.
4037 AllocaInst *Slot = cast<AllocaInst>(I.getOperand(2));
Bill Wendlingb2a42982008-11-06 02:29:10 +00004038
Bill Wendlingb7c6ebc2008-11-07 01:23:58 +00004039 int FI = FuncInfo.StaticAllocaMap[Slot];
Bill Wendlingb2a42982008-11-06 02:29:10 +00004040 MFI->setStackProtectorIndex(FI);
4041
4042 SDValue FIN = DAG.getFrameIndex(FI, PtrTy);
4043
4044 // Store the stack protector onto the stack.
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004045 Res = DAG.getStore(getRoot(), getCurDebugLoc(), Src, FIN,
4046 PseudoSourceValue::getFixedStack(FI),
David Greene1e559442010-02-15 17:00:31 +00004047 0, true, false, 0);
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004048 setValue(&I, Res);
4049 DAG.setRoot(Res);
Bill Wendlingb2a42982008-11-06 02:29:10 +00004050 return 0;
4051 }
Eric Christopher7b5e6172009-10-27 00:52:25 +00004052 case Intrinsic::objectsize: {
4053 // If we don't know by now, we're never going to know.
Eric Christopher551754c2010-04-16 23:37:20 +00004054 ConstantInt *CI = dyn_cast<ConstantInt>(I.getOperand(2));
Eric Christopher7b5e6172009-10-27 00:52:25 +00004055
4056 assert(CI && "Non-constant type in __builtin_object_size?");
4057
Eric Christopher551754c2010-04-16 23:37:20 +00004058 SDValue Arg = getValue(I.getOperand(0));
Eric Christopher7e5d2ff2009-10-28 21:32:16 +00004059 EVT Ty = Arg.getValueType();
4060
Eric Christopherd060b252009-12-23 02:51:48 +00004061 if (CI->getZExtValue() == 0)
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004062 Res = DAG.getConstant(-1ULL, Ty);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004063 else
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004064 Res = DAG.getConstant(0, Ty);
4065
4066 setValue(&I, Res);
Eric Christopher7b5e6172009-10-27 00:52:25 +00004067 return 0;
4068 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004069 case Intrinsic::var_annotation:
4070 // Discard annotate attributes
4071 return 0;
4072
4073 case Intrinsic::init_trampoline: {
Eric Christopher551754c2010-04-16 23:37:20 +00004074 const Function *F = cast<Function>(I.getOperand(2)->stripPointerCasts());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004075
4076 SDValue Ops[6];
4077 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004078 Ops[1] = getValue(I.getOperand(1));
4079 Ops[2] = getValue(I.getOperand(2));
4080 Ops[3] = getValue(I.getOperand(3));
4081 Ops[4] = DAG.getSrcValue(I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004082 Ops[5] = DAG.getSrcValue(F);
4083
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004084 Res = DAG.getNode(ISD::TRAMPOLINE, dl,
4085 DAG.getVTList(TLI.getPointerTy(), MVT::Other),
4086 Ops, 6);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004087
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004088 setValue(&I, Res);
4089 DAG.setRoot(Res.getValue(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004090 return 0;
4091 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004092 case Intrinsic::gcroot:
4093 if (GFI) {
Eric Christopher551754c2010-04-16 23:37:20 +00004094 const Value *Alloca = I.getOperand(1);
4095 const Constant *TypeMap = cast<Constant>(I.getOperand(2));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004096
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004097 FrameIndexSDNode *FI = cast<FrameIndexSDNode>(getValue(Alloca).getNode());
4098 GFI->addStackRoot(FI->getIndex(), TypeMap);
4099 }
4100 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004101 case Intrinsic::gcread:
4102 case Intrinsic::gcwrite:
Torok Edwinc23197a2009-07-14 16:55:14 +00004103 llvm_unreachable("GC failed to lower gcread/gcwrite intrinsics!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004104 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004105 case Intrinsic::flt_rounds:
Bill Wendling4533cac2010-01-28 21:51:40 +00004106 setValue(&I, DAG.getNode(ISD::FLT_ROUNDS_, dl, MVT::i32));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004107 return 0;
Bill Wendlingd0283fa2009-12-22 00:40:51 +00004108 case Intrinsic::trap:
Bill Wendling4533cac2010-01-28 21:51:40 +00004109 DAG.setRoot(DAG.getNode(ISD::TRAP, dl,MVT::Other, getRoot()));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004110 return 0;
Bill Wendlingef375462008-11-21 02:38:44 +00004111 case Intrinsic::uadd_with_overflow:
Bill Wendling74c37652008-12-09 22:08:41 +00004112 return implVisitAluOverflow(I, ISD::UADDO);
4113 case Intrinsic::sadd_with_overflow:
4114 return implVisitAluOverflow(I, ISD::SADDO);
4115 case Intrinsic::usub_with_overflow:
4116 return implVisitAluOverflow(I, ISD::USUBO);
4117 case Intrinsic::ssub_with_overflow:
4118 return implVisitAluOverflow(I, ISD::SSUBO);
4119 case Intrinsic::umul_with_overflow:
4120 return implVisitAluOverflow(I, ISD::UMULO);
4121 case Intrinsic::smul_with_overflow:
4122 return implVisitAluOverflow(I, ISD::SMULO);
Bill Wendling7cdc3c82008-11-21 02:03:52 +00004123
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004124 case Intrinsic::prefetch: {
4125 SDValue Ops[4];
4126 Ops[0] = getRoot();
Eric Christopher551754c2010-04-16 23:37:20 +00004127 Ops[1] = getValue(I.getOperand(1));
4128 Ops[2] = getValue(I.getOperand(2));
4129 Ops[3] = getValue(I.getOperand(3));
Bill Wendling4533cac2010-01-28 21:51:40 +00004130 DAG.setRoot(DAG.getNode(ISD::PREFETCH, dl, MVT::Other, &Ops[0], 4));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004131 return 0;
4132 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004133
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004134 case Intrinsic::memory_barrier: {
4135 SDValue Ops[6];
4136 Ops[0] = getRoot();
4137 for (int x = 1; x < 6; ++x)
Eric Christopher551754c2010-04-16 23:37:20 +00004138 Ops[x] = getValue(I.getOperand(x));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004139
Bill Wendling4533cac2010-01-28 21:51:40 +00004140 DAG.setRoot(DAG.getNode(ISD::MEMBARRIER, dl, MVT::Other, &Ops[0], 6));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004141 return 0;
4142 }
4143 case Intrinsic::atomic_cmp_swap: {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004144 SDValue Root = getRoot();
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004145 SDValue L =
Dale Johannesen66978ee2009-01-31 02:22:37 +00004146 DAG.getAtomic(ISD::ATOMIC_CMP_SWAP, getCurDebugLoc(),
Eric Christopher551754c2010-04-16 23:37:20 +00004147 getValue(I.getOperand(2)).getValueType().getSimpleVT(),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004148 Root,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004149 getValue(I.getOperand(1)),
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004150 getValue(I.getOperand(2)),
Eric Christopher551754c2010-04-16 23:37:20 +00004151 getValue(I.getOperand(3)),
4152 I.getOperand(1));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004153 setValue(&I, L);
4154 DAG.setRoot(L.getValue(1));
4155 return 0;
4156 }
4157 case Intrinsic::atomic_load_add:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004158 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_ADD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004159 case Intrinsic::atomic_load_sub:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004160 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_SUB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004161 case Intrinsic::atomic_load_or:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004162 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_OR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004163 case Intrinsic::atomic_load_xor:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004164 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_XOR);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004165 case Intrinsic::atomic_load_and:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004166 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_AND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004167 case Intrinsic::atomic_load_nand:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004168 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_NAND);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004169 case Intrinsic::atomic_load_max:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004170 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004171 case Intrinsic::atomic_load_min:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004172 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_MIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004173 case Intrinsic::atomic_load_umin:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004174 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMIN);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004175 case Intrinsic::atomic_load_umax:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004176 return implVisitBinaryAtomic(I, ISD::ATOMIC_LOAD_UMAX);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004177 case Intrinsic::atomic_swap:
Dan Gohman0b1d4a72008-12-23 21:37:04 +00004178 return implVisitBinaryAtomic(I, ISD::ATOMIC_SWAP);
Duncan Sandsf07c9492009-11-10 09:08:09 +00004179
4180 case Intrinsic::invariant_start:
4181 case Intrinsic::lifetime_start:
4182 // Discard region information.
Bill Wendling4533cac2010-01-28 21:51:40 +00004183 setValue(&I, DAG.getUNDEF(TLI.getPointerTy()));
Duncan Sandsf07c9492009-11-10 09:08:09 +00004184 return 0;
4185 case Intrinsic::invariant_end:
4186 case Intrinsic::lifetime_end:
4187 // Discard region information.
4188 return 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004189 }
4190}
4191
Dan Gohman46510a72010-04-15 01:51:59 +00004192void SelectionDAGBuilder::LowerCallTo(ImmutableCallSite CS, SDValue Callee,
Dan Gohman2048b852009-11-23 18:04:58 +00004193 bool isTailCall,
4194 MachineBasicBlock *LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004195 const PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
4196 const FunctionType *FTy = cast<FunctionType>(PT->getElementType());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004197 const Type *RetTy = FTy->getReturnType();
Chris Lattner512063d2010-04-05 06:19:28 +00004198 MachineModuleInfo &MMI = DAG.getMachineFunction().getMMI();
Chris Lattner16112732010-03-14 01:41:15 +00004199 MCSymbol *BeginLabel = 0;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004200
4201 TargetLowering::ArgListTy Args;
4202 TargetLowering::ArgListEntry Entry;
4203 Args.reserve(CS.arg_size());
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004204
4205 // Check whether the function can return without sret-demotion.
4206 SmallVector<EVT, 4> OutVTs;
4207 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
4208 SmallVector<uint64_t, 4> Offsets;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004209 getReturnInfo(RetTy, CS.getAttributes().getRetAttributes(),
Bill Wendlinge80ae832009-12-22 00:50:32 +00004210 OutVTs, OutsFlags, TLI, &Offsets);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004211
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004212 bool CanLowerReturn = TLI.CanLowerReturn(CS.getCallingConv(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004213 FTy->isVarArg(), OutVTs, OutsFlags, DAG);
4214
4215 SDValue DemoteStackSlot;
4216
4217 if (!CanLowerReturn) {
4218 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(
4219 FTy->getReturnType());
4220 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(
4221 FTy->getReturnType());
4222 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00004223 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004224 const Type *StackSlotPtrType = PointerType::getUnqual(FTy->getReturnType());
4225
4226 DemoteStackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
4227 Entry.Node = DemoteStackSlot;
4228 Entry.Ty = StackSlotPtrType;
4229 Entry.isSExt = false;
4230 Entry.isZExt = false;
4231 Entry.isInReg = false;
4232 Entry.isSRet = true;
4233 Entry.isNest = false;
4234 Entry.isByVal = false;
4235 Entry.Alignment = Align;
4236 Args.push_back(Entry);
4237 RetTy = Type::getVoidTy(FTy->getContext());
4238 }
4239
Dan Gohman46510a72010-04-15 01:51:59 +00004240 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004241 i != e; ++i) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004242 SDValue ArgNode = getValue(*i);
4243 Entry.Node = ArgNode; Entry.Ty = (*i)->getType();
4244
4245 unsigned attrInd = i - CS.arg_begin() + 1;
Devang Patel05988662008-09-25 21:00:45 +00004246 Entry.isSExt = CS.paramHasAttr(attrInd, Attribute::SExt);
4247 Entry.isZExt = CS.paramHasAttr(attrInd, Attribute::ZExt);
4248 Entry.isInReg = CS.paramHasAttr(attrInd, Attribute::InReg);
4249 Entry.isSRet = CS.paramHasAttr(attrInd, Attribute::StructRet);
4250 Entry.isNest = CS.paramHasAttr(attrInd, Attribute::Nest);
4251 Entry.isByVal = CS.paramHasAttr(attrInd, Attribute::ByVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004252 Entry.Alignment = CS.getParamAlignment(attrInd);
4253 Args.push_back(Entry);
4254 }
4255
Chris Lattner512063d2010-04-05 06:19:28 +00004256 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004257 // Insert a label before the invoke call to mark the try range. This can be
4258 // used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004259 BeginLabel = MMI.getContext().CreateTempSymbol();
Jim Grosbach1b747ad2009-08-11 00:09:57 +00004260
Jim Grosbachca752c92010-01-28 01:45:32 +00004261 // For SjLj, keep track of which landing pads go with which invokes
4262 // so as to maintain the ordering of pads in the LSDA.
Chris Lattner512063d2010-04-05 06:19:28 +00004263 unsigned CallSiteIndex = MMI.getCurrentCallSite();
Jim Grosbachca752c92010-01-28 01:45:32 +00004264 if (CallSiteIndex) {
Chris Lattner512063d2010-04-05 06:19:28 +00004265 MMI.setCallSiteBeginLabel(BeginLabel, CallSiteIndex);
Jim Grosbachca752c92010-01-28 01:45:32 +00004266 // Now that the call site is handled, stop tracking it.
Chris Lattner512063d2010-04-05 06:19:28 +00004267 MMI.setCurrentCallSite(0);
Jim Grosbachca752c92010-01-28 01:45:32 +00004268 }
4269
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004270 // Both PendingLoads and PendingExports must be flushed here;
4271 // this call might not return.
4272 (void)getRoot();
Chris Lattner7561d482010-03-14 02:33:54 +00004273 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getControlRoot(), BeginLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004274 }
4275
Dan Gohman98ca4f22009-08-05 01:29:28 +00004276 // Check if target-independent constraints permit a tail call here.
4277 // Target-dependent constraints are checked within TLI.LowerCallTo.
4278 if (isTailCall &&
Evan Cheng86809cc2010-02-03 03:28:02 +00004279 !isInTailCallPosition(CS, CS.getAttributes().getRetAttributes(), TLI))
Dan Gohman98ca4f22009-08-05 01:29:28 +00004280 isTailCall = false;
4281
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004282 std::pair<SDValue,SDValue> Result =
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004283 TLI.LowerCallTo(getRoot(), RetTy,
Devang Patel05988662008-09-25 21:00:45 +00004284 CS.paramHasAttr(0, Attribute::SExt),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004285 CS.paramHasAttr(0, Attribute::ZExt), FTy->isVarArg(),
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00004286 CS.paramHasAttr(0, Attribute::InReg), FTy->getNumParams(),
Dale Johannesen86098bd2008-09-26 19:31:26 +00004287 CS.getCallingConv(),
Dan Gohman98ca4f22009-08-05 01:29:28 +00004288 isTailCall,
4289 !CS.getInstruction()->use_empty(),
Bill Wendling46ada192010-03-02 01:55:18 +00004290 Callee, Args, DAG, getCurDebugLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00004291 assert((isTailCall || Result.second.getNode()) &&
4292 "Non-null chain expected with non-tail call!");
4293 assert((Result.second.getNode() || !Result.first.getNode()) &&
4294 "Null value expected with tail call!");
Bill Wendlinge80ae832009-12-22 00:50:32 +00004295 if (Result.first.getNode()) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004296 setValue(CS.getInstruction(), Result.first);
Bill Wendlinge80ae832009-12-22 00:50:32 +00004297 } else if (!CanLowerReturn && Result.second.getNode()) {
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004298 // The instruction result is the result of loading from the
4299 // hidden sret parameter.
4300 SmallVector<EVT, 1> PVTs;
4301 const Type *PtrRetTy = PointerType::getUnqual(FTy->getReturnType());
4302
4303 ComputeValueVTs(TLI, PtrRetTy, PVTs);
4304 assert(PVTs.size() == 1 && "Pointers should fit in one register");
4305 EVT PtrVT = PVTs[0];
4306 unsigned NumValues = OutVTs.size();
4307 SmallVector<SDValue, 4> Values(NumValues);
4308 SmallVector<SDValue, 4> Chains(NumValues);
4309
4310 for (unsigned i = 0; i < NumValues; ++i) {
Bill Wendlinge80ae832009-12-22 00:50:32 +00004311 SDValue Add = DAG.getNode(ISD::ADD, getCurDebugLoc(), PtrVT,
4312 DemoteStackSlot,
4313 DAG.getConstant(Offsets[i], PtrVT));
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004314 SDValue L = DAG.getLoad(OutVTs[i], getCurDebugLoc(), Result.second,
David Greene1e559442010-02-15 17:00:31 +00004315 Add, NULL, Offsets[i], false, false, 1);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004316 Values[i] = L;
4317 Chains[i] = L.getValue(1);
4318 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004319
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004320 SDValue Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(),
4321 MVT::Other, &Chains[0], NumValues);
4322 PendingLoads.push_back(Chain);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004323
4324 // Collect the legal value parts into potentially illegal values
4325 // that correspond to the original function's return values.
4326 SmallVector<EVT, 4> RetTys;
4327 RetTy = FTy->getReturnType();
4328 ComputeValueVTs(TLI, RetTy, RetTys);
4329 ISD::NodeType AssertOp = ISD::DELETED_NODE;
4330 SmallVector<SDValue, 4> ReturnValues;
4331 unsigned CurReg = 0;
4332 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
4333 EVT VT = RetTys[I];
4334 EVT RegisterVT = TLI.getRegisterType(RetTy->getContext(), VT);
4335 unsigned NumRegs = TLI.getNumRegisters(RetTy->getContext(), VT);
4336
4337 SDValue ReturnValue =
Bill Wendling46ada192010-03-02 01:55:18 +00004338 getCopyFromParts(DAG, getCurDebugLoc(), &Values[CurReg], NumRegs,
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004339 RegisterVT, VT, AssertOp);
4340 ReturnValues.push_back(ReturnValue);
Kenneth Uildriks93ae4072010-01-16 23:37:33 +00004341 CurReg += NumRegs;
4342 }
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004343
Bill Wendling4533cac2010-01-28 21:51:40 +00004344 setValue(CS.getInstruction(),
4345 DAG.getNode(ISD::MERGE_VALUES, getCurDebugLoc(),
4346 DAG.getVTList(&RetTys[0], RetTys.size()),
4347 &ReturnValues[0], ReturnValues.size()));
Bill Wendlinge80ae832009-12-22 00:50:32 +00004348
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00004349 }
Bill Wendlinge80ae832009-12-22 00:50:32 +00004350
4351 // As a special case, a null chain means that a tail call has been emitted and
4352 // the DAG root is already updated.
Bill Wendling4533cac2010-01-28 21:51:40 +00004353 if (Result.second.getNode())
Dan Gohman98ca4f22009-08-05 01:29:28 +00004354 DAG.setRoot(Result.second);
Bill Wendling4533cac2010-01-28 21:51:40 +00004355 else
Dan Gohman98ca4f22009-08-05 01:29:28 +00004356 HasTailCall = true;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004357
Chris Lattner512063d2010-04-05 06:19:28 +00004358 if (LandingPad) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004359 // Insert a label at the end of the invoke call to mark the try range. This
4360 // can be used to detect deletion of the invoke via the MachineModuleInfo.
Chris Lattner512063d2010-04-05 06:19:28 +00004361 MCSymbol *EndLabel = MMI.getContext().CreateTempSymbol();
Chris Lattner7561d482010-03-14 02:33:54 +00004362 DAG.setRoot(DAG.getEHLabel(getCurDebugLoc(), getRoot(), EndLabel));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004363
4364 // Inform MachineModuleInfo of range.
Chris Lattner512063d2010-04-05 06:19:28 +00004365 MMI.addInvoke(LandingPad, BeginLabel, EndLabel);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004366 }
4367}
4368
Chris Lattner8047d9a2009-12-24 00:37:38 +00004369/// IsOnlyUsedInZeroEqualityComparison - Return true if it only matters that the
4370/// value is equal or not-equal to zero.
Dan Gohman46510a72010-04-15 01:51:59 +00004371static bool IsOnlyUsedInZeroEqualityComparison(const Value *V) {
4372 for (Value::const_use_iterator UI = V->use_begin(), E = V->use_end();
Chris Lattner8047d9a2009-12-24 00:37:38 +00004373 UI != E; ++UI) {
Dan Gohman46510a72010-04-15 01:51:59 +00004374 if (const ICmpInst *IC = dyn_cast<ICmpInst>(*UI))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004375 if (IC->isEquality())
Dan Gohman46510a72010-04-15 01:51:59 +00004376 if (const Constant *C = dyn_cast<Constant>(IC->getOperand(1)))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004377 if (C->isNullValue())
4378 continue;
4379 // Unknown instruction.
4380 return false;
4381 }
4382 return true;
4383}
4384
Dan Gohman46510a72010-04-15 01:51:59 +00004385static SDValue getMemCmpLoad(const Value *PtrVal, MVT LoadVT,
4386 const Type *LoadTy,
Chris Lattner8047d9a2009-12-24 00:37:38 +00004387 SelectionDAGBuilder &Builder) {
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004388
Chris Lattner8047d9a2009-12-24 00:37:38 +00004389 // Check to see if this load can be trivially constant folded, e.g. if the
4390 // input is from a string literal.
Dan Gohman46510a72010-04-15 01:51:59 +00004391 if (const Constant *LoadInput = dyn_cast<Constant>(PtrVal)) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004392 // Cast pointer to the type we really want to load.
Dan Gohman46510a72010-04-15 01:51:59 +00004393 LoadInput = ConstantExpr::getBitCast(const_cast<Constant *>(LoadInput),
Chris Lattner8047d9a2009-12-24 00:37:38 +00004394 PointerType::getUnqual(LoadTy));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004395
Dan Gohman46510a72010-04-15 01:51:59 +00004396 if (const Constant *LoadCst =
4397 ConstantFoldLoadFromConstPtr(const_cast<Constant *>(LoadInput),
4398 Builder.TD))
Chris Lattner8047d9a2009-12-24 00:37:38 +00004399 return Builder.getValue(LoadCst);
4400 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004401
Chris Lattner8047d9a2009-12-24 00:37:38 +00004402 // Otherwise, we have to emit the load. If the pointer is to unfoldable but
4403 // still constant memory, the input chain can be the entry node.
4404 SDValue Root;
4405 bool ConstantMemory = false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004406
Chris Lattner8047d9a2009-12-24 00:37:38 +00004407 // Do not serialize (non-volatile) loads of constant memory with anything.
4408 if (Builder.AA->pointsToConstantMemory(PtrVal)) {
4409 Root = Builder.DAG.getEntryNode();
4410 ConstantMemory = true;
4411 } else {
4412 // Do not serialize non-volatile loads against each other.
4413 Root = Builder.DAG.getRoot();
4414 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004415
Chris Lattner8047d9a2009-12-24 00:37:38 +00004416 SDValue Ptr = Builder.getValue(PtrVal);
4417 SDValue LoadVal = Builder.DAG.getLoad(LoadVT, Builder.getCurDebugLoc(), Root,
4418 Ptr, PtrVal /*SrcValue*/, 0/*SVOffset*/,
David Greene1e559442010-02-15 17:00:31 +00004419 false /*volatile*/,
4420 false /*nontemporal*/, 1 /* align=1 */);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004421
Chris Lattner8047d9a2009-12-24 00:37:38 +00004422 if (!ConstantMemory)
4423 Builder.PendingLoads.push_back(LoadVal.getValue(1));
4424 return LoadVal;
4425}
4426
4427
4428/// visitMemCmpCall - See if we can lower a call to memcmp in an optimized form.
4429/// If so, return true and lower it, otherwise return false and it will be
4430/// lowered like a normal call.
Dan Gohman46510a72010-04-15 01:51:59 +00004431bool SelectionDAGBuilder::visitMemCmpCall(const CallInst &I) {
Chris Lattner8047d9a2009-12-24 00:37:38 +00004432 // Verify that the prototype makes sense. int memcmp(void*,void*,size_t)
4433 if (I.getNumOperands() != 4)
4434 return false;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004435
Eric Christopher551754c2010-04-16 23:37:20 +00004436 const Value *LHS = I.getOperand(1), *RHS = I.getOperand(2);
Duncan Sands1df98592010-02-16 11:11:14 +00004437 if (!LHS->getType()->isPointerTy() || !RHS->getType()->isPointerTy() ||
Eric Christopher551754c2010-04-16 23:37:20 +00004438 !I.getOperand(3)->getType()->isIntegerTy() ||
Duncan Sands1df98592010-02-16 11:11:14 +00004439 !I.getType()->isIntegerTy())
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004440 return false;
4441
Eric Christopher551754c2010-04-16 23:37:20 +00004442 const ConstantInt *Size = dyn_cast<ConstantInt>(I.getOperand(3));
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004443
Chris Lattner8047d9a2009-12-24 00:37:38 +00004444 // memcmp(S1,S2,2) != 0 -> (*(short*)LHS != *(short*)RHS) != 0
4445 // memcmp(S1,S2,4) != 0 -> (*(int*)LHS != *(int*)RHS) != 0
Chris Lattner04b091a2009-12-24 01:07:17 +00004446 if (Size && IsOnlyUsedInZeroEqualityComparison(&I)) {
4447 bool ActuallyDoIt = true;
4448 MVT LoadVT;
4449 const Type *LoadTy;
4450 switch (Size->getZExtValue()) {
4451 default:
4452 LoadVT = MVT::Other;
4453 LoadTy = 0;
4454 ActuallyDoIt = false;
4455 break;
4456 case 2:
4457 LoadVT = MVT::i16;
4458 LoadTy = Type::getInt16Ty(Size->getContext());
4459 break;
4460 case 4:
4461 LoadVT = MVT::i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004462 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004463 break;
4464 case 8:
4465 LoadVT = MVT::i64;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004466 LoadTy = Type::getInt64Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004467 break;
4468 /*
4469 case 16:
4470 LoadVT = MVT::v4i32;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004471 LoadTy = Type::getInt32Ty(Size->getContext());
Chris Lattner04b091a2009-12-24 01:07:17 +00004472 LoadTy = VectorType::get(LoadTy, 4);
4473 break;
4474 */
4475 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004476
Chris Lattner04b091a2009-12-24 01:07:17 +00004477 // This turns into unaligned loads. We only do this if the target natively
4478 // supports the MVT we'll be loading or if it is small enough (<= 4) that
4479 // we'll only produce a small number of byte loads.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004480
Chris Lattner04b091a2009-12-24 01:07:17 +00004481 // Require that we can find a legal MVT, and only do this if the target
4482 // supports unaligned loads of that type. Expanding into byte loads would
4483 // bloat the code.
4484 if (ActuallyDoIt && Size->getZExtValue() > 4) {
4485 // TODO: Handle 5 byte compare as 4-byte + 1 byte.
4486 // TODO: Handle 8 byte compare on x86-32 as two 32-bit loads.
4487 if (!TLI.isTypeLegal(LoadVT) ||!TLI.allowsUnalignedMemoryAccesses(LoadVT))
4488 ActuallyDoIt = false;
4489 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004490
Chris Lattner04b091a2009-12-24 01:07:17 +00004491 if (ActuallyDoIt) {
4492 SDValue LHSVal = getMemCmpLoad(LHS, LoadVT, LoadTy, *this);
4493 SDValue RHSVal = getMemCmpLoad(RHS, LoadVT, LoadTy, *this);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004494
Chris Lattner04b091a2009-12-24 01:07:17 +00004495 SDValue Res = DAG.getSetCC(getCurDebugLoc(), MVT::i1, LHSVal, RHSVal,
4496 ISD::SETNE);
4497 EVT CallVT = TLI.getValueType(I.getType(), true);
4498 setValue(&I, DAG.getZExtOrTrunc(Res, getCurDebugLoc(), CallVT));
4499 return true;
4500 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004501 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004502
4503
Chris Lattner8047d9a2009-12-24 00:37:38 +00004504 return false;
4505}
4506
4507
Dan Gohman46510a72010-04-15 01:51:59 +00004508void SelectionDAGBuilder::visitCall(const CallInst &I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004509 const char *RenameFn = 0;
4510 if (Function *F = I.getCalledFunction()) {
4511 if (F->isDeclaration()) {
Dan Gohman55e59c12010-04-19 19:05:59 +00004512 const TargetIntrinsicInfo *II = TM.getIntrinsicInfo();
Dale Johannesen49de9822009-02-05 01:49:45 +00004513 if (II) {
4514 if (unsigned IID = II->getIntrinsicID(F)) {
4515 RenameFn = visitIntrinsicCall(I, IID);
4516 if (!RenameFn)
4517 return;
4518 }
4519 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004520 if (unsigned IID = F->getIntrinsicID()) {
4521 RenameFn = visitIntrinsicCall(I, IID);
4522 if (!RenameFn)
4523 return;
4524 }
4525 }
4526
4527 // Check for well-known libc/libm calls. If the function is internal, it
4528 // can't be a library call.
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004529 if (!F->hasLocalLinkage() && F->hasName()) {
4530 StringRef Name = F->getName();
Duncan Sandsd2c817e2010-03-14 21:08:40 +00004531 if (Name == "copysign" || Name == "copysignf" || Name == "copysignl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004532 if (I.getNumOperands() == 3 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004533 I.getOperand(1)->getType()->isFloatingPointTy() &&
4534 I.getType() == I.getOperand(1)->getType() &&
4535 I.getType() == I.getOperand(2)->getType()) {
4536 SDValue LHS = getValue(I.getOperand(1));
4537 SDValue RHS = getValue(I.getOperand(2));
Bill Wendling0d580132009-12-23 01:28:19 +00004538 setValue(&I, DAG.getNode(ISD::FCOPYSIGN, getCurDebugLoc(),
4539 LHS.getValueType(), LHS, RHS));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004540 return;
4541 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004542 } else if (Name == "fabs" || Name == "fabsf" || Name == "fabsl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004543 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004544 I.getOperand(1)->getType()->isFloatingPointTy() &&
4545 I.getType() == I.getOperand(1)->getType()) {
4546 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004547 setValue(&I, DAG.getNode(ISD::FABS, getCurDebugLoc(),
4548 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004549 return;
4550 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004551 } else if (Name == "sin" || Name == "sinf" || Name == "sinl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004552 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004553 I.getOperand(1)->getType()->isFloatingPointTy() &&
4554 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004555 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004556 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004557 setValue(&I, DAG.getNode(ISD::FSIN, getCurDebugLoc(),
4558 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004559 return;
4560 }
Daniel Dunbarf0443c12009-07-26 08:34:35 +00004561 } else if (Name == "cos" || Name == "cosf" || Name == "cosl") {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004562 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004563 I.getOperand(1)->getType()->isFloatingPointTy() &&
4564 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004565 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004566 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004567 setValue(&I, DAG.getNode(ISD::FCOS, getCurDebugLoc(),
4568 Tmp.getValueType(), Tmp));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004569 return;
4570 }
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004571 } else if (Name == "sqrt" || Name == "sqrtf" || Name == "sqrtl") {
4572 if (I.getNumOperands() == 2 && // Basic sanity checks.
Eric Christopher551754c2010-04-16 23:37:20 +00004573 I.getOperand(1)->getType()->isFloatingPointTy() &&
4574 I.getType() == I.getOperand(1)->getType() &&
Dale Johannesena45bfd32009-09-25 18:00:35 +00004575 I.onlyReadsMemory()) {
Eric Christopher551754c2010-04-16 23:37:20 +00004576 SDValue Tmp = getValue(I.getOperand(1));
Bill Wendling0d580132009-12-23 01:28:19 +00004577 setValue(&I, DAG.getNode(ISD::FSQRT, getCurDebugLoc(),
4578 Tmp.getValueType(), Tmp));
Dale Johannesen52fb79b2009-09-25 17:23:22 +00004579 return;
4580 }
Chris Lattner8047d9a2009-12-24 00:37:38 +00004581 } else if (Name == "memcmp") {
4582 if (visitMemCmpCall(I))
4583 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004584 }
4585 }
Eric Christopher551754c2010-04-16 23:37:20 +00004586 } else if (isa<InlineAsm>(I.getOperand(0))) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004587 visitInlineAsm(&I);
4588 return;
4589 }
4590
4591 SDValue Callee;
4592 if (!RenameFn)
Eric Christopher551754c2010-04-16 23:37:20 +00004593 Callee = getValue(I.getOperand(0));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004594 else
Bill Wendling056292f2008-09-16 21:48:12 +00004595 Callee = DAG.getExternalSymbol(RenameFn, TLI.getPointerTy());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004596
Bill Wendling0d580132009-12-23 01:28:19 +00004597 // Check if we can potentially perform a tail call. More detailed checking is
4598 // be done within LowerCallTo, after more information about the call is known.
Evan Cheng11e67932010-01-26 23:13:04 +00004599 LowerCallTo(&I, Callee, I.isTailCall());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004600}
4601
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004602/// getCopyFromRegs - Emit a series of CopyFromReg nodes that copies from
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004603/// this value and returns the result as a ValueVT value. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004604/// Chain/Flag as the input and updates them for the output Chain/Flag.
4605/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004606SDValue RegsForValue::getCopyFromRegs(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004607 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004608 // Assemble the legal parts into the final values.
4609 SmallVector<SDValue, 4> Values(ValueVTs.size());
4610 SmallVector<SDValue, 8> Parts;
4611 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
4612 // Copy the legal parts from the registers.
Owen Andersone50ed302009-08-10 22:56:29 +00004613 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004614 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004615 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004616
4617 Parts.resize(NumRegs);
4618 for (unsigned i = 0; i != NumRegs; ++i) {
4619 SDValue P;
Bill Wendlingec72e322009-12-22 01:11:43 +00004620 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004621 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT);
Bill Wendlingec72e322009-12-22 01:11:43 +00004622 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004623 P = DAG.getCopyFromReg(Chain, dl, Regs[Part+i], RegisterVT, *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004624 *Flag = P.getValue(2);
4625 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004626
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004627 Chain = P.getValue(1);
Bill Wendlingec72e322009-12-22 01:11:43 +00004628
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004629 // If the source register was virtual and if we know something about it,
4630 // add an assert node.
4631 if (TargetRegisterInfo::isVirtualRegister(Regs[Part+i]) &&
4632 RegisterVT.isInteger() && !RegisterVT.isVector()) {
4633 unsigned SlotNo = Regs[Part+i]-TargetRegisterInfo::FirstVirtualRegister;
4634 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
4635 if (FLI.LiveOutRegInfo.size() > SlotNo) {
4636 FunctionLoweringInfo::LiveOutInfo &LOI = FLI.LiveOutRegInfo[SlotNo];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004637
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004638 unsigned RegSize = RegisterVT.getSizeInBits();
4639 unsigned NumSignBits = LOI.NumSignBits;
4640 unsigned NumZeroBits = LOI.KnownZero.countLeadingOnes();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004641
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004642 // FIXME: We capture more information than the dag can represent. For
4643 // now, just use the tightest assertzext/assertsext possible.
4644 bool isSExt = true;
Owen Anderson825b72b2009-08-11 20:47:22 +00004645 EVT FromVT(MVT::Other);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004646 if (NumSignBits == RegSize)
Owen Anderson825b72b2009-08-11 20:47:22 +00004647 isSExt = true, FromVT = MVT::i1; // ASSERT SEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004648 else if (NumZeroBits >= RegSize-1)
Owen Anderson825b72b2009-08-11 20:47:22 +00004649 isSExt = false, FromVT = MVT::i1; // ASSERT ZEXT 1
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004650 else if (NumSignBits > RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004651 isSExt = true, FromVT = MVT::i8; // ASSERT SEXT 8
Dan Gohman07c26ee2009-03-31 01:38:29 +00004652 else if (NumZeroBits >= RegSize-8)
Owen Anderson825b72b2009-08-11 20:47:22 +00004653 isSExt = false, FromVT = MVT::i8; // ASSERT ZEXT 8
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004654 else if (NumSignBits > RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004655 isSExt = true, FromVT = MVT::i16; // ASSERT SEXT 16
Dan Gohman07c26ee2009-03-31 01:38:29 +00004656 else if (NumZeroBits >= RegSize-16)
Owen Anderson825b72b2009-08-11 20:47:22 +00004657 isSExt = false, FromVT = MVT::i16; // ASSERT ZEXT 16
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004658 else if (NumSignBits > RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004659 isSExt = true, FromVT = MVT::i32; // ASSERT SEXT 32
Dan Gohman07c26ee2009-03-31 01:38:29 +00004660 else if (NumZeroBits >= RegSize-32)
Owen Anderson825b72b2009-08-11 20:47:22 +00004661 isSExt = false, FromVT = MVT::i32; // ASSERT ZEXT 32
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004662
Bill Wendling4533cac2010-01-28 21:51:40 +00004663 if (FromVT != MVT::Other)
Dale Johannesen66978ee2009-01-31 02:22:37 +00004664 P = DAG.getNode(isSExt ? ISD::AssertSext : ISD::AssertZext, dl,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004665 RegisterVT, P, DAG.getValueType(FromVT));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004666 }
4667 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004668
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004669 Parts[i] = P;
4670 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004671
Bill Wendling46ada192010-03-02 01:55:18 +00004672 Values[Value] = getCopyFromParts(DAG, dl, Parts.begin(),
Dale Johannesen66978ee2009-01-31 02:22:37 +00004673 NumRegs, RegisterVT, ValueVT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004674 Part += NumRegs;
4675 Parts.clear();
4676 }
4677
Bill Wendling4533cac2010-01-28 21:51:40 +00004678 return DAG.getNode(ISD::MERGE_VALUES, dl,
4679 DAG.getVTList(&ValueVTs[0], ValueVTs.size()),
4680 &Values[0], ValueVTs.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004681}
4682
4683/// getCopyToRegs - Emit a series of CopyToReg nodes that copies the
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004684/// specified value into the registers specified by this object. This uses
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004685/// Chain/Flag as the input and updates them for the output Chain/Flag.
4686/// If the Flag pointer is NULL, no flag is used.
Dale Johannesen66978ee2009-01-31 02:22:37 +00004687void RegsForValue::getCopyToRegs(SDValue Val, SelectionDAG &DAG, DebugLoc dl,
Bill Wendling46ada192010-03-02 01:55:18 +00004688 SDValue &Chain, SDValue *Flag) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004689 // Get the list of the values's legal parts.
4690 unsigned NumRegs = Regs.size();
4691 SmallVector<SDValue, 8> Parts(NumRegs);
4692 for (unsigned Value = 0, Part = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00004693 EVT ValueVT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00004694 unsigned NumParts = TLI->getNumRegisters(*DAG.getContext(), ValueVT);
Owen Andersone50ed302009-08-10 22:56:29 +00004695 EVT RegisterVT = RegVTs[Value];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004696
Bill Wendling46ada192010-03-02 01:55:18 +00004697 getCopyToParts(DAG, dl,
Bill Wendling3ea3c242009-12-22 02:10:19 +00004698 Val.getValue(Val.getResNo() + Value),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004699 &Parts[Part], NumParts, RegisterVT);
4700 Part += NumParts;
4701 }
4702
4703 // Copy the parts into the registers.
4704 SmallVector<SDValue, 8> Chains(NumRegs);
4705 for (unsigned i = 0; i != NumRegs; ++i) {
4706 SDValue Part;
Bill Wendlingec72e322009-12-22 01:11:43 +00004707 if (Flag == 0) {
Dale Johannesena04b7572009-02-03 23:04:43 +00004708 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i]);
Bill Wendlingec72e322009-12-22 01:11:43 +00004709 } else {
Dale Johannesena04b7572009-02-03 23:04:43 +00004710 Part = DAG.getCopyToReg(Chain, dl, Regs[i], Parts[i], *Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004711 *Flag = Part.getValue(1);
4712 }
Bill Wendlingec72e322009-12-22 01:11:43 +00004713
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004714 Chains[i] = Part.getValue(0);
4715 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004716
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004717 if (NumRegs == 1 || Flag)
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004718 // If NumRegs > 1 && Flag is used then the use of the last CopyToReg is
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004719 // flagged to it. That is the CopyToReg nodes and the user are considered
4720 // a single scheduling unit. If we create a TokenFactor and return it as
4721 // chain, then the TokenFactor is both a predecessor (operand) of the
4722 // user as well as a successor (the TF operands are flagged to the user).
4723 // c1, f1 = CopyToReg
4724 // c2, f2 = CopyToReg
4725 // c3 = TokenFactor c1, c2
4726 // ...
4727 // = op c3, ..., f2
4728 Chain = Chains[NumRegs-1];
4729 else
Owen Anderson825b72b2009-08-11 20:47:22 +00004730 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, &Chains[0], NumRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004731}
4732
4733/// AddInlineAsmOperands - Add this value to the specified inlineasm node
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004734/// operand list. This adds the code marker and includes the number of
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004735/// values added into it.
Chris Lattnerdecc2672010-04-07 05:20:54 +00004736void RegsForValue::AddInlineAsmOperands(unsigned Code, bool HasMatching,
4737 unsigned MatchingIdx,
Bill Wendling46ada192010-03-02 01:55:18 +00004738 SelectionDAG &DAG,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004739 std::vector<SDValue> &Ops) const {
Chris Lattnerdecc2672010-04-07 05:20:54 +00004740 unsigned Flag = InlineAsm::getFlagWord(Code, Regs.size());
Evan Cheng697cbbf2009-03-20 18:03:34 +00004741 if (HasMatching)
Chris Lattnerdecc2672010-04-07 05:20:54 +00004742 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, MatchingIdx);
Dale Johannesen99499332009-12-23 07:32:51 +00004743 SDValue Res = DAG.getTargetConstant(Flag, MVT::i32);
Bill Wendling651ad132009-12-22 01:25:10 +00004744 Ops.push_back(Res);
4745
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004746 for (unsigned Value = 0, Reg = 0, e = ValueVTs.size(); Value != e; ++Value) {
Owen Anderson23b9b192009-08-12 00:36:31 +00004747 unsigned NumRegs = TLI->getNumRegisters(*DAG.getContext(), ValueVTs[Value]);
Owen Andersone50ed302009-08-10 22:56:29 +00004748 EVT RegisterVT = RegVTs[Value];
Chris Lattner58f15c42008-10-17 16:21:11 +00004749 for (unsigned i = 0; i != NumRegs; ++i) {
4750 assert(Reg < Regs.size() && "Mismatch in # registers expected");
Bill Wendling4533cac2010-01-28 21:51:40 +00004751 Ops.push_back(DAG.getRegister(Regs[Reg++], RegisterVT));
Chris Lattner58f15c42008-10-17 16:21:11 +00004752 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004753 }
4754}
4755
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004756/// isAllocatableRegister - If the specified register is safe to allocate,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004757/// i.e. it isn't a stack pointer or some other special register, return the
4758/// register class for the register. Otherwise, return null.
4759static const TargetRegisterClass *
4760isAllocatableRegister(unsigned Reg, MachineFunction &MF,
4761 const TargetLowering &TLI,
4762 const TargetRegisterInfo *TRI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004763 EVT FoundVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004764 const TargetRegisterClass *FoundRC = 0;
4765 for (TargetRegisterInfo::regclass_iterator RCI = TRI->regclass_begin(),
4766 E = TRI->regclass_end(); RCI != E; ++RCI) {
Owen Anderson825b72b2009-08-11 20:47:22 +00004767 EVT ThisVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004768
4769 const TargetRegisterClass *RC = *RCI;
Dan Gohmanf451cb82010-02-10 16:03:48 +00004770 // If none of the value types for this register class are valid, we
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004771 // can't use it. For example, 64-bit reg classes on 32-bit targets.
4772 for (TargetRegisterClass::vt_iterator I = RC->vt_begin(), E = RC->vt_end();
4773 I != E; ++I) {
4774 if (TLI.isTypeLegal(*I)) {
4775 // If we have already found this register in a different register class,
4776 // choose the one with the largest VT specified. For example, on
4777 // PowerPC, we favor f64 register classes over f32.
Owen Anderson825b72b2009-08-11 20:47:22 +00004778 if (FoundVT == MVT::Other || FoundVT.bitsLT(*I)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004779 ThisVT = *I;
4780 break;
4781 }
4782 }
4783 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004784
Owen Anderson825b72b2009-08-11 20:47:22 +00004785 if (ThisVT == MVT::Other) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004786
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004787 // NOTE: This isn't ideal. In particular, this might allocate the
4788 // frame pointer in functions that need it (due to them not being taken
4789 // out of allocation, because a variable sized allocation hasn't been seen
4790 // yet). This is a slight code pessimization, but should still work.
4791 for (TargetRegisterClass::iterator I = RC->allocation_order_begin(MF),
4792 E = RC->allocation_order_end(MF); I != E; ++I)
4793 if (*I == Reg) {
4794 // We found a matching register class. Keep looking at others in case
4795 // we find one with larger registers that this physreg is also in.
4796 FoundRC = RC;
4797 FoundVT = ThisVT;
4798 break;
4799 }
4800 }
4801 return FoundRC;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004802}
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004803
4804
4805namespace llvm {
4806/// AsmOperandInfo - This contains information for each constraint that we are
4807/// lowering.
Cedric Venetaff9c272009-02-14 16:06:42 +00004808class VISIBILITY_HIDDEN SDISelAsmOperandInfo :
Daniel Dunbarc0c3b9a2008-09-10 04:16:29 +00004809 public TargetLowering::AsmOperandInfo {
Cedric Venetaff9c272009-02-14 16:06:42 +00004810public:
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004811 /// CallOperand - If this is the result output operand or a clobber
4812 /// this is null, otherwise it is the incoming operand to the CallInst.
4813 /// This gets modified as the asm is processed.
4814 SDValue CallOperand;
4815
4816 /// AssignedRegs - If this is a register or register class operand, this
4817 /// contains the set of register corresponding to the operand.
4818 RegsForValue AssignedRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004819
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004820 explicit SDISelAsmOperandInfo(const InlineAsm::ConstraintInfo &info)
4821 : TargetLowering::AsmOperandInfo(info), CallOperand(0,0) {
4822 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004823
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004824 /// MarkAllocatedRegs - Once AssignedRegs is set, mark the assigned registers
4825 /// busy in OutputRegs/InputRegs.
4826 void MarkAllocatedRegs(bool isOutReg, bool isInReg,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004827 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004828 std::set<unsigned> &InputRegs,
4829 const TargetRegisterInfo &TRI) const {
4830 if (isOutReg) {
4831 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4832 MarkRegAndAliases(AssignedRegs.Regs[i], OutputRegs, TRI);
4833 }
4834 if (isInReg) {
4835 for (unsigned i = 0, e = AssignedRegs.Regs.size(); i != e; ++i)
4836 MarkRegAndAliases(AssignedRegs.Regs[i], InputRegs, TRI);
4837 }
4838 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004839
Owen Andersone50ed302009-08-10 22:56:29 +00004840 /// getCallOperandValEVT - Return the EVT of the Value* that this operand
Chris Lattner81249c92008-10-17 17:05:25 +00004841 /// corresponds to. If there is no Value* for this operand, it returns
Owen Anderson825b72b2009-08-11 20:47:22 +00004842 /// MVT::Other.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004843 EVT getCallOperandValEVT(LLVMContext &Context,
Owen Anderson1d0be152009-08-13 21:58:54 +00004844 const TargetLowering &TLI,
Chris Lattner81249c92008-10-17 17:05:25 +00004845 const TargetData *TD) const {
Owen Anderson825b72b2009-08-11 20:47:22 +00004846 if (CallOperandVal == 0) return MVT::Other;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004847
Chris Lattner81249c92008-10-17 17:05:25 +00004848 if (isa<BasicBlock>(CallOperandVal))
4849 return TLI.getPointerTy();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004850
Chris Lattner81249c92008-10-17 17:05:25 +00004851 const llvm::Type *OpTy = CallOperandVal->getType();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004852
Chris Lattner81249c92008-10-17 17:05:25 +00004853 // If this is an indirect operand, the operand is a pointer to the
4854 // accessed type.
Bob Wilsone261b0c2009-12-22 18:34:19 +00004855 if (isIndirect) {
4856 const llvm::PointerType *PtrTy = dyn_cast<PointerType>(OpTy);
4857 if (!PtrTy)
Chris Lattner75361b62010-04-07 22:58:41 +00004858 report_fatal_error("Indirect operand for inline asm not a pointer!");
Bob Wilsone261b0c2009-12-22 18:34:19 +00004859 OpTy = PtrTy->getElementType();
4860 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004861
Chris Lattner81249c92008-10-17 17:05:25 +00004862 // If OpTy is not a single value, it may be a struct/union that we
4863 // can tile with integers.
4864 if (!OpTy->isSingleValueType() && OpTy->isSized()) {
4865 unsigned BitSize = TD->getTypeSizeInBits(OpTy);
4866 switch (BitSize) {
4867 default: break;
4868 case 1:
4869 case 8:
4870 case 16:
4871 case 32:
4872 case 64:
Chris Lattnercfc14c12008-10-17 19:59:51 +00004873 case 128:
Owen Anderson1d0be152009-08-13 21:58:54 +00004874 OpTy = IntegerType::get(Context, BitSize);
Chris Lattner81249c92008-10-17 17:05:25 +00004875 break;
4876 }
4877 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004878
Chris Lattner81249c92008-10-17 17:05:25 +00004879 return TLI.getValueType(OpTy, true);
4880 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004881
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004882private:
4883 /// MarkRegAndAliases - Mark the specified register and all aliases in the
4884 /// specified set.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004885 static void MarkRegAndAliases(unsigned Reg, std::set<unsigned> &Regs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004886 const TargetRegisterInfo &TRI) {
4887 assert(TargetRegisterInfo::isPhysicalRegister(Reg) && "Isn't a physreg");
4888 Regs.insert(Reg);
4889 if (const unsigned *Aliases = TRI.getAliasSet(Reg))
4890 for (; *Aliases; ++Aliases)
4891 Regs.insert(*Aliases);
4892 }
4893};
4894} // end llvm namespace.
4895
4896
4897/// GetRegistersForValue - Assign registers (virtual or physical) for the
4898/// specified operand. We prefer to assign virtual registers, to allow the
Bob Wilson266d9452009-12-17 05:07:36 +00004899/// register allocator to handle the assignment process. However, if the asm
4900/// uses features that we can't model on machineinstrs, we have SDISel do the
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004901/// allocation. This produces generally horrible, but correct, code.
4902///
4903/// OpInfo describes the operand.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004904/// Input and OutputRegs are the set of already allocated physical registers.
4905///
Dan Gohman2048b852009-11-23 18:04:58 +00004906void SelectionDAGBuilder::
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004907GetRegistersForValue(SDISelAsmOperandInfo &OpInfo,
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004908 std::set<unsigned> &OutputRegs,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004909 std::set<unsigned> &InputRegs) {
Dan Gohman0d24bfb2009-08-15 02:06:22 +00004910 LLVMContext &Context = FuncInfo.Fn->getContext();
Owen Anderson23b9b192009-08-12 00:36:31 +00004911
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004912 // Compute whether this value requires an input register, an output register,
4913 // or both.
4914 bool isOutReg = false;
4915 bool isInReg = false;
4916 switch (OpInfo.Type) {
4917 case InlineAsm::isOutput:
4918 isOutReg = true;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004919
4920 // If there is an input constraint that matches this, we need to reserve
Dale Johannesen8e3455b2008-09-24 23:13:09 +00004921 // the input register so no other inputs allocate to it.
Chris Lattner6bdcda32008-10-17 16:47:46 +00004922 isInReg = OpInfo.hasMatchingInput();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004923 break;
4924 case InlineAsm::isInput:
4925 isInReg = true;
4926 isOutReg = false;
4927 break;
4928 case InlineAsm::isClobber:
4929 isOutReg = true;
4930 isInReg = true;
4931 break;
4932 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004933
4934
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004935 MachineFunction &MF = DAG.getMachineFunction();
4936 SmallVector<unsigned, 4> Regs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004937
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004938 // If this is a constraint for a single physreg, or a constraint for a
4939 // register class, find it.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004940 std::pair<unsigned, const TargetRegisterClass*> PhysReg =
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004941 TLI.getRegForInlineAsmConstraint(OpInfo.ConstraintCode,
4942 OpInfo.ConstraintVT);
4943
4944 unsigned NumRegs = 1;
Owen Anderson825b72b2009-08-11 20:47:22 +00004945 if (OpInfo.ConstraintVT != MVT::Other) {
Chris Lattner01426e12008-10-21 00:45:36 +00004946 // If this is a FP input in an integer register (or visa versa) insert a bit
4947 // cast of the input value. More generally, handle any case where the input
4948 // value disagrees with the register class we plan to stick this in.
4949 if (OpInfo.Type == InlineAsm::isInput &&
4950 PhysReg.second && !PhysReg.second->hasType(OpInfo.ConstraintVT)) {
Owen Andersone50ed302009-08-10 22:56:29 +00004951 // Try to convert to the first EVT that the reg class contains. If the
Chris Lattner01426e12008-10-21 00:45:36 +00004952 // types are identical size, use a bitcast to convert (e.g. two differing
4953 // vector types).
Owen Andersone50ed302009-08-10 22:56:29 +00004954 EVT RegVT = *PhysReg.second->vt_begin();
Chris Lattner01426e12008-10-21 00:45:36 +00004955 if (RegVT.getSizeInBits() == OpInfo.ConstraintVT.getSizeInBits()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00004956 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004957 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004958 OpInfo.ConstraintVT = RegVT;
4959 } else if (RegVT.isInteger() && OpInfo.ConstraintVT.isFloatingPoint()) {
4960 // If the input is a FP value and we want it in FP registers, do a
4961 // bitcast to the corresponding integer type. This turns an f64 value
4962 // into i64, which can be passed with two i32 values on a 32-bit
4963 // machine.
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00004964 RegVT = EVT::getIntegerVT(Context,
Owen Anderson23b9b192009-08-12 00:36:31 +00004965 OpInfo.ConstraintVT.getSizeInBits());
Dale Johannesen66978ee2009-01-31 02:22:37 +00004966 OpInfo.CallOperand = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00004967 RegVT, OpInfo.CallOperand);
Chris Lattner01426e12008-10-21 00:45:36 +00004968 OpInfo.ConstraintVT = RegVT;
4969 }
4970 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004971
Owen Anderson23b9b192009-08-12 00:36:31 +00004972 NumRegs = TLI.getNumRegisters(Context, OpInfo.ConstraintVT);
Chris Lattner01426e12008-10-21 00:45:36 +00004973 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004974
Owen Andersone50ed302009-08-10 22:56:29 +00004975 EVT RegVT;
4976 EVT ValueVT = OpInfo.ConstraintVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004977
4978 // If this is a constraint for a specific physical register, like {r17},
4979 // assign it now.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004980 if (unsigned AssignedReg = PhysReg.first) {
4981 const TargetRegisterClass *RC = PhysReg.second;
Owen Anderson825b72b2009-08-11 20:47:22 +00004982 if (OpInfo.ConstraintVT == MVT::Other)
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004983 ValueVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004984
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004985 // Get the actual register value type. This is important, because the user
4986 // may have asked for (e.g.) the AX register in i32 type. We need to
4987 // remember that AX is actually i16 to get the right extension.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004988 RegVT = *RC->vt_begin();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004989
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004990 // This is a explicit reference to a physical register.
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004991 Regs.push_back(AssignedReg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004992
4993 // If this is an expanded reference, add the rest of the regs to Regs.
4994 if (NumRegs != 1) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00004995 TargetRegisterClass::iterator I = RC->begin();
4996 for (; *I != AssignedReg; ++I)
4997 assert(I != RC->end() && "Didn't find reg!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00004998
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00004999 // Already added the first reg.
5000 --NumRegs; ++I;
5001 for (; NumRegs; --NumRegs, ++I) {
Chris Lattnere2f7bf82009-03-24 15:27:37 +00005002 assert(I != RC->end() && "Ran out of registers to allocate!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005003 Regs.push_back(*I);
5004 }
5005 }
Bill Wendling651ad132009-12-22 01:25:10 +00005006
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005007 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5008 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5009 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5010 return;
5011 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005012
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005013 // Otherwise, if this was a reference to an LLVM register class, create vregs
5014 // for this reference.
Chris Lattnerb3b44842009-03-24 15:25:07 +00005015 if (const TargetRegisterClass *RC = PhysReg.second) {
5016 RegVT = *RC->vt_begin();
Owen Anderson825b72b2009-08-11 20:47:22 +00005017 if (OpInfo.ConstraintVT == MVT::Other)
Evan Chengfb112882009-03-23 08:01:15 +00005018 ValueVT = RegVT;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005019
Evan Chengfb112882009-03-23 08:01:15 +00005020 // Create the appropriate number of virtual registers.
5021 MachineRegisterInfo &RegInfo = MF.getRegInfo();
5022 for (; NumRegs; --NumRegs)
Chris Lattnerb3b44842009-03-24 15:25:07 +00005023 Regs.push_back(RegInfo.createVirtualRegister(RC));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005024
Evan Chengfb112882009-03-23 08:01:15 +00005025 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, RegVT, ValueVT);
5026 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005027 }
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005028
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005029 // This is a reference to a register class that doesn't directly correspond
5030 // to an LLVM register class. Allocate NumRegs consecutive, available,
5031 // registers from the class.
5032 std::vector<unsigned> RegClassRegs
5033 = TLI.getRegClassForInlineAsmConstraint(OpInfo.ConstraintCode,
5034 OpInfo.ConstraintVT);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005035
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005036 const TargetRegisterInfo *TRI = DAG.getTarget().getRegisterInfo();
5037 unsigned NumAllocated = 0;
5038 for (unsigned i = 0, e = RegClassRegs.size(); i != e; ++i) {
5039 unsigned Reg = RegClassRegs[i];
5040 // See if this register is available.
5041 if ((isOutReg && OutputRegs.count(Reg)) || // Already used.
5042 (isInReg && InputRegs.count(Reg))) { // Already used.
5043 // Make sure we find consecutive registers.
5044 NumAllocated = 0;
5045 continue;
5046 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005047
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005048 // Check to see if this register is allocatable (i.e. don't give out the
5049 // stack pointer).
Chris Lattnerfc9d1612009-03-24 15:22:11 +00005050 const TargetRegisterClass *RC = isAllocatableRegister(Reg, MF, TLI, TRI);
5051 if (!RC) { // Couldn't allocate this register.
5052 // Reset NumAllocated to make sure we return consecutive registers.
5053 NumAllocated = 0;
5054 continue;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005055 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005056
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005057 // Okay, this register is good, we can use it.
5058 ++NumAllocated;
5059
5060 // If we allocated enough consecutive registers, succeed.
5061 if (NumAllocated == NumRegs) {
5062 unsigned RegStart = (i-NumAllocated)+1;
5063 unsigned RegEnd = i+1;
5064 // Mark all of the allocated registers used.
5065 for (unsigned i = RegStart; i != RegEnd; ++i)
5066 Regs.push_back(RegClassRegs[i]);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005067
5068 OpInfo.AssignedRegs = RegsForValue(TLI, Regs, *RC->vt_begin(),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005069 OpInfo.ConstraintVT);
5070 OpInfo.MarkAllocatedRegs(isOutReg, isInReg, OutputRegs, InputRegs, *TRI);
5071 return;
5072 }
5073 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005074
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005075 // Otherwise, we couldn't allocate enough registers for this.
5076}
5077
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005078/// visitInlineAsm - Handle a call to an InlineAsm object.
5079///
Dan Gohman46510a72010-04-15 01:51:59 +00005080void SelectionDAGBuilder::visitInlineAsm(ImmutableCallSite CS) {
5081 const InlineAsm *IA = cast<InlineAsm>(CS.getCalledValue());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005082
5083 /// ConstraintOperands - Information about all of the constraints.
5084 std::vector<SDISelAsmOperandInfo> ConstraintOperands;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005085
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005086 std::set<unsigned> OutputRegs, InputRegs;
5087
5088 // Do a prepass over the constraints, canonicalizing them, and building up the
5089 // ConstraintOperands list.
5090 std::vector<InlineAsm::ConstraintInfo>
5091 ConstraintInfos = IA->ParseConstraints();
5092
Evan Chengda43bcf2008-09-24 00:05:32 +00005093 bool hasMemory = hasInlineAsmMemConstraint(ConstraintInfos, TLI);
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005094
Chris Lattner6c147292009-04-30 00:48:50 +00005095 SDValue Chain, Flag;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005096
Chris Lattner6c147292009-04-30 00:48:50 +00005097 // We won't need to flush pending loads if this asm doesn't touch
5098 // memory and is nonvolatile.
5099 if (hasMemory || IA->hasSideEffects())
Dale Johannesen97d14fc2009-04-18 00:09:40 +00005100 Chain = getRoot();
Chris Lattner6c147292009-04-30 00:48:50 +00005101 else
5102 Chain = DAG.getRoot();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005103
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005104 unsigned ArgNo = 0; // ArgNo - The argument of the CallInst.
5105 unsigned ResNo = 0; // ResNo - The result number of the next output.
5106 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5107 ConstraintOperands.push_back(SDISelAsmOperandInfo(ConstraintInfos[i]));
5108 SDISelAsmOperandInfo &OpInfo = ConstraintOperands.back();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005109
Owen Anderson825b72b2009-08-11 20:47:22 +00005110 EVT OpVT = MVT::Other;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005111
5112 // Compute the value type for each operand.
5113 switch (OpInfo.Type) {
5114 case InlineAsm::isOutput:
5115 // Indirect outputs just consume an argument.
5116 if (OpInfo.isIndirect) {
Dan Gohman46510a72010-04-15 01:51:59 +00005117 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005118 break;
5119 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005120
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005121 // The return value of the call is this value. As such, there is no
5122 // corresponding argument.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005123 assert(!CS.getType()->isVoidTy() &&
Owen Anderson1d0be152009-08-13 21:58:54 +00005124 "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005125 if (const StructType *STy = dyn_cast<StructType>(CS.getType())) {
5126 OpVT = TLI.getValueType(STy->getElementType(ResNo));
5127 } else {
5128 assert(ResNo == 0 && "Asm only has one result!");
5129 OpVT = TLI.getValueType(CS.getType());
5130 }
5131 ++ResNo;
5132 break;
5133 case InlineAsm::isInput:
Dan Gohman46510a72010-04-15 01:51:59 +00005134 OpInfo.CallOperandVal = const_cast<Value *>(CS.getArgument(ArgNo++));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005135 break;
5136 case InlineAsm::isClobber:
5137 // Nothing to do.
5138 break;
5139 }
5140
5141 // If this is an input or an indirect output, process the call argument.
5142 // BasicBlocks are labels, currently appearing only in asm's.
5143 if (OpInfo.CallOperandVal) {
Dale Johannesen5339c552009-07-20 23:27:39 +00005144 // Strip bitcasts, if any. This mostly comes up for functions.
Dale Johannesen76711242009-08-06 22:45:51 +00005145 OpInfo.CallOperandVal = OpInfo.CallOperandVal->stripPointerCasts();
5146
Dan Gohman46510a72010-04-15 01:51:59 +00005147 if (const BasicBlock *BB = dyn_cast<BasicBlock>(OpInfo.CallOperandVal)) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005148 OpInfo.CallOperand = DAG.getBasicBlock(FuncInfo.MBBMap[BB]);
Chris Lattner81249c92008-10-17 17:05:25 +00005149 } else {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005150 OpInfo.CallOperand = getValue(OpInfo.CallOperandVal);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005151 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005152
Owen Anderson1d0be152009-08-13 21:58:54 +00005153 OpVT = OpInfo.getCallOperandValEVT(*DAG.getContext(), TLI, TD);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005154 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005155
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005156 OpInfo.ConstraintVT = OpVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005157 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005158
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005159 // Second pass over the constraints: compute which constraint option to use
5160 // and assign registers to constraints that want a specific physreg.
5161 for (unsigned i = 0, e = ConstraintInfos.size(); i != e; ++i) {
5162 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005163
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005164 // If this is an output operand with a matching input operand, look up the
Evan Cheng09dc9c02008-12-16 18:21:39 +00005165 // matching input. If their types mismatch, e.g. one is an integer, the
5166 // other is floating point, or their sizes are different, flag it as an
5167 // error.
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005168 if (OpInfo.hasMatchingInput()) {
5169 SDISelAsmOperandInfo &Input = ConstraintOperands[OpInfo.MatchingInput];
Chris Lattner87d677c2010-04-07 23:50:38 +00005170
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005171 if (OpInfo.ConstraintVT != Input.ConstraintVT) {
Evan Cheng09dc9c02008-12-16 18:21:39 +00005172 if ((OpInfo.ConstraintVT.isInteger() !=
5173 Input.ConstraintVT.isInteger()) ||
5174 (OpInfo.ConstraintVT.getSizeInBits() !=
5175 Input.ConstraintVT.getSizeInBits())) {
Chris Lattner75361b62010-04-07 22:58:41 +00005176 report_fatal_error("Unsupported asm: input constraint"
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005177 " with a matching output constraint of"
5178 " incompatible type!");
Evan Cheng09dc9c02008-12-16 18:21:39 +00005179 }
5180 Input.ConstraintVT = OpInfo.ConstraintVT;
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005181 }
5182 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005183
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005184 // Compute the constraint code and ConstraintType to use.
Evan Chengda43bcf2008-09-24 00:05:32 +00005185 TLI.ComputeConstraintToUse(OpInfo, OpInfo.CallOperand, hasMemory, &DAG);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005186
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005187 // If this is a memory input, and if the operand is not indirect, do what we
5188 // need to to provide an address for the memory input.
5189 if (OpInfo.ConstraintType == TargetLowering::C_Memory &&
5190 !OpInfo.isIndirect) {
5191 assert(OpInfo.Type == InlineAsm::isInput &&
5192 "Can only indirectify direct input operands!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005193
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005194 // Memory operands really want the address of the value. If we don't have
5195 // an indirect input, put it in the constpool if we can, otherwise spill
5196 // it to a stack slot.
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005197
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005198 // If the operand is a float, integer, or vector constant, spill to a
5199 // constant pool entry to get its address.
Dan Gohman46510a72010-04-15 01:51:59 +00005200 const Value *OpVal = OpInfo.CallOperandVal;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005201 if (isa<ConstantFP>(OpVal) || isa<ConstantInt>(OpVal) ||
5202 isa<ConstantVector>(OpVal)) {
5203 OpInfo.CallOperand = DAG.getConstantPool(cast<Constant>(OpVal),
5204 TLI.getPointerTy());
5205 } else {
5206 // Otherwise, create a stack slot and emit a store to it before the
5207 // asm.
5208 const Type *Ty = OpVal->getType();
Duncan Sands777d2302009-05-09 07:06:46 +00005209 uint64_t TySize = TLI.getTargetData()->getTypeAllocSize(Ty);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005210 unsigned Align = TLI.getTargetData()->getPrefTypeAlignment(Ty);
5211 MachineFunction &MF = DAG.getMachineFunction();
David Greene3f2bf852009-11-12 20:49:22 +00005212 int SSFI = MF.getFrameInfo()->CreateStackObject(TySize, Align, false);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005213 SDValue StackSlot = DAG.getFrameIndex(SSFI, TLI.getPointerTy());
Dale Johannesen66978ee2009-01-31 02:22:37 +00005214 Chain = DAG.getStore(Chain, getCurDebugLoc(),
David Greene1e559442010-02-15 17:00:31 +00005215 OpInfo.CallOperand, StackSlot, NULL, 0,
5216 false, false, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005217 OpInfo.CallOperand = StackSlot;
5218 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005219
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005220 // There is no longer a Value* corresponding to this operand.
5221 OpInfo.CallOperandVal = 0;
Bill Wendling651ad132009-12-22 01:25:10 +00005222
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005223 // It is now an indirect operand.
5224 OpInfo.isIndirect = true;
5225 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005226
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005227 // If this constraint is for a specific register, allocate it before
5228 // anything else.
5229 if (OpInfo.ConstraintType == TargetLowering::C_Register)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005230 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005231 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005232
Bill Wendling651ad132009-12-22 01:25:10 +00005233 ConstraintInfos.clear();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005234
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005235 // Second pass - Loop over all of the operands, assigning virtual or physregs
Chris Lattner58f15c42008-10-17 16:21:11 +00005236 // to register class operands.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005237 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5238 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005239
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005240 // C_Register operands have already been allocated, Other/Memory don't need
5241 // to be.
5242 if (OpInfo.ConstraintType == TargetLowering::C_RegisterClass)
Dale Johannesen8e3455b2008-09-24 23:13:09 +00005243 GetRegistersForValue(OpInfo, OutputRegs, InputRegs);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005244 }
5245
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005246 // AsmNodeOperands - The operands for the ISD::INLINEASM node.
5247 std::vector<SDValue> AsmNodeOperands;
5248 AsmNodeOperands.push_back(SDValue()); // reserve space for input chain
5249 AsmNodeOperands.push_back(
Dan Gohmanf2d7fb32010-01-04 21:00:54 +00005250 DAG.getTargetExternalSymbol(IA->getAsmString().c_str(),
5251 TLI.getPointerTy()));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005252
Chris Lattnerdecc2672010-04-07 05:20:54 +00005253 // If we have a !srcloc metadata node associated with it, we want to attach
5254 // this to the ultimately generated inline asm machineinstr. To do this, we
5255 // pass in the third operand as this (potentially null) inline asm MDNode.
5256 const MDNode *SrcLoc = CS.getInstruction()->getMetadata("srcloc");
5257 AsmNodeOperands.push_back(DAG.getMDNode(SrcLoc));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005258
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005259 // Loop over all of the inputs, copying the operand values into the
5260 // appropriate registers and processing the output regs.
5261 RegsForValue RetValRegs;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005262
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005263 // IndirectStoresToEmit - The set of stores to emit after the inline asm node.
5264 std::vector<std::pair<RegsForValue, Value*> > IndirectStoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005265
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005266 for (unsigned i = 0, e = ConstraintOperands.size(); i != e; ++i) {
5267 SDISelAsmOperandInfo &OpInfo = ConstraintOperands[i];
5268
5269 switch (OpInfo.Type) {
5270 case InlineAsm::isOutput: {
5271 if (OpInfo.ConstraintType != TargetLowering::C_RegisterClass &&
5272 OpInfo.ConstraintType != TargetLowering::C_Register) {
5273 // Memory output, or 'other' output (e.g. 'X' constraint).
5274 assert(OpInfo.isIndirect && "Memory output must be indirect operand");
5275
5276 // Add information to the INLINEASM node to know about this output.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005277 unsigned OpFlags = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
5278 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlags,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005279 TLI.getPointerTy()));
5280 AsmNodeOperands.push_back(OpInfo.CallOperand);
5281 break;
5282 }
5283
5284 // Otherwise, this is a register or register class output.
5285
5286 // Copy the output from the appropriate register. Find a register that
5287 // we can use.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005288 if (OpInfo.AssignedRegs.Regs.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005289 report_fatal_error("Couldn't allocate output reg for constraint '" +
5290 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005291
5292 // If this is an indirect operand, store through the pointer after the
5293 // asm.
5294 if (OpInfo.isIndirect) {
5295 IndirectStoresToEmit.push_back(std::make_pair(OpInfo.AssignedRegs,
5296 OpInfo.CallOperandVal));
5297 } else {
5298 // This is the result value of the call.
Benjamin Kramerf0127052010-01-05 13:12:22 +00005299 assert(!CS.getType()->isVoidTy() && "Bad inline asm!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005300 // Concatenate this output onto the outputs list.
5301 RetValRegs.append(OpInfo.AssignedRegs);
5302 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005303
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005304 // Add information to the INLINEASM node to know that this register is
5305 // set.
Dale Johannesen913d3df2008-09-12 17:49:03 +00005306 OpInfo.AssignedRegs.AddInlineAsmOperands(OpInfo.isEarlyClobber ?
Chris Lattnerdecc2672010-04-07 05:20:54 +00005307 InlineAsm::Kind_RegDefEarlyClobber :
5308 InlineAsm::Kind_RegDef,
Evan Chengfb112882009-03-23 08:01:15 +00005309 false,
5310 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005311 DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005312 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005313 break;
5314 }
5315 case InlineAsm::isInput: {
5316 SDValue InOperandVal = OpInfo.CallOperand;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005317
Chris Lattner6bdcda32008-10-17 16:47:46 +00005318 if (OpInfo.isMatchingInputConstraint()) { // Matching constraint?
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005319 // If this is required to match an output register we have already set,
5320 // just use its register.
Chris Lattner58f15c42008-10-17 16:21:11 +00005321 unsigned OperandNo = OpInfo.getMatchedOperand();
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005322
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005323 // Scan until we find the definition we already emitted of this operand.
5324 // When we find it, create a RegsForValue operand.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005325 unsigned CurOp = InlineAsm::Op_FirstOperand;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005326 for (; OperandNo; --OperandNo) {
5327 // Advance to the next operand.
Evan Cheng697cbbf2009-03-20 18:03:34 +00005328 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005329 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005330 assert((InlineAsm::isRegDefKind(OpFlag) ||
5331 InlineAsm::isRegDefEarlyClobberKind(OpFlag) ||
5332 InlineAsm::isMemKind(OpFlag)) && "Skipped past definitions?");
Evan Cheng697cbbf2009-03-20 18:03:34 +00005333 CurOp += InlineAsm::getNumOperandRegisters(OpFlag)+1;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005334 }
5335
Evan Cheng697cbbf2009-03-20 18:03:34 +00005336 unsigned OpFlag =
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005337 cast<ConstantSDNode>(AsmNodeOperands[CurOp])->getZExtValue();
Chris Lattnerdecc2672010-04-07 05:20:54 +00005338 if (InlineAsm::isRegDefKind(OpFlag) ||
5339 InlineAsm::isRegDefEarlyClobberKind(OpFlag)) {
Evan Cheng697cbbf2009-03-20 18:03:34 +00005340 // Add (OpFlag&0xffff)>>3 registers to MatchedRegs.
Chris Lattner6129c372010-04-08 00:09:16 +00005341 if (OpInfo.isIndirect) {
5342 // This happens on gcc/testsuite/gcc.dg/pr8788-1.c
5343 LLVMContext &Ctx = CurMBB->getParent()->getFunction()->getContext();
5344 Ctx.emitError(CS.getInstruction(), "inline asm not supported yet:"
5345 " don't know how to handle tied "
5346 "indirect register inputs");
5347 }
5348
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005349 RegsForValue MatchedRegs;
5350 MatchedRegs.TLI = &TLI;
5351 MatchedRegs.ValueVTs.push_back(InOperandVal.getValueType());
Owen Andersone50ed302009-08-10 22:56:29 +00005352 EVT RegVT = AsmNodeOperands[CurOp+1].getValueType();
Evan Chengfb112882009-03-23 08:01:15 +00005353 MatchedRegs.RegVTs.push_back(RegVT);
5354 MachineRegisterInfo &RegInfo = DAG.getMachineFunction().getRegInfo();
Evan Cheng697cbbf2009-03-20 18:03:34 +00005355 for (unsigned i = 0, e = InlineAsm::getNumOperandRegisters(OpFlag);
Evan Chengfb112882009-03-23 08:01:15 +00005356 i != e; ++i)
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005357 MatchedRegs.Regs.push_back
5358 (RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT)));
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005359
5360 // Use the produced MatchedRegs object to
Dale Johannesen66978ee2009-01-31 02:22:37 +00005361 MatchedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005362 Chain, &Flag);
Chris Lattnerdecc2672010-04-07 05:20:54 +00005363 MatchedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse,
Evan Chengfb112882009-03-23 08:01:15 +00005364 true, OpInfo.getMatchedOperand(),
Bill Wendling46ada192010-03-02 01:55:18 +00005365 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005366 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005367 }
Chris Lattnerdecc2672010-04-07 05:20:54 +00005368
5369 assert(InlineAsm::isMemKind(OpFlag) && "Unknown matching constraint!");
5370 assert(InlineAsm::getNumOperandRegisters(OpFlag) == 1 &&
5371 "Unexpected number of operands");
5372 // Add information to the INLINEASM node to know about this input.
5373 // See InlineAsm.h isUseOperandTiedToDef.
5374 OpFlag = InlineAsm::getFlagWordForMatchingOp(OpFlag,
5375 OpInfo.getMatchedOperand());
5376 AsmNodeOperands.push_back(DAG.getTargetConstant(OpFlag,
5377 TLI.getPointerTy()));
5378 AsmNodeOperands.push_back(AsmNodeOperands[CurOp+1]);
5379 break;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005380 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005381
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005382 if (OpInfo.ConstraintType == TargetLowering::C_Other) {
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005383 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005384 "Don't know how to handle indirect other inputs yet!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005385
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005386 std::vector<SDValue> Ops;
5387 TLI.LowerAsmOperandForConstraint(InOperandVal, OpInfo.ConstraintCode[0],
Evan Chengda43bcf2008-09-24 00:05:32 +00005388 hasMemory, Ops, DAG);
Chris Lattner87d677c2010-04-07 23:50:38 +00005389 if (Ops.empty())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005390 report_fatal_error("Invalid operand for inline asm constraint '" +
5391 Twine(OpInfo.ConstraintCode) + "'!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005392
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005393 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005394 unsigned ResOpType =
5395 InlineAsm::getFlagWord(InlineAsm::Kind_Imm, Ops.size());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005396 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005397 TLI.getPointerTy()));
5398 AsmNodeOperands.insert(AsmNodeOperands.end(), Ops.begin(), Ops.end());
5399 break;
Chris Lattnerdecc2672010-04-07 05:20:54 +00005400 }
5401
5402 if (OpInfo.ConstraintType == TargetLowering::C_Memory) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005403 assert(OpInfo.isIndirect && "Operand must be indirect to be a mem!");
5404 assert(InOperandVal.getValueType() == TLI.getPointerTy() &&
5405 "Memory operands expect pointer values");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005406
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005407 // Add information to the INLINEASM node to know about this input.
Chris Lattnerdecc2672010-04-07 05:20:54 +00005408 unsigned ResOpType = InlineAsm::getFlagWord(InlineAsm::Kind_Mem, 1);
Dale Johannesen86b49f82008-09-24 01:07:17 +00005409 AsmNodeOperands.push_back(DAG.getTargetConstant(ResOpType,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005410 TLI.getPointerTy()));
5411 AsmNodeOperands.push_back(InOperandVal);
5412 break;
5413 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005414
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005415 assert((OpInfo.ConstraintType == TargetLowering::C_RegisterClass ||
5416 OpInfo.ConstraintType == TargetLowering::C_Register) &&
5417 "Unknown constraint type!");
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005418 assert(!OpInfo.isIndirect &&
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005419 "Don't know how to handle indirect register inputs yet!");
5420
5421 // Copy the input into the appropriate registers.
Evan Cheng8112b532010-02-10 01:21:02 +00005422 if (OpInfo.AssignedRegs.Regs.empty() ||
Chris Lattner87d677c2010-04-07 23:50:38 +00005423 !OpInfo.AssignedRegs.areValueTypesLegal())
Benjamin Kramer1bd73352010-04-08 10:44:28 +00005424 report_fatal_error("Couldn't allocate input reg for constraint '" +
5425 Twine(OpInfo.ConstraintCode) + "'!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005426
Dale Johannesen66978ee2009-01-31 02:22:37 +00005427 OpInfo.AssignedRegs.getCopyToRegs(InOperandVal, DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005428 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005429
Chris Lattnerdecc2672010-04-07 05:20:54 +00005430 OpInfo.AssignedRegs.AddInlineAsmOperands(InlineAsm::Kind_RegUse, false, 0,
Bill Wendling46ada192010-03-02 01:55:18 +00005431 DAG, AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005432 break;
5433 }
5434 case InlineAsm::isClobber: {
5435 // Add the clobbered value to the operand list, so that the register
5436 // allocator is aware that the physreg got clobbered.
5437 if (!OpInfo.AssignedRegs.Regs.empty())
Chris Lattnerdecc2672010-04-07 05:20:54 +00005438 OpInfo.AssignedRegs.AddInlineAsmOperands(
5439 InlineAsm::Kind_RegDefEarlyClobber,
Bill Wendling46ada192010-03-02 01:55:18 +00005440 false, 0, DAG,
Bill Wendling651ad132009-12-22 01:25:10 +00005441 AsmNodeOperands);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005442 break;
5443 }
5444 }
5445 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005446
Chris Lattnerdecc2672010-04-07 05:20:54 +00005447 // Finish up input operands. Set the input chain and add the flag last.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005448 AsmNodeOperands[0] = Chain;
5449 if (Flag.getNode()) AsmNodeOperands.push_back(Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005450
Dale Johannesen66978ee2009-01-31 02:22:37 +00005451 Chain = DAG.getNode(ISD::INLINEASM, getCurDebugLoc(),
Owen Anderson825b72b2009-08-11 20:47:22 +00005452 DAG.getVTList(MVT::Other, MVT::Flag),
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005453 &AsmNodeOperands[0], AsmNodeOperands.size());
5454 Flag = Chain.getValue(1);
5455
5456 // If this asm returns a register value, copy the result from that register
5457 // and set it as the value of the call.
5458 if (!RetValRegs.Regs.empty()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00005459 SDValue Val = RetValRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005460 Chain, &Flag);
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005461
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005462 // FIXME: Why don't we do this for inline asms with MRVs?
5463 if (CS.getType()->isSingleValueType() && CS.getType()->isSized()) {
Owen Andersone50ed302009-08-10 22:56:29 +00005464 EVT ResultType = TLI.getValueType(CS.getType());
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005465
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005466 // If any of the results of the inline asm is a vector, it may have the
5467 // wrong width/num elts. This can happen for register classes that can
5468 // contain multiple different value types. The preg or vreg allocated may
5469 // not have the same VT as was expected. Convert it to the right type
5470 // with bit_convert.
5471 if (ResultType != Val.getValueType() && Val.getValueType().isVector()) {
Dale Johannesen66978ee2009-01-31 02:22:37 +00005472 Val = DAG.getNode(ISD::BIT_CONVERT, getCurDebugLoc(),
Dale Johannesenfa42dea2009-01-30 01:34:22 +00005473 ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005474
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005475 } else if (ResultType != Val.getValueType() &&
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005476 ResultType.isInteger() && Val.getValueType().isInteger()) {
5477 // If a result value was tied to an input value, the computed result may
5478 // have a wider width than the expected result. Extract the relevant
5479 // portion.
Dale Johannesen66978ee2009-01-31 02:22:37 +00005480 Val = DAG.getNode(ISD::TRUNCATE, getCurDebugLoc(), ResultType, Val);
Dan Gohman95915732008-10-18 01:03:45 +00005481 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005482
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005483 assert(ResultType == Val.getValueType() && "Asm result value mismatch!");
Chris Lattner0c526442008-10-17 17:52:49 +00005484 }
Dan Gohman95915732008-10-18 01:03:45 +00005485
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005486 setValue(CS.getInstruction(), Val);
Dale Johannesenec65a7d2009-04-14 00:56:56 +00005487 // Don't need to use this as a chain in this case.
5488 if (!IA->hasSideEffects() && !hasMemory && IndirectStoresToEmit.empty())
5489 return;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005490 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005491
Dan Gohman46510a72010-04-15 01:51:59 +00005492 std::vector<std::pair<SDValue, const Value *> > StoresToEmit;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005493
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005494 // Process indirect outputs, first output all of the flagged copies out of
5495 // physregs.
5496 for (unsigned i = 0, e = IndirectStoresToEmit.size(); i != e; ++i) {
5497 RegsForValue &OutRegs = IndirectStoresToEmit[i].first;
Dan Gohman46510a72010-04-15 01:51:59 +00005498 const Value *Ptr = IndirectStoresToEmit[i].second;
Dale Johannesen66978ee2009-01-31 02:22:37 +00005499 SDValue OutVal = OutRegs.getCopyFromRegs(DAG, getCurDebugLoc(),
Bill Wendling46ada192010-03-02 01:55:18 +00005500 Chain, &Flag);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005501 StoresToEmit.push_back(std::make_pair(OutVal, Ptr));
5502 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005503
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005504 // Emit the non-flagged stores from the physregs.
5505 SmallVector<SDValue, 8> OutChains;
Bill Wendling651ad132009-12-22 01:25:10 +00005506 for (unsigned i = 0, e = StoresToEmit.size(); i != e; ++i) {
5507 SDValue Val = DAG.getStore(Chain, getCurDebugLoc(),
5508 StoresToEmit[i].first,
5509 getValue(StoresToEmit[i].second),
David Greene1e559442010-02-15 17:00:31 +00005510 StoresToEmit[i].second, 0,
5511 false, false, 0);
Bill Wendling651ad132009-12-22 01:25:10 +00005512 OutChains.push_back(Val);
Bill Wendling651ad132009-12-22 01:25:10 +00005513 }
5514
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005515 if (!OutChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +00005516 Chain = DAG.getNode(ISD::TokenFactor, getCurDebugLoc(), MVT::Other,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005517 &OutChains[0], OutChains.size());
Bill Wendling651ad132009-12-22 01:25:10 +00005518
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005519 DAG.setRoot(Chain);
5520}
5521
Dan Gohman46510a72010-04-15 01:51:59 +00005522void SelectionDAGBuilder::visitVAStart(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005523 DAG.setRoot(DAG.getNode(ISD::VASTART, getCurDebugLoc(),
5524 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005525 getValue(I.getOperand(1)),
5526 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005527}
5528
Dan Gohman46510a72010-04-15 01:51:59 +00005529void SelectionDAGBuilder::visitVAArg(const VAArgInst &I) {
Dale Johannesena04b7572009-02-03 23:04:43 +00005530 SDValue V = DAG.getVAArg(TLI.getValueType(I.getType()), getCurDebugLoc(),
5531 getRoot(), getValue(I.getOperand(0)),
5532 DAG.getSrcValue(I.getOperand(0)));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005533 setValue(&I, V);
5534 DAG.setRoot(V.getValue(1));
5535}
5536
Dan Gohman46510a72010-04-15 01:51:59 +00005537void SelectionDAGBuilder::visitVAEnd(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005538 DAG.setRoot(DAG.getNode(ISD::VAEND, getCurDebugLoc(),
5539 MVT::Other, getRoot(),
Eric Christopher551754c2010-04-16 23:37:20 +00005540 getValue(I.getOperand(1)),
5541 DAG.getSrcValue(I.getOperand(1))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005542}
5543
Dan Gohman46510a72010-04-15 01:51:59 +00005544void SelectionDAGBuilder::visitVACopy(const CallInst &I) {
Bill Wendlingc1d3c942009-12-23 00:44:51 +00005545 DAG.setRoot(DAG.getNode(ISD::VACOPY, getCurDebugLoc(),
5546 MVT::Other, getRoot(),
5547 getValue(I.getOperand(1)),
Eric Christopher551754c2010-04-16 23:37:20 +00005548 getValue(I.getOperand(2)),
5549 DAG.getSrcValue(I.getOperand(1)),
5550 DAG.getSrcValue(I.getOperand(2))));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005551}
5552
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005553/// TargetLowering::LowerCallTo - This is the default LowerCallTo
Dan Gohman98ca4f22009-08-05 01:29:28 +00005554/// implementation, which just calls LowerCall.
5555/// FIXME: When all targets are
5556/// migrated to using LowerCall, this hook should be integrated into SDISel.
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005557std::pair<SDValue, SDValue>
5558TargetLowering::LowerCallTo(SDValue Chain, const Type *RetTy,
5559 bool RetSExt, bool RetZExt, bool isVarArg,
Tilmann Scheller6b61cd12009-07-03 06:44:53 +00005560 bool isInreg, unsigned NumFixedArgs,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +00005561 CallingConv::ID CallConv, bool isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005562 bool isReturnValueUsed,
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005563 SDValue Callee,
Dan Gohmand858e902010-04-17 15:26:15 +00005564 ArgListTy &Args, SelectionDAG &DAG,
5565 DebugLoc dl) const {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005566 // Handle all of the outgoing arguments.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005567 SmallVector<ISD::OutputArg, 32> Outs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005568 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
Owen Andersone50ed302009-08-10 22:56:29 +00005569 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005570 ComputeValueVTs(*this, Args[i].Ty, ValueVTs);
5571 for (unsigned Value = 0, NumValues = ValueVTs.size();
5572 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005573 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005574 const Type *ArgTy = VT.getTypeForEVT(RetTy->getContext());
Chris Lattner2a0b96c2008-10-18 18:49:30 +00005575 SDValue Op = SDValue(Args[i].Node.getNode(),
5576 Args[i].Node.getResNo() + Value);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005577 ISD::ArgFlagsTy Flags;
5578 unsigned OriginalAlignment =
5579 getTargetData()->getABITypeAlignment(ArgTy);
5580
5581 if (Args[i].isZExt)
5582 Flags.setZExt();
5583 if (Args[i].isSExt)
5584 Flags.setSExt();
5585 if (Args[i].isInReg)
5586 Flags.setInReg();
5587 if (Args[i].isSRet)
5588 Flags.setSRet();
5589 if (Args[i].isByVal) {
5590 Flags.setByVal();
5591 const PointerType *Ty = cast<PointerType>(Args[i].Ty);
5592 const Type *ElementTy = Ty->getElementType();
5593 unsigned FrameAlign = getByValTypeAlignment(ElementTy);
Duncan Sands777d2302009-05-09 07:06:46 +00005594 unsigned FrameSize = getTargetData()->getTypeAllocSize(ElementTy);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005595 // For ByVal, alignment should come from FE. BE will guess if this
5596 // info is not there but there are cases it cannot get right.
5597 if (Args[i].Alignment)
5598 FrameAlign = Args[i].Alignment;
5599 Flags.setByValAlign(FrameAlign);
5600 Flags.setByValSize(FrameSize);
5601 }
5602 if (Args[i].isNest)
5603 Flags.setNest();
5604 Flags.setOrigAlign(OriginalAlignment);
5605
Owen Anderson23b9b192009-08-12 00:36:31 +00005606 EVT PartVT = getRegisterType(RetTy->getContext(), VT);
5607 unsigned NumParts = getNumRegisters(RetTy->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005608 SmallVector<SDValue, 4> Parts(NumParts);
5609 ISD::NodeType ExtendKind = ISD::ANY_EXTEND;
5610
5611 if (Args[i].isSExt)
5612 ExtendKind = ISD::SIGN_EXTEND;
5613 else if (Args[i].isZExt)
5614 ExtendKind = ISD::ZERO_EXTEND;
5615
Bill Wendling46ada192010-03-02 01:55:18 +00005616 getCopyToParts(DAG, dl, Op, &Parts[0], NumParts,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005617 PartVT, ExtendKind);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005618
Dan Gohman98ca4f22009-08-05 01:29:28 +00005619 for (unsigned j = 0; j != NumParts; ++j) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005620 // if it isn't first piece, alignment must be 1
Dan Gohman98ca4f22009-08-05 01:29:28 +00005621 ISD::OutputArg MyFlags(Flags, Parts[j], i < NumFixedArgs);
5622 if (NumParts > 1 && j == 0)
5623 MyFlags.Flags.setSplit();
5624 else if (j != 0)
5625 MyFlags.Flags.setOrigAlign(1);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005626
Dan Gohman98ca4f22009-08-05 01:29:28 +00005627 Outs.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005628 }
5629 }
5630 }
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005631
Dan Gohman98ca4f22009-08-05 01:29:28 +00005632 // Handle the incoming return values from the call.
5633 SmallVector<ISD::InputArg, 32> Ins;
Owen Andersone50ed302009-08-10 22:56:29 +00005634 SmallVector<EVT, 4> RetTys;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005635 ComputeValueVTs(*this, RetTy, RetTys);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005636 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005637 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005638 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5639 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005640 for (unsigned i = 0; i != NumRegs; ++i) {
5641 ISD::InputArg MyFlags;
5642 MyFlags.VT = RegisterVT;
5643 MyFlags.Used = isReturnValueUsed;
5644 if (RetSExt)
5645 MyFlags.Flags.setSExt();
5646 if (RetZExt)
5647 MyFlags.Flags.setZExt();
5648 if (isInreg)
5649 MyFlags.Flags.setInReg();
5650 Ins.push_back(MyFlags);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005651 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005652 }
5653
Dan Gohman98ca4f22009-08-05 01:29:28 +00005654 SmallVector<SDValue, 4> InVals;
Evan Cheng022d9e12010-02-02 23:55:14 +00005655 Chain = LowerCall(Chain, Callee, CallConv, isVarArg, isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005656 Outs, Ins, dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005657
5658 // Verify that the target's LowerCall behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005659 assert(Chain.getNode() && Chain.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005660 "LowerCall didn't return a valid chain!");
5661 assert((!isTailCall || InVals.empty()) &&
5662 "LowerCall emitted a return value for a tail call!");
5663 assert((isTailCall || InVals.size() == Ins.size()) &&
5664 "LowerCall didn't emit the correct number of values!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00005665
5666 // For a tail call, the return value is merely live-out and there aren't
5667 // any nodes in the DAG representing it. Return a special value to
5668 // indicate that a tail call has been emitted and no more Instructions
5669 // should be processed in the current block.
5670 if (isTailCall) {
5671 DAG.setRoot(Chain);
5672 return std::make_pair(SDValue(), SDValue());
5673 }
5674
Evan Chengaf1871f2010-03-11 19:38:18 +00005675 DEBUG(for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5676 assert(InVals[i].getNode() &&
5677 "LowerCall emitted a null value!");
5678 assert(Ins[i].VT == InVals[i].getValueType() &&
5679 "LowerCall emitted a value with the wrong type!");
5680 });
5681
Dan Gohman98ca4f22009-08-05 01:29:28 +00005682 // Collect the legal value parts into potentially illegal values
5683 // that correspond to the original function's return values.
5684 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5685 if (RetSExt)
5686 AssertOp = ISD::AssertSext;
5687 else if (RetZExt)
5688 AssertOp = ISD::AssertZext;
5689 SmallVector<SDValue, 4> ReturnValues;
5690 unsigned CurReg = 0;
5691 for (unsigned I = 0, E = RetTys.size(); I != E; ++I) {
Owen Andersone50ed302009-08-10 22:56:29 +00005692 EVT VT = RetTys[I];
Owen Anderson23b9b192009-08-12 00:36:31 +00005693 EVT RegisterVT = getRegisterType(RetTy->getContext(), VT);
5694 unsigned NumRegs = getNumRegisters(RetTy->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005695
Bill Wendling46ada192010-03-02 01:55:18 +00005696 ReturnValues.push_back(getCopyFromParts(DAG, dl, &InVals[CurReg],
Bill Wendling4533cac2010-01-28 21:51:40 +00005697 NumRegs, RegisterVT, VT,
5698 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005699 CurReg += NumRegs;
5700 }
5701
5702 // For a function returning void, there is no return value. We can't create
5703 // such a node, so we just return a null return value in that case. In
5704 // that case, nothing will actualy look at the value.
5705 if (ReturnValues.empty())
5706 return std::make_pair(SDValue(), Chain);
5707
5708 SDValue Res = DAG.getNode(ISD::MERGE_VALUES, dl,
5709 DAG.getVTList(&RetTys[0], RetTys.size()),
5710 &ReturnValues[0], ReturnValues.size());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005711 return std::make_pair(Res, Chain);
5712}
5713
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005714void TargetLowering::LowerOperationWrapper(SDNode *N,
5715 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +00005716 SelectionDAG &DAG) const {
Duncan Sands9fbc7e22009-01-21 09:00:29 +00005717 SDValue Res = LowerOperation(SDValue(N, 0), DAG);
Sanjiv Guptabb326bb2009-01-21 04:48:39 +00005718 if (Res.getNode())
5719 Results.push_back(Res);
5720}
5721
Dan Gohmand858e902010-04-17 15:26:15 +00005722SDValue TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) const {
Torok Edwinc23197a2009-07-14 16:55:14 +00005723 llvm_unreachable("LowerOperation not implemented for this target!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005724 return SDValue();
5725}
5726
Dan Gohman46510a72010-04-15 01:51:59 +00005727void
5728SelectionDAGBuilder::CopyValueToVirtualRegister(const Value *V, unsigned Reg) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005729 SDValue Op = getValue(V);
5730 assert((Op.getOpcode() != ISD::CopyFromReg ||
5731 cast<RegisterSDNode>(Op.getOperand(1))->getReg() != Reg) &&
5732 "Copy from a reg to the same reg!");
5733 assert(!TargetRegisterInfo::isPhysicalRegister(Reg) && "Is a physreg");
5734
Owen Anderson23b9b192009-08-12 00:36:31 +00005735 RegsForValue RFV(V->getContext(), TLI, Reg, V->getType());
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005736 SDValue Chain = DAG.getEntryNode();
Bill Wendling46ada192010-03-02 01:55:18 +00005737 RFV.getCopyToRegs(Op, DAG, getCurDebugLoc(), Chain, 0);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005738 PendingExports.push_back(Chain);
5739}
5740
5741#include "llvm/CodeGen/SelectionDAGISel.h"
5742
Dan Gohman46510a72010-04-15 01:51:59 +00005743void SelectionDAGISel::LowerArguments(const BasicBlock *LLVMBB) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005744 // If this is the entry block, emit arguments.
Dan Gohman46510a72010-04-15 01:51:59 +00005745 const Function &F = *LLVMBB->getParent();
Dan Gohman2048b852009-11-23 18:04:58 +00005746 SelectionDAG &DAG = SDB->DAG;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005747 SDValue OldRoot = DAG.getRoot();
Dan Gohman2048b852009-11-23 18:04:58 +00005748 DebugLoc dl = SDB->getCurDebugLoc();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005749 const TargetData *TD = TLI.getTargetData();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005750 SmallVector<ISD::InputArg, 16> Ins;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005751
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005752 // Check whether the function can return without sret-demotion.
5753 SmallVector<EVT, 4> OutVTs;
5754 SmallVector<ISD::ArgFlagsTy, 4> OutsFlags;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005755 getReturnInfo(F.getReturnType(), F.getAttributes().getRetAttributes(),
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005756 OutVTs, OutsFlags, TLI);
5757 FunctionLoweringInfo &FLI = DAG.getFunctionLoweringInfo();
5758
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005759 FLI.CanLowerReturn = TLI.CanLowerReturn(F.getCallingConv(), F.isVarArg(),
Bill Wendling3ea3c242009-12-22 02:10:19 +00005760 OutVTs, OutsFlags, DAG);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005761 if (!FLI.CanLowerReturn) {
5762 // Put in an sret pointer parameter before all the other parameters.
5763 SmallVector<EVT, 1> ValueVTs;
5764 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5765
5766 // NOTE: Assuming that a pointer will never break down to more than one VT
5767 // or one register.
5768 ISD::ArgFlagsTy Flags;
5769 Flags.setSRet();
5770 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), ValueVTs[0]);
5771 ISD::InputArg RetArg(Flags, RegisterVT, true);
5772 Ins.push_back(RetArg);
5773 }
Kenneth Uildriksb4997ae2009-11-07 02:11:54 +00005774
Dan Gohman98ca4f22009-08-05 01:29:28 +00005775 // Set up the incoming argument description vector.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005776 unsigned Idx = 1;
Dan Gohman46510a72010-04-15 01:51:59 +00005777 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005778 I != E; ++I, ++Idx) {
Owen Andersone50ed302009-08-10 22:56:29 +00005779 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005780 ComputeValueVTs(TLI, I->getType(), ValueVTs);
5781 bool isArgValueUsed = !I->use_empty();
5782 for (unsigned Value = 0, NumValues = ValueVTs.size();
5783 Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005784 EVT VT = ValueVTs[Value];
Owen Anderson1d0be152009-08-13 21:58:54 +00005785 const Type *ArgTy = VT.getTypeForEVT(*DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00005786 ISD::ArgFlagsTy Flags;
5787 unsigned OriginalAlignment =
5788 TD->getABITypeAlignment(ArgTy);
5789
5790 if (F.paramHasAttr(Idx, Attribute::ZExt))
5791 Flags.setZExt();
5792 if (F.paramHasAttr(Idx, Attribute::SExt))
5793 Flags.setSExt();
5794 if (F.paramHasAttr(Idx, Attribute::InReg))
5795 Flags.setInReg();
5796 if (F.paramHasAttr(Idx, Attribute::StructRet))
5797 Flags.setSRet();
5798 if (F.paramHasAttr(Idx, Attribute::ByVal)) {
5799 Flags.setByVal();
5800 const PointerType *Ty = cast<PointerType>(I->getType());
5801 const Type *ElementTy = Ty->getElementType();
5802 unsigned FrameAlign = TLI.getByValTypeAlignment(ElementTy);
5803 unsigned FrameSize = TD->getTypeAllocSize(ElementTy);
5804 // For ByVal, alignment should be passed from FE. BE will guess if
5805 // this info is not there but there are cases it cannot get right.
5806 if (F.getParamAlignment(Idx))
5807 FrameAlign = F.getParamAlignment(Idx);
5808 Flags.setByValAlign(FrameAlign);
5809 Flags.setByValSize(FrameSize);
5810 }
5811 if (F.paramHasAttr(Idx, Attribute::Nest))
5812 Flags.setNest();
5813 Flags.setOrigAlign(OriginalAlignment);
5814
Owen Anderson23b9b192009-08-12 00:36:31 +00005815 EVT RegisterVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5816 unsigned NumRegs = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005817 for (unsigned i = 0; i != NumRegs; ++i) {
5818 ISD::InputArg MyFlags(Flags, RegisterVT, isArgValueUsed);
5819 if (NumRegs > 1 && i == 0)
5820 MyFlags.Flags.setSplit();
5821 // if it isn't first piece, alignment must be 1
5822 else if (i > 0)
5823 MyFlags.Flags.setOrigAlign(1);
5824 Ins.push_back(MyFlags);
5825 }
5826 }
5827 }
5828
5829 // Call the target to set up the argument values.
5830 SmallVector<SDValue, 8> InVals;
5831 SDValue NewRoot = TLI.LowerFormalArguments(DAG.getRoot(), F.getCallingConv(),
5832 F.isVarArg(), Ins,
5833 dl, DAG, InVals);
Dan Gohman5e866062009-08-06 15:37:27 +00005834
5835 // Verify that the target's LowerFormalArguments behaved as expected.
Owen Anderson825b72b2009-08-11 20:47:22 +00005836 assert(NewRoot.getNode() && NewRoot.getValueType() == MVT::Other &&
Dan Gohman5e866062009-08-06 15:37:27 +00005837 "LowerFormalArguments didn't return a valid chain!");
5838 assert(InVals.size() == Ins.size() &&
5839 "LowerFormalArguments didn't emit the correct number of values!");
Bill Wendling3ea58b62009-12-22 21:35:02 +00005840 DEBUG({
5841 for (unsigned i = 0, e = Ins.size(); i != e; ++i) {
5842 assert(InVals[i].getNode() &&
5843 "LowerFormalArguments emitted a null value!");
5844 assert(Ins[i].VT == InVals[i].getValueType() &&
5845 "LowerFormalArguments emitted a value with the wrong type!");
5846 }
5847 });
Bill Wendling3ea3c242009-12-22 02:10:19 +00005848
Dan Gohman5e866062009-08-06 15:37:27 +00005849 // Update the DAG with the new chain value resulting from argument lowering.
Dan Gohman98ca4f22009-08-05 01:29:28 +00005850 DAG.setRoot(NewRoot);
5851
5852 // Set up the argument values.
5853 unsigned i = 0;
5854 Idx = 1;
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005855 if (!FLI.CanLowerReturn) {
5856 // Create a virtual register for the sret pointer, and put in a copy
5857 // from the sret argument into it.
5858 SmallVector<EVT, 1> ValueVTs;
5859 ComputeValueVTs(TLI, PointerType::getUnqual(F.getReturnType()), ValueVTs);
5860 EVT VT = ValueVTs[0];
5861 EVT RegVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5862 ISD::NodeType AssertOp = ISD::DELETED_NODE;
Bill Wendling46ada192010-03-02 01:55:18 +00005863 SDValue ArgValue = getCopyFromParts(DAG, dl, &InVals[0], 1,
Bill Wendling3ea3c242009-12-22 02:10:19 +00005864 RegVT, VT, AssertOp);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005865
Dan Gohman2048b852009-11-23 18:04:58 +00005866 MachineFunction& MF = SDB->DAG.getMachineFunction();
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005867 MachineRegisterInfo& RegInfo = MF.getRegInfo();
5868 unsigned SRetReg = RegInfo.createVirtualRegister(TLI.getRegClassFor(RegVT));
5869 FLI.DemoteRegister = SRetReg;
Mikhail Glushenkovb3c01992010-01-01 04:41:22 +00005870 NewRoot = SDB->DAG.getCopyToReg(NewRoot, SDB->getCurDebugLoc(),
5871 SRetReg, ArgValue);
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005872 DAG.setRoot(NewRoot);
Bill Wendling3ea3c242009-12-22 02:10:19 +00005873
Kenneth Uildriksc158dde2009-11-11 19:59:24 +00005874 // i indexes lowered arguments. Bump it past the hidden sret argument.
5875 // Idx indexes LLVM arguments. Don't touch it.
5876 ++i;
5877 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005878
Dan Gohman46510a72010-04-15 01:51:59 +00005879 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005880 ++I, ++Idx) {
5881 SmallVector<SDValue, 4> ArgValues;
Owen Andersone50ed302009-08-10 22:56:29 +00005882 SmallVector<EVT, 4> ValueVTs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00005883 ComputeValueVTs(TLI, I->getType(), ValueVTs);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005884 unsigned NumValues = ValueVTs.size();
Dan Gohman98ca4f22009-08-05 01:29:28 +00005885 for (unsigned Value = 0; Value != NumValues; ++Value) {
Owen Andersone50ed302009-08-10 22:56:29 +00005886 EVT VT = ValueVTs[Value];
Owen Anderson23b9b192009-08-12 00:36:31 +00005887 EVT PartVT = TLI.getRegisterType(*CurDAG->getContext(), VT);
5888 unsigned NumParts = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohman98ca4f22009-08-05 01:29:28 +00005889
5890 if (!I->use_empty()) {
5891 ISD::NodeType AssertOp = ISD::DELETED_NODE;
5892 if (F.paramHasAttr(Idx, Attribute::SExt))
5893 AssertOp = ISD::AssertSext;
5894 else if (F.paramHasAttr(Idx, Attribute::ZExt))
5895 AssertOp = ISD::AssertZext;
5896
Bill Wendling46ada192010-03-02 01:55:18 +00005897 ArgValues.push_back(getCopyFromParts(DAG, dl, &InVals[i],
Bill Wendling3ea3c242009-12-22 02:10:19 +00005898 NumParts, PartVT, VT,
5899 AssertOp));
Dan Gohman98ca4f22009-08-05 01:29:28 +00005900 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005901
Dan Gohman98ca4f22009-08-05 01:29:28 +00005902 i += NumParts;
5903 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005904
Dan Gohman98ca4f22009-08-05 01:29:28 +00005905 if (!I->use_empty()) {
Evan Cheng8e36a5c2010-03-29 21:27:30 +00005906 SDValue Res;
5907 if (!ArgValues.empty())
5908 Res = DAG.getMergeValues(&ArgValues[0], NumValues,
5909 SDB->getCurDebugLoc());
Bill Wendling3ea3c242009-12-22 02:10:19 +00005910 SDB->setValue(I, Res);
5911
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005912 // If this argument is live outside of the entry block, insert a copy from
5913 // whereever we got it to the vreg that other BB's will reference it as.
Dan Gohman2048b852009-11-23 18:04:58 +00005914 SDB->CopyToExportRegsIfNeeded(I);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005915 }
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005916 }
Bill Wendling3ea3c242009-12-22 02:10:19 +00005917
Dan Gohman98ca4f22009-08-05 01:29:28 +00005918 assert(i == InVals.size() && "Argument register count mismatch!");
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005919
5920 // Finally, if the target has anything special to do, allow it to do so.
5921 // FIXME: this should insert code into the DAG!
Dan Gohman64652652010-04-14 20:17:22 +00005922 EmitFunctionEntryCode();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005923}
5924
5925/// Handle PHI nodes in successor blocks. Emit code into the SelectionDAG to
5926/// ensure constants are generated when needed. Remember the virtual registers
5927/// that need to be added to the Machine PHI nodes as input. We cannot just
5928/// directly add them, because expansion might result in multiple MBB's for one
5929/// BB. As such, the start of the BB might correspond to a different MBB than
5930/// the end.
5931///
5932void
Dan Gohman46510a72010-04-15 01:51:59 +00005933SelectionDAGISel::HandlePHINodesInSuccessorBlocks(const BasicBlock *LLVMBB) {
5934 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005935
5936 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
5937
5938 // Check successor nodes' PHI nodes that expect a constant to be available
5939 // from this block.
5940 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00005941 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005942 if (!isa<PHINode>(SuccBB->begin())) continue;
5943 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005944
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005945 // If this terminator has multiple identical successors (common for
5946 // switches), only handle each succ once.
5947 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00005948
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005949 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005950
5951 // At this point we know that there is a 1-1 correspondence between LLVM PHI
5952 // nodes and Machine PHI nodes, but the incoming operands have not been
5953 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00005954 for (BasicBlock::const_iterator I = SuccBB->begin();
5955 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005956 // Ignore dead phi's.
5957 if (PN->use_empty()) continue;
5958
5959 unsigned Reg;
Dan Gohman46510a72010-04-15 01:51:59 +00005960 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005961
Dan Gohman46510a72010-04-15 01:51:59 +00005962 if (const Constant *C = dyn_cast<Constant>(PHIOp)) {
Dan Gohman2048b852009-11-23 18:04:58 +00005963 unsigned &RegOut = SDB->ConstantsOut[C];
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005964 if (RegOut == 0) {
5965 RegOut = FuncInfo->CreateRegForValue(C);
Dan Gohman2048b852009-11-23 18:04:58 +00005966 SDB->CopyValueToVirtualRegister(C, RegOut);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005967 }
5968 Reg = RegOut;
5969 } else {
5970 Reg = FuncInfo->ValueMap[PHIOp];
5971 if (Reg == 0) {
5972 assert(isa<AllocaInst>(PHIOp) &&
5973 FuncInfo->StaticAllocaMap.count(cast<AllocaInst>(PHIOp)) &&
5974 "Didn't codegen value into a register!??");
5975 Reg = FuncInfo->CreateRegForValue(PHIOp);
Dan Gohman2048b852009-11-23 18:04:58 +00005976 SDB->CopyValueToVirtualRegister(PHIOp, Reg);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005977 }
5978 }
5979
5980 // Remember that this register needs to added to the machine PHI node as
5981 // the input for this MBB.
Owen Andersone50ed302009-08-10 22:56:29 +00005982 SmallVector<EVT, 4> ValueVTs;
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005983 ComputeValueVTs(TLI, PN->getType(), ValueVTs);
5984 for (unsigned vti = 0, vte = ValueVTs.size(); vti != vte; ++vti) {
Owen Andersone50ed302009-08-10 22:56:29 +00005985 EVT VT = ValueVTs[vti];
Owen Anderson23b9b192009-08-12 00:36:31 +00005986 unsigned NumRegisters = TLI.getNumRegisters(*CurDAG->getContext(), VT);
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005987 for (unsigned i = 0, e = NumRegisters; i != e; ++i)
Dan Gohman2048b852009-11-23 18:04:58 +00005988 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg+i));
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005989 Reg += NumRegisters;
5990 }
5991 }
5992 }
Dan Gohman2048b852009-11-23 18:04:58 +00005993 SDB->ConstantsOut.clear();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00005994}
5995
Dan Gohman3df24e62008-09-03 23:12:08 +00005996/// This is the Fast-ISel version of HandlePHINodesInSuccessorBlocks. It only
5997/// supports legal types, and it emits MachineInstrs directly instead of
5998/// creating SelectionDAG nodes.
5999///
6000bool
Dan Gohman46510a72010-04-15 01:51:59 +00006001SelectionDAGISel::HandlePHINodesInSuccessorBlocksFast(const BasicBlock *LLVMBB,
Dan Gohman3df24e62008-09-03 23:12:08 +00006002 FastISel *F) {
Dan Gohman46510a72010-04-15 01:51:59 +00006003 const TerminatorInst *TI = LLVMBB->getTerminator();
Dan Gohmanf0cbcd42008-09-03 16:12:24 +00006004
Dan Gohman3df24e62008-09-03 23:12:08 +00006005 SmallPtrSet<MachineBasicBlock *, 4> SuccsHandled;
Dan Gohman2048b852009-11-23 18:04:58 +00006006 unsigned OrigNumPHINodesToUpdate = SDB->PHINodesToUpdate.size();
Dan Gohman3df24e62008-09-03 23:12:08 +00006007
6008 // Check successor nodes' PHI nodes that expect a constant to be available
6009 // from this block.
6010 for (unsigned succ = 0, e = TI->getNumSuccessors(); succ != e; ++succ) {
Dan Gohman46510a72010-04-15 01:51:59 +00006011 const BasicBlock *SuccBB = TI->getSuccessor(succ);
Dan Gohman3df24e62008-09-03 23:12:08 +00006012 if (!isa<PHINode>(SuccBB->begin())) continue;
6013 MachineBasicBlock *SuccMBB = FuncInfo->MBBMap[SuccBB];
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006014
Dan Gohman3df24e62008-09-03 23:12:08 +00006015 // If this terminator has multiple identical successors (common for
6016 // switches), only handle each succ once.
6017 if (!SuccsHandled.insert(SuccMBB)) continue;
Mikhail Glushenkov5c1799b2009-01-16 06:53:46 +00006018
Dan Gohman3df24e62008-09-03 23:12:08 +00006019 MachineBasicBlock::iterator MBBI = SuccMBB->begin();
Dan Gohman3df24e62008-09-03 23:12:08 +00006020
6021 // At this point we know that there is a 1-1 correspondence between LLVM PHI
6022 // nodes and Machine PHI nodes, but the incoming operands have not been
6023 // emitted yet.
Dan Gohman46510a72010-04-15 01:51:59 +00006024 for (BasicBlock::const_iterator I = SuccBB->begin();
6025 const PHINode *PN = dyn_cast<PHINode>(I); ++I) {
Dan Gohman3df24e62008-09-03 23:12:08 +00006026 // Ignore dead phi's.
6027 if (PN->use_empty()) continue;
6028
6029 // Only handle legal types. Two interesting things to note here. First,
6030 // by bailing out early, we may leave behind some dead instructions,
6031 // since SelectionDAG's HandlePHINodesInSuccessorBlocks will insert its
6032 // own moves. Second, this check is necessary becuase FastISel doesn't
6033 // use CreateRegForValue to create registers, so it always creates
6034 // exactly one register for each non-void instruction.
Owen Andersone50ed302009-08-10 22:56:29 +00006035 EVT VT = TLI.getValueType(PN->getType(), /*AllowUnknown=*/true);
Owen Anderson825b72b2009-08-11 20:47:22 +00006036 if (VT == MVT::Other || !TLI.isTypeLegal(VT)) {
6037 // Promote MVT::i1.
6038 if (VT == MVT::i1)
Owen Anderson23b9b192009-08-12 00:36:31 +00006039 VT = TLI.getTypeToTransformTo(*CurDAG->getContext(), VT);
Dan Gohman74321ab2008-09-10 21:01:31 +00006040 else {
Dan Gohman2048b852009-11-23 18:04:58 +00006041 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman74321ab2008-09-10 21:01:31 +00006042 return false;
6043 }
Dan Gohman3df24e62008-09-03 23:12:08 +00006044 }
6045
Dan Gohman46510a72010-04-15 01:51:59 +00006046 const Value *PHIOp = PN->getIncomingValueForBlock(LLVMBB);
Dan Gohman3df24e62008-09-03 23:12:08 +00006047
6048 unsigned Reg = F->getRegForValue(PHIOp);
6049 if (Reg == 0) {
Dan Gohman2048b852009-11-23 18:04:58 +00006050 SDB->PHINodesToUpdate.resize(OrigNumPHINodesToUpdate);
Dan Gohman3df24e62008-09-03 23:12:08 +00006051 return false;
6052 }
Dan Gohman2048b852009-11-23 18:04:58 +00006053 SDB->PHINodesToUpdate.push_back(std::make_pair(MBBI++, Reg));
Dan Gohman3df24e62008-09-03 23:12:08 +00006054 }
6055 }
6056
6057 return true;
6058}